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 $dialog = NULL; // a dialog, e.g. new disk dialog
37 var $SubObjects = array(); // All leafobjects of this object
39 var $FAIstate = "";
40 var $ui;
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 /* 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['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 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
165 $this->SubObjects[$entry]['status']= "delete";
166 }else{
167 unset($this->SubObjects[$entry]);
168 }
169 }
170 }
171 }
173 /* Add new sub object */
174 if(isset($_POST['AddSubObject'])){
175 $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
176 $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
178 if(preg_match("/c/",$acl)){
179 $this->dialog= new $this->subClassName($this->config,"new");
180 $this->dialog->set_acl_base($this->acl_base);
181 $this->dialog->set_acl_category("fai");
182 $this->is_dialog=true;
183 }
184 }
186 if($this->dn != "new"){
187 $_SESSION['objectinfo']= $this->dn;
188 }
190 /* Save Dialog */
191 if(isset($_POST['SaveSubObject'])){
192 $this->dialog->save_object();
193 $msgs = $this->dialog->check();
194 if(count($msgs)>0){
195 foreach($msgs as $msg){
196 print_red($msg);
197 }
198 }else{
199 /* Get return object */
200 $obj = $this->dialog->save();
201 if(isset($obj['remove'])){
203 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
205 /* Depending on status, set new status */
206 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
207 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
208 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
209 unset($this->SubObjects[$obj['remove']['from']]);
210 }
211 $obj['status'] = "new";
212 $this->SubObjects[$obj['remove']['to']] = $obj;
213 unset($this->SubObjects[$obj['remove']['to']]['remove']);
214 }else{
215 if($obj['status'] == "FreshLoaded"){
216 $obj['status'] = "edited";
217 }
218 $this->SubObjects[$obj['cn']]=$obj;
219 }
220 $this->is_dialog=false;
221 unset($this->dialog);
222 $this->dialog=NULL;
223 }
224 }
226 /* Sort entries */
227 $tmp = $keys = array();
228 foreach($this->SubObjects as $key => $entry){
229 $keys[$key]=$key;
230 }
231 natcasesort($keys);
232 foreach($keys as $key){
233 $tmp[$key]=$this->SubObjects[$key];
234 }
235 $this->SubObjects = $tmp;
237 /* Cancel Dialog */
238 if(isset($_POST['CancelSubObject'])){
239 $this->is_dialog=false;
240 unset($this->dialog);
241 $this->dialog=NULL;
242 }
244 /* Print dialog if $this->dialog is set */
245 if($this->dialog){
246 $this->dialog->save_object();
247 $display = $this->dialog->execute();
248 return($display);
249 }
251 /* Divlist Containing FAItemplates */
252 $divlist = new divSelectBox("FAItemplates");
253 $divlist->setHeight(400);
255 $tmp = $this->getList(true);
257 /* Create div list with all sub entries listed */
258 foreach($this->SubObjects as $key => $name){
260 /* Skip removed entries */
261 if($name['status'] == "delete") continue;
263 /* Get permissions */
264 $dn = $this->acl_base_for_current_object($name['dn']);
265 $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry") ;
266 $act = "";
268 /* Hide delete icon if this object is freezed */
269 if(preg_match("/freeze/",$this->FAIstate)){
270 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
271 }else{
272 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
273 if(preg_match("/d/",$acl)){
274 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
275 }
276 }
278 /* Check acls for download icon */
279 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile") ;
280 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
281 $down = "";
282 }else{
283 $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
284 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
285 </a>";
286 }
288 /* Check if we are allowed to view this object */
289 $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn") ;
290 if(preg_match("/r/",$s_acl)){
291 $divlist->AddEntry(array( array("string"=> $tmp[$key] ),
292 array("string"=>$down , "attach" => "style='width:20px;'"),
293 array("string"=>str_replace("%s",base64_encode($key),$act),
294 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
295 }
296 }
297 $smarty->assign("Entry_divlist",$divlist->DrawList());
298 /* Divlist creation complete
299 */
301 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
302 * If we post the escaped strings they will be escaped again
303 */
304 foreach($this->attributes as $attrs){
305 if(get_magic_quotes_gpc()){
306 $smarty->assign($attrs,stripslashes($this->$attrs));
307 }else{
308 $smarty->assign($attrs,($this->$attrs));
309 }
310 }
312 $dn = $this->acl_base_for_current_object($this->dn);
313 $smarty->assign("sub_object_is_addable",
314 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) &&
315 !preg_match("/freeze/",$this->FAIstate));
317 foreach($this->attributes as $attr){
318 $smarty->assign($attr."ACL",$this->getacl($attr));
319 }
321 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
322 return($display);
323 }
326 function acl_base_for_current_object($dn)
327 {
328 if($dn == "new"){
329 if($this->dn == "new"){
330 $dn= $_SESSION['CurrentMainBase'];
331 }else{
332 $dn = $this->dn;
333 }
334 }
335 return($dn);
336 }
339 /* Generate listbox friendly SubObject list
340 */
341 function getList(){
342 $a_return=array();
343 foreach($this->SubObjects as $obj){
344 if($obj['status'] != "delete"){
346 if((isset($obj['description']))&&(!empty($obj['description']))){
347 if(strlen($obj['description']) > 40){
348 $obj['description'] = substr($obj['description'],0,40)."...";
349 }
350 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
351 }else{
352 $a_return[$obj['cn']]= $obj['cn'];
353 }
354 }
355 }
356 return($a_return);
357 }
359 /* Delete me, and all my subtrees
360 */
361 function remove_from_parent()
362 {
363 $ldap = $this->config->get_ldap_link();
364 $ldap->cd ($this->dn);
366 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
367 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
368 if($_SESSION['faifilter']['branch'] == "main"){
369 $use_dn = $this->dn;
370 }
372 prepare_to_save_FAI_object($use_dn,array(),true);
374 foreach($this->SubObjects as $name => $obj){
375 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
376 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
377 if($_SESSION['faifilter']['branch'] == "main"){
378 $use_dn = $obj['dn'];
379 }
380 prepare_to_save_FAI_object($use_dn,array(),true);
381 }
382 $this->handle_post_events("remove");
383 }
386 /* Save data to object
387 */
388 function save_object()
389 {
390 if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
391 plugin::save_object();
392 }
393 }
396 /* Check supplied data */
397 function check()
398 {
399 /* Call common method to give check the hook */
400 $message= plugin::check();
402 return ($message);
403 }
406 /* Save to LDAP */
407 function save()
408 {
409 plugin::save();
411 $ldap = $this->config->get_ldap_link();
413 prepare_to_save_FAI_object($this->dn,$this->attrs);
414 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
416 /* Do object tagging */
417 $this->handle_object_tagging();
419 /* Prepare FAIscriptEntry to write it to ldap
420 * First sort array.
421 * Because we must delete old entries first.
422 * After deletion, we perform add and modify
423 */
424 $Objects = array();
425 foreach($this->SubObjects as $name => $obj){
426 if($obj['status'] == "FreshLoaded"){
427 unset($this->SubObjects[$name]);
428 }
429 }
431 foreach($this->SubObjects as $name => $obj){
432 if($obj['status'] == "delete"){
433 $Objects[$name] = $obj;
434 }
435 }
436 foreach($this->SubObjects as $name => $obj){
437 if($obj['status'] != "delete"){
438 $Objects[$name] = $obj;
439 }
440 }
442 foreach($Objects as $name => $obj){
444 foreach($this->sub64coded as $codeIt){
445 $obj[$codeIt]=base64_encode($obj[$codeIt]);
446 }
447 $tmp = array();
448 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
449 foreach($attributes as $attrs){
450 if(empty($obj[$attrs])){
451 $obj[$attrs] = array();
452 }
453 $tmp[$attrs] =($obj[$attrs]);
454 }
456 $tmp['objectClass'] = $this->subClasses;
458 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
460 if($obj['status']=="new"){
461 $ldap->cat($sub_dn,array("objectClass"));
462 if($ldap->count()){
463 $obj['status']="edited";
464 }
465 }
467 /* Check if gosaAdministrativeUnitTag is required as object class */
468 if($obj['status'] == "edited"){
469 $ldap->cat($sub_dn,array("objectClass"));
470 $attrs = $ldap->fetch();
471 if(isset($attrs['objectClass'])){
472 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
473 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
474 }
475 }
476 }
478 if($obj['status'] == "delete"){
479 prepare_to_save_FAI_object($sub_dn,array(),true);
480 $this->handle_post_events("remove");
481 }elseif($obj['status'] == "edited"){
482 prepare_to_save_FAI_object($sub_dn,$tmp);
483 $this->handle_post_events("modify");
484 }elseif($obj['status']=="new"){
485 prepare_to_save_FAI_object($sub_dn,$tmp);
486 $this->handle_post_events("add");
487 }
490 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
491 }
492 }
495 /* Return plugin informations for acl handling */
496 function plInfo()
497 {
498 return (array(
499 "plShortName" => _("Template"),
500 "plDescription" => _("FAI template"),
501 "plSelfModify" => FALSE,
502 "plDepends" => array(),
503 "plPriority" => 24,
504 "plSection" => array("administration"),
505 "plCategory" => array("fai"),
506 "plProvidedAcls" => array(
507 "cn" => _("Name")." ("._("Readonly").")",
508 "description" => _("Description"))
509 ));
510 }
511 }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>