Code

Removed unused parameter from profilesEntry
[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     if(isset($this->attrs['FAIstate'][0])){
44       $this->FAIstate = $this->attrs['FAIstate'][0];
45     }
47     /* Sort assigned classes */ 
48     if(is_array($tmp2)){
49       foreach($tmp2 as $class){
50         $this->FAIclasses[$class]=$class;
51       }
52     }
54     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
56     /* Build filter */
57     $filter= "";
58     foreach ($categories as $cat){
59       $filter.= "(objectClass=$cat)";
60     }
61     
62     /* Get ldap connection */ 
63     $base  = session::get('CurrentMainBase');
64     $ldap->cd($base);
65     $sort = array();
67     /* search all FAI classes */
68     $ldap->search("(|$filter)",array("*"));
69     while($attrs = $ldap->fetch()){
71       /* Sort by categorie */
72       foreach($categories as $cat){
73         if(in_array($cat,$attrs['objectClass'])){
75           /* Append entry */
76           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
77   
78           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
79           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
80         }
81       }
82     } 
84     /* Sort the sort array */
85     //ksort($sort);
87     /* Reorder the FAIclasses array */
88     foreach($sort as $name){
89       $tmp[$name] =$this->FAIAllclasses[$name];
90     }
92     /* Assign sorted classes */
93     $this->FAIAllclasses = array();
94     $this->FAIAllclasses = $tmp;
96     if($dn != "new"){
97       $this->dn =$dn;
98     }
99     $this->old_cn   = $this->cn;
100   }
103   /* Combine new array, used for up down buttons */
104   function combineArrays($ar0,$ar1,$ar2)
105   {
106     $ret = array();
107     if(is_array($ar0))
108       foreach($ar0 as $ar => $a){
109         $ret[$ar]=$a;
110       }
111     if(is_array($ar1))
112       foreach($ar1 as $ar => $a){
113         $ret[$ar]=$a;
114       }
115     if(is_array($ar2))
116       foreach($ar2 as $ar => $a){
117         $ret[$ar]=$a;
118       }
119     return($ret);
120   }
123   function acl_base_for_current_object($dn)
124   {
125     if($dn == "new"){
126       if($this->dn == "new"){
127         $dn = session::get('CurrentMainBase');
128       }else{
129         $dn = $this->dn;
130       }
131     }
132     return($dn);
133   }
136   /* returns position in array */
137   function getpos($atr,$attrs)
138   {
139     $i = 0;
140     foreach($attrs as $attr => $name)    {
141       $i++;
142       if($attr == $atr){
143         return($i);
144       }
145     }
146     return(-1);
147   }
149   /* Transports the given Arraykey one position up*/
150   function ArrayUp($atr,$attrs)
151   {
152     $ret = $attrs;
153     $pos = $this->getpos($atr,$attrs) ;
154     $cn = count($attrs);
155     if(!(($pos == -1)||($pos == 1))){
156       $before = array_slice($attrs,0,($pos-2));
157       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
158       $unten  = array_slice($attrs,$pos);
159       $ret = array();
160       $ret = $this->combineArrays($before,$mitte,$unten);
161     }
162     return($ret);
163   }
166   /* Transports the given Arraykey one position down*/
167   function ArrayDown($atr,$attrs)
168   {
169     $ret = $attrs;
170     $pos = $this->getpos($atr,$attrs) ;
171     $cn = count($attrs);
172     if(!(($pos == -1)||($pos == $cn))){
173       $before = array_slice($attrs,0,($pos-1));
174       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
175       $unten  = array_slice($attrs,($pos+1));
176       $ret = array();
177       $ret = $this->combineArrays($before,$mitte,$unten);
178     }
179     return($ret);
180   }
182   /* class one position up */
183   function catUp($id)
184   {
185     /* Get all cats depinding on current dir */
186     $cats = $this->FAIclasses;
187     $this->FAIclasses =$this->ArrayUp($id,$cats);
188   }
190   /* Class one position down */
191   function catDown($id)
192   {
193     /* Get all cats depinding on current dir */
194     $cats = $this->FAIclasses;
195     $this->FAIclasses =$this->ArrayDown($id,$cats);
196   }
198   function execute()
199   {
200     /* Call parent execute */
201     plugin::execute();
203     if($this->is_account && !$this->view_logged){
204       $this->view_logged = TRUE;
205       new log("view","fai/".get_class($this),$this->dn);
206     }
208     /* Fill templating stuff */
209     $smarty= get_smarty();
210     $display= "";
212     $s_entry = "";
213     $s_action = "";
215     /* Remove class name From list */
216     $sort_once = false;
217     foreach($_POST as $name => $post){
218       if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
219         $s_action = "delete";
220         $s_entry  = preg_replace("/DEL_/","",$name);
221         $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
222       }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
223         $s_action  = "add";
224       }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
225         $s_action  = "delete";
226         $s_entry = $_POST['FAIclass'];
227       }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
228         $s_action  = "add";
229       }
231       /* Check if a list element should be pushed one position up */
232       if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
233         $sort_once = true;
234         $val = preg_replace("/sortup_/","",$name);
235         $val = preg_replace("/_.*$/","",$val);
236         $val = base64_decode($val);
237         $this->catUp($val);
238       }
239       
240       /* Check if a list element should be pushed one position down */
241       if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
242         $sort_once = true;
243         $val = preg_replace("/sortdown_/","",$name);
244         $val = preg_replace("/_.*$/","",$val);
245         $val = base64_decode($val);
246         $this->catDown($val);
247       }
249     }
251     if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
252       unset($this->FAIclasses[$s_entry]);
253     }
255     if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
256       $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses,$this->parent->parent->fai_release);
257       $this->is_dialog  =true;
258     }
260     /* Save Dialog */
261     if(isset($_POST['SaveSubObject'])){
262       $this->dialog->save_object();
263       $msgs= $this->dialog->check();
264       if(count($msgs)){
265         msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
266       }else{
267         $ret = $this->dialog->save();
268         foreach($ret as $class){
269           $this->FAIclasses[$class] =$class; 
270         }
271         $this->is_dialog=false;
272         unset($this->dialog);
273         $this->dialog=FALSE;
274         //ksort($this->FAIclasses);
275       }
276     }
278     /* Cancel Dialog */
279     if(isset($_POST['CancelSubObject'])){
280       $this->is_dialog=false;
281       unset($this->dialog);
282       $this->dialog=FALSE;
283     }
285     if(is_object($this->dialog)){
286       $this->dialog->save_object();
287       return($this->dialog->execute());
288     }
290     $divlist  =new divSelectBox("Profile");
291     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
293     /* item images */
294     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
295     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
296     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
297     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
298     $objTypes['FAIpackageList']        = "<img src='images/fai_packages.png' title='"._("Package bundle")."' alt=''>";
299     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
301     /* Delete button */
302     $actions = "<input type='image' src='images/lists/trash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
303     
304     /* Up down buttons */
305     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
306     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
308     /* Append fai classes to divlist */
309     if($this->acl_is_readable("FAIclass")){
310       foreach($this->FAIclasses as $usedClass){
311         $str = "&nbsp;";
312         $act = "";
314         if(isset($this->FAIAllclasses[$usedClass])){
315           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
316             $str.= $objTypes[$class]; 
317           }
318         }
320         $field1 = array("string"=> $usedClass,"attach"=>"");
321         $field2 = array("string"=> $str,"attach"=>"");
322         if(!preg_match("/freeze/", $this->FAIstate) && $this->acl_is_writeable("FAIclass")){
323           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
324               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
325               "attach"=>"style='border-right:none;'");
326         }else{
327           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
328         }
329         $divlist->AddEntry(array($field1,$field2,$field3));
330       }
331     }
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));
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     plugin::save_object();
379   }
382   /* Check supplied data */
383   function check()
384   {
385     /* Call common method to give check the hook */
386     $message= plugin::check();
388     if(count($this->FAIclasses) == 0){
389       $message[]=_("No class specified for this profile!");
390     }
392     if(empty($this->cn)){
393       $message[]= msgPool::invalid(_("Name"));
394     }
396     /* Ensure that we do not overwrite an allready existing entry 
397      */
398     if($this->dn == "new" || $this->cn != $this->old_cn){
399       $release = $this->parent->parent->fai_release;
400       $new_dn= 'cn='.$this->cn.",".get_ou('faiprofileou').get_ou('faiou').$release;
401       $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
402       if(isset($res[$this->cn])){
403         $message[] = msgPool::duplicated(_("Name"));
404       }
405     }
407     return ($message);
408   }
411   /* Save to LDAP */
412   function save()
413   {
414     plugin::save();
416     $ldap = $this->config->get_ldap_link();
418     $this->FAIclass = "";
419     foreach($this->FAIclasses as $class){
420       $this->FAIclass.=$class." ";
421     }
423     $this->attrs['FAIclass']=trim($this->FAIclass);
425     /* Remove the old FAI profile, if the dn has changed.
426      */
427     if($this->cn != $this->old_cn && $this->old_cn != ""){
429       $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
430       FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
431     }
432     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
433    
434     if($this->initially_was_account){
435       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
436     }else{
437       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
438     }
439   }
442   function PrepareForCopyPaste($source)
443   {
444     plugin::PrepareForCopyPaste($source);
446     /* Parse ldap attribute to get all assigned classes */
447     $tmp = split(" ",$this->FAIclass);
448     $tmp2 = array();
449     foreach($tmp as $class){
450       if(!empty($class)){
451         $tmp2[trim($class)] = trim($class);
452       }
453     }
455     /* Sort assigned classes */
456     if(is_array($tmp2)){
457       foreach($tmp2 as $class){
458         $this->FAIclasses[$class]=$class;
459       }
460     }
461   }
464   /* Return plugin informations for acl handling */ 
465   static function plInfo()
466   {
467     return (array( 
468           "plShortName" => _("Profile"),
469           "plDescription" => _("FAI profile"),
470           "plSelfModify"  => FALSE,
471           "plDepends"     => array(),
472           "plPriority"    => 30,
473           "plSection"     => array("administration"),
474           "plCategory"    => array("fai"),
475           "plProvidedAcls" => array(
476             "cn"                => _("Name"),
477             "description"       => _("Description"),
478             "FAIclass"          => _("FAI classes"))
479           ));
480   }
483   /*! \brief  Used for copy & paste.
484               Returns a HTML input mask, which allows to change the cn of this entry.
485       @param  Array   Array containing current status && a HTML template.
486    */
487   function getCopyDialog()
488   {
489     $vars = array("cn");
490     $smarty = get_smarty();
491     $smarty->assign("cn", htmlentities($this->cn));
492     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
493     $ret = array();
494     $ret['string'] = $str;
495     $ret['status'] = "";
496     return($ret);
497   }
500   /*! \brief  Used for copy & paste.
501               Some entries must be renamed to avaoid duplicate entries.
502    */
503   function saveCopyDialog()
504   {
505     if(isset($_POST['cn'])){
506       $this->cn = get_post('cn');
507     }
508   }
511 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
512 ?>