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 $sub64coded = array();
29 var $sub_Load_Later = array("FAItemplateFile");
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);
72 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
74 while($object = $ldap->fetch()){
76 /* Set status for save management */
77 $objects = array();
78 $objects['status'] = "FreshLoaded";
79 $objects['dn'] = $object['dn'];
80 $objects = $this->get_object_attributes($objects,$this->subAttributes);
81 $this->SubObjects[$objects['cn']] = $objects;
82 }
83 }
84 }
87 /* Reload some attributes */
88 function get_object_attributes($object,$attributes)
89 {
90 $ldap = $this->config->get_ldap_link();
91 $ldap->cd($this->config->current['BASE']);
92 $ldap->cat($object['dn'],$attributes);
93 $tmp = $ldap->fetch();
95 foreach($attributes as $attrs){
96 if(isset($tmp[$attrs][0])){
97 $var = $tmp[$attrs][0];
99 /* Check if we must decode some attributes */
100 if(in_array_ics($attrs,$this->sub64coded)){
101 $var = base64_decode($var);
102 }
104 /* check if this is a binary entry */
105 if(in_array_ics($attrs,$this->subBinary)){
106 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
107 }
109 /* Fix slashes */
110 $var = addslashes($var);
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 }
141 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
142 $this->dialog->acl = $this->acl;
143 $_SESSION['objectinfo'] = $obj['dn'];
144 $this->dialog->parent = &$this;
145 $this->is_dialog=true;
146 }
147 if(preg_match("/^deletescript_/",$name)&&($once)){
148 $once = false;
149 $entry = preg_replace("/^deletescript_/","",$name);
150 $entry = base64_decode(preg_replace("/_.*/","",$entry));
151 if($this->SubObjects[$entry]['status'] == "edited"){
152 $this->SubObjects[$entry]['status']= "delete";
153 }else{
154 unset($this->SubObjects[$entry]);
155 }
156 }
157 }
159 /* Add new sub object */
160 if(isset($_POST['AddSubObject'])){
161 $this->dialog= new $this->subClassName($this->config,"new");
162 $this->dialog->acl = $this->acl;
163 $this->is_dialog=true;
164 }
166 if($this->dn != "new"){
167 $_SESSION['objectinfo']= $this->dn;
168 }
170 /* Save Dialog */
171 if(isset($_POST['SaveSubObject'])){
172 $this->dialog->save_object();
173 $msgs = $this->dialog->check();
174 if(count($msgs)>0){
175 foreach($msgs as $msg){
176 print_red($msg);
177 }
178 }else{
180 /* Get return object */
181 $obj = $this->dialog->save();
182 if(isset($obj['remove'])){
183 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
185 /* Depending on status, set new status */
186 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
187 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
188 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
189 unset($this->SubObjects[$obj['remove']['from']]);
190 }
191 $obj['status'] = "new";
192 $this->SubObjects[$obj['remove']['to']] = $obj;
193 unset($this->SubObjects[$obj['remove']['to']]['remove']);
194 }else{
195 if($obj['status'] == "FreshLoaded"){
196 $obj['status'] = "edited";
197 }
198 $this->SubObjects[$obj['cn']]=$obj;
199 }
201 $this->is_dialog=false;
202 unset($this->dialog);
203 $this->dialog=NULL;
204 }
205 }
207 /* Sort entries */
208 $tmp = $keys = array();
209 foreach($this->SubObjects as $key => $entry){
210 $keys[$key]=$key;
211 }
212 natcasesort($keys);
213 foreach($keys as $key){
214 $tmp[$key]=$this->SubObjects[$key];
215 }
216 $this->SubObjects = $tmp;
218 /* Cancel Dialog */
219 if(isset($_POST['CancelSubObject'])){
220 $this->is_dialog=false;
221 unset($this->dialog);
222 $this->dialog=NULL;
223 }
225 /* Print dialog if $this->dialog is set */
226 if($this->dialog){
227 $this->dialog->save_object();
228 $display = $this->dialog->execute();
229 return($display);
230 }
232 /* Divlist Containing FAItemplates */
233 $divlist = new divSelectBox("FAItemplates");
234 $divlist->setHeight(400);
235 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
236 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
237 $img_remo = "";
238 }else{
239 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
240 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
241 }
243 foreach($this->getList(true) as $key => $name){
245 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
246 $down = "";
247 }else{
249 $dn = $this->SubObjects[$key]['dn'];
251 $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
252 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
253 </a>";
254 }
256 $divlist->AddEntry(array( array("string"=>$name),
257 array("string"=>$down , "attach" => "style='width:20px;'"),
258 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
259 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
260 }
261 $smarty->assign("Entry_divlist",$divlist->DrawList());
262 /* Divlist creation complete
263 */
265 $smarty->assign("SubObjects",$this->getList());
267 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
268 * If we post the escaped strings they will be escaped again
269 */
270 foreach($this->attributes as $attrs){
271 if(get_magic_quotes_gpc()){
272 $smarty->assign($attrs,stripslashes($this->$attrs));
273 }else{
274 $smarty->assign($attrs,($this->$attrs));
275 }
276 }
278 foreach($this->attributes as $attr){
279 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
280 }
282 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
283 return($display);
284 }
286 /* Generate listbox friendly SubObject list
287 */
288 function getList(){
289 $a_return=array();
290 foreach($this->SubObjects as $obj){
291 if($obj['status'] != "delete"){
293 if((isset($obj['description']))&&(!empty($obj['description']))){
294 if(strlen($obj['description']) > 40){
295 $obj['description'] = substr($obj['description'],0,40)."...";
296 }
297 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
298 }else{
299 $a_return[$obj['cn']]= $obj['cn'];
300 }
301 }
302 }
303 return($a_return);
304 }
306 /* Delete me, and all my subtrees
307 */
308 function remove_from_parent()
309 {
310 $ldap = $this->config->get_ldap_link();
311 $ldap->cd ($this->dn);
312 $ldap->rmdir_recursive($this->dn);
313 $this->handle_post_events("remove");
314 }
317 /* Save data to object
318 */
319 function save_object()
320 {
321 if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
322 plugin::save_object();
323 foreach($this->attributes as $attrs){
324 if(isset($_POST[$attrs])){
325 $this->$attrs = $_POST[$attrs];
326 }
327 }
328 }
329 }
332 /* Check supplied data */
333 function check()
334 {
335 /* Call common method to give check the hook */
336 $message= plugin::check();
338 return ($message);
339 }
342 /* Save to LDAP */
343 function save()
344 {
345 plugin::save();
347 $ldap = $this->config->get_ldap_link();
349 $ldap->cat($this->dn,array("objectClass"));
350 if($ldap->count()!=0){
351 /* Write FAIscript to ldap*/
352 $ldap->cd($this->dn);
353 $this->cleanup();
354 $ldap->modify ($this->attrs);
356 }else{
357 /* Write FAIscript to ldap*/
358 $ldap->cd($this->config->current['BASE']);
359 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
360 $ldap->cd($this->dn);
361 $ldap->add($this->attrs);
362 }
363 show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
365 /* Do object tagging */
366 $this->handle_object_tagging();
368 /* Prepare FAIscriptEntry to write it to ldap
369 * First sort array.
370 * Because we must delete old entries first.
371 * After deletion, we perform add and modify
372 */
373 $Objects = array();
375 /* We do not need to save untouched objects */
376 foreach($this->SubObjects as $name => $obj){
377 if($obj['status'] == "FreshLoaded"){
378 unset($this->SubObjects[$name]);
379 }
380 }
382 foreach($this->SubObjects as $name => $obj){
383 if($obj['status'] == "delete"){
384 $Objects[$name] = $obj;
385 }
386 }
388 foreach($this->SubObjects as $name => $obj){
389 if($obj['status'] != "delete"){
390 $Objects[$name] = $obj;
391 }
392 }
394 foreach($Objects as $name => $obj){
396 foreach($this->sub64coded as $codeIt){
397 $obj[$codeIt]=base64_encode($obj[$codeIt]);
398 }
399 $tmp = array();
400 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
401 foreach($attributes as $attrs){
402 if(empty($obj[$attrs])){
403 $obj[$attrs] = array();
404 }
405 $tmp[$attrs] = $obj[$attrs];
406 }
408 $tmp['objectClass'] = $this->subClasses;
410 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
412 if($obj['status']=="new"){
413 $ldap->cat($sub_dn,array("objectClass"));
414 if($ldap->count()){
415 $obj['status']="edited";
416 }
417 }
419 /* Check if gosaAdministrativeUnitTag is required as object class */
420 if($obj['status'] == "edited"){
421 $ldap->cat($sub_dn,array("objectClass"));
422 $attrs = $ldap->fetch();
423 if(isset($attrs['objectClass'])){
424 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
425 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
426 }
427 }
428 }
430 if($obj['status'] == "delete"){
431 $ldap->cd($sub_dn);
432 $ldap->rmdir_recursive($sub_dn);
433 $this->handle_post_events("remove");
434 show_ldap_error($ldap->get_error(), _("Removing FAI template base failed"));
435 }elseif($obj['status'] == "edited"){
436 $ldap->cd($sub_dn);
437 $this->cleanup();
438 $ldap->modify ($tmp);
439 show_ldap_error($ldap->get_error(), _("Saving FAI template failed"));
441 $this->handle_post_events("modify");
442 }elseif($obj['status']=="new"){
444 if($tmp['description']==array()){
445 unset($tmp['description']);
446 }
447 $ldap->cd($this->config->current['BASE']);
448 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
449 $ldap->cd($sub_dn);
450 $ldap->add($tmp);
451 $this->handle_post_events("add");
452 show_ldap_error($ldap->get_error(), _("Saving FAI template failed"));
453 }
455 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
456 }
457 }
458 }
460 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
461 ?>