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 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
333 if($_SESSION['faifilter']['branch'] == "main"){
334 $use_dn = $this->dn;
335 }
337 prepare_to_save_FAI_object($use_dn,array(),true);
339 foreach($this->SubObjects as $name => $obj){
340 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
341 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
342 if($_SESSION['faifilter']['branch'] == "main"){
343 $use_dn = $obj['dn'];
344 }
345 prepare_to_save_FAI_object($use_dn,array(),true);
346 }
347 $this->handle_post_events("remove");
348 }
351 /* Save data to object
352 */
353 function save_object()
354 {
355 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
356 plugin::save_object();
357 foreach($this->attributes as $attrs){
358 if(isset($_POST[$attrs])){
359 $this->$attrs = $_POST[$attrs];
360 }
361 }
362 }
363 }
366 /* Check supplied data */
367 function check()
368 {
369 /* Call common method to give check the hook */
370 $message= plugin::check();
372 return ($message);
373 }
376 /* Save to LDAP */
377 function save()
378 {
379 plugin::save();
381 $ldap = $this->config->get_ldap_link();
383 prepare_to_save_FAI_object($this->dn,$this->attrs);
384 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
386 /* Do object tagging */
387 $this->handle_object_tagging();
389 /* Prepare FAIscriptEntry to write it to ldap
390 * First sort array.
391 * Because we must delete old entries first.
392 * After deletion, we perform add and modify
393 */
394 $Objects = array();
396 /* We do not need to save untouched objects */
397 foreach($this->SubObjects as $name => $obj){
398 if($obj['status'] == "FreshLoaded"){
399 unset($this->SubObjects[$name]);
400 }
401 }
403 foreach($this->SubObjects as $name => $obj){
404 if($obj['status'] == "delete"){
405 $Objects[$name] = $obj;
406 }
407 }
408 foreach($this->SubObjects as $name => $obj){
409 if($obj['status'] != "delete"){
410 $Objects[$name] = $obj;
411 }
412 }
414 foreach($Objects as $name => $obj){
416 foreach($this->sub64coded as $codeIt){
417 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
418 }
420 $tmp = array();
421 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
422 foreach($attributes as $attrs){
424 if(empty($obj[$attrs])){
425 $obj[$attrs] = array();
426 }
427 if(!is_array($obj[$attrs])){
428 $tmp[$attrs] = stripslashes($obj[$attrs]);
429 }else{
430 $tmp[$attrs] = $obj[$attrs];
431 }
432 }
434 $tmp['objectClass'] = $this->subClasses;
436 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
438 if($obj['status']=="new"){
439 $ldap->cat($sub_dn,array("objectClass"));
440 if($ldap->count()){
441 $obj['status']="edited";
442 }
443 }
445 if(empty($tmp['FAIpriority'])){
446 $tmp['FAIpriority'] ="0";
447 }
449 /* Check if gosaAdministrativeUnitTag is required as object class */
450 if($obj['status'] == "edited"){
451 $ldap->cat($sub_dn,array("objectClass"));
452 $attrs = $ldap->fetch();
453 if(isset($attrs['objectClass'])){
454 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
455 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
456 }
457 }
458 }
460 if($obj['status'] == "delete"){
461 prepare_to_save_FAI_object($sub_dn,array(),true);
462 $this->handle_post_events("remove");
463 }elseif($obj['status'] == "edited"){
464 prepare_to_save_FAI_object($sub_dn,$tmp);
465 $this->handle_post_events("modify");
466 }elseif($obj['status']=="new"){
467 prepare_to_save_FAI_object($sub_dn,$tmp);
468 $this->handle_post_events("add");
469 }
471 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
472 }
473 }
476 /* Return plugin informations for acl handling */
477 function plInfo()
478 {
479 return (array(
480 "plShortName" => _("Script"),
481 "plDescription" => _("FAI script"),
482 "plSelfModify" => FALSE,
483 "plDepends" => array(),
484 "plPriority" => 0,
485 "plSection" => array("administration"),
486 "plCategory" => array("fai"),
487 "plProvidedAcls" => array(
488 "cn" => _("Name"),
489 "description" => _("Description"),
490 "FAIpriority" => _("Script priority"),
491 "FAIscript" => _("FAI script"))
492 ));
493 }
494 }
496 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
497 ?>