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 $dialog = NULL; // a dialog, e.g. new disk dialog
32 var $SubObjects = array(); // All leafobjects of this object
34 var $FAIstate = "";
35 var $sort_by = "name";
36 var $sort_order = "up";
38 var $base ="";
39 var $release ="";
40 var $copy_paste_mode = false;
41 var $cut_paste_mode = false;
42 var $CopyPasteVars = array("SubObjects");
45 function faiScript ($config, $dn= NULL)
46 {
47 /* Load Attributes */
48 plugin::plugin ($config, $dn);
50 $this->acl ="#all#";
52 /* If "dn==new" we try to create a new entry
53 * Else we must read all objects from ldap which belong to this entry.
54 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
55 */
56 if($dn != "new"){
58 /* Set acls
59 */
60 $ui = get_userinfo();
61 $acl = get_permissions ($this->dn, $ui->subtreeACL);
62 $acli = get_module_permission($acl, "FAIclass", $this->dn);
63 $this->acl=$acli;
65 $this->dn =$dn;
67 /* Get FAIstate
68 */
69 if(isset($this->attrs['FAIstate'][0])){
70 $this->FAIstate = $this->attrs['FAIstate'][0];
71 }
73 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
74 */
75 $ldap = $this->config->get_ldap_link();
76 $ldap->cd ($this->dn);
77 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
79 while($object = $ldap->fetch()){
80 /* Set status for save management */
81 $objects = array();
82 $objects['status'] = "FreshLoaded";
83 $objects['dn'] = $object['dn'];
84 $objects = $this->get_object_attributes($objects,$this->subAttributes);
85 $this->SubObjects[$objects['cn']] = $objects;
87 }
88 }
89 }
92 /* Reload some attributes */
93 function get_object_attributes($object,$attributes)
94 {
95 $ldap = $this->config->get_ldap_link();
96 $ldap->cd($this->config->current['BASE']);
97 $ldap->cat($object['dn'],$attributes);
98 $tmp = $ldap->fetch();
100 foreach($attributes as $attrs){
101 if(isset($tmp[$attrs][0])){
102 $var = $tmp[$attrs][0];
104 /* Check if we must decode some attributes */
105 if(in_array_ics($attrs,$this->sub64coded)){
106 $var = base64_decode($var);
107 }
109 /* check if this is a binary entry */
110 if(in_array_ics($attrs,$this->subBinary)){
111 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
112 }
114 /* Fix slashes */
115 $var = addslashes($var);
116 $object[$attrs] = $var;
117 }
118 }
119 return($object);
120 }
123 function execute()
124 {
125 /* Call parent execute */
126 plugin::execute();
128 /* Fill templating stuff */
129 $smarty= get_smarty();
130 $display= "";
132 /* Add new sub object */
133 if(isset($_POST['AddSubObject'])){
134 $this->dialog= new $this->subClassName($this->config,"new");
135 $this->dialog->acl = $this->acl;
136 $this->dialog->parent = &$this;
137 $this->is_dialog=true;
138 }
140 if($this->dn != "new"){
141 $_SESSION['objectinfo']= $this->dn;
142 }
145 /* New Listhandling
146 */
147 $once = true;
148 foreach($_POST as $name => $value){
149 if(preg_match("/^editscript_/",$name)&&($once)){
150 $once = false;
151 $entry = preg_replace("/^editscript_/","",$name);
152 $entry = base64_decode(preg_replace("/_.*/","",$entry));
154 $obj = $this->SubObjects[$entry];
155 if($obj['status'] == "FreshLoaded"){
156 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
157 }
159 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
160 $this->dialog->acl = $this->acl;
161 $_SESSION['objectinfo'] = $obj['dn'];
162 $this->dialog->parent = &$this;
163 $this->is_dialog=true;
165 }
166 if(preg_match("/^deletescript_/",$name)&&($once)){
167 $once = false;
168 $entry = preg_replace("/^deletescript_/","",$name);
169 $entry = base64_decode(preg_replace("/_.*/","",$entry));
171 $status = $this->SubObjects[$entry]['status'];
172 if($status == "edited" || $status == "FreshLoaded"){
173 $this->SubObjects[$entry]['status']= "delete";
174 }else{
175 unset($this->SubObjects[$entry]);
176 }
177 }
178 }
179 ///// Ende new list handling
180 if(isset($_POST['SaveSubObject'])){
181 $this->dialog->save_object();
182 $msgs = $this->dialog->check();
183 if(count($msgs)>0){
184 foreach($msgs as $msg){
185 print_red($msg);
186 }
187 }else{
189 /* Get return object */
190 $obj = $this->dialog->save();
191 if(isset($obj['remove'])){
192 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
194 /* Depending on status, set new status */
195 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
196 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
197 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
198 unset($this->SubObjects[$obj['remove']['from']]);
199 }
200 $obj['status'] = "new";
201 $this->SubObjects[$obj['remove']['to']] = $obj;
202 unset($this->SubObjects[$obj['remove']['to']]['remove']);
203 }else{
204 if($obj['status'] == "FreshLoaded"){
205 $obj['status'] = "edited";
206 }
207 $this->SubObjects[$obj['cn']]=$obj;
208 }
210 $this->is_dialog=false;
211 unset($this->dialog);
212 $this->dialog=NULL;
213 }
214 }
216 /* Sort entries */
217 $tmp = $keys = array();
219 if($this->sort_by == "name"){
220 foreach($this->SubObjects as $key => $entry){
221 $keys[$key]=$entry['cn'];
222 }
223 }elseif($this->sort_by == "priority"){
224 foreach($this->SubObjects as $key => $entry){
225 $keys[$key]=$entry['FAIpriority'];
226 }
227 }
229 natcasesort($keys);
231 if($this->sort_order == "down"){
232 $keys =array_reverse($keys);
233 }
235 foreach($keys as $key => $order_var){
236 $tmp[$key]=$this->SubObjects[$key];
237 }
238 $this->SubObjects = $tmp;
240 /* Cancel Dialog */
241 if(isset($_POST['CancelSubObject'])){
242 $this->is_dialog=false;
243 unset($this->dialog);
244 $this->dialog=NULL;
245 }
247 /* Print dialog if $this->dialog is set */
248 if($this->dialog){
249 $this->dialog->save_object();
250 $display = $this->dialog->execute();
251 return($display);
252 }
254 /* Divlist added 23.02.2006
255 Containing FAIscripts
256 */
257 $divlist = new divlist("FAIscripts");
258 $plug = $_GET['plug'];
260 $divlist->SetHeader(array( array("string"=>"<a href='?plug=".$plug."&sort=name'>"._("Name")."</a>"),
261 array("string"=>"<a href='?plug=".$plug."&sort=priority'>"._("Priority")."</a>"),
262 array("string"=>_("Download")),
263 array("string"=>_("Action"))));
264 $divlist->SetHeight(300);
265 $divlist->SetWidth("100%");
267 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
268 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
269 $img_remo = "";
270 }else{
271 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
272 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
273 }
275 foreach($this->getList(true) as $key => $name){
277 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
278 $down = "";
279 }else{
280 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
281 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
282 </a>";
283 }
285 $divlist->AddEntry(array( array("string"=>$name['name']),
286 array("string"=>$name['FAIpriority'] , "attach" => "style='width:20px;'"),
287 array("string"=>$down , "attach" => "style='width:20px;'"),
288 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
289 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
290 }
291 $smarty->assign("Entry_divlist",$divlist->DrawList());
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 }
304 foreach($this->attributes as $attr){
305 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
306 }
308 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
309 return($display);
310 }
312 /* Generate listbox friendly SubObject list
313 */
314 function getList($use_dns=false){
315 $a_return=array();
316 foreach($this->SubObjects as $obj){
317 if($obj['status'] != "delete"){
318 if($use_dns){
319 if((isset($obj['description']))&&(!empty($obj['description']))){
320 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
321 }else{
322 $a_return[$obj['cn']]['name']= $obj['cn'];
323 }
324 $a_return[$obj['cn']]['dn']= $obj['dn'];
325 $a_return[$obj['cn']]['FAIpriority']= $obj['FAIpriority'];
326 }else{
327 if((isset($obj['description']))&&(!empty($obj['description']))){
328 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
329 }else{
330 $a_return[$obj['cn']]= $obj['cn'];
331 }
332 }
333 }
334 }
335 return($a_return);
336 }
338 /* Delete me, and all my subtrees
339 */
340 function remove_from_parent()
341 {
342 $ldap = $this->config->get_ldap_link();
343 $ldap->cd ($this->dn);
344 $ldap->rmdir_recursive($this->dn);
345 show_ldap_error($ldap->get_error(), _("Removing FAI script base failed"));
346 $this->handle_post_events("remove");
347 }
350 /* Save data to object
351 */
352 function save_object()
353 {
354 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
355 plugin::save_object();
356 foreach($this->attributes as $attrs){
357 if(isset($_POST[$attrs])){
358 $this->$attrs = $_POST[$attrs];
359 }
360 }
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 /* If this is a new script, check if a script with this name already exists */
384 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
386 /* Check if current name is already used for fai scripts in selected release */
387 $dn = 'cn='.$this->cn.",ou=scripts,".$this->release;
388 $ldap = $this->config->get_ldap_link();
389 $ldap->cat($dn);
390 if($ldap->count()){
392 $r =convert_department_dn($this->release);;
393 $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
394 }
395 }
396 return ($message);
397 }
400 /* Save to LDAP */
401 function save()
402 {
403 plugin::save();
405 $ldap = $this->config->get_ldap_link();
407 /* Copy & Paste : Ensure that FAIstate is copied too */
408 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
409 $this->attrs['FAIstate'] = $this->FAIstate;
410 }
412 $ldap->cat($this->dn,array("objectClass"));
413 if($ldap->count()!=0){
414 /* Write FAIscript to ldap*/
415 $ldap->cd($this->dn);
416 $this->cleanup();
417 $ldap->modify ($this->attrs);
419 }else{
420 /* Write FAIscript to ldap*/
421 $ldap->cd($this->config->current['BASE']);
422 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
423 $ldap->cd($this->dn);
424 $ldap->add($this->attrs);
425 }
426 show_ldap_error($ldap->get_error(), _("Creating FAI script base failed"));
428 /* Do object tagging */
429 $this->handle_object_tagging();
431 /* Prepare FAIscriptEntry to write it to ldap
432 * First sort array.
433 * Because we must delete old entries first.
434 * After deletion, we perform add and modify
435 */
436 $Objects = array();
438 /* We do not need to save untouched objects */
439 foreach($this->SubObjects as $name => $obj){
440 if($obj['status'] == "FreshLoaded"){
441 if($this->copy_paste_mode){
442 $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
443 $this->SubObjects[$name]['status'] = "new";
444 }else{
445 unset($this->SubObjects[$name]);
446 }
447 }
448 }
450 foreach($this->SubObjects as $name => $obj){
451 if($obj['status'] == "delete"){
452 $Objects[$name] = $obj;
453 }
454 }
455 foreach($this->SubObjects as $name => $obj){
456 if($obj['status'] != "delete"){
457 $Objects[$name] = $obj;
458 }
459 }
461 foreach($Objects as $name => $obj){
463 foreach($this->sub64coded as $codeIt){
464 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
465 }
467 $tmp = array();
468 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
469 foreach($attributes as $attrs){
470 if(empty($obj[$attrs])){
471 $obj[$attrs] = array();
472 }
473 if(!is_array($obj[$attrs])){
474 $tmp[$attrs] = stripslashes($obj[$attrs]);
475 }else{
476 $tmp[$attrs] = $obj[$attrs];
477 }
478 }
480 $tmp['objectClass'] = $this->subClasses;
482 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
484 if($obj['status']=="new"){
485 $ldap->cat($sub_dn,array("objectClass"));
486 if($ldap->count()){
487 $obj['status']="edited";
488 }
489 }
491 if(empty($tmp['FAIpriority'])){
492 $tmp['FAIpriority'] ="0";
493 }
495 /* Check if gosaAdministrativeUnitTag is required as object class */
496 if($obj['status'] == "edited"){
497 $ldap->cat($sub_dn,array("objectClass"));
498 $attrs = $ldap->fetch();
499 if(isset($attrs['objectClass'])){
500 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
501 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
502 }
503 }
504 }
506 if($obj['status'] == "delete"){
507 $ldap->cd($sub_dn);
508 $ldap->rmdir_recursive($sub_dn);
509 $this->handle_post_events("remove");
510 show_ldap_error($ldap->get_error(), _("Removing FAI script failed"));
511 }elseif($obj['status'] == "edited"){
512 $ldap->cd($sub_dn);
513 $this->cleanup();
514 $ldap->modify ($tmp);
516 $this->handle_post_events("modify");
517 }elseif($obj['status']=="new"){
518 if($tmp['description']==array()){
519 unset($tmp['description']);
520 }
521 if($tmp['FAIscript']==array()){
522 $tmp['FAIscript']=" ";
523 }
524 $ldap->cd($this->config->current['BASE']);
525 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
526 $ldap->cd($sub_dn);
527 $ldap->add($tmp);
528 $this->handle_post_events("add");
529 show_ldap_error($ldap->get_error(), _("Saving FAI script failed"));
530 }
532 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
533 }
534 }
537 /* return copy & paste dialog
538 */
539 function getCopyDialog()
540 {
541 /* Ask for cn */
542 $smarty = get_smarty();
543 $smarty->assign("cn" ,$this->cn);
544 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
545 $ret = array();
546 $ret['string'] = $str;
547 $ret['status'] = "";
548 return($ret);
549 }
551 /* Get posted cn */
552 function saveCopyDialog()
553 {
554 if(isset($_POST['cn'])){
555 $this->cn = $_POST['cn'];
556 }
557 }
560 }
562 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
563 ?>