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