Code

Fixed fai package.
[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
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->acl = "#all#";
39     if($this->dn != "new"){
40       /* Set acls
41        */
42       $ui   = get_userinfo();
43       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
44       $acli = get_module_permission($acl, "FAIclass", $this->dn);
45       $this->acl=$acli;
46     }
48     /* Parse ldap attribute to get all assigned classes */
49     $tmp = split(" ",$this->FAIclass);
50     $tmp2 = array();
51     foreach($tmp as $class){
52       if(!empty($class)){
53         $tmp2[trim($class)] = trim($class);
54       }
55     }
57     if(isset($this->attrs['FAIstate'][0])){
58       $this->FAIstate = $this->attrs['FAIstate'][0];
59     }
61     /* Sort assigned classes */ 
62     if(is_array($tmp2)){
63       foreach($tmp2 as $class){
64         $this->FAIclasses[$class]=$class;
65       }
66     }
68     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
70     /* Build filter */
71     $filter= "";
72     foreach ($categories as $cat){
73       $filter.= "(objectClass=$cat)";
74     }
75     
76     /* Get ldap connection */ 
77     $base = $_SESSION['CurrentMainBase'];
78     $ldap->cd($base);
79     $sort = array();
81     /* search all FAI classes */
82     $ldap->search("(|$filter)",array("*"));
83     while($attrs = $ldap->fetch()){
85       /* Sort by categorie */
86       foreach($categories as $cat){
87         if(in_array($cat,$attrs['objectClass'])){
89           /* Append entry */
90           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
91   
92           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
93           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
94         }
95       }
96     } 
98     /* Sort the sort array */
99     //ksort($sort);
101     /* Reorder the FAIclasses array */
102     foreach($sort as $name){
103       $tmp[$name] =$this->FAIAllclasses[$name];
104     }
106     /* Assign sorted classes */
107     $this->FAIAllclasses = array();
108     $this->FAIAllclasses = $tmp;
110     if($dn != "new"){
111       $this->dn =$dn;
112     }
113     $this->old_cn   = $this->cn;
114   }
117   /* Combine new array, used for up down buttons */
118   function combineArrays($ar0,$ar1,$ar2)
119   {
120     $ret = array();
121     if(is_array($ar0))
122       foreach($ar0 as $ar => $a){
123         $ret[$ar]=$a;
124       }
125     if(is_array($ar1))
126       foreach($ar1 as $ar => $a){
127         $ret[$ar]=$a;
128       }
129     if(is_array($ar2))
130       foreach($ar2 as $ar => $a){
131         $ret[$ar]=$a;
132       }
133     return($ret);
134   }
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();
202     /* Fill templating stuff */
203     $smarty= get_smarty();
204     $display= "";
206     $s_entry = "";
207     $s_action = "";
209     /* Remove class name From list */
210     $sort_once = false;
211     foreach($_POST as $name => $post){
212       if(preg_match("/DEL_/i",$name)){
213         $s_action = "delete";
214         $s_entry  = preg_replace("/DEL_/","",$name);
215         $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
216       }elseif(preg_match("/Add_class/i",$name)){
217         $s_action  = "add";
218       }elseif(preg_match("/DelClass/i",$name)){
219         $s_action  = "delete";
220         $s_entry = $_POST['FAIclass'];
221       }elseif(preg_match("/AddClass/i",$name)){
222         $s_action  = "add";
223       }
225       /* Check if a list element should be pushed one position up */
226       if((preg_match("/sortup_/",$name))&&(!$sort_once)){
227         $sort_once = true;
228         $val = preg_replace("/sortup_/","",$name);
229         $val = preg_replace("/_.*$/","",$val);
230         $val = base64_decode($val);
231         $this->catUp($val);
232       }
233       
234       /* Check if a list element should be pushed one position down */
235       if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
236         $sort_once = true;
237         $val = preg_replace("/sortdown_/","",$name);
238         $val = preg_replace("/_.*$/","",$val);
239         $val = base64_decode($val);
240         $this->catDown($val);
241       }
243     }
245     if($s_action == "delete"){
246       unset($this->FAIclasses[$s_entry]);
247     }
249     if($s_action == "add"){
250       $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
251       $this->is_dialog  =true;
252     }
254     /* Save Dialog */
255     if(isset($_POST['SaveSubObject'])){
256       $this->dialog->save_object();
257       $msgs= $this->dialog->check();
258       if(count($msgs)){
259         print_red($msgs);
260       }else{
261         $ret = $this->dialog->save();
262         foreach($ret as $class){
263           $this->FAIclasses[$class] =$class; 
264         }
265         $this->is_dialog=false;
266         unset($this->dialog);
267         $this->dialog=NULL;
268         //ksort($this->FAIclasses);
269       }
270     }
272     /* Cancel Dialog */
273     if(isset($_POST['CancelSubObject'])){
274       $this->is_dialog=false;
275       unset($this->dialog);
276       $this->dialog=NULL;
277     }
279     if(isset($this->dialog)){
280       $this->dialog->save_object();
281       return($this->dialog->execute());
282     }
284     $divlist  =new divSelectBox("Profile");
285     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
287     /* item images */
288     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
289     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
290     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
291     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
292     $objTypes['FAIpackageList']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
293     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
295     /* Delete button */
296     $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
297     
298     /* Up down buttons */
299     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
300     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
302     /* Append fai classes to divlist */
303     foreach($this->FAIclasses as $usedClass){
304       $str = "&nbsp;";
306       if(isset($this->FAIAllclasses[$usedClass])){
307         foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
308           $str.= $objTypes[$class]; 
309         }
310       }
311   
312       $field1 = array("string"=> $usedClass,"attach"=>"");
313       $field2 = array("string"=> $str,"attach"=>"");
314       if($this->FAIstate != "freeze"){
315         $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
316             preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
317             "attach"=>"style='border-right:none;'");
318       }else{
319         $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
320       }
321       $divlist->AddEntry(array($field1,$field2,$field3));
322     }
324     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
325     $smarty->assign("divlist"     ,$divlist->DrawList());
327     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
328      * If we post the escaped strings they will be escaped again
329      */
330     foreach($this->attributes as $attrs){
331       if(get_magic_quotes_gpc()){
332         $smarty->assign($attrs,stripslashes($this->$attrs));
333       }else{
334         $smarty->assign($attrs,($this->$attrs));
335       }
336     }
338     foreach($this->attributes as $attr){
339       if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
340         $smarty->assign($attr."ACL"," disabled ");
341       }else{
342         $smarty->assign($attr."ACL","  ");
343       }
344     }
346     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
347     return($display);
348   }
350   function remove_from_parent()
351   {
352     $ldap = $this->config->get_ldap_link();
353     $ldap->cd ($this->dn);
355 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
356     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
357     if($_SESSION['faifilter']['branch'] == "main"){
358       $use_dn = $this->dn;
359     }
361     prepare_to_save_FAI_object($use_dn,array(),true);
362     $this->handle_post_events("remove");    
363   }
366   /* Save data to object 
367    */
368   function save_object()
369   {
370     plugin::save_object();
371     foreach($this->attributes as $attrs){
372       if(isset($_POST[$attrs])){
373         $this->$attrs = $_POST[$attrs];
374       }
375     }
376   }
379   /* Check supplied data */
380   function check()
381   {
382     /* Call common method to give check the hook */
383     $message= plugin::check();
385     if(count($this->FAIclasses) == 0){
386       $message[]=_("Please assign at least one class to this  profile.");
387     }
389     if(empty($this->cn)){
390       $message[]=_("Please enter a valid name.");
391     }
393     $ldap = $this->config->get_ldap_link();
394     $ldap->cd($_SESSION['CurrentMainBase']);
395     if ($this->old_cn == ""){
396       $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
397     } else {
398       $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
399     }
401     if($ldap->count()){
402       $message[]=_("There is already a profile with this class name defined.");
403     }
405     return ($message);
406   }
409   /* Save to LDAP */
410   function save()
411   {
412     plugin::save();
414     $ldap = $this->config->get_ldap_link();
416     $this->FAIclass = "";
417     foreach($this->FAIclasses as $class){
418       $this->FAIclass.=$class." ";
419     }
421     $this->attrs['FAIclass']=trim($this->FAIclass);
423     prepare_to_save_FAI_object($this->dn,$this->attrs);
424     
425     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
427     /* Do object tagging */
428     $this->handle_object_tagging();
429     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
430   }
433   /* Return plugin informations for acl handling */ 
434   function plInfo()
435   {
436     return (array( 
437           "plShortName" => _("Profile"),
438           "plDescription" => _("FAI profile"),
439           "plSelfModify"  => FALSE,
440           "plDepends"     => array(),
441           "plPriority"    => 0,
442           "plSection"     => array("administration"),
443           "plCategory"    => array("fai"),
444           "plProvidedAcls" => array(
445             "cn"                => _("Name"),
446             "description"       => _("Description"),
447             "FAIclass"          => _("FAI classes"))
448           ));
449   }
452 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
453 ?>