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 $var = stripslashes($var);
113 $object[$attrs] = $var;
114 }
115 }
116 return($object);
117 }
120 function execute()
121 {
122 /* Call parent execute */
123 plugin::execute();
125 /* Fill templating stuff */
126 $smarty= get_smarty();
127 $display= "";
129 /* New Listhandling
130 */
131 $once = true;
132 foreach($_POST as $name => $value){
133 if(preg_match("/^editscript_/",$name)&&($once)){
134 $once = false;
135 $entry = preg_replace("/^editscript_/","",$name);
136 $entry = base64_decode(preg_replace("/_.*/","",$entry));
138 $obj = $this->SubObjects[$entry];
139 if($obj['status'] == "FreshLoaded"){
140 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
141 }
142 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
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));
152 if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
153 $this->SubObjects[$entry]['status']= "delete";
154 }else{
155 unset($this->SubObjects[$entry]);
156 }
157 }
158 }
160 /* Add new sub object */
161 if(isset($_POST['AddSubObject'])){
162 $this->dialog= new $this->subClassName($this->config,"new");
163 $this->dialog->acl = $this->acl;
164 $this->is_dialog=true;
165 }
167 if($this->dn != "new"){
168 $_SESSION['objectinfo']= $this->dn;
169 }
171 /* Save Dialog */
172 if(isset($_POST['SaveSubObject'])){
173 $this->dialog->save_object();
174 $msgs = $this->dialog->check();
175 if(count($msgs)>0){
176 foreach($msgs as $msg){
177 print_red($msg);
178 }
179 }else{
180 /* Get return object */
181 $obj = $this->dialog->save();
182 if(isset($obj['remove'])){
184 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
186 /* Depending on status, set new status */
187 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
188 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
189 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
190 unset($this->SubObjects[$obj['remove']['from']]);
191 }
192 $obj['status'] = "new";
193 $this->SubObjects[$obj['remove']['to']] = $obj;
194 unset($this->SubObjects[$obj['remove']['to']]['remove']);
195 }else{
196 if($obj['status'] == "FreshLoaded"){
197 $obj['status'] = "edited";
198 }
199 $this->SubObjects[$obj['cn']]=$obj;
200 }
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);
313 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
314 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
315 if($_SESSION['faifilter']['branch'] == "main"){
316 $use_dn = $this->dn;
317 }
319 prepare_to_save_FAI_object($use_dn,array(),true);
321 foreach($this->SubObjects as $name => $obj){
322 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
323 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
324 if($_SESSION['faifilter']['branch'] == "main"){
325 $use_dn = $obj['dn'];
326 }
327 prepare_to_save_FAI_object($use_dn,array(),true);
328 }
329 $this->handle_post_events("remove");
330 }
333 /* Save data to object
334 */
335 function save_object()
336 {
337 if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
338 plugin::save_object();
339 foreach($this->attributes as $attrs){
340 if(isset($_POST[$attrs])){
341 $this->$attrs = $_POST[$attrs];
342 }
343 }
344 }
345 }
348 /* Check supplied data */
349 function check()
350 {
351 /* Call common method to give check the hook */
352 $message= plugin::check();
354 return ($message);
355 }
358 /* Save to LDAP */
359 function save()
360 {
361 plugin::save();
363 $ldap = $this->config->get_ldap_link();
365 prepare_to_save_FAI_object($this->dn,$this->attrs);
366 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
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();
377 foreach($this->SubObjects as $name => $obj){
378 if($obj['status'] == "FreshLoaded"){
379 unset($this->SubObjects[$name]);
380 }
381 }
383 foreach($this->SubObjects as $name => $obj){
384 if($obj['status'] == "delete"){
385 $Objects[$name] = $obj;
386 }
387 }
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] = stripslashes($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 prepare_to_save_FAI_object($sub_dn,array(),true);
432 $this->handle_post_events("remove");
433 }elseif($obj['status'] == "edited"){
434 prepare_to_save_FAI_object($sub_dn,$tmp);
435 $this->handle_post_events("modify");
436 }elseif($obj['status']=="new"){
437 prepare_to_save_FAI_object($sub_dn,$tmp);
438 $this->handle_post_events("add");
439 }
442 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
443 }
444 }
447 /* Return plugin informations for acl handling */
448 function plInfo()
449 {
450 return (array(
451 "plShortName" => _("Template"),
452 "plDescription" => _("FAI template"),
453 "plSelfModify" => FALSE,
454 "plDepends" => array(),
455 "plPriority" => 24,
456 "plSection" => array("administration"),
457 "plCategory" => array("fai"),
458 "plProvidedAcls" => array(
459 "cn" => _("Name"),
460 "description" => _("Description"),
461 "FAItemplateFile" => _("Template file"),
462 "FAItemplatePath" => _("Template path"),
463 "FAIowner" => _("File owner"),
464 "FAImode" => _("File permissions"))
465 ));
466 }
467 }
469 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
470 ?>