Code

84e5b40b1174dc503303d66235f18985c104ea0c
[gosa.git] / plugins / 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   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAIscriptEntry";
16   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
18   /* Specific attributes */
19   var $old_cn           = "";
20   var $cn               = "";       // The class name for this object
21   var $description      = "";       // The description for this set of partitions
22   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
23   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
24   var $FAIclass         = "";       // Classnames used for this profile
25   var $FAIclasses       = array();  // Contains classname seperated in an array
26   var $FAIAllclasses    = array();  // Contains all possible Classnames
28   var $FAIstate       = "";
29   var $base           = "";
30   var $release        = "";
31   var $copy_paste_mode= false;
32   var $cut_paste_mode = false;
34   var $CopyPasteVars  = array("FAIclass","FAIclasses","FAIAllclasses");
36   function faiProfile($config, $dn= NULL)
37   {
38     /* Load Attributes */
39     plugin::plugin ($config, $dn);
40     $ldap=$this->config->get_ldap_link();
42     $this->acl = "#all#";
44     if($this->dn != "new"){
45       /* Set acls
46        */
47       $ui   = get_userinfo();
48       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
49       $acli = get_module_permission($acl, "FAIclass", $this->dn);
50       $this->acl=$acli;
51     }
53     /* Parse ldap attribute to get all assigned classes */
54     $tmp = split(" ",$this->FAIclass);
55     $tmp2 = array();
56     foreach($tmp as $class){
57       if(!empty($class)){
58         $tmp2[trim($class)] = trim($class);
59       }
60     }
62     if(isset($this->attrs['FAIstate'][0])){
63       $this->FAIstate = $this->attrs['FAIstate'][0];
64     }
66     /* Sort assigned classes */ 
67     if(is_array($tmp2)){
68       foreach($tmp2 as $class){
69         $this->FAIclasses[$class]=$class;
70       }
71     }
73     /* Search only in fai tree */
74     $ObjectTypes = array(
75         "FAIpartitionTable"  => array("OU"=>"ou=disk,"       ),
76         "FAIpackageList"     => array("OU"=>"ou=packages,"   ),
77         "FAIscript"          => array("OU"=>"ou=scripts,"    ),
78         "FAIvariable"        => array("OU"=>"ou=variables,"  ),
79         "FAIhook"            => array("OU"=>"ou=hooks,"      ),
80 #        "FAIprofile"         => array("OU"=>"ou=profiles,"  ),
81         "FAItemplate"        => array("OU"=>"ou=templates,"  ));
83     $base= "ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
84     if($_SESSION['faifilter']['branch']!="main"){
85       $base = $_SESSION['faifilter']['branch'];
86     }
88     /* Get ldap connection */
89     $ldap= $this->config->get_ldap_link();
90     $ldap->cd($base);
91     $sort = array();
92     /* Capture objects from given base */
93     $result = array();
94     foreach($ObjectTypes as $oc => $data){
95       $ou = $data['OU'].$base;
97       $ldap->ls("(objectClass=".$oc.")",$ou,array("cn","objectClass","dn"));
98       while($attrs = $ldap->fetch()){
99         $this->FAIAllclasses[$attrs['cn'][0]][$oc]=$attrs;
100         $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
101       }
102     }
103   
104     /* Sort the sort array */
105     //ksort($sort);
107     /* Reorder the FAIclasses array */
108     foreach($sort as $name){
109       $tmp[$name] =$this->FAIAllclasses[$name];
110     }
112     /* Assign sorted classes */
113     $this->FAIAllclasses = array();
114     $this->FAIAllclasses = $tmp;
116     if($dn != "new"){
117       $this->dn =$dn;
118     }
119     $this->old_cn   = $this->cn;
120   }
123   /* Combine new array, used for up down buttons */
124   function combineArrays($ar0,$ar1,$ar2)
125   {
126     $ret = array();
127     if(is_array($ar0))
128       foreach($ar0 as $ar => $a){
129         $ret[$ar]=$a;
130       }
131     if(is_array($ar1))
132       foreach($ar1 as $ar => $a){
133         $ret[$ar]=$a;
134       }
135     if(is_array($ar2))
136       foreach($ar2 as $ar => $a){
137         $ret[$ar]=$a;
138       }
139     return($ret);
140   }
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)){
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)){
223         $s_action  = "add";
224       }elseif(preg_match("/DelClass/i",$name)){
225         $s_action  = "delete";
226         $s_entry = $_POST['FAIclass'];
227       }elseif(preg_match("/AddClass/i",$name)){
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)){
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)){
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"){
252       unset($this->FAIclasses[$s_entry]);
253     }
255     if($s_action == "add"){
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     foreach($this->FAIclasses as $usedClass){
310       $str = "&nbsp;";
312       if(isset($this->FAIAllclasses[$usedClass])){
313         foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
314           $str.= $objTypes[$class]; 
315         }
316       }
317   
318       $field1 = array("string"=> $usedClass,"attach"=>"");
319       $field2 = array("string"=> $str,"attach"=>"");
320       if($this->FAIstate != "freeze"){
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     }
330     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
331     $smarty->assign("divlist"     ,$divlist->DrawList());
333     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
334      * If we post the escaped strings they will be escaped again
335      */
336     foreach($this->attributes as $attrs){
337       if(get_magic_quotes_gpc()){
338         $smarty->assign($attrs,stripslashes($this->$attrs));
339       }else{
340         $smarty->assign($attrs,($this->$attrs));
341       }
342     }
344     foreach($this->attributes as $attr){
345       if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
346         $smarty->assign($attr."ACL"," disabled ");
347       }else{
348         $smarty->assign($attr."ACL","  ");
349       }
350     }
352     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
353     return($display);
354   }
356   function remove_from_parent()
357   {
358     $ldap = $this->config->get_ldap_link();
359     $ldap->cd ($this->dn);
360     $ldap->rmdir_recursive($this->dn);
361     show_ldap_error($ldap->get_error(), _("Removing FAI profile failed"));
362     $this->handle_post_events("remove");    
363   }
365   /* Save data to object 
366    */
367   function save_object()
368   {
369     plugin::save_object();
370     foreach($this->attributes as $attrs){
371       if(isset($_POST[$attrs])){
372         $this->$attrs = $_POST[$attrs];
373       }
374     }
375   }
378   /* Check supplied data */
379   function check()
380   {
381     /* Call common method to give check the hook */
382     $message= plugin::check();
384     if($this->copy_paste_mode){
386       /* If this is a new script, check if a script with this name already exists */
387       if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
389         /* Check if current name is already used for fai scripts in selected release */
390         $dn = 'cn='.$this->cn.",ou=profiles,".$this->release;
391         $ldap = $this->config->get_ldap_link();
392         $ldap->cat($dn);
393         if($ldap->count() || in_array($this->cn,$this->FAIAllclasses)){
395           $r =convert_department_dn($this->release);;
396           $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
397         }
398       }
399     }else{
400       if(count($this->FAIclasses) == 0){
401         $message[]=_("Please assign at least one class to this  profile.");
402       }
404       if(empty($this->cn)){
405         $message[]=_("Please enter a valid name.");
406       }
408       $ldap = $this->config->get_ldap_link();
409       $release = "ou=profiles,ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
410       if($_SESSION['faifilter']['branch']!="main"){
411         $release = "ou=profiles,".$_SESSION['faifilter']['branch'];
412       }
413       $ldap->cd($release);
414       if ($this->old_cn == ""){
415         $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
416       } else {
417         $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
418       }
419       if($ldap->count() || isset($this->FAIAllclasses[$this->cn])){
420         $message[]=_("There is already a FAI object with this class name defined.");
421       }
422     }
423     return ($message);
424   }
427   /* Save to LDAP */
428   function save()
429   {
430     plugin::save();
432     $ldap = $this->config->get_ldap_link();
434     $this->FAIclass = "";
435     foreach($this->FAIclasses as $class){
436       $this->FAIclass.=$class." ";
437     }
439     $this->attrs['FAIclass']=trim($this->FAIclass);
441     /* Copy & Paste : Ensure that FAIstate is copied too */
442     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
443       $this->attrs['FAIstate'] = $this->FAIstate;
444     }
446     $ldap->cat($this->dn,array("objectClass"));
447     if($ldap->count()!=0){
448       /* Write FAIscript to ldap*/
449       $ldap->cd($this->dn);
450       $this->cleanup();
451       $ldap->modify ($this->attrs); 
452     }else{
453       /* Write FAIscript to ldap*/
454       $ldap->cd($this->config->current['BASE']);
455       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
456       $ldap->cd($this->dn);
457       $ldap->add($this->attrs);
458     }
459     show_ldap_error($ldap->get_error(), _("Saving FAI profile failed"));
461     /* Do object tagging */
462     $this->handle_object_tagging();
463     show_ldap_error($ldap->get_error());
464   }
466   
467   /* return copy & paste dialog
468    */
469   function getCopyDialog()
470   {
471     /* Ask for cn */
472     $smarty = get_smarty();
473     $smarty->assign("cn" ,$this->cn);
474     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
475     $ret = array();
476     $ret['string'] = $str;
477     $ret['status'] = "";
478     return($ret);
479   }
481   /* Get posted cn */
482   function saveCopyDialog()
483   {
484     if(isset($_POST['cn'])){
485       $this->cn = $_POST['cn'];
486     }
487   }
490 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
491 ?>