Code

- Make indices in FAIclasses numeric
[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     $ldap=$this->config->get_ldap_link();
32     $this->ui = get_userinfo();
34     /* Parse ldap attribute to get all assigned classes */
35     $tmp = split(" ",$this->FAIclass);
36     $tmp2 = array();
37     foreach($tmp as $class){
38       if(!empty($class)){
39         $class = trim($class);
40         $tmp2[] = $class;
41       }
42     }
44     /* Sort assigned classes */ 
45     if(is_array($tmp2)){
46       foreach($tmp2 as $class){
47         $this->FAIclasses[]=$class;
48       }
49     }
51     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
53     /* Build filter */
54     $filter= "";
55     foreach ($categories as $cat){
56       $filter.= "(objectClass=$cat)";
57     }
58     
59     /* Get ldap connection */ 
60     $base  = session::get('CurrentMainBase');
61     $ldap->cd($base);
62     $sort = array();
64     /* search all FAI classes */
65     $ldap->search("(|$filter)",array("*"));
66     while($attrs = $ldap->fetch()){
68       /* Sort by categorie */
69       foreach($categories as $cat){
70         if(in_array($cat,$attrs['objectClass'])){
72           /* Append entry */
73           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
74   
75           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
76           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
77         }
78       }
79     } 
81     /* Sort the sort array */
82     //ksort($sort);
84     /* Reorder the FAIclasses array */
85     foreach($sort as $name){
86       $tmp[$name] =$this->FAIAllclasses[$name];
87     }
89     /* Assign sorted classes */
90     $this->FAIAllclasses = array();
91     $this->FAIAllclasses = $tmp;
93     if($dn != "new"){
94       $this->dn =$dn;
95     }
96     $this->old_cn   = $this->cn;
97   }
99   function acl_base_for_current_object($dn)
100   {
101     if($dn == "new" || $dn == ""){
102       if($this->dn == "new"){
103         $dn= $this->parent->parent->acl_base;
104       }else{
105         $dn = $this->dn;
106       }
107     }
108     return($dn);
109   }
112   /* returns position in array */
113   function getpos($atr,$attrs)
114   {
115     $i = 0;
116     foreach($attrs as $value) {
117       if($atr == $value) {
118         return($i);
119       }
120       $i++;
121     }
122   }
124   /* Transports the given Arraykey one position up*/
125   function ArrayUp($atr,$attrs)
126   {
127     $index = $this->getpos($atr,$attrs);
128     $res = $attrs;
130     for($i=0;$i<count($attrs);$i++) {
131       if ($i==$index) {
132         $left_index = $index - 1;
133         $res[$left_index] = $attrs[$index];
134         $res[$index] = $attrs[$left_index];
135       }
136     }
138     return($res);
139   }
142   /* Transports the given Arraykey one position down*/
143   function ArrayDown($atr,$attrs)
144   {
145     $index = $this->getpos($atr,$attrs);
146     $res = $attrs;
148     for($i=0;$i<count($attrs);$i++) {
149       if ($i==$index) {
150         $left_index = ($index+1);
151         $res[$left_index] = $attrs[$index];
152         $res[$index] = $attrs[$left_index];
153       }
154     }
156     return($res);
157   }
159   /* class one position up */
160   function catUp($id)
161   {
162     /* Get all cats depinding on current dir */
163     $cats = $this->FAIclasses;
164     $this->FAIclasses =$this->ArrayUp($id,$cats);
165   }
167   /* Class one position down */
168   function catDown($id)
169   {
170     /* Get all cats depinding on current dir */
171     $cats = $this->FAIclasses;
172     $this->FAIclasses =$this->ArrayDown($id,$cats);
173   }
175   function execute()
176   {
177     /* Call parent execute */
178     plugin::execute();
180     if($this->is_account && !$this->view_logged){
181       $this->view_logged = TRUE;
182       new log("view","fai/".get_class($this),$this->dn);
183     }
185     /* Fill templating stuff */
186     $smarty= get_smarty();
187     $display= "";
189     $s_entry = "";
190     $s_action = "";
192     /* Remove class name From list */
193     $sort_once = false;
194     if(!preg_match("/freeze/",$this->FAIstate)){
195       foreach($_POST as $name => $post){
196         if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
197           $s_action = "delete";
198           $s_entry  = preg_replace("/DEL_/","",$name);
199           $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
200         }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
201           $s_action  = "add";
202         }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
203           $s_action  = "delete";
204           $s_entry = $_POST['FAIclass'];
205         }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
206           $s_action  = "add";
207         }
209         /* Check if a list element should be pushed one position up */
210         if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
211           $sort_once = true;
212           $val = preg_replace("/sortup_/","",$name);
213           $val = preg_replace("/_.*$/","",$val);
214           $val = base64_decode($val);
215           $this->catUp($val);
216         }
218         /* Check if a list element should be pushed one position down */
219         if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
220           $sort_once = true;
221           $val = preg_replace("/sortdown_/","",$name);
222           $val = preg_replace("/_.*$/","",$val);
223           $val = base64_decode($val);
224           $this->catDown($val);
225         }
227       }
229       if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
230         unset($this->FAIclasses[$s_entry]);
231       }
233       if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
234         $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses,$this->parent->parent->fai_release);
235         $this->is_dialog  =true;
236       }
238       /* Save Dialog */
239       if(isset($_POST['SaveSubObject'])){
240         $this->dialog->save_object();
241         $msgs= $this->dialog->check();
242         if(count($msgs)){
243           msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
244         }else{
245           $ret = $this->dialog->save();
246           foreach($ret as $class){
247             $this->FAIclasses[] =$class; 
248           }
249           $this->is_dialog=false;
250           unset($this->dialog);
251           $this->dialog=FALSE;
252           //ksort($this->FAIclasses);
253         }
254       }
255     }
257     /* Cancel Dialog */
258     if(isset($_POST['CancelSubObject'])){
259       $this->is_dialog=false;
260       unset($this->dialog);
261       $this->dialog=FALSE;
262     }
264     if(is_object($this->dialog)){
265       $this->dialog->save_object();
266       return($this->dialog->execute());
267     }
269     $divlist  =new divSelectBox("Profile");
270     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
272     /* item images */
273     $objTypes['FAIhook']            = "<img src='plugins/fai/images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
274     $objTypes['FAItemplate']        = "<img src='plugins/fai/images/fai_template.png' title='"._("Template bundle")."' alt=''>";
275     $objTypes['FAIscript']          = "<img src='plugins/fai/images/fai_script.png' title='"._("Script bundle")."' alt=''>";
276     $objTypes['FAIvariable']        = "<img src='plugins/fai/images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
277     $objTypes['FAIpackageList']        = "<img src='plugins/fai/images/fai_packages.png' title='"._("Package bundle")."' alt=''>";
278     $objTypes['FAIpartitionTable']  = "<img src='plugins/fai/images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
280     /* Delete button */
281     $actions = "<input type='image' src='images/lists/trash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
282     
283     /* Up down buttons */
284     $linkup = " &nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/lists/sort-up.png' align='top' >";
285     $linkdown = " &nbsp;<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/lists/sort-down.png' >";
286     /* Append fai classes to divlist */
287     if($this->acl_is_readable("FAIclass")){
288       $i = 1;
289       foreach($this->FAIclasses as $usedClass){
290         if($i==1) {
291           $linkupdown = "&nbsp;&nbsp;&nbsp;&nbsp;" . $linkdown;
292         } elseif ($i<count($this->FAIclasses)) {
293           $linkupdown = $linkup . $linkdown;    
294         }
295         else {
296           $linkupdown = $linkup;
297         }
299         $str = "&nbsp;";
300         $act = "";
302         if(isset($this->FAIAllclasses[$usedClass])){
303           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
304             $str.= $objTypes[$class]; 
305           }
306         }
308         $field1 = array("string"=> $usedClass,"attach"=>"");
309         $field2 = array("string"=> $str,"attach"=>"");
310         if(!preg_match("/freeze/", $this->FAIstate) && $this->acl_is_writeable("FAIclass")){
311           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
312               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
313               "attach"=>"style='border-right:none;'");
314         }else{
315           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
316         }
317         $divlist->AddEntry(array($field1,$field2,$field3));
318         $i++;
319       }
320     }
322     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
324     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
325     $smarty->assign("divlist"     ,$divlist->DrawList());
327     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
328      * If we post the escaped strings they will be escaped again
329      */
330     foreach($this->attributes as $attrs){
331       if(get_magic_quotes_gpc()){
332         $smarty->assign($attrs,stripslashes($this->$attrs));
333       }else{
334         $smarty->assign($attrs,($this->$attrs));
335       }
336     }
338     
339     $dn = $this->acl_base_for_current_object($this->dn);
340     $smarty->assign("sub_object_is_addable",
341         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
342         !preg_match("/freeze/",$this->FAIstate));
344     $tmp = $this->plInfo();
345     foreach($tmp['plProvidedAcls'] as $name => $translated){
346       $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
347     }
349     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
350     return($display);
351   }
353   function remove_from_parent()
354   {
355     $ldap = $this->config->get_ldap_link();
356     $ldap->cd ($this->dn);
357     $release = $this->parent->parent->fai_release;
358     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
359     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
360     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
361     $this->handle_post_events("remove");    
362   }
365   /* Save data to object 
366    */
367   function save_object()
368   {
369     if(!preg_match("/freeze/",$this->FAIstate)){
370       plugin::save_object();
371     }
372   }
375   /* Check supplied data */
376   function check()
377   {
378     /* Call common method to give check the hook */
379     $message= plugin::check();
381     if(count($this->FAIclasses) == 0){
382       $message[]=_("No class specified for this profile!");
383     }
385     if($this->cn == ""){
386       $message[]= msgPool::required(_("Name"));
387     }
389     /* Ensure that we do not overwrite an allready existing entry 
390      */
391     if($this->dn == "new" || $this->cn != $this->old_cn){
392       $release = $this->parent->parent->fai_release;
393       $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
394       $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
395       if(isset($res[$this->cn])){
396         $message[] = msgPool::duplicated(_("Name"));
397       }
398     }
400     return ($message);
401   }
404   /* Save to LDAP */
405   function save()
406   {
407     plugin::save();
409     $ldap = $this->config->get_ldap_link();
411     $this->FAIclass = "";
412     foreach($this->FAIclasses as $class){
413       $this->FAIclass.=$class." ";
414     }
416     $this->attrs['FAIclass']=trim($this->FAIclass);
418     /* Remove the old FAI profile, if the dn has changed.
419      */
420     if($this->cn != $this->old_cn && $this->old_cn != ""){
422       $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
423       FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
424     }
425     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
426    
427     if($this->initially_was_account){
428       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
429     }else{
430       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
431     }
432   }
435   function PrepareForCopyPaste($source)
436   {
437     plugin::PrepareForCopyPaste($source);
439     /* Parse ldap attribute to get all assigned classes */
440     $tmp = split(" ",$this->FAIclass);
441     $tmp2 = array();
442     foreach($tmp as $class){
443       if(!empty($class)){
444         $class = trim($class);
445         $tmp2[] = $class;
446       }
447     }
449     /* Sort assigned classes */
450     if(is_array($tmp2)){
451       foreach($tmp2 as $class){
452         $this->FAIclasses[]=$class;
453       }
454     }
455   }
458   /* Return plugin informations for acl handling */ 
459   static function plInfo()
460   {
461     return (array( 
462           "plShortName" => _("Profile"),
463           "plDescription" => _("FAI profile"),
464           "plSelfModify"  => FALSE,
465           "plDepends"     => array(),
466           "plPriority"    => 30,
467           "plSection"     => array("administration"),
468           "plCategory"    => array("fai"),
469           "plProvidedAcls" => array(
470             "cn"                => _("Name"),
471             "description"       => _("Description"),
472             "FAIclass"          => _("FAI classes"))
473           ));
474   }
477   /*! \brief  Used for copy & paste.
478               Returns a HTML input mask, which allows to change the cn of this entry.
479       @param  Array   Array containing current status && a HTML template.
480    */
481   function getCopyDialog()
482   {
483     $vars = array("cn");
484     $smarty = get_smarty();
485     $smarty->assign("cn", htmlentities($this->cn));
486     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
487     $ret = array();
488     $ret['string'] = $str;
489     $ret['status'] = "";
490     return($ret);
491   }
494   /*! \brief  Used for copy & paste.
495               Some entries must be renamed to avaoid duplicate entries.
496    */
497   function saveCopyDialog()
498   {
499     if(isset($_POST['cn'])){
500       $this->cn = get_post('cn');
501     }
502   }
505 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
506 ?>