Code

db587ac06f055f69019b88dab17e8760664771e2
[gosa.git] / 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   var $classSelect;
28   function faiProfile(&$config, $dn= NULL)
29   {
30     /* Load Attributes */
31     plugin::plugin ($config, $dn);
32     $ldap=$this->config->get_ldap_link();
34     $this->ui = get_userinfo();
36     /* Parse ldap attribute to get all assigned classes */
37     $tmp = explode(" ",$this->FAIclass);
38     $tmp2 = array();
39     foreach($tmp as $class){
40       if(!empty($class)){
41         $tmp2[trim($class)] = trim($class);
42       }
43     }
45     /* Sort assigned classes */ 
46     if(is_array($tmp2)){
47       foreach($tmp2 as $class){
48         $this->FAIclasses[$class]=$class;
49       }
50     }
52     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
54     /* Build filter */
55     $filter= "";
56     foreach ($categories as $cat){
57       $filter.= "(objectClass=$cat)";
58     }
59     
60     /* Get ldap connection */ 
61     $base  = session::get('CurrentMainBase');
62     $ldap->cd($base);
63     $sort = array();
65     /* search all FAI classes */
66     $ldap->search("(|$filter)",array("*"));
67     while($attrs = $ldap->fetch()){
69       /* Sort by categorie */
70       foreach($categories as $cat){
71         if(in_array($cat,$attrs['objectClass'])){
73           /* Append entry */
74           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
75   
76           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
77           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
78         }
79       }
80     } 
82     /* Sort the sort array */
83     //ksort($sort);
85     /* Reorder the FAIclasses array */
86     foreach($sort as $name){
87       $tmp[$name] =$this->FAIAllclasses[$name];
88     }
90     /* Assign sorted classes */
91     $this->FAIAllclasses = array();
92     $this->FAIAllclasses = $tmp;
94     if($dn != "new"){
95       $this->dn =$dn;
96     }
97     $this->old_cn   = $this->cn;
98   }
101   /* Combine new array, used for up down buttons */
102   function combineArrays($ar0,$ar1,$ar2)
103   {
104     $ret = array();
105     if(is_array($ar0))
106       foreach($ar0 as $ar => $a){
107         $ret[$ar]=$a;
108       }
109     if(is_array($ar1))
110       foreach($ar1 as $ar => $a){
111         $ret[$ar]=$a;
112       }
113     if(is_array($ar2))
114       foreach($ar2 as $ar => $a){
115         $ret[$ar]=$a;
116       }
117     return($ret);
118   }
121   function acl_base_for_current_object($dn)
122   {
123     if($dn == "new" || $dn == ""){
124       if($this->dn == "new"){
125         $dn= $this->parent->parent->acl_base;
126       }else{
127         $dn = $this->dn;
128       }
129     }
130     return($dn);
131   }
134   /* returns position in array */
135   function getpos($atr,$attrs)
136   {
137     $i = 0;
138     foreach($attrs as $attr => $name)    {
139       $i++;
140       if($attr == $atr){
141         return($i);
142       }
143     }
144     return(-1);
145   }
147   /* Transports the given Arraykey one position up*/
148   function ArrayUp($atr,$attrs)
149   {
150     $ret = $attrs;
151     $pos = $this->getpos($atr,$attrs) ;
152     $cn = count($attrs);
153     if(!(($pos == -1)||($pos == 1))){
154       $before = array_slice($attrs,0,($pos-2));
155       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
156       $unten  = array_slice($attrs,$pos);
157       $ret = array();
158       $ret = $this->combineArrays($before,$mitte,$unten);
159     }
160     return($ret);
161   }
164   /* Transports the given Arraykey one position down*/
165   function ArrayDown($atr,$attrs)
166   {
167     $ret = $attrs;
168     $pos = $this->getpos($atr,$attrs) ;
169     $cn = count($attrs);
170     if(!(($pos == -1)||($pos == $cn))){
171       $before = array_slice($attrs,0,($pos-1));
172       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
173       $unten  = array_slice($attrs,($pos+1));
174       $ret = array();
175       $ret = $this->combineArrays($before,$mitte,$unten);
176     }
177     return($ret);
178   }
180   /* class one position up */
181   function catUp($id)
182   {
183     /* Get all cats depinding on current dir */
184     $cats = $this->FAIclasses;
185     $this->FAIclasses =$this->ArrayUp($id,$cats);
186   }
188   /* Class one position down */
189   function catDown($id)
190   {
191     /* Get all cats depinding on current dir */
192     $cats = $this->FAIclasses;
193     $this->FAIclasses =$this->ArrayDown($id,$cats);
194   }
196   function execute()
197   {
198     /* Call parent execute */
199     plugin::execute();
201     if($this->is_account && !$this->view_logged){
202       $this->view_logged = TRUE;
203       new log("view","fai/".get_class($this),$this->dn);
204     }
206     /* Fill templating stuff */
207     $smarty= get_smarty();
208     $display= "";
210     $s_entry = "";
211     $s_action = "";
213     /* Remove class name From list */
214     $sort_once = false;
215     if(!preg_match("/freeze/",$this->FAIstate)){
216       foreach($_POST as $name => $post){
217         if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
218           $s_action = "delete";
219           $s_entry  = preg_replace("/DEL_/","",$name);
220           $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
221         }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
222           $s_action  = "add";
223         }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
224           $s_action  = "delete";
225           $s_entry = $_POST['FAIclass'];
226         }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
227           $s_action  = "add";
228         }
230         /* Check if a list element should be pushed one position up */
231         if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
232           $sort_once = true;
233           $val = preg_replace("/sortup_/","",$name);
234           $val = preg_replace("/_.*$/","",$val);
235           $val = base64_decode($val);
236           $this->catUp($val);
237         }
239         /* Check if a list element should be pushed one position down */
240         if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
241           $sort_once = true;
242           $val = preg_replace("/sortdown_/","",$name);
243           $val = preg_replace("/_.*$/","",$val);
244           $val = base64_decode($val);
245           $this->catDown($val);
246         }
248       }
250       if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
251         unset($this->FAIclasses[$s_entry]);
252       }
254       if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
255         $this->classSelect = new classSelect($this->config, get_userinfo());
256         $this->dialog  =true;
257       }
259       /* Save Dialog */
260       if(isset($_POST['classSelect_save']) && $this->classSelect instanceOf classSelect){
261         $list = $this->classSelect->save();
262         foreach($list as $entry){
263           $class = $entry['cn'][0];
264           $this->FAIclasses[$class] =$class; 
265         }
266         $this->dialog=false;
267         $this->classSelect=FALSE;
268       }
269     }
271     /* Cancel Dialog */
272     if(isset($_POST['classSelect_cancel']) && $this->classSelect instanceOf classSelect){
273       $this->dialog=false;
274       $this->classSelect=FALSE;
275     }
277     if($this->classSelect instanceOf classSelect){
278       session::set('filterFAIClass_base',$this->parent->parent->fai_release);
279       session::set('filterBlacklist', array('cn' => array_values($this->FAIclasses)));
280       return($this->classSelect->execute());
281     }
283     $divlist  =new divSelectBox("Profile");
284     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
286     /* item images */
287     $objTypes['FAIhook']            = "<img src='plugins/fai/images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
288     $objTypes['FAItemplate']        = "<img src='plugins/fai/images/fai_template.png' title='"._("Template bundle")."' alt=''>";
289     $objTypes['FAIscript']          = "<img src='plugins/fai/images/fai_script.png' title='"._("Script bundle")."' alt=''>";
290     $objTypes['FAIvariable']        = "<img src='plugins/fai/images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
291     $objTypes['FAIpackageList']        = "<img src='plugins/fai/images/fai_packages.png' title='"._("Package bundle")."' alt=''>";
292     $objTypes['FAIpartitionTable']  = "<img src='plugins/fai/images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
294     /* Delete button */
295     $actions = "<input type='image' src='images/lists/trash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
296     
297     /* Up down buttons */
298     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/lists/sort-up.png' align='top' >";
299     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/lists/sort-down.png' >";
301     /* Append fai classes to divlist */
302     if($this->acl_is_readable("FAIclass")){
303       foreach($this->FAIclasses as $usedClass){
304         $str = "&nbsp;";
305         $act = "";
307         if(isset($this->FAIAllclasses[$usedClass])){
308           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
309             $str.= $objTypes[$class]; 
310           }
311         }
313         $field1 = array("string"=> $usedClass,"attach"=>"");
314         $field2 = array("string"=> $str,"attach"=>"");
315         if(!preg_match("/freeze/", $this->FAIstate) && $this->acl_is_writeable("FAIclass")){
316           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
317               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
318               "attach"=>"style='border-right:none;'");
319         }else{
320           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
321         }
322         $divlist->AddEntry(array($field1,$field2,$field3));
323       }
324     }
326     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
328     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
329     $smarty->assign("divlist"     ,$divlist->DrawList());
331     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
332      * If we post the escaped strings they will be escaped again
333      */
334     foreach($this->attributes as $attrs){
335       if(get_magic_quotes_gpc()){
336         $smarty->assign($attrs,stripslashes($this->$attrs));
337       }else{
338         $smarty->assign($attrs,($this->$attrs));
339       }
340     }
342     
343     $dn = $this->acl_base_for_current_object($this->dn);
344     $smarty->assign("sub_object_is_addable",
345         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
346         !preg_match("/freeze/",$this->FAIstate));
348     $tmp = $this->plInfo();
349     foreach($tmp['plProvidedAcls'] as $name => $translated){
350       $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
351     }
353     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
354     return($display);
355   }
357   function remove_from_parent()
358   {
359     $ldap = $this->config->get_ldap_link();
360     $ldap->cd ($this->dn);
361     $release = $this->parent->parent->fai_release;
362     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
363     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
364     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
365     $this->handle_post_events("remove");    
366   }
369   /* Save data to object 
370    */
371   function save_object()
372   {
373     if(!preg_match("/freeze/",$this->FAIstate)){
374       plugin::save_object();
375     }
376   }
379   /* Check supplied data */
380   function check()
381   {
382     /* Call common method to give check the hook */
383     $message= plugin::check();
385     if(count($this->FAIclasses) == 0){
386       $message[]=_("No class specified for this profile!");
387     }
389     if($this->cn == ""){
390       $message[]= msgPool::required(_("Name"));
391     }
393     /* Ensure that we do not overwrite an allready existing entry 
394      */
395     if($this->dn == "new" || $this->cn != $this->old_cn){
396       $release = $this->parent->parent->fai_release;
397       $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
398       $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
399       if(isset($res[$this->cn])){
400         $message[] = msgPool::duplicated(_("Name"));
401       }
402     }
404     return ($message);
405   }
408   /* Save to LDAP */
409   function save()
410   {
411     plugin::save();
413     $ldap = $this->config->get_ldap_link();
415     $this->FAIclass = "";
416     foreach($this->FAIclasses as $class){
417       $this->FAIclass.=$class." ";
418     }
420     $this->attrs['FAIclass']=trim($this->FAIclass);
422     /* Remove the old FAI profile, if the dn has changed.
423      */
424     if($this->cn != $this->old_cn && $this->old_cn != ""){
426       $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
427       FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
428     }
429     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
430    
431     if($this->initially_was_account){
432       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
433     }else{
434       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
435     }
436   }
439   function PrepareForCopyPaste($source)
440   {
441     plugin::PrepareForCopyPaste($source);
443     /* Parse ldap attribute to get all assigned classes */
444     $tmp = explode(" ",$this->FAIclass);
445     $tmp2 = array();
446     foreach($tmp as $class){
447       if(!empty($class)){
448         $tmp2[trim($class)] = trim($class);
449       }
450     }
452     /* Sort assigned classes */
453     if(is_array($tmp2)){
454       foreach($tmp2 as $class){
455         $this->FAIclasses[$class]=$class;
456       }
457     }
458   }
461   /* Return plugin informations for acl handling */ 
462   static function plInfo()
463   {
464     return (array( 
465           "plShortName" => _("Profile"),
466           "plDescription" => _("FAI profile"),
467           "plSelfModify"  => FALSE,
468           "plDepends"     => array(),
469           "plPriority"    => 30,
470           "plSection"     => array("administration"),
471           "plCategory"    => array("fai"),
472           "plProvidedAcls" => array(
473             "cn"                => _("Name"),
474             "description"       => _("Description"),
475             "FAIclass"          => _("FAI classes"))
476           ));
477   }
480   /*! \brief  Used for copy & paste.
481               Returns a HTML input mask, which allows to change the cn of this entry.
482       @param  Array   Array containing current status && a HTML template.
483    */
484   function getCopyDialog()
485   {
486     $vars = array("cn");
487     $smarty = get_smarty();
488     $smarty->assign("cn", htmlentities($this->cn));
489     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
490     $ret = array();
491     $ret['string'] = $str;
492     $ret['status'] = "";
493     return($ret);
494   }
497   /*! \brief  Used for copy & paste.
498               Some entries must be renamed to avaoid duplicate entries.
499    */
500   function saveCopyDialog()
501   {
502     if(isset($_POST['cn'])){
503       $this->cn = get_post('cn');
504     }
505   }
508 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
509 ?>