Code

Also display new created variables
[gosa.git] / plugins / admin / fai / class_faiProfile.inc
1 <?php
3 class faiProfile extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description","FAIclass");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIprofile");
19   /* Specific attributes */
20   var $old_cn           = "";
21   var $cn               = "";       // The class name for this object
22   var $description      = "";       // The description for this set of partitions
23   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
24   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
25   var $FAIclass         = "";       // Classnames used for this profile
26   var $FAIclasses       = array();  // Contains classname seperated in an array
27   var $FAIAllclasses    = array();  // Contains all possible Classnames
28   var $ui               ;
29   var $FAIstate      = "";
31   function faiProfile($config, $dn= NULL)
32   {
33     /* Load Attributes */
34     plugin::plugin ($config, $dn);
35     $ldap=$this->config->get_ldap_link();
37     $this->ui = get_userinfo();
39     /* Parse ldap attribute to get all assigned classes */
40     $tmp = split(" ",$this->FAIclass);
41     $tmp2 = array();
42     foreach($tmp as $class){
43       if(!empty($class)){
44         $tmp2[trim($class)] = trim($class);
45       }
46     }
48     if(isset($this->attrs['FAIstate'][0])){
49       $this->FAIstate = $this->attrs['FAIstate'][0];
50     }
52     /* Sort assigned classes */ 
53     if(is_array($tmp2)){
54       foreach($tmp2 as $class){
55         $this->FAIclasses[$class]=$class;
56       }
57     }
59     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
61     /* Build filter */
62     $filter= "";
63     foreach ($categories as $cat){
64       $filter.= "(objectClass=$cat)";
65     }
66     
67     /* Get ldap connection */ 
68     $base = $_SESSION['CurrentMainBase'];
69     $ldap->cd($base);
70     $sort = array();
72     /* search all FAI classes */
73     $ldap->search("(|$filter)",array("*"));
74     while($attrs = $ldap->fetch()){
76       /* Sort by categorie */
77       foreach($categories as $cat){
78         if(in_array($cat,$attrs['objectClass'])){
80           /* Append entry */
81           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
82   
83           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
84           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
85         }
86       }
87     } 
89     /* Sort the sort array */
90     //ksort($sort);
92     /* Reorder the FAIclasses array */
93     foreach($sort as $name){
94       $tmp[$name] =$this->FAIAllclasses[$name];
95     }
97     /* Assign sorted classes */
98     $this->FAIAllclasses = array();
99     $this->FAIAllclasses = $tmp;
101     if($dn != "new"){
102       $this->dn =$dn;
103       @log::log("view","fai/".get_class($this),$this->dn);
104     }
105     $this->old_cn   = $this->cn;
106   }
109   /* Combine new array, used for up down buttons */
110   function combineArrays($ar0,$ar1,$ar2)
111   {
112     $ret = array();
113     if(is_array($ar0))
114       foreach($ar0 as $ar => $a){
115         $ret[$ar]=$a;
116       }
117     if(is_array($ar1))
118       foreach($ar1 as $ar => $a){
119         $ret[$ar]=$a;
120       }
121     if(is_array($ar2))
122       foreach($ar2 as $ar => $a){
123         $ret[$ar]=$a;
124       }
125     return($ret);
126   }
129   function acl_base_for_current_object($dn)
130   {
131     if($dn == "new"){
132       if($this->dn == "new"){
133         $dn= $_SESSION['CurrentMainBase'];
134       }else{
135         $dn = $this->dn;
136       }
137     }
138     return($dn);
139   }
142   /* returns position in array */
143   function getpos($atr,$attrs)
144   {
145     $i = 0;
146     foreach($attrs as $attr => $name)    {
147       $i++;
148       if($attr == $atr){
149         return($i);
150       }
151     }
152     return(-1);
153   }
155   /* Transports the given Arraykey one position up*/
156   function ArrayUp($atr,$attrs)
157   {
158     $ret = $attrs;
159     $pos = $this->getpos($atr,$attrs) ;
160     $cn = count($attrs);
161     if(!(($pos == -1)||($pos == 1))){
162       $before = array_slice($attrs,0,($pos-2));
163       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
164       $unten  = array_slice($attrs,$pos);
165       $ret = array();
166       $ret = $this->combineArrays($before,$mitte,$unten);
167     }
168     return($ret);
169   }
172   /* Transports the given Arraykey one position down*/
173   function ArrayDown($atr,$attrs)
174   {
175     $ret = $attrs;
176     $pos = $this->getpos($atr,$attrs) ;
177     $cn = count($attrs);
178     if(!(($pos == -1)||($pos == $cn))){
179       $before = array_slice($attrs,0,($pos-1));
180       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
181       $unten  = array_slice($attrs,($pos+1));
182       $ret = array();
183       $ret = $this->combineArrays($before,$mitte,$unten);
184     }
185     return($ret);
186   }
188   /* class one position up */
189   function catUp($id)
190   {
191     /* Get all cats depinding on current dir */
192     $cats = $this->FAIclasses;
193     $this->FAIclasses =$this->ArrayUp($id,$cats);
194   }
196   /* Class one position down */
197   function catDown($id)
198   {
199     /* Get all cats depinding on current dir */
200     $cats = $this->FAIclasses;
201     $this->FAIclasses =$this->ArrayDown($id,$cats);
202   }
204   function execute()
205   {
206     /* Call parent execute */
207     plugin::execute();
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);
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         print_red($msgs);
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=NULL;
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=NULL;
283     }
285     if(isset($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='"._("Packages 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/edittrash.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(($this->FAIstate != "freeze") && $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);
367 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
368     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
369     if($_SESSION['faifilter']['branch'] == "main"){
370       $use_dn = $this->dn;
371     }
373     @log::log("remove","fai/".get_class($this),$use_dn,$this->attributes);
374     prepare_to_save_FAI_object($use_dn,array(),true);
375     $this->handle_post_events("remove");    
376   }
379   /* Save data to object 
380    */
381   function save_object()
382   {
383     plugin::save_object();
384   }
387   /* Check supplied data */
388   function check()
389   {
390     /* Call common method to give check the hook */
391     $message= plugin::check();
393     if(count($this->FAIclasses) == 0){
394       $message[]=_("Please assign at least one class to this  profile.");
395     }
397     if(empty($this->cn)){
398       $message[]=_("Please enter a valid name.");
399     }
401     $ldap = $this->config->get_ldap_link();
402     $ldap->cd($_SESSION['CurrentMainBase']);
403     if ($this->old_cn == ""){
404       $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
405     } else {
406       $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
407     }
409     if($ldap->count()){
410       $message[]=_("There is already a profile with this class name defined.");
411     }
413     return ($message);
414   }
417   /* Save to LDAP */
418   function save()
419   {
420     plugin::save();
422     $ldap = $this->config->get_ldap_link();
424     $this->FAIclass = "";
425     foreach($this->FAIclasses as $class){
426       $this->FAIclass.=$class." ";
427     }
429     $this->attrs['FAIclass']=trim($this->FAIclass);
431     prepare_to_save_FAI_object($this->dn,$this->attrs);
432    
433     if($this->initially_was_account){
434       @log::log("modify","fai/".get_class($this),$this->dn,$this->attributes);
435     }else{
436       @log::log("create","fai/".get_class($this),$this->dn,$this->attributes);
437     }
438  
439     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
441     /* Do object tagging */
442     $this->handle_object_tagging();
443     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
444   }
447   /* Return plugin informations for acl handling */ 
448   function plInfo()
449   {
450     return (array( 
451           "plShortName" => _("Profile"),
452           "plDescription" => _("FAI profile"),
453           "plSelfModify"  => FALSE,
454           "plDepends"     => array(),
455           "plPriority"    => 30,
456           "plSection"     => array("administration"),
457           "plCategory"    => array("fai"),
458           "plProvidedAcls" => array(
459             "cn"                => _("Name"),
460             "description"       => _("Description"),
461             "FAIclass"          => _("FAI classes"))
462           ));
463   }
466 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
467 ?>