1 <?php
3 class faiScript 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");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIscript");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIscriptEntry";
21 var $subClasses = array("top","FAIclass","FAIscriptEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiScriptEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAIpriority");
28 var $sub_Load_Later = array("FAIscript");
29 var $sub64coded = array();
30 var $subBinary = array("FAIscript");
32 /* Specific attributes */
33 var $cn = ""; // The class name for this object
34 var $description = ""; // The description for this set of partitions
35 var $is_dialog = false; // specifies which buttons will be shown to save or abort
36 var $dialog = NULL; // a dialog, e.g. new disk dialog
37 var $SubObjects = array(); // All leafobjects of this object
39 var $FAIstate ="";
41 var $ui;
43 function faiScript ($config, $dn= NULL)
44 {
45 /* Load Attributes */
46 plugin::plugin ($config, $dn);
48 /* If "dn==new" we try to create a new entry
49 * Else we must read all objects from ldap which belong to this entry.
50 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
51 */
52 if($dn != "new"){
54 $this->dn =$dn;
56 /* Get FAIstate
57 */
58 if(isset($this->attrs['FAIstate'][0])){
59 $this->FAIstate = $this->attrs['FAIstate'][0];
60 }
62 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
63 */
64 $ldap = $this->config->get_ldap_link();
65 $ldap->cd ($this->dn);
67 $attrs_to_search = $this->subAttributes;
68 $attrs_to_search[] = "FAIstate";
69 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
71 while($object = $ldap->fetch()){
73 /* Skip objects, that are tagged as removed */
74 if(isset($object['FAIstate'][0])){
75 if(preg_match("/removed$/",$object['FAIstate'][0])){
76 continue;
77 }
78 }
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;
86 }
87 }
88 $this->ui = get_userinfo();
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 /* Return a valid dn to fetch acls. Because 'new' will not work. */
124 function acl_base_for_current_object($dn)
125 {
126 if($dn == "new"){
127 if($this->dn == "new"){
128 $dn= "cn=dummy,".$_SESSION['CurrentMainBase'];
129 }else{
130 $dn = $this->dn;
131 }
132 }
133 return($dn);
134 }
137 function execute()
138 {
139 /* Call parent execute */
140 plugin::execute();
142 /* Fill templating stuff */
143 $smarty= get_smarty();
144 $display= "";
146 /* Add new sub object */
147 if(isset($_POST['AddSubObject'])){
148 $this->dialog= new $this->subClassName($this->config,"new");
149 $this->dialog->set_acl_base($this->acl_base);
150 $this->dialog->set_acl_category("fai");
151 $this->is_dialog=true;
152 }
154 if($this->dn != "new"){
155 $_SESSION['objectinfo']= $this->dn;
156 }
158 /* Handle posts */
159 $once = true;
160 foreach($_POST as $name => $value){
162 /* Edit script posted */
163 if(preg_match("/^editscript_/",$name)&&($once)){
165 /* Get posted entry id */
166 $once = false;
167 $entry = preg_replace("/^editscript_/","",$name);
168 $entry = base64_decode(preg_replace("/_.*/","",$entry));
170 /* Get object, and load missing entry values */
171 $obj = $this->SubObjects[$entry];
172 if($obj['status'] == "FreshLoaded"){
173 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
174 }
176 /* Create new dialog and set acl attributes */
177 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
178 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
179 $this->dialog->set_acl_category("fai");
181 /* Assign some additional dialog informations like headline and parent */
182 $_SESSION['objectinfo'] = $obj['dn'];
183 $this->dialog->parent = &$this;
184 $this->is_dialog=true;
185 }
187 /* Delete script requested */
188 if(preg_match("/^deletescript_/",$name)&&($once)){
190 /* Parse out posted entry id */
191 $once = false;
192 $entry = preg_replace("/^deletescript_/","",$name);
193 $entry = base64_decode(preg_replace("/_.*/","",$entry));
195 /* Check acls, are we allowed to delete an entry */
196 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($this->SubObjects[$entry]['dn']),"fai/faiScriptEntry") ;
197 if(preg_match("/d/",$acl)){
198 $status = $this->SubObjects[$entry]['status'];
199 if($status == "edited" || $status == "FreshLoaded"){
200 $this->SubObjects[$entry]['status']= "delete";
201 }else{
202 unset($this->SubObjects[$entry]);
203 }
204 }
205 }
206 }
209 /* Save the edited entry */
210 if(isset($_POST['SaveSubObject'])){
212 /* Check if there are still errors remaining that must be fixed before saving */
213 $this->dialog->save_object();
214 $msgs = $this->dialog->check();
215 if(count($msgs)>0){
216 foreach($msgs as $msg){
217 print_red($msg);
218 }
219 }else{
221 /* Get return object */
222 $obj = $this->dialog->save();
224 /* If we have renamed the script entry, we must remove the old entry */
225 if(isset($obj['remove'])){
227 /* Get old entry values */
228 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
230 /* Depending on status, set new status */
231 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
232 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
233 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
234 unset($this->SubObjects[$obj['remove']['from']]);
235 }
237 /* Append the new entry */
238 $obj['status'] = "new";
239 $this->SubObjects[$obj['remove']['to']] = $obj;
240 unset($this->SubObjects[$obj['remove']['to']]['remove']);
241 }else{
243 /* Set new status and append the entry */
244 if($obj['status'] == "FreshLoaded"){
245 $obj['status'] = "edited";
246 }
247 $this->SubObjects[$obj['cn']]=$obj;
248 }
249 $this->is_dialog=false;
250 unset($this->dialog);
251 $this->dialog=NULL;
253 }
254 }
256 /* Sort entries */
257 $tmp = $keys = array();
258 foreach($this->SubObjects as $key => $entry){
259 $keys[$key]=$key;
260 }
261 natcasesort($keys);
262 foreach($keys as $key){
263 $tmp[$key]=$this->SubObjects[$key];
264 }
265 $this->SubObjects = $tmp;
267 /* Cancel Dialog */
268 if(isset($_POST['CancelSubObject'])){
269 $this->is_dialog=false;
270 unset($this->dialog);
271 $this->dialog=NULL;
272 }
274 /* Print dialog if $this->dialog is set */
275 if($this->dialog){
276 $this->dialog->save_object();
277 $display = $this->dialog->execute();
278 return($display);
279 }
281 /* Divlist added 23.02.2006
282 Containing FAIscripts
283 */
284 $divlist = new divSelectBox("FAIscripts");
285 $divlist->setHeight(400);
286 foreach($this->getList(true) as $key => $name){
288 $dn= $this->acl_base_for_current_object($name['dn']);
289 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
290 $act = "";
292 /* Hide delete icon if this object is freezed */
293 if($this->FAIstate == "freeze"){
294 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
295 }else{
296 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
297 if(preg_match("/d/",$acl)){
298 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
299 }
300 }
302 /* Check acls for download icon */
303 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
304 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
305 $down = "";
306 }else{
307 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
308 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
309 </a>";
310 }
312 /* Check if we are allowed to view this object */
313 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
314 if(preg_match("/r/",$s_acl)){
315 $divlist->AddEntry(array( array("string"=>$name['name']),
316 array("string"=>$down , "attach" => "style='width:20px;'"),
317 array("string"=>str_replace("%s",base64_encode($key),$act),
318 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
319 }
320 }
321 $smarty->assign("Entry_divlist",$divlist->DrawList());
324 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
325 * If we post the escaped strings they will be escaped again
326 */
327 foreach($this->attributes as $attrs){
328 if(get_magic_quotes_gpc()){
329 $smarty->assign($attrs,stripslashes($this->$attrs));
330 }else{
331 $smarty->assign($attrs,($this->$attrs));
332 }
333 }
335 $dn = $this->acl_base_for_current_object($this->dn);
336 $smarty->assign("sub_object_is_addable",
337 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
338 !preg_match("/freeze/",$this->FAIstate));
340 $tmp = $this->plInfo();
341 foreach($tmp['plProvidedAcls'] as $name => $translated){
342 $smarty->assign($name."ACL",$this->getacl($name));
343 }
345 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
346 return($display);
347 }
349 /* Generate listbox friendly SubObject list
350 */
351 function getList($use_dns=false){
352 $a_return=array();
353 foreach($this->SubObjects as $obj){
354 if($obj['status'] != "delete"){
355 if($use_dns){
356 if((isset($obj['description']))&&(!empty($obj['description']))){
357 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
358 }else{
359 $a_return[$obj['cn']]['name']= $obj['cn'];
360 }
361 $a_return[$obj['cn']]['dn']= $obj['dn'];
362 }else{
363 if((isset($obj['description']))&&(!empty($obj['description']))){
364 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
365 }else{
366 $a_return[$obj['cn']]= $obj['cn'];
367 }
368 }
369 }
370 }
371 return($a_return);
372 }
374 /* Delete me, and all my subtrees
375 */
376 function remove_from_parent()
377 {
378 if($this->acl_is_removeable()){
379 $ldap = $this->config->get_ldap_link();
380 $ldap->cd ($this->dn);
382 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
383 if($_SESSION['faifilter']['branch'] == "main"){
384 $use_dn = $this->dn;
385 }
387 prepare_to_save_FAI_object($use_dn,array(),true);
389 foreach($this->SubObjects as $name => $obj){
390 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
391 if($_SESSION['faifilter']['branch'] == "main"){
392 $use_dn = $obj['dn'];
393 }
394 prepare_to_save_FAI_object($use_dn,array(),true);
395 }
396 $this->handle_post_events("remove");
397 }
398 }
401 /* Save data to object
402 */
403 function save_object()
404 {
405 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
406 plugin::save_object();
407 foreach($this->attributes as $attrs){
408 if(isset($_POST[$attrs])){
409 $this->$attrs = $_POST[$attrs];
410 }
411 }
412 }
413 }
416 /* Check supplied data */
417 function check()
418 {
419 /* Call common method to give check the hook */
420 $message= plugin::check();
422 return ($message);
423 }
426 /* Save to LDAP */
427 function save()
428 {
429 plugin::save();
431 $ldap = $this->config->get_ldap_link();
433 prepare_to_save_FAI_object($this->dn,$this->attrs);
434 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
436 /* Do object tagging */
437 $this->handle_object_tagging();
439 /* Prepare FAIscriptEntry to write it to ldap
440 * First sort array.
441 * Because we must delete old entries first.
442 * After deletion, we perform add and modify
443 */
444 $Objects = array();
446 /* We do not need to save untouched objects */
447 foreach($this->SubObjects as $name => $obj){
448 if($obj['status'] == "FreshLoaded"){
449 unset($this->SubObjects[$name]);
450 }
451 }
453 foreach($this->SubObjects as $name => $obj){
454 if($obj['status'] == "delete"){
455 $Objects[$name] = $obj;
456 }
457 }
458 foreach($this->SubObjects as $name => $obj){
459 if($obj['status'] != "delete"){
460 $Objects[$name] = $obj;
461 }
462 }
464 foreach($Objects as $name => $obj){
466 foreach($this->sub64coded as $codeIt){
467 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
468 }
470 $tmp = array();
471 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
472 foreach($attributes as $attrs){
474 if(empty($obj[$attrs])){
475 $obj[$attrs] = array();
476 }
477 if(!is_array($obj[$attrs])){
478 $tmp[$attrs] = stripslashes($obj[$attrs]);
479 }else{
480 $tmp[$attrs] = $obj[$attrs];
481 }
482 }
484 $tmp['objectClass'] = $this->subClasses;
486 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
488 if($obj['status']=="new"){
489 $ldap->cat($sub_dn,array("objectClass"));
490 if($ldap->count()){
491 $obj['status']="edited";
492 }
493 }
495 if(empty($tmp['FAIpriority'])){
496 $tmp['FAIpriority'] ="0";
497 }
499 /* Check if gosaAdministrativeUnitTag is required as object class */
500 if($obj['status'] == "edited"){
501 $ldap->cat($sub_dn,array("objectClass"));
502 $attrs = $ldap->fetch();
503 if(isset($attrs['objectClass'])){
504 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
505 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
506 }
507 }
508 }
510 if($obj['status'] == "delete"){
511 prepare_to_save_FAI_object($sub_dn,array(),true);
512 $this->handle_post_events("remove");
513 }elseif($obj['status'] == "edited"){
514 prepare_to_save_FAI_object($sub_dn,$tmp);
515 $this->handle_post_events("modify");
516 }elseif($obj['status']=="new"){
517 prepare_to_save_FAI_object($sub_dn,$tmp);
518 $this->handle_post_events("add");
519 }
521 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
522 }
523 }
526 /* Return plugin informations for acl handling */
527 function plInfo()
528 {
529 return (array(
530 "plShortName" => _("Script"),
531 "plDescription" => _("FAI script"),
532 "plSelfModify" => FALSE,
533 "plDepends" => array(),
534 "plPriority" => 18,
535 "plSection" => array("administration"),
536 "plCategory" => array("fai"),
537 "plProvidedAcls" => array(
538 "cn" => _("Name")." ("._("Readonly").")",
539 "description" => _("Description"))
540 ));
541 }
542 }
544 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
545 ?>