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 /* 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 /* Sort assigned classes */
49 if(is_array($tmp2)){
50 foreach($tmp2 as $class){
51 $this->FAIclasses[$class]=$class;
52 }
53 }
55 $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackage");
57 /* Build filter */
58 $filter= "";
59 foreach ($categories as $cat){
60 $filter.= "(objectClass=$cat)";
61 }
63 /* Get ldap connection */
64 $base = $_SESSION['faifilter']['base'];
65 $ldap->cd($base);
66 $sort = array();
68 /* search all FAI classes */
69 $ldap->search("(|$filter)",array("*"));
70 while($attrs = $ldap->fetch()){
72 /* Sort by categorie */
73 foreach($categories as $cat){
74 if(in_array($cat,$attrs['objectClass'])){
76 /* Append entry */
77 $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
79 /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
80 $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
81 }
82 }
83 }
85 /* Sort the sort array */
86 ksort($sort);
88 /* Reorder the FAIclasses array */
89 foreach($sort as $name){
90 $tmp[$name] =$this->FAIAllclasses[$name];
91 }
93 /* Assign sorted classes */
94 $this->FAIAllclasses = array();
95 $this->FAIAllclasses = $tmp;
97 if($dn != "new"){
98 $this->dn =$dn;
99 }
100 $this->old_cn = $this->cn;
101 }
104 /* Combine new array, used for up down buttons */
105 function combineArrays($ar0,$ar1,$ar2)
106 {
107 $ret = array();
108 if(is_array($ar0))
109 foreach($ar0 as $ar => $a){
110 $ret[$ar]=$a;
111 }
112 if(is_array($ar1))
113 foreach($ar1 as $ar => $a){
114 $ret[$ar]=$a;
115 }
116 if(is_array($ar2))
117 foreach($ar2 as $ar => $a){
118 $ret[$ar]=$a;
119 }
120 return($ret);
121 }
123 /* returns position in array */
124 function getpos($atr,$attrs)
125 {
126 $i = 0;
127 foreach($attrs as $attr => $name) {
128 $i++;
129 if($attr == $atr){
130 return($i);
131 }
132 }
133 return(-1);
134 }
136 /* Transports the given Arraykey one position up*/
137 function ArrayUp($atr,$attrs)
138 {
139 $ret = $attrs;
140 $pos = $this->getpos($atr,$attrs) ;
141 $cn = count($attrs);
142 if(!(($pos == -1)||($pos == 1))){
143 $before = array_slice($attrs,0,($pos-2));
144 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
145 $unten = array_slice($attrs,$pos);
146 $ret = array();
147 $ret = $this->combineArrays($before,$mitte,$unten);
148 }
149 return($ret);
150 }
153 /* Transports the given Arraykey one position down*/
154 function ArrayDown($atr,$attrs)
155 {
156 $ret = $attrs;
157 $pos = $this->getpos($atr,$attrs) ;
158 $cn = count($attrs);
159 if(!(($pos == -1)||($pos == $cn))){
160 $before = array_slice($attrs,0,($pos-1));
161 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
162 $unten = array_slice($attrs,($pos+1));
163 $ret = array();
164 $ret = $this->combineArrays($before,$mitte,$unten);
165 }
166 return($ret);
167 }
169 /* class one position up */
170 function catUp($id)
171 {
172 /* Get all cats depinding on current dir */
173 $cats = $this->FAIclasses;
174 $this->FAIclasses =$this->ArrayUp($id,$cats);
175 }
177 /* Class one position down */
178 function catDown($id)
179 {
180 /* Get all cats depinding on current dir */
181 $cats = $this->FAIclasses;
182 $this->FAIclasses =$this->ArrayDown($id,$cats);
183 }
185 function execute()
186 {
187 /* Call parent execute */
188 plugin::execute();
189 /* Fill templating stuff */
190 $smarty= get_smarty();
191 $display= "";
193 $s_entry = "";
194 $s_action = "";
196 /* Remove class name From list */
197 $sort_once = false;
198 foreach($_POST as $name => $post){
199 if(preg_match("/DEL_/i",$name)){
200 $s_action = "delete";
201 $s_entry = preg_replace("/DEL_/","",$name);
202 $s_entry = base64_decode(preg_replace("/_.*$/","",$s_entry));
203 }elseif(preg_match("/Add_class/i",$name)){
204 $s_action = "add";
205 }elseif(preg_match("/DelClass/i",$name)){
206 $s_action = "delete";
207 $s_entry = $_POST['FAIclass'];
208 }elseif(preg_match("/AddClass/i",$name)){
209 $s_action = "add";
210 }
212 /* Check if a list element should be pushed one position up */
213 if((preg_match("/sortup_/",$name))&&(!$sort_once)){
214 $sort_once = true;
215 $val = preg_replace("/sortup_/","",$name);
216 $val = preg_replace("/_.*$/","",$val);
217 $val = base64_decode($val);
218 $this->catUp($val);
219 }
221 /* Check if a list element should be pushed one position down */
222 if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
223 $sort_once = true;
224 $val = preg_replace("/sortdown_/","",$name);
225 $val = preg_replace("/_.*$/","",$val);
226 $val = base64_decode($val);
227 $this->catDown($val);
228 }
230 }
232 if($s_action == "delete"){
233 unset($this->FAIclasses[$s_entry]);
234 }
236 if($s_action == "add"){
237 $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
238 $this->is_dialog =true;
239 }
241 /* Save Dialog */
242 if(isset($_POST['SaveSubObject'])){
243 $this->dialog->save_object();
244 $msgs= $this->dialog->check();
245 if(count($msgs)){
246 print_red($msgs);
247 }else{
248 $ret = $this->dialog->save();
249 foreach($ret as $class){
250 $this->FAIclasses[$class] =$class;
251 }
252 $this->is_dialog=false;
253 unset($this->dialog);
254 $this->dialog=NULL;
255 ksort($this->FAIclasses);
256 }
257 }
259 /* Cancel Dialog */
260 if(isset($_POST['CancelSubObject'])){
261 $this->is_dialog=false;
262 unset($this->dialog);
263 $this->dialog=NULL;
264 }
266 if(isset($this->dialog)){
267 $this->dialog->save_object();
268 return($this->dialog->execute());
269 }
271 $divlist =new divSelectBox("Profile");
272 $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
274 /* item images */
275 $objTypes['FAIhook'] = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
276 $objTypes['FAItemplate'] = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
277 $objTypes['FAIscript'] = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
278 $objTypes['FAIvariable'] = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
279 $objTypes['FAIpackages'] = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
280 $objTypes['FAIpartitionTable'] = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
282 /* Delete button */
283 $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>";
285 /* Up down buttons */
286 $linkupdown = " <input type='image' name='sortup_%s' alt='up' title='"._("Up")."' src='images/sort_up.png' align='top' >";
287 $linkupdown.= "<input type='image' name='sortdown_%s' alt='down' title='"._("Down")."' src='images/sort_down.png' >";
289 /* Append fai classes to divlist */
290 foreach($this->FAIclasses as $usedClass){
291 $str = " ";
293 if(isset($this->FAIAllclasses[$usedClass])){
294 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
295 $str.= $objTypes[$class];
296 }
297 }
299 $field1 = array("string"=> $usedClass,"attach"=>"");
300 $field2 = array("string"=> $str,"attach"=>"");
301 $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).preg_replace("/%s/",base64_encode($usedClass),$linkupdown),"attach"=>"style='border-right:none;'");
302 $divlist->AddEntry(array($field1,$field2,$field3));
303 }
305 $smarty->assign("FAIclasses" ,$this->FAIclasses);
306 $smarty->assign("divlist" ,$divlist->DrawList());
308 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
309 * If we post the escaped strings they will be escaped again
310 */
311 foreach($this->attributes as $attrs){
312 if(get_magic_quotes_gpc()){
313 $smarty->assign($attrs,stripslashes($this->$attrs));
314 }else{
315 $smarty->assign($attrs,($this->$attrs));
316 }
317 }
320 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
321 return($display);
322 }
324 function remove_from_parent()
325 {
326 $ldap = $this->config->get_ldap_link();
327 $ldap->cd ($this->dn);
328 $ldap->rmdir_recursive($this->dn);
329 $this->handle_post_events("remove");
330 }
332 /* Save data to object
333 */
334 function save_object()
335 {
336 plugin::save_object();
337 foreach($this->attributes as $attrs){
338 if(isset($_POST[$attrs])){
339 $this->$attrs = $_POST[$attrs];
340 }
341 }
342 }
345 /* Check supplied data */
346 function check()
347 {
348 $message= array();
350 if(count($this->FAIclasses) == 0){
351 $message[]=_("Please assign at least one class to this profile.");
352 }
354 if(empty($this->cn)){
355 $message[]=_("Please enter a valid name.");
356 }
358 $ldap = $this->config->get_ldap_link();
359 $ldap->cd($_SESSION['faifilter']['base']);
360 $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
362 if($ldap->count()){
363 $message[]=_("There is already a profile with this class name defined.");
364 }
366 return ($message);
367 }
370 /* Save to LDAP */
371 function save()
372 {
373 plugin::save();
375 $ldap = $this->config->get_ldap_link();
377 $this->FAIclass = "";
378 foreach($this->FAIclasses as $class){
379 $this->FAIclass.=$class." ";
380 }
382 $this->attrs['FAIclass']=trim($this->FAIclass);
384 $ldap->cat($this->dn);
385 if($ldap->count()!=0){
386 /* Write FAIscript to ldap*/
387 $ldap->cd($this->dn);
388 $ldap->modify($this->attrs);
389 }else{
390 /* Write FAIscript to ldap*/
391 $ldap->cd($this->config->current['BASE']);
392 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
393 $ldap->cd($this->dn);
394 $ldap->add($this->attrs);
395 }
396 show_ldap_error($ldap->get_error());
397 }
398 }
400 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
401 ?>