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_leaf_objects($this->dn, $this->cn, $this->subClass, get_ou('faiTemplateRDN'));
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 set_object_info($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 $this->dialog->set_acl_category("fai");
177 set_object_info($obj['dn']);
178 $this->dialog->parent = &$this;
179 $this->is_dialog=true;
180 }
181 }
183 /* Add new sub object */
184 if(isset($_POST['AddSubObject']) && !preg_match("/freeze/",$this->FAIstate)){
185 $acl_dn = $this->acl_base_for_current_object($this->dn);
186 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
188 if(preg_match("/c/",$acl)){
189 $this->dialog= new $this->subClassName($this->config,"new");
190 $this->dialog->set_acl_base($this->acl_base);
191 $this->dialog->FAIstate = $this->FAIstate;
192 $this->dialog->set_acl_category("fai");
193 $this->dialog->parent = &$this;
194 $this->is_dialog=true;
195 }
196 }
198 if($this->dn != "new"){
199 set_object_info($this->dn);
200 }
202 /* Save Dialog */
203 if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
204 $this->dialog->save_object();
205 $msgs = $this->dialog->check();
206 if(count($msgs)>0){
207 foreach($msgs as $msg){
208 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
209 }
210 }else{
211 /* Get return object */
212 $obj = $this->dialog->save();
213 if(isset($obj['remove'])){
215 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
217 /* Depending on status, set new status */
218 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
219 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
220 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
221 unset($this->SubObjects[$obj['remove']['from']]);
222 }
223 $obj['status'] = "new";
224 $this->SubObjects[$obj['remove']['to']] = $obj;
225 unset($this->SubObjects[$obj['remove']['to']]['remove']);
226 }else{
227 if($obj['status'] == "FreshLoaded"){
228 $obj['status'] = "edited";
229 }
230 $this->SubObjects[$obj['cn']]=$obj;
231 }
232 $this->is_dialog=false;
233 unset($this->dialog);
234 $this->dialog=FALSE;
235 }
236 }
238 /* Sort entries */
239 $tmp = $keys = array();
240 foreach($this->SubObjects as $key => $entry){
241 $keys[$key]=$key;
242 }
243 natcasesort($keys);
244 foreach($keys as $key){
245 $tmp[$key]=$this->SubObjects[$key];
246 }
247 $this->SubObjects = $tmp;
249 /* Cancel Dialog */
250 if(isset($_POST['CancelSubObject'])){
251 $this->is_dialog=false;
252 unset($this->dialog);
253 $this->dialog=FALSE;
254 }
256 /* Print dialog if $this->dialog is set */
257 if(is_object($this->dialog)){
258 $this->dialog->save_object();
259 $display = $this->dialog->execute();
260 return($display);
261 }
263 /* Divlist Containing FAItemplates */
264 $divlist = new divSelectBox("FAItemplates");
265 $divlist->setHeight(400);
267 $tmp = $this->getList(true);
269 /* Create div list with all sub entries listed */
270 foreach($this->SubObjects as $key => $name){
272 /* Skip removed entries */
273 if($name['status'] == "delete") continue;
275 /* Get permissions */
276 $dn = $this->acl_base_for_current_object($name['dn']);
277 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
278 $act = "";
280 /* Hide delete icon if this object is freezed */
281 if(preg_match("/freeze/",$this->FAIstate)){
282 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
283 }else{
284 $act .= "<input type='image' src='images/lists/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
285 if(preg_match("/d/",$acl)){
286 $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
287 }
288 }
290 /* Check acls for download icon */
291 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
292 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
293 $down = "";
294 }else{
295 $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
296 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
297 </a>";
298 }
300 /* Check if we are allowed to view this object */
301 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
302 if(preg_match("/r/",$s_acl)){
304 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$tmp[$key]."</a>";
305 $divlist->AddEntry(array( array("string"=> $edit_link),
306 array("string"=>$down , "attach" => "style='width:20px;'"),
307 array("string"=>str_replace("%s",base64_encode($key),$act),
308 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
309 }
310 }
311 $smarty->assign("Entry_divlist",$divlist->DrawList());
312 /* Divlist creation complete
313 */
315 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
316 * If we post the escaped strings they will be escaped again
317 */
318 foreach($this->attributes as $attrs){
319 if(get_magic_quotes_gpc()){
320 $smarty->assign($attrs,stripslashes($this->$attrs));
321 }else{
322 $smarty->assign($attrs,($this->$attrs));
323 }
324 }
326 $dn = $this->acl_base_for_current_object($this->dn);
327 $smarty->assign("sub_object_is_addable",
328 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
329 !preg_match("/freeze/",$this->FAIstate));
331 foreach($this->attributes as $attr){
332 $smarty->assign($attr."ACL",$this->getacl($attr, preg_match("/freeze/",$this->FAIstate)));
333 }
335 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
336 return($display);
337 }
340 function acl_base_for_current_object($dn)
341 {
342 if($dn == "new" || $dn == ""){
343 if($this->dn == "new"){
344 $dn = $dn= $this->parent->parent->acl_base;
345 }else{
346 $dn = $this->dn;
347 }
348 }
349 return($dn);
350 }
353 /* Generate listbox friendly SubObject list
354 */
355 function getList(){
356 $a_return=array();
357 foreach($this->SubObjects as $obj){
358 if($obj['status'] != "delete"){
360 if((isset($obj['description']))&&(!empty($obj['description']))){
361 if(strlen($obj['description']) > 40){
362 $obj['description'] = substr($obj['description'],0,40)."...";
363 }
364 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
365 }else{
366 $a_return[$obj['cn']]= $obj['cn'];
367 }
368 }
369 }
370 return($a_return);
371 }
373 /* Delete me, and all my subtrees
374 */
375 function remove_from_parent()
376 {
377 if($this->acl_is_removeable()){
378 $ldap = $this->config->get_ldap_link();
379 $ldap->cd ($this->dn);
380 $release = $this->parent->parent->fai_release;
381 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn, '/'))."/i", $release, $this->dn);
382 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
383 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
385 foreach($this->SubObjects as $name => $obj){
386 $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
387 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
388 }
389 $this->handle_post_events("remove");
390 }
391 }
394 /* Save data to object
395 */
396 function save_object()
397 {
398 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
399 plugin::save_object();
400 }
401 }
404 /* Check supplied data */
405 function check()
406 {
407 /* Call common method to give check the hook */
408 $message= plugin::check();
410 /* Ensure that we do not overwrite an allready existing entry
411 */
412 if($this->is_new){
413 $release = $this->parent->parent->fai_release;
414 $new_dn= 'cn='.$this->cn.",".get_ou('faiTemplateRDN').get_ou('faiBaseRDN').$release;
415 $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
416 if(isset($res[$this->cn])){
417 $message[] = msgPool::duplicated(_("Name"));
418 }
419 }
421 return ($message);
422 }
425 /* Save to LDAP */
426 function save()
427 {
428 plugin::save();
430 $ldap = $this->config->get_ldap_link();
432 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
434 if($this->initially_was_account){
435 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
436 }else{
437 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
438 }
440 /* Prepare FAIscriptEntry to write it to ldap
441 * First sort array.
442 * Because we must delete old entries first.
443 * After deletion, we perform add and modify
444 */
445 $Objects = array();
446 $tmp = $this->SubObjects;
447 foreach($tmp as $name => $obj){
448 if($obj['status'] == "FreshLoaded"){
449 unset($tmp[$name]);
450 }
451 }
453 foreach($tmp as $name => $obj){
454 if($obj['status'] == "delete"){
455 $Objects[$name] = $obj;
456 }
457 }
458 foreach($tmp as $name => $obj){
459 if($obj['status'] != "delete"){
460 $Objects[$name] = $obj;
461 }
462 }
464 foreach($Objects as $name => $obj){
466 foreach($this->sub64coded as $codeIt){
467 $obj[$codeIt]=base64_encode($obj[$codeIt]);
468 }
469 $tmp = array();
470 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
471 foreach($attributes as $attrs){
472 if(!isset($obj[$attrs]) || $obj[$attrs] == ""){
473 $obj[$attrs] = array();
474 }
475 $tmp[$attrs] =($obj[$attrs]);
476 }
478 $tmp['objectClass'] = $this->subClasses;
480 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
482 if($obj['status']=="new"){
483 $ldap->cat($sub_dn,array("objectClass"));
484 if($ldap->count()){
485 $obj['status']="edited";
486 }
487 }
489 /* Tag object */
490 $ui= get_userinfo();
491 $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
493 if($obj['status'] == "delete"){
494 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
495 $this->handle_post_events("remove");
496 }elseif($obj['status'] == "edited"){
497 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
498 $this->handle_post_events("modify");
499 }elseif($obj['status']=="new"){
500 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
501 $this->handle_post_events("add");
502 }
503 }
504 }
507 function PrepareForCopyPaste($source)
508 {
509 plugin::PrepareForCopyPaste($source);
511 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
512 */
513 $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
514 foreach($res as $obj){
516 /* Skip not relevant objects */
517 if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
519 $objects = array();
520 $objects['status'] = "edited";
521 $objects['dn'] = $obj['dn'];
522 $objects = $this->get_object_attributes($objects,$this->subAttributes);
523 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
524 $this->SubObjects[$objects['cn']] = $objects;
525 }
526 }
529 /* Return plugin informations for acl handling */
530 static function plInfo()
531 {
532 return (array(
533 "plShortName" => _("Template"),
534 "plDescription" => _("FAI template"),
535 "plSelfModify" => FALSE,
536 "plDepends" => array(),
537 "plPriority" => 24,
538 "plSection" => array("administration"),
539 "plCategory" => array("fai"),
540 "plProvidedAcls" => array(
541 "cn" => _("Name")." ("._("Readonly").")",
542 "description" => _("Description"))
543 ));
544 }
547 /*! \brief Used for copy & paste.
548 Returns a HTML input mask, which allows to change the cn of this entry.
549 @param Array Array containing current status && a HTML template.
550 */
551 function getCopyDialog()
552 {
553 $vars = array("cn");
554 $smarty = get_smarty();
555 $smarty->assign("cn", htmlentities($this->cn));
556 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
557 $ret = array();
558 $ret['string'] = $str;
559 $ret['status'] = "";
560 return($ret);
561 }
564 /*! \brief Used for copy & paste.
565 Some entries must be renamed to avaoid duplicate entries.
566 */
567 function saveCopyDialog()
568 {
569 if(isset($_POST['cn'])){
570 $this->cn = get_post('cn');
571 }
572 }
573 }
575 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
576 ?>