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['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['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['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['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 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
391 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
392 if($_SESSION['faifilter']['branch'] == "main"){
393 $use_dn = $this->dn;
394 }
396 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 = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
401 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
402 if($_SESSION['faifilter']['branch'] == "main"){
403 $use_dn = $obj['dn'];
404 }
405 prepare_to_save_FAI_object($use_dn,array(),true);
406 }
407 $this->handle_post_events("remove");
408 }
411 /* Save data to object
412 */
413 function save_object()
414 {
415 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
416 plugin::save_object();
417 }
418 }
421 /* Check supplied data */
422 function check()
423 {
424 /* Call common method to give check the hook */
425 $message= plugin::check();
427 return ($message);
428 }
431 /* Save to LDAP */
432 function save()
433 {
434 plugin::save();
436 $ldap = $this->config->get_ldap_link();
438 prepare_to_save_FAI_object($this->dn,$this->attrs);
439 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
441 if($this->initially_was_account){
442 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
443 }else{
444 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
445 }
447 /* Do object tagging */
448 $this->handle_object_tagging();
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 /* Check if gosaAdministrativeUnitTag is required as object class */
499 if($obj['status'] == "edited"){
500 $ldap->cat($sub_dn,array("objectClass"));
501 $attrs = $ldap->fetch();
502 if(isset($attrs['objectClass'])){
503 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
504 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
505 }
506 }
507 }
509 if($obj['status'] == "delete"){
510 prepare_to_save_FAI_object($sub_dn,array(),true);
511 $this->handle_post_events("remove");
512 }elseif($obj['status'] == "edited"){
513 prepare_to_save_FAI_object($sub_dn,$tmp);
514 $this->handle_post_events("modify");
515 }elseif($obj['status']=="new"){
516 prepare_to_save_FAI_object($sub_dn,$tmp);
517 $this->handle_post_events("add");
518 }
521 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
522 }
523 }
526 /* Return plugin informations for acl handling */
527 static function plInfo()
528 {
529 return (array(
530 "plShortName" => _("Template"),
531 "plDescription" => _("FAI template"),
532 "plSelfModify" => FALSE,
533 "plDepends" => array(),
534 "plPriority" => 24,
535 "plSection" => array("administration"),
536 "plCategory" => array("fai"),
537 "plProvidedAcls" => array(
538 "cn" => _("Name")." ("._("Readonly").")",
539 "description" => _("Description"))
540 ));
541 }
542 }
544 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
545 ?>