bb552443cfa8a9690ec4711ccc25f151abc6d5e8
1 <?php
3 class faiTemplate extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
8 /* Attributes for this Object */
9 var $attributes = array("cn","description");
11 /* ObjectClasses for this Object*/
12 var $objectclasses = array("top","FAIclass","FAItemplate");
14 /* Class name of the Ldap ObjectClass for the Sub Object */
15 var $subClass = "FAItemplateEntry";
16 var $subClasses = array("top","FAIclass","FAItemplateEntry");
18 /* Class name of the php class which allows us to edit a Sub Object */
19 var $subClassName = "faiTemplateEntry";
21 /* Attributes to initialise for each subObject */
22 var $subAttributes = array("cn","description","FAItemplatePath","FAIowner","FAImode");
23 var $sub64coded = array();
24 var $sub_Load_Later = array("FAItemplateFile");
25 var $subBinary = array("FAItemplateFile");
27 /* Specific attributes */
28 var $cn = ""; // The class name for this object
29 var $description = ""; // The description for this set of partitions
30 var $is_dialog = false; // specifies which buttons will be shown to save or abort
31 var $dialog = NULL; // a dialog, e.g. new disk dialog
32 var $SubObjects = array(); // All leafobjects of this object
34 var $FAIstate = "";
35 var $base = "";
36 var $release = "";
37 var $copy_paste_mode = false;
38 var $cut_paste_mode = false;
40 var $CopyPasteVars = array("SubObjects");
42 function faiTemplate ($config, $dn= NULL)
43 {
44 /* Load Attributes */
45 plugin::plugin ($config, $dn);
47 $this->acl="#all#";
49 /* If "dn==new" we try to create a new entry
50 * Else we must read all objects from ldap which belong to this entry.
51 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
52 */
53 if($dn != "new"){
54 $this->dn =$dn;
56 /* Set acls
57 */
58 $ui = get_userinfo();
59 $acl = get_permissions ($this->dn, $ui->subtreeACL);
60 $acli = get_module_permission($acl, "FAIclass", $this->dn);
61 $this->acl=$acli;
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."))",array("dn"));
75 while($object = $ldap->fetch()){
77 /* Set status for save management */
78 $objects = array();
79 $objects['status'] = "FreshLoaded";
80 $objects['dn'] = $object['dn'];
81 $objects = $this->get_object_attributes($objects,$this->subAttributes);
82 $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);
113 $object[$attrs] = $var;
114 }
115 }
116 return($object);
117 }
120 function execute()
121 {
122 /* Call parent execute */
123 plugin::execute();
125 /* Fill templating stuff */
126 $smarty= get_smarty();
127 $display= "";
129 /* New Listhandling
130 */
131 $once = true;
132 foreach($_POST as $name => $value){
133 if(preg_match("/^editscript_/",$name)&&($once)){
134 $once = false;
135 $entry = preg_replace("/^editscript_/","",$name);
136 $entry = base64_decode(preg_replace("/_.*/","",$entry));
138 $obj = $this->SubObjects[$entry];
139 if($obj['status'] == "FreshLoaded"){
140 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
141 }
143 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
144 $this->dialog->acl = $this->acl;
145 $_SESSION['objectinfo'] = $obj['dn'];
146 $this->dialog->parent = &$this;
147 $this->is_dialog=true;
148 }
149 if(preg_match("/^deletescript_/",$name)&&($once)){
150 $once = false;
151 $entry = preg_replace("/^deletescript_/","",$name);
152 $entry = base64_decode(preg_replace("/_.*/","",$entry));
154 $status = $this->SubObjects[$entry]['status'];
155 if($status == "edited" || $status == "FreshLoaded"){
156 $this->SubObjects[$entry]['status']= "delete";
157 }else{
158 unset($this->SubObjects[$entry]);
159 }
160 }
161 }
163 /* Edit entries via GET */
164 if(isset($_GET['act']) && isset($_GET['id'])){
165 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
166 $obj = $this->SubObjects[$_GET['id']];
167 if($obj['status'] == "FreshLoaded"){
168 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
169 }
170 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
171 $this->dialog->acl = $this->acl;
172 $_SESSION['objectinfo'] = $obj['dn'];
173 $this->dialog->parent = &$this;
174 $this->is_dialog=true;
175 }
176 }
178 /* Add new sub object */
179 if(isset($_POST['AddSubObject'])){
180 $this->dialog= new $this->subClassName($this->config,"new");
181 $this->dialog->acl = $this->acl;
182 $this->dialog->parent = &$this;
183 $this->is_dialog=true;
184 }
186 if($this->dn != "new"){
187 $_SESSION['objectinfo']= $this->dn;
188 }
190 /* Save Dialog */
191 if(isset($_POST['SaveSubObject'])){
192 $this->dialog->save_object();
193 $msgs = $this->dialog->check();
194 if(count($msgs)>0){
195 foreach($msgs as $msg){
196 print_red($msg);
197 }
198 }else{
200 /* Get return object */
201 $obj = $this->dialog->save();
202 if(isset($obj['remove'])){
203 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
205 /* Depending on status, set new status */
206 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
207 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
208 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
209 unset($this->SubObjects[$obj['remove']['from']]);
210 }
211 $obj['status'] = "new";
212 $this->SubObjects[$obj['remove']['to']] = $obj;
213 unset($this->SubObjects[$obj['remove']['to']]['remove']);
214 }else{
215 if($obj['status'] == "FreshLoaded"){
216 $obj['status'] = "edited";
217 }
218 $this->SubObjects[$obj['cn']]=$obj;
219 }
221 $this->is_dialog=false;
222 unset($this->dialog);
223 $this->dialog=NULL;
224 }
225 }
227 /* Sort entries */
228 $tmp = $keys = array();
229 foreach($this->SubObjects as $key => $entry){
230 $keys[$key]=$key;
231 }
232 natcasesort($keys);
233 foreach($keys as $key){
234 $tmp[$key]=$this->SubObjects[$key];
235 }
236 $this->SubObjects = $tmp;
238 /* Cancel Dialog */
239 if(isset($_POST['CancelSubObject'])){
240 $this->is_dialog=false;
241 unset($this->dialog);
242 $this->dialog=NULL;
243 }
245 /* Print dialog if $this->dialog is set */
246 if($this->dialog){
247 $this->dialog->save_object();
248 $display = $this->dialog->execute();
249 return($display);
250 }
252 /* Divlist Containing FAItemplates */
253 $divlist = new divSelectBox("FAItemplates");
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{
269 $dn = $this->SubObjects[$key]['dn'];
271 $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
272 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
273 </a>";
274 }
276 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$name."</a>";
278 $divlist->AddEntry(array( array("string"=> $edit_link),
279 array("string"=>$down , "attach" => "style='width:20px;'"),
280 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
281 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
282 }
283 $smarty->assign("Entry_divlist",$divlist->DrawList());
284 /* Divlist creation complete
285 */
287 $smarty->assign("SubObjects",$this->getList());
289 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
290 * If we post the escaped strings they will be escaped again
291 */
292 foreach($this->attributes as $attrs){
293 if(get_magic_quotes_gpc()){
294 $smarty->assign($attrs,stripslashes($this->$attrs));
295 }else{
296 $smarty->assign($attrs,($this->$attrs));
297 }
298 }
300 foreach($this->attributes as $attr){
301 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
302 }
304 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
305 return($display);
306 }
308 /* Generate listbox friendly SubObject list
309 */
310 function getList(){
311 $a_return=array();
312 foreach($this->SubObjects as $obj){
313 if($obj['status'] != "delete"){
315 if((isset($obj['description']))&&(!empty($obj['description']))){
316 if(strlen($obj['description']) > 40){
317 $obj['description'] = substr($obj['description'],0,40)."...";
318 }
319 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes( $obj['description'] )."]";
320 }else{
321 $a_return[$obj['cn']]= $obj['cn'];
322 }
323 }
324 }
325 return($a_return);
326 }
328 /* Delete me, and all my subtrees
329 */
330 function remove_from_parent()
331 {
332 $ldap = $this->config->get_ldap_link();
333 $ldap->cd ($this->dn);
334 $ldap->rmdir_recursive($this->dn);
335 $this->handle_post_events("remove");
336 }
339 /* Save data to object
340 */
341 function save_object()
342 {
343 if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
344 plugin::save_object();
345 foreach($this->attributes as $attrs){
346 if(isset($_POST[$attrs])){
347 $this->$attrs = $_POST[$attrs];
348 }
349 }
350 }
351 }
354 /* Check supplied data */
355 function check()
356 {
357 /* Call common method to give check the hook */
358 $message= plugin::check();
360 /* If this is a new script, check if a script with this name already exists */
361 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
363 /* Check if current name is already used for fai scripts in selected release */
364 $dn = 'cn='.$this->cn.",ou=templates,".$this->release;
365 $ldap = $this->config->get_ldap_link();
366 $ldap->cat($dn);
367 if($ldap->count()){
369 $r =convert_department_dn($this->release);;
370 $message[] = sprintf(_("Can't insert a fai template named '%s' in '%s' there is already a template with the given name."),$this->cn,$r);
371 }
372 }
373 return ($message);
374 }
377 /* Save to LDAP */
378 function save()
379 {
380 plugin::save();
382 $ldap = $this->config->get_ldap_link();
384 /* Copy & Paste : Ensure that FAIstate is copied too */
385 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
386 $this->attrs['FAIstate'] = $this->FAIstate;
387 }
389 $ldap->cat($this->dn,array("objectClass"));
390 if($ldap->count()!=0){
391 /* Write FAIscript to ldap*/
392 $ldap->cd($this->dn);
393 $this->cleanup();
394 $ldap->modify ($this->attrs);
396 }else{
397 /* Write FAIscript to ldap*/
398 $ldap->cd($this->config->current['BASE']);
399 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
400 $ldap->cd($this->dn);
401 $ldap->add($this->attrs);
402 }
403 show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
405 /* Do object tagging */
406 $this->handle_object_tagging();
408 /* Prepare FAIscriptEntry to write it to ldap
409 * First sort array.
410 * Because we must delete old entries first.
411 * After deletion, we perform add and modify
412 */
413 $Objects = array();
415 /* We do not need to save untouched objects */
416 foreach($this->SubObjects as $name => $obj){
417 if($obj['status'] == "FreshLoaded"){
418 if($this->copy_paste_mode){
419 $this->SubObjects[$name] = $this->get_object_attributes($obj,$this->sub_Load_Later);
420 $this->SubObjects[$name]['status'] = "new";
421 }else{
422 unset($this->SubObjects[$name]);
423 }
424 }
425 }
427 foreach($this->SubObjects as $name => $obj){
428 if($obj['status'] == "delete"){
429 $Objects[$name] = $obj;
430 }
431 }
433 foreach($this->SubObjects as $name => $obj){
434 if($obj['status'] != "delete"){
435 $Objects[$name] = $obj;
436 }
437 }
439 foreach($Objects as $name => $obj){
441 foreach($this->sub64coded as $codeIt){
442 $obj[$codeIt]=base64_encode($obj[$codeIt]);
443 }
444 $tmp = array();
445 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
446 foreach($attributes as $attrs){
447 if(empty($obj[$attrs])){
448 $obj[$attrs] = array();
449 $tmp[$attrs] = $obj[$attrs];
450 }else{
451 $tmp[$attrs] = stripslashes ($obj[$attrs]);
452 }
453 }
455 $tmp['objectClass'] = $this->subClasses;
457 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
459 if($obj['status']=="new"){
460 $ldap->cat($sub_dn,array("objectClass"));
461 if($ldap->count()){
462 $obj['status']="edited";
463 }
464 }
466 /* Check if gosaAdministrativeUnitTag is required as object class */
467 if($obj['status'] == "edited"){
468 $ldap->cat($sub_dn,array("objectClass"));
469 $attrs = $ldap->fetch();
470 if(isset($attrs['objectClass'])){
471 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
472 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
473 }
474 }
475 }
477 if($obj['status'] == "delete"){
478 $ldap->cd($sub_dn);
479 $ldap->rmdir_recursive($sub_dn);
480 $this->handle_post_events("remove");
481 show_ldap_error($ldap->get_error(), _("Removing FAI template entry failed"));
482 }elseif($obj['status'] == "edited"){
483 $ldap->cd($sub_dn);
484 $this->cleanup();
485 $ldap->modify ($tmp);
486 show_ldap_error($ldap->get_error(), _("Modifying FAI template entry failed"));
488 $this->handle_post_events("modify");
489 }elseif($obj['status']=="new"){
491 if($tmp['description']==array()){
492 unset($tmp['description']);
493 }
494 $ldap->cd($this->config->current['BASE']);
495 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
496 $ldap->cd($sub_dn);
497 $ldap->add($tmp);
498 $this->handle_post_events("add");
499 show_ldap_error($ldap->get_error(), _("Creating FAI template entry failed"));
500 }
502 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
503 }
504 }
506 /* return copy & paste dialog
507 */
508 function getCopyDialog()
509 {
510 /* Ask for cn */
511 $smarty = get_smarty();
512 $smarty->assign("cn" ,$this->cn);
513 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
514 $ret = array();
515 $ret['string'] = $str;
516 $ret['status'] = "";
517 return($ret);
518 }
520 /* Get posted cn */
521 function saveCopyDialog()
522 {
523 if(isset($_POST['cn'])){
524 $this->cn = $_POST['cn'];
525 }
526 }
527 }
529 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
530 ?>