Code

ed8fcaba31db5c063e582c6560b813def0a7dc7d
[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   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIscriptEntry";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Specific attributes */
24   var $old_cn           = "";
25   var $cn               = "";       // The class name for this object
26   var $description      = "";       // The description for this set of partitions
27   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
28   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
29   var $FAIclass         = "";       // Classnames used for this profile
30   var $FAIclasses       = array();  // Contains classname seperated in an array
31   var $FAIAllclasses    = array();  // Contains all possible Classnames
33   var $FAIstate       = "";
34   var $base           = "";
35   var $release        = "";
36   var $copy_paste_mode= false;
37   var $cut_paste_mode = false;
39   var $CopyPasteVars  = array("FAIclass","FAIclasses","FAIAllclasses");
41   function faiProfile($config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
45     $ldap=$this->config->get_ldap_link();
47     $this->acl = "#all#";
49     if($this->dn != "new"){
50       /* Set acls
51        */
52       $ui   = get_userinfo();
53       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
54       $acli = get_module_permission($acl, "FAIclass", $this->dn);
55       $this->acl=$acli;
56     }
58     /* Parse ldap attribute to get all assigned classes */
59     $tmp = split(" ",$this->FAIclass);
60     $tmp2 = array();
61     foreach($tmp as $class){
62       if(!empty($class)){
63         $tmp2[trim($class)] = trim($class);
64       }
65     }
67     if(isset($this->attrs['FAIstate'][0])){
68       $this->FAIstate = $this->attrs['FAIstate'][0];
69     }
71     /* Sort assigned classes */ 
72     if(is_array($tmp2)){
73       foreach($tmp2 as $class){
74         $this->FAIclasses[$class]=$class;
75       }
76     }
78     /* Search only in fai tree */
79     $ObjectTypes = array(
80         "FAIpartitionTable"  => array("OU"=>"ou=disk,"       ),
81         "FAIpackageList"     => array("OU"=>"ou=packages,"   ),
82         "FAIscript"          => array("OU"=>"ou=scripts,"    ),
83         "FAIvariable"        => array("OU"=>"ou=variables,"  ),
84         "FAIhook"            => array("OU"=>"ou=hooks,"      ),
85 #        "FAIprofile"         => array("OU"=>"ou=profiles,"  ),
86         "FAItemplate"        => array("OU"=>"ou=templates,"  ));
88     $base= "ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
89     if($_SESSION['faifilter']['branch']!="main"){
90       $base = $_SESSION['faifilter']['branch'];
91     }
93     /* Get ldap connection */
94     $ldap= $this->config->get_ldap_link();
95     $ldap->cd($base);
96     $sort = array();
97     /* Capture objects from given base */
98     $result = array();
99     foreach($ObjectTypes as $oc => $data){
100       $ou = $data['OU'].$base;
102       $ldap->ls("(objectClass=".$oc.")",$ou,array("cn","objectClass","dn"));
103       while($attrs = $ldap->fetch()){
104         $this->FAIAllclasses[$attrs['cn'][0]][$oc]=$attrs;
105         $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
106       }
107     }
108   
109     /* Sort the sort array */
110     //ksort($sort);
112     /* Reorder the FAIclasses array */
113     foreach($sort as $name){
114       $tmp[$name] =$this->FAIAllclasses[$name];
115     }
117     /* Assign sorted classes */
118     $this->FAIAllclasses = array();
119     $this->FAIAllclasses = $tmp;
121     if($dn != "new"){
122       $this->dn =$dn;
123     }
124     $this->old_cn   = $this->cn;
125   }
128   /* Combine new array, used for up down buttons */
129   function combineArrays($ar0,$ar1,$ar2)
130   {
131     $ret = array();
132     if(is_array($ar0))
133       foreach($ar0 as $ar => $a){
134         $ret[$ar]=$a;
135       }
136     if(is_array($ar1))
137       foreach($ar1 as $ar => $a){
138         $ret[$ar]=$a;
139       }
140     if(is_array($ar2))
141       foreach($ar2 as $ar => $a){
142         $ret[$ar]=$a;
143       }
144     return($ret);
145   }
147   /* returns position in array */
148   function getpos($atr,$attrs)
149   {
150     $i = 0;
151     foreach($attrs as $attr => $name)    {
152       $i++;
153       if($attr == $atr){
154         return($i);
155       }
156     }
157     return(-1);
158   }
160   /* Transports the given Arraykey one position up*/
161   function ArrayUp($atr,$attrs)
162   {
163     $ret = $attrs;
164     $pos = $this->getpos($atr,$attrs) ;
165     $cn = count($attrs);
166     if(!(($pos == -1)||($pos == 1))){
167       $before = array_slice($attrs,0,($pos-2));
168       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
169       $unten  = array_slice($attrs,$pos);
170       $ret = array();
171       $ret = $this->combineArrays($before,$mitte,$unten);
172     }
173     return($ret);
174   }
177   /* Transports the given Arraykey one position down*/
178   function ArrayDown($atr,$attrs)
179   {
180     $ret = $attrs;
181     $pos = $this->getpos($atr,$attrs) ;
182     $cn = count($attrs);
183     if(!(($pos == -1)||($pos == $cn))){
184       $before = array_slice($attrs,0,($pos-1));
185       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
186       $unten  = array_slice($attrs,($pos+1));
187       $ret = array();
188       $ret = $this->combineArrays($before,$mitte,$unten);
189     }
190     return($ret);
191   }
193   /* class one position up */
194   function catUp($id)
195   {
196     /* Get all cats depinding on current dir */
197     $cats = $this->FAIclasses;
198     $this->FAIclasses =$this->ArrayUp($id,$cats);
199   }
201   /* Class one position down */
202   function catDown($id)
203   {
204     /* Get all cats depinding on current dir */
205     $cats = $this->FAIclasses;
206     $this->FAIclasses =$this->ArrayDown($id,$cats);
207   }
209   function execute()
210   {
211     /* Call parent execute */
212     plugin::execute();
213     /* Fill templating stuff */
214     $smarty= get_smarty();
215     $display= "";
217     $s_entry = "";
218     $s_action = "";
220     /* Remove class name From list */
221     $sort_once = false;
222     foreach($_POST as $name => $post){
223       if(preg_match("/DEL_/i",$name)){
224         $s_action = "delete";
225         $s_entry  = preg_replace("/DEL_/","",$name);
226         $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
227       }elseif(preg_match("/Add_class/i",$name)){
228         $s_action  = "add";
229       }elseif(preg_match("/DelClass/i",$name)){
230         $s_action  = "delete";
231         $s_entry = $_POST['FAIclass'];
232       }elseif(preg_match("/AddClass/i",$name)){
233         $s_action  = "add";
234       }
236       /* Check if a list element should be pushed one position up */
237       if((preg_match("/sortup_/",$name))&&(!$sort_once)){
238         $sort_once = true;
239         $val = preg_replace("/sortup_/","",$name);
240         $val = preg_replace("/_.*$/","",$val);
241         $val = base64_decode($val);
242         $this->catUp($val);
243       }
244       
245       /* Check if a list element should be pushed one position down */
246       if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
247         $sort_once = true;
248         $val = preg_replace("/sortdown_/","",$name);
249         $val = preg_replace("/_.*$/","",$val);
250         $val = base64_decode($val);
251         $this->catDown($val);
252       }
254     }
256     if($s_action == "delete"){
257       unset($this->FAIclasses[$s_entry]);
258     }
260     if($s_action == "add"){
261       $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
262       $this->is_dialog  =true;
263     }
265     /* Save Dialog */
266     if(isset($_POST['SaveSubObject'])){
267       $this->dialog->save_object();
268       $msgs= $this->dialog->check();
269       if(count($msgs)){
270         print_red($msgs);
271       }else{
272         $ret = $this->dialog->save();
273         foreach($ret as $class){
274           $this->FAIclasses[$class] =$class; 
275         }
276         $this->is_dialog=false;
277         unset($this->dialog);
278         $this->dialog=NULL;
279         //ksort($this->FAIclasses);
280       }
281     }
283     /* Cancel Dialog */
284     if(isset($_POST['CancelSubObject'])){
285       $this->is_dialog=false;
286       unset($this->dialog);
287       $this->dialog=NULL;
288     }
290     if(isset($this->dialog)){
291       $this->dialog->save_object();
292       return($this->dialog->execute());
293     }
295     $divlist  =new divSelectBox("Profile");
296     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
298     /* item images */
299     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
300     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
301     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
302     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
303     $objTypes['FAIpackageList']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
304     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
306     /* Delete button */
307     $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
308     
309     /* Up down buttons */
310     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
311     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
313     /* Append fai classes to divlist */
314     foreach($this->FAIclasses as $usedClass){
315       $str = "&nbsp;";
317       if(isset($this->FAIAllclasses[$usedClass])){
318         foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
319           $str.= $objTypes[$class]; 
320         }
321       }
322   
323       $field1 = array("string"=> $usedClass,"attach"=>"");
324       $field2 = array("string"=> $str,"attach"=>"");
325       if($this->FAIstate != "freeze"){
326         $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
327             preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
328             "attach"=>"style='border-right:none;'");
329       }else{
330         $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
331       }
332       $divlist->AddEntry(array($field1,$field2,$field3));
333     }
335     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
336     $smarty->assign("divlist"     ,$divlist->DrawList());
338     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
339      * If we post the escaped strings they will be escaped again
340      */
341     foreach($this->attributes as $attrs){
342       if(get_magic_quotes_gpc()){
343         $smarty->assign($attrs,stripslashes($this->$attrs));
344       }else{
345         $smarty->assign($attrs,($this->$attrs));
346       }
347     }
349     foreach($this->attributes as $attr){
350       if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
351         $smarty->assign($attr."ACL"," disabled ");
352       }else{
353         $smarty->assign($attr."ACL","  ");
354       }
355     }
357     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
358     return($display);
359   }
361   function remove_from_parent()
362   {
363     $ldap = $this->config->get_ldap_link();
364     $ldap->cd ($this->dn);
365     $ldap->rmdir_recursive($this->dn);
366     show_ldap_error($ldap->get_error(), _("Removing FAI profile failed"));
367     $this->handle_post_events("remove");    
368   }
370   /* Save data to object 
371    */
372   function save_object()
373   {
374     plugin::save_object();
375     foreach($this->attributes as $attrs){
376       if(isset($_POST[$attrs])){
377         $this->$attrs = $_POST[$attrs];
378       }
379     }
380   }
383   /* Check supplied data */
384   function check()
385   {
386     /* Call common method to give check the hook */
387     $message= plugin::check();
389     if($this->copy_paste_mode){
391       /* If this is a new script, check if a script with this name already exists */
392       if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
394         /* Check if current name is already used for fai scripts in selected release */
395         $dn = 'cn='.$this->cn.",ou=profiles,".$this->release;
396         $ldap = $this->config->get_ldap_link();
397         $ldap->cat($dn);
398         if($ldap->count()){
400           $r =convert_department_dn($this->release);;
401           $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
402         }
403       }
404     }else{
405       if(count($this->FAIclasses) == 0){
406         $message[]=_("Please assign at least one class to this  profile.");
407       }
409       if(empty($this->cn)){
410         $message[]=_("Please enter a valid name.");
411       }
413       $ldap = $this->config->get_ldap_link();
414       $release = "ou=profiles,ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
415       if($_SESSION['faifilter']['branch']!="main"){
416         $release = "ou=profiles,".$_SESSION['faifilter']['branch'];
417       }
418       $ldap->cd($release);
419       if ($this->old_cn == ""){
420         $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
421       } else {
422         $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
423       }
424       if($ldap->count()){
425         $message[]=_("There is already a profile with this class name defined.");
426       }
427     }
428     return ($message);
429   }
432   /* Save to LDAP */
433   function save()
434   {
435     plugin::save();
437     $ldap = $this->config->get_ldap_link();
439     $this->FAIclass = "";
440     foreach($this->FAIclasses as $class){
441       $this->FAIclass.=$class." ";
442     }
444     $this->attrs['FAIclass']=trim($this->FAIclass);
446     /* Copy & Paste : Ensure that FAIstate is copied too */
447     if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
448       $this->attrs['FAIstate'] = $this->FAIstate;
449     }
451     $ldap->cat($this->dn,array("objectClass"));
452     if($ldap->count()!=0){
453       /* Write FAIscript to ldap*/
454       $ldap->cd($this->dn);
455       $this->cleanup();
456       $ldap->modify ($this->attrs); 
457     }else{
458       /* Write FAIscript to ldap*/
459       $ldap->cd($this->config->current['BASE']);
460       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
461       $ldap->cd($this->dn);
462       $ldap->add($this->attrs);
463     }
464     show_ldap_error($ldap->get_error(), _("Saving FAI profile failed"));
466     /* Do object tagging */
467     $this->handle_object_tagging();
468     show_ldap_error($ldap->get_error());
469   }
471   
472   /* return copy & paste dialog
473    */
474   function getCopyDialog()
475   {
476     /* Ask for cn */
477     $smarty = get_smarty();
478     $smarty->assign("cn" ,$this->cn);
479     $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
480     $ret = array();
481     $ret['string'] = $str;
482     $ret['status'] = "";
483     return($ret);
484   }
486   /* Get posted cn */
487   function saveCopyDialog()
488   {
489     if(isset($_POST['cn'])){
490       $this->cn = $_POST['cn'];
491     }
492   }
495 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
496 ?>