ee8c3c5c143e4157edee795e4d1be61599353aa1
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);
73 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
75 while($object = $ldap->fetch()){
76 /* Set status for save management */
77 $objects = array();
78 $objects['status'] = "FreshLoaded";
79 $objects['dn'] = $object['dn'];
80 $objects = $this->get_object_attributes($objects,$this->subAttributes);
81 $this->SubObjects[$objects['cn']] = $objects;
83 }
84 }
85 }
88 /* Reload some attributes */
89 function get_object_attributes($object,$attributes)
90 {
91 $ldap = $this->config->get_ldap_link();
92 $ldap->cd($this->config->current['BASE']);
93 $ldap->cat($object['dn'],$attributes);
94 $tmp = $ldap->fetch();
96 foreach($attributes as $attrs){
97 if(isset($tmp[$attrs][0])){
98 $var = $tmp[$attrs][0];
100 /* Check if we must decode some attributes */
101 if(in_array_ics($attrs,$this->sub64coded)){
102 $var = base64_decode($var);
103 }
105 /* check if this is a binary entry */
106 if(in_array_ics($attrs,$this->subBinary)){
107 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
108 }
110 /* Fix slashes */
111 $var = addslashes($var);
112 $object[$attrs] = $var;
113 }
114 }
115 return($object);
116 }
119 function execute()
120 {
121 /* Call parent execute */
122 plugin::execute();
124 /* Fill templating stuff */
125 $smarty= get_smarty();
126 $display= "";
128 /* Add new sub object */
129 if(isset($_POST['AddSubObject'])){
130 $this->dialog= new $this->subClassName($this->config,"new");
131 $this->dialog->acl = $this->acl;
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)){
145 $once = false;
146 $entry = preg_replace("/^editscript_/","",$name);
147 $entry = base64_decode(preg_replace("/_.*/","",$entry));
149 $obj = $this->SubObjects[$entry];
150 if($obj['status'] == "FreshLoaded"){
151 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
152 }
154 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
155 $this->dialog->acl = $this->acl;
156 $_SESSION['objectinfo'] = $obj['dn'];
157 $this->dialog->parent = &$this;
158 $this->is_dialog=true;
160 }
161 if(preg_match("/^deletescript_/",$name)&&($once)){
162 $once = false;
163 $entry = preg_replace("/^deletescript_/","",$name);
164 $entry = base64_decode(preg_replace("/_.*/","",$entry));
166 $status = $this->SubObjects[$entry]['status'];
167 if($status == "edited" || $status == "FreshLoaded"){
168 $this->SubObjects[$entry]['status']= "delete";
169 }else{
170 unset($this->SubObjects[$entry]);
171 }
172 }
173 }
174 ///// Ende new list handling
175 if(isset($_POST['SaveSubObject'])){
176 $this->dialog->save_object();
177 $msgs = $this->dialog->check();
178 if(count($msgs)>0){
179 foreach($msgs as $msg){
180 print_red($msg);
181 }
182 }else{
184 /* Get return object */
185 $obj = $this->dialog->save();
186 if(isset($obj['remove'])){
187 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
189 /* Depending on status, set new status */
190 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
191 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
192 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
193 unset($this->SubObjects[$obj['remove']['from']]);
194 }
195 $obj['status'] = "new";
196 $this->SubObjects[$obj['remove']['to']] = $obj;
197 unset($this->SubObjects[$obj['remove']['to']]['remove']);
198 }else{
199 if($obj['status'] == "FreshLoaded"){
200 $obj['status'] = "edited";
201 }
202 $this->SubObjects[$obj['cn']]=$obj;
203 }
205 $this->is_dialog=false;
206 unset($this->dialog);
207 $this->dialog=NULL;
208 }
209 }
211 /* Sort entries */
212 $tmp = $keys = array();
213 foreach($this->SubObjects as $key => $entry){
214 $keys[$key]=$key;
215 }
216 natcasesort($keys);
217 foreach($keys as $key){
218 $tmp[$key]=$this->SubObjects[$key];
219 }
220 $this->SubObjects = $tmp;
222 /* Cancel Dialog */
223 if(isset($_POST['CancelSubObject'])){
224 $this->is_dialog=false;
225 unset($this->dialog);
226 $this->dialog=NULL;
227 }
229 /* Print dialog if $this->dialog is set */
230 if($this->dialog){
231 $this->dialog->save_object();
232 $display = $this->dialog->execute();
233 return($display);
234 }
236 /* Divlist added 23.02.2006
237 Containing FAIscripts
238 */
239 $divlist = new divSelectBox("FAIscripts");
240 $divlist->setHeight(400);
241 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
242 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
243 $img_remo = "";
244 }else{
245 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
246 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
247 }
249 foreach($this->getList(true) as $key => $name){
251 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
252 $down = "";
253 }else{
254 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
255 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
256 </a>";
257 }
259 $divlist->AddEntry(array( array("string"=>$name['name']),
260 array("string"=>$down , "attach" => "style='width:20px;'"),
261 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
262 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
263 }
264 $smarty->assign("Entry_divlist",$divlist->DrawList());
266 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
267 * If we post the escaped strings they will be escaped again
268 */
269 foreach($this->attributes as $attrs){
270 if(get_magic_quotes_gpc()){
271 $smarty->assign($attrs,stripslashes($this->$attrs));
272 }else{
273 $smarty->assign($attrs,($this->$attrs));
274 }
275 }
277 foreach($this->attributes as $attr){
278 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
279 }
281 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
282 return($display);
283 }
285 /* Generate listbox friendly SubObject list
286 */
287 function getList($use_dns=false){
288 $a_return=array();
289 foreach($this->SubObjects as $obj){
290 if($obj['status'] != "delete"){
291 if($use_dns){
292 if((isset($obj['description']))&&(!empty($obj['description']))){
293 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
294 }else{
295 $a_return[$obj['cn']]['name']= $obj['cn'];
296 }
297 $a_return[$obj['cn']]['dn']= $obj['dn'];
298 }else{
299 if((isset($obj['description']))&&(!empty($obj['description']))){
300 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
301 }else{
302 $a_return[$obj['cn']]= $obj['cn'];
303 }
304 }
305 }
306 }
307 return($a_return);
308 }
310 /* Delete me, and all my subtrees
311 */
312 function remove_from_parent()
313 {
314 $ldap = $this->config->get_ldap_link();
315 $ldap->cd ($this->dn);
316 $ldap->rmdir_recursive($this->dn);
317 show_ldap_error($ldap->get_error(), _("Removing FAI script base failed"));
318 $this->handle_post_events("remove");
319 }
322 /* Save data to object
323 */
324 function save_object()
325 {
326 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
327 plugin::save_object();
328 foreach($this->attributes as $attrs){
329 if(isset($_POST[$attrs])){
330 $this->$attrs = $_POST[$attrs];
331 }
332 }
333 }
334 }
337 /* Check supplied data */
338 function check()
339 {
340 /* Call common method to give check the hook */
341 $message= plugin::check();
343 return ($message);
344 }
347 /* Save to LDAP */
348 function save()
349 {
350 plugin::save();
352 $ldap = $this->config->get_ldap_link();
354 $ldap->cat($this->dn,array("objectClass"));
355 if($ldap->count()!=0){
356 /* Write FAIscript to ldap*/
357 $ldap->cd($this->dn);
358 $this->cleanup();
359 $ldap->modify ($this->attrs);
361 }else{
362 /* Write FAIscript to ldap*/
363 $ldap->cd($this->config->current['BASE']);
364 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
365 $ldap->cd($this->dn);
366 $ldap->add($this->attrs);
367 }
368 show_ldap_error($ldap->get_error(), _("Creating FAI script base failed"));
370 /* Do object tagging */
371 $this->handle_object_tagging();
373 /* Prepare FAIscriptEntry to write it to ldap
374 * First sort array.
375 * Because we must delete old entries first.
376 * After deletion, we perform add and modify
377 */
378 $Objects = array();
380 /* We do not need to save untouched objects */
381 foreach($this->SubObjects as $name => $obj){
382 if($obj['status'] == "FreshLoaded"){
383 unset($this->SubObjects[$name]);
384 }
385 }
387 foreach($this->SubObjects as $name => $obj){
388 if($obj['status'] == "delete"){
389 $Objects[$name] = $obj;
390 }
391 }
392 foreach($this->SubObjects as $name => $obj){
393 if($obj['status'] != "delete"){
394 $Objects[$name] = $obj;
395 }
396 }
398 foreach($Objects as $name => $obj){
400 foreach($this->sub64coded as $codeIt){
401 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
402 }
404 $tmp = array();
405 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
406 foreach($attributes as $attrs){
407 if(empty($obj[$attrs])){
408 $obj[$attrs] = array();
409 }
410 if(!is_array($obj[$attrs])){
411 $tmp[$attrs] = stripslashes($obj[$attrs]);
412 }else{
413 $tmp[$attrs] = $obj[$attrs];
414 }
415 }
417 $tmp['objectClass'] = $this->subClasses;
419 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
421 if($obj['status']=="new"){
422 $ldap->cat($sub_dn,array("objectClass"));
423 if($ldap->count()){
424 $obj['status']="edited";
425 }
426 }
428 if(empty($tmp['FAIpriority'])){
429 $tmp['FAIpriority'] ="0";
430 }
432 /* Check if gosaAdministrativeUnitTag is required as object class */
433 if($obj['status'] == "edited"){
434 $ldap->cat($sub_dn,array("objectClass"));
435 $attrs = $ldap->fetch();
436 if(isset($attrs['objectClass'])){
437 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
438 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
439 }
440 }
441 }
443 if($obj['status'] == "delete"){
444 $ldap->cd($sub_dn);
445 $ldap->rmdir_recursive($sub_dn);
446 $this->handle_post_events("remove");
447 show_ldap_error($ldap->get_error(), _("Removing FAI script failed"));
448 }elseif($obj['status'] == "edited"){
449 $ldap->cd($sub_dn);
450 $this->cleanup();
451 $ldap->modify ($tmp);
453 $this->handle_post_events("modify");
454 }elseif($obj['status']=="new"){
455 if($tmp['description']==array()){
456 unset($tmp['description']);
457 }
458 if($tmp['FAIscript']==array()){
459 $tmp['FAIscript']=" ";
460 }
461 $ldap->cd($this->config->current['BASE']);
462 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
463 $ldap->cd($sub_dn);
464 $ldap->add($tmp);
465 $this->handle_post_events("add");
466 show_ldap_error($ldap->get_error(), _("Saving FAI script failed"));
467 }
469 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
470 }
471 }
472 }
474 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
475 ?>