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 = "";
35 function faiProfile($config, $dn= NULL)
36 {
37 /* Load Attributes */
38 plugin::plugin ($config, $dn);
39 $ldap=$this->config->get_ldap_link();
41 $this->acl = "#all#";
43 if($this->dn != "new"){
44 /* Set acls
45 */
46 $ui = get_userinfo();
47 $acl = get_permissions ($this->dn, $ui->subtreeACL);
48 $acli = get_module_permission($acl, "FAIclass", $this->dn);
49 $this->acl=$acli;
50 }
52 /* Parse ldap attribute to get all assigned classes */
53 $tmp = split(" ",$this->FAIclass);
54 $tmp2 = array();
55 foreach($tmp as $class){
56 if(!empty($class)){
57 $tmp2[trim($class)] = trim($class);
58 }
59 }
61 if(isset($this->attrs['FAIstate'][0])){
62 $this->FAIstate = $this->attrs['FAIstate'][0];
63 }
65 /* Sort assigned classes */
66 if(is_array($tmp2)){
67 foreach($tmp2 as $class){
68 $this->FAIclasses[$class]=$class;
69 }
70 }
72 $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
74 /* Build filter */
75 $filter= "";
76 foreach ($categories as $cat){
77 $filter.= "(objectClass=$cat)";
78 }
80 /* Get ldap connection */
81 $base = $_SESSION['CurrentMainBase'];
82 $ldap->cd($base);
83 $sort = array();
85 /* search all FAI classes */
86 $ldap->search("(|$filter)",array("*"));
87 while($attrs = $ldap->fetch()){
89 /* Sort by categorie */
90 foreach($categories as $cat){
91 if(in_array($cat,$attrs['objectClass'])){
93 /* Append entry */
94 $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
96 /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
97 $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
98 }
99 }
100 }
102 /* Sort the sort array */
103 //ksort($sort);
105 /* Reorder the FAIclasses array */
106 foreach($sort as $name){
107 $tmp[$name] =$this->FAIAllclasses[$name];
108 }
110 /* Assign sorted classes */
111 $this->FAIAllclasses = array();
112 $this->FAIAllclasses = $tmp;
114 if($dn != "new"){
115 $this->dn =$dn;
116 }
117 $this->old_cn = $this->cn;
118 }
121 /* Combine new array, used for up down buttons */
122 function combineArrays($ar0,$ar1,$ar2)
123 {
124 $ret = array();
125 if(is_array($ar0))
126 foreach($ar0 as $ar => $a){
127 $ret[$ar]=$a;
128 }
129 if(is_array($ar1))
130 foreach($ar1 as $ar => $a){
131 $ret[$ar]=$a;
132 }
133 if(is_array($ar2))
134 foreach($ar2 as $ar => $a){
135 $ret[$ar]=$a;
136 }
137 return($ret);
138 }
140 /* returns position in array */
141 function getpos($atr,$attrs)
142 {
143 $i = 0;
144 foreach($attrs as $attr => $name) {
145 $i++;
146 if($attr == $atr){
147 return($i);
148 }
149 }
150 return(-1);
151 }
153 /* Transports the given Arraykey one position up*/
154 function ArrayUp($atr,$attrs)
155 {
156 $ret = $attrs;
157 $pos = $this->getpos($atr,$attrs) ;
158 $cn = count($attrs);
159 if(!(($pos == -1)||($pos == 1))){
160 $before = array_slice($attrs,0,($pos-2));
161 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
162 $unten = array_slice($attrs,$pos);
163 $ret = array();
164 $ret = $this->combineArrays($before,$mitte,$unten);
165 }
166 return($ret);
167 }
170 /* Transports the given Arraykey one position down*/
171 function ArrayDown($atr,$attrs)
172 {
173 $ret = $attrs;
174 $pos = $this->getpos($atr,$attrs) ;
175 $cn = count($attrs);
176 if(!(($pos == -1)||($pos == $cn))){
177 $before = array_slice($attrs,0,($pos-1));
178 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
179 $unten = array_slice($attrs,($pos+1));
180 $ret = array();
181 $ret = $this->combineArrays($before,$mitte,$unten);
182 }
183 return($ret);
184 }
186 /* class one position up */
187 function catUp($id)
188 {
189 /* Get all cats depinding on current dir */
190 $cats = $this->FAIclasses;
191 $this->FAIclasses =$this->ArrayUp($id,$cats);
192 }
194 /* Class one position down */
195 function catDown($id)
196 {
197 /* Get all cats depinding on current dir */
198 $cats = $this->FAIclasses;
199 $this->FAIclasses =$this->ArrayDown($id,$cats);
200 }
202 function execute()
203 {
204 /* Call parent execute */
205 plugin::execute();
206 /* Fill templating stuff */
207 $smarty= get_smarty();
208 $display= "";
210 $s_entry = "";
211 $s_action = "";
213 /* Remove class name From list */
214 $sort_once = false;
215 foreach($_POST as $name => $post){
216 if(preg_match("/DEL_/i",$name)){
217 $s_action = "delete";
218 $s_entry = preg_replace("/DEL_/","",$name);
219 $s_entry = base64_decode(preg_replace("/_.*$/","",$s_entry));
220 }elseif(preg_match("/Add_class/i",$name)){
221 $s_action = "add";
222 }elseif(preg_match("/DelClass/i",$name)){
223 $s_action = "delete";
224 $s_entry = $_POST['FAIclass'];
225 }elseif(preg_match("/AddClass/i",$name)){
226 $s_action = "add";
227 }
229 /* Check if a list element should be pushed one position up */
230 if((preg_match("/sortup_/",$name))&&(!$sort_once)){
231 $sort_once = true;
232 $val = preg_replace("/sortup_/","",$name);
233 $val = preg_replace("/_.*$/","",$val);
234 $val = base64_decode($val);
235 $this->catUp($val);
236 }
238 /* Check if a list element should be pushed one position down */
239 if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
240 $sort_once = true;
241 $val = preg_replace("/sortdown_/","",$name);
242 $val = preg_replace("/_.*$/","",$val);
243 $val = base64_decode($val);
244 $this->catDown($val);
245 }
247 }
249 if($s_action == "delete"){
250 unset($this->FAIclasses[$s_entry]);
251 }
253 if($s_action == "add"){
254 $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
255 $this->is_dialog =true;
256 }
258 /* Save Dialog */
259 if(isset($_POST['SaveSubObject'])){
260 $this->dialog->save_object();
261 $msgs= $this->dialog->check();
262 if(count($msgs)){
263 print_red($msgs);
264 }else{
265 $ret = $this->dialog->save();
266 foreach($ret as $class){
267 $this->FAIclasses[$class] =$class;
268 }
269 $this->is_dialog=false;
270 unset($this->dialog);
271 $this->dialog=NULL;
272 //ksort($this->FAIclasses);
273 }
274 }
276 /* Cancel Dialog */
277 if(isset($_POST['CancelSubObject'])){
278 $this->is_dialog=false;
279 unset($this->dialog);
280 $this->dialog=NULL;
281 }
283 if(isset($this->dialog)){
284 $this->dialog->save_object();
285 return($this->dialog->execute());
286 }
288 $divlist =new divSelectBox("Profile");
289 $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
291 /* item images */
292 $objTypes['FAIhook'] = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
293 $objTypes['FAItemplate'] = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
294 $objTypes['FAIscript'] = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
295 $objTypes['FAIvariable'] = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
296 $objTypes['FAIpackageList'] = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
297 $objTypes['FAIpartitionTable'] = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
299 /* Delete button */
300 $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>";
302 /* Up down buttons */
303 $linkupdown = " <input type='image' name='sortup_%s' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top' >";
304 $linkupdown.= "<input type='image' name='sortdown_%s' alt='down' title='"._("Down")."' src='images/sort_down.png' >";
306 /* Append fai classes to divlist */
307 foreach($this->FAIclasses as $usedClass){
308 $str = " ";
310 if(isset($this->FAIAllclasses[$usedClass])){
311 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
312 $str.= $objTypes[$class];
313 }
314 }
316 $field1 = array("string"=> $usedClass,"attach"=>"");
317 $field2 = array("string"=> $str,"attach"=>"");
318 if($this->FAIstate != "freeze"){
319 $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
320 preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
321 "attach"=>"style='border-right:none;'");
322 }else{
323 $field3 = array("string"=>" ", "attach"=>"style='border-right:none;'");
324 }
325 $divlist->AddEntry(array($field1,$field2,$field3));
326 }
328 $smarty->assign("FAIclasses" ,$this->FAIclasses);
329 $smarty->assign("divlist" ,$divlist->DrawList());
331 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
332 * If we post the escaped strings they will be escaped again
333 */
334 foreach($this->attributes as $attrs){
335 if(get_magic_quotes_gpc()){
336 $smarty->assign($attrs,stripslashes($this->$attrs));
337 }else{
338 $smarty->assign($attrs,($this->$attrs));
339 }
340 }
342 foreach($this->attributes as $attr){
343 if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
344 $smarty->assign($attr."ACL"," disabled ");
345 }else{
346 $smarty->assign($attr."ACL"," ");
347 }
348 }
350 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
351 return($display);
352 }
354 function remove_from_parent()
355 {
356 $ldap = $this->config->get_ldap_link();
357 $ldap->cd ($this->dn);
358 $ldap->rmdir_recursive($this->dn);
359 show_ldap_error($ldap->get_error(), sprintf(_("Removing of FAI/profile with dn '%s' failed."),$this->dn));
360 $this->handle_post_events("remove");
361 }
363 /* Save data to object
364 */
365 function save_object()
366 {
367 plugin::save_object();
368 foreach($this->attributes as $attrs){
369 if(isset($_POST[$attrs])){
370 $this->$attrs = $_POST[$attrs];
371 }
372 }
373 }
376 /* Check supplied data */
377 function check()
378 {
379 /* Call common method to give check the hook */
380 $message= plugin::check();
382 if(count($this->FAIclasses) == 0){
383 $message[]=_("Please assign at least one class to this profile.");
384 }
386 if(empty($this->cn)){
387 $message[]=_("Please enter a valid name.");
388 }
390 $ldap = $this->config->get_ldap_link();
391 $ldap->cd($_SESSION['CurrentMainBase']);
392 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
394 if($ldap->count()){
395 $message[]=_("There is already a profile with this class name defined.");
396 }
398 return ($message);
399 }
402 /* Save to LDAP */
403 function save()
404 {
405 plugin::save();
407 $ldap = $this->config->get_ldap_link();
409 $this->FAIclass = "";
410 foreach($this->FAIclasses as $class){
411 $this->FAIclass.=$class." ";
412 }
414 $this->attrs['FAIclass']=trim($this->FAIclass);
416 $ldap->cat($this->dn,array("objectClass"));
417 if($ldap->count()!=0){
418 /* Write FAIscript to ldap*/
419 $ldap->cd($this->dn);
420 $this->cleanup();
421 $ldap->modify ($this->attrs);
422 }else{
423 /* Write FAIscript to ldap*/
424 $ldap->cd($this->config->current['BASE']);
425 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
426 $ldap->cd($this->dn);
427 $ldap->add($this->attrs);
428 }
429 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
431 /* Do object tagging */
432 $this->handle_object_tagging();
433 show_ldap_error($ldap->get_error());
434 }
435 }
437 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
438 ?>