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 function faiScript ($config, $dn= NULL)
42 {
43 /* Load Attributes */
44 plugin::plugin ($config, $dn);
46 /* If "dn==new" we try to create a new entry
47 * Else we must read all objects from ldap which belong to this entry.
48 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
49 */
50 if($dn != "new"){
52 $this->dn =$dn;
54 /* Get FAIstate
55 */
56 if(isset($this->attrs['FAIstate'][0])){
57 $this->FAIstate = $this->attrs['FAIstate'][0];
58 }
60 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
61 */
62 $ldap = $this->config->get_ldap_link();
63 $ldap->cd ($this->dn);
65 $attrs_to_search = $this->subAttributes;
66 $attrs_to_search[] = "FAIstate";
67 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
69 while($object = $ldap->fetch()){
70 /* Set status for save management */
71 $objects = array();
72 $objects['status'] = "FreshLoaded";
73 $objects['dn'] = $object['dn'];
74 $objects = $this->get_object_attributes($objects,$this->subAttributes);
75 $this->SubObjects[$objects['cn']] = $objects;
76 }
77 }
78 }
81 /* Reload some attributes */
82 function get_object_attributes($object,$attributes)
83 {
84 $ldap = $this->config->get_ldap_link();
85 $ldap->cd($this->config->current['BASE']);
86 $ldap->cat($object['dn'],$attributes);
87 $tmp = $ldap->fetch();
89 foreach($attributes as $attrs){
90 if(isset($tmp[$attrs][0])){
91 $var = $tmp[$attrs][0];
93 /* Check if we must decode some attributes */
94 if(in_array_ics($attrs,$this->sub64coded)){
95 $var = base64_decode($var);
96 }
98 /* check if this is a binary entry */
99 if(in_array_ics($attrs,$this->subBinary)){
100 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
101 }
103 /* Fix slashes */
104 $var = addslashes($var);
105 $object[$attrs] = $var;
106 }
107 }
108 return($object);
109 }
112 function execute()
113 {
114 /* Call parent execute */
115 plugin::execute();
117 /* Fill templating stuff */
118 $smarty= get_smarty();
119 $display= "";
121 /* Add new sub object */
122 if(isset($_POST['AddSubObject'])){
124 $c_dn = $this->dn;
125 if($c_dn == "new"){
126 $c_dn = $_SESSION['CurrentMainBase'];
127 }
129 $this->dialog= new $this->subClassName($this->config,"new");
130 $this->dialog->set_acl_base($c_dn);
131 $this->dialog->set_acl_category("fai");
132 $this->is_dialog=true;
133 }
135 if($this->dn != "new"){
136 $_SESSION['objectinfo']= $this->dn;
137 }
140 /* New Listhandling
141 */
142 $once = true;
143 foreach($_POST as $name => $value){
144 if(preg_match("/^editscript_/",$name)&&($once)){
146 $once = false;
147 $entry = preg_replace("/^editscript_/","",$name);
148 $entry = base64_decode(preg_replace("/_.*/","",$entry));
150 $obj = $this->SubObjects[$entry];
151 if($obj['status'] == "FreshLoaded"){
152 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
153 }
154 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
155 $this->dialog->set_acl_base($this->dn);
156 $this->dialog->set_acl_category("fai");
159 $_SESSION['objectinfo'] = $obj['dn'];
160 $this->dialog->parent = &$this;
161 $this->is_dialog=true;
162 }
163 if(preg_match("/^deletescript_/",$name)&&($once)){
164 $once = false;
165 $entry = preg_replace("/^deletescript_/","",$name);
166 $entry = base64_decode(preg_replace("/_.*/","",$entry));
169 $dn = $this->SubObjects[$entry]['dn'];
171 if($dn == "new"){
172 $dn = $_SESSION['CurrentMainBase'];
173 }
174 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
176 if(preg_match("/d/",$acl)){
177 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
178 $this->SubObjects[$entry]['status']= "delete";
179 }else{
180 unset($this->SubObjects[$entry]);
181 }
182 }
183 }
184 }
185 ///// Ende new list handling
188 /* Edit selected Sub Object */
189 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
190 $script = $_POST['SubObject'][0];
191 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
192 $this->dialog->set_acl_base($_SESSION['CurrentMainBase']);
193 $this->dialog->set_acl_category("fai");
194 $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
195 $this->is_dialog=true;
196 }
199 /* Save Dialog */
200 if(isset($_POST['SaveSubObject'])){
201 $this->dialog->save_object();
202 $msgs = $this->dialog->check();
203 if(count($msgs)>0){
204 foreach($msgs as $msg){
205 print_red($msg);
206 }
207 }else{
208 /* Get return object */
209 $obj = $this->dialog->save();
210 if(isset($obj['remove'])){
212 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
214 /* Depending on status, set new status */
215 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
216 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
217 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
218 unset($this->SubObjects[$obj['remove']['from']]);
219 }
220 $obj['status'] = "new";
221 $this->SubObjects[$obj['remove']['to']] = $obj;
222 unset($this->SubObjects[$obj['remove']['to']]['remove']);
223 }else{
224 if($obj['status'] == "FreshLoaded"){
225 $obj['status'] = "edited";
226 }
227 $this->SubObjects[$obj['cn']]=$obj;
228 }
229 $this->is_dialog=false;
230 unset($this->dialog);
231 $this->dialog=NULL;
233 }
234 }
236 /* Sort entries */
237 $tmp = $keys = array();
238 foreach($this->SubObjects as $key => $entry){
239 $keys[$key]=$key;
240 }
241 natcasesort($keys);
242 foreach($keys as $key){
243 $tmp[$key]=$this->SubObjects[$key];
244 }
245 $this->SubObjects = $tmp;
247 /* Cancel Dialog */
248 if(isset($_POST['CancelSubObject'])){
249 $this->is_dialog=false;
250 unset($this->dialog);
251 $this->dialog=NULL;
252 }
254 /* Print dialog if $this->dialog is set */
255 if($this->dialog){
256 $this->dialog->save_object();
257 $display = $this->dialog->execute();
258 return($display);
259 }
261 /* Divlist added 23.02.2006
262 Containing FAIscripts
263 */
264 $divlist = new divSelectBox("FAIscripts");
265 $divlist->setHeight(400);
266 foreach($this->getList(true) as $key => $name){
268 $dn= $name['dn'];
269 if($dn == "new"){
270 $dn = $_SESSION['CurrentMainBase'];
271 }
272 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
273 $act = "";
275 if($this->FAIstate == "freeze"){
276 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
277 }else{
278 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
279 if(preg_match("/d/",$acl)){
280 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
281 }
282 }
284 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
285 $down = "";
286 }else{
287 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
288 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
289 </a>";
290 }
292 $divlist->AddEntry(array( array("string"=>$name['name']),
293 array("string"=>$down , "attach" => "style='width:20px;'"),
294 array("string"=>str_replace("%s",base64_encode($key),$act),
295 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
296 }
297 $smarty->assign("Entry_divlist",$divlist->DrawList());
299 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
300 * If we post the escaped strings they will be escaped again
301 */
302 foreach($this->attributes as $attrs){
303 if(get_magic_quotes_gpc()){
304 $smarty->assign($attrs,stripslashes($this->$attrs));
305 }else{
306 $smarty->assign($attrs,($this->$attrs));
307 }
308 }
310 $tmp = $this->plInfo();
311 $this->ui = get_userinfo();
313 $c_dn = $this->dn;
314 if($c_dn == "new"){
315 $c_dn = $_SESSION['CurrentMainBase'];
316 }
317 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
318 foreach($tmp['plProvidedAcls'] as $name => $translated){
319 $smarty->assign($name."ACL",$this->getacl($name));
320 }
322 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
323 return($display);
324 }
326 /* Generate listbox friendly SubObject list
327 */
328 function getList($use_dns=false){
329 $a_return=array();
330 foreach($this->SubObjects as $obj){
331 if($obj['status'] != "delete"){
332 if($use_dns){
333 if((isset($obj['description']))&&(!empty($obj['description']))){
334 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
335 }else{
336 $a_return[$obj['cn']]['name']= $obj['cn'];
337 }
338 $a_return[$obj['cn']]['dn']= $obj['dn'];
339 }else{
340 if((isset($obj['description']))&&(!empty($obj['description']))){
341 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
342 }else{
343 $a_return[$obj['cn']]= $obj['cn'];
344 }
345 }
346 }
347 }
348 return($a_return);
349 }
351 /* Delete me, and all my subtrees
352 */
353 function remove_from_parent()
354 {
355 $ldap = $this->config->get_ldap_link();
356 $ldap->cd ($this->dn);
358 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
359 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
360 if($_SESSION['faifilter']['branch'] == "main"){
361 $use_dn = $this->dn;
362 }
364 prepare_to_save_FAI_object($use_dn,array(),true);
366 foreach($this->SubObjects as $name => $obj){
367 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
368 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
369 if($_SESSION['faifilter']['branch'] == "main"){
370 $use_dn = $obj['dn'];
371 }
372 prepare_to_save_FAI_object($use_dn,array(),true);
373 }
374 $this->handle_post_events("remove");
375 }
378 /* Save data to object
379 */
380 function save_object()
381 {
382 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
383 plugin::save_object();
384 foreach($this->attributes as $attrs){
385 if(isset($_POST[$attrs])){
386 $this->$attrs = $_POST[$attrs];
387 }
388 }
389 }
390 }
393 /* Check supplied data */
394 function check()
395 {
396 /* Call common method to give check the hook */
397 $message= plugin::check();
399 return ($message);
400 }
403 /* Save to LDAP */
404 function save()
405 {
406 plugin::save();
408 $ldap = $this->config->get_ldap_link();
410 prepare_to_save_FAI_object($this->dn,$this->attrs);
411 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
413 /* Do object tagging */
414 $this->handle_object_tagging();
416 /* Prepare FAIscriptEntry to write it to ldap
417 * First sort array.
418 * Because we must delete old entries first.
419 * After deletion, we perform add and modify
420 */
421 $Objects = array();
423 /* We do not need to save untouched objects */
424 foreach($this->SubObjects as $name => $obj){
425 if($obj['status'] == "FreshLoaded"){
426 unset($this->SubObjects[$name]);
427 }
428 }
430 foreach($this->SubObjects as $name => $obj){
431 if($obj['status'] == "delete"){
432 $Objects[$name] = $obj;
433 }
434 }
435 foreach($this->SubObjects as $name => $obj){
436 if($obj['status'] != "delete"){
437 $Objects[$name] = $obj;
438 }
439 }
441 foreach($Objects as $name => $obj){
443 foreach($this->sub64coded as $codeIt){
444 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
445 }
447 $tmp = array();
448 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
449 foreach($attributes as $attrs){
451 if(empty($obj[$attrs])){
452 $obj[$attrs] = array();
453 }
454 if(!is_array($obj[$attrs])){
455 $tmp[$attrs] = stripslashes($obj[$attrs]);
456 }else{
457 $tmp[$attrs] = $obj[$attrs];
458 }
459 }
461 $tmp['objectClass'] = $this->subClasses;
463 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
465 if($obj['status']=="new"){
466 $ldap->cat($sub_dn,array("objectClass"));
467 if($ldap->count()){
468 $obj['status']="edited";
469 }
470 }
472 if(empty($tmp['FAIpriority'])){
473 $tmp['FAIpriority'] ="0";
474 }
476 /* Check if gosaAdministrativeUnitTag is required as object class */
477 if($obj['status'] == "edited"){
478 $ldap->cat($sub_dn,array("objectClass"));
479 $attrs = $ldap->fetch();
480 if(isset($attrs['objectClass'])){
481 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
482 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
483 }
484 }
485 }
487 if($obj['status'] == "delete"){
488 prepare_to_save_FAI_object($sub_dn,array(),true);
489 $this->handle_post_events("remove");
490 }elseif($obj['status'] == "edited"){
491 prepare_to_save_FAI_object($sub_dn,$tmp);
492 $this->handle_post_events("modify");
493 }elseif($obj['status']=="new"){
494 prepare_to_save_FAI_object($sub_dn,$tmp);
495 $this->handle_post_events("add");
496 }
498 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
499 }
500 }
503 /* Return plugin informations for acl handling */
504 function plInfo()
505 {
506 return (array(
507 "plShortName" => _("Script"),
508 "plDescription" => _("FAI script"),
509 "plSelfModify" => FALSE,
510 "plDepends" => array(),
511 "plPriority" => 18,
512 "plSection" => array("administration"),
513 "plCategory" => array("fai"),
514 "plProvidedAcls" => array(
515 "cn" => _("Name")." ("._("Readonly").")",
516 "description" => _("Description"))
517 ));
518 }
519 }
521 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
522 ?>