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 /* Get FAIstate
50 */
51 if(isset($this->attrs['FAIstate'][0])){
52 $this->FAIstate = $this->attrs['FAIstate'][0];
53 }
55 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
56 */
57 $ldap = $this->config->get_ldap_link();
58 $ldap->cd ($this->dn);
60 $attrs_to_search = $this->subAttributes;
61 $attrs_to_search[] = "FAIstate";
62 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
64 $data = array();
65 while($object = $ldap->fetch()){
66 $data[] = $object;
67 }
68 foreach($data as $object){
70 /* Skip objects, that are tagged as removed */
71 if(isset($object['FAIstate'][0])){
72 if(preg_match("/removed$/",$object['FAIstate'][0])){
73 continue;
74 }
75 }
77 /* Set status for save management */
78 $objects = array();
79 $objects['status'] = "FreshLoaded";
80 $objects['dn'] = $object['dn'];
81 $objects = $this->get_object_attributes($objects,$this->subAttributes);
82 $this->SubObjects[$objects['cn']] = $objects;
83 }
84 }
85 $this->ui = get_userinfo();
86 }
89 /* Reload some attributes */
90 function get_object_attributes($object,$attributes)
91 {
92 $ldap = $this->config->get_ldap_link();
93 $ldap->cd($this->config->current['BASE']);
94 $ldap->cat($object['dn'],$attributes);
95 $tmp = $ldap->fetch();
97 foreach($attributes as $attrs){
98 if(isset($tmp[$attrs][0])){
99 $var = $tmp[$attrs][0];
101 /* Check if we must decode some attributes */
102 if(in_array_ics($attrs,$this->sub64coded)){
103 $var = base64_decode($var);
104 }
105 $var = stripslashes($var);
107 /* check if this is a binary entry */
108 if(in_array_ics($attrs,$this->subBinary)){
109 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
110 }
112 $object[$attrs] = $var;
113 }
114 }
115 return($object);
116 }
119 function execute()
120 {
121 /* Call parent execute */
122 plugin::execute();
124 if($this->is_account && !$this->view_logged){
125 $this->view_logged = TRUE;
126 new log("view","fai/".get_class($this),$this->dn);
127 }
129 /* Fill templating stuff */
130 $smarty= get_smarty();
131 $display= "";
133 /* New Listhandling
134 */
135 $once = true;
136 foreach($_POST as $name => $value){
137 if(preg_match("/^editscript_/",$name)&&($once)){
138 $once = false;
139 $entry = preg_replace("/^editscript_/","",$name);
140 $entry = base64_decode(preg_replace("/_.*/","",$entry));
141 $obj = $this->SubObjects[$entry];
143 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
144 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
145 if(preg_match("/r/",$acl)){
146 if($obj['status'] == "FreshLoaded"){
147 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
148 }
149 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
150 $this->dialog->set_acl_base($this->acl_base);
151 $this->dialog->set_acl_category("fai");
153 session::set('objectinfo',$obj['dn']);
154 $this->dialog->parent = &$this;
155 $this->is_dialog=true;
156 }
157 }
158 if(preg_match("/^deletescript_/",$name)&&($once)){
159 $once = false;
160 $entry = preg_replace("/^deletescript_/","",$name);
161 $entry = base64_decode(preg_replace("/_.*/","",$entry));
162 $obj = $this->SubObjects[$entry];
164 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
165 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
166 if(preg_match("/d/",$acl)){
167 $status = $this->SubObjects[$entry]['status'];
168 if($status == "edited" || $status == "FreshLoaded"){
169 $this->SubObjects[$entry]['status']= "delete";
170 }else{
171 unset($this->SubObjects[$entry]);
172 }
173 }
174 }
175 }
177 /* File download requested */
178 if(isset($_GET['getFAItemplate'])){
179 if(isset($this->SubObjects[$_GET['getFAItemplate']])){
180 $obj = $this->SubObjects[$_GET['getFAItemplate']];
181 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
182 send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
183 }
184 }
186 /* Edit entries via GET */
187 if(isset($_GET['act']) && isset($_GET['id'])){
188 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
189 $obj = $this->SubObjects[$_GET['id']];
190 if($obj['status'] == "FreshLoaded"){
191 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
192 }
193 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
194 $this->dialog->acl = $this->acl;
195 session::set('objectinfo',$obj['dn']);
196 $this->dialog->parent = &$this;
197 $this->is_dialog=true;
198 }
199 }
201 /* Add new sub object */
202 if(isset($_POST['AddSubObject'])){
203 $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
204 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
206 if(preg_match("/c/",$acl)){
207 $this->dialog= new $this->subClassName($this->config,"new");
208 $this->dialog->set_acl_base($this->acl_base);
209 $this->dialog->set_acl_category("fai");
210 $this->dialog->parent = &$this;
211 $this->is_dialog=true;
212 }
213 }
215 if($this->dn != "new"){
216 session::set('objectinfo',$this->dn);
217 }
219 /* Save Dialog */
220 if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
221 $this->dialog->save_object();
222 $msgs = $this->dialog->check();
223 if(count($msgs)>0){
224 foreach($msgs as $msg){
225 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
226 }
227 }else{
228 /* Get return object */
229 $obj = $this->dialog->save();
230 if(isset($obj['remove'])){
232 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
234 /* Depending on status, set new status */
235 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
236 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
237 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
238 unset($this->SubObjects[$obj['remove']['from']]);
239 }
240 $obj['status'] = "new";
241 $this->SubObjects[$obj['remove']['to']] = $obj;
242 unset($this->SubObjects[$obj['remove']['to']]['remove']);
243 }else{
244 if($obj['status'] == "FreshLoaded"){
245 $obj['status'] = "edited";
246 }
247 $this->SubObjects[$obj['cn']]=$obj;
248 }
249 $this->is_dialog=false;
250 unset($this->dialog);
251 $this->dialog=FALSE;
252 }
253 }
255 /* Sort entries */
256 $tmp = $keys = array();
257 foreach($this->SubObjects as $key => $entry){
258 $keys[$key]=$key;
259 }
260 natcasesort($keys);
261 foreach($keys as $key){
262 $tmp[$key]=$this->SubObjects[$key];
263 }
264 $this->SubObjects = $tmp;
266 /* Cancel Dialog */
267 if(isset($_POST['CancelSubObject'])){
268 $this->is_dialog=false;
269 unset($this->dialog);
270 $this->dialog=FALSE;
271 }
273 /* Print dialog if $this->dialog is set */
274 if(is_object($this->dialog)){
275 $this->dialog->save_object();
276 $display = $this->dialog->execute();
277 return($display);
278 }
280 /* Divlist Containing FAItemplates */
281 $divlist = new divSelectBox("FAItemplates");
282 $divlist->setHeight(400);
284 $tmp = $this->getList(true);
286 /* Create div list with all sub entries listed */
287 foreach($this->SubObjects as $key => $name){
289 /* Skip removed entries */
290 if($name['status'] == "delete") continue;
292 /* Get permissions */
293 $dn = $this->acl_base_for_current_object($name['dn']);
294 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
295 $act = "";
297 /* Hide delete icon if this object is freezed */
298 if(preg_match("/freeze/",$this->FAIstate)){
299 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
300 }else{
301 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
302 if(preg_match("/d/",$acl)){
303 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
304 }
305 }
307 /* Check acls for download icon */
308 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
309 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
310 $down = "";
311 }else{
312 $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
313 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
314 </a>";
315 }
317 /* Check if we are allowed to view this object */
318 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
319 if(preg_match("/r/",$s_acl)){
321 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$tmp[$key]."</a>";
322 $divlist->AddEntry(array( array("string"=> $edit_link),
323 array("string"=>$down , "attach" => "style='width:20px;'"),
324 array("string"=>str_replace("%s",base64_encode($key),$act),
325 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
326 }
327 }
328 $smarty->assign("Entry_divlist",$divlist->DrawList());
329 /* Divlist creation complete
330 */
332 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
333 * If we post the escaped strings they will be escaped again
334 */
335 foreach($this->attributes as $attrs){
336 if(get_magic_quotes_gpc()){
337 $smarty->assign($attrs,stripslashes($this->$attrs));
338 }else{
339 $smarty->assign($attrs,($this->$attrs));
340 }
341 }
343 $dn = $this->acl_base_for_current_object($this->dn);
344 $smarty->assign("sub_object_is_addable",
345 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
346 !preg_match("/freeze/",$this->FAIstate));
348 foreach($this->attributes as $attr){
349 $smarty->assign($attr."ACL",$this->getacl($attr));
350 }
352 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
353 return($display);
354 }
357 function acl_base_for_current_object($dn)
358 {
359 if($dn == "new"){
360 if($this->dn == "new"){
361 $dn = session::get('CurrentMainBase');
362 }else{
363 $dn = $this->dn;
364 }
365 }
366 return($dn);
367 }
370 /* Generate listbox friendly SubObject list
371 */
372 function getList(){
373 $a_return=array();
374 foreach($this->SubObjects as $obj){
375 if($obj['status'] != "delete"){
377 if((isset($obj['description']))&&(!empty($obj['description']))){
378 if(strlen($obj['description']) > 40){
379 $obj['description'] = substr($obj['description'],0,40)."...";
380 }
381 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
382 }else{
383 $a_return[$obj['cn']]= $obj['cn'];
384 }
385 }
386 }
387 return($a_return);
388 }
390 /* Delete me, and all my subtrees
391 */
392 function remove_from_parent()
393 {
394 $ldap = $this->config->get_ldap_link();
395 $ldap->cd ($this->dn);
397 $faifilter = session::get('faifilter');
398 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
399 if($faifilter['branch'] == "main"){
400 $use_dn = $this->dn;
401 }
403 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
404 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
406 foreach($this->SubObjects as $name => $obj){
407 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
408 if($faifilter['branch'] == "main"){
409 $use_dn = $obj['dn'];
410 }
411 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
412 }
413 $this->handle_post_events("remove");
414 }
417 /* Save data to object
418 */
419 function save_object()
420 {
421 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
422 plugin::save_object();
423 }
424 }
427 /* Check supplied data */
428 function check()
429 {
430 /* Call common method to give check the hook */
431 $message= plugin::check();
433 return ($message);
434 }
437 /* Save to LDAP */
438 function save()
439 {
440 plugin::save();
442 $ldap = $this->config->get_ldap_link();
444 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
446 if($this->initially_was_account){
447 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
448 }else{
449 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
450 }
452 /* Prepare FAIscriptEntry to write it to ldap
453 * First sort array.
454 * Because we must delete old entries first.
455 * After deletion, we perform add and modify
456 */
457 $Objects = array();
458 foreach($this->SubObjects as $name => $obj){
459 if($obj['status'] == "FreshLoaded"){
460 unset($this->SubObjects[$name]);
461 }
462 }
464 foreach($this->SubObjects as $name => $obj){
465 if($obj['status'] == "delete"){
466 $Objects[$name] = $obj;
467 }
468 }
469 foreach($this->SubObjects as $name => $obj){
470 if($obj['status'] != "delete"){
471 $Objects[$name] = $obj;
472 }
473 }
475 foreach($Objects as $name => $obj){
477 foreach($this->sub64coded as $codeIt){
478 $obj[$codeIt]=base64_encode($obj[$codeIt]);
479 }
480 $tmp = array();
481 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
482 foreach($attributes as $attrs){
483 if(empty($obj[$attrs])){
484 $obj[$attrs] = array();
485 }
486 $tmp[$attrs] =($obj[$attrs]);
487 }
489 $tmp['objectClass'] = $this->subClasses;
491 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
493 if($obj['status']=="new"){
494 $ldap->cat($sub_dn,array("objectClass"));
495 if($ldap->count()){
496 $obj['status']="edited";
497 }
498 }
500 /* Tag object */
501 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
503 if($obj['status'] == "delete"){
504 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
505 $this->handle_post_events("remove");
506 }elseif($obj['status'] == "edited"){
507 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
508 $this->handle_post_events("modify");
509 }elseif($obj['status']=="new"){
510 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
511 $this->handle_post_events("add");
512 }
514 }
515 }
518 function PrepareForCopyPaste($source)
519 {
520 plugin::PrepareForCopyPaste($source);
522 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
523 */
524 $ldap = $this->config->get_ldap_link();
525 $ldap->cd ($source['dn']);
527 $attrs_to_search = $this->subAttributes;
528 $attrs_to_search[] = "FAIstate";
529 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
531 while($object = $ldap->fetch()){
533 /* Skip objects, that are tagged as removed */
534 if(isset($object['FAIstate'][0])){
535 if(preg_match("/removed$/",$object['FAIstate'][0])){
536 continue;
537 }
538 }
540 /* Set status for save management */
541 $objects = array();
542 $objects['status'] = "edited";
543 $objects['dn'] = $object['dn'];
544 $objects = $this->get_object_attributes($objects,$this->subAttributes);
545 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
547 $this->SubObjects[$objects['cn']] = $objects;
548 }
549 }
552 /* Return plugin informations for acl handling */
553 static function plInfo()
554 {
555 return (array(
556 "plShortName" => _("Template"),
557 "plDescription" => _("FAI template"),
558 "plSelfModify" => FALSE,
559 "plDepends" => array(),
560 "plPriority" => 24,
561 "plSection" => array("administration"),
562 "plCategory" => array("fai"),
563 "plProvidedAcls" => array(
564 "cn" => _("Name")." ("._("Readonly").")",
565 "description" => _("Description"))
566 ));
567 }
568 }
570 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
571 ?>