Code

Apply fix for #3750
[gosa.git] / trunk / gosa-plugins / fai / admin / fai / class_faiProfile.inc
1 <?php
3 class faiProfile extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description","FAIclass");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAIprofile");
14   /* Specific attributes */
15   var $old_cn           = "";
16   var $cn               = "";       // The class name for this object
17   var $description      = "";       // The description for this set of partitions
18   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
19   var $FAIclass         = "";       // Classnames used for this profile
20   var $FAIclasses       = array();  // Contains classname seperated in an array
21   var $FAIAllclasses    = array();  // Contains all possible Classnames
22   var $ui               ;
23   var $FAIstate      = "";
24   var $view_logged      = FALSE;
26   function faiProfile(&$config, $dn= NULL)
27   {
28     /* Load Attributes */
29     plugin::plugin ($config, $dn);
30     
31     $this->ui = get_userinfo();
33     /* Parse ldap attribute to get all assigned classes */
34     $tmp = split(" ",$this->FAIclass);
35     $tmp2 = array();
36     foreach($tmp as $class){
37       if(!empty($class)){
38         $class = trim($class);
39         $tmp2[] = $class;
40       }
41     }
43     /* Sort assigned classes */ 
44     if(is_array($tmp2)){
45       foreach($tmp2 as $class){
46         $this->FAIclasses[]=$class;
47       }
48     }
51     if($dn != "new"){
52       $this->dn =$dn;
53     }
54     $this->old_cn   = $this->cn;
55   }
57   function get_all_fai_classes()
58   {
59     if (count($this->FAIAllclasses)) {
60       return;
61     }
62     
63     $ldap=$this->config->get_ldap_link();
65     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
67     /* Build filter */
68     $filter= "";
69     foreach ($categories as $cat){
70       $filter.= "(objectClass=$cat)";
71     }
72     
73     $sort = array();
74     /* search all FAI classes */
75     $res = FAI::get_all_objects_for_given_base($this->parent->parent->fai_release, "(|$filter)", true);
76     foreach($res as $attrs){
77       /* Sort by categorie */
78       foreach($categories as $cat){
79         if(in_array($cat,$attrs['objectClass'])){
81           /* Append entry */
82           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
83   
84           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
85           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
86         }
87       }
88     } 
90     /* Sort the sort array */
91     //ksort($sort);
93     /* Reorder the FAIclasses array */
94     foreach($sort as $name){
95       $tmp[$name] =$this->FAIAllclasses[$name];
96     }
98     /* Assign sorted classes */
99     $this->FAIAllclasses = array();
100     $this->FAIAllclasses = $tmp;
101   }
103   function acl_base_for_current_object($dn)
104   {
105     if($dn == "new" || $dn == ""){
106       if($this->dn == "new"){
107         $dn= $this->parent->parent->acl_base;
108       }else{
109         $dn = $this->dn;
110       }
111     }
112     return($dn);
113   }
116   /* returns position in array */
117   function getpos($atr,$attrs)
118   {
119     $i = 0;
120     foreach($attrs as $value) {
121       if($atr == $value) {
122         return($i);
123       }
124       $i++;
125     }
126   }
128   /* Transports the given Arraykey one position up*/
129   function ArrayUp($atr,$attrs)
130   {
131     $index = $this->getpos($atr,$attrs);
132     $res = $attrs;
134     /* Don't do anything if there is just one element */
135     if (count($attrs) == 1) {
136       return($res);
137     }
139     for($i=0;$i<count($attrs);$i++) {
140       if ($i==$index) {
141         $left_index = $index - 1;
142         $res[$left_index] = $attrs[$index];
143         $res[$index] = $attrs[$left_index];
144       }
145     }
147     return($res);
148   }
151   /* Transports the given Arraykey one position down*/
152   function ArrayDown($atr,$attrs)
153   {
154     $index = $this->getpos($atr,$attrs);
155     $res = $attrs;
157     /* Don't do anything if there is just one element */
158     if (count($attrs) == 1) {
159       return($res);
160     }
162     for($i=0;$i<count($attrs);$i++) {
163       if ($i==$index) {
164         $left_index = ($index+1);
165         $res[$left_index] = $attrs[$index];
166         $res[$index] = $attrs[$left_index];
167       }
168     }
170     return($res);
171   }
173   /* class one position up */
174   function catUp($id)
175   {
176     /* Get all cats depinding on current dir */
177     $cats = $this->FAIclasses;
178     $this->FAIclasses =$this->ArrayUp($id,$cats);
179   }
181   /* Class one position down */
182   function catDown($id)
183   {
184     /* Get all cats depinding on current dir */
185     $cats = $this->FAIclasses;
186     $this->FAIclasses =$this->ArrayDown($id,$cats);
187   }
189   function execute()
190   {
191     /* Call parent execute */
192     plugin::execute();
194     if($this->is_account && !$this->view_logged){
195       $this->view_logged = TRUE;
196       new log("view","fai/".get_class($this),$this->dn);
197     }
199     /* Fill templating stuff */
200     $smarty= get_smarty();
201     $display= "";
203     $s_entry = "";
204     $s_action = "";
206     /* Remove class name From list */
207     $sort_once = false;
208     if(!preg_match("/freeze/",$this->FAIstate)){
209       foreach($_POST as $name => $post){
210         if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
211           $s_action = "delete";
212           $s_entry  = preg_replace("/DEL_/","",$name);
213           $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
214         }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
215           $s_action  = "add";
216         }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
217           $s_action  = "delete";
218           $s_entry = $_POST['FAIclass'];
219         }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
220           $s_action  = "add";
221         }
223         /* Check if a list element should be pushed one position up */
224         if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
225           $sort_once = true;
226           $val = preg_replace("/sortup_/","",$name);
227           $val = preg_replace("/_.*$/","",$val);
228           $val = base64_decode($val);
229           $this->catUp($val);
230         }
232         /* Check if a list element should be pushed one position down */
233         if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
234           $sort_once = true;
235           $val = preg_replace("/sortdown_/","",$name);
236           $val = preg_replace("/_.*$/","",$val);
237           $val = base64_decode($val);
238           $this->catDown($val);
239         }
241       }
243       if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
244         $newfaiclasses = array();
245         foreach($this->FAIclasses as $key => $name) {
246           if ($name != $s_entry) {
247             array_push($newfaiclasses, $name);
248           }
249         }
250         $this->FAIclasses = $newfaiclasses;
251       }
253       if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
254         $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses,$this->parent->parent->fai_release);
255         $this->is_dialog  =true;
256       }
258       /* Save Dialog */
259       if(isset($_POST['SaveSubObject'])){
260         $this->dialog->save_object();
261         $msgs= $this->dialog->check();
262         if(count($msgs)){
263           msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
264         }else{
265           $ret = $this->dialog->save();
266           foreach($ret as $class){
267             $this->FAIclasses[] =$class; 
268           }
269           $this->is_dialog=false;
270           unset($this->dialog);
271           $this->dialog=FALSE;
272           //ksort($this->FAIclasses);
273         }
274       }
275     }
277     /* Cancel Dialog */
278     if(isset($_POST['CancelSubObject'])){
279       $this->is_dialog=false;
280       unset($this->dialog);
281       $this->dialog=FALSE;
282     }
284     if(is_object($this->dialog)){
285       $this->dialog->save_object();
286       return($this->dialog->execute());
287     }
289     $divlist  =new divSelectBox("Profile");
290     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
292     /* item images */
293     $objTypes['FAIhook']            = "<img src='plugins/fai/images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
294     $objTypes['FAItemplate']        = "<img src='plugins/fai/images/fai_template.png' title='"._("Template bundle")."' alt=''>";
295     $objTypes['FAIscript']          = "<img src='plugins/fai/images/fai_script.png' title='"._("Script bundle")."' alt=''>";
296     $objTypes['FAIvariable']        = "<img src='plugins/fai/images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
297     $objTypes['FAIpackageList']        = "<img src='plugins/fai/images/fai_packages.png' title='"._("Package bundle")."' alt=''>";
298     $objTypes['FAIpartitionTable']  = "<img src='plugins/fai/images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
300     /* Delete button */
301     $actions = "<input type='image' src='images/lists/trash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
302     
303     /* Up down buttons */
304     $linkup = " &nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/lists/sort-up.png' align='top' >";
305     $linkdown = " &nbsp;<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/lists/sort-down.png' >";
306     /* Append fai classes to divlist */
307     if($this->acl_is_readable("FAIclass")){
308       $i = 1;
309       $count = count($this->FAIclasses);
311       $linkupdown = '';
312       foreach($this->FAIclasses as $usedClass){
313         if($i==1) {
314           if ($count > 1) {
315             $linkupdown = "&nbsp;&nbsp;&nbsp;&nbsp;" . $linkdown;
316           }
317         } elseif ($i<$count) {
318           $linkupdown = $linkup . $linkdown;    
319         }
320         else {
321           $linkupdown = $linkup;
322         }
324         $str = "&nbsp;";
325         $act = "";
327         $this->get_all_fai_classes();
329         if(isset($this->FAIAllclasses[$usedClass])){
330           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
331             $str.= $objTypes[$class]; 
332           }
333         }
335         $field1 = array("string"=> $usedClass,"attach"=>"");
336         $field2 = array("string"=> $str,"attach"=>"");
337         if(!preg_match("/freeze/", $this->FAIstate) && $this->acl_is_writeable("FAIclass")){
338           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
339               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
340               "attach"=>"style='border-right:none;'");
341         }else{
342           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
343         }
344         $divlist->AddEntry(array($field1,$field2,$field3));
345         $i++;
346       }
347     }
349     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
351     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
352     $smarty->assign("divlist"     ,$divlist->DrawList());
354     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
355      * If we post the escaped strings they will be escaped again
356      */
357     foreach($this->attributes as $attrs){
358       if(get_magic_quotes_gpc()){
359         $smarty->assign($attrs,stripslashes($this->$attrs));
360       }else{
361         $smarty->assign($attrs,($this->$attrs));
362       }
363     }
365     
366     $dn = $this->acl_base_for_current_object($this->dn);
367     $smarty->assign("sub_object_is_addable",
368         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
369         !preg_match("/freeze/",$this->FAIstate));
371     $tmp = $this->plInfo();
372     foreach($tmp['plProvidedAcls'] as $name => $translated){
373       $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
374     }
376     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
377     return($display);
378   }
380   function remove_from_parent()
381   {
382     $ldap = $this->config->get_ldap_link();
383     $ldap->cd ($this->dn);
384     $release = $this->parent->parent->fai_release;
385     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
386     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
387     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
388     $this->handle_post_events("remove");    
389   }
392   /* Save data to object 
393    */
394   function save_object()
395   {
396     if(!preg_match("/freeze/",$this->FAIstate)){
397       plugin::save_object();
398     }
399   }
402   /* Check supplied data */
403   function check()
404   {
405     /* Call common method to give check the hook */
406     $message= plugin::check();
408     if(count($this->FAIclasses) == 0){
409       $message[]=_("No class specified for this profile!");
410     }
412     if($this->cn == ""){
413       $message[]= msgPool::required(_("Name"));
414     }
416     /* Ensure that we do not overwrite an allready existing entry 
417      */
418     if($this->dn == "new" || $this->cn != $this->old_cn){
419       $release = $this->parent->parent->fai_release;
420       $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
421       $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
422       if(isset($res[$this->cn])){
423         $message[] = msgPool::duplicated(_("Name"));
424       }
425     }
427     return ($message);
428   }
431   /* Save to LDAP */
432   function save()
433   {
434     plugin::save();
436     $ldap = $this->config->get_ldap_link();
438     $this->FAIclass = "";
439     foreach($this->FAIclasses as $class){
440       $this->FAIclass.=$class." ";
441     }
443     $this->attrs['FAIclass']=trim($this->FAIclass);
445     /* Remove the old FAI profile, if the dn has changed.
446      */
447     if($this->cn != $this->old_cn && $this->old_cn != ""){
449       $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
450       FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
451     }
452     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
453    
454     if($this->initially_was_account){
455       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
456     }else{
457       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
458     }
459   }
462   function PrepareForCopyPaste($source)
463   {
464     plugin::PrepareForCopyPaste($source);
466     /* Parse ldap attribute to get all assigned classes */
467     $tmp = split(" ",$this->FAIclass);
468     $tmp2 = array();
469     foreach($tmp as $class){
470       if(!empty($class)){
471         $class = trim($class);
472         $tmp2[] = $class;
473       }
474     }
476     /* Sort assigned classes */
477     if(is_array($tmp2)){
478       foreach($tmp2 as $class){
479         $this->FAIclasses[]=$class;
480       }
481     }
482   }
485   /* Return plugin informations for acl handling */ 
486   static function plInfo()
487   {
488     return (array( 
489           "plShortName" => _("Profile"),
490           "plDescription" => _("FAI profile"),
491           "plSelfModify"  => FALSE,
492           "plDepends"     => array(),
493           "plPriority"    => 30,
494           "plSection"     => array("administration"),
495           "plCategory"    => array("fai"),
496           "plProvidedAcls" => array(
497             "cn"                => _("Name"),
498             "description"       => _("Description"),
499             "FAIclass"          => _("FAI classes"))
500           ));
501   }
504   /*! \brief  Used for copy & paste.
505               Returns a HTML input mask, which allows to change the cn of this entry.
506       @param  Array   Array containing current status && a HTML template.
507    */
508   function getCopyDialog()
509   {
510     $vars = array("cn");
511     $smarty = get_smarty();
512     $smarty->assign("cn", htmlentities($this->cn));
513     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
514     $ret = array();
515     $ret['string'] = $str;
516     $ret['status'] = "";
517     return($ret);
518   }
521   /*! \brief  Used for copy & paste.
522               Some entries must be renamed to avaoid duplicate entries.
523    */
524   function saveCopyDialog()
525   {
526     if(isset($_POST['cn'])){
527       $this->cn = get_post('cn');
528     }
529   }
532 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
533 ?>