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);
71 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
73 while($object = $ldap->fetch()){
74 /* Set status for save management */
76 foreach($this->subAttributes as $attrs){
77 if(!isset($object[$attrs][0])){
78 $this->SubObjects[$object['cn'][0]][$attrs]="";
79 }else{
80 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
81 }
82 }
84 foreach($this->sub64coded as $codeIt){
85 $this->SubObjects[$object['cn'][0]][$codeIt]=(base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]));
86 }
88 foreach($this->subAttributes as $attrs){
89 $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
90 }
92 $this->SubObjects[$object['cn'][0]]['FAIscript'] = addslashes ($ldap->get_attribute($object['dn'], "FAIscript",$r_array=0));
94 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
95 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
96 }
97 }
98 }
100 function execute()
101 {
102 /* Call parent execute */
103 plugin::execute();
105 /* Fill templating stuff */
106 $smarty= get_smarty();
107 $display= "";
109 /* Add new sub object */
110 if(isset($_POST['AddSubObject'])){
111 $this->dialog= new $this->subClassName($this->config,"new");
112 $this->dialog->acl = $this->acl;
113 $this->is_dialog=true;
114 }
116 if($this->dn != "new"){
117 $_SESSION['objectinfo']= $this->dn;
118 }
121 /* New Listhandling
122 */
123 $once = true;
124 foreach($_POST as $name => $value){
125 if(preg_match("/^editscript_/",$name)&&($once)){
126 $once = false;
127 $entry = preg_replace("/^editscript_/","",$name);
128 $entry = base64_decode(preg_replace("/_.*/","",$entry));
129 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
130 $this->dialog->acl = $this->acl;
131 $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
132 $this->is_dialog=true;
133 }
134 if(preg_match("/^deletescript_/",$name)&&($once)){
135 $once = false;
136 $entry = preg_replace("/^deletescript_/","",$name);
137 $entry = base64_decode(preg_replace("/_.*/","",$entry));
138 if($this->SubObjects[$entry]['status'] == "edited"){
139 $this->SubObjects[$entry]['status']= "delete";
140 }else{
141 unset($this->SubObjects[$entry]);
142 }
143 }
144 }
145 ///// Ende new list handling
148 /* Edit selected Sub Object */
149 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
150 $script = $_POST['SubObject'][0];
151 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
152 $this->dialog->acl = $this->acl;
153 $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
154 $this->is_dialog=true;
155 }
157 /* Remove Sub object */
158 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
160 foreach($_POST['SubObject'] as $script){
161 if($this->SubObjects[$script]['status'] == "edited"){
162 $this->SubObjects[$script]['status']= "delete";
163 }else{
164 unset($this->SubObjects[$script]);
165 }
166 }
167 }
169 /* Save Dialog */
170 if(isset($_POST['SaveSubObject'])){
171 $this->dialog->save_object();
172 $msgs = $this->dialog->check();
173 if(count($msgs)>0){
174 foreach($msgs as $msg){
175 print_red($msg);
176 }
177 }else{
178 $obj = $this->dialog->save();
179 if(isset($obj['remove'])){
180 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
181 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
182 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
183 unset($this->SubObjects[$obj['remove']['from']]);
184 }
185 $obj['status'] = "new";
186 $this->SubObjects[$obj['remove']['to']] = $obj;
187 unset($this->SubObjects[$obj['remove']['to']]['remove']);
188 }else{
189 $this->SubObjects[$obj['cn']]=$obj;
190 }
191 $this->is_dialog=false;
192 unset($this->dialog);
193 $this->dialog=NULL;
194 }
195 }
197 /* Sort entries */
198 $tmp = $keys = array();
199 foreach($this->SubObjects as $key => $entry){
200 $keys[$key]=$key;
201 }
202 natcasesort($keys);
203 foreach($keys as $key){
204 $tmp[$key]=$this->SubObjects[$key];
205 }
206 $this->SubObjects = $tmp;
208 /* Cancel Dialog */
209 if(isset($_POST['CancelSubObject'])){
210 $this->is_dialog=false;
211 unset($this->dialog);
212 $this->dialog=NULL;
213 }
215 /* Print dialog if $this->dialog is set */
216 if($this->dialog){
217 $this->dialog->save_object();
218 $display = $this->dialog->execute();
219 return($display);
220 }
222 /* Divlist added 23.02.2006
223 Containing FAIscripts
224 */
225 $divlist = new divSelectBox("FAIscripts");
226 $divlist->setHeight(400);
227 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
228 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
229 $img_remo = "";
230 }else{
231 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
232 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
233 }
235 foreach($this->getList(true) as $key => $name){
237 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
238 $down = "";
239 }else{
240 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' >
241 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
242 </a>";
243 }
245 $divlist->AddEntry(array( array("string"=>$name['name']),
246 array("string"=>$down , "attach" => "style='width:20px;'"),
247 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
248 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
249 }
250 $smarty->assign("Entry_divlist",$divlist->DrawList());
252 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
253 * If we post the escaped strings they will be escaped again
254 */
255 foreach($this->attributes as $attrs){
256 if(get_magic_quotes_gpc()){
257 $smarty->assign($attrs,stripslashes($this->$attrs));
258 }else{
259 $smarty->assign($attrs,($this->$attrs));
260 }
261 }
263 foreach($this->attributes as $attr){
264 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
265 }
267 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
268 return($display);
269 }
271 /* Generate listbox friendly SubObject list
272 */
273 function getList($use_dns=false){
274 $a_return=array();
275 foreach($this->SubObjects as $obj){
276 if($obj['status'] != "delete"){
277 if($use_dns){
278 if((isset($obj['description']))&&(!empty($obj['description']))){
279 $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
280 }else{
281 $a_return[$obj['cn']]['name']= $obj['cn'];
282 }
283 $a_return[$obj['cn']]['dn']= $obj['dn'];
284 }else{
285 if((isset($obj['description']))&&(!empty($obj['description']))){
286 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
287 }else{
288 $a_return[$obj['cn']]= $obj['cn'];
289 }
290 }
291 }
292 }
293 return($a_return);
294 }
296 /* Delete me, and all my subtrees
297 */
298 function remove_from_parent()
299 {
300 $ldap = $this->config->get_ldap_link();
301 $ldap->cd ($this->dn);
302 $ldap->rmdir_recursive($this->dn);
303 show_ldap_error($ldap->get_error(), sprintf(_("Removing of FAI/script with dn '%s' failed."),$this->dn));
304 $this->handle_post_events("remove");
305 }
308 /* Save data to object
309 */
310 function save_object()
311 {
312 if((isset($_POST['FAIscript_posted'])) && ($this->FAIstate != "freeze")){
313 plugin::save_object();
314 foreach($this->attributes as $attrs){
315 if(isset($_POST[$attrs])){
316 $this->$attrs = $_POST[$attrs];
317 }
318 }
319 }
320 }
323 /* Check supplied data */
324 function check()
325 {
326 /* Call common method to give check the hook */
327 $message= plugin::check();
329 return ($message);
330 }
333 /* Save to LDAP */
334 function save()
335 {
336 plugin::save();
338 $ldap = $this->config->get_ldap_link();
340 $ldap->cat($this->dn,array("objectClass"));
341 if($ldap->count()!=0){
342 /* Write FAIscript to ldap*/
343 $ldap->cd($this->dn);
344 $this->cleanup();
345 $ldap->modify ($this->attrs);
347 }else{
348 /* Write FAIscript to ldap*/
349 $ldap->cd($this->config->current['BASE']);
350 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
351 $ldap->cd($this->dn);
352 $ldap->add($this->attrs);
353 }
354 show_ldap_error($ldap->get_error(), sprintf(_("Creating of FAI/script with dn '%s' failed."),$this->dn));
356 /* Do object tagging */
357 $this->handle_object_tagging();
359 /* Prepare FAIscriptEntry to write it to ldap
360 * First sort array.
361 * Because we must delete old entries first.
362 * After deletion, we perform add and modify
363 */
364 $Objects = array();
365 foreach($this->SubObjects as $name => $obj){
366 if($obj['status'] == "delete"){
367 $Objects[$name] = $obj;
368 }
369 }
370 foreach($this->SubObjects as $name => $obj){
371 if($obj['status'] != "delete"){
372 $Objects[$name] = $obj;
373 }
374 }
376 foreach($Objects as $name => $obj){
378 foreach($this->sub64coded as $codeIt){
379 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
380 }
382 $tmp = array();
383 foreach($this->subAttributes as $attrs){
384 if(empty($obj[$attrs])){
385 $obj[$attrs] = array();
386 }
387 if(!is_array($obj[$attrs])){
388 $tmp[$attrs] = stripslashes($obj[$attrs]);
389 }else{
390 $tmp[$attrs] = $obj[$attrs];
391 }
392 }
394 $tmp['objectClass'] = $this->subClasses;
396 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
398 if($obj['status']=="new"){
399 $ldap->cat($sub_dn,array("objectClass"));
400 if($ldap->count()){
401 $obj['status']="edited";
402 }
403 }
405 if(empty($tmp['FAIpriority'])){
406 $tmp['FAIpriority'] ="0";
407 }
409 /* Check if gosaAdministrativeUnitTag is required as object class */
410 if($obj['status'] == "edited"){
411 $ldap->cat($sub_dn,array("objectClass"));
412 $attrs = $ldap->fetch();
413 if(isset($attrs['objectClass'])){
414 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
415 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
416 }
417 }
418 }
420 if($obj['status'] == "delete"){
421 $ldap->cd($sub_dn);
422 $ldap->rmdir_recursive($sub_dn);
423 $this->handle_post_events("remove");
424 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/script entry with dn '%s' failed."),$this->dn));
425 }elseif($obj['status'] == "edited"){
426 $ldap->cd($sub_dn);
427 $this->cleanup();
428 $ldap->modify ($tmp);
430 $this->handle_post_events("modify");
431 }elseif($obj['status']=="new"){
432 if($tmp['description']==array()){
433 unset($tmp['description']);
434 }
435 if($tmp['FAIscript']==array()){
436 $tmp['FAIscript']=" ";
437 }
438 $ldap->cd($this->config->current['BASE']);
439 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
440 $ldap->cd($sub_dn);
441 $ldap->add($tmp);
442 $this->handle_post_events("add");
443 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/script entry with dn '%s' failed."),$this->dn));
444 }
446 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
447 }
448 }
449 }
451 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
452 ?>