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 $this->acl ="#all#";
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 /* Set acls
55 */
56 $ui = get_userinfo();
57 $acl = get_permissions ($this->dn, $ui->subtreeACL);
58 $acli = get_module_permission($acl, "FAIclass", $this->dn);
59 $this->acl=$acli;
61 $this->dn =$dn;
63 /* Get FAIstate
64 */
65 if(isset($this->attrs['FAIstate'][0])){
66 $this->FAIstate = $this->attrs['FAIstate'][0];
67 }
69 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
70 */
71 $ldap = $this->config->get_ldap_link();
72 $ldap->cd ($this->dn);
74 $attrs_to_search = $this->subAttributes;
75 $attrs_to_search[] = "FAIstate";
76 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
78 while($object = $ldap->fetch()){
79 /* Set status for save management */
80 $objects = array();
81 $objects['status'] = "FreshLoaded";
82 $objects['dn'] = $object['dn'];
83 $objects = $this->get_object_attributes($objects,$this->subAttributes);
84 $this->SubObjects[$objects['cn']] = $objects;
85 }
86 }
87 }
90 /* Reload some attributes */
91 function get_object_attributes($object,$attributes)
92 {
93 $ldap = $this->config->get_ldap_link();
94 $ldap->cd($this->config->current['BASE']);
95 $ldap->cat($object['dn'],$attributes);
96 $tmp = $ldap->fetch();
98 foreach($attributes as $attrs){
99 if(isset($tmp[$attrs][0])){
100 $var = $tmp[$attrs][0];
102 /* Check if we must decode some attributes */
103 if(in_array_ics($attrs,$this->sub64coded)){
104 $var = base64_decode($var);
105 }
107 /* check if this is a binary entry */
108 if(in_array_ics($attrs,$this->subBinary)){
109 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
110 }
112 /* Fix slashes */
113 $var = addslashes($var);
114 $object[$attrs] = $var;
115 }
116 }
117 return($object);
118 }
121 function execute()
122 {
123 /* Call parent execute */
124 plugin::execute();
126 /* Fill templating stuff */
127 $smarty= get_smarty();
128 $display= "";
130 /* Add new sub object */
131 if(isset($_POST['AddSubObject'])){
132 $this->dialog= new $this->subClassName($this->config,"new");
133 $this->dialog->acl = $this->acl;
134 $this->is_dialog=true;
135 }
137 if($this->dn != "new"){
138 $_SESSION['objectinfo']= $this->dn;
139 }
142 /* New Listhandling
143 */
144 $once = true;
145 foreach($_POST as $name => $value){
146 if(preg_match("/^editscript_/",$name)&&($once)){
148 $once = false;
149 $entry = preg_replace("/^editscript_/","",$name);
150 $entry = base64_decode(preg_replace("/_.*/","",$entry));
152 $obj = $this->SubObjects[$entry];
153 if($obj['status'] == "FreshLoaded"){
154 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
155 }
156 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
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));
168 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
169 $this->SubObjects[$entry]['status']= "delete";
170 }else{
171 unset($this->SubObjects[$entry]);
172 }
173 }
174 }
175 ///// Ende new list handling
178 /* Edit selected Sub Object */
179 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
180 $script = $_POST['SubObject'][0];
181 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
182 $this->dialog->acl = $this->acl;
183 $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
184 $this->is_dialog=true;
185 }
188 /* Save Dialog */
189 if(isset($_POST['SaveSubObject'])){
190 $this->dialog->save_object();
191 $msgs = $this->dialog->check();
192 if(count($msgs)>0){
193 foreach($msgs as $msg){
194 print_red($msg);
195 }
196 }else{
197 /* Get return object */
198 $obj = $this->dialog->save();
199 if(isset($obj['remove'])){
201 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
203 /* Depending on status, set new status */
204 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
205 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
206 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
207 unset($this->SubObjects[$obj['remove']['from']]);
208 }
209 $obj['status'] = "new";
210 $this->SubObjects[$obj['remove']['to']] = $obj;
211 unset($this->SubObjects[$obj['remove']['to']]['remove']);
212 }else{
213 if($obj['status'] == "FreshLoaded"){
214 $obj['status'] = "edited";
215 }
216 $this->SubObjects[$obj['cn']]=$obj;
217 }
218 $this->is_dialog=false;
219 unset($this->dialog);
220 $this->dialog=NULL;
222 }
223 }
225 /* Sort entries */
226 $tmp = $keys = array();
227 foreach($this->SubObjects as $key => $entry){
228 $keys[$key]=$key;
229 }
230 natcasesort($keys);
231 foreach($keys as $key){
232 $tmp[$key]=$this->SubObjects[$key];
233 }
234 $this->SubObjects = $tmp;
236 /* Cancel Dialog */
237 if(isset($_POST['CancelSubObject'])){
238 $this->is_dialog=false;
239 unset($this->dialog);
240 $this->dialog=NULL;
241 }
243 /* Print dialog if $this->dialog is set */
244 if($this->dialog){
245 $this->dialog->save_object();
246 $display = $this->dialog->execute();
247 return($display);
248 }
250 /* Divlist added 23.02.2006
251 Containing FAIscripts
252 */
253 $divlist = new divSelectBox("FAIscripts");
254 $divlist->setHeight(400);
255 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
256 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
257 $img_remo = "";
258 }else{
259 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
260 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
261 }
263 foreach($this->getList(true) as $key => $name){
265 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
266 $down = "";
267 }else{
268 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
269 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
270 </a>";
271 }
273 $divlist->AddEntry(array( array("string"=>$name['name']),
274 array("string"=>$down , "attach" => "style='width:20px;'"),
275 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
276 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
277 }
278 $smarty->assign("Entry_divlist",$divlist->DrawList());
280 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
281 * If we post the escaped strings they will be escaped again
282 */
283 foreach($this->attributes as $attrs){
284 if(get_magic_quotes_gpc()){
285 $smarty->assign($attrs,stripslashes($this->$attrs));
286 }else{
287 $smarty->assign($attrs,($this->$attrs));
288 }
289 }
291 foreach($this->attributes as $attr){
292 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
293 }
295 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
296 return($display);
297 }
299 /* Generate listbox friendly SubObject list
300 */
301 function getList($use_dns=false){
302 $a_return=array();
303 foreach($this->SubObjects as $obj){
304 if($obj['status'] != "delete"){
305 if($use_dns){
306 if((isset($obj['description']))&&(!empty($obj['description']))){
307 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
308 }else{
309 $a_return[$obj['cn']]['name']= $obj['cn'];
310 }
311 $a_return[$obj['cn']]['dn']= $obj['dn'];
312 }else{
313 if((isset($obj['description']))&&(!empty($obj['description']))){
314 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
315 }else{
316 $a_return[$obj['cn']]= $obj['cn'];
317 }
318 }
319 }
320 }
321 return($a_return);
322 }
324 /* Delete me, and all my subtrees
325 */
326 function remove_from_parent()
327 {
328 $ldap = $this->config->get_ldap_link();
329 $ldap->cd ($this->dn);
331 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
332 if($_SESSION['faifilter']['branch'] == "main"){
333 $use_dn = $this->dn;
334 }
336 prepare_to_save_FAI_object($use_dn,array(),true);
338 foreach($this->SubObjects as $name => $obj){
339 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
340 if($_SESSION['faifilter']['branch'] == "main"){
341 $use_dn = $obj['dn'];
342 }
343 prepare_to_save_FAI_object($use_dn,array(),true);
344 }
345 $this->handle_post_events("remove");
346 }
349 /* Save data to object
350 */
351 function save_object()
352 {
353 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
354 plugin::save_object();
355 foreach($this->attributes as $attrs){
356 if(isset($_POST[$attrs])){
357 $this->$attrs = $_POST[$attrs];
358 }
359 }
360 }
361 }
364 /* Check supplied data */
365 function check()
366 {
367 /* Call common method to give check the hook */
368 $message= plugin::check();
370 return ($message);
371 }
374 /* Save to LDAP */
375 function save()
376 {
377 plugin::save();
379 $ldap = $this->config->get_ldap_link();
381 prepare_to_save_FAI_object($this->dn,$this->attrs);
382 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
384 /* Do object tagging */
385 $this->handle_object_tagging();
387 /* Prepare FAIscriptEntry to write it to ldap
388 * First sort array.
389 * Because we must delete old entries first.
390 * After deletion, we perform add and modify
391 */
392 $Objects = array();
394 /* We do not need to save untouched objects */
395 foreach($this->SubObjects as $name => $obj){
396 if($obj['status'] == "FreshLoaded"){
397 unset($this->SubObjects[$name]);
398 }
399 }
401 foreach($this->SubObjects as $name => $obj){
402 if($obj['status'] == "delete"){
403 $Objects[$name] = $obj;
404 }
405 }
406 foreach($this->SubObjects as $name => $obj){
407 if($obj['status'] != "delete"){
408 $Objects[$name] = $obj;
409 }
410 }
412 foreach($Objects as $name => $obj){
414 foreach($this->sub64coded as $codeIt){
415 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
416 }
418 $tmp = array();
419 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
420 foreach($attributes as $attrs){
422 if(empty($obj[$attrs])){
423 $obj[$attrs] = array();
424 }
425 if(!is_array($obj[$attrs])){
426 $tmp[$attrs] = stripslashes($obj[$attrs]);
427 }else{
428 $tmp[$attrs] = $obj[$attrs];
429 }
430 }
432 $tmp['objectClass'] = $this->subClasses;
434 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
436 if($obj['status']=="new"){
437 $ldap->cat($sub_dn,array("objectClass"));
438 if($ldap->count()){
439 $obj['status']="edited";
440 }
441 }
443 if(empty($tmp['FAIpriority'])){
444 $tmp['FAIpriority'] ="0";
445 }
447 /* Check if gosaAdministrativeUnitTag is required as object class */
448 if($obj['status'] == "edited"){
449 $ldap->cat($sub_dn,array("objectClass"));
450 $attrs = $ldap->fetch();
451 if(isset($attrs['objectClass'])){
452 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
453 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
454 }
455 }
456 }
458 if($obj['status'] == "delete"){
459 prepare_to_save_FAI_object($sub_dn,array(),true);
460 $this->handle_post_events("remove");
461 }elseif($obj['status'] == "edited"){
462 prepare_to_save_FAI_object($sub_dn,$tmp);
463 $this->handle_post_events("modify");
464 }elseif($obj['status']=="new"){
465 prepare_to_save_FAI_object($sub_dn,$tmp);
466 $this->handle_post_events("add");
467 }
469 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
470 }
471 }
474 /* Return plugin informations for acl handling */
475 function plInfo()
476 {
477 return (array(
478 "plShortName" => _("Script"),
479 "plDescription" => _("FAI script"),
480 "plSelfModify" => FALSE,
481 "plDepends" => array(),
482 "plPriority" => 0,
483 "plSection" => array("administration"),
484 "plCategory" => array("fai"),
485 "plProvidedAcls" => array(
486 "cn" => _("Name"),
487 "description" => _("Description"),
488 "FAIpriority" => _("Script priority"),
489 "FAIscript" => _("FAI script"))
490 ));
491 }
492 }
494 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
495 ?>