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