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 $SubObjects = array(); // All leafobjects of this object
31 var $FAIstate = "";
32 var $ui ;
33 var $view_logged = FALSE;
35 function faiVariable (&$config, $dn= NULL)
36 {
37 /* Load Attributes */
38 plugin::plugin ($config, $dn);
40 if($dn != "new"){
41 $this->dn =$dn;
43 /* Get FAIstate
44 */
45 if(isset($this->attrs['FAIstate'][0])){
46 $this->FAIstate = $this->attrs['FAIstate'][0];
47 }
49 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50 */
51 $ldap = $this->config->get_ldap_link();
52 $ldap->cd ($this->dn);
54 $attrs_to_search = $this->subAttributes;
55 $attrs_to_search[] = "FAIstate";
56 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
58 while($object = $ldap->fetch()){
60 /* Skip objects, that are tagged as removed */
61 if(isset($object['FAIstate'][0])){
62 if(preg_match("/removed$/",$object['FAIstate'][0])){
63 continue;
64 }
65 }
67 /* Set status for save management */
68 foreach($this->subAttributes as $attrs){
69 if(!isset($object[$attrs][0])){
70 $this->SubObjects[$object['cn'][0]][$attrs]="";
71 }else{
72 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
73 }
74 }
76 foreach($this->sub64coded as $codeIt){
77 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
78 }
80 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
81 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
82 }
84 }
85 $this->ui = get_userinfo();
86 }
89 function acl_base_for_current_object($dn)
90 {
91 if($dn == "new"){
92 if($this->dn == "new"){
93 $dn = session::get('CurrentMainBase');
94 }else{
95 $dn = $this->dn;
96 }
97 }
98 return($dn);
99 }
102 function execute()
103 {
104 /* Call parent execute */
105 plugin::execute();
107 if($this->is_account && !$this->view_logged){
108 $this->view_logged = TRUE;
109 new log("view","fai/".get_class($this),$this->dn);
110 }
112 /* Fill templating stuff */
113 $smarty= get_smarty();
114 $display= "";
116 /* Add new sub object */
117 if(isset($_POST['AddSubObject'])){
118 $this->dialog= new $this->subClassName($this->config,"new");
119 $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
120 $this->dialog->set_acl_category("fai");
121 $this->dialog->parent = &$this;
122 $this->is_dialog=true;
123 }
125 if($this->dn != "new"){
126 session::set('objectinfo',$this->dn);
127 }
130 /* Edit selected Sub Object */
131 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
133 $var = $_POST['SubObject'][0];
134 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
135 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
136 $this->dialog->set_acl_category("fai");
137 $this->dialog->set_acl_base($c_dn);
138 $this->dialog->parent = &$this;
139 session::set('objectinfo',$this->SubObjects[$var]['dn']);
140 $this->is_dialog=true;
141 }
143 /* Remove Sub object */
144 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
145 foreach($_POST['SubObject'] as $var){
147 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
148 $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
149 if(preg_match("/d/",$acl)){
150 if($this->SubObjects[$var]['status'] == "edited"){
151 $this->SubObjects[$var]['status']= "delete";
152 }else{
153 unset($this->SubObjects[$var]);
154 }
155 }
156 }
157 }
159 /* Save Dialog */
160 if(isset($_POST['SaveSubObject'])){
161 $this->dialog->save_object();
162 $msgs = $this->dialog->check();
163 if(count($msgs)>0){
164 foreach($msgs as $msg){
165 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
166 }
167 }else{
168 $obj = $this->dialog->save();
169 if(isset($obj['remove'])){
170 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
171 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
172 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
173 unset($this->SubObjects[$obj['remove']['from']]);
174 }
175 $obj['status'] = "new";
176 $this->SubObjects[$obj['remove']['to']] = $obj;
177 unset($this->SubObjects[$obj['remove']['to']]['remove']);
178 }else{
179 $this->SubObjects[$obj['cn']]=$obj;
180 }
181 $this->is_dialog=false;
182 unset($this->dialog);
183 $this->dialog=FALSE;
184 }
185 }
187 /* Sort entries */
188 $tmp = $keys = array();
189 foreach($this->SubObjects as $key => $entry){
190 $keys[$key]=$key;
191 }
192 natcasesort($keys);
193 foreach($keys as $key){
194 $tmp[$key]=$this->SubObjects[$key];
195 }
196 $this->SubObjects = $tmp;
198 /* Cancel Dialog */
199 if(isset($_POST['CancelSubObject'])){
200 $this->is_dialog=false;
201 unset($this->dialog);
202 $this->dialog=FALSE;
203 }
205 /* Print dialog if $this->dialog is set */
206 if(is_object($this->dialog)){
207 $this->dialog->save_object();
208 $display = $this->dialog->execute();
209 return($display);
210 }
212 $ui = get_userinfo();
213 $ret = $this->getList();
214 $tmp = array();
215 foreach($this->SubObjects as $key => $obj){
216 $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
217 if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
218 $tmp[$key] = $ret[$key];
219 }
220 }
221 $smarty->assign("SubObjects",$tmp);
224 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
225 * If we post the escaped strings they will be escaped again
226 */
227 foreach($this->attributes as $attrs){
228 if(get_magic_quotes_gpc()){
229 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
230 }else{
231 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
232 }
233 }
235 $c_dn = $this->acl_base_for_current_object($this->dn);
236 $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
237 $smarty->assign("is_removeable", preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
239 $tmp = $this->plInfo();
240 foreach($tmp['plProvidedAcls'] as $name => $translation) {
241 $smarty->assign($name."ACL",$this->getacl($name));
242 }
245 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
246 return($display);
247 }
249 /* Generate listbox friendly SubObject list
250 */
251 function getList(){
252 $a_return=array();
253 foreach($this->SubObjects as $obj){
254 if($obj['status'] != "delete"){
256 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
257 if (preg_match("/\[\*\]/", $obj['description'])){
258 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
259 } else {
260 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
261 }
262 }else{
263 if (preg_match("/\[\*\]/", $obj['description'])){
264 $a_return[$obj['cn']]= $obj['cn'];
265 } else {
266 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
267 }
268 }
269 }
270 }
271 return($a_return);
272 }
274 /* Delete me, and all my subtrees
275 */
276 function remove_from_parent()
277 {
278 if($this->acl_is_removeable()){
279 $ldap = $this->config->get_ldap_link();
280 $ldap->cd ($this->dn);
282 $faifilter = session::get('faifilter');
283 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
284 if($faifilter['branch'] == "main"){
285 $use_dn = $this->dn;
286 }
288 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
289 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
291 foreach($this->SubObjects as $name => $obj){
292 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
293 if($faifilter['branch'] == "main"){
294 $use_dn = $obj['dn'];
295 }
296 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
297 }
298 $this->handle_post_events("remove");
299 }
300 }
303 /* Save data to object
304 */
305 function save_object()
306 {
307 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
308 plugin::save_object();
309 foreach($this->attributes as $attrs){
310 if(isset($_POST[$attrs])){
311 $this->$attrs = $_POST[$attrs];
312 }
313 }
314 }
315 }
318 /* Check supplied data */
319 function check()
320 {
321 /* Call common method to give check the hook */
322 $message= plugin::check();
324 return ($message);
325 }
328 /* Save to LDAP */
329 function save()
330 {
331 plugin::save();
333 $ldap = $this->config->get_ldap_link();
334 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
335 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
337 if($this->initially_was_account){
338 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
339 }else{
340 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
341 }
343 /* Prepare FAIscriptEntry to write it to ldap
344 * First sort array.
345 * Because we must delete old entries first.
346 * After deletion, we perform add and modify
347 */
348 $Objects = array();
349 foreach($this->SubObjects as $name => $obj){
350 if($obj['status'] == "delete"){
351 $Objects[$name] = $obj;
352 }
353 }
354 foreach($this->SubObjects as $name => $obj){
355 if($obj['status'] != "delete"){
356 $Objects[$name] = $obj;
357 }
358 }
360 foreach($Objects as $name => $obj){
362 foreach($this->sub64coded as $codeIt){
363 $obj[$codeIt]=base64_encode($obj[$codeIt]);
364 }
366 $tmp = array();
367 foreach($this->subAttributes as $attrs){
368 if(empty($obj[$attrs])){
369 $obj[$attrs] = array();
370 }
371 $tmp[$attrs] = $obj[$attrs];
372 }
374 $tmp['objectClass'] = $this->subClasses;
376 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
378 if($obj['status']=="new"){
379 $ldap->cat($sub_dn,array("objectClass"));
380 if($ldap->count()){
381 $obj['status']="edited";
382 }
383 }
385 /* Tag object */
386 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
388 if($obj['status'] == "delete"){
389 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
390 $this->handle_post_events("remove");
391 }elseif($obj['status'] == "edited"){
392 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
393 $this->handle_post_events("modify");
394 }elseif($obj['status']=="new"){
395 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
396 $this->handle_post_events("add");
397 }
399 }
400 }
403 function PrepareForCopyPaste($source)
404 {
405 plugin::PrepareForCopyPaste($source);
407 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
408 */
409 $ldap = $this->config->get_ldap_link();
410 $ldap->cd ($source['dn']);
411 $attrs_to_search = $this->subAttributes;
412 $attrs_to_search[] = "FAIstate";
413 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
414 while($object = $ldap->fetch()){
416 /* Skip objects, that are tagged as removed */
417 if(isset($object['FAIstate'][0])){
418 if(preg_match("/removed$/",$object['FAIstate'][0])){
419 continue;
420 }
421 }
423 /* Set status for save management */
424 foreach($this->subAttributes as $attrs){
425 if(!isset($object[$attrs][0])){
426 $this->SubObjects[$object['cn'][0]][$attrs]="";
427 }else{
428 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
429 }
430 }
431 foreach($this->sub64coded as $codeIt){
432 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
433 }
434 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
435 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
436 }
437 }
440 /* Return plugin informations for acl handling */
441 static function plInfo()
442 {
443 return (array(
444 "plShortName" => _("Variable"),
445 "plDescription" => _("FAI variable"),
446 "plSelfModify" => FALSE,
447 "plDepends" => array(),
448 "plPriority" => 22,
449 "plSection" => array("administration"),
450 "plCategory" => array("fai"),
451 "plProvidedAcls" => array(
452 "cn" => _("Name")." ("._("Read only").")",
453 "description" => _("Description"))
454 ));
455 }
456 }
458 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
459 ?>