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 if (preg_match("/^-/", $this->cn)) {
422 $message[] = sprintf(_("The specified classname '%s' is invalid. Classnames must not start with a dash."), $this->cn);
423 }
425 if($this->cn != preg_replace("/ /","",trim($this->cn))){
426 $message[] = msgPool::invalid(_("Name"),preg_replace("/ /","_",$this->cn),"/[^_]/");
427 }
429 return ($message);
430 }
433 /* Save to LDAP */
434 function save()
435 {
436 plugin::save();
438 $ldap = $this->config->get_ldap_link();
440 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
442 if($this->initially_was_account){
443 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
444 }else{
445 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
446 }
448 /* Prepare FAIscriptEntry to write it to ldap
449 * First sort array.
450 * Because we must delete old entries first.
451 * After deletion, we perform add and modify
452 */
453 $Objects = array();
454 $tmp = $this->SubObjects;
455 foreach($tmp as $name => $obj){
456 if($obj['status'] == "FreshLoaded"){
457 unset($tmp[$name]);
458 }
459 }
461 foreach($tmp as $name => $obj){
462 if($obj['status'] == "delete"){
463 $Objects[$name] = $obj;
464 }
465 }
466 foreach($tmp as $name => $obj){
467 if($obj['status'] != "delete"){
468 $Objects[$name] = $obj;
469 }
470 }
472 foreach($Objects as $name => $obj){
474 foreach($this->sub64coded as $codeIt){
475 $obj[$codeIt]=base64_encode($obj[$codeIt]);
476 }
477 $tmp = array();
478 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
479 foreach($attributes as $attrs){
480 if(!isset($obj[$attrs]) || $obj[$attrs] == ""){
481 $obj[$attrs] = array();
482 }
483 $tmp[$attrs] =($obj[$attrs]);
484 }
486 $tmp['objectClass'] = $this->subClasses;
488 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
490 if($obj['status']=="new"){
491 $ldap->cat($sub_dn,array("objectClass"));
492 if($ldap->count()){
493 $obj['status']="edited";
494 }
495 }
497 /* Tag object */
498 $ui= get_userinfo();
499 $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
501 if($obj['status'] == "delete"){
502 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
503 $this->handle_post_events("remove");
504 }elseif($obj['status'] == "edited"){
505 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
506 $this->handle_post_events("modify");
507 }elseif($obj['status']=="new"){
508 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
509 $this->handle_post_events("add");
510 }
511 }
512 }
515 function PrepareForCopyPaste($source)
516 {
517 plugin::PrepareForCopyPaste($source);
519 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
520 */
521 $res = FAI::get_leaf_objects($source['dn'], $source['cn'][0], $this->subClass, get_ou('faiTemplateRDN'));
522 foreach($res as $obj){
523 $objects = array();
524 $objects['status'] = "edited";
525 $objects['dn'] = $obj['dn'];
526 $objects = $this->get_object_attributes($objects,$this->subAttributes);
527 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
528 $this->SubObjects[$objects['cn']] = $objects;
529 }
530 }
533 /* Return plugin informations for acl handling */
534 static function plInfo()
535 {
536 return (array(
537 "plShortName" => _("Template"),
538 "plDescription" => _("FAI template"),
539 "plSelfModify" => FALSE,
540 "plDepends" => array(),
541 "plPriority" => 24,
542 "plSection" => array("administration"),
543 "plCategory" => array("fai"),
544 "plProvidedAcls" => array(
545 "cn" => _("Name")." ("._("Readonly").")",
546 "description" => _("Description"))
547 ));
548 }
551 /*! \brief Used for copy & paste.
552 Returns a HTML input mask, which allows to change the cn of this entry.
553 @param Array Array containing current status && a HTML template.
554 */
555 function getCopyDialog()
556 {
557 $vars = array("cn");
558 $smarty = get_smarty();
559 $smarty->assign("cn", htmlentities($this->cn));
560 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
561 $ret = array();
562 $ret['string'] = $str;
563 $ret['status'] = "";
564 return($ret);
565 }
568 /*! \brief Used for copy & paste.
569 Some entries must be renamed to avaoid duplicate entries.
570 */
571 function saveCopyDialog()
572 {
573 if(isset($_POST['cn'])){
574 $this->cn = get_post('cn');
575 }
576 }
577 }
579 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
580 ?>