1 <?php
3 class faiVariable extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
8 /* Attributes for this Object */
9 var $attributes = array("cn","description");
11 /* ObjectClasses for this Object*/
12 var $objectclasses = array("top","FAIclass","FAIvariable");
14 /* Class name of the Ldap ObjectClass for the Sub Object */
15 var $subClass = "FAIvariableEntry";
16 var $subClasses = array("top","FAIclass","FAIvariableEntry");
18 /* Class name of the php class which allows us to edit a Sub Object */
19 var $subClassName = "faiVariableEntry";
21 /* Attributes to initialise for each subObject */
22 var $subAttributes = array("cn","description","FAIvariableContent");
23 var $sub64coded = array();
25 /* Specific attributes */
26 var $cn = ""; // The class name for this object
27 var $description = ""; // The description for this set of partitions
28 var $is_dialog = false; // specifies which buttons will be shown to save or abort
29 var $dialog = NULL; // a dialog, e.g. new disk dialog
30 var $SubObjects = array(); // All leafobjects of this object
32 var $FAIstate = "";
33 var $base = "";
34 var $release = "";
35 var $copy_paste_mode = false;
36 var $cut_paste_mode = false;
38 var $CopyPasteVars = array("SubObjects");
40 function faiVariable ($config, $dn= NULL)
41 {
42 /* Load Attributes */
43 plugin::plugin ($config, $dn);
45 $this->acl ="#all#";
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 */
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."))",$this->subAttributes);
74 while($object = $ldap->fetch()){
75 /* Set status for save management */
77 foreach($this->subAttributes as $attrs){
78 if(!isset($object[$attrs][0])){
79 $this->SubObjects[$object['cn'][0]][$attrs]="";
80 }else{
81 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
82 }
83 }
85 foreach($this->sub64coded as $codeIt){
86 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
87 }
89 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
90 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
91 }
92 }
93 }
95 function execute()
96 {
97 /* Call parent execute */
98 plugin::execute();
100 /* Fill templating stuff */
101 $smarty= get_smarty();
102 $display= "";
104 /* Add new sub object */
105 if(isset($_POST['AddSubObject'])){
106 $this->dialog= new $this->subClassName($this->config,"new");
107 $this->dialog->acl = $this->acl;
108 $this->is_dialog=true;
109 }
111 if($this->dn != "new"){
112 $_SESSION['objectinfo']= $this->dn;
113 }
116 /* Edit selected Sub Object */
117 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
119 $var = $_POST['SubObject'][0];
120 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
121 $this->dialog->acl = $this->acl;
122 $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
123 $this->is_dialog=true;
124 }
126 /* Remove Sub object */
127 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
128 foreach($_POST['SubObject'] as $var){
129 if($this->SubObjects[$var]['status'] == "edited"){
130 $this->SubObjects[$var]['status']= "delete";
131 }else{
132 unset($this->SubObjects[$var]);
133 }
134 }
135 }
137 /* Save Dialog */
138 if(isset($_POST['SaveSubObject'])){
139 $this->dialog->save_object();
140 $msgs = $this->dialog->check();
141 if(count($msgs)>0){
142 foreach($msgs as $msg){
143 print_red($msg);
144 }
145 }else{
146 $obj = $this->dialog->save();
147 if(isset($obj['remove'])){
148 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
149 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
150 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
151 unset($this->SubObjects[$obj['remove']['from']]);
152 }
153 $obj['status'] = "new";
154 $this->SubObjects[$obj['remove']['to']] = $obj;
155 unset($this->SubObjects[$obj['remove']['to']]['remove']);
156 }else{
157 $this->SubObjects[$obj['cn']]=$obj;
158 }
159 $this->is_dialog=false;
160 unset($this->dialog);
161 $this->dialog=NULL;
162 }
163 }
165 /* Sort entries */
166 $tmp = $keys = array();
167 foreach($this->SubObjects as $key => $entry){
168 $keys[$key]=$key;
169 }
170 natcasesort($keys);
171 foreach($keys as $key){
172 $tmp[$key]=$this->SubObjects[$key];
173 }
174 $this->SubObjects = $tmp;
176 /* Cancel Dialog */
177 if(isset($_POST['CancelSubObject'])){
178 $this->is_dialog=false;
179 unset($this->dialog);
180 $this->dialog=NULL;
181 }
183 /* Print dialog if $this->dialog is set */
184 if($this->dialog){
185 $this->dialog->save_object();
186 $display = $this->dialog->execute();
187 return($display);
188 }
190 $smarty->assign("SubObjects",$this->getList());
192 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
193 * If we post the escaped strings they will be escaped again
194 */
196 foreach($this->attributes as $attrs){
197 if(get_magic_quotes_gpc()){
198 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
199 }else{
200 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
201 }
202 }
204 foreach($this->attributes as $attr){
205 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
206 }
209 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
210 return($display);
211 }
213 /* Generate listbox friendly SubObject list
214 */
215 function getList(){
216 $a_return=array();
217 foreach($this->SubObjects as $obj){
218 if($obj['status'] != "delete"){
220 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
221 if (preg_match("/\[\*\]/", $obj['description'])){
222 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
223 } else {
224 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
225 }
226 }else{
227 if (preg_match("/\[\*\]/", $obj['description'])){
228 $a_return[$obj['cn']]= $obj['cn'];
229 } else {
230 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
231 }
232 }
233 }
234 }
235 return($a_return);
236 }
238 /* Delete me, and all my subtrees
239 */
240 function remove_from_parent()
241 {
242 $ldap = $this->config->get_ldap_link();
243 $ldap->cd ($this->dn);
244 $ldap->rmdir_recursive($this->dn);
245 $this->handle_post_events("remove");
246 }
249 /* Save data to object
250 */
251 function save_object()
252 {
253 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
254 plugin::save_object();
255 foreach($this->attributes as $attrs){
256 if(isset($_POST[$attrs])){
257 $this->$attrs = $_POST[$attrs];
258 }
259 }
260 }
261 }
264 /* Check supplied data */
265 function check()
266 {
267 /* Call common method to give check the hook */
268 $message= plugin::check();
270 /* If this is a new script, check if a script with this name already exists */
271 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
273 /* Check if current name is already used for fai scripts in selected release */
274 $dn = 'cn='.$this->cn.",ou=variables,".$this->release;
275 $ldap = $this->config->get_ldap_link();
276 $ldap->cat($dn);
277 if($ldap->count()){
279 $r =convert_department_dn($this->release);;
280 $message[] = sprintf(_("Can't insert a fai variable named '%s' in '%s' there is already a variable with the given name."),$this->cn,$r);
281 }
282 }
283 return ($message);
284 }
287 /* Save to LDAP */
288 function save()
289 {
290 plugin::save();
292 $ldap = $this->config->get_ldap_link();
294 /* Copy & Paste : Ensure that FAIstate is copied too */
295 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
296 $this->attrs['FAIstate'] = $this->FAIstate;
297 }
299 $ldap->cat($this->dn,array("objectClass"));
300 if($ldap->count()!=0){
301 /* Write FAIscript to ldap*/
302 $ldap->cd($this->dn);
303 $this->cleanup();
304 $ldap->modify ($this->attrs);
306 }else{
307 /* Write FAIscript to ldap*/
308 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
309 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
310 $ldap->cd($this->dn);
311 $ldap->add($this->attrs);
312 }
313 show_ldap_error($ldap->get_error(), _("Saving FAI variable base failed"));
315 /* Do object tagging */
316 $this->handle_object_tagging();
318 /* Prepare FAIscriptEntry to write it to ldap
319 * First sort array.
320 * Because we must delete old entries first.
321 * After deletion, we perform add and modify
322 */
323 $Objects = array();
324 foreach($this->SubObjects as $name => $obj){
325 if($obj['status'] == "delete"){
326 $Objects[$name] = $obj;
327 }
328 }
329 foreach($this->SubObjects as $name => $obj){
330 if($obj['status'] != "delete"){
331 $Objects[$name] = $obj;
332 }
333 }
335 foreach($Objects as $name => $obj){
337 foreach($this->sub64coded as $codeIt){
338 $obj[$codeIt]=base64_encode($obj[$codeIt]);
339 }
341 $tmp = array();
342 foreach($this->subAttributes as $attrs){
343 if(empty($obj[$attrs])){
344 $obj[$attrs] = array();
345 }
346 $tmp[$attrs] = $obj[$attrs];
347 }
349 $tmp['objectClass'] = $this->subClasses;
351 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
353 if($this->copy_paste_mode){
354 $obj['status'] = "new";
355 }
357 if($obj['status']=="new"){
358 $ldap->cat($sub_dn,array("objectClass"));
359 if($ldap->count()){
360 $obj['status']="edited";
361 }
362 }
364 /* Check if gosaAdministrativeUnitTag is required as object class */
365 if($obj['status'] == "edited"){
366 $ldap->cat($sub_dn,array("objectClass"));
367 $attrs = $ldap->fetch();
368 if(isset($attrs['objectClass'])){
369 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
370 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
371 }
372 }
373 }
375 if($obj['status'] == "delete"){
376 $ldap->cd($sub_dn);
377 $ldap->rmdir_recursive($sub_dn);
378 $this->handle_post_events("remove");
379 show_ldap_error($ldap->get_error(), _("Removing FAI variable failed"));
380 }elseif($obj['status'] == "edited"){
381 $ldap->cd($sub_dn);
382 $this->cleanup();
383 $ldap->modify ($tmp);
385 $this->handle_post_events("modify");
386 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
387 }elseif($obj['status']=="new"){
389 if($tmp['description'] == array()){
390 unset($tmp['description']);
391 }
393 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
394 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
395 $ldap->cd($sub_dn);
396 $ldap->add($tmp);
397 $this->handle_post_events("add");
398 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
399 }
401 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
402 }
403 }
405 /* return copy & paste dialog
406 */
407 function getCopyDialog()
408 {
409 /* Ask for cn */
410 $smarty = get_smarty();
411 $smarty->assign("cn" ,$this->cn);
412 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
413 $ret = array();
414 $ret['string'] = $str;
415 $ret['status'] = "";
416 return($ret);
417 }
419 /* Get posted cn */
420 function saveCopyDialog()
421 {
422 if(isset($_POST['cn'])){
423 $this->cn = $_POST['cn'];
424 }
425 }
426 }
428 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
429 ?>