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