Code

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