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);
112 $object[$attrs] = $var;
113 }
114 }
115 return($object);
116 }
119 function execute()
120 {
121 /* Call parent execute */
122 plugin::execute();
124 /* Fill templating stuff */
125 $smarty= get_smarty();
126 $display= "";
128 /* New Listhandling
129 */
130 $once = true;
131 foreach($_POST as $name => $value){
132 if(preg_match("/^editscript_/",$name)&&($once)){
133 $once = false;
134 $entry = preg_replace("/^editscript_/","",$name);
135 $entry = base64_decode(preg_replace("/_.*/","",$entry));
137 $obj = $this->SubObjects[$entry];
138 if($obj['status'] == "FreshLoaded"){
139 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
140 }
142 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
143 $this->dialog->acl = $this->acl;
144 $_SESSION['objectinfo'] = $obj['dn'];
145 $this->dialog->parent = &$this;
146 $this->is_dialog=true;
147 }
148 if(preg_match("/^deletescript_/",$name)&&($once)){
149 $once = false;
150 $entry = preg_replace("/^deletescript_/","",$name);
151 $entry = base64_decode(preg_replace("/_.*/","",$entry));
153 $status = $this->SubObjects[$entry]['status'];
154 if($status == "edited" || $status == "FreshLoaded"){
155 $this->SubObjects[$entry]['status']= "delete";
156 }else{
157 unset($this->SubObjects[$entry]);
158 }
159 }
160 }
162 /* Add new sub object */
163 if(isset($_POST['AddSubObject'])){
164 $this->dialog= new $this->subClassName($this->config,"new");
165 $this->dialog->acl = $this->acl;
166 $this->is_dialog=true;
167 }
169 if($this->dn != "new"){
170 $_SESSION['objectinfo']= $this->dn;
171 }
173 /* Save Dialog */
174 if(isset($_POST['SaveSubObject'])){
175 $this->dialog->save_object();
176 $msgs = $this->dialog->check();
177 if(count($msgs)>0){
178 foreach($msgs as $msg){
179 print_red($msg);
180 }
181 }else{
183 /* Get return object */
184 $obj = $this->dialog->save();
185 if(isset($obj['remove'])){
186 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
188 /* Depending on status, set new status */
189 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
190 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
191 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
192 unset($this->SubObjects[$obj['remove']['from']]);
193 }
194 $obj['status'] = "new";
195 $this->SubObjects[$obj['remove']['to']] = $obj;
196 unset($this->SubObjects[$obj['remove']['to']]['remove']);
197 }else{
198 if($obj['status'] == "FreshLoaded"){
199 $obj['status'] = "edited";
200 }
201 $this->SubObjects[$obj['cn']]=$obj;
202 }
204 $this->is_dialog=false;
205 unset($this->dialog);
206 $this->dialog=NULL;
207 }
208 }
210 /* Sort entries */
211 $tmp = $keys = array();
212 foreach($this->SubObjects as $key => $entry){
213 $keys[$key]=$key;
214 }
215 natcasesort($keys);
216 foreach($keys as $key){
217 $tmp[$key]=$this->SubObjects[$key];
218 }
219 $this->SubObjects = $tmp;
221 /* Cancel Dialog */
222 if(isset($_POST['CancelSubObject'])){
223 $this->is_dialog=false;
224 unset($this->dialog);
225 $this->dialog=NULL;
226 }
228 /* Print dialog if $this->dialog is set */
229 if($this->dialog){
230 $this->dialog->save_object();
231 $display = $this->dialog->execute();
232 return($display);
233 }
235 /* Divlist Containing FAItemplates */
236 $divlist = new divSelectBox("FAItemplates");
237 $divlist->setHeight(400);
238 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
239 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
240 $img_remo = "";
241 }else{
242 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
243 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
244 }
246 foreach($this->getList(true) as $key => $name){
248 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
249 $down = "";
250 }else{
252 $dn = $this->SubObjects[$key]['dn'];
254 $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
255 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
256 </a>";
257 }
259 $divlist->AddEntry(array( array("string"=>$name),
260 array("string"=>$down , "attach" => "style='width:20px;'"),
261 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
262 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
263 }
264 $smarty->assign("Entry_divlist",$divlist->DrawList());
265 /* Divlist creation complete
266 */
268 $smarty->assign("SubObjects",$this->getList());
270 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
271 * If we post the escaped strings they will be escaped again
272 */
273 foreach($this->attributes as $attrs){
274 if(get_magic_quotes_gpc()){
275 $smarty->assign($attrs,stripslashes($this->$attrs));
276 }else{
277 $smarty->assign($attrs,($this->$attrs));
278 }
279 }
281 foreach($this->attributes as $attr){
282 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
283 }
285 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
286 return($display);
287 }
289 /* Generate listbox friendly SubObject list
290 */
291 function getList(){
292 $a_return=array();
293 foreach($this->SubObjects as $obj){
294 if($obj['status'] != "delete"){
296 if((isset($obj['description']))&&(!empty($obj['description']))){
297 if(strlen($obj['description']) > 40){
298 $obj['description'] = substr($obj['description'],0,40)."...";
299 }
300 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes( $obj['description'] )."]";
301 }else{
302 $a_return[$obj['cn']]= $obj['cn'];
303 }
304 }
305 }
306 return($a_return);
307 }
309 /* Delete me, and all my subtrees
310 */
311 function remove_from_parent()
312 {
313 $ldap = $this->config->get_ldap_link();
314 $ldap->cd ($this->dn);
315 $ldap->rmdir_recursive($this->dn);
316 $this->handle_post_events("remove");
317 }
320 /* Save data to object
321 */
322 function save_object()
323 {
324 if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
325 plugin::save_object();
326 foreach($this->attributes as $attrs){
327 if(isset($_POST[$attrs])){
328 $this->$attrs = $_POST[$attrs];
329 }
330 }
331 }
332 }
335 /* Check supplied data */
336 function check()
337 {
338 /* Call common method to give check the hook */
339 $message= plugin::check();
341 return ($message);
342 }
345 /* Save to LDAP */
346 function save()
347 {
348 plugin::save();
350 $ldap = $this->config->get_ldap_link();
352 $ldap->cat($this->dn,array("objectClass"));
353 if($ldap->count()!=0){
354 /* Write FAIscript to ldap*/
355 $ldap->cd($this->dn);
356 $this->cleanup();
357 $ldap->modify ($this->attrs);
359 }else{
360 /* Write FAIscript to ldap*/
361 $ldap->cd($this->config->current['BASE']);
362 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
363 $ldap->cd($this->dn);
364 $ldap->add($this->attrs);
365 }
366 show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
368 /* Do object tagging */
369 $this->handle_object_tagging();
371 /* Prepare FAIscriptEntry to write it to ldap
372 * First sort array.
373 * Because we must delete old entries first.
374 * After deletion, we perform add and modify
375 */
376 $Objects = array();
378 /* We do not need to save untouched objects */
379 foreach($this->SubObjects as $name => $obj){
380 if($obj['status'] == "FreshLoaded"){
381 unset($this->SubObjects[$name]);
382 }
383 }
385 foreach($this->SubObjects as $name => $obj){
386 if($obj['status'] == "delete"){
387 $Objects[$name] = $obj;
388 }
389 }
391 foreach($this->SubObjects as $name => $obj){
392 if($obj['status'] != "delete"){
393 $Objects[$name] = $obj;
394 }
395 }
397 foreach($Objects as $name => $obj){
399 foreach($this->sub64coded as $codeIt){
400 $obj[$codeIt]=base64_encode($obj[$codeIt]);
401 }
402 $tmp = array();
403 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
404 foreach($attributes as $attrs){
405 if(empty($obj[$attrs])){
406 $obj[$attrs] = array();
407 }
408 $tmp[$attrs] = stripslashes ($obj[$attrs]);
409 }
411 $tmp['objectClass'] = $this->subClasses;
413 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
415 if($obj['status']=="new"){
416 $ldap->cat($sub_dn,array("objectClass"));
417 if($ldap->count()){
418 $obj['status']="edited";
419 }
420 }
422 /* Check if gosaAdministrativeUnitTag is required as object class */
423 if($obj['status'] == "edited"){
424 $ldap->cat($sub_dn,array("objectClass"));
425 $attrs = $ldap->fetch();
426 if(isset($attrs['objectClass'])){
427 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
428 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
429 }
430 }
431 }
433 if($obj['status'] == "delete"){
434 $ldap->cd($sub_dn);
435 $ldap->rmdir_recursive($sub_dn);
436 $this->handle_post_events("remove");
437 show_ldap_error($ldap->get_error(), _("Removing FAI template base failed"));
438 }elseif($obj['status'] == "edited"){
439 $ldap->cd($sub_dn);
440 $this->cleanup();
441 $ldap->modify ($tmp);
442 show_ldap_error($ldap->get_error(), _("Saving FAI template failed"));
444 $this->handle_post_events("modify");
445 }elseif($obj['status']=="new"){
447 if($tmp['description']==array()){
448 unset($tmp['description']);
449 }
450 $ldap->cd($this->config->current['BASE']);
451 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
452 $ldap->cd($sub_dn);
453 $ldap->add($tmp);
454 $this->handle_post_events("add");
455 show_ldap_error($ldap->get_error(), _("Saving FAI template failed"));
456 }
458 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
459 }
460 }
461 }
463 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
464 ?>