1 <?php
3 class faiScript 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");
11 /* ObjectClasses for this Object*/
12 var $objectclasses = array("top","FAIclass","FAIscript");
14 /* Class name of the Ldap ObjectClass for the Sub Object */
15 var $subClass = "FAIscriptEntry";
16 var $subClasses = array("top","FAIclass","FAIscriptEntry");
18 /* Class name of the php class which allows us to edit a Sub Object */
19 var $subClassName = "faiScriptEntry";
21 /* Attributes to initialise for each subObject */
22 var $subAttributes = array("cn","description","FAIpriority");
23 var $sub_Load_Later = array("FAIscript");
24 var $sub64coded = array();
25 var $subBinary = array("FAIscript");
27 /* Specific attributes */
28 var $cn = ""; // The class name for this object
29 var $description = ""; // The description for this set of partitions
30 var $is_dialog = false; // specifies which buttons will be shown to save or abort
31 var $SubObjects = array(); // All leafobjects of this object
33 var $FAIstate = "branch";
34 var $sort_by = "name";
35 var $sort_order = "up";
37 var $view_logged = FALSE;
38 var $ui;
40 function faiScript (&$config, $dn= NULL)
41 {
42 /* Load Attributes */
43 plugin::plugin ($config, $dn);
45 /* If "dn==new" we try to create a new entry
46 * Else we must read all objects from ldap which belong to this entry.
47 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
48 */
49 if($dn != "new"){
50 $this->dn =$dn;
52 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
53 */
54 $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
55 foreach($res as $obj){
57 /* Skip not relevant objects */
58 if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
60 $objects = array();
61 $objects['status'] = "FreshLoaded";
62 $objects['dn'] = $obj['dn'];
63 $objects = $this->get_object_attributes($objects,$this->subAttributes);
64 $this->SubObjects[$objects['cn']] = $objects;
65 }
66 }
68 $this->is_new = FALSE;
69 if($this->dn == "new"){
70 $this->is_new =TRUE;
71 }
73 $this->ui = get_userinfo();
75 $this->scriptListWidget= new sortableListing($this->convertList(TRUE), $this->convertList());
76 $this->scriptListWidget->setDeleteable(true);
77 $this->scriptListWidget->setInstantDelete(false);
78 $this->scriptListWidget->setEditable(true);
79 $this->scriptListWidget->setWidth("100%");
80 $this->scriptListWidget->setHeight("140px");
81 $this->scriptListWidget->setHeader(array(_("Name"),_("Description")));
82 }
84 function convertList($type = FALSE)
85 {
86 $data = array();
87 $s_acl = $this->ui->get_permissions($this->dn,"fai/faiScriptEntry","FAIscript");
88 foreach($this->SubObjects as $cn => $entry){
89 if($entry['status'] == "delete") continue;
90 if($type){
91 $data[$cn] = $entry;
92 }else{
93 $data[$cn] = array('data' => array($entry['cn'], $entry['description']));
94 }
95 }
96 return($data);
97 }
100 /* Reload some attributes */
101 function get_object_attributes($object,$attributes)
102 {
103 $ldap = $this->config->get_ldap_link();
104 $ldap->cd($this->config->current['BASE']);
105 $ldap->cat($object['dn'],$attributes);
106 $tmp = $ldap->fetch();
108 foreach($attributes as $attrs){
109 if(isset($tmp[$attrs][0])){
110 $var = $tmp[$attrs][0];
112 /* Check if we must decode some attributes */
113 if(in_array_ics($attrs,$this->sub64coded)){
114 $var = postDecode($var);
115 }
117 /* check if this is a binary entry */
118 if(in_array_ics($attrs,$this->subBinary)){
119 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
120 }
122 /* Fix slashes */
123 $var = addslashes($var);
124 $object[$attrs] = $var;
125 }
126 }
127 return($object);
128 }
131 /* Return a valid dn to fetch acls. Because 'new' will not work. */
132 function acl_base_for_current_object($dn)
133 {
134 if($dn == "new" || $dn == ""){
135 if($this->dn == "new"){
136 $dn= $this->parent->parent->acl_base;
137 }else{
138 $dn = $this->dn;
139 }
140 }
141 return($dn);
142 }
145 function execute()
146 {
147 /* Call parent execute */
148 plugin::execute();
150 if($this->is_account && !$this->view_logged){
151 $this->view_logged = TRUE;
152 new log("view","fai/".get_class($this),$this->dn);
153 }
155 /* Fill templating stuff */
156 $smarty= get_smarty();
157 $display= "";
159 /* Add new sub object */
160 if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
161 $this->dialog= new $this->subClassName($this->config,"new");
162 $this->dialog->FAIstate = $this->FAIstate;
163 $this->dialog->set_acl_base($this->acl_base);
164 $this->dialog->set_acl_category("fai");
165 $this->dialog->parent = &$this;
166 $this->is_dialog=true;
167 }
169 if($this->dn != "new"){
170 set_object_info($this->dn);
171 }
173 $this->scriptListWidget->setAcl($this->getacl(""));
174 $this->scriptListWidget->save_object();
175 $action = $this->scriptListWidget->getAction();
176 if($action['action'] =="edit"){
177 $s_entry = $this->scriptListWidget->getKey($action['targets'][0]);
178 if(isset($this->SubObjects[$s_entry])){
180 $obj = $this->SubObjects[$s_entry];
181 if($obj['status'] == "FreshLoaded"){
182 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
183 }
185 /* Create new dialog and set acl attributes */
186 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
187 $this->dialog->FAIstate = $this->FAIstate;
188 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
189 $this->dialog->set_acl_category("fai");
191 /* Assign some additional dialog informations like headline and parent */
192 set_object_info($obj['dn']);
193 $this->dialog->parent = &$this;
194 $this->is_dialog=true;
195 }
196 }
198 /* Check acls, are we allowed to delete an entry */
199 if($action['action'] =="delete"){
200 $s_entry = $this->scriptListWidget->getKey($action['targets'][0]);
201 if(isset($this->SubObjects[$s_entry])){
202 $entry = $this->SubObjects[$s_entry];
203 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($entry['dn']),"fai/faiScriptEntry") ;
204 if(preg_match("/d/",$acl)){
205 $status = $entry['status'];
206 if($status == "edited" || $status == "FreshLoaded"){
207 $this->SubObjects[$s_entry]['status']= "delete";
208 }else{
209 unset($this->SubObjects[$s_entry]);
210 }
211 }
212 }
213 }
216 /* Save the edited entry */
217 if(isset($_POST['SaveSubObject'])){
219 /* Check if there are still errors remaining that must be fixed before saving */
220 $this->dialog->save_object();
221 $msgs = $this->dialog->check();
222 if(count($msgs)>0){
223 foreach($msgs as $msg){
224 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
225 }
226 }else{
228 /* Get return object */
229 $obj = $this->dialog->save();
231 /* If we have renamed the script entry, we must remove the old entry */
232 if(isset($obj['remove'])){
234 /* Get old entry values */
235 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
237 /* Depending on status, set new status */
238 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
239 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
240 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
241 unset($this->SubObjects[$obj['remove']['from']]);
242 }
244 /* Append the new entry */
245 $obj['status'] = "new";
246 $this->SubObjects[$obj['remove']['to']] = $obj;
247 unset($this->SubObjects[$obj['remove']['to']]['remove']);
248 }else{
250 /* Set new status and append the entry */
251 if($obj['status'] == "FreshLoaded"){
252 $obj['status'] = "edited";
253 }
254 $this->SubObjects[$obj['cn']]=$obj;
255 }
256 $this->is_dialog=false;
257 unset($this->dialog);
258 $this->dialog=FALSE;
260 }
261 }
263 /* Cancel Dialog */
264 if(isset($_POST['CancelSubObject'])){
265 $this->is_dialog=false;
266 unset($this->dialog);
267 $this->dialog=FALSE;
268 }
270 /* Print dialog if $this->dialog is set */
271 if(is_object($this->dialog)){
272 $this->dialog->save_object();
273 $display = $this->dialog->execute();
274 return($display);
275 }
277 $this->scriptListWidget->setListData($this->convertList(TRUE), $this->convertList());
278 $this->scriptListWidget->update();
279 $smarty->assign("Entry_divlist",$this->scriptListWidget->render());
281 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
282 * If we post the escaped strings they will be escaped again
283 */
284 foreach($this->attributes as $attrs){
285 if(get_magic_quotes_gpc()){
286 $smarty->assign($attrs,stripslashes($this->$attrs));
287 }else{
288 $smarty->assign($attrs,($this->$attrs));
289 }
290 }
292 $dn = $this->acl_base_for_current_object($this->dn);
293 $smarty->assign("sub_object_is_addable",
294 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
295 !preg_match("/freeze/",$this->FAIstate));
297 $tmp = $this->plInfo();
298 foreach($tmp['plProvidedAcls'] as $name => $translated){
299 $smarty->assign($name."ACL",$this->getacl($name));
300 }
302 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
303 return($display);
304 }
307 /* Generate listbox friendly SubObject list
308 */
309 function getList($use_dns=false){
310 $a_return=array();
311 foreach($this->SubObjects as $obj){
312 if($obj['status'] != "delete"){
314 $cn = stripslashes($obj['cn']);
315 $desc = "";
317 if((isset($obj['description']))&&(!empty($obj['description']))){
318 $desc = " [".stripslashes($obj['description'])."]";
319 }
321 if($use_dns){
322 $a_return[$obj['cn']]['name']= $cn.$desc;
323 $a_return[$obj['cn']]['dn']= $obj['dn'];
324 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
325 }else{
326 $a_return[$obj['cn']] = $cn.$desc;
327 }
328 }
329 }
330 return($a_return);
331 }
334 /* Delete me, and all my subtrees
335 */
336 function remove_from_parent()
337 {
338 if($this->acl_is_removeable()){
339 $ldap = $this->config->get_ldap_link();
340 $ldap->cd ($this->dn);
341 $release = $this->parent->parent->fai_release;
342 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
343 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
344 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
346 foreach($this->SubObjects as $name => $obj){
347 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
348 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
349 }
350 $this->handle_post_events("remove");
351 }
352 }
355 /* Save data to object
356 */
357 function save_object()
358 {
359 if((isset($_POST['FAIscript_posted'])) && !preg_match("/freeze/", $this->FAIstate)){
360 plugin::save_object();
361 }
363 /* Get sort order */
364 if(isset($_GET['sort']) && in_array($_GET['sort'],array("name","priority"))){
365 if($this->sort_by == $_GET['sort']){
366 if($this->sort_order == "up"){
367 $this->sort_order = "down";
368 }elseif($this->sort_order == "down"){
369 $this->sort_order = "up";
370 }
371 }
372 $this->sort_by = $_GET['sort'];
373 }
374 }
377 /* Check supplied data */
378 function check()
379 {
380 /* Call common method to give check the hook */
381 $message= plugin::check();
383 /* Ensure that we do not overwrite an allready existing entry
384 */
385 if($this->is_new){
386 $release = $this->parent->parent->fai_release;
387 $new_dn= 'cn='.$this->cn.",".get_ou('faiScriptRDN').get_ou('faiBaseRDN').$release;
388 $res = faiManagement::check_class_name("FAIscript",$this->cn,$new_dn);
389 if(isset($res[$this->cn])){
390 $message[] = msgPool::duplicated(_("Name"));
391 }
392 }
394 return ($message);
395 }
398 /* Save to LDAP */
399 function save()
400 {
401 plugin::save();
403 $ldap = $this->config->get_ldap_link();
405 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
407 if($this->initially_was_account){
408 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
409 }else{
410 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
411 }
413 /* Prepare FAIscriptEntry to write it to ldap
414 * First sort array.
415 * Because we must delete old entries first.
416 * After deletion, we perform add and modify
417 */
418 $Objects = array();
420 /* We do not need to save untouched objects */
421 foreach($this->SubObjects as $name => $obj){
422 if($obj['status'] == "FreshLoaded"){
423 unset($this->SubObjects[$name]);
424 }
425 }
427 foreach($this->SubObjects as $name => $obj){
428 if($obj['status'] == "delete"){
429 $Objects[$name] = $obj;
430 }
431 }
432 foreach($this->SubObjects as $name => $obj){
433 if($obj['status'] != "delete"){
434 $Objects[$name] = $obj;
435 }
436 }
438 foreach($Objects as $name => $obj){
440 foreach($this->sub64coded as $codeIt){
441 $obj[$codeIt]=postEncode(stripslashes($obj[$codeIt]));
442 }
444 $tmp = array();
445 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
446 foreach($attributes as $attrs){
447 if(!isset($obj[$attrs])) continue;
448 if($obj[$attrs] == ""){
449 $obj[$attrs] = array();
450 }
451 if(!is_array($obj[$attrs])){
452 $tmp[$attrs] = stripslashes($obj[$attrs]);
453 }else{
454 $tmp[$attrs] = $obj[$attrs];
455 }
456 }
458 $tmp['objectClass'] = $this->subClasses;
460 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
462 if($obj['status']=="new"){
463 $ldap->cat($sub_dn,array("objectClass"));
464 if($ldap->count()){
465 $obj['status']="edited";
466 }
467 }
469 if(empty($tmp['FAIpriority'])){
470 $tmp['FAIpriority'] ="0";
471 }
473 /* Tag object */
474 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
476 if($obj['status'] == "delete"){
477 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
478 $this->handle_post_events("remove");
479 }elseif($obj['status'] == "edited"){
480 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
481 $this->handle_post_events("modify");
482 }elseif($obj['status']=="new"){
483 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
484 $this->handle_post_events("add");
485 }
486 }
487 }
490 function PrepareForCopyPaste($source)
491 {
492 plugin::PrepareForCopyPaste($source);
494 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
495 */
496 $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
497 foreach($res as $obj){
499 /* Skip not relevant objects */
500 if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
502 $objects = array();
503 $objects['status'] = "edited";
504 $objects['dn'] = $obj['dn'];
505 $objects = $this->get_object_attributes($objects,$this->subAttributes);
506 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
507 $this->SubObjects[$objects['cn']] = $objects;
508 }
509 }
512 /*! \brief Used for copy & paste.
513 Returns a HTML input mask, which allows to change the cn of this entry.
514 @param Array Array containing current status && a HTML template.
515 */
516 function getCopyDialog()
517 {
518 $vars = array("cn");
519 $smarty = get_smarty();
520 $smarty->assign("cn", htmlentities($this->cn));
521 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
522 $ret = array();
523 $ret['string'] = $str;
524 $ret['status'] = "";
525 return($ret);
526 }
529 /*! \brief Used for copy & paste.
530 Some entries must be renamed to avaoid duplicate entries.
531 */
532 function saveCopyDialog()
533 {
534 if(isset($_POST['cn'])){
535 $this->cn = get_post('cn');
536 }
537 }
540 /* Return plugin informations for acl handling */
541 static function plInfo()
542 {
543 return (array(
544 "plShortName" => _("Script"),
545 "plDescription" => _("FAI script"),
546 "plSelfModify" => FALSE,
547 "plDepends" => array(),
548 "plPriority" => 18,
549 "plSection" => array("administration"),
550 "plCategory" => array("fai"),
551 "plProvidedAcls" => array(
552 "cn" => _("Name")." ("._("Readonly").")",
553 "description" => _("Description"))
554 ));
555 }
556 }
558 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
559 ?>