Code

0c8ef80a9ea99b2337b9633d7f5acfb80c861576
[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      = "";
30   var $view_logged      = FALSE;
32   function faiProfile($config, $dn= NULL)
33   {
34     /* Load Attributes */
35     plugin::plugin ($config, $dn);
36     $ldap=$this->config->get_ldap_link();
38     $this->ui = get_userinfo();
40     /* Parse ldap attribute to get all assigned classes */
41     $tmp = split(" ",$this->FAIclass);
42     $tmp2 = array();
43     foreach($tmp as $class){
44       if(!empty($class)){
45         $tmp2[trim($class)] = trim($class);
46       }
47     }
49     if(isset($this->attrs['FAIstate'][0])){
50       $this->FAIstate = $this->attrs['FAIstate'][0];
51     }
53     /* Sort assigned classes */ 
54     if(is_array($tmp2)){
55       foreach($tmp2 as $class){
56         $this->FAIclasses[$class]=$class;
57       }
58     }
60     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
62     /* Build filter */
63     $filter= "";
64     foreach ($categories as $cat){
65       $filter.= "(objectClass=$cat)";
66     }
67     
68     /* Get ldap connection */ 
69     $base = $_SESSION['CurrentMainBase'];
70     $ldap->cd($base);
71     $sort = array();
73     /* search all FAI classes */
74     $ldap->search("(|$filter)",array("*"));
75     while($attrs = $ldap->fetch()){
77       /* Sort by categorie */
78       foreach($categories as $cat){
79         if(in_array($cat,$attrs['objectClass'])){
81           /* Append entry */
82           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
83   
84           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
85           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
86         }
87       }
88     } 
90     /* Sort the sort array */
91     //ksort($sort);
93     /* Reorder the FAIclasses array */
94     foreach($sort as $name){
95       $tmp[$name] =$this->FAIAllclasses[$name];
96     }
98     /* Assign sorted classes */
99     $this->FAIAllclasses = array();
100     $this->FAIAllclasses = $tmp;
102     if($dn != "new"){
103       $this->dn =$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();
209     if($this->is_account && !$this->view_logged){
210       $this->view_logged = TRUE;
211       @log::log("view","fai/".get_class($this),$this->dn);
212     }
214     /* Fill templating stuff */
215     $smarty= get_smarty();
216     $display= "";
218     $s_entry = "";
219     $s_action = "";
221     /* Remove class name From list */
222     $sort_once = false;
223     foreach($_POST as $name => $post){
224       if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
225         $s_action = "delete";
226         $s_entry  = preg_replace("/DEL_/","",$name);
227         $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
228       }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
229         $s_action  = "add";
230       }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
231         $s_action  = "delete";
232         $s_entry = $_POST['FAIclass'];
233       }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
234         $s_action  = "add";
235       }
237       /* Check if a list element should be pushed one position up */
238       if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
239         $sort_once = true;
240         $val = preg_replace("/sortup_/","",$name);
241         $val = preg_replace("/_.*$/","",$val);
242         $val = base64_decode($val);
243         $this->catUp($val);
244       }
245       
246       /* Check if a list element should be pushed one position down */
247       if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
248         $sort_once = true;
249         $val = preg_replace("/sortdown_/","",$name);
250         $val = preg_replace("/_.*$/","",$val);
251         $val = base64_decode($val);
252         $this->catDown($val);
253       }
255     }
257     if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
258       unset($this->FAIclasses[$s_entry]);
259     }
261     if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
262       $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
263       $this->is_dialog  =true;
264     }
266     /* Save Dialog */
267     if(isset($_POST['SaveSubObject'])){
268       $this->dialog->save_object();
269       $msgs= $this->dialog->check();
270       if(count($msgs)){
271         print_red($msgs);
272       }else{
273         $ret = $this->dialog->save();
274         foreach($ret as $class){
275           $this->FAIclasses[$class] =$class; 
276         }
277         $this->is_dialog=false;
278         unset($this->dialog);
279         $this->dialog=NULL;
280         //ksort($this->FAIclasses);
281       }
282     }
284     /* Cancel Dialog */
285     if(isset($_POST['CancelSubObject'])){
286       $this->is_dialog=false;
287       unset($this->dialog);
288       $this->dialog=NULL;
289     }
291     if(isset($this->dialog)){
292       $this->dialog->save_object();
293       return($this->dialog->execute());
294     }
296     $divlist  =new divSelectBox("Profile");
297     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
299     /* item images */
300     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
301     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
302     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
303     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
304     $objTypes['FAIpackageList']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
305     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
307     /* Delete button */
308     $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
309     
310     /* Up down buttons */
311     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
312     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
314     /* Append fai classes to divlist */
315     if($this->acl_is_readable("FAIclass")){
316       foreach($this->FAIclasses as $usedClass){
317         $str = "&nbsp;";
318         $act = "";
320         if(isset($this->FAIAllclasses[$usedClass])){
321           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
322             $str.= $objTypes[$class]; 
323           }
324         }
326         $field1 = array("string"=> $usedClass,"attach"=>"");
327         $field2 = array("string"=> $str,"attach"=>"");
328         if(($this->FAIstate != "freeze") && $this->acl_is_writeable("FAIclass")){
329           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
330               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
331               "attach"=>"style='border-right:none;'");
332         }else{
333           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
334         }
335         $divlist->AddEntry(array($field1,$field2,$field3));
336       }
337     }
339     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
340     $smarty->assign("divlist"     ,$divlist->DrawList());
342     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
343      * If we post the escaped strings they will be escaped again
344      */
345     foreach($this->attributes as $attrs){
346       if(get_magic_quotes_gpc()){
347         $smarty->assign($attrs,stripslashes($this->$attrs));
348       }else{
349         $smarty->assign($attrs,($this->$attrs));
350       }
351     }
353     
354     $dn = $this->acl_base_for_current_object($this->dn);
355     $smarty->assign("sub_object_is_addable",
356         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
357         !preg_match("/freeze/",$this->FAIstate));
359     $tmp = $this->plInfo();
360     foreach($tmp['plProvidedAcls'] as $name => $translated){
361       $smarty->assign($name."ACL",$this->getacl($name));
362     }
364     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
365     return($display);
366   }
368   function remove_from_parent()
369   {
370     $ldap = $this->config->get_ldap_link();
371     $ldap->cd ($this->dn);
373 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
374     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
375     if($_SESSION['faifilter']['branch'] == "main"){
376       $use_dn = $this->dn;
377     }
379     @log::log("remove","fai/".get_class($this),$use_dn,$this->attributes);
380     prepare_to_save_FAI_object($use_dn,array(),true);
381     $this->handle_post_events("remove");    
382   }
385   /* Save data to object 
386    */
387   function save_object()
388   {
389     plugin::save_object();
390   }
393   /* Check supplied data */
394   function check()
395   {
396     /* Call common method to give check the hook */
397     $message= plugin::check();
399     if(count($this->FAIclasses) == 0){
400       $message[]=_("Please assign at least one class to this  profile.");
401     }
403     if(empty($this->cn)){
404       $message[]=_("Please enter a valid name.");
405     }
407     $ldap = $this->config->get_ldap_link();
408     $ldap->cd($_SESSION['CurrentMainBase']);
409     if ($this->old_cn == ""){
410       $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
411     } else {
412       $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
413     }
415     if($ldap->count()){
416       $message[]=_("There is already a profile with this class name defined.");
417     }
419     return ($message);
420   }
423   /* Save to LDAP */
424   function save()
425   {
426     plugin::save();
428     $ldap = $this->config->get_ldap_link();
430     $this->FAIclass = "";
431     foreach($this->FAIclasses as $class){
432       $this->FAIclass.=$class." ";
433     }
435     $this->attrs['FAIclass']=trim($this->FAIclass);
437     prepare_to_save_FAI_object($this->dn,$this->attrs);
438    
439     if($this->initially_was_account){
440       @log::log("modify","fai/".get_class($this),$this->dn,$this->attributes);
441     }else{
442       @log::log("create","fai/".get_class($this),$this->dn,$this->attributes);
443     }
444  
445     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
447     /* Do object tagging */
448     $this->handle_object_tagging();
449     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
450   }
453   /* Return plugin informations for acl handling */ 
454   function plInfo()
455   {
456     return (array( 
457           "plShortName" => _("Profile"),
458           "plDescription" => _("FAI profile"),
459           "plSelfModify"  => FALSE,
460           "plDepends"     => array(),
461           "plPriority"    => 30,
462           "plSection"     => array("administration"),
463           "plCategory"    => array("fai"),
464           "plProvidedAcls" => array(
465             "cn"                => _("Name"),
466             "description"       => _("Description"),
467             "FAIclass"          => _("FAI classes"))
468           ));
469   }
472 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
473 ?>