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 /* Search only in fai tree */
73 $ObjectTypes = array(
74 "FAIpartitionTable" => array("OU"=>"ou=disk," , "CHKBOX"=>"ShowPartitions"),
75 "FAIpackageList" => array("OU"=>"ou=packages," , "CHKBOX"=>"ShowPackages"),
76 "FAIscript" => array("OU"=>"ou=scripts," , "CHKBOX"=>"ShowScripts"),
77 "FAIvariable" => array("OU"=>"ou=variables," , "CHKBOX"=>"ShowVariables"),
78 "FAIhook" => array("OU"=>"ou=hooks," , "CHKBOX"=>"ShowHooks"),
79 # "FAIprofile" => array("OU"=>"ou=profiles," , "CHKBOX"=>"ShowProfiles"),
80 "FAItemplate" => array("OU"=>"ou=templates," , "CHKBOX"=>"ShowTemplates"));
82 $base= "ou=fai,ou=configs,ou=systems,".$_SESSION['CurrentMainBase'];
83 if($_SESSION['faifilter']['branch']!="main"){
84 $base = $_SESSION['faifilter']['branch'];
85 }
87 /* Get ldap connection */
88 $ldap= $this->config->get_ldap_link();
89 $ldap->cd($base);
91 /* Capture objects from given base */
92 $result = array();
93 foreach($ObjectTypes as $oc => $data){
94 $ou = $data['OU'].$base;
96 $ldap->ls("(objectClass=".$oc.")",$ou,array("cn","objectClass","dn"));
97 while($attrs = $ldap->fetch()){
98 $this->FAIAllclasses[$attrs['cn'][0]][$oc]=$attrs;
99 $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
100 }
101 }
103 /* Sort the sort array */
104 //ksort($sort);
106 /* Reorder the FAIclasses array */
107 foreach($sort as $name){
108 $tmp[$name] =$this->FAIAllclasses[$name];
109 }
111 /* Assign sorted classes */
112 $this->FAIAllclasses = array();
113 $this->FAIAllclasses = $tmp;
115 if($dn != "new"){
116 $this->dn =$dn;
117 }
118 $this->old_cn = $this->cn;
119 }
122 /* Combine new array, used for up down buttons */
123 function combineArrays($ar0,$ar1,$ar2)
124 {
125 $ret = array();
126 if(is_array($ar0))
127 foreach($ar0 as $ar => $a){
128 $ret[$ar]=$a;
129 }
130 if(is_array($ar1))
131 foreach($ar1 as $ar => $a){
132 $ret[$ar]=$a;
133 }
134 if(is_array($ar2))
135 foreach($ar2 as $ar => $a){
136 $ret[$ar]=$a;
137 }
138 return($ret);
139 }
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)){
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)){
222 $s_action = "add";
223 }elseif(preg_match("/DelClass/i",$name)){
224 $s_action = "delete";
225 $s_entry = $_POST['FAIclass'];
226 }elseif(preg_match("/AddClass/i",$name)){
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)){
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)){
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"){
251 unset($this->FAIclasses[$s_entry]);
252 }
254 if($s_action == "add"){
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 foreach($this->FAIclasses as $usedClass){
309 $str = " ";
311 if(isset($this->FAIAllclasses[$usedClass])){
312 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
313 $str.= $objTypes[$class];
314 }
315 }
317 $field1 = array("string"=> $usedClass,"attach"=>"");
318 $field2 = array("string"=> $str,"attach"=>"");
319 if($this->FAIstate != "freeze"){
320 $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
321 preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
322 "attach"=>"style='border-right:none;'");
323 }else{
324 $field3 = array("string"=>" ", "attach"=>"style='border-right:none;'");
325 }
326 $divlist->AddEntry(array($field1,$field2,$field3));
327 }
329 $smarty->assign("FAIclasses" ,$this->FAIclasses);
330 $smarty->assign("divlist" ,$divlist->DrawList());
332 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
333 * If we post the escaped strings they will be escaped again
334 */
335 foreach($this->attributes as $attrs){
336 if(get_magic_quotes_gpc()){
337 $smarty->assign($attrs,stripslashes($this->$attrs));
338 }else{
339 $smarty->assign($attrs,($this->$attrs));
340 }
341 }
343 foreach($this->attributes as $attr){
344 if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
345 $smarty->assign($attr."ACL"," disabled ");
346 }else{
347 $smarty->assign($attr."ACL"," ");
348 }
349 }
351 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
352 return($display);
353 }
355 function remove_from_parent()
356 {
357 $ldap = $this->config->get_ldap_link();
358 $ldap->cd ($this->dn);
359 $ldap->rmdir_recursive($this->dn);
360 show_ldap_error($ldap->get_error(), _("Removing FAI profile failed"));
361 $this->handle_post_events("remove");
362 }
364 /* Save data to object
365 */
366 function save_object()
367 {
368 plugin::save_object();
369 foreach($this->attributes as $attrs){
370 if(isset($_POST[$attrs])){
371 $this->$attrs = $_POST[$attrs];
372 }
373 }
374 }
377 /* Check supplied data */
378 function check()
379 {
380 /* Call common method to give check the hook */
381 $message= plugin::check();
383 if(count($this->FAIclasses) == 0){
384 $message[]=_("Please assign at least one class to this profile.");
385 }
387 if(empty($this->cn)){
388 $message[]=_("Please enter a valid name.");
389 }
391 $ldap = $this->config->get_ldap_link();
392 $ldap->cd($_SESSION['CurrentMainBase']);
393 if ($this->old_cn == ""){
394 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn."))",array("*"));
395 } else {
396 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
397 }
399 if($ldap->count()){
400 $message[]=_("There is already a profile with this class name defined.");
401 }
403 return ($message);
404 }
407 /* Save to LDAP */
408 function save()
409 {
410 plugin::save();
412 $ldap = $this->config->get_ldap_link();
414 $this->FAIclass = "";
415 foreach($this->FAIclasses as $class){
416 $this->FAIclass.=$class." ";
417 }
419 $this->attrs['FAIclass']=trim($this->FAIclass);
421 $ldap->cat($this->dn,array("objectClass"));
422 if($ldap->count()!=0){
423 /* Write FAIscript to ldap*/
424 $ldap->cd($this->dn);
425 $this->cleanup();
426 $ldap->modify ($this->attrs);
427 }else{
428 /* Write FAIscript to ldap*/
429 $ldap->cd($this->config->current['BASE']);
430 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
431 $ldap->cd($this->dn);
432 $ldap->add($this->attrs);
433 }
434 show_ldap_error($ldap->get_error(), _("Saving FAI profile failed"));
436 /* Do object tagging */
437 $this->handle_object_tagging();
438 show_ldap_error($ldap->get_error());
439 }
440 }
442 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
443 ?>