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 = "";
31 function faiProfile($config, $dn= NULL)
32 {
33 /* Load Attributes */
34 plugin::plugin ($config, $dn);
35 $ldap=$this->config->get_ldap_link();
37 $this->ui = get_userinfo();
39 /* Parse ldap attribute to get all assigned classes */
40 $tmp = split(" ",$this->FAIclass);
41 $tmp2 = array();
42 foreach($tmp as $class){
43 if(!empty($class)){
44 $tmp2[trim($class)] = trim($class);
45 }
46 }
48 if(isset($this->attrs['FAIstate'][0])){
49 $this->FAIstate = $this->attrs['FAIstate'][0];
50 }
52 /* Sort assigned classes */
53 if(is_array($tmp2)){
54 foreach($tmp2 as $class){
55 $this->FAIclasses[$class]=$class;
56 }
57 }
59 $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
61 /* Build filter */
62 $filter= "";
63 foreach ($categories as $cat){
64 $filter.= "(objectClass=$cat)";
65 }
67 /* Get ldap connection */
68 $base = $_SESSION['CurrentMainBase'];
69 $ldap->cd($base);
70 $sort = array();
72 /* search all FAI classes */
73 $ldap->search("(|$filter)",array("*"));
74 while($attrs = $ldap->fetch()){
76 /* Sort by categorie */
77 foreach($categories as $cat){
78 if(in_array($cat,$attrs['objectClass'])){
80 /* Append entry */
81 $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
83 /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
84 $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
85 }
86 }
87 }
89 /* Sort the sort array */
90 //ksort($sort);
92 /* Reorder the FAIclasses array */
93 foreach($sort as $name){
94 $tmp[$name] =$this->FAIAllclasses[$name];
95 }
97 /* Assign sorted classes */
98 $this->FAIAllclasses = array();
99 $this->FAIAllclasses = $tmp;
101 if($dn != "new"){
102 $this->dn =$dn;
103 }
104 $this->old_cn = $this->cn;
105 }
108 /* Combine new array, used for up down buttons */
109 function combineArrays($ar0,$ar1,$ar2)
110 {
111 $ret = array();
112 if(is_array($ar0))
113 foreach($ar0 as $ar => $a){
114 $ret[$ar]=$a;
115 }
116 if(is_array($ar1))
117 foreach($ar1 as $ar => $a){
118 $ret[$ar]=$a;
119 }
120 if(is_array($ar2))
121 foreach($ar2 as $ar => $a){
122 $ret[$ar]=$a;
123 }
124 return($ret);
125 }
128 function acl_base_for_current_object($dn)
129 {
130 if($dn == "new"){
131 if($this->dn == "new"){
132 $dn= $_SESSION['CurrentMainBase'];
133 }else{
134 $dn = $this->dn;
135 }
136 }
137 return($dn);
138 }
141 /* returns position in array */
142 function getpos($atr,$attrs)
143 {
144 $i = 0;
145 foreach($attrs as $attr => $name) {
146 $i++;
147 if($attr == $atr){
148 return($i);
149 }
150 }
151 return(-1);
152 }
154 /* Transports the given Arraykey one position up*/
155 function ArrayUp($atr,$attrs)
156 {
157 $ret = $attrs;
158 $pos = $this->getpos($atr,$attrs) ;
159 $cn = count($attrs);
160 if(!(($pos == -1)||($pos == 1))){
161 $before = array_slice($attrs,0,($pos-2));
162 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
163 $unten = array_slice($attrs,$pos);
164 $ret = array();
165 $ret = $this->combineArrays($before,$mitte,$unten);
166 }
167 return($ret);
168 }
171 /* Transports the given Arraykey one position down*/
172 function ArrayDown($atr,$attrs)
173 {
174 $ret = $attrs;
175 $pos = $this->getpos($atr,$attrs) ;
176 $cn = count($attrs);
177 if(!(($pos == -1)||($pos == $cn))){
178 $before = array_slice($attrs,0,($pos-1));
179 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
180 $unten = array_slice($attrs,($pos+1));
181 $ret = array();
182 $ret = $this->combineArrays($before,$mitte,$unten);
183 }
184 return($ret);
185 }
187 /* class one position up */
188 function catUp($id)
189 {
190 /* Get all cats depinding on current dir */
191 $cats = $this->FAIclasses;
192 $this->FAIclasses =$this->ArrayUp($id,$cats);
193 }
195 /* Class one position down */
196 function catDown($id)
197 {
198 /* Get all cats depinding on current dir */
199 $cats = $this->FAIclasses;
200 $this->FAIclasses =$this->ArrayDown($id,$cats);
201 }
203 function execute()
204 {
205 /* Call parent execute */
206 plugin::execute();
207 /* Fill templating stuff */
208 $smarty= get_smarty();
209 $display= "";
211 $s_entry = "";
212 $s_action = "";
214 /* Remove class name From list */
215 $sort_once = false;
216 foreach($_POST as $name => $post){
217 if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
218 $s_action = "delete";
219 $s_entry = preg_replace("/DEL_/","",$name);
220 $s_entry = base64_decode(preg_replace("/_.*$/","",$s_entry));
221 }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
222 $s_action = "add";
223 }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
224 $s_action = "delete";
225 $s_entry = $_POST['FAIclass'];
226 }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
227 $s_action = "add";
228 }
230 /* Check if a list element should be pushed one position up */
231 if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
232 $sort_once = true;
233 $val = preg_replace("/sortup_/","",$name);
234 $val = preg_replace("/_.*$/","",$val);
235 $val = base64_decode($val);
236 $this->catUp($val);
237 }
239 /* Check if a list element should be pushed one position down */
240 if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
241 $sort_once = true;
242 $val = preg_replace("/sortdown_/","",$name);
243 $val = preg_replace("/_.*$/","",$val);
244 $val = base64_decode($val);
245 $this->catDown($val);
246 }
248 }
250 if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
251 unset($this->FAIclasses[$s_entry]);
252 }
254 if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
255 $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
256 $this->is_dialog =true;
257 }
259 /* Save Dialog */
260 if(isset($_POST['SaveSubObject'])){
261 $this->dialog->save_object();
262 $msgs= $this->dialog->check();
263 if(count($msgs)){
264 print_red($msgs);
265 }else{
266 $ret = $this->dialog->save();
267 foreach($ret as $class){
268 $this->FAIclasses[$class] =$class;
269 }
270 $this->is_dialog=false;
271 unset($this->dialog);
272 $this->dialog=NULL;
273 //ksort($this->FAIclasses);
274 }
275 }
277 /* Cancel Dialog */
278 if(isset($_POST['CancelSubObject'])){
279 $this->is_dialog=false;
280 unset($this->dialog);
281 $this->dialog=NULL;
282 }
284 if(isset($this->dialog)){
285 $this->dialog->save_object();
286 return($this->dialog->execute());
287 }
289 $divlist =new divSelectBox("Profile");
290 $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
292 /* item images */
293 $objTypes['FAIhook'] = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
294 $objTypes['FAItemplate'] = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
295 $objTypes['FAIscript'] = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
296 $objTypes['FAIvariable'] = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
297 $objTypes['FAIpackageList'] = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
298 $objTypes['FAIpartitionTable'] = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
300 /* Delete button */
301 $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>";
303 /* Up down buttons */
304 $linkupdown = " <input type='image' name='sortup_%s' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top' >";
305 $linkupdown.= "<input type='image' name='sortdown_%s' alt='down' title='"._("Down")."' src='images/sort_down.png' >";
307 /* Append fai classes to divlist */
308 if($this->acl_is_readable("FAIclass")){
309 foreach($this->FAIclasses as $usedClass){
310 $str = " ";
311 $act = "";
313 if(isset($this->FAIAllclasses[$usedClass])){
314 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
315 $str.= $objTypes[$class];
316 }
317 }
319 $field1 = array("string"=> $usedClass,"attach"=>"");
320 $field2 = array("string"=> $str,"attach"=>"");
321 if(($this->FAIstate != "freeze") && $this->acl_is_writeable("FAIclass")){
322 $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
323 preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
324 "attach"=>"style='border-right:none;'");
325 }else{
326 $field3 = array("string"=>" ", "attach"=>"style='border-right:none;'");
327 }
328 $divlist->AddEntry(array($field1,$field2,$field3));
329 }
330 }
332 $smarty->assign("FAIclasses" ,$this->FAIclasses);
333 $smarty->assign("divlist" ,$divlist->DrawList());
335 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
336 * If we post the escaped strings they will be escaped again
337 */
338 foreach($this->attributes as $attrs){
339 if(get_magic_quotes_gpc()){
340 $smarty->assign($attrs,stripslashes($this->$attrs));
341 }else{
342 $smarty->assign($attrs,($this->$attrs));
343 }
344 }
347 $dn = $this->acl_base_for_current_object($this->dn);
348 $smarty->assign("sub_object_is_addable",
349 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
350 !preg_match("/freeze/",$this->FAIstate));
352 $tmp = $this->plInfo();
353 foreach($tmp['plProvidedAcls'] as $name => $translated){
354 $smarty->assign($name."ACL",$this->getacl($name));
355 }
357 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
358 return($display);
359 }
361 function remove_from_parent()
362 {
363 $ldap = $this->config->get_ldap_link();
364 $ldap->cd ($this->dn);
366 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
367 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
368 if($_SESSION['faifilter']['branch'] == "main"){
369 $use_dn = $this->dn;
370 }
372 prepare_to_save_FAI_object($use_dn,array(),true);
373 $this->handle_post_events("remove");
374 }
377 /* Save data to object
378 */
379 function save_object()
380 {
381 plugin::save_object();
382 }
385 /* Check supplied data */
386 function check()
387 {
388 /* Call common method to give check the hook */
389 $message= plugin::check();
391 if(count($this->FAIclasses) == 0){
392 $message[]=_("Please assign at least one class to this profile.");
393 }
395 if(empty($this->cn)){
396 $message[]=_("Please enter a valid name.");
397 }
399 $ldap = $this->config->get_ldap_link();
400 $ldap->cd($_SESSION['CurrentMainBase']);
401 if ($this->old_cn == ""){
402 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
403 } else {
404 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
405 }
407 if($ldap->count()){
408 $message[]=_("There is already a profile with this class name defined.");
409 }
411 return ($message);
412 }
415 /* Save to LDAP */
416 function save()
417 {
418 plugin::save();
420 $ldap = $this->config->get_ldap_link();
422 $this->FAIclass = "";
423 foreach($this->FAIclasses as $class){
424 $this->FAIclass.=$class." ";
425 }
427 $this->attrs['FAIclass']=trim($this->FAIclass);
429 prepare_to_save_FAI_object($this->dn,$this->attrs);
431 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
433 /* Do object tagging */
434 $this->handle_object_tagging();
435 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
436 }
439 /* Return plugin informations for acl handling */
440 function plInfo()
441 {
442 return (array(
443 "plShortName" => _("Profile"),
444 "plDescription" => _("FAI profile"),
445 "plSelfModify" => FALSE,
446 "plDepends" => array(),
447 "plPriority" => 30,
448 "plSection" => array("administration"),
449 "plCategory" => array("fai"),
450 "plProvidedAcls" => array(
451 "cn" => _("Name"),
452 "description" => _("Description"),
453 "FAIclass" => _("FAI classes"))
454 ));
455 }
456 }
458 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
459 ?>