e70a404564d2f63b0c524e9b89ea5f26a9b6f7ec
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 $base ="";
42 var $release ="";
43 var $copy_paste_mode = false;
44 var $CopyPasteVars = array("SubObjects","FAIstate");
47 function faiScript ($config, $dn= NULL)
48 {
49 /* Load Attributes */
50 plugin::plugin ($config, $dn);
52 $this->acl ="#all#";
54 /* If "dn==new" we try to create a new entry
55 * Else we must read all objects from ldap which belong to this entry.
56 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
57 */
58 if($dn != "new"){
60 /* Set acls
61 */
62 $ui = get_userinfo();
63 $acl = get_permissions ($this->dn, $ui->subtreeACL);
64 $acli = get_module_permission($acl, "FAIclass", $this->dn);
65 $this->acl=$acli;
67 $this->dn =$dn;
69 /* Get FAIstate
70 */
71 if(isset($this->attrs['FAIstate'][0])){
72 $this->FAIstate = $this->attrs['FAIstate'][0];
73 }
75 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
76 */
77 $ldap = $this->config->get_ldap_link();
78 $ldap->cd ($this->dn);
79 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
81 while($object = $ldap->fetch()){
82 /* Set status for save management */
83 $objects = array();
84 $objects['status'] = "FreshLoaded";
85 $objects['dn'] = $object['dn'];
86 $objects = $this->get_object_attributes($objects,$this->subAttributes);
87 $this->SubObjects[$objects['cn']] = $objects;
89 }
90 }
91 }
94 /* Reload some attributes */
95 function get_object_attributes($object,$attributes)
96 {
97 $ldap = $this->config->get_ldap_link();
98 $ldap->cd($this->config->current['BASE']);
99 $ldap->cat($object['dn'],$attributes);
100 $tmp = $ldap->fetch();
102 foreach($attributes as $attrs){
103 if(isset($tmp[$attrs][0])){
104 $var = $tmp[$attrs][0];
106 /* Check if we must decode some attributes */
107 if(in_array_ics($attrs,$this->sub64coded)){
108 $var = base64_decode($var);
109 }
111 /* check if this is a binary entry */
112 if(in_array_ics($attrs,$this->subBinary)){
113 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
114 }
116 /* Fix slashes */
117 $var = addslashes($var);
118 $object[$attrs] = $var;
119 }
120 }
121 return($object);
122 }
125 function execute()
126 {
127 /* Call parent execute */
128 plugin::execute();
130 /* Fill templating stuff */
131 $smarty= get_smarty();
132 $display= "";
134 /* Add new sub object */
135 if(isset($_POST['AddSubObject'])){
136 $this->dialog= new $this->subClassName($this->config,"new");
137 $this->dialog->acl = $this->acl;
138 $this->is_dialog=true;
139 }
141 if($this->dn != "new"){
142 $_SESSION['objectinfo']= $this->dn;
143 }
146 /* New Listhandling
147 */
148 $once = true;
149 foreach($_POST as $name => $value){
150 if(preg_match("/^editscript_/",$name)&&($once)){
151 $once = false;
152 $entry = preg_replace("/^editscript_/","",$name);
153 $entry = base64_decode(preg_replace("/_.*/","",$entry));
155 $obj = $this->SubObjects[$entry];
156 if($obj['status'] == "FreshLoaded"){
157 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
158 }
160 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
161 $this->dialog->acl = $this->acl;
162 $_SESSION['objectinfo'] = $obj['dn'];
163 $this->dialog->parent = &$this;
164 $this->is_dialog=true;
166 }
167 if(preg_match("/^deletescript_/",$name)&&($once)){
168 $once = false;
169 $entry = preg_replace("/^deletescript_/","",$name);
170 $entry = base64_decode(preg_replace("/_.*/","",$entry));
172 $status = $this->SubObjects[$entry]['status'];
173 if($status == "edited" || $status == "FreshLoaded"){
174 $this->SubObjects[$entry]['status']= "delete";
175 }else{
176 unset($this->SubObjects[$entry]);
177 }
178 }
179 }
180 ///// Ende new list handling
181 if(isset($_POST['SaveSubObject'])){
182 $this->dialog->save_object();
183 $msgs = $this->dialog->check();
184 if(count($msgs)>0){
185 foreach($msgs as $msg){
186 print_red($msg);
187 }
188 }else{
190 /* Get return object */
191 $obj = $this->dialog->save();
192 if(isset($obj['remove'])){
193 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
195 /* Depending on status, set new status */
196 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
197 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
198 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
199 unset($this->SubObjects[$obj['remove']['from']]);
200 }
201 $obj['status'] = "new";
202 $this->SubObjects[$obj['remove']['to']] = $obj;
203 unset($this->SubObjects[$obj['remove']['to']]['remove']);
204 }else{
205 if($obj['status'] == "FreshLoaded"){
206 $obj['status'] = "edited";
207 }
208 $this->SubObjects[$obj['cn']]=$obj;
209 }
211 $this->is_dialog=false;
212 unset($this->dialog);
213 $this->dialog=NULL;
214 }
215 }
217 /* Sort entries */
218 $tmp = $keys = array();
219 foreach($this->SubObjects as $key => $entry){
220 $keys[$key]=$key;
221 }
222 natcasesort($keys);
223 foreach($keys as $key){
224 $tmp[$key]=$this->SubObjects[$key];
225 }
226 $this->SubObjects = $tmp;
228 /* Cancel Dialog */
229 if(isset($_POST['CancelSubObject'])){
230 $this->is_dialog=false;
231 unset($this->dialog);
232 $this->dialog=NULL;
233 }
235 /* Print dialog if $this->dialog is set */
236 if($this->dialog){
237 $this->dialog->save_object();
238 $display = $this->dialog->execute();
239 return($display);
240 }
242 /* Divlist added 23.02.2006
243 Containing FAIscripts
244 */
245 $divlist = new divSelectBox("FAIscripts");
246 $divlist->setHeight(400);
247 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
248 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
249 $img_remo = "";
250 }else{
251 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
252 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
253 }
255 foreach($this->getList(true) as $key => $name){
257 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
258 $down = "";
259 }else{
260 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
261 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
262 </a>";
263 }
265 $divlist->AddEntry(array( array("string"=>$name['name']),
266 array("string"=>$down , "attach" => "style='width:20px;'"),
267 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
268 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
269 }
270 $smarty->assign("Entry_divlist",$divlist->DrawList());
272 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
273 * If we post the escaped strings they will be escaped again
274 */
275 foreach($this->attributes as $attrs){
276 if(get_magic_quotes_gpc()){
277 $smarty->assign($attrs,stripslashes($this->$attrs));
278 }else{
279 $smarty->assign($attrs,($this->$attrs));
280 }
281 }
283 foreach($this->attributes as $attr){
284 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
285 }
287 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
288 return($display);
289 }
291 /* Generate listbox friendly SubObject list
292 */
293 function getList($use_dns=false){
294 $a_return=array();
295 foreach($this->SubObjects as $obj){
296 if($obj['status'] != "delete"){
297 if($use_dns){
298 if((isset($obj['description']))&&(!empty($obj['description']))){
299 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
300 }else{
301 $a_return[$obj['cn']]['name']= $obj['cn'];
302 }
303 $a_return[$obj['cn']]['dn']= $obj['dn'];
304 }else{
305 if((isset($obj['description']))&&(!empty($obj['description']))){
306 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
307 }else{
308 $a_return[$obj['cn']]= $obj['cn'];
309 }
310 }
311 }
312 }
313 return($a_return);
314 }
316 /* Delete me, and all my subtrees
317 */
318 function remove_from_parent()
319 {
320 $ldap = $this->config->get_ldap_link();
321 $ldap->cd ($this->dn);
322 $ldap->rmdir_recursive($this->dn);
323 show_ldap_error($ldap->get_error(), _("Removing FAI script base failed"));
324 $this->handle_post_events("remove");
325 }
328 /* Save data to object
329 */
330 function save_object()
331 {
332 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
333 plugin::save_object();
334 foreach($this->attributes as $attrs){
335 if(isset($_POST[$attrs])){
336 $this->$attrs = $_POST[$attrs];
337 }
338 }
339 }
340 }
343 /* Check supplied data */
344 function check()
345 {
346 /* Call common method to give check the hook */
347 $message= plugin::check();
349 /* If this is a new script, check if a script with this name already exists */
350 if(!empty($this->release) && $this->copy_paste_mode){
352 /* Check if current name is already used for fai scripts in selected release */
353 $dn = 'cn='.$this->cn.",ou=scripts,".$this->release;
354 $ldap = $this->config->get_ldap_link();
355 $ldap->cat($dn);
356 if($ldap->count()){
358 $r =convert_department_dn($this->release);;
359 $message[] = sprintf(_("Can't insert a script named '%s' in '%s' there is already a script with the given name."),$this->cn,$r);
360 }
361 }
362 return ($message);
363 }
366 /* Save to LDAP */
367 function save()
368 {
369 plugin::save();
371 $ldap = $this->config->get_ldap_link();
373 /* Copy & Paste : Ensure that FAIstate is copied too */
374 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
375 $this->attrs['FAIstate'] = $this->FAIstate;
376 }
378 $ldap->cat($this->dn,array("objectClass"));
379 if($ldap->count()!=0){
380 /* Write FAIscript to ldap*/
381 $ldap->cd($this->dn);
382 $this->cleanup();
383 $ldap->modify ($this->attrs);
385 }else{
386 /* Write FAIscript to ldap*/
387 $ldap->cd($this->config->current['BASE']);
388 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
389 $ldap->cd($this->dn);
390 $ldap->add($this->attrs);
391 }
392 show_ldap_error($ldap->get_error(), _("Creating FAI script base failed"));
394 /* Do object tagging */
395 $this->handle_object_tagging();
397 /* Prepare FAIscriptEntry to write it to ldap
398 * First sort array.
399 * Because we must delete old entries first.
400 * After deletion, we perform add and modify
401 */
402 $Objects = array();
404 /* We do not need to save untouched objects */
405 foreach($this->SubObjects as $name => $obj){
406 if($obj['status'] == "FreshLoaded"){
407 if($this->copy_paste_mode){
408 $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
409 $this->SubObjects[$name]['status'] = "new";
410 }else{
411 unset($this->SubObjects[$name]);
412 }
413 }
414 }
416 foreach($this->SubObjects as $name => $obj){
417 if($obj['status'] == "delete"){
418 $Objects[$name] = $obj;
419 }
420 }
421 foreach($this->SubObjects as $name => $obj){
422 if($obj['status'] != "delete"){
423 $Objects[$name] = $obj;
424 }
425 }
427 foreach($Objects as $name => $obj){
429 foreach($this->sub64coded as $codeIt){
430 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
431 }
433 $tmp = array();
434 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
435 foreach($attributes as $attrs){
436 if(empty($obj[$attrs])){
437 $obj[$attrs] = array();
438 }
439 if(!is_array($obj[$attrs])){
440 $tmp[$attrs] = stripslashes($obj[$attrs]);
441 }else{
442 $tmp[$attrs] = $obj[$attrs];
443 }
444 }
446 $tmp['objectClass'] = $this->subClasses;
448 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
450 if($obj['status']=="new"){
451 $ldap->cat($sub_dn,array("objectClass"));
452 if($ldap->count()){
453 $obj['status']="edited";
454 }
455 }
457 if(empty($tmp['FAIpriority'])){
458 $tmp['FAIpriority'] ="0";
459 }
461 /* Check if gosaAdministrativeUnitTag is required as object class */
462 if($obj['status'] == "edited"){
463 $ldap->cat($sub_dn,array("objectClass"));
464 $attrs = $ldap->fetch();
465 if(isset($attrs['objectClass'])){
466 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
467 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
468 }
469 }
470 }
472 if($obj['status'] == "delete"){
473 $ldap->cd($sub_dn);
474 $ldap->rmdir_recursive($sub_dn);
475 $this->handle_post_events("remove");
476 show_ldap_error($ldap->get_error(), _("Removing FAI script failed"));
477 }elseif($obj['status'] == "edited"){
478 $ldap->cd($sub_dn);
479 $this->cleanup();
480 $ldap->modify ($tmp);
482 $this->handle_post_events("modify");
483 }elseif($obj['status']=="new"){
484 if($tmp['description']==array()){
485 unset($tmp['description']);
486 }
487 if($tmp['FAIscript']==array()){
488 $tmp['FAIscript']=" ";
489 }
490 $ldap->cd($this->config->current['BASE']);
491 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
492 $ldap->cd($sub_dn);
493 $ldap->add($tmp);
494 $this->handle_post_events("add");
495 show_ldap_error($ldap->get_error(), _("Saving FAI script failed"));
496 }
498 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
499 }
500 }
503 /* return copy & paste dialog
504 */
505 function getCopyDialog()
506 {
507 /* Ask for cn */
508 $smarty = get_smarty();
509 $smarty->assign("cn" ,$this->cn);
510 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
511 $ret = array();
512 $ret['string'] = $str;
513 $ret['status'] = "";
514 return($ret);
515 }
517 /* Get posted cn */
518 function saveCopyDialog()
519 {
520 if(isset($_POST['cn'])){
521 $this->cn = $_POST['cn'];
522 }
523 }
526 }
528 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
529 ?>