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 }
106 /* check if this is a binary entry */
107 if(in_array_ics($attrs,$this->subBinary)){
108 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
109 }
111 $var = stripslashes($var);
113 $object[$attrs] = $var;
114 }
115 }
116 return($object);
117 }
120 function execute()
121 {
122 /* Call parent execute */
123 plugin::execute();
125 if($this->is_account && !$this->view_logged){
126 $this->view_logged = TRUE;
127 new log("view","fai/".get_class($this),$this->dn);
128 }
130 /* Fill templating stuff */
131 $smarty= get_smarty();
132 $display= "";
134 /* New Listhandling
135 */
136 $once = true;
137 foreach($_POST as $name => $value){
138 if(preg_match("/^editscript_/",$name)&&($once)){
139 $once = false;
140 $entry = preg_replace("/^editscript_/","",$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("/r/",$acl)){
147 if($obj['status'] == "FreshLoaded"){
148 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
149 }
150 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
151 $this->dialog->set_acl_base($this->acl_base);
152 $this->dialog->set_acl_category("fai");
154 session::set('objectinfo',$obj['dn']);
155 $this->dialog->parent = &$this;
156 $this->is_dialog=true;
157 }
158 }
159 if(preg_match("/^deletescript_/",$name)&&($once)){
160 $once = false;
161 $entry = preg_replace("/^deletescript_/","",$name);
162 $entry = base64_decode(preg_replace("/_.*/","",$entry));
163 $obj = $this->SubObjects[$entry];
165 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
166 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
167 if(preg_match("/d/",$acl)){
168 $status = $this->SubObjects[$entry]['status'];
169 if($status == "edited" || $status == "FreshLoaded"){
170 $this->SubObjects[$entry]['status']= "delete";
171 }else{
172 unset($this->SubObjects[$entry]);
173 }
174 }
175 }
176 }
178 /* File download requested */
179 if(isset($_GET['getFAItemplate'])){
180 if(isset($this->SubObjects[$_GET['getFAItemplate']])){
181 $obj = $this->SubObjects[$_GET['getFAItemplate']];
182 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
183 send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
184 }
185 }
187 /* Edit entries via GET */
188 if(isset($_GET['act']) && isset($_GET['id'])){
189 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
190 $obj = $this->SubObjects[$_GET['id']];
191 if($obj['status'] == "FreshLoaded"){
192 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
193 }
194 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
195 $this->dialog->acl = $this->acl;
196 session::set('objectinfo',$obj['dn']);
197 $this->dialog->parent = &$this;
198 $this->is_dialog=true;
199 }
200 }
202 /* Add new sub object */
203 if(isset($_POST['AddSubObject'])){
204 $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
205 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
207 if(preg_match("/c/",$acl)){
208 $this->dialog= new $this->subClassName($this->config,"new");
209 $this->dialog->set_acl_base($this->acl_base);
210 $this->dialog->set_acl_category("fai");
211 $this->dialog->parent = &$this;
212 $this->is_dialog=true;
213 }
214 }
216 if($this->dn != "new"){
217 session::set('objectinfo',$this->dn);
218 }
220 /* Save Dialog */
221 if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
222 $this->dialog->save_object();
223 $msgs = $this->dialog->check();
224 if(count($msgs)>0){
225 foreach($msgs as $msg){
226 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
227 }
228 }else{
229 /* Get return object */
230 $obj = $this->dialog->save();
231 if(isset($obj['remove'])){
233 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
235 /* Depending on status, set new status */
236 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
237 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
238 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
239 unset($this->SubObjects[$obj['remove']['from']]);
240 }
241 $obj['status'] = "new";
242 $this->SubObjects[$obj['remove']['to']] = $obj;
243 unset($this->SubObjects[$obj['remove']['to']]['remove']);
244 }else{
245 if($obj['status'] == "FreshLoaded"){
246 $obj['status'] = "edited";
247 }
248 $this->SubObjects[$obj['cn']]=$obj;
249 }
250 $this->is_dialog=false;
251 unset($this->dialog);
252 $this->dialog=FALSE;
253 }
254 }
256 /* Sort entries */
257 $tmp = $keys = array();
258 foreach($this->SubObjects as $key => $entry){
259 $keys[$key]=$key;
260 }
261 natcasesort($keys);
262 foreach($keys as $key){
263 $tmp[$key]=$this->SubObjects[$key];
264 }
265 $this->SubObjects = $tmp;
267 /* Cancel Dialog */
268 if(isset($_POST['CancelSubObject'])){
269 $this->is_dialog=false;
270 unset($this->dialog);
271 $this->dialog=FALSE;
272 }
274 /* Print dialog if $this->dialog is set */
275 if(is_object($this->dialog)){
276 $this->dialog->save_object();
277 $display = $this->dialog->execute();
278 return($display);
279 }
281 /* Divlist Containing FAItemplates */
282 $divlist = new divSelectBox("FAItemplates");
283 $divlist->setHeight(400);
285 $tmp = $this->getList(true);
287 /* Create div list with all sub entries listed */
288 foreach($this->SubObjects as $key => $name){
290 /* Skip removed entries */
291 if($name['status'] == "delete") continue;
293 /* Get permissions */
294 $dn = $this->acl_base_for_current_object($name['dn']);
295 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
296 $act = "";
298 /* Hide delete icon if this object is freezed */
299 if(preg_match("/freeze/",$this->FAIstate)){
300 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
301 }else{
302 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
303 if(preg_match("/d/",$acl)){
304 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
305 }
306 }
308 /* Check acls for download icon */
309 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
310 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
311 $down = "";
312 }else{
313 $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
314 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
315 </a>";
316 }
318 /* Check if we are allowed to view this object */
319 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
320 if(preg_match("/r/",$s_acl)){
322 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$tmp[$key]."</a>";
323 $divlist->AddEntry(array( array("string"=> $edit_link),
324 array("string"=>$down , "attach" => "style='width:20px;'"),
325 array("string"=>str_replace("%s",base64_encode($key),$act),
326 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
327 }
328 }
329 $smarty->assign("Entry_divlist",$divlist->DrawList());
330 /* Divlist creation complete
331 */
333 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
334 * If we post the escaped strings they will be escaped again
335 */
336 foreach($this->attributes as $attrs){
337 if(get_magic_quotes_gpc()){
338 $smarty->assign($attrs,stripslashes($this->$attrs));
339 }else{
340 $smarty->assign($attrs,($this->$attrs));
341 }
342 }
344 $dn = $this->acl_base_for_current_object($this->dn);
345 $smarty->assign("sub_object_is_addable",
346 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
347 !preg_match("/freeze/",$this->FAIstate));
349 foreach($this->attributes as $attr){
350 $smarty->assign($attr."ACL",$this->getacl($attr));
351 }
353 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
354 return($display);
355 }
358 function acl_base_for_current_object($dn)
359 {
360 if($dn == "new"){
361 if($this->dn == "new"){
362 $dn = session::get('CurrentMainBase');
363 }else{
364 $dn = $this->dn;
365 }
366 }
367 return($dn);
368 }
371 /* Generate listbox friendly SubObject list
372 */
373 function getList(){
374 $a_return=array();
375 foreach($this->SubObjects as $obj){
376 if($obj['status'] != "delete"){
378 if((isset($obj['description']))&&(!empty($obj['description']))){
379 if(strlen($obj['description']) > 40){
380 $obj['description'] = substr($obj['description'],0,40)."...";
381 }
382 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
383 }else{
384 $a_return[$obj['cn']]= $obj['cn'];
385 }
386 }
387 }
388 return($a_return);
389 }
391 /* Delete me, and all my subtrees
392 */
393 function remove_from_parent()
394 {
395 $ldap = $this->config->get_ldap_link();
396 $ldap->cd ($this->dn);
398 $faifilter = session::get('faifilter');
399 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
400 if($faifilter['branch'] == "main"){
401 $use_dn = $this->dn;
402 }
404 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
405 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
407 foreach($this->SubObjects as $name => $obj){
408 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
409 if($faifilter['branch'] == "main"){
410 $use_dn = $obj['dn'];
411 }
412 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
413 }
414 $this->handle_post_events("remove");
415 }
418 /* Save data to object
419 */
420 function save_object()
421 {
422 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
423 plugin::save_object();
424 }
425 }
428 /* Check supplied data */
429 function check()
430 {
431 /* Call common method to give check the hook */
432 $message= plugin::check();
434 return ($message);
435 }
438 /* Save to LDAP */
439 function save()
440 {
441 plugin::save();
443 $ldap = $this->config->get_ldap_link();
445 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
446 if (!$ldap->success()){
447 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
448 }
450 if($this->initially_was_account){
451 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
452 }else{
453 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
454 }
456 /* Prepare FAIscriptEntry to write it to ldap
457 * First sort array.
458 * Because we must delete old entries first.
459 * After deletion, we perform add and modify
460 */
461 $Objects = array();
462 foreach($this->SubObjects as $name => $obj){
463 if($obj['status'] == "FreshLoaded"){
464 unset($this->SubObjects[$name]);
465 }
466 }
468 foreach($this->SubObjects as $name => $obj){
469 if($obj['status'] == "delete"){
470 $Objects[$name] = $obj;
471 }
472 }
473 foreach($this->SubObjects as $name => $obj){
474 if($obj['status'] != "delete"){
475 $Objects[$name] = $obj;
476 }
477 }
479 foreach($Objects as $name => $obj){
481 foreach($this->sub64coded as $codeIt){
482 $obj[$codeIt]=base64_encode($obj[$codeIt]);
483 }
484 $tmp = array();
485 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
486 foreach($attributes as $attrs){
487 if(empty($obj[$attrs])){
488 $obj[$attrs] = array();
489 }
490 $tmp[$attrs] =($obj[$attrs]);
491 }
493 $tmp['objectClass'] = $this->subClasses;
495 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
497 if($obj['status']=="new"){
498 $ldap->cat($sub_dn,array("objectClass"));
499 if($ldap->count()){
500 $obj['status']="edited";
501 }
502 }
504 /* Tag object */
505 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
507 if($obj['status'] == "delete"){
508 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
509 $this->handle_post_events("remove");
510 }elseif($obj['status'] == "edited"){
511 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
512 $this->handle_post_events("modify");
513 }elseif($obj['status']=="new"){
514 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
515 $this->handle_post_events("add");
516 }
518 }
519 }
522 function PrepareForCopyPaste($source)
523 {
524 plugin::PrepareForCopyPaste($source);
526 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
527 */
528 $ldap = $this->config->get_ldap_link();
529 $ldap->cd ($source['dn']);
531 $attrs_to_search = $this->subAttributes;
532 $attrs_to_search[] = "FAIstate";
533 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
535 while($object = $ldap->fetch()){
537 /* Skip objects, that are tagged as removed */
538 if(isset($object['FAIstate'][0])){
539 if(preg_match("/removed$/",$object['FAIstate'][0])){
540 continue;
541 }
542 }
544 /* Set status for save management */
545 $objects = array();
546 $objects['status'] = "edited";
547 $objects['dn'] = $object['dn'];
548 $objects = $this->get_object_attributes($objects,$this->subAttributes);
549 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
551 $this->SubObjects[$objects['cn']] = $objects;
552 }
553 }
556 /* Return plugin informations for acl handling */
557 static function plInfo()
558 {
559 return (array(
560 "plShortName" => _("Template"),
561 "plDescription" => _("FAI template"),
562 "plSelfModify" => FALSE,
563 "plDepends" => array(),
564 "plPriority" => 24,
565 "plSection" => array("administration"),
566 "plCategory" => array("fai"),
567 "plProvidedAcls" => array(
568 "cn" => _("Name")." ("._("Readonly").")",
569 "description" => _("Description"))
570 ));
571 }
572 }
574 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
575 ?>