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 $sub_Load_Later = array("FAItemplateFile");
24 var $sub64coded = array();
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 $SubObjects = array(); // All leafobjects of this object
33 var $FAIstate = "";
34 var $ui;
35 var $view_logged = FALSE;
37 function faiTemplate (&$config, $dn= NULL)
38 {
39 /* Load Attributes */
40 plugin::plugin ($config, $dn);
42 /* If "dn==new" we try to create a new entry
43 * Else we must read all objects from ldap which belong to this entry.
44 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
45 */
46 if($dn != "new"){
47 $this->dn =$dn;
49 /* Get FAIstate
50 */
51 if(isset($this->attrs['FAIstate'][0])){
52 $this->FAIstate = $this->attrs['FAIstate'][0];
53 }
55 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
56 */
57 $ldap = $this->config->get_ldap_link();
58 $ldap->cd ($this->dn);
60 $attrs_to_search = $this->subAttributes;
61 $attrs_to_search[] = "FAIstate";
62 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
64 $data = array();
65 while($object = $ldap->fetch()){
66 $data[] = $object;
67 }
68 foreach($data as $object){
70 /* Skip objects, that are tagged as removed */
71 if(isset($object['FAIstate'][0])){
72 if(preg_match("/removed$/",$object['FAIstate'][0])){
73 continue;
74 }
75 }
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 $this->is_new = FALSE;
86 if($this->dn == "new"){
87 $this->is_new =TRUE;
88 }
89 $this->ui = get_userinfo();
90 }
93 /* Reload some attributes */
94 function get_object_attributes($object,$attributes)
95 {
96 $ldap = $this->config->get_ldap_link();
97 $ldap->cd($this->config->current['BASE']);
98 $ldap->cat($object['dn'],$attributes);
99 $tmp = $ldap->fetch();
101 foreach($attributes as $attrs){
102 if(isset($tmp[$attrs][0])){
103 $var = $tmp[$attrs][0];
105 /* Check if we must decode some attributes */
106 if(in_array_ics($attrs,$this->sub64coded)){
107 $var = base64_decode($var);
108 }
109 $var = stripslashes($var);
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 $object[$attrs] = $var;
117 }
118 }
119 return($object);
120 }
123 function execute()
124 {
125 /* Call parent execute */
126 plugin::execute();
128 if($this->is_account && !$this->view_logged){
129 $this->view_logged = TRUE;
130 new log("view","fai/".get_class($this),$this->dn);
131 }
133 /* Fill templating stuff */
134 $smarty= get_smarty();
135 $display= "";
137 /* New Listhandling
138 */
139 $once = true;
140 foreach($_POST as $name => $value){
141 if(preg_match("/^editscript_/",$name)&&($once)){
142 $once = false;
143 $entry = preg_replace("/^editscript_/","",$name);
144 $entry = base64_decode(preg_replace("/_.*/","",$entry));
145 $obj = $this->SubObjects[$entry];
147 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
148 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
149 if(preg_match("/r/",$acl)){
150 if($obj['status'] == "FreshLoaded"){
151 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
152 }
153 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
154 $this->dialog->set_acl_base($this->acl_base);
155 $this->dialog->set_acl_category("fai");
157 session::set('objectinfo',$obj['dn']);
158 $this->dialog->parent = &$this;
159 $this->is_dialog=true;
160 }
161 }
162 if(preg_match("/^deletescript_/",$name)&&($once)){
163 $once = false;
164 $entry = preg_replace("/^deletescript_/","",$name);
165 $entry = base64_decode(preg_replace("/_.*/","",$entry));
166 $obj = $this->SubObjects[$entry];
168 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
169 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
170 if(preg_match("/d/",$acl)){
171 $status = $this->SubObjects[$entry]['status'];
172 if($status == "edited" || $status == "FreshLoaded"){
173 $this->SubObjects[$entry]['status']= "delete";
174 }else{
175 unset($this->SubObjects[$entry]);
176 }
177 }
178 }
179 }
181 /* File download requested */
182 if(isset($_GET['getFAItemplate'])){
183 if(isset($this->SubObjects[$_GET['getFAItemplate']])){
184 $obj = $this->SubObjects[$_GET['getFAItemplate']];
185 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
186 send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
187 }
188 }
190 /* Edit entries via GET */
191 if(isset($_GET['act']) && isset($_GET['id'])){
192 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
193 $obj = $this->SubObjects[$_GET['id']];
194 if($obj['status'] == "FreshLoaded"){
195 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
196 }
197 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
198 $this->dialog->acl = $this->acl;
199 session::set('objectinfo',$obj['dn']);
200 $this->dialog->parent = &$this;
201 $this->is_dialog=true;
202 }
203 }
205 /* Add new sub object */
206 if(isset($_POST['AddSubObject'])){
207 $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
208 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
210 if(preg_match("/c/",$acl)){
211 $this->dialog= new $this->subClassName($this->config,"new");
212 $this->dialog->set_acl_base($this->acl_base);
213 $this->dialog->set_acl_category("fai");
214 $this->dialog->parent = &$this;
215 $this->is_dialog=true;
216 }
217 }
219 if($this->dn != "new"){
220 session::set('objectinfo',$this->dn);
221 }
223 /* Save Dialog */
224 if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
225 $this->dialog->save_object();
226 $msgs = $this->dialog->check();
227 if(count($msgs)>0){
228 foreach($msgs as $msg){
229 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
230 }
231 }else{
232 /* Get return object */
233 $obj = $this->dialog->save();
234 if(isset($obj['remove'])){
236 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
238 /* Depending on status, set new status */
239 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
240 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
241 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
242 unset($this->SubObjects[$obj['remove']['from']]);
243 }
244 $obj['status'] = "new";
245 $this->SubObjects[$obj['remove']['to']] = $obj;
246 unset($this->SubObjects[$obj['remove']['to']]['remove']);
247 }else{
248 if($obj['status'] == "FreshLoaded"){
249 $obj['status'] = "edited";
250 }
251 $this->SubObjects[$obj['cn']]=$obj;
252 }
253 $this->is_dialog=false;
254 unset($this->dialog);
255 $this->dialog=FALSE;
256 }
257 }
259 /* Sort entries */
260 $tmp = $keys = array();
261 foreach($this->SubObjects as $key => $entry){
262 $keys[$key]=$key;
263 }
264 natcasesort($keys);
265 foreach($keys as $key){
266 $tmp[$key]=$this->SubObjects[$key];
267 }
268 $this->SubObjects = $tmp;
270 /* Cancel Dialog */
271 if(isset($_POST['CancelSubObject'])){
272 $this->is_dialog=false;
273 unset($this->dialog);
274 $this->dialog=FALSE;
275 }
277 /* Print dialog if $this->dialog is set */
278 if(is_object($this->dialog)){
279 $this->dialog->save_object();
280 $display = $this->dialog->execute();
281 return($display);
282 }
284 /* Divlist Containing FAItemplates */
285 $divlist = new divSelectBox("FAItemplates");
286 $divlist->setHeight(400);
288 $tmp = $this->getList(true);
290 /* Create div list with all sub entries listed */
291 foreach($this->SubObjects as $key => $name){
293 /* Skip removed entries */
294 if($name['status'] == "delete") continue;
296 /* Get permissions */
297 $dn = $this->acl_base_for_current_object($name['dn']);
298 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
299 $act = "";
301 /* Hide delete icon if this object is freezed */
302 if(preg_match("/freeze/",$this->FAIstate)){
303 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
304 }else{
305 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
306 if(preg_match("/d/",$acl)){
307 $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
308 }
309 }
311 /* Check acls for download icon */
312 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
313 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
314 $down = "";
315 }else{
316 $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
317 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
318 </a>";
319 }
321 /* Check if we are allowed to view this object */
322 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
323 if(preg_match("/r/",$s_acl)){
325 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$tmp[$key]."</a>";
326 $divlist->AddEntry(array( array("string"=> $edit_link),
327 array("string"=>$down , "attach" => "style='width:20px;'"),
328 array("string"=>str_replace("%s",base64_encode($key),$act),
329 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
330 }
331 }
332 $smarty->assign("Entry_divlist",$divlist->DrawList());
333 /* Divlist creation complete
334 */
336 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
337 * If we post the escaped strings they will be escaped again
338 */
339 foreach($this->attributes as $attrs){
340 if(get_magic_quotes_gpc()){
341 $smarty->assign($attrs,stripslashes($this->$attrs));
342 }else{
343 $smarty->assign($attrs,($this->$attrs));
344 }
345 }
347 $dn = $this->acl_base_for_current_object($this->dn);
348 $smarty->assign("sub_object_is_addable",
349 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
350 !preg_match("/freeze/",$this->FAIstate));
352 foreach($this->attributes as $attr){
353 $smarty->assign($attr."ACL",$this->getacl($attr));
354 }
356 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
357 return($display);
358 }
361 function acl_base_for_current_object($dn)
362 {
363 if($dn == "new"){
364 if($this->dn == "new"){
365 $dn = session::get('CurrentMainBase');
366 }else{
367 $dn = $this->dn;
368 }
369 }
370 return($dn);
371 }
374 /* Generate listbox friendly SubObject list
375 */
376 function getList(){
377 $a_return=array();
378 foreach($this->SubObjects as $obj){
379 if($obj['status'] != "delete"){
381 if((isset($obj['description']))&&(!empty($obj['description']))){
382 if(strlen($obj['description']) > 40){
383 $obj['description'] = substr($obj['description'],0,40)."...";
384 }
385 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
386 }else{
387 $a_return[$obj['cn']]= $obj['cn'];
388 }
389 }
390 }
391 return($a_return);
392 }
394 /* Delete me, and all my subtrees
395 */
396 function remove_from_parent()
397 {
398 if($this->acl_is_removeable()){
399 $ldap = $this->config->get_ldap_link();
400 $ldap->cd ($this->dn);
401 $release = $this->parent->parent->fai_release;
402 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
403 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
404 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
406 foreach($this->SubObjects as $name => $obj){
407 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
408 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
409 }
410 $this->handle_post_events("remove");
411 }
412 }
415 /* Save data to object
416 */
417 function save_object()
418 {
419 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
420 plugin::save_object();
421 }
422 }
425 /* Check supplied data */
426 function check()
427 {
428 /* Call common method to give check the hook */
429 $message= plugin::check();
431 /* Ensure that we do not overwrite an allready existing entry
432 */
433 if($this->is_new){
434 $release = $this->parent->parent->fai_release;
435 $new_dn= 'cn='.$this->cn.",".get_ou('faitemplateou').get_ou('faiou').$release;
436 $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
437 if(isset($res[$this->cn])){
438 $message[] = msgPool::duplicated(_("Name"));
439 }
440 }
442 return ($message);
443 }
446 /* Save to LDAP */
447 function save()
448 {
449 plugin::save();
451 $ldap = $this->config->get_ldap_link();
453 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
455 if($this->initially_was_account){
456 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
457 }else{
458 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
459 }
461 /* Prepare FAIscriptEntry to write it to ldap
462 * First sort array.
463 * Because we must delete old entries first.
464 * After deletion, we perform add and modify
465 */
466 $Objects = array();
467 foreach($this->SubObjects as $name => $obj){
468 if($obj['status'] == "FreshLoaded"){
469 unset($this->SubObjects[$name]);
470 }
471 }
473 foreach($this->SubObjects as $name => $obj){
474 if($obj['status'] == "delete"){
475 $Objects[$name] = $obj;
476 }
477 }
478 foreach($this->SubObjects as $name => $obj){
479 if($obj['status'] != "delete"){
480 $Objects[$name] = $obj;
481 }
482 }
484 foreach($Objects as $name => $obj){
486 foreach($this->sub64coded as $codeIt){
487 $obj[$codeIt]=base64_encode($obj[$codeIt]);
488 }
489 $tmp = array();
490 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
491 foreach($attributes as $attrs){
492 if(empty($obj[$attrs])){
493 $obj[$attrs] = array();
494 }
495 $tmp[$attrs] =($obj[$attrs]);
496 }
498 $tmp['objectClass'] = $this->subClasses;
500 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
502 if($obj['status']=="new"){
503 $ldap->cat($sub_dn,array("objectClass"));
504 if($ldap->count()){
505 $obj['status']="edited";
506 }
507 }
509 /* Tag object */
510 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
512 if($obj['status'] == "delete"){
513 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
514 $this->handle_post_events("remove");
515 }elseif($obj['status'] == "edited"){
516 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
517 $this->handle_post_events("modify");
518 }elseif($obj['status']=="new"){
519 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
520 $this->handle_post_events("add");
521 }
523 }
524 }
527 function PrepareForCopyPaste($source)
528 {
529 plugin::PrepareForCopyPaste($source);
531 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
532 */
533 $ldap = $this->config->get_ldap_link();
534 $ldap->cd ($source['dn']);
536 $attrs_to_search = $this->subAttributes;
537 $attrs_to_search[] = "FAIstate";
538 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
540 while($object = $ldap->fetch()){
542 /* Skip objects, that are tagged as removed */
543 if(isset($object['FAIstate'][0])){
544 if(preg_match("/removed$/",$object['FAIstate'][0])){
545 continue;
546 }
547 }
549 /* Set status for save management */
550 $objects = array();
551 $objects['status'] = "edited";
552 $objects['dn'] = $object['dn'];
553 $objects = $this->get_object_attributes($objects,$this->subAttributes);
554 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
556 $this->SubObjects[$objects['cn']] = $objects;
557 }
558 }
561 /* Return plugin informations for acl handling */
562 static function plInfo()
563 {
564 return (array(
565 "plShortName" => _("Template"),
566 "plDescription" => _("FAI template"),
567 "plSelfModify" => FALSE,
568 "plDepends" => array(),
569 "plPriority" => 24,
570 "plSection" => array("administration"),
571 "plCategory" => array("fai"),
572 "plProvidedAcls" => array(
573 "cn" => _("Name")." ("._("Readonly").")",
574 "description" => _("Description"))
575 ));
576 }
579 /*! \brief Used for copy & paste.
580 Returns a HTML input mask, which allows to change the cn of this entry.
581 @param Array Array containing current status && a HTML template.
582 */
583 function getCopyDialog()
584 {
585 $vars = array("cn");
586 $smarty = get_smarty();
587 $smarty->assign("cn", htmlentities($this->cn));
588 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
589 $ret = array();
590 $ret['string'] = $str;
591 $ret['status'] = "";
592 return($ret);
593 }
596 /*! \brief Used for copy & paste.
597 Some entries must be renamed to avaoid duplicate entries.
598 */
599 function saveCopyDialog()
600 {
601 if(isset($_POST['cn'])){
602 $this->cn = get_post('cn');
603 }
604 }
605 }
607 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
608 ?>