Code

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