Code

3daa2424d77f3ad669c546a99db65953cbea0e57
[gosa.git] / trunk / gosa-plugins / fai / 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   /* Specific attributes */
15   var $old_cn           = "";
16   var $cn               = "";       // The class name for this object
17   var $description      = "";       // The description for this set of partitions
18   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
19   var $FAIclass         = "";       // Classnames used for this profile
20   var $FAIclasses       = array();  // Contains classname seperated in an array
21   var $FAIAllclasses    = array();  // Contains all possible Classnames
22   var $ui               ;
23   var $FAIstate      = "";
24   var $view_logged      = FALSE;
26   function faiProfile(&$config, $dn= NULL)
27   {
28     /* Load Attributes */
29     plugin::plugin ($config, $dn);
30     $ldap=$this->config->get_ldap_link();
32     $this->ui = get_userinfo();
34     /* Parse ldap attribute to get all assigned classes */
35     $tmp = split(" ",$this->FAIclass);
36     $tmp2 = array();
37     foreach($tmp as $class){
38       if(!empty($class)){
39         $class = trim($class);
40         $tmp2[] = $class;
41       }
42     }
44     /* Sort assigned classes */ 
45     if(is_array($tmp2)){
46       foreach($tmp2 as $class){
47         $this->FAIclasses[]=$class;
48       }
49     }
51     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
53     /* Build filter */
54     $filter= "";
55     foreach ($categories as $cat){
56       $filter.= "(objectClass=$cat)";
57     }
58     
59     /* Get ldap connection */ 
60     $base  = session::get('CurrentMainBase');
61     $ldap->cd($base);
62     $sort = array();
64     /* search all FAI classes */
65     $ldap->search("(|$filter)",array("*"));
66     while($attrs = $ldap->fetch()){
68       /* Sort by categorie */
69       foreach($categories as $cat){
70         if(in_array($cat,$attrs['objectClass'])){
72           /* Append entry */
73           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
74   
75           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
76           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
77         }
78       }
79     } 
81     /* Sort the sort array */
82     //ksort($sort);
84     /* Reorder the FAIclasses array */
85     foreach($sort as $name){
86       $tmp[$name] =$this->FAIAllclasses[$name];
87     }
89     /* Assign sorted classes */
90     $this->FAIAllclasses = array();
91     $this->FAIAllclasses = $tmp;
93     if($dn != "new"){
94       $this->dn =$dn;
95     }
96     $this->old_cn   = $this->cn;
97   }
99   function acl_base_for_current_object($dn)
100   {
101     if($dn == "new" || $dn == ""){
102       if($this->dn == "new"){
103         $dn= $this->parent->parent->acl_base;
104       }else{
105         $dn = $this->dn;
106       }
107     }
108     return($dn);
109   }
112   /* returns position in array */
113   function getpos($atr,$attrs)
114   {
115     $i = 0;
116     foreach($attrs as $value) {
117       if($atr == $value) {
118         return($i);
119       }
120       $i++;
121     }
122   }
124   /* Transports the given Arraykey one position up*/
125   function ArrayUp($atr,$attrs)
126   {
127     $index = $this->getpos($atr,$attrs);
128     $res = $attrs;
130     /* Don't do anything if there is just one element */
131     if (count($attrs) == 1) {
132       return($res);
133     }
135     for($i=0;$i<count($attrs);$i++) {
136       if ($i==$index) {
137         $left_index = $index - 1;
138         $res[$left_index] = $attrs[$index];
139         $res[$index] = $attrs[$left_index];
140       }
141     }
143     return($res);
144   }
147   /* Transports the given Arraykey one position down*/
148   function ArrayDown($atr,$attrs)
149   {
150     $index = $this->getpos($atr,$attrs);
151     $res = $attrs;
153     /* Don't do anything if there is just one element */
154     if (count($attrs) == 1) {
155       return($res);
156     }
158     for($i=0;$i<count($attrs);$i++) {
159       if ($i==$index) {
160         $left_index = ($index+1);
161         $res[$left_index] = $attrs[$index];
162         $res[$index] = $attrs[$left_index];
163       }
164     }
166     return($res);
167   }
169   /* class one position up */
170   function catUp($id)
171   {
172     /* Get all cats depinding on current dir */
173     $cats = $this->FAIclasses;
174     $this->FAIclasses =$this->ArrayUp($id,$cats);
175   }
177   /* Class one position down */
178   function catDown($id)
179   {
180     /* Get all cats depinding on current dir */
181     $cats = $this->FAIclasses;
182     $this->FAIclasses =$this->ArrayDown($id,$cats);
183   }
185   function execute()
186   {
187     /* Call parent execute */
188     plugin::execute();
190     if($this->is_account && !$this->view_logged){
191       $this->view_logged = TRUE;
192       new log("view","fai/".get_class($this),$this->dn);
193     }
195     /* Fill templating stuff */
196     $smarty= get_smarty();
197     $display= "";
199     $s_entry = "";
200     $s_action = "";
202     /* Remove class name From list */
203     $sort_once = false;
204     if(!preg_match("/freeze/",$this->FAIstate)){
205       foreach($_POST as $name => $post){
206         if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
207           $s_action = "delete";
208           $s_entry  = preg_replace("/DEL_/","",$name);
209           $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
210         }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
211           $s_action  = "add";
212         }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
213           $s_action  = "delete";
214           $s_entry = $_POST['FAIclass'];
215         }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
216           $s_action  = "add";
217         }
219         /* Check if a list element should be pushed one position up */
220         if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
221           $sort_once = true;
222           $val = preg_replace("/sortup_/","",$name);
223           $val = preg_replace("/_.*$/","",$val);
224           $val = base64_decode($val);
225           $this->catUp($val);
226         }
228         /* Check if a list element should be pushed one position down */
229         if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
230           $sort_once = true;
231           $val = preg_replace("/sortdown_/","",$name);
232           $val = preg_replace("/_.*$/","",$val);
233           $val = base64_decode($val);
234           $this->catDown($val);
235         }
237       }
239       if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
240         $newfaiclasses = array();
241         foreach($this->FAIclasses as $key => $name) {
242           if ($name != $s_entry) {
243             array_push($newfaiclasses, $name);
244           }
245         }
246         $this->FAIclasses = $newfaiclasses;
247       }
249       if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
250         $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses,$this->parent->parent->fai_release);
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           msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
260         }else{
261           $ret = $this->dialog->save();
262           foreach($ret as $class){
263             $this->FAIclasses[] =$class; 
264           }
265           $this->is_dialog=false;
266           unset($this->dialog);
267           $this->dialog=FALSE;
268           //ksort($this->FAIclasses);
269         }
270       }
271     }
273     /* Cancel Dialog */
274     if(isset($_POST['CancelSubObject'])){
275       $this->is_dialog=false;
276       unset($this->dialog);
277       $this->dialog=FALSE;
278     }
280     if(is_object($this->dialog)){
281       $this->dialog->save_object();
282       return($this->dialog->execute());
283     }
285     $divlist  =new divSelectBox("Profile");
286     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
288     /* item images */
289     $objTypes['FAIhook']            = "<img src='plugins/fai/images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
290     $objTypes['FAItemplate']        = "<img src='plugins/fai/images/fai_template.png' title='"._("Template bundle")."' alt=''>";
291     $objTypes['FAIscript']          = "<img src='plugins/fai/images/fai_script.png' title='"._("Script bundle")."' alt=''>";
292     $objTypes['FAIvariable']        = "<img src='plugins/fai/images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
293     $objTypes['FAIpackageList']        = "<img src='plugins/fai/images/fai_packages.png' title='"._("Package bundle")."' alt=''>";
294     $objTypes['FAIpartitionTable']  = "<img src='plugins/fai/images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
296     /* Delete button */
297     $actions = "<input type='image' src='images/lists/trash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
298     
299     /* Up down buttons */
300     $linkup = " &nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/lists/sort-up.png' align='top' >";
301     $linkdown = " &nbsp;<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/lists/sort-down.png' >";
302     /* Append fai classes to divlist */
303     if($this->acl_is_readable("FAIclass")){
304       $i = 1;
305       $count = count($this->FAIclasses);
307       $linkupdown = '';
308       foreach($this->FAIclasses as $usedClass){
309         if($i==1) {
310           if ($count > 1) {
311             $linkupdown = "&nbsp;&nbsp;&nbsp;&nbsp;" . $linkdown;
312           }
313         } elseif ($i<$count) {
314           $linkupdown = $linkup . $linkdown;    
315         }
316         else {
317           $linkupdown = $linkup;
318         }
320         $str = "&nbsp;";
321         $act = "";
323         if(isset($this->FAIAllclasses[$usedClass])){
324           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
325             $str.= $objTypes[$class]; 
326           }
327         }
329         $field1 = array("string"=> $usedClass,"attach"=>"");
330         $field2 = array("string"=> $str,"attach"=>"");
331         if(!preg_match("/freeze/", $this->FAIstate) && $this->acl_is_writeable("FAIclass")){
332           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
333               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
334               "attach"=>"style='border-right:none;'");
335         }else{
336           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
337         }
338         $divlist->AddEntry(array($field1,$field2,$field3));
339         $i++;
340       }
341     }
343     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
345     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
346     $smarty->assign("divlist"     ,$divlist->DrawList());
348     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
349      * If we post the escaped strings they will be escaped again
350      */
351     foreach($this->attributes as $attrs){
352       if(get_magic_quotes_gpc()){
353         $smarty->assign($attrs,stripslashes($this->$attrs));
354       }else{
355         $smarty->assign($attrs,($this->$attrs));
356       }
357     }
359     
360     $dn = $this->acl_base_for_current_object($this->dn);
361     $smarty->assign("sub_object_is_addable",
362         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
363         !preg_match("/freeze/",$this->FAIstate));
365     $tmp = $this->plInfo();
366     foreach($tmp['plProvidedAcls'] as $name => $translated){
367       $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
368     }
370     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
371     return($display);
372   }
374   function remove_from_parent()
375   {
376     $ldap = $this->config->get_ldap_link();
377     $ldap->cd ($this->dn);
378     $release = $this->parent->parent->fai_release;
379     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
380     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
381     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
382     $this->handle_post_events("remove");    
383   }
386   /* Save data to object 
387    */
388   function save_object()
389   {
390     if(!preg_match("/freeze/",$this->FAIstate)){
391       plugin::save_object();
392     }
393   }
396   /* Check supplied data */
397   function check()
398   {
399     /* Call common method to give check the hook */
400     $message= plugin::check();
402     if(count($this->FAIclasses) == 0){
403       $message[]=_("No class specified for this profile!");
404     }
406     if($this->cn == ""){
407       $message[]= msgPool::required(_("Name"));
408     }
410     /* Ensure that we do not overwrite an allready existing entry 
411      */
412     if($this->dn == "new" || $this->cn != $this->old_cn){
413       $release = $this->parent->parent->fai_release;
414       $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
415       $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
416       if(isset($res[$this->cn])){
417         $message[] = msgPool::duplicated(_("Name"));
418       }
419     }
421     return ($message);
422   }
425   /* Save to LDAP */
426   function save()
427   {
428     plugin::save();
430     $ldap = $this->config->get_ldap_link();
432     $this->FAIclass = "";
433     foreach($this->FAIclasses as $class){
434       $this->FAIclass.=$class." ";
435     }
437     $this->attrs['FAIclass']=trim($this->FAIclass);
439     /* Remove the old FAI profile, if the dn has changed.
440      */
441     if($this->cn != $this->old_cn && $this->old_cn != ""){
443       $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
444       FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
445     }
446     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
447    
448     if($this->initially_was_account){
449       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
450     }else{
451       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
452     }
453   }
456   function PrepareForCopyPaste($source)
457   {
458     plugin::PrepareForCopyPaste($source);
460     /* Parse ldap attribute to get all assigned classes */
461     $tmp = split(" ",$this->FAIclass);
462     $tmp2 = array();
463     foreach($tmp as $class){
464       if(!empty($class)){
465         $class = trim($class);
466         $tmp2[] = $class;
467       }
468     }
470     /* Sort assigned classes */
471     if(is_array($tmp2)){
472       foreach($tmp2 as $class){
473         $this->FAIclasses[]=$class;
474       }
475     }
476   }
479   /* Return plugin informations for acl handling */ 
480   static function plInfo()
481   {
482     return (array( 
483           "plShortName" => _("Profile"),
484           "plDescription" => _("FAI profile"),
485           "plSelfModify"  => FALSE,
486           "plDepends"     => array(),
487           "plPriority"    => 30,
488           "plSection"     => array("administration"),
489           "plCategory"    => array("fai"),
490           "plProvidedAcls" => array(
491             "cn"                => _("Name"),
492             "description"       => _("Description"),
493             "FAIclass"          => _("FAI classes"))
494           ));
495   }
498   /*! \brief  Used for copy & paste.
499               Returns a HTML input mask, which allows to change the cn of this entry.
500       @param  Array   Array containing current status && a HTML template.
501    */
502   function getCopyDialog()
503   {
504     $vars = array("cn");
505     $smarty = get_smarty();
506     $smarty->assign("cn", htmlentities($this->cn));
507     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
508     $ret = array();
509     $ret['string'] = $str;
510     $ret['status'] = "";
511     return($ret);
512   }
515   /*! \brief  Used for copy & paste.
516               Some entries must be renamed to avaoid duplicate entries.
517    */
518   function saveCopyDialog()
519   {
520     if(isset($_POST['cn'])){
521       $this->cn = get_post('cn');
522     }
523   }
526 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
527 ?>