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->dialog->parent = $this;
109 $this->is_dialog=true;
110 }
112 if($this->dn != "new"){
113 $_SESSION['objectinfo']= $this->dn;
114 }
117 /* Edit selected Sub Object */
118 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
120 $var = $_POST['SubObject'][0];
121 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
122 $this->dialog->acl = $this->acl;
123 $this->dialog->parent = $this;
124 $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
125 $this->is_dialog=true;
126 }
128 /* Remove Sub object */
129 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
130 foreach($_POST['SubObject'] as $var){
131 if($this->SubObjects[$var]['status'] == "edited"){
132 $this->SubObjects[$var]['status']= "delete";
133 }else{
134 unset($this->SubObjects[$var]);
135 }
136 }
137 }
139 /* Save Dialog */
140 if(isset($_POST['SaveSubObject'])){
141 $this->dialog->save_object();
142 $msgs = $this->dialog->check();
143 if(count($msgs)>0){
144 foreach($msgs as $msg){
145 print_red($msg);
146 }
147 }else{
148 $obj = $this->dialog->save();
149 if(isset($obj['remove'])){
150 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
151 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
152 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
153 unset($this->SubObjects[$obj['remove']['from']]);
154 }
155 $obj['status'] = "new";
156 $this->SubObjects[$obj['remove']['to']] = $obj;
157 unset($this->SubObjects[$obj['remove']['to']]['remove']);
158 }else{
159 $this->SubObjects[$obj['cn']]=$obj;
160 }
161 $this->is_dialog=false;
162 unset($this->dialog);
163 $this->dialog=NULL;
164 }
165 }
167 /* Sort entries */
168 $tmp = $keys = array();
169 foreach($this->SubObjects as $key => $entry){
170 $keys[$key]=$key;
171 }
172 natcasesort($keys);
173 foreach($keys as $key){
174 $tmp[$key]=$this->SubObjects[$key];
175 }
176 $this->SubObjects = $tmp;
178 /* Cancel Dialog */
179 if(isset($_POST['CancelSubObject'])){
180 $this->is_dialog=false;
181 unset($this->dialog);
182 $this->dialog=NULL;
183 }
185 /* Print dialog if $this->dialog is set */
186 if($this->dialog){
187 $this->dialog->save_object();
188 $display = $this->dialog->execute();
189 return($display);
190 }
192 $smarty->assign("SubObjects",$this->getList());
194 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
195 * If we post the escaped strings they will be escaped again
196 */
198 foreach($this->attributes as $attrs){
199 if(get_magic_quotes_gpc()){
200 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
201 }else{
202 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
203 }
204 }
206 foreach($this->attributes as $attr){
207 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
208 }
211 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
212 return($display);
213 }
215 /* Generate listbox friendly SubObject list
216 */
217 function getList(){
218 $a_return=array();
219 foreach($this->SubObjects as $obj){
220 if($obj['status'] != "delete"){
222 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
223 if (preg_match("/\[\*\]/", $obj['description'])){
224 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
225 } else {
226 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
227 }
228 }else{
229 if (preg_match("/\[\*\]/", $obj['description'])){
230 $a_return[$obj['cn']]= $obj['cn'];
231 } else {
232 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
233 }
234 }
235 }
236 }
237 return($a_return);
238 }
240 /* Delete me, and all my subtrees
241 */
242 function remove_from_parent()
243 {
244 $ldap = $this->config->get_ldap_link();
245 $ldap->cd ($this->dn);
246 $ldap->rmdir_recursive($this->dn);
247 $this->handle_post_events("remove");
248 }
251 /* Save data to object
252 */
253 function save_object()
254 {
255 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
256 plugin::save_object();
257 foreach($this->attributes as $attrs){
258 if(isset($_POST[$attrs])){
259 $this->$attrs = $_POST[$attrs];
260 }
261 }
262 }
263 }
266 /* Check supplied data */
267 function check()
268 {
269 /* Call common method to give check the hook */
270 $message= plugin::check();
272 /* If this is a new script, check if a script with this name already exists */
273 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
275 /* Check if current name is already used for fai scripts in selected release */
276 $dn = 'cn='.$this->cn.",ou=variables,".$this->release;
277 $ldap = $this->config->get_ldap_link();
278 $ldap->cat($dn);
279 if($ldap->count()){
281 $r =convert_department_dn($this->release);;
282 $message[] = sprintf(_("Can't insert a fai variable named '%s' in '%s' there is already a variable with the given name."),$this->cn,$r);
283 }
284 }
285 return ($message);
286 }
289 /* Save to LDAP */
290 function save()
291 {
292 plugin::save();
294 $ldap = $this->config->get_ldap_link();
296 /* Copy & Paste : Ensure that FAIstate is copied too */
297 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
298 $this->attrs['FAIstate'] = $this->FAIstate;
299 }
301 $ldap->cat($this->dn,array("objectClass"));
302 if($ldap->count()!=0){
303 /* Write FAIscript to ldap*/
304 $ldap->cd($this->dn);
305 $this->cleanup();
306 $ldap->modify ($this->attrs);
308 }else{
309 /* Write FAIscript to ldap*/
310 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
311 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
312 $ldap->cd($this->dn);
313 $ldap->add($this->attrs);
314 }
315 show_ldap_error($ldap->get_error(), _("Saving FAI variable base failed"));
317 /* Do object tagging */
318 $this->handle_object_tagging();
320 /* Prepare FAIscriptEntry to write it to ldap
321 * First sort array.
322 * Because we must delete old entries first.
323 * After deletion, we perform add and modify
324 */
325 $Objects = array();
326 foreach($this->SubObjects as $name => $obj){
327 if($obj['status'] == "delete"){
328 $Objects[$name] = $obj;
329 }
330 }
331 foreach($this->SubObjects as $name => $obj){
332 if($obj['status'] != "delete"){
333 $Objects[$name] = $obj;
334 }
335 }
337 foreach($Objects as $name => $obj){
339 foreach($this->sub64coded as $codeIt){
340 $obj[$codeIt]=base64_encode($obj[$codeIt]);
341 }
343 $tmp = array();
344 foreach($this->subAttributes as $attrs){
345 if(empty($obj[$attrs])){
346 $obj[$attrs] = array();
347 }
348 $tmp[$attrs] = $obj[$attrs];
349 }
351 $tmp['objectClass'] = $this->subClasses;
353 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
355 if($this->copy_paste_mode){
356 $obj['status'] = "new";
357 }
359 if($obj['status']=="new"){
360 $ldap->cat($sub_dn,array("objectClass"));
361 if($ldap->count()){
362 $obj['status']="edited";
363 }
364 }
366 /* Check if gosaAdministrativeUnitTag is required as object class */
367 if($obj['status'] == "edited"){
368 $ldap->cat($sub_dn,array("objectClass"));
369 $attrs = $ldap->fetch();
370 if(isset($attrs['objectClass'])){
371 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
372 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
373 }
374 }
375 }
377 if($obj['status'] == "delete"){
378 $ldap->cd($sub_dn);
379 $ldap->rmdir_recursive($sub_dn);
380 $this->handle_post_events("remove");
381 show_ldap_error($ldap->get_error(), _("Removing FAI variable failed"));
382 }elseif($obj['status'] == "edited"){
383 $ldap->cd($sub_dn);
384 $this->cleanup();
385 $ldap->modify ($tmp);
387 $this->handle_post_events("modify");
388 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
389 }elseif($obj['status']=="new"){
391 if($tmp['description'] == array()){
392 unset($tmp['description']);
393 }
395 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
396 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
397 $ldap->cd($sub_dn);
398 $ldap->add($tmp);
399 $this->handle_post_events("add");
400 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
401 }
403 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
404 }
405 }
407 /* return copy & paste dialog
408 */
409 function getCopyDialog()
410 {
411 /* Ask for cn */
412 $smarty = get_smarty();
413 $smarty->assign("cn" ,$this->cn);
414 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
415 $ret = array();
416 $ret['string'] = $str;
417 $ret['status'] = "";
418 return($ret);
419 }
421 /* Get posted cn */
422 function saveCopyDialog()
423 {
424 if(isset($_POST['cn'])){
425 $this->cn = $_POST['cn'];
426 }
427 }
428 }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>