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 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50 */
51 $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
52 foreach($res as $obj){
53 $objects = array();
54 $objects['status'] = "FreshLoaded";
55 $objects['dn'] = $obj['dn'];
56 $objects = $this->get_object_attributes($objects,$this->subAttributes);
57 $this->SubObjects[$objects['cn']] = $objects;
58 }
59 }
60 $this->is_new = FALSE;
61 if($this->dn == "new"){
62 $this->is_new =TRUE;
63 }
64 $this->ui = get_userinfo();
65 }
68 /* Reload some attributes */
69 function get_object_attributes($object,$attributes)
70 {
71 $ldap = $this->config->get_ldap_link();
72 $ldap->cd($this->config->current['BASE']);
73 $ldap->cat($object['dn'],$attributes);
74 $tmp = $ldap->fetch();
76 foreach($attributes as $attrs){
77 if(isset($tmp[$attrs][0])){
78 $var = $tmp[$attrs][0];
80 /* Check if we must decode some attributes */
81 if(in_array_ics($attrs,$this->sub64coded)){
82 $var = base64_decode($var);
83 }
84 $var = stripslashes($var);
86 /* check if this is a binary entry */
87 if(in_array_ics($attrs,$this->subBinary)){
88 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
89 }
91 $object[$attrs] = $var;
92 }
93 }
94 return($object);
95 }
98 function execute()
99 {
100 /* Call parent execute */
101 plugin::execute();
103 if($this->is_account && !$this->view_logged){
104 $this->view_logged = TRUE;
105 new log("view","fai/".get_class($this),$this->dn);
106 }
108 /* Fill templating stuff */
109 $smarty= get_smarty();
110 $display= "";
112 /* New Listhandling
113 */
114 $once = true;
115 foreach($_POST as $name => $value){
116 if(preg_match("/^editscript_/",$name)&&($once)){
117 $once = false;
118 $entry = preg_replace("/^editscript_/","",$name);
119 $entry = base64_decode(preg_replace("/_.*/","",$entry));
120 $obj = $this->SubObjects[$entry];
122 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
123 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
124 if(preg_match("/r/",$acl)){
125 if($obj['status'] == "FreshLoaded"){
126 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
127 }
128 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
129 $this->dialog->set_acl_base($this->acl_base);
130 $this->dialog->FAIstate = $this->FAIstate;
131 $this->dialog->set_acl_category("fai");
133 session::set('objectinfo',$obj['dn']);
134 $this->dialog->parent = &$this;
135 $this->is_dialog=true;
136 }
137 }
138 if(preg_match("/^deletescript_/",$name)&&($once) && !preg_match("/freeze/",$this->FAIstate)){
139 $once = false;
140 $entry = preg_replace("/^deletescript_/","",$name);
141 $entry = base64_decode(preg_replace("/_.*/","",$entry));
142 $obj = $this->SubObjects[$entry];
144 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
145 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
146 if(preg_match("/d/",$acl)){
147 $status = $this->SubObjects[$entry]['status'];
148 if($status == "edited" || $status == "FreshLoaded"){
149 $this->SubObjects[$entry]['status']= "delete";
150 }else{
151 unset($this->SubObjects[$entry]);
152 }
153 }
154 }
155 }
157 /* File download requested */
158 if(isset($_GET['getFAItemplate'])){
159 if(isset($this->SubObjects[$_GET['getFAItemplate']])){
160 $obj = $this->SubObjects[$_GET['getFAItemplate']];
161 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
162 send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
163 }
164 }
166 /* Edit entries via GET */
167 if(isset($_GET['act']) && isset($_GET['id'])){
168 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
169 $obj = $this->SubObjects[$_GET['id']];
170 if($obj['status'] == "FreshLoaded"){
171 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
172 }
173 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
174 $this->dialog->FAIstate = $this->FAIstate;
175 $this->dialog->acl = $this->acl;
176 session::set('objectinfo',$obj['dn']);
177 $this->dialog->parent = &$this;
178 $this->is_dialog=true;
179 }
180 }
182 /* Add new sub object */
183 if(isset($_POST['AddSubObject']) && !preg_match("/freeze/",$this->FAIstate)){
184 $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
185 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
187 if(preg_match("/c/",$acl)){
188 $this->dialog= new $this->subClassName($this->config,"new");
189 $this->dialog->set_acl_base($this->acl_base);
190 $this->dialog->FAIstate = $this->FAIstate;
191 $this->dialog->set_acl_category("fai");
192 $this->dialog->parent = &$this;
193 $this->is_dialog=true;
194 }
195 }
197 if($this->dn != "new"){
198 session::set('objectinfo',$this->dn);
199 }
201 /* Save Dialog */
202 if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
203 $this->dialog->save_object();
204 $msgs = $this->dialog->check();
205 if(count($msgs)>0){
206 foreach($msgs as $msg){
207 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
208 }
209 }else{
210 /* Get return object */
211 $obj = $this->dialog->save();
212 if(isset($obj['remove'])){
214 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
216 /* Depending on status, set new status */
217 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
218 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
219 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
220 unset($this->SubObjects[$obj['remove']['from']]);
221 }
222 $obj['status'] = "new";
223 $this->SubObjects[$obj['remove']['to']] = $obj;
224 unset($this->SubObjects[$obj['remove']['to']]['remove']);
225 }else{
226 if($obj['status'] == "FreshLoaded"){
227 $obj['status'] = "edited";
228 }
229 $this->SubObjects[$obj['cn']]=$obj;
230 }
231 $this->is_dialog=false;
232 unset($this->dialog);
233 $this->dialog=FALSE;
234 }
235 }
237 /* Sort entries */
238 $tmp = $keys = array();
239 foreach($this->SubObjects as $key => $entry){
240 $keys[$key]=$key;
241 }
242 natcasesort($keys);
243 foreach($keys as $key){
244 $tmp[$key]=$this->SubObjects[$key];
245 }
246 $this->SubObjects = $tmp;
248 /* Cancel Dialog */
249 if(isset($_POST['CancelSubObject'])){
250 $this->is_dialog=false;
251 unset($this->dialog);
252 $this->dialog=FALSE;
253 }
255 /* Print dialog if $this->dialog is set */
256 if(is_object($this->dialog)){
257 $this->dialog->save_object();
258 $display = $this->dialog->execute();
259 return($display);
260 }
262 /* Divlist Containing FAItemplates */
263 $divlist = new divSelectBox("FAItemplates");
264 $divlist->setHeight(400);
266 $tmp = $this->getList(true);
268 /* Create div list with all sub entries listed */
269 foreach($this->SubObjects as $key => $name){
271 /* Skip removed entries */
272 if($name['status'] == "delete") continue;
274 /* Get permissions */
275 $dn = $this->acl_base_for_current_object($name['dn']);
276 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
277 $act = "";
279 /* Hide delete icon if this object is freezed */
280 if(preg_match("/freeze/",$this->FAIstate)){
281 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
282 }else{
283 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
284 if(preg_match("/d/",$acl)){
285 $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
286 }
287 }
289 /* Check acls for download icon */
290 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
291 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
292 $down = "";
293 }else{
294 $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
295 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
296 </a>";
297 }
299 /* Check if we are allowed to view this object */
300 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
301 if(preg_match("/r/",$s_acl)){
303 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$tmp[$key]."</a>";
304 $divlist->AddEntry(array( array("string"=> $edit_link),
305 array("string"=>$down , "attach" => "style='width:20px;'"),
306 array("string"=>str_replace("%s",base64_encode($key),$act),
307 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
308 }
309 }
310 $smarty->assign("Entry_divlist",$divlist->DrawList());
311 /* Divlist creation complete
312 */
314 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
315 * If we post the escaped strings they will be escaped again
316 */
317 foreach($this->attributes as $attrs){
318 if(get_magic_quotes_gpc()){
319 $smarty->assign($attrs,stripslashes($this->$attrs));
320 }else{
321 $smarty->assign($attrs,($this->$attrs));
322 }
323 }
325 $dn = $this->acl_base_for_current_object($this->dn);
326 $smarty->assign("sub_object_is_addable",
327 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
328 !preg_match("/freeze/",$this->FAIstate));
330 foreach($this->attributes as $attr){
331 $smarty->assign($attr."ACL",$this->getacl($attr, preg_match("/freeze/",$this->FAIstate)));
332 }
334 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
335 return($display);
336 }
339 function acl_base_for_current_object($dn)
340 {
341 if($dn == "new"){
342 if($this->dn == "new"){
343 $dn = session::get('CurrentMainBase');
344 }else{
345 $dn = $this->dn;
346 }
347 }
348 return($dn);
349 }
352 /* Generate listbox friendly SubObject list
353 */
354 function getList(){
355 $a_return=array();
356 foreach($this->SubObjects as $obj){
357 if($obj['status'] != "delete"){
359 if((isset($obj['description']))&&(!empty($obj['description']))){
360 if(strlen($obj['description']) > 40){
361 $obj['description'] = substr($obj['description'],0,40)."...";
362 }
363 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
364 }else{
365 $a_return[$obj['cn']]= $obj['cn'];
366 }
367 }
368 }
369 return($a_return);
370 }
372 /* Delete me, and all my subtrees
373 */
374 function remove_from_parent()
375 {
376 if($this->acl_is_removeable()){
377 $ldap = $this->config->get_ldap_link();
378 $ldap->cd ($this->dn);
379 $release = $this->parent->parent->fai_release;
380 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
381 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
382 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
384 foreach($this->SubObjects as $name => $obj){
385 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
386 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
387 }
388 $this->handle_post_events("remove");
389 }
390 }
393 /* Save data to object
394 */
395 function save_object()
396 {
397 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
398 plugin::save_object();
399 }
400 }
403 /* Check supplied data */
404 function check()
405 {
406 /* Call common method to give check the hook */
407 $message= plugin::check();
409 /* Ensure that we do not overwrite an allready existing entry
410 */
411 if($this->is_new){
412 $release = $this->parent->parent->fai_release;
413 $new_dn= 'cn='.$this->cn.",".get_ou('faitemplateou').get_ou('faiou').$release;
414 $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
415 if(isset($res[$this->cn])){
416 $message[] = msgPool::duplicated(_("Name"));
417 }
418 }
420 return ($message);
421 }
424 /* Save to LDAP */
425 function save()
426 {
427 plugin::save();
429 $ldap = $this->config->get_ldap_link();
431 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
433 if($this->initially_was_account){
434 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
435 }else{
436 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
437 }
439 /* Prepare FAIscriptEntry to write it to ldap
440 * First sort array.
441 * Because we must delete old entries first.
442 * After deletion, we perform add and modify
443 */
444 $Objects = array();
445 $tmp = $this->SubObjects;
446 foreach($tmp as $name => $obj){
447 if($obj['status'] == "FreshLoaded"){
448 unset($tmp[$name]);
449 }
450 }
452 foreach($tmp as $name => $obj){
453 if($obj['status'] == "delete"){
454 $Objects[$name] = $obj;
455 }
456 }
457 foreach($tmp as $name => $obj){
458 if($obj['status'] != "delete"){
459 $Objects[$name] = $obj;
460 }
461 }
463 foreach($Objects as $name => $obj){
465 foreach($this->sub64coded as $codeIt){
466 $obj[$codeIt]=base64_encode($obj[$codeIt]);
467 }
468 $tmp = array();
469 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
470 foreach($attributes as $attrs){
471 if(empty($obj[$attrs])){
472 $obj[$attrs] = array();
473 }
474 $tmp[$attrs] =($obj[$attrs]);
475 }
477 $tmp['objectClass'] = $this->subClasses;
479 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
481 if($obj['status']=="new"){
482 $ldap->cat($sub_dn,array("objectClass"));
483 if($ldap->count()){
484 $obj['status']="edited";
485 }
486 }
488 /* Tag object */
489 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
491 if($obj['status'] == "delete"){
492 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
493 $this->handle_post_events("remove");
494 }elseif($obj['status'] == "edited"){
495 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
496 $this->handle_post_events("modify");
497 }elseif($obj['status']=="new"){
498 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
499 $this->handle_post_events("add");
500 }
501 }
502 }
505 function PrepareForCopyPaste($source)
506 {
507 plugin::PrepareForCopyPaste($source);
509 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
510 */
511 $ldap = $this->config->get_ldap_link();
512 $ldap->cd ($source['dn']);
514 $attrs_to_search = $this->subAttributes;
515 $attrs_to_search[] = "FAIstate";
516 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
518 while($object = $ldap->fetch()){
520 /* Skip objects, that are tagged as removed */
521 if(isset($object['FAIstate'][0])){
522 if(preg_match("/removed$/",$object['FAIstate'][0])){
523 continue;
524 }
525 }
527 /* Set status for save management */
528 $objects = array();
529 $objects['status'] = "edited";
530 $objects['dn'] = $object['dn'];
531 $objects = $this->get_object_attributes($objects,$this->subAttributes);
532 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
534 $this->SubObjects[$objects['cn']] = $objects;
535 }
536 }
539 /* Return plugin informations for acl handling */
540 static function plInfo()
541 {
542 return (array(
543 "plShortName" => _("Template"),
544 "plDescription" => _("FAI template"),
545 "plSelfModify" => FALSE,
546 "plDepends" => array(),
547 "plPriority" => 24,
548 "plSection" => array("administration"),
549 "plCategory" => array("fai"),
550 "plProvidedAcls" => array(
551 "cn" => _("Name")." ("._("Readonly").")",
552 "description" => _("Description"))
553 ));
554 }
557 /*! \brief Used for copy & paste.
558 Returns a HTML input mask, which allows to change the cn of this entry.
559 @param Array Array containing current status && a HTML template.
560 */
561 function getCopyDialog()
562 {
563 $vars = array("cn");
564 $smarty = get_smarty();
565 $smarty->assign("cn", htmlentities($this->cn));
566 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
567 $ret = array();
568 $ret['string'] = $str;
569 $ret['status'] = "";
570 return($ret);
571 }
574 /*! \brief Used for copy & paste.
575 Some entries must be renamed to avaoid duplicate entries.
576 */
577 function saveCopyDialog()
578 {
579 if(isset($_POST['cn'])){
580 $this->cn = get_post('cn');
581 }
582 }
583 }
585 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
586 ?>