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 = "";
41 function faiTemplate ($config, $dn= NULL)
42 {
43 /* Load Attributes */
44 plugin::plugin ($config, $dn);
46 $this->acl="#all#";
48 /* If "dn==new" we try to create a new entry
49 * Else we must read all objects from ldap which belong to this entry.
50 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
51 */
52 if($dn != "new"){
53 $this->dn =$dn;
55 /* Set acls
56 */
57 $ui = get_userinfo();
58 $acl = get_permissions ($this->dn, $ui->subtreeACL);
59 $acli = get_module_permission($acl, "FAIclass", $this->dn);
60 $this->acl=$acli;
62 /* Get FAIstate
63 */
64 if(isset($this->attrs['FAIstate'][0])){
65 $this->FAIstate = $this->attrs['FAIstate'][0];
66 }
68 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
69 */
70 $ldap = $this->config->get_ldap_link();
71 $ldap->cd ($this->dn);
73 $attrs_to_search = $this->subAttributes;
74 $attrs_to_search[] = "FAIstate";
75 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
77 while($object = $ldap->fetch()){
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 }
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 $object[$attrs] = $var;
112 }
113 }
114 return($object);
115 }
118 function execute()
119 {
120 /* Call parent execute */
121 plugin::execute();
123 /* Fill templating stuff */
124 $smarty= get_smarty();
125 $display= "";
127 /* New Listhandling
128 */
129 $once = true;
130 foreach($_POST as $name => $value){
131 if(preg_match("/^editscript_/",$name)&&($once)){
132 $once = false;
133 $entry = preg_replace("/^editscript_/","",$name);
134 $entry = base64_decode(preg_replace("/_.*/","",$entry));
136 $obj = $this->SubObjects[$entry];
137 if($obj['status'] == "FreshLoaded"){
138 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
139 }
140 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
142 $_SESSION['objectinfo'] = $obj['dn'];
143 $this->dialog->parent = &$this;
144 $this->is_dialog=true;
145 }
146 if(preg_match("/^deletescript_/",$name)&&($once)){
147 $once = false;
148 $entry = preg_replace("/^deletescript_/","",$name);
149 $entry = base64_decode(preg_replace("/_.*/","",$entry));
150 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
151 $this->SubObjects[$entry]['status']= "delete";
152 }else{
153 unset($this->SubObjects[$entry]);
154 }
155 }
156 }
158 /* Add new sub object */
159 if(isset($_POST['AddSubObject'])){
160 $this->dialog= new $this->subClassName($this->config,"new");
161 $this->dialog->acl = $this->acl;
162 $this->is_dialog=true;
163 }
165 if($this->dn != "new"){
166 $_SESSION['objectinfo']= $this->dn;
167 }
169 /* Save Dialog */
170 if(isset($_POST['SaveSubObject'])){
171 $this->dialog->save_object();
172 $msgs = $this->dialog->check();
173 if(count($msgs)>0){
174 foreach($msgs as $msg){
175 print_red($msg);
176 }
177 }else{
178 /* Get return object */
179 $obj = $this->dialog->save();
180 if(isset($obj['remove'])){
182 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
184 /* Depending on status, set new status */
185 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
186 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
187 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
188 unset($this->SubObjects[$obj['remove']['from']]);
189 }
190 $obj['status'] = "new";
191 $this->SubObjects[$obj['remove']['to']] = $obj;
192 unset($this->SubObjects[$obj['remove']['to']]['remove']);
193 }else{
194 if($obj['status'] == "FreshLoaded"){
195 $obj['status'] = "edited";
196 }
197 $this->SubObjects[$obj['cn']]=$obj;
198 }
199 $this->is_dialog=false;
200 unset($this->dialog);
201 $this->dialog=NULL;
202 }
203 }
205 /* Sort entries */
206 $tmp = $keys = array();
207 foreach($this->SubObjects as $key => $entry){
208 $keys[$key]=$key;
209 }
210 natcasesort($keys);
211 foreach($keys as $key){
212 $tmp[$key]=$this->SubObjects[$key];
213 }
214 $this->SubObjects = $tmp;
216 /* Cancel Dialog */
217 if(isset($_POST['CancelSubObject'])){
218 $this->is_dialog=false;
219 unset($this->dialog);
220 $this->dialog=NULL;
221 }
223 /* Print dialog if $this->dialog is set */
224 if($this->dialog){
225 $this->dialog->save_object();
226 $display = $this->dialog->execute();
227 return($display);
228 }
230 /* Divlist Containing FAItemplates */
231 $divlist = new divSelectBox("FAItemplates");
232 $divlist->setHeight(400);
233 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
234 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
235 $img_remo = "";
236 }else{
237 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
238 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
239 }
241 foreach($this->getList(true) as $key => $name){
243 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
244 $down = "";
245 }else{
247 $dn = $this->SubObjects[$key]['dn'];
249 $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
250 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
251 </a>";
252 }
254 $divlist->AddEntry(array( array("string"=>$name),
255 array("string"=>$down , "attach" => "style='width:20px;'"),
256 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
257 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
258 }
259 $smarty->assign("Entry_divlist",$divlist->DrawList());
260 /* Divlist creation complete
261 */
263 $smarty->assign("SubObjects",$this->getList());
265 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
266 * If we post the escaped strings they will be escaped again
267 */
268 foreach($this->attributes as $attrs){
269 if(get_magic_quotes_gpc()){
270 $smarty->assign($attrs,stripslashes($this->$attrs));
271 }else{
272 $smarty->assign($attrs,($this->$attrs));
273 }
274 }
276 foreach($this->attributes as $attr){
277 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
278 }
280 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
281 return($display);
282 }
284 /* Generate listbox friendly SubObject list
285 */
286 function getList(){
287 $a_return=array();
288 foreach($this->SubObjects as $obj){
289 if($obj['status'] != "delete"){
291 if((isset($obj['description']))&&(!empty($obj['description']))){
292 if(strlen($obj['description']) > 40){
293 $obj['description'] = substr($obj['description'],0,40)."...";
294 }
295 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
296 }else{
297 $a_return[$obj['cn']]= $obj['cn'];
298 }
299 }
300 }
301 return($a_return);
302 }
304 /* Delete me, and all my subtrees
305 */
306 function remove_from_parent()
307 {
308 $ldap = $this->config->get_ldap_link();
309 $ldap->cd ($this->dn);
311 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
312 if($_SESSION['faifilter']['branch'] == "main"){
313 $use_dn = $this->dn;
314 }
316 prepare_to_save_FAI_object($use_dn,array(),true);
318 foreach($this->SubObjects as $name => $obj){
319 $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
320 if($_SESSION['faifilter']['branch'] == "main"){
321 $use_dn = $obj['dn'];
322 }
323 prepare_to_save_FAI_object($use_dn,array(),true);
324 }
325 $this->handle_post_events("remove");
326 }
329 /* Save data to object
330 */
331 function save_object()
332 {
333 if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
334 plugin::save_object();
335 foreach($this->attributes as $attrs){
336 if(isset($_POST[$attrs])){
337 $this->$attrs = $_POST[$attrs];
338 }
339 }
340 }
341 }
344 /* Check supplied data */
345 function check()
346 {
347 /* Call common method to give check the hook */
348 $message= plugin::check();
350 return ($message);
351 }
354 /* Save to LDAP */
355 function save()
356 {
357 plugin::save();
359 $ldap = $this->config->get_ldap_link();
361 prepare_to_save_FAI_object($this->dn,$this->attrs);
362 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
364 /* Do object tagging */
365 $this->handle_object_tagging();
367 /* Prepare FAIscriptEntry to write it to ldap
368 * First sort array.
369 * Because we must delete old entries first.
370 * After deletion, we perform add and modify
371 */
372 $Objects = array();
373 foreach($this->SubObjects as $name => $obj){
374 if($obj['status'] == "FreshLoaded"){
375 unset($this->SubObjects[$name]);
376 }
377 }
379 foreach($this->SubObjects as $name => $obj){
380 if($obj['status'] == "delete"){
381 $Objects[$name] = $obj;
382 }
383 }
384 foreach($this->SubObjects as $name => $obj){
385 if($obj['status'] != "delete"){
386 $Objects[$name] = $obj;
387 }
388 }
390 foreach($Objects as $name => $obj){
392 foreach($this->sub64coded as $codeIt){
393 $obj[$codeIt]=base64_encode($obj[$codeIt]);
394 }
395 $tmp = array();
396 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
397 foreach($attributes as $attrs){
398 if(empty($obj[$attrs])){
399 $obj[$attrs] = array();
400 }
401 $tmp[$attrs] = $obj[$attrs];
402 }
404 $tmp['objectClass'] = $this->subClasses;
406 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
408 if($obj['status']=="new"){
409 $ldap->cat($sub_dn,array("objectClass"));
410 if($ldap->count()){
411 $obj['status']="edited";
412 }
413 }
415 /* Check if gosaAdministrativeUnitTag is required as object class */
416 if($obj['status'] == "edited"){
417 $ldap->cat($sub_dn,array("objectClass"));
418 $attrs = $ldap->fetch();
419 if(isset($attrs['objectClass'])){
420 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
421 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
422 }
423 }
424 }
426 if($obj['status'] == "delete"){
427 prepare_to_save_FAI_object($sub_dn,array(),true);
428 $this->handle_post_events("remove");
429 }elseif($obj['status'] == "edited"){
430 prepare_to_save_FAI_object($sub_dn,$tmp);
431 $this->handle_post_events("modify");
432 }elseif($obj['status']=="new"){
433 prepare_to_save_FAI_object($sub_dn,$tmp);
434 $this->handle_post_events("add");
435 }
438 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
439 }
440 }
443 /* Return plugin informations for acl handling */
444 function plInfo()
445 {
446 return (array(
447 "plShortName" => _("Template"),
448 "plDescription" => _("FAI template"),
449 "plSelfModify" => FALSE,
450 "plDepends" => array(),
451 "plPriority" => 0,
452 "plSection" => array("administration"),
453 "plCategory" => array("fai"),
454 "plProvidedAcls" => array(
455 "cn" => _("Name"),
456 "description" => _("Description"),
457 "FAItemplateFile" => _("Template file"),
458 "FAItemplatePath" => _("Template path"),
459 "FAIowner" => _("File owner"),
460 "FAImode" => _("File permissions"))
461 ));
462 }
463 }
465 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
466 ?>