4b10b70e4f2bf530972723bbfcc380834705ca8f
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;
42 function faiVariable ($config, $dn= NULL)
43 {
44 /* Load Attributes */
45 plugin::plugin ($config, $dn);
47 $this->acl ="#all#";
49 /* If "dn==new" we try to create a new entry
50 * Else we must read all objects from ldap which belong to this entry.
51 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
52 */
54 if($dn != "new"){
55 $this->dn =$dn;
57 /* Set acls
58 */
59 $ui = get_userinfo();
60 $acl = get_permissions ($this->dn, $ui->subtreeACL);
61 $acli = get_module_permission($acl, "FAIclass", $this->dn);
62 $this->acl=$acli;
64 /* Get FAIstate
65 */
66 if(isset($this->attrs['FAIstate'][0])){
67 $this->FAIstate = $this->attrs['FAIstate'][0];
68 }
70 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
71 */
72 $ldap = $this->config->get_ldap_link();
73 $ldap->cd ($this->dn);
74 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
76 while($object = $ldap->fetch()){
77 /* Set status for save management */
79 foreach($this->subAttributes as $attrs){
80 if(!isset($object[$attrs][0])){
81 $this->SubObjects[$object['cn'][0]][$attrs]="";
82 }else{
83 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
84 }
85 }
87 foreach($this->sub64coded as $codeIt){
88 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
89 }
91 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
92 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
93 }
94 }
95 }
97 function execute()
98 {
99 /* Call parent execute */
100 plugin::execute();
102 /* Fill templating stuff */
103 $smarty= get_smarty();
104 $display= "";
106 /* Add new sub object */
107 if(isset($_POST['AddSubObject'])){
108 $this->dialog= new $this->subClassName($this->config,"new");
109 $this->dialog->acl = $this->acl;
110 $this->is_dialog=true;
111 }
113 if($this->dn != "new"){
114 $_SESSION['objectinfo']= $this->dn;
115 }
118 /* Edit selected Sub Object */
119 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
121 $var = $_POST['SubObject'][0];
122 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
123 $this->dialog->acl = $this->acl;
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){
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 $ldap->cat($this->dn,array("objectClass"));
297 if($ldap->count()!=0){
298 /* Write FAIscript to ldap*/
299 $ldap->cd($this->dn);
300 $this->cleanup();
301 $ldap->modify ($this->attrs);
303 }else{
304 /* Write FAIscript to ldap*/
305 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
306 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
307 $ldap->cd($this->dn);
308 $ldap->add($this->attrs);
309 }
310 show_ldap_error($ldap->get_error(), _("Saving FAI variable base failed"));
312 /* Do object tagging */
313 $this->handle_object_tagging();
315 /* Prepare FAIscriptEntry to write it to ldap
316 * First sort array.
317 * Because we must delete old entries first.
318 * After deletion, we perform add and modify
319 */
320 $Objects = array();
321 foreach($this->SubObjects as $name => $obj){
322 if($obj['status'] == "delete"){
323 $Objects[$name] = $obj;
324 }
325 }
326 foreach($this->SubObjects as $name => $obj){
327 if($obj['status'] != "delete"){
328 $Objects[$name] = $obj;
329 }
330 }
332 foreach($Objects as $name => $obj){
334 foreach($this->sub64coded as $codeIt){
335 $obj[$codeIt]=base64_encode($obj[$codeIt]);
336 }
338 $tmp = array();
339 foreach($this->subAttributes as $attrs){
340 if(empty($obj[$attrs])){
341 $obj[$attrs] = array();
342 }
343 $tmp[$attrs] = $obj[$attrs];
344 }
346 $tmp['objectClass'] = $this->subClasses;
348 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
350 if($obj['status']=="new"){
351 $ldap->cat($sub_dn,array("objectClass"));
352 if($ldap->count()){
353 $obj['status']="edited";
354 }
355 }
357 /* Check if gosaAdministrativeUnitTag is required as object class */
358 if($obj['status'] == "edited"){
359 $ldap->cat($sub_dn,array("objectClass"));
360 $attrs = $ldap->fetch();
361 if(isset($attrs['objectClass'])){
362 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
363 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
364 }
365 }
366 }
368 if($obj['status'] == "delete"){
369 $ldap->cd($sub_dn);
370 $ldap->rmdir_recursive($sub_dn);
371 $this->handle_post_events("remove");
372 show_ldap_error($ldap->get_error(), _("Removing FAI variable failed"));
373 }elseif($obj['status'] == "edited"){
374 $ldap->cd($sub_dn);
375 $this->cleanup();
376 $ldap->modify ($tmp);
378 $this->handle_post_events("modify");
379 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
380 }elseif($obj['status']=="new"){
382 if($tmp['description'] == array()){
383 unset($tmp['description']);
384 }
386 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
387 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
388 $ldap->cd($sub_dn);
389 $ldap->add($tmp);
390 $this->handle_post_events("add");
391 show_ldap_error($ldap->get_error(), _("Saving FAI variable failed"));
392 }
394 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
395 }
396 }
397 }
399 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
400 ?>