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 $FAIclass = ""; // Classnames used for this profile
25 var $FAIclasses = array(); // Contains classname seperated in an array
26 var $FAIAllclasses = array(); // Contains all possible Classnames
27 var $ui ;
28 var $FAIstate = "";
29 var $view_logged = FALSE;
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();
208 if($this->is_account && !$this->view_logged){
209 $this->view_logged = TRUE;
210 new log("view","fai/".get_class($this),$this->dn);
211 }
213 /* Fill templating stuff */
214 $smarty= get_smarty();
215 $display= "";
217 $s_entry = "";
218 $s_action = "";
220 /* Remove class name From list */
221 $sort_once = false;
222 foreach($_POST as $name => $post){
223 if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
224 $s_action = "delete";
225 $s_entry = preg_replace("/DEL_/","",$name);
226 $s_entry = base64_decode(preg_replace("/_.*$/","",$s_entry));
227 }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
228 $s_action = "add";
229 }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
230 $s_action = "delete";
231 $s_entry = $_POST['FAIclass'];
232 }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
233 $s_action = "add";
234 }
236 /* Check if a list element should be pushed one position up */
237 if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
238 $sort_once = true;
239 $val = preg_replace("/sortup_/","",$name);
240 $val = preg_replace("/_.*$/","",$val);
241 $val = base64_decode($val);
242 $this->catUp($val);
243 }
245 /* Check if a list element should be pushed one position down */
246 if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
247 $sort_once = true;
248 $val = preg_replace("/sortdown_/","",$name);
249 $val = preg_replace("/_.*$/","",$val);
250 $val = base64_decode($val);
251 $this->catDown($val);
252 }
254 }
256 if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
257 unset($this->FAIclasses[$s_entry]);
258 }
260 if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
261 $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
262 $this->is_dialog =true;
263 }
265 /* Save Dialog */
266 if(isset($_POST['SaveSubObject'])){
267 $this->dialog->save_object();
268 $msgs= $this->dialog->check();
269 if(count($msgs)){
270 print_red($msgs);
271 }else{
272 $ret = $this->dialog->save();
273 foreach($ret as $class){
274 $this->FAIclasses[$class] =$class;
275 }
276 $this->is_dialog=false;
277 unset($this->dialog);
278 $this->dialog=FALSE;
279 //ksort($this->FAIclasses);
280 }
281 }
283 /* Cancel Dialog */
284 if(isset($_POST['CancelSubObject'])){
285 $this->is_dialog=false;
286 unset($this->dialog);
287 $this->dialog=FALSE;
288 }
290 if(is_object($this->dialog)){
291 $this->dialog->save_object();
292 return($this->dialog->execute());
293 }
295 $divlist =new divSelectBox("Profile");
296 $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
298 /* item images */
299 $objTypes['FAIhook'] = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
300 $objTypes['FAItemplate'] = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
301 $objTypes['FAIscript'] = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
302 $objTypes['FAIvariable'] = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
303 $objTypes['FAIpackageList'] = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
304 $objTypes['FAIpartitionTable'] = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
306 /* Delete button */
307 $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>";
309 /* Up down buttons */
310 $linkupdown = " <input type='image' name='sortup_%s' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top' >";
311 $linkupdown.= "<input type='image' name='sortdown_%s' alt='down' title='"._("Down")."' src='images/sort_down.png' >";
313 /* Append fai classes to divlist */
314 if($this->acl_is_readable("FAIclass")){
315 foreach($this->FAIclasses as $usedClass){
316 $str = " ";
317 $act = "";
319 if(isset($this->FAIAllclasses[$usedClass])){
320 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
321 $str.= $objTypes[$class];
322 }
323 }
325 $field1 = array("string"=> $usedClass,"attach"=>"");
326 $field2 = array("string"=> $str,"attach"=>"");
327 if(($this->FAIstate != "freeze") && $this->acl_is_writeable("FAIclass")){
328 $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
329 preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
330 "attach"=>"style='border-right:none;'");
331 }else{
332 $field3 = array("string"=>" ", "attach"=>"style='border-right:none;'");
333 }
334 $divlist->AddEntry(array($field1,$field2,$field3));
335 }
336 }
338 $smarty->assign("FAIclasses" ,$this->FAIclasses);
339 $smarty->assign("divlist" ,$divlist->DrawList());
341 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
342 * If we post the escaped strings they will be escaped again
343 */
344 foreach($this->attributes as $attrs){
345 if(get_magic_quotes_gpc()){
346 $smarty->assign($attrs,stripslashes($this->$attrs));
347 }else{
348 $smarty->assign($attrs,($this->$attrs));
349 }
350 }
353 $dn = $this->acl_base_for_current_object($this->dn);
354 $smarty->assign("sub_object_is_addable",
355 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
356 !preg_match("/freeze/",$this->FAIstate));
358 $tmp = $this->plInfo();
359 foreach($tmp['plProvidedAcls'] as $name => $translated){
360 $smarty->assign($name."ACL",$this->getacl($name));
361 }
363 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
364 return($display);
365 }
367 function remove_from_parent()
368 {
369 $ldap = $this->config->get_ldap_link();
370 $ldap->cd ($this->dn);
372 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
373 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
374 if($_SESSION['faifilter']['branch'] == "main"){
375 $use_dn = $this->dn;
376 }
378 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
379 prepare_to_save_FAI_object($use_dn,array(),true);
380 $this->handle_post_events("remove");
381 }
384 /* Save data to object
385 */
386 function save_object()
387 {
388 plugin::save_object();
389 }
392 /* Check supplied data */
393 function check()
394 {
395 /* Call common method to give check the hook */
396 $message= plugin::check();
398 if(count($this->FAIclasses) == 0){
399 $message[]=_("Please assign at least one class to this profile.");
400 }
402 if(empty($this->cn)){
403 $message[]=_("Please enter a valid name.");
404 }
406 $ldap = $this->config->get_ldap_link();
407 $ldap->cd($_SESSION['CurrentMainBase']);
408 if ($this->old_cn == ""){
409 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
410 } else {
411 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
412 }
414 if($ldap->count() || isset($this->FAIAllclasses[$this->cn])){
415 $message[]=_("There is already a FAI object with this class name defined.");
416 }
418 return ($message);
419 }
422 /* Save to LDAP */
423 function save()
424 {
425 plugin::save();
427 $ldap = $this->config->get_ldap_link();
429 $this->FAIclass = "";
430 foreach($this->FAIclasses as $class){
431 $this->FAIclass.=$class." ";
432 }
434 $this->attrs['FAIclass']=trim($this->FAIclass);
436 prepare_to_save_FAI_object($this->dn,$this->attrs);
438 if($this->initially_was_account){
439 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
440 }else{
441 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
442 }
444 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
446 /* Do object tagging */
447 $this->handle_object_tagging();
448 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/profile with dn '%s' failed."),$this->dn));
449 }
452 /* Return plugin informations for acl handling */
453 function plInfo()
454 {
455 return (array(
456 "plShortName" => _("Profile"),
457 "plDescription" => _("FAI profile"),
458 "plSelfModify" => FALSE,
459 "plDepends" => array(),
460 "plPriority" => 30,
461 "plSection" => array("administration"),
462 "plCategory" => array("fai"),
463 "plProvidedAcls" => array(
464 "cn" => _("Name"),
465 "description" => _("Description"),
466 "FAIclass" => _("FAI classes"))
467 ));
468 }
469 }
471 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
472 ?>