21722813e27da2251466ec79351338e6a802727c
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") && isset($ret[$key])){
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")) && !preg_match("/freeze/", $this->FAIstate));
237 $smarty->assign("is_removeable", preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
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'])) && !preg_match("/freeze/", $this->FAIstate) ){
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 /* Ensure that we do not overwrite an allready existing entry
325 */
326 if($this->is_new){
327 $new_dn= 'cn='.$this->cn.",".get_ou('faivariableou').get_ou('faiou').session::get('CurrentMainBase');
328 $faifilter = session::get('faifilter');
329 if($faifilter['branch']!="main"){
330 $new_dn ='cn='.$this->cn.",".get_ou('faivariableou').$faifilter['branch'];
331 }
333 $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
334 if(isset($res[$this->cn])){
335 $message[] = msgPool::duplicated(_("Name"));
336 }
337 }
339 return ($message);
340 }
343 /* Save to LDAP */
344 function save()
345 {
346 plugin::save();
348 $ldap = $this->config->get_ldap_link();
349 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
351 if($this->initially_was_account){
352 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
353 }else{
354 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
355 }
357 /* Prepare FAIscriptEntry to write it to ldap
358 * First sort array.
359 * Because we must delete old entries first.
360 * After deletion, we perform add and modify
361 */
362 $Objects = array();
363 foreach($this->SubObjects as $name => $obj){
364 if($obj['status'] == "delete"){
365 $Objects[$name] = $obj;
366 }
367 }
368 foreach($this->SubObjects as $name => $obj){
369 if($obj['status'] != "delete"){
370 $Objects[$name] = $obj;
371 }
372 }
374 foreach($Objects as $name => $obj){
376 foreach($this->sub64coded as $codeIt){
377 $obj[$codeIt]=base64_encode($obj[$codeIt]);
378 }
380 $tmp = array();
381 foreach($this->subAttributes as $attrs){
382 if(empty($obj[$attrs])){
383 $obj[$attrs] = array();
384 }
385 $tmp[$attrs] = $obj[$attrs];
386 }
388 $tmp['objectClass'] = $this->subClasses;
390 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
392 if($obj['status']=="new"){
393 $ldap->cat($sub_dn,array("objectClass"));
394 if($ldap->count()){
395 $obj['status']="edited";
396 }
397 }
399 /* Tag object */
400 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
402 if($obj['status'] == "delete"){
403 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
404 $this->handle_post_events("remove");
405 }elseif($obj['status'] == "edited"){
406 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
407 $this->handle_post_events("modify");
408 }elseif($obj['status']=="new"){
409 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
410 $this->handle_post_events("add");
411 }
413 }
414 }
417 function PrepareForCopyPaste($source)
418 {
419 plugin::PrepareForCopyPaste($source);
421 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
422 */
423 $ldap = $this->config->get_ldap_link();
424 $ldap->cd ($source['dn']);
425 $attrs_to_search = $this->subAttributes;
426 $attrs_to_search[] = "FAIstate";
427 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
428 while($object = $ldap->fetch()){
430 /* Skip objects, that are tagged as removed */
431 if(isset($object['FAIstate'][0])){
432 if(preg_match("/removed$/",$object['FAIstate'][0])){
433 continue;
434 }
435 }
437 /* Set status for save management */
438 foreach($this->subAttributes as $attrs){
439 if(!isset($object[$attrs][0])){
440 $this->SubObjects[$object['cn'][0]][$attrs]="";
441 }else{
442 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
443 }
444 }
445 foreach($this->sub64coded as $codeIt){
446 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
447 }
448 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
449 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
450 }
451 }
454 /* Return plugin informations for acl handling */
455 static function plInfo()
456 {
457 return (array(
458 "plShortName" => _("Variable"),
459 "plDescription" => _("FAI variable"),
460 "plSelfModify" => FALSE,
461 "plDepends" => array(),
462 "plPriority" => 22,
463 "plSection" => array("administration"),
464 "plCategory" => array("fai"),
465 "plProvidedAcls" => array(
466 "cn" => _("Name")." ("._("Read only").")",
467 "description" => _("Description"))
468 ));
469 }
472 /*! \brief Used for copy & paste.
473 Returns a HTML input mask, which allows to change the cn of this entry.
474 @param Array Array containing current status && a HTML template.
475 */
476 function getCopyDialog()
477 {
478 $vars = array("cn");
479 $smarty = get_smarty();
480 $smarty->assign("cn", htmlentities($this->cn));
481 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
482 $ret = array();
483 $ret['string'] = $str;
484 $ret['status'] = "";
485 return($ret);
486 }
489 /*! \brief Used for copy & paste.
490 Some entries must be renamed to avaoid duplicate entries.
491 */
492 function saveCopyDialog()
493 {
494 if(isset($_POST['cn'])){
495 $this->cn = get_post('cn');
496 }
497 }
498 }
500 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
501 ?>