1 <?php
3 class faiTemplate extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAItemplate");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAItemplateEntry";
21 var $subClasses = array("top","FAIclass","FAItemplateEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiTemplateEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAItemplatePath","FAIowner","FAImode");
28 var $sub_Load_Later = array("FAItemplateFile");
29 var $sub64coded = array();
30 var $subBinary = array("FAItemplateFile");
32 /* Specific attributes */
33 var $cn = ""; // The class name for this object
34 var $description = ""; // The description for this set of partitions
35 var $is_dialog = false; // specifies which buttons will be shown to save or abort
36 var $dialog = NULL; // a dialog, e.g. new disk dialog
37 var $SubObjects = array(); // All leafobjects of this object
39 var $FAIstate = "";
40 var $ui;
42 function faiTemplate ($config, $dn= NULL)
43 {
44 /* Load Attributes */
45 plugin::plugin ($config, $dn);
47 /* If "dn==new" we try to create a new entry
48 * Else we must read all objects from ldap which belong to this entry.
49 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
50 */
51 if($dn != "new"){
52 $this->dn =$dn;
54 /* Get FAIstate
55 */
56 if(isset($this->attrs['FAIstate'][0])){
57 $this->FAIstate = $this->attrs['FAIstate'][0];
58 }
60 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
61 */
62 $ldap = $this->config->get_ldap_link();
63 $ldap->cd ($this->dn);
65 $attrs_to_search = $this->subAttributes;
66 $attrs_to_search[] = "FAIstate";
67 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
69 while($object = $ldap->fetch()){
70 /* Set status for save management */
71 $objects = array();
72 $objects['status'] = "FreshLoaded";
73 $objects['dn'] = $object['dn'];
74 $objects = $this->get_object_attributes($objects,$this->subAttributes);
75 $this->SubObjects[$objects['cn']] = $objects;
76 }
77 }
78 $this->ui = get_userinfo();
79 }
82 /* Reload some attributes */
83 function get_object_attributes($object,$attributes)
84 {
85 $ldap = $this->config->get_ldap_link();
86 $ldap->cd($this->config->current['BASE']);
87 $ldap->cat($object['dn'],$attributes);
88 $tmp = $ldap->fetch();
90 foreach($attributes as $attrs){
91 if(isset($tmp[$attrs][0])){
92 $var = $tmp[$attrs][0];
94 /* Check if we must decode some attributes */
95 if(in_array_ics($attrs,$this->sub64coded)){
96 $var = base64_decode($var);
97 }
99 /* check if this is a binary entry */
100 if(in_array_ics($attrs,$this->subBinary)){
101 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
102 }
104 $var = stripslashes($var);
106 $object[$attrs] = $var;
107 }
108 }
109 return($object);
110 }
113 function execute()
114 {
115 /* Call parent execute */
116 plugin::execute();
118 /* Fill templating stuff */
119 $smarty= get_smarty();
120 $display= "";
122 /* New Listhandling
123 */
124 $once = true;
125 foreach($_POST as $name => $value){
126 if(preg_match("/^editscript_/",$name)&&($once)){
127 $once = false;
128 $entry = preg_replace("/^editscript_/","",$name);
129 $entry = base64_decode(preg_replace("/_.*/","",$entry));
130 $obj = $this->SubObjects[$entry];
132 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
133 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
134 if(preg_match("/r/",$acl)){
135 if($obj['status'] == "FreshLoaded"){
136 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
137 }
138 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
139 $_SESSION['objectinfo'] = $obj['dn'];
140 $this->dialog->parent = &$this;
141 $this->is_dialog=true;
142 }
143 }
144 if(preg_match("/^deletescript_/",$name)&&($once)){
145 $once = false;
146 $entry = preg_replace("/^deletescript_/","",$name);
147 $entry = base64_decode(preg_replace("/_.*/","",$entry));
148 $obj = $this->SubObjects[$entry];
150 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
151 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
152 if(preg_match("/d/",$acl)){
153 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
154 $this->SubObjects[$entry]['status']= "delete";
155 }else{
156 unset($this->SubObjects[$entry]);
157 }
158 }
159 }
160 }
162 /* Add new sub object */
163 if(isset($_POST['AddSubObject'])){
164 $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
165 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
167 if(preg_match("/c/",$acl)){
168 $this->dialog= new $this->subClassName($this->config,"new");
169 $this->is_dialog=true;
170 }
171 }
173 if($this->dn != "new"){
174 $_SESSION['objectinfo']= $this->dn;
175 }
177 /* Save Dialog */
178 if(isset($_POST['SaveSubObject'])){
179 $this->dialog->save_object();
180 $msgs = $this->dialog->check();
181 if(count($msgs)>0){
182 foreach($msgs as $msg){
183 print_red($msg);
184 }
185 }else{
186 /* Get return object */
187 $obj = $this->dialog->save();
188 if(isset($obj['remove'])){
190 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
192 /* Depending on status, set new status */
193 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
194 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
195 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
196 unset($this->SubObjects[$obj['remove']['from']]);
197 }
198 $obj['status'] = "new";
199 $this->SubObjects[$obj['remove']['to']] = $obj;
200 unset($this->SubObjects[$obj['remove']['to']]['remove']);
201 }else{
202 if($obj['status'] == "FreshLoaded"){
203 $obj['status'] = "edited";
204 }
205 $this->SubObjects[$obj['cn']]=$obj;
206 }
207 $this->is_dialog=false;
208 unset($this->dialog);
209 $this->dialog=NULL;
210 }
211 }
213 /* Sort entries */
214 $tmp = $keys = array();
215 foreach($this->SubObjects as $key => $entry){
216 $keys[$key]=$key;
217 }
218 natcasesort($keys);
219 foreach($keys as $key){
220 $tmp[$key]=$this->SubObjects[$key];
221 }
222 $this->SubObjects = $tmp;
224 /* Cancel Dialog */
225 if(isset($_POST['CancelSubObject'])){
226 $this->is_dialog=false;
227 unset($this->dialog);
228 $this->dialog=NULL;
229 }
231 /* Print dialog if $this->dialog is set */
232 if($this->dialog){
233 $this->dialog->save_object();
234 $display = $this->dialog->execute();
235 return($display);
236 }
238 /* Divlist Containing FAItemplates */
239 $divlist = new divSelectBox("FAItemplates");
240 $divlist->setHeight(400);
242 $tmp = $this->getList(true);
244 foreach($this->SubObjects as $key => $name){
246 $dn = $this->acl_base_for_current_object($name['dn']);
247 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
248 $act = "";
250 /* Hide delete icon if this object is freezed */
251 if(preg_match("/freeze/",$this->FAIstate)){
252 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
253 }else{
254 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
255 if(preg_match("/d/",$acl)){
256 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
257 }
258 }
260 /* Check acls for download icon */
261 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
262 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
263 $down = "";
264 }else{
265 $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
266 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
267 </a>";
268 }
270 /* Check if we are allowed to view this object */
271 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
272 if(preg_match("/r/",$s_acl)){
273 $divlist->AddEntry(array( array("string"=> $tmp[$key] ),
274 array("string"=>$down , "attach" => "style='width:20px;'"),
275 array("string"=>str_replace("%s",base64_encode($key),$act),
276 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
277 }
278 }
279 $smarty->assign("Entry_divlist",$divlist->DrawList());
280 /* Divlist creation complete
281 */
283 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
284 * If we post the escaped strings they will be escaped again
285 */
286 foreach($this->attributes as $attrs){
287 if(get_magic_quotes_gpc()){
288 $smarty->assign($attrs,stripslashes($this->$attrs));
289 }else{
290 $smarty->assign($attrs,($this->$attrs));
291 }
292 }
294 $dn = $this->acl_base_for_current_object($this->dn);
295 $smarty->assign("sub_object_is_addable",
296 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
297 !preg_match("/freeze/",$this->FAIstate));
299 foreach($this->attributes as $attr){
300 $smarty->assign($attr."ACL",$this->getacl($attr));
301 }
303 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
304 return($display);
305 }
308 function acl_base_for_current_object($dn)
309 {
310 if($dn == "new"){
311 if($this->dn == "new"){
312 $dn= $_SESSION['CurrentMainBase'];
313 }else{
314 $dn = $this->dn;
315 }
316 }
317 return($dn);
318 }
321 /* Generate listbox friendly SubObject list
322 */
323 function getList(){
324 $a_return=array();
325 foreach($this->SubObjects as $obj){
326 if($obj['status'] != "delete"){
328 if((isset($obj['description']))&&(!empty($obj['description']))){
329 if(strlen($obj['description']) > 40){
330 $obj['description'] = substr($obj['description'],0,40)."...";
331 }
332 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
333 }else{
334 $a_return[$obj['cn']]= $obj['cn'];
335 }
336 }
337 }
338 return($a_return);
339 }
341 /* Delete me, and all my subtrees
342 */
343 function remove_from_parent()
344 {
345 $ldap = $this->config->get_ldap_link();
346 $ldap->cd ($this->dn);
348 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
349 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
350 if($_SESSION['faifilter']['branch'] == "main"){
351 $use_dn = $this->dn;
352 }
354 prepare_to_save_FAI_object($use_dn,array(),true);
356 foreach($this->SubObjects as $name => $obj){
357 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
358 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
359 if($_SESSION['faifilter']['branch'] == "main"){
360 $use_dn = $obj['dn'];
361 }
362 prepare_to_save_FAI_object($use_dn,array(),true);
363 }
364 $this->handle_post_events("remove");
365 }
368 /* Save data to object
369 */
370 function save_object()
371 {
372 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
373 plugin::save_object();
374 }
375 }
378 /* Check supplied data */
379 function check()
380 {
381 /* Call common method to give check the hook */
382 $message= plugin::check();
384 return ($message);
385 }
388 /* Save to LDAP */
389 function save()
390 {
391 plugin::save();
393 $ldap = $this->config->get_ldap_link();
395 prepare_to_save_FAI_object($this->dn,$this->attrs);
396 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
398 /* Do object tagging */
399 $this->handle_object_tagging();
401 /* Prepare FAIscriptEntry to write it to ldap
402 * First sort array.
403 * Because we must delete old entries first.
404 * After deletion, we perform add and modify
405 */
406 $Objects = array();
407 foreach($this->SubObjects as $name => $obj){
408 if($obj['status'] == "FreshLoaded"){
409 unset($this->SubObjects[$name]);
410 }
411 }
413 foreach($this->SubObjects as $name => $obj){
414 if($obj['status'] == "delete"){
415 $Objects[$name] = $obj;
416 }
417 }
418 foreach($this->SubObjects as $name => $obj){
419 if($obj['status'] != "delete"){
420 $Objects[$name] = $obj;
421 }
422 }
424 foreach($Objects as $name => $obj){
426 foreach($this->sub64coded as $codeIt){
427 $obj[$codeIt]=base64_encode($obj[$codeIt]);
428 }
429 $tmp = array();
430 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
431 foreach($attributes as $attrs){
432 if(empty($obj[$attrs])){
433 $obj[$attrs] = array();
434 }
435 $tmp[$attrs] = stripslashes($obj[$attrs]);
436 }
438 $tmp['objectClass'] = $this->subClasses;
440 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
442 if($obj['status']=="new"){
443 $ldap->cat($sub_dn,array("objectClass"));
444 if($ldap->count()){
445 $obj['status']="edited";
446 }
447 }
449 /* Check if gosaAdministrativeUnitTag is required as object class */
450 if($obj['status'] == "edited"){
451 $ldap->cat($sub_dn,array("objectClass"));
452 $attrs = $ldap->fetch();
453 if(isset($attrs['objectClass'])){
454 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
455 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
456 }
457 }
458 }
460 if($obj['status'] == "delete"){
461 prepare_to_save_FAI_object($sub_dn,array(),true);
462 $this->handle_post_events("remove");
463 }elseif($obj['status'] == "edited"){
464 prepare_to_save_FAI_object($sub_dn,$tmp);
465 $this->handle_post_events("modify");
466 }elseif($obj['status']=="new"){
467 prepare_to_save_FAI_object($sub_dn,$tmp);
468 $this->handle_post_events("add");
469 }
472 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
473 }
474 }
477 /* Return plugin informations for acl handling */
478 function plInfo()
479 {
480 return (array(
481 "plShortName" => _("Template"),
482 "plDescription" => _("FAI template"),
483 "plSelfModify" => FALSE,
484 "plDepends" => array(),
485 "plPriority" => 24,
486 "plSection" => array("administration"),
487 "plCategory" => array("fai"),
488 "plProvidedAcls" => array(
489 "cn" => _("Name")." ("._("Readonly").")",
490 "description" => _("Description"))
491 ));
492 }
493 }
495 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
496 ?>