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 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
199 $this->SubObjects[$entry]['status']= "delete";
200 }else{
201 unset($this->SubObjects[$entry]);
202 }
203 }
204 }
205 }
208 /* Save the edited entry */
209 if(isset($_POST['SaveSubObject'])){
211 /* Check if there are still errors remaining that must be fixed before saving */
212 $this->dialog->save_object();
213 $msgs = $this->dialog->check();
214 if(count($msgs)>0){
215 foreach($msgs as $msg){
216 print_red($msg);
217 }
218 }else{
220 /* Get return object */
221 $obj = $this->dialog->save();
223 /* If we have renamed the script entry, we must remove the old entry */
224 if(isset($obj['remove'])){
226 /* Get old entry values */
227 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
229 /* Depending on status, set new status */
230 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
231 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
232 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
233 unset($this->SubObjects[$obj['remove']['from']]);
234 }
236 /* Append the new entry */
237 $obj['status'] = "new";
238 $this->SubObjects[$obj['remove']['to']] = $obj;
239 unset($this->SubObjects[$obj['remove']['to']]['remove']);
240 }else{
242 /* Set new status and append the entry */
243 if($obj['status'] == "FreshLoaded"){
244 $obj['status'] = "edited";
245 }
246 $this->SubObjects[$obj['cn']]=$obj;
247 }
248 $this->is_dialog=false;
249 unset($this->dialog);
250 $this->dialog=NULL;
252 }
253 }
255 /* Sort entries */
256 $tmp = $keys = array();
257 foreach($this->SubObjects as $key => $entry){
258 $keys[$key]=$key;
259 }
260 natcasesort($keys);
261 foreach($keys as $key){
262 $tmp[$key]=$this->SubObjects[$key];
263 }
264 $this->SubObjects = $tmp;
266 /* Cancel Dialog */
267 if(isset($_POST['CancelSubObject'])){
268 $this->is_dialog=false;
269 unset($this->dialog);
270 $this->dialog=NULL;
271 }
273 /* Print dialog if $this->dialog is set */
274 if($this->dialog){
275 $this->dialog->save_object();
276 $display = $this->dialog->execute();
277 return($display);
278 }
280 /* Divlist added 23.02.2006
281 Containing FAIscripts
282 */
283 $divlist = new divSelectBox("FAIscripts");
284 $divlist->setHeight(400);
285 foreach($this->getList(true) as $key => $name){
287 $dn= $this->acl_base_for_current_object($name['dn']);
288 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
289 $act = "";
291 /* Hide delete icon if this object is freezed */
292 if($this->FAIstate == "freeze"){
293 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
294 }else{
295 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
296 if(preg_match("/d/",$acl)){
297 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
298 }
299 }
301 /* Check acls for download icon */
302 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
303 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
304 $down = "";
305 }else{
306 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
307 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
308 </a>";
309 }
311 /* Check if we are allowed to view this object */
312 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
313 if(preg_match("/r/",$s_acl)){
314 $divlist->AddEntry(array( array("string"=>$name['name']),
315 array("string"=>$down , "attach" => "style='width:20px;'"),
316 array("string"=>str_replace("%s",base64_encode($key),$act),
317 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
318 }
319 }
320 $smarty->assign("Entry_divlist",$divlist->DrawList());
323 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
324 * If we post the escaped strings they will be escaped again
325 */
326 foreach($this->attributes as $attrs){
327 if(get_magic_quotes_gpc()){
328 $smarty->assign($attrs,stripslashes($this->$attrs));
329 }else{
330 $smarty->assign($attrs,($this->$attrs));
331 }
332 }
334 $dn = $this->acl_base_for_current_object($this->dn);
335 $smarty->assign("sub_object_is_addable",
336 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
337 !preg_match("/freeze/",$this->FAIstate));
339 $tmp = $this->plInfo();
340 foreach($tmp['plProvidedAcls'] as $name => $translated){
341 $smarty->assign($name."ACL",$this->getacl($name));
342 }
344 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
345 return($display);
346 }
348 /* Generate listbox friendly SubObject list
349 */
350 function getList($use_dns=false){
351 $a_return=array();
352 foreach($this->SubObjects as $obj){
353 if($obj['status'] != "delete"){
354 if($use_dns){
355 if((isset($obj['description']))&&(!empty($obj['description']))){
356 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
357 }else{
358 $a_return[$obj['cn']]['name']= $obj['cn'];
359 }
360 $a_return[$obj['cn']]['dn']= $obj['dn'];
361 }else{
362 if((isset($obj['description']))&&(!empty($obj['description']))){
363 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
364 }else{
365 $a_return[$obj['cn']]= $obj['cn'];
366 }
367 }
368 }
369 }
370 return($a_return);
371 }
373 /* Delete me, and all my subtrees
374 */
375 function remove_from_parent()
376 {
377 if($this->acl_is_removeable()){
378 $ldap = $this->config->get_ldap_link();
379 $ldap->cd ($this->dn);
381 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
382 if($_SESSION['faifilter']['branch'] == "main"){
383 $use_dn = $this->dn;
384 }
386 prepare_to_save_FAI_object($use_dn,array(),true);
388 foreach($this->SubObjects as $name => $obj){
389 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
390 if($_SESSION['faifilter']['branch'] == "main"){
391 $use_dn = $obj['dn'];
392 }
393 prepare_to_save_FAI_object($use_dn,array(),true);
394 }
395 $this->handle_post_events("remove");
396 }
397 }
400 /* Save data to object
401 */
402 function save_object()
403 {
404 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
405 plugin::save_object();
406 foreach($this->attributes as $attrs){
407 if(isset($_POST[$attrs])){
408 $this->$attrs = $_POST[$attrs];
409 }
410 }
411 }
412 }
415 /* Check supplied data */
416 function check()
417 {
418 /* Call common method to give check the hook */
419 $message= plugin::check();
421 return ($message);
422 }
425 /* Save to LDAP */
426 function save()
427 {
428 plugin::save();
430 $ldap = $this->config->get_ldap_link();
432 prepare_to_save_FAI_object($this->dn,$this->attrs);
433 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
435 /* Do object tagging */
436 $this->handle_object_tagging();
438 /* Prepare FAIscriptEntry to write it to ldap
439 * First sort array.
440 * Because we must delete old entries first.
441 * After deletion, we perform add and modify
442 */
443 $Objects = array();
445 /* We do not need to save untouched objects */
446 foreach($this->SubObjects as $name => $obj){
447 if($obj['status'] == "FreshLoaded"){
448 unset($this->SubObjects[$name]);
449 }
450 }
452 foreach($this->SubObjects as $name => $obj){
453 if($obj['status'] == "delete"){
454 $Objects[$name] = $obj;
455 }
456 }
457 foreach($this->SubObjects as $name => $obj){
458 if($obj['status'] != "delete"){
459 $Objects[$name] = $obj;
460 }
461 }
463 foreach($Objects as $name => $obj){
465 foreach($this->sub64coded as $codeIt){
466 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
467 }
469 $tmp = array();
470 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
471 foreach($attributes as $attrs){
473 if(empty($obj[$attrs])){
474 $obj[$attrs] = array();
475 }
476 if(!is_array($obj[$attrs])){
477 $tmp[$attrs] = stripslashes($obj[$attrs]);
478 }else{
479 $tmp[$attrs] = $obj[$attrs];
480 }
481 }
483 $tmp['objectClass'] = $this->subClasses;
485 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
487 if($obj['status']=="new"){
488 $ldap->cat($sub_dn,array("objectClass"));
489 if($ldap->count()){
490 $obj['status']="edited";
491 }
492 }
494 if(empty($tmp['FAIpriority'])){
495 $tmp['FAIpriority'] ="0";
496 }
498 /* Check if gosaAdministrativeUnitTag is required as object class */
499 if($obj['status'] == "edited"){
500 $ldap->cat($sub_dn,array("objectClass"));
501 $attrs = $ldap->fetch();
502 if(isset($attrs['objectClass'])){
503 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
504 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
505 }
506 }
507 }
509 if($obj['status'] == "delete"){
510 prepare_to_save_FAI_object($sub_dn,array(),true);
511 $this->handle_post_events("remove");
512 }elseif($obj['status'] == "edited"){
513 prepare_to_save_FAI_object($sub_dn,$tmp);
514 $this->handle_post_events("modify");
515 }elseif($obj['status']=="new"){
516 prepare_to_save_FAI_object($sub_dn,$tmp);
517 $this->handle_post_events("add");
518 }
520 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
521 }
522 }
525 /* Return plugin informations for acl handling */
526 function plInfo()
527 {
528 return (array(
529 "plShortName" => _("Script"),
530 "plDescription" => _("FAI script"),
531 "plSelfModify" => FALSE,
532 "plDepends" => array(),
533 "plPriority" => 18,
534 "plSection" => array("administration"),
535 "plCategory" => array("fai"),
536 "plProvidedAcls" => array(
537 "cn" => _("Name")." ("._("Readonly").")",
538 "description" => _("Description"))
539 ));
540 }
541 }
543 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
544 ?>