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 }
122 function acl_base_for_current_object($dn)
123 {
124 if($dn == "new"){
125 if($this->dn == "new"){
126 $dn= $_SESSION['CurrentMainBase'];
127 }else{
128 $dn = $this->dn;
129 }
130 }
131 return($dn);
132 }
135 function execute()
136 {
137 /* Call parent execute */
138 plugin::execute();
140 /* Fill templating stuff */
141 $smarty= get_smarty();
142 $display= "";
144 /* Add new sub object */
145 if(isset($_POST['AddSubObject'])){
146 $this->dialog= new $this->subClassName($this->config,"new");
147 $this->dialog->set_acl_base($this->acl_base);
148 $this->dialog->set_acl_category("fai");
149 $this->is_dialog=true;
150 }
152 if($this->dn != "new"){
153 $_SESSION['objectinfo']= $this->dn;
154 }
156 /* New Listhandling
157 */
158 $once = true;
159 foreach($_POST as $name => $value){
160 if(preg_match("/^editscript_/",$name)&&($once)){
162 $once = false;
163 $entry = preg_replace("/^editscript_/","",$name);
164 $entry = base64_decode(preg_replace("/_.*/","",$entry));
166 $obj = $this->SubObjects[$entry];
167 if($obj['status'] == "FreshLoaded"){
168 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
169 }
170 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
171 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
172 $this->dialog->set_acl_category("fai");
175 $_SESSION['objectinfo'] = $obj['dn'];
176 $this->dialog->parent = &$this;
177 $this->is_dialog=true;
178 }
179 if(preg_match("/^deletescript_/",$name)&&($once)){
180 $once = false;
181 $entry = preg_replace("/^deletescript_/","",$name);
182 $entry = base64_decode(preg_replace("/_.*/","",$entry));
184 $acl = $this->ui->get_permissions($this->acl_base_for_current_object($this->SubObjects[$entry]['dn']),"fai/faiScriptEntry") ;
185 if(preg_match("/d/",$acl)){
186 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
187 $this->SubObjects[$entry]['status']= "delete";
188 }else{
189 unset($this->SubObjects[$entry]);
190 }
191 }
192 }
193 }
194 ///// Ende new list handling
197 /* Edit selected Sub Object */
198 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
199 $script = $_POST['SubObject'][0];
200 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
201 $this->dialog->set_acl_base($this->acl_base_for_current_object($this->SubObjects[$script]['dn']));
202 $this->dialog->set_acl_category("fai");
203 $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
204 $this->is_dialog=true;
205 }
208 /* Save Dialog */
209 if(isset($_POST['SaveSubObject'])){
210 $this->dialog->save_object();
211 $msgs = $this->dialog->check();
212 if(count($msgs)>0){
213 foreach($msgs as $msg){
214 print_red($msg);
215 }
216 }else{
217 /* Get return object */
218 $obj = $this->dialog->save();
219 if(isset($obj['remove'])){
221 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
223 /* Depending on status, set new status */
224 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
225 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
226 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
227 unset($this->SubObjects[$obj['remove']['from']]);
228 }
229 $obj['status'] = "new";
230 $this->SubObjects[$obj['remove']['to']] = $obj;
231 unset($this->SubObjects[$obj['remove']['to']]['remove']);
232 }else{
233 if($obj['status'] == "FreshLoaded"){
234 $obj['status'] = "edited";
235 }
236 $this->SubObjects[$obj['cn']]=$obj;
237 }
238 $this->is_dialog=false;
239 unset($this->dialog);
240 $this->dialog=NULL;
242 }
243 }
245 /* Sort entries */
246 $tmp = $keys = array();
247 foreach($this->SubObjects as $key => $entry){
248 $keys[$key]=$key;
249 }
250 natcasesort($keys);
251 foreach($keys as $key){
252 $tmp[$key]=$this->SubObjects[$key];
253 }
254 $this->SubObjects = $tmp;
256 /* Cancel Dialog */
257 if(isset($_POST['CancelSubObject'])){
258 $this->is_dialog=false;
259 unset($this->dialog);
260 $this->dialog=NULL;
261 }
263 /* Print dialog if $this->dialog is set */
264 if($this->dialog){
265 $this->dialog->save_object();
266 $display = $this->dialog->execute();
267 return($display);
268 }
270 /* Divlist added 23.02.2006
271 Containing FAIscripts
272 */
273 $divlist = new divSelectBox("FAIscripts");
274 $divlist->setHeight(400);
275 foreach($this->getList(true) as $key => $name){
277 $dn= $this->acl_base_for_current_object($name['dn']);
278 $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry") ;
279 $act = "";
281 /* Hide delete icon if this object is freezed */
282 if($this->FAIstate == "freeze"){
283 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
284 }else{
285 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
286 if(preg_match("/d/",$acl)){
287 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
288 }
289 }
291 /* Check acls for download icon */
292 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript") ;
293 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
294 $down = "";
295 }else{
296 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
297 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
298 </a>";
299 }
301 /* Check if we are allowed to view this object */
302 $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn") ;
303 if(preg_match("/r/",$s_acl)){
304 $divlist->AddEntry(array( array("string"=>$name['name']),
305 array("string"=>$down , "attach" => "style='width:20px;'"),
306 array("string"=>str_replace("%s",base64_encode($key),$act),
307 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
308 }
309 }
310 $smarty->assign("Entry_divlist",$divlist->DrawList());
312 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
313 * If we post the escaped strings they will be escaped again
314 */
315 foreach($this->attributes as $attrs){
316 if(get_magic_quotes_gpc()){
317 $smarty->assign($attrs,stripslashes($this->$attrs));
318 }else{
319 $smarty->assign($attrs,($this->$attrs));
320 }
321 }
323 $dn = $this->acl_base_for_current_object($this->dn);
324 $smarty->assign("sub_object_is_addable",
325 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
326 !preg_match("/freeze/",$this->FAIstate));
328 $tmp = $this->plInfo();
329 foreach($tmp['plProvidedAcls'] as $name => $translated){
330 $smarty->assign($name."ACL",$this->getacl($name));
331 }
333 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
334 return($display);
335 }
337 /* Generate listbox friendly SubObject list
338 */
339 function getList($use_dns=false){
340 $a_return=array();
341 foreach($this->SubObjects as $obj){
342 if($obj['status'] != "delete"){
343 if($use_dns){
344 if((isset($obj['description']))&&(!empty($obj['description']))){
345 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
346 }else{
347 $a_return[$obj['cn']]['name']= $obj['cn'];
348 }
349 $a_return[$obj['cn']]['dn']= $obj['dn'];
350 }else{
351 if((isset($obj['description']))&&(!empty($obj['description']))){
352 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
353 }else{
354 $a_return[$obj['cn']]= $obj['cn'];
355 }
356 }
357 }
358 }
359 return($a_return);
360 }
362 /* Delete me, and all my subtrees
363 */
364 function remove_from_parent()
365 {
366 if($this->acl_is_removeable()){
367 $ldap = $this->config->get_ldap_link();
368 $ldap->cd ($this->dn);
370 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
371 if($_SESSION['faifilter']['branch'] == "main"){
372 $use_dn = $this->dn;
373 }
375 prepare_to_save_FAI_object($use_dn,array(),true);
377 foreach($this->SubObjects as $name => $obj){
378 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
379 if($_SESSION['faifilter']['branch'] == "main"){
380 $use_dn = $obj['dn'];
381 }
382 prepare_to_save_FAI_object($use_dn,array(),true);
383 }
384 $this->handle_post_events("remove");
385 }
386 }
389 /* Save data to object
390 */
391 function save_object()
392 {
393 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
394 plugin::save_object();
395 foreach($this->attributes as $attrs){
396 if(isset($_POST[$attrs])){
397 $this->$attrs = $_POST[$attrs];
398 }
399 }
400 }
401 }
404 /* Check supplied data */
405 function check()
406 {
407 /* Call common method to give check the hook */
408 $message= plugin::check();
410 return ($message);
411 }
414 /* Save to LDAP */
415 function save()
416 {
417 plugin::save();
419 $ldap = $this->config->get_ldap_link();
421 prepare_to_save_FAI_object($this->dn,$this->attrs);
422 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
424 /* Do object tagging */
425 $this->handle_object_tagging();
427 /* Prepare FAIscriptEntry to write it to ldap
428 * First sort array.
429 * Because we must delete old entries first.
430 * After deletion, we perform add and modify
431 */
432 $Objects = array();
434 /* We do not need to save untouched objects */
435 foreach($this->SubObjects as $name => $obj){
436 if($obj['status'] == "FreshLoaded"){
437 unset($this->SubObjects[$name]);
438 }
439 }
441 foreach($this->SubObjects as $name => $obj){
442 if($obj['status'] == "delete"){
443 $Objects[$name] = $obj;
444 }
445 }
446 foreach($this->SubObjects as $name => $obj){
447 if($obj['status'] != "delete"){
448 $Objects[$name] = $obj;
449 }
450 }
452 foreach($Objects as $name => $obj){
454 foreach($this->sub64coded as $codeIt){
455 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
456 }
458 $tmp = array();
459 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
460 foreach($attributes as $attrs){
462 if(empty($obj[$attrs])){
463 $obj[$attrs] = array();
464 }
465 if(!is_array($obj[$attrs])){
466 $tmp[$attrs] = stripslashes($obj[$attrs]);
467 }else{
468 $tmp[$attrs] = $obj[$attrs];
469 }
470 }
472 $tmp['objectClass'] = $this->subClasses;
474 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
476 if($obj['status']=="new"){
477 $ldap->cat($sub_dn,array("objectClass"));
478 if($ldap->count()){
479 $obj['status']="edited";
480 }
481 }
483 if(empty($tmp['FAIpriority'])){
484 $tmp['FAIpriority'] ="0";
485 }
487 /* Check if gosaAdministrativeUnitTag is required as object class */
488 if($obj['status'] == "edited"){
489 $ldap->cat($sub_dn,array("objectClass"));
490 $attrs = $ldap->fetch();
491 if(isset($attrs['objectClass'])){
492 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
493 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
494 }
495 }
496 }
498 if($obj['status'] == "delete"){
499 prepare_to_save_FAI_object($sub_dn,array(),true);
500 $this->handle_post_events("remove");
501 }elseif($obj['status'] == "edited"){
502 prepare_to_save_FAI_object($sub_dn,$tmp);
503 $this->handle_post_events("modify");
504 }elseif($obj['status']=="new"){
505 prepare_to_save_FAI_object($sub_dn,$tmp);
506 $this->handle_post_events("add");
507 }
509 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
510 }
511 }
514 /* Return plugin informations for acl handling */
515 function plInfo()
516 {
517 return (array(
518 "plShortName" => _("Script"),
519 "plDescription" => _("FAI script"),
520 "plSelfModify" => FALSE,
521 "plDepends" => array(),
522 "plPriority" => 18,
523 "plSection" => array("administration"),
524 "plCategory" => array("fai"),
525 "plProvidedAcls" => array(
526 "cn" => _("Name")." ("._("Readonly").")",
527 "description" => _("Description"))
528 ));
529 }
530 }
532 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
533 ?>