1 <?php
3 class faiProfile extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
8 /* Attributes for this Object */
9 var $attributes = array("cn","description","FAIclass");
11 /* ObjectClasses for this Object*/
12 var $objectclasses = array("top","FAIclass","FAIprofile");
14 /* Specific attributes */
15 var $old_cn = "";
16 var $cn = ""; // The class name for this object
17 var $description = ""; // The description for this set of partitions
18 var $is_dialog = false; // specifies which buttons will be shown to save or abort
19 var $FAIclass = ""; // Classnames used for this profile
20 var $FAIclasses = array(); // Contains classname seperated in an array
21 var $FAIAllclasses = array(); // Contains all possible Classnames
22 var $ui ;
23 var $FAIstate = "";
24 var $view_logged = FALSE;
26 var $classSelect;
28 function faiProfile(&$config, $dn= NULL)
29 {
30 /* Load Attributes */
31 plugin::plugin ($config, $dn);
32 $ldap=$this->config->get_ldap_link();
34 $this->ui = get_userinfo();
36 /* Parse ldap attribute to get all assigned classes */
37 $tmp = explode(" ",$this->FAIclass);
38 $tmp2 = array();
39 foreach($tmp as $class){
40 if(!empty($class)){
41 $tmp2[trim($class)] = trim($class);
42 }
43 }
45 /* Sort assigned classes */
46 if(is_array($tmp2)){
47 foreach($tmp2 as $class){
48 $this->FAIclasses[$class]=$class;
49 }
50 }
52 $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
54 /* Build filter */
55 $filter= "";
56 foreach ($categories as $cat){
57 $filter.= "(objectClass=$cat)";
58 }
60 /* Get ldap connection */
61 $base = session::get('CurrentMainBase');
62 $ldap->cd($base);
63 $sort = array();
65 /* search all FAI classes */
66 $ldap->search("(|$filter)",array("*"));
67 while($attrs = $ldap->fetch()){
69 /* Sort by categorie */
70 foreach($categories as $cat){
71 if(in_array($cat,$attrs['objectClass'])){
73 /* Append entry */
74 $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
76 /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
77 $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
78 }
79 }
80 }
82 /* Sort the sort array */
83 //ksort($sort);
85 /* Reorder the FAIclasses array */
86 foreach($sort as $name){
87 $tmp[$name] =$this->FAIAllclasses[$name];
88 }
90 /* Assign sorted classes */
91 $this->FAIAllclasses = array();
92 $this->FAIAllclasses = $tmp;
94 if($dn != "new"){
95 $this->dn =$dn;
96 }
97 $this->old_cn = $this->cn;
99 // Prepare lists
100 $this->classList = new sortableListing(array(),array(), TRUE);
101 $this->classList->setDeleteable(true);
102 $this->classList->setEditable(false);
103 $this->classList->setWidth("100%");
104 $this->classList->setHeight("200px");
105 $this->classList->setHeader(array(_("Name"),_("Types")));
106 $this->classList->setDefaultSortColumn(0);
107 }
110 /* Combine new array, used for up down buttons */
111 function combineArrays($ar0,$ar1,$ar2)
112 {
113 $ret = array();
114 if(is_array($ar0))
115 foreach($ar0 as $ar => $a){
116 $ret[$ar]=$a;
117 }
118 if(is_array($ar1))
119 foreach($ar1 as $ar => $a){
120 $ret[$ar]=$a;
121 }
122 if(is_array($ar2))
123 foreach($ar2 as $ar => $a){
124 $ret[$ar]=$a;
125 }
126 return($ret);
127 }
130 function acl_base_for_current_object($dn)
131 {
132 if($dn == "new" || $dn == ""){
133 if($this->dn == "new"){
134 $dn= $this->parent->parent->acl_base;
135 }else{
136 $dn = $this->dn;
137 }
138 }
139 return($dn);
140 }
143 /* returns position in array */
144 function getpos($atr,$attrs)
145 {
146 $i = 0;
147 foreach($attrs as $attr => $name) {
148 $i++;
149 if($attr == $atr){
150 return($i);
151 }
152 }
153 return(-1);
154 }
156 /* Transports the given Arraykey one position up*/
157 function ArrayUp($atr,$attrs)
158 {
159 $ret = $attrs;
160 $pos = $this->getpos($atr,$attrs) ;
161 $cn = count($attrs);
162 if(!(($pos == -1)||($pos == 1))){
163 $before = array_slice($attrs,0,($pos-2));
164 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
165 $unten = array_slice($attrs,$pos);
166 $ret = array();
167 $ret = $this->combineArrays($before,$mitte,$unten);
168 }
169 return($ret);
170 }
173 /* Transports the given Arraykey one position down*/
174 function ArrayDown($atr,$attrs)
175 {
176 $ret = $attrs;
177 $pos = $this->getpos($atr,$attrs) ;
178 $cn = count($attrs);
179 if(!(($pos == -1)||($pos == $cn))){
180 $before = array_slice($attrs,0,($pos-1));
181 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
182 $unten = array_slice($attrs,($pos+1));
183 $ret = array();
184 $ret = $this->combineArrays($before,$mitte,$unten);
185 }
186 return($ret);
187 }
189 /* class one position up */
190 function catUp($id)
191 {
192 /* Get all cats depinding on current dir */
193 $cats = $this->FAIclasses;
194 $this->FAIclasses =$this->ArrayUp($id,$cats);
195 }
197 /* Class one position down */
198 function catDown($id)
199 {
200 /* Get all cats depinding on current dir */
201 $cats = $this->FAIclasses;
202 $this->FAIclasses =$this->ArrayDown($id,$cats);
203 }
205 function execute()
206 {
207 /* Call parent execute */
208 plugin::execute();
210 if($this->is_account && !$this->view_logged){
211 $this->view_logged = TRUE;
212 new log("view","fai/".get_class($this),$this->dn);
213 }
215 /* Fill templating stuff */
216 $smarty= get_smarty();
217 $display= "";
219 $s_entry = "";
220 $s_action = "";
222 /* Remove class name From list */
223 $sort_once = false;
224 if(!preg_match("/freeze/",$this->FAIstate)){
226 if(isset($_POST['AddClass']) && $this->acl_is_writeable("FAIclass")){
227 $this->classSelect = new classSelect($this->config, get_userinfo());
228 $this->dialog =true;
229 }
232 /* Save Dialog */
233 if(isset($_POST['classSelect_save']) && $this->classSelect instanceOf classSelect){
234 $list = $this->classSelect->save();
235 foreach($list as $entry){
236 $class = $entry['cn'][0];
237 $this->FAIclasses[$class] =$class;
238 }
239 $this->dialog=false;
240 $this->classSelect=FALSE;
241 }
242 }
244 /* Cancel Dialog */
245 if(isset($_POST['classSelect_cancel']) && $this->classSelect instanceOf classSelect){
246 $this->dialog=false;
247 $this->classSelect=FALSE;
248 }
250 if($this->classSelect instanceOf classSelect){
251 session::set('filterFAIClass_base',$this->parent->parent->fai_release);
252 session::set('filterBlacklist', array('cn' => array_values($this->FAIclasses)));
253 return($this->classSelect->execute());
254 }
256 /* item images */
257 $objTypes['FAIhook'] = image('plugins/fai/images/fai_hook.png','',_("Hook bundle"));
258 $objTypes['FAItemplate'] = image('plugins/fai/images/fai_template.png','',_("Template bundle"));
259 $objTypes['FAIscript'] = image('plugins/fai/images/fai_script.png','',_("Script bundle"));
260 $objTypes['FAIvariable'] = image('plugins/fai/images/fai_variable.png','',_("Variable bundle"));
261 $objTypes['FAIpackageList'] = image('plugins/fai/images/fai_packages.png','',_("Package bundle"));
262 $objTypes['FAIpartitionTable'] = image('plugins/fai/images/fai_partitionTable.png','',_("Partition table"));
264 $emp = image('images/empty.png');
266 /* Delete button */
267 $actions = image('images/lists/trash.png','DEL_%KEY%',_("Remove class from profile"));
269 /* Up down buttons */
270 $linkupdown = image('images/lists/sort-up.png','sortup_%s',_("Up"), 'top');
271 $linkupdown.= image('images/lists/sort-down.png','sortdown_%s',_("Down"), 'bottom');
273 $this->classList->setAcl($this->getacl('FAIclass',preg_match('/^freeze/', $this->FAIstate)));
274 $data = $lData = array();
275 foreach($this->FAIclasses as $usedClass){
276 $str = "";
277 if(isset($this->FAIAllclasses[$usedClass])){
278 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
279 $str.= $objTypes[$class];
280 }
281 }
282 $data[$usedClass] = $usedClass;
283 $lData[$usedClass] = array('data'=>array($usedClass,$str));
284 }
285 $this->classList->setListData($data,$lData);
286 $this->classList->update();
288 $smarty->assign("usePrototype", "true");
289 $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
290 $smarty->assign("FAIclasses" ,$this->FAIclasses);
291 $smarty->assign("divlist" ,$this->classList->render());
293 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
294 * If we post the escaped strings they will be escaped again
295 */
296 foreach($this->attributes as $attrs){
297 if(get_magic_quotes_gpc()){
298 $smarty->assign($attrs,stripslashes($this->$attrs));
299 }else{
300 $smarty->assign($attrs,($this->$attrs));
301 }
302 }
305 $dn = $this->acl_base_for_current_object($this->dn);
306 $smarty->assign("sub_object_is_addable",
307 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
308 !preg_match("/freeze/",$this->FAIstate));
310 $tmp = $this->plInfo();
311 foreach($tmp['plProvidedAcls'] as $name => $translated){
312 $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
313 }
315 $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
316 return($display);
317 }
319 function remove_from_parent()
320 {
321 $ldap = $this->config->get_ldap_link();
322 $ldap->cd ($this->dn);
323 $release = $this->parent->parent->fai_release;
324 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
325 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
326 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
327 $this->handle_post_events("remove");
328 }
331 /* Save data to object
332 */
333 function save_object()
334 {
335 if(!preg_match("/freeze/",$this->FAIstate)){
336 plugin::save_object();
338 $this->classList->save_object();
339 $action = $this->classList->getAction();
340 if($action['action'] == 'reorder'){
341 $this->FAIclasses = $this->classList->getMaintainedData();
342 }
343 if($action['action'] == 'delete'){
344 $s_entry = $this->classList->getKey($action['targets'][0]);
345 unset($this->FAIclasses[$s_entry]);
346 }
347 }
348 }
351 /* Check supplied data */
352 function check()
353 {
354 /* Call common method to give check the hook */
355 $message= plugin::check();
357 if(count($this->FAIclasses) == 0){
358 $message[]=_("No class specified for this profile!");
359 }
361 if($this->cn == ""){
362 $message[]= msgPool::required(_("Name"));
363 }
365 /* Ensure that we do not overwrite an allready existing entry
366 */
367 if($this->dn == "new" || $this->cn != $this->old_cn){
368 $release = $this->parent->parent->fai_release;
369 $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
370 $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
371 if(isset($res[$this->cn])){
372 $message[] = msgPool::duplicated(_("Name"));
373 }
374 }
376 return ($message);
377 }
380 /* Save to LDAP */
381 function save()
382 {
383 plugin::save();
385 $ldap = $this->config->get_ldap_link();
387 $this->FAIclass = "";
388 foreach($this->FAIclasses as $class){
389 $this->FAIclass.=$class." ";
390 }
392 $this->attrs['FAIclass']=trim($this->FAIclass);
394 /* Remove the old FAI profile, if the dn has changed.
395 */
396 if($this->cn != $this->old_cn && $this->old_cn != ""){
398 $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
399 FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
400 }
401 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
403 if($this->initially_was_account){
404 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
405 }else{
406 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
407 }
408 }
411 function PrepareForCopyPaste($source)
412 {
413 plugin::PrepareForCopyPaste($source);
415 /* Parse ldap attribute to get all assigned classes */
416 $tmp = explode(" ",$this->FAIclass);
417 $tmp2 = array();
418 foreach($tmp as $class){
419 if(!empty($class)){
420 $tmp2[trim($class)] = trim($class);
421 }
422 }
424 /* Sort assigned classes */
425 if(is_array($tmp2)){
426 foreach($tmp2 as $class){
427 $this->FAIclasses[$class]=$class;
428 }
429 }
430 }
433 /* Return plugin informations for acl handling */
434 static function plInfo()
435 {
436 return (array(
437 "plShortName" => _("Profile"),
438 "plDescription" => _("FAI profile"),
439 "plSelfModify" => FALSE,
440 "plDepends" => array(),
441 "plPriority" => 30,
442 "plSection" => array("administration"),
443 "plCategory" => array("fai"),
444 "plProvidedAcls" => array(
445 "cn" => _("Name"),
446 "description" => _("Description"),
447 "FAIclass" => _("FAI classes"))
448 ));
449 }
452 /*! \brief Used for copy & paste.
453 Returns a HTML input mask, which allows to change the cn of this entry.
454 @param Array Array containing current status && a HTML template.
455 */
456 function getCopyDialog()
457 {
458 $vars = array("cn");
459 $smarty = get_smarty();
460 $smarty->assign("cn", htmlentities($this->cn));
461 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
462 $ret = array();
463 $ret['string'] = $str;
464 $ret['status'] = "";
465 return($ret);
466 }
469 /*! \brief Used for copy & paste.
470 Some entries must be renamed to avaoid duplicate entries.
471 */
472 function saveCopyDialog()
473 {
474 if(isset($_POST['cn'])){
475 $this->cn = get_post('cn');
476 }
477 }
478 }
480 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
481 ?>