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","FAIscript");
28 var $sub64coded = array();
30 /* Specific attributes */
31 var $cn = ""; // The class name for this object
32 var $description = ""; // The description for this set of partitions
33 var $is_dialog = false; // specifies which buttons will be shown to save or abort
34 var $dialog = NULL; // a dialog, e.g. new disk dialog
35 var $SubObjects = array(); // All leafobjects of this object
37 var $FAIstate ="";
39 function faiScript ($config, $dn= NULL)
40 {
41 /* Load Attributes */
42 plugin::plugin ($config, $dn);
44 $this->acl ="#all#";
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 /* Set acls
53 */
54 $ui = get_userinfo();
55 $acl = get_permissions ($this->dn, $ui->subtreeACL);
56 $acli = get_module_permission($acl, "FAIclass", $this->dn);
57 $this->acl=$acli;
59 $this->dn =$dn;
61 /* Get FAIstate
62 */
63 if(isset($this->attrs['FAIstate'][0])){
64 $this->FAIstate = $this->attrs['FAIstate'][0];
65 }
67 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68 */
69 $ldap = $this->config->get_ldap_link();
70 $ldap->cd ($this->dn);
72 $attrs_to_search = $this->subAttributes;
73 $attrs_to_search[] = "FAIstate";
74 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
76 while($object = $ldap->fetch()){
78 /* Skip objects, that are tagged as removed */
79 if(isset($object['FAIstate'][0])){
80 if(preg_match("/removed$/",$object['FAIstate'][0])){
81 continue;
82 }
83 }
85 /* Set status for save management */
86 foreach($this->subAttributes as $attrs){
87 if(!isset($object[$attrs][0])){
88 $this->SubObjects[$object['cn'][0]][$attrs]="";
89 }else{
90 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
91 }
92 }
94 foreach($this->sub64coded as $codeIt){
95 $this->SubObjects[$object['cn'][0]][$codeIt]=(base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]));
96 }
98 foreach($this->subAttributes as $attrs){
99 $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
100 }
102 $this->SubObjects[$object['cn'][0]]['FAIscript'] = addslashes ($ldap->get_attribute($object['dn'], "FAIscript",$r_array=0));
104 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
105 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
106 }
107 }
108 }
110 function execute()
111 {
112 /* Call parent execute */
113 plugin::execute();
115 /* Fill templating stuff */
116 $smarty= get_smarty();
117 $display= "";
119 /* Add new sub object */
120 if(isset($_POST['AddSubObject'])){
121 $this->dialog= new $this->subClassName($this->config,"new");
122 $this->dialog->acl = $this->acl;
123 $this->is_dialog=true;
124 }
126 if($this->dn != "new"){
127 $_SESSION['objectinfo']= $this->dn;
128 }
131 /* New Listhandling
132 */
133 $once = true;
134 foreach($_POST as $name => $value){
135 if(preg_match("/^editscript_/",$name)&&($once)){
136 $once = false;
137 $entry = preg_replace("/^editscript_/","",$name);
138 $entry = base64_decode(preg_replace("/_.*/","",$entry));
139 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
140 $this->dialog->acl = $this->acl;
141 $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
142 $this->is_dialog=true;
143 }
144 if(preg_match("/^deletescript_/",$name)&&($once)){
145 $once = false;
146 $entry = preg_replace("/^deletescript_/","",$name);
147 $entry = base64_decode(preg_replace("/_.*/","",$entry));
148 if($this->SubObjects[$entry]['status'] == "edited"){
149 $this->SubObjects[$entry]['status']= "delete";
150 }else{
151 unset($this->SubObjects[$entry]);
152 }
153 }
154 }
155 ///// Ende new list handling
158 /* Edit selected Sub Object */
159 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
160 $script = $_POST['SubObject'][0];
161 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
162 $this->dialog->acl = $this->acl;
163 $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
164 $this->is_dialog=true;
165 }
167 /* Remove Sub object */
168 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
170 foreach($_POST['SubObject'] as $script){
171 if($this->SubObjects[$script]['status'] == "edited"){
172 $this->SubObjects[$script]['status']= "delete";
173 }else{
174 unset($this->SubObjects[$script]);
175 }
176 }
177 }
179 /* Save Dialog */
180 if(isset($_POST['SaveSubObject'])){
181 $this->dialog->save_object();
182 $msgs = $this->dialog->check();
183 if(count($msgs)>0){
184 foreach($msgs as $msg){
185 print_red($msg);
186 }
187 }else{
188 $obj = $this->dialog->save();
189 if(isset($obj['remove'])){
190 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
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 $this->SubObjects[$obj['cn']]=$obj;
200 }
201 $this->is_dialog=false;
202 unset($this->dialog);
203 $this->dialog=NULL;
204 }
205 }
207 /* Sort entries */
208 $tmp = $keys = array();
209 foreach($this->SubObjects as $key => $entry){
210 $keys[$key]=$key;
211 }
212 natcasesort($keys);
213 foreach($keys as $key){
214 $tmp[$key]=$this->SubObjects[$key];
215 }
216 $this->SubObjects = $tmp;
218 /* Cancel Dialog */
219 if(isset($_POST['CancelSubObject'])){
220 $this->is_dialog=false;
221 unset($this->dialog);
222 $this->dialog=NULL;
223 }
225 /* Print dialog if $this->dialog is set */
226 if($this->dialog){
227 $this->dialog->save_object();
228 $display = $this->dialog->execute();
229 return($display);
230 }
232 /* Divlist added 23.02.2006
233 Containing FAIscripts
234 */
235 $divlist = new divSelectBox("FAIscripts");
236 $divlist->setHeight(400);
237 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
238 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
239 $img_remo = "";
240 }else{
241 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
242 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
243 }
245 foreach($this->getList(true) as $key => $name){
247 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
248 $down = "";
249 }else{
250 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
251 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
252 </a>";
253 }
255 $divlist->AddEntry(array( array("string"=>$name['name']),
256 array("string"=>$down , "attach" => "style='width:20px;'"),
257 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
258 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
259 }
260 $smarty->assign("Entry_divlist",$divlist->DrawList());
262 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
263 * If we post the escaped strings they will be escaped again
264 */
265 foreach($this->attributes as $attrs){
266 if(get_magic_quotes_gpc()){
267 $smarty->assign($attrs,stripslashes($this->$attrs));
268 }else{
269 $smarty->assign($attrs,($this->$attrs));
270 }
271 }
273 foreach($this->attributes as $attr){
274 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
275 }
277 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
278 return($display);
279 }
281 /* Generate listbox friendly SubObject list
282 */
283 function getList($use_dns=false){
284 $a_return=array();
285 foreach($this->SubObjects as $obj){
286 if($obj['status'] != "delete"){
287 if($use_dns){
288 if((isset($obj['description']))&&(!empty($obj['description']))){
289 $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
290 }else{
291 $a_return[$obj['cn']]['name']= $obj['cn'];
292 }
293 $a_return[$obj['cn']]['dn']= $obj['dn'];
294 }else{
295 if((isset($obj['description']))&&(!empty($obj['description']))){
296 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
297 }else{
298 $a_return[$obj['cn']]= $obj['cn'];
299 }
300 }
301 }
302 }
303 return($a_return);
304 }
306 /* Delete me, and all my subtrees
307 */
308 function remove_from_parent()
309 {
310 $ldap = $this->config->get_ldap_link();
311 $ldap->cd ($this->dn);
313 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
314 if($_SESSION['faifilter']['branch'] == "main"){
315 $use_dn = $this->dn;
316 }
318 prepare_to_save_FAI_object($use_dn,array(),true);
320 foreach($this->SubObjects as $name => $obj){
321 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
322 if($_SESSION['faifilter']['branch'] == "main"){
323 $use_dn = $obj['dn'];
324 }
325 prepare_to_save_FAI_object($use_dn,array(),true);
326 }
327 $this->handle_post_events("remove");
328 }
331 /* Save data to object
332 */
333 function save_object()
334 {
335 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
336 plugin::save_object();
337 foreach($this->attributes as $attrs){
338 if(isset($_POST[$attrs])){
339 $this->$attrs = $_POST[$attrs];
340 }
341 }
342 }
343 }
346 /* Check supplied data */
347 function check()
348 {
349 /* Call common method to give check the hook */
350 $message= plugin::check();
352 return ($message);
353 }
356 /* Save to LDAP */
357 function save()
358 {
359 plugin::save();
361 $ldap = $this->config->get_ldap_link();
363 prepare_to_save_FAI_object($this->dn,$this->attrs);
364 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
366 /* Do object tagging */
367 $this->handle_object_tagging();
369 /* Prepare FAIscriptEntry to write it to ldap
370 * First sort array.
371 * Because we must delete old entries first.
372 * After deletion, we perform add and modify
373 */
374 $Objects = array();
375 foreach($this->SubObjects as $name => $obj){
376 if($obj['status'] == "delete"){
377 $Objects[$name] = $obj;
378 }
379 }
380 foreach($this->SubObjects as $name => $obj){
381 if($obj['status'] != "delete"){
382 $Objects[$name] = $obj;
383 }
384 }
386 foreach($Objects as $name => $obj){
388 foreach($this->sub64coded as $codeIt){
389 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
390 }
392 $tmp = array();
393 foreach($this->subAttributes as $attrs){
394 if(empty($obj[$attrs])){
395 $obj[$attrs] = array();
396 }
397 if(!is_array($obj[$attrs])){
398 $tmp[$attrs] = stripslashes($obj[$attrs]);
399 }else{
400 $tmp[$attrs] = $obj[$attrs];
401 }
402 }
404 $tmp['objectClass'] = $this->subClasses;
406 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
408 if($obj['status']=="new"){
409 $ldap->cat($sub_dn,array("objectClass"));
410 if($ldap->count()){
411 $obj['status']="edited";
412 }
413 }
415 if(empty($tmp['FAIpriority'])){
416 $tmp['FAIpriority'] ="0";
417 }
419 /* Check if gosaAdministrativeUnitTag is required as object class */
420 if($obj['status'] == "edited"){
421 $ldap->cat($sub_dn,array("objectClass"));
422 $attrs = $ldap->fetch();
423 if(isset($attrs['objectClass'])){
424 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
425 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
426 }
427 }
428 }
430 if($obj['status'] == "delete"){
431 prepare_to_save_FAI_object($sub_dn,array(),true);
432 $this->handle_post_events("remove");
433 }elseif($obj['status'] == "edited"){
434 prepare_to_save_FAI_object($sub_dn,$tmp);
435 $this->handle_post_events("modify");
436 }elseif($obj['status']=="new"){
437 prepare_to_save_FAI_object($sub_dn,$tmp);
438 $this->handle_post_events("add");
439 }
441 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
442 }
443 }
446 /* Return plugin informations for acl handling */
447 function plInfo()
448 {
449 return (array(
450 "plShortName" => _("Script"),
451 "plDescription" => _("FAI script"),
452 "plSelfModify" => FALSE,
453 "plDepends" => array(),
454 "plPriority" => 0,
455 "plSection" => array("administration"),
456 "plCategory" => array("fai"),
457 "plProvidedAcls" => array(
458 "cn" => _("Name"),
459 "description" => _("Description"),
460 "FAIpriority" => _("Script priority"),
461 "FAIscript" => _("FAI script"))
462 ));
463 }
464 }
466 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
467 ?>