54bd8326728984c69c5e1cc89dc396f02a1301cd
1 <?php
3 class faiTemplate extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAItemplate");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAItemplateEntry";
21 var $subClasses = array("top","FAIclass","FAItemplateEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiTemplateEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAItemplatePath","FAIowner","FAImode");
28 var $sub_Load_Later = array("FAItemplateFile");
29 var $sub64coded = array();
30 var $subBinary = array("FAItemplateFile");
32 /* Specific attributes */
33 var $cn = ""; // The class name for this object
34 var $description = ""; // The description for this set of partitions
35 var $is_dialog = false; // specifies which buttons will be shown to save or abort
36 var $SubObjects = array(); // All leafobjects of this object
38 var $FAIstate = "";
39 var $ui;
40 var $view_logged = FALSE;
42 function faiTemplate (&$config, $dn= NULL)
43 {
44 /* Load Attributes */
45 plugin::plugin ($config, $dn);
47 /* If "dn==new" we try to create a new entry
48 * Else we must read all objects from ldap which belong to this entry.
49 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
50 */
51 if($dn != "new"){
52 $this->dn =$dn;
54 /* Get FAIstate
55 */
56 if(isset($this->attrs['FAIstate'][0])){
57 $this->FAIstate = $this->attrs['FAIstate'][0];
58 }
60 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
61 */
62 $ldap = $this->config->get_ldap_link();
63 $ldap->cd ($this->dn);
65 $attrs_to_search = $this->subAttributes;
66 $attrs_to_search[] = "FAIstate";
67 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
69 while($object = $ldap->fetch()){
71 /* Skip objects, that are tagged as removed */
72 if(isset($object['FAIstate'][0])){
73 if(preg_match("/removed$/",$object['FAIstate'][0])){
74 continue;
75 }
76 }
78 /* Set status for save management */
79 $objects = array();
80 $objects['status'] = "FreshLoaded";
81 $objects['dn'] = $object['dn'];
82 $objects = $this->get_object_attributes($objects,$this->subAttributes);
83 $this->SubObjects[$objects['cn']] = $objects;
84 }
85 }
86 $this->ui = get_userinfo();
87 }
90 /* Reload some attributes */
91 function get_object_attributes($object,$attributes)
92 {
93 $ldap = $this->config->get_ldap_link();
94 $ldap->cd($this->config->current['BASE']);
95 $ldap->cat($object['dn'],$attributes);
96 $tmp = $ldap->fetch();
98 foreach($attributes as $attrs){
99 if(isset($tmp[$attrs][0])){
100 $var = $tmp[$attrs][0];
102 /* Check if we must decode some attributes */
103 if(in_array_ics($attrs,$this->sub64coded)){
104 $var = base64_decode($var);
105 }
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 $var = stripslashes($var);
114 $object[$attrs] = $var;
115 }
116 }
117 return($object);
118 }
121 function execute()
122 {
123 /* Call parent execute */
124 plugin::execute();
126 if($this->is_account && !$this->view_logged){
127 $this->view_logged = TRUE;
128 new log("view","fai/".get_class($this),$this->dn);
129 }
131 /* Fill templating stuff */
132 $smarty= get_smarty();
133 $display= "";
135 /* New Listhandling
136 */
137 $once = true;
138 foreach($_POST as $name => $value){
139 if(preg_match("/^editscript_/",$name)&&($once)){
140 $once = false;
141 $entry = preg_replace("/^editscript_/","",$name);
142 $entry = base64_decode(preg_replace("/_.*/","",$entry));
143 $obj = $this->SubObjects[$entry];
145 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
146 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
147 if(preg_match("/r/",$acl)){
148 if($obj['status'] == "FreshLoaded"){
149 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
150 }
151 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
152 $this->dialog->set_acl_base($this->acl_base);
153 $this->dialog->set_acl_category("fai");
155 session::set('objectinfo',$obj['dn']);
156 $this->dialog->parent = &$this;
157 $this->is_dialog=true;
158 }
159 }
160 if(preg_match("/^deletescript_/",$name)&&($once)){
161 $once = false;
162 $entry = preg_replace("/^deletescript_/","",$name);
163 $entry = base64_decode(preg_replace("/_.*/","",$entry));
164 $obj = $this->SubObjects[$entry];
166 $acl_dn = $this->acl_base_for_current_object($obj['dn']);
167 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
168 if(preg_match("/d/",$acl)){
169 $status = $this->SubObjects[$entry]['status'];
170 if($status == "edited" || $status == "FreshLoaded"){
171 $this->SubObjects[$entry]['status']= "delete";
172 }else{
173 unset($this->SubObjects[$entry]);
174 }
175 }
176 }
177 }
179 /* Edit entries via GET */
180 if(isset($_GET['act']) && isset($_GET['id'])){
181 if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
182 $obj = $this->SubObjects[$_GET['id']];
183 if($obj['status'] == "FreshLoaded"){
184 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
185 }
186 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
187 $this->dialog->acl = $this->acl;
188 session::set('objectinfo',$obj['dn']);
189 $this->dialog->parent = &$this;
190 $this->is_dialog=true;
191 }
192 }
194 /* Add new sub object */
195 if(isset($_POST['AddSubObject'])){
196 $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
197 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
199 if(preg_match("/c/",$acl)){
200 $this->dialog= new $this->subClassName($this->config,"new");
201 $this->dialog->set_acl_base($this->acl_base);
202 $this->dialog->set_acl_category("fai");
203 $this->dialog->parent = &$this;
204 $this->is_dialog=true;
205 }
206 }
208 if($this->dn != "new"){
209 session::set('objectinfo',$this->dn);
210 }
212 /* Save Dialog */
213 if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
214 $this->dialog->save_object();
215 $msgs = $this->dialog->check();
216 if(count($msgs)>0){
217 foreach($msgs as $msg){
218 print_red($msg);
219 }
220 }else{
221 /* Get return object */
222 $obj = $this->dialog->save();
223 if(isset($obj['remove'])){
225 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
227 /* Depending on status, set new status */
228 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
229 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
230 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
231 unset($this->SubObjects[$obj['remove']['from']]);
232 }
233 $obj['status'] = "new";
234 $this->SubObjects[$obj['remove']['to']] = $obj;
235 unset($this->SubObjects[$obj['remove']['to']]['remove']);
236 }else{
237 if($obj['status'] == "FreshLoaded"){
238 $obj['status'] = "edited";
239 }
240 $this->SubObjects[$obj['cn']]=$obj;
241 }
242 $this->is_dialog=false;
243 unset($this->dialog);
244 $this->dialog=FALSE;
245 }
246 }
248 /* Sort entries */
249 $tmp = $keys = array();
250 foreach($this->SubObjects as $key => $entry){
251 $keys[$key]=$key;
252 }
253 natcasesort($keys);
254 foreach($keys as $key){
255 $tmp[$key]=$this->SubObjects[$key];
256 }
257 $this->SubObjects = $tmp;
259 /* Cancel Dialog */
260 if(isset($_POST['CancelSubObject'])){
261 $this->is_dialog=false;
262 unset($this->dialog);
263 $this->dialog=FALSE;
264 }
266 /* Print dialog if $this->dialog is set */
267 if(is_object($this->dialog)){
268 $this->dialog->save_object();
269 $display = $this->dialog->execute();
270 return($display);
271 }
273 /* Divlist Containing FAItemplates */
274 $divlist = new divSelectBox("FAItemplates");
275 $divlist->setHeight(400);
277 $tmp = $this->getList(true);
279 /* Create div list with all sub entries listed */
280 foreach($this->SubObjects as $key => $name){
282 /* Skip removed entries */
283 if($name['status'] == "delete") continue;
285 /* Get permissions */
286 $dn = $this->acl_base_for_current_object($name['dn']);
287 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
288 $act = "";
290 /* Hide delete icon if this object is freezed */
291 if(preg_match("/freeze/",$this->FAIstate)){
292 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
293 }else{
294 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
295 if(preg_match("/d/",$acl)){
296 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
297 }
298 }
300 /* Check acls for download icon */
301 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
302 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
303 $down = "";
304 }else{
305 $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
306 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
307 </a>";
308 }
310 /* Check if we are allowed to view this object */
311 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
312 if(preg_match("/r/",$s_acl)){
314 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".$key."'>".$tmp[$key]."</a>";
315 $divlist->AddEntry(array( array("string"=> $edit_link),
316 array("string"=>$down , "attach" => "style='width:20px;'"),
317 array("string"=>str_replace("%s",base64_encode($key),$act),
318 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
319 }
320 }
321 $smarty->assign("Entry_divlist",$divlist->DrawList());
322 /* Divlist creation complete
323 */
325 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
326 * If we post the escaped strings they will be escaped again
327 */
328 foreach($this->attributes as $attrs){
329 if(get_magic_quotes_gpc()){
330 $smarty->assign($attrs,stripslashes($this->$attrs));
331 }else{
332 $smarty->assign($attrs,($this->$attrs));
333 }
334 }
336 $dn = $this->acl_base_for_current_object($this->dn);
337 $smarty->assign("sub_object_is_addable",
338 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
339 !preg_match("/freeze/",$this->FAIstate));
341 foreach($this->attributes as $attr){
342 $smarty->assign($attr."ACL",$this->getacl($attr));
343 }
345 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
346 return($display);
347 }
350 function acl_base_for_current_object($dn)
351 {
352 if($dn == "new"){
353 if($this->dn == "new"){
354 $dn = session::get('CurrentMainBase');
355 }else{
356 $dn = $this->dn;
357 }
358 }
359 return($dn);
360 }
363 /* Generate listbox friendly SubObject list
364 */
365 function getList(){
366 $a_return=array();
367 foreach($this->SubObjects as $obj){
368 if($obj['status'] != "delete"){
370 if((isset($obj['description']))&&(!empty($obj['description']))){
371 if(strlen($obj['description']) > 40){
372 $obj['description'] = substr($obj['description'],0,40)."...";
373 }
374 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
375 }else{
376 $a_return[$obj['cn']]= $obj['cn'];
377 }
378 }
379 }
380 return($a_return);
381 }
383 /* Delete me, and all my subtrees
384 */
385 function remove_from_parent()
386 {
387 $ldap = $this->config->get_ldap_link();
388 $ldap->cd ($this->dn);
390 $faifilter = session::get('faifilter');
391 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
392 if($faifilter['branch'] == "main"){
393 $use_dn = $this->dn;
394 }
396 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
397 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
399 foreach($this->SubObjects as $name => $obj){
400 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
401 if($faifilter['branch'] == "main"){
402 $use_dn = $obj['dn'];
403 }
404 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
405 }
406 $this->handle_post_events("remove");
407 }
410 /* Save data to object
411 */
412 function save_object()
413 {
414 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
415 plugin::save_object();
416 }
417 }
420 /* Check supplied data */
421 function check()
422 {
423 /* Call common method to give check the hook */
424 $message= plugin::check();
426 return ($message);
427 }
430 /* Save to LDAP */
431 function save()
432 {
433 plugin::save();
435 $ldap = $this->config->get_ldap_link();
437 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
438 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
440 if($this->initially_was_account){
441 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
442 }else{
443 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
444 }
446 /* Prepare FAIscriptEntry to write it to ldap
447 * First sort array.
448 * Because we must delete old entries first.
449 * After deletion, we perform add and modify
450 */
451 $Objects = array();
452 foreach($this->SubObjects as $name => $obj){
453 if($obj['status'] == "FreshLoaded"){
454 unset($this->SubObjects[$name]);
455 }
456 }
458 foreach($this->SubObjects as $name => $obj){
459 if($obj['status'] == "delete"){
460 $Objects[$name] = $obj;
461 }
462 }
463 foreach($this->SubObjects as $name => $obj){
464 if($obj['status'] != "delete"){
465 $Objects[$name] = $obj;
466 }
467 }
469 foreach($Objects as $name => $obj){
471 foreach($this->sub64coded as $codeIt){
472 $obj[$codeIt]=base64_encode($obj[$codeIt]);
473 }
474 $tmp = array();
475 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
476 foreach($attributes as $attrs){
477 if(empty($obj[$attrs])){
478 $obj[$attrs] = array();
479 }
480 $tmp[$attrs] =($obj[$attrs]);
481 }
483 $tmp['objectClass'] = $this->subClasses;
485 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
487 if($obj['status']=="new"){
488 $ldap->cat($sub_dn,array("objectClass"));
489 if($ldap->count()){
490 $obj['status']="edited";
491 }
492 }
494 /* Tag object */
495 $this->tag_attrs(&$tmp, $sub_dn, $this->gosaUnitTag);
497 if($obj['status'] == "delete"){
498 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
499 $this->handle_post_events("remove");
500 }elseif($obj['status'] == "edited"){
501 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
502 $this->handle_post_events("modify");
503 }elseif($obj['status']=="new"){
504 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
505 $this->handle_post_events("add");
506 }
508 }
509 }
512 function PrepareForCopyPaste($source)
513 {
514 plugin::PrepareForCopyPaste($source);
516 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
517 */
518 $ldap = $this->config->get_ldap_link();
519 $ldap->cd ($source['dn']);
521 $attrs_to_search = $this->subAttributes;
522 $attrs_to_search[] = "FAIstate";
523 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
525 while($object = $ldap->fetch()){
527 /* Skip objects, that are tagged as removed */
528 if(isset($object['FAIstate'][0])){
529 if(preg_match("/removed$/",$object['FAIstate'][0])){
530 continue;
531 }
532 }
534 /* Set status for save management */
535 $objects = array();
536 $objects['status'] = "edited";
537 $objects['dn'] = $object['dn'];
538 $objects = $this->get_object_attributes($objects,$this->subAttributes);
539 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
541 $this->SubObjects[$objects['cn']] = $objects;
542 }
543 }
546 /* Return plugin informations for acl handling */
547 static function plInfo()
548 {
549 return (array(
550 "plShortName" => _("Template"),
551 "plDescription" => _("FAI template"),
552 "plSelfModify" => FALSE,
553 "plDepends" => array(),
554 "plPriority" => 24,
555 "plSection" => array("administration"),
556 "plCategory" => array("fai"),
557 "plProvidedAcls" => array(
558 "cn" => _("Name")." ("._("Readonly").")",
559 "description" => _("Description"))
560 ));
561 }
562 }
564 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
565 ?>