Code

Fixed some typos
[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   function faiProfile($config, $dn= NULL)
34   {
35     /* Load Attributes */
36     plugin::plugin ($config, $dn);
37     $ldap=$this->config->get_ldap_link();
39     $tmp = split(" ",$this->FAIclass);
40     $tmp2 = array();
41     foreach($tmp as $class){
42       if(!empty($class)){
43         $tmp2[trim($class)] = trim($class);
44       }
45     }
46     
47     if(is_array($tmp2)){
48       foreach($tmp2 as $class){
49         $this->FAIclasses[$class]=$class;
50       }
51     }
53     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTAble","FAIpackage");
54     $sort = array();
55     $base = $_SESSION['faifilter']['base'];
56     $ldap->cd($base);
57     $ldap->search("(objectClass=*)",array("*"));
58     while($attrs = $ldap->fetch()){
59       foreach($categories as $cat){
60         if(in_array($cat,$attrs['objectClass'])){
61           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
62           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
63         }
64       }
65     } 
66     ksort($sort);
67     foreach($sort as $name){
68       $tmp[$name] =$this->FAIAllclasses[$name];
69     }
70     $this->FAIAllclasses = array();
71     $this->FAIAllclasses = $tmp;
73     if($dn != "new"){
74       $this->dn =$dn;
75     }
76     $this->old_cn   = $this->cn;
77   }
80   /* Combine new array */
81   function combineArrays($ar0,$ar1,$ar2)
82   {
83     $ret = array();
84     if(is_array($ar0))
85       foreach($ar0 as $ar => $a){
86         $ret[$ar]=$a;
87       }
88     if(is_array($ar1))
89       foreach($ar1 as $ar => $a){
90         $ret[$ar]=$a;
91       }
92     if(is_array($ar2))
93       foreach($ar2 as $ar => $a){
94         $ret[$ar]=$a;
95       }
96     return($ret);
97   }
99   function getpos($atr,$attrs)
100   {
101     $i = 0;
102     foreach($attrs as $attr => $name)    {
103       $i++;
104       if($attr == $atr){
105         return($i);
106       }
107     }
108     return(-1);
109   }
111   /* TRansports the geiven Arraykey one position up*/
112   function ArrayUp($atr,$attrs)
113   {
114     $ret = $attrs;
115     $pos = $this->getpos($atr,$attrs) ;
116     $cn = count($attrs);
117     if(!(($pos == -1)||($pos == 1))){
118       $before = array_slice($attrs,0,($pos-2));
119       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
120       $unten  = array_slice($attrs,$pos);
121       $ret = array();
122       $ret = $this->combineArrays($before,$mitte,$unten);
123     }
124     return($ret);
125   }
128   /* TRansports the geiven Arraykey one position up*/
129   function ArrayDown($atr,$attrs)
130   {
131     $ret = $attrs;
132     $pos = $this->getpos($atr,$attrs) ;
133     $cn = count($attrs);
134     if(!(($pos == -1)||($pos == $cn))){
135       $before = array_slice($attrs,0,($pos-1));
136       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
137       $unten  = array_slice($attrs,($pos+1));
138       $ret = array();
139       $ret = $this->combineArrays($before,$mitte,$unten);
140     }
141     return($ret);
142   }
144   function catUp($id)
145   {
146     /* Get all cats depinding on current dir */
147     $cats = $this->FAIclasses;
148     $this->FAIclasses =$this->ArrayUp($id,$cats);
149   }
151   function catDown($id)
152   {
153     /* Get all cats depinding on current dir */
154     $cats = $this->FAIclasses;
155     $this->FAIclasses =$this->ArrayDown($id,$cats);
156   }
158   function execute()
159   {
160     /* Call parent execute */
161     plugin::execute();
162     /* Fill templating stuff */
163     $smarty= get_smarty();
164     $display= "";
166     $s_entry = "";
167     $s_action = "";
169     /* Remove class name From list */
170     $sort_once = false;
171     foreach($_POST as $name => $post){
172       if(preg_match("/DEL_/i",$name)){
173         $s_action = "delete";
174         $s_entry  = preg_replace("/DEL_/","",$name);
175         $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
176       }elseif(preg_match("/Add_class/i",$name)){
177         $s_action  = "add";
178       }elseif(preg_match("/DelClass/i",$name)){
179         $s_action  = "delete";
180         $s_entry = $_POST['FAIclass'];
181       }elseif(preg_match("/AddClass/i",$name)){
182         $s_action  = "add";
183       }
185       if((preg_match("/sortup_/",$name))&&(!$sort_once)){
186         $sort_once = true;
187         $val = preg_replace("/sortup_/","",$name);
188         $val = preg_replace("/_.*$/","",$val);
189         $val = base64_decode($val);
190         $this->catUp($val);
191       }
192       
193       if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
194         $sort_once = true;
195         $val = preg_replace("/sortdown_/","",$name);
196         $val = preg_replace("/_.*$/","",$val);
197         $val = base64_decode($val);
198         $this->catDown($val);
199       }
201     }
203     if($s_action == "delete"){
204       unset($this->FAIclasses[$s_entry]);
205     }
207     if($s_action == "add"){
208       $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
209       $this->is_dialog  =true;
210     }
212     /* Save Dialog */
213     if(isset($_POST['SaveSubObject'])){
214       $this->dialog->save_object();
215       $msgs= $this->dialog->check();
216       if(count($msgs)){
217         print_red($msgs);
218       }else{
219         $ret = $this->dialog->save();
220         foreach($ret as $class){
221           $this->FAIclasses[$class] =$class; 
222         }
223         $this->is_dialog=false;
224         unset($this->dialog);
225         $this->dialog=NULL;
226         ksort($this->FAIclasses);
227       }
228     }
230     /* Cancel Dialog */
231     if(isset($_POST['CancelSubObject'])){
232       $this->is_dialog=false;
233       unset($this->dialog);
234       $this->dialog=NULL;
235     }
237     if(isset($this->dialog)){
238       $this->dialog->save_object();
239       return($this->dialog->execute());
240     }
242     $divlist  =new divSelectBox("Profile");
243     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
245     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
246     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
247     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
248     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
249     $objTypes['FAIpackages']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
250     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
252     $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
254     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
255     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
257     foreach($this->FAIclasses as $usedClass){
258       $str = "&nbsp;";
261       if(isset($this->FAIAllclasses[$usedClass])){
262         foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
263           $str.= $objTypes[$class]; 
264         }
265       }
267           
268   
269       $field1 = array("string"=> $usedClass,"attach"=>"");
270       $field2 = array("string"=> $str,"attach"=>"");
271       $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).preg_replace("/%s/",base64_encode($usedClass),$linkupdown),"attach"=>"style='border-right:none;'");
272       $divlist->AddEntry(array($field1,$field2,$field3));
273     }
275     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
276     $smarty->assign("divlist"     ,$divlist->DrawList());
278     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
279      * If we post the escaped strings they will be escaped again
280      */
281     foreach($this->attributes as $attrs){
282       if(get_magic_quotes_gpc()){
283         $smarty->assign($attrs,stripslashes($this->$attrs));
284       }else{
285         $smarty->assign($attrs,($this->$attrs));
286       }
287     }
290     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
291     return($display);
292   }
294   function remove_from_parent()
295   {
296     $ldap = $this->config->get_ldap_link();
297     $ldap->cd ($this->dn);
298     $ldap->rmdir_recursive($this->dn);
299     $this->handle_post_events("remove");    
300   }
302   /* Save data to object 
303    */
304   function save_object()
305   {
306     plugin::save_object();
307     foreach($this->attributes as $attrs){
308       if(isset($_POST[$attrs])){
309         $this->$attrs = $_POST[$attrs];
310       }
311     }
312   }
315   /* Check supplied data */
316   function check()
317   {
318     $message= array();
320     if(count($this->FAIclasses) == 0){
321       $message[]=_("Please assign at least one class to this  profile.");
322     }
324     if(empty($this->cn)){
325       $message[]=_("Please enter a valid name.");
326     }
328     $ldap = $this->config->get_ldap_link();
329     $ldap->cd($_SESSION['faifilter']['base']);
330     $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
332     if($ldap->count()){
333       $message[]=_("There is already a profile with this class name defined.");
334     }
336     return ($message);
337   }
340   /* Save to LDAP */
341   function save()
342   {
343     plugin::save();
345     $ldap = $this->config->get_ldap_link();
347     $this->FAIclass = "";
348     foreach($this->FAIclasses as $class){
349       $this->FAIclass.=$class." ";
350     }
352     $this->attrs['FAIclass']=trim($this->FAIclass);
354     $ldap->cat($this->dn);
355     if($ldap->count()!=0){
356       /* Write FAIscript to ldap*/
357       $ldap->cd($this->dn);
358       $ldap->modify($this->attrs);
359     }else{
360       /* Write FAIscript to ldap*/
361       $ldap->cd($this->config->current['BASE']);
362       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
363       $ldap->cd($this->dn);
364       $ldap->add($this->attrs);
365     }
366     show_ldap_error($ldap->get_error());
367   }
370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
371 ?>