1 <?php
3 class faiVariable 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","FAIvariable");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIvariableEntry";
21 var $subClasses = array("top","FAIclass","FAIvariableEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiVariableEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAIvariableContent");
28 var $sub64coded = array();
30 /* Specific attributes */
31 var $cn = ""; // The class name for this object
32 var $description = ""; // The description for this set of partitions
33 var $is_dialog = false; // specifies which buttons will be shown to save or abort
34 var $dialog = NULL; // a dialog, e.g. new disk dialog
35 var $SubObjects = array(); // All leafobjects of this object
37 var $FAIstate = "";
38 var $base = "";
39 var $release = "";
40 var $copy_paste_mode = false;
41 var $cut_paste_mode = false;
43 var $CopyPasteVars = array("SubObjects","FAIstate");
45 function faiVariable ($config, $dn= NULL)
46 {
47 /* Load Attributes */
48 plugin::plugin ($config, $dn);
50 $this->acl ="#all#";
52 /* If "dn==new" we try to create a new entry
53 * Else we must read all objects from ldap which belong to this entry.
54 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
55 */
57 if($dn != "new"){
58 $this->dn =$dn;
60 /* Set acls
61 */
62 $ui = get_userinfo();
63 $acl = get_permissions ($this->dn, $ui->subtreeACL);
64 $acli = get_module_permission($acl, "FAIclass", $this->dn);
65 $this->acl=$acli;
67 /* Get FAIstate
68 */
69 if(isset($this->attrs['FAIstate'][0])){
70 $this->FAIstate = $this->attrs['FAIstate'][0];
71 }
73 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
74 */
75 $ldap = $this->config->get_ldap_link();
76 $ldap->cd ($this->dn);
77 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
79 while($object = $ldap->fetch()){
80 /* Set status for save management */
82 foreach($this->subAttributes as $attrs){
83 if(!isset($object[$attrs][0])){
84 $this->SubObjects[$object['cn'][0]][$attrs]="";
85 }else{
86 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
87 }
88 }
90 foreach($this->sub64coded as $codeIt){
91 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
92 }
94 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
95 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
96 }
97 }
98 }
100 function execute()
101 {
102 /* Call parent execute */
103 plugin::execute();
105 /* Fill templating stuff */
106 $smarty= get_smarty();
107 $display= "";
109 /* Add new sub object */
110 if(isset($_POST['AddSubObject'])){
111 $this->dialog= new $this->subClassName($this->config,"new");
112 $this->dialog->acl = $this->acl;
113 $this->is_dialog=true;
114 }
116 if($this->dn != "new"){
117 $_SESSION['objectinfo']= $this->dn;
118 }
121 /* Edit selected Sub Object */
122 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
124 $var = $_POST['SubObject'][0];
125 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
126 $this->dialog->acl = $this->acl;
127 $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
128 $this->is_dialog=true;
129 }
131 /* Remove Sub object */
132 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
133 foreach($_POST['SubObject'] as $var){
134 if($this->SubObjects[$var]['status'] == "edited"){
135 $this->SubObjects[$var]['status']= "delete";
136 }else{
137 unset($this->SubObjects[$var]);
138 }
139 }
140 }
142 /* Save Dialog */
143 if(isset($_POST['SaveSubObject'])){
144 $this->dialog->save_object();
145 $msgs = $this->dialog->check();
146 if(count($msgs)>0){
147 foreach($msgs as $msg){
148 print_red($msg);
149 }
150 }else{
151 $obj = $this->dialog->save();
152 if(isset($obj['remove'])){
153 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
154 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
155 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
156 unset($this->SubObjects[$obj['remove']['from']]);
157 }
158 $obj['status'] = "new";
159 $this->SubObjects[$obj['remove']['to']] = $obj;
160 unset($this->SubObjects[$obj['remove']['to']]['remove']);
161 }else{
162 $this->SubObjects[$obj['cn']]=$obj;
163 }
164 $this->is_dialog=false;
165 unset($this->dialog);
166 $this->dialog=NULL;
167 }
168 }
170 /* Sort entries */
171 $tmp = $keys = array();
172 foreach($this->SubObjects as $key => $entry){
173 $keys[$key]=$key;
174 }
175 natcasesort($keys);
176 foreach($keys as $key){
177 $tmp[$key]=$this->SubObjects[$key];
178 }
179 $this->SubObjects = $tmp;
181 /* Cancel Dialog */
182 if(isset($_POST['CancelSubObject'])){
183 $this->is_dialog=false;
184 unset($this->dialog);
185 $this->dialog=NULL;
186 }
188 /* Print dialog if $this->dialog is set */
189 if($this->dialog){
190 $this->dialog->save_object();
191 $display = $this->dialog->execute();
192 return($display);
193 }
195 $smarty->assign("SubObjects",$this->getList());
197 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
198 * If we post the escaped strings they will be escaped again
199 */
201 foreach($this->attributes as $attrs){
202 if(get_magic_quotes_gpc()){
203 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
204 }else{
205 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
206 }
207 }
209 foreach($this->attributes as $attr){
210 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
211 }
214 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
215 return($display);
216 }
218 /* Generate listbox friendly SubObject list
219 */
220 function getList(){
221 $a_return=array();
222 foreach($this->SubObjects as $obj){
223 if($obj['status'] != "delete"){
225 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
226 if (preg_match("/\[\*\]/", $obj['description'])){
227 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
228 } else {
229 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
230 }
231 }else{
232 if (preg_match("/\[\*\]/", $obj['description'])){
233 $a_return[$obj['cn']]= $obj['cn'];
234 } else {
235 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
236 }
237 }
238 }
239 }
240 return($a_return);
241 }
243 /* Delete me, and all my subtrees
244 */
245 function remove_from_parent()
246 {
247 $ldap = $this->config->get_ldap_link();
248 $ldap->cd ($this->dn);
249 $ldap->rmdir_recursive($this->dn);
250 $this->handle_post_events("remove");
251 }
254 /* Save data to object
255 */
256 function save_object()
257 {
258 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
259 plugin::save_object();
260 foreach($this->attributes as $attrs){
261 if(isset($_POST[$attrs])){
262 $this->$attrs = $_POST[$attrs];
263 }
264 }
265 }
266 }
269 /* Check supplied data */
270 function check()
271 {
272 /* Call common method to give check the hook */
273 $message= plugin::check();
275 /* If this is a new script, check if a script with this name already exists */
276 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
278 /* Check if current name is already used for fai scripts in selected release */
279 $dn = 'cn='.$this->cn.",ou=variables,".$this->release;
280 $ldap = $this->config->get_ldap_link();
281 $ldap->cat($dn);
282 if($ldap->count()){
284 $r =convert_department_dn($this->release);;
285 $message[] = sprintf(_("Can't insert a fai variable named '%s' in '%s' there is already a variable with the given name."),$this->cn,$r);
286 }
287 }
288 return ($message);
289 }
292 /* Save to LDAP */
293 function save()
294 {
295 plugin::save();
297 $ldap = $this->config->get_ldap_link();
299 /* Copy & Paste : Ensure that FAIstate is copied too */
300 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
301 $this->attrs['FAIstate'] = $this->FAIstate;
302 }
304 $ldap->cat($this->dn,array("objectClass"));
305 if($ldap->count()!=0){
306 /* Write FAIscript to ldap*/
307 $ldap->cd($this->dn);
308 $this->cleanup();
309 $ldap->modify ($this->attrs);
311 }else{
312 /* Write FAIscript to ldap*/
313 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
314 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
315 $ldap->cd($this->dn);
316 $ldap->add($this->attrs);
317 }
318 show_ldap_error($ldap->get_error(), _("Saving FAI variable base failed"));
320 /* Do object tagging */
321 $this->handle_object_tagging();
323 /* Prepare FAIscriptEntry to write it to ldap
324 * First sort array.
325 * Because we must delete old entries first.
326 * After deletion, we perform add and modify
327 */
328 $Objects = array();
329 foreach($this->SubObjects as $name => $obj){
330 if($obj['status'] == "delete"){
331 $Objects[$name] = $obj;
332 }
333 }
334 foreach($this->SubObjects as $name => $obj){
335 if($obj['status'] != "delete"){
336 $Objects[$name] = $obj;
337 }
338 }
340 foreach($Objects as $name => $obj){
342 foreach($this->sub64coded as $codeIt){
343 $obj[$codeIt]=base64_encode($obj[$codeIt]);
344 }
346 $tmp = array();
347 foreach($this->subAttributes as $attrs){
348 if(empty($obj[$attrs])){
349 $obj[$attrs] = array();
350 }
351 $tmp[$attrs] = $obj[$attrs];
352 }
354 $tmp['objectClass'] = $this->subClasses;
356 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
358 if($this->copy_paste_mode){
359 $obj['status'] = "new";
360 }
362 if($obj['status']=="new"){
363 $ldap->cat($sub_dn,array("objectClass"));
364 if($ldap->count()){
365 $obj['status']="edited";
366 }
367 }
369 /* Check if gosaAdministrativeUnitTag is required as object class */
370 if($obj['status'] == "edited"){
371 $ldap->cat($sub_dn,array("objectClass"));
372 $attrs = $ldap->fetch();
373 if(isset($attrs['objectClass'])){
374 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
375 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
376 }
377 }
378 }
380 if($obj['status'] == "delete"){
381 $ldap->cd($sub_dn);
382 $ldap->rmdir_recursive($sub_dn);
383 $this->handle_post_events("remove");
384 show_ldap_error($ldap->get_error(), _("Removing FAI variable failed"));
385 }elseif($obj['status'] == "edited"){
386 $ldap->cd($sub_dn);
387 $this->cleanup();
388 $ldap->modify ($tmp);
390 $this->handle_post_events("modify");
391 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
392 }elseif($obj['status']=="new"){
394 if($tmp['description'] == array()){
395 unset($tmp['description']);
396 }
398 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
399 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
400 $ldap->cd($sub_dn);
401 $ldap->add($tmp);
402 $this->handle_post_events("add");
403 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
404 }
406 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
407 }
408 }
410 /* return copy & paste dialog
411 */
412 function getCopyDialog()
413 {
414 /* Ask for cn */
415 $smarty = get_smarty();
416 $smarty->assign("cn" ,$this->cn);
417 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
418 $ret = array();
419 $ret['string'] = $str;
420 $ret['status'] = "";
421 return($ret);
422 }
424 /* Get posted cn */
425 function saveCopyDialog()
426 {
427 if(isset($_POST['cn'])){
428 $this->cn = $_POST['cn'];
429 }
430 }
431 }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>