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();
24 var $subBinary = array();
26 /* Specific attributes */
27 var $cn = ""; // The class name for this object
28 var $description = ""; // The description for this set of partitions
29 var $is_dialog = false; // specifies which buttons will be shown to save or abort
30 var $SubObjects = array(); // All leafobjects of this object
32 var $FAIstate = "";
33 var $ui ;
34 var $view_logged = FALSE;
36 function faiVariable (&$config, $dn= NULL)
37 {
38 /* Load Attributes */
39 plugin::plugin ($config, $dn);
41 if($dn != "new"){
42 $this->dn =$dn;
44 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
45 */
46 $res = FAI::get_all_objects_for_given_object($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
47 foreach($res as $obj){
48 $objects = array();
49 $objects['description'] = "";
50 $objects['status'] = "FreshLoaded";
51 $objects['dn'] = $obj['dn'];
52 $objects = $this->get_object_attributes($objects,$this->subAttributes);
53 $this->SubObjects[$objects['cn']] = $objects;
54 }
55 }
56 $this->is_new = FALSE;
57 if($this->dn == "new"){
58 $this->is_new =TRUE;
59 }
60 $this->ui = get_userinfo();
61 }
64 function acl_base_for_current_object($dn)
65 {
66 if($dn == "new"){
67 if($this->dn == "new"){
68 $dn = session::get('CurrentMainBase');
69 }else{
70 $dn = $this->dn;
71 }
72 }
73 return($dn);
74 }
77 function execute()
78 {
79 /* Call parent execute */
80 plugin::execute();
82 if($this->is_account && !$this->view_logged){
83 $this->view_logged = TRUE;
84 new log("view","fai/".get_class($this),$this->dn);
85 }
87 /* Fill templating stuff */
88 $smarty= get_smarty();
89 $display= "";
91 /* Add new sub object */
92 if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
93 $this->dialog= new $this->subClassName($this->config,"new");
94 $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
95 $this->dialog->set_acl_category("fai");
96 $this->dialog->parent = &$this;
97 $this->dialog->FAIstate = $this->FAIstate;
98 $this->is_dialog=true;
99 }
101 if($this->dn != "new"){
102 session::set('objectinfo',$this->dn);
103 }
106 /* Edit selected Sub Object */
107 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
109 $var = $_POST['SubObject'][0];
110 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
111 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
112 $this->dialog->set_acl_category("fai");
113 $this->dialog->set_acl_base($c_dn);
114 $this->dialog->FAIstate = $this->FAIstate;
115 $this->dialog->parent = &$this;
116 session::set('objectinfo',$this->SubObjects[$var]['dn']);
117 $this->is_dialog=true;
118 }
120 /* Remove Sub object */
121 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject'])) && !preg_match("/freeze/i",$this->FAIstate)){
122 foreach($_POST['SubObject'] as $var){
124 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
125 $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
126 if(preg_match("/d/",$acl)){
127 if($this->SubObjects[$var]['status'] != "new"){
128 $this->SubObjects[$var]['status']= "delete";
129 }else{
130 unset($this->SubObjects[$var]);
131 }
132 }
133 }
134 }
136 /* Save Dialog */
137 if(isset($_POST['SaveSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
138 $this->dialog->save_object();
139 $msgs = $this->dialog->check();
140 if(count($msgs)>0){
141 foreach($msgs as $msg){
142 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
143 }
144 }else{
145 $obj = $this->dialog->save();
146 if(isset($obj['remove'])){
147 if($this->SubObjects[$obj['remove']['from']]['status'] != "new"){
148 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
149 }else{
150 unset($this->SubObjects[$obj['remove']['from']]);
151 }
152 $obj['status'] = "new";
153 $this->SubObjects[$obj['remove']['to']] = $obj;
154 unset($this->SubObjects[$obj['remove']['to']]['remove']);
155 }else{
156 $obj['status'] = "edited";
157 $this->SubObjects[$obj['cn']]=$obj;
158 }
159 $this->is_dialog=false;
160 unset($this->dialog);
161 $this->dialog=FALSE;
162 }
163 }
165 /* Sort entries */
166 $tmp = $keys = array();
167 foreach($this->SubObjects as $key => $entry){
168 $keys[$key]=$key;
169 }
170 natcasesort($keys);
171 foreach($keys as $key){
172 $tmp[$key]=$this->SubObjects[$key];
173 }
174 $this->SubObjects = $tmp;
176 /* Cancel Dialog */
177 if(isset($_POST['CancelSubObject'])){
178 $this->is_dialog=false;
179 unset($this->dialog);
180 $this->dialog=FALSE;
181 }
183 /* Print dialog if $this->dialog is set */
184 if(is_object($this->dialog)){
185 $this->dialog->save_object();
186 $display = $this->dialog->execute();
187 return($display);
188 }
190 $ui = get_userinfo();
191 $ret = $this->getList();
192 $tmp = array();
193 foreach($this->SubObjects as $key => $obj){
194 $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
195 if((preg_match("/r/",$acl) || $obj['dn'] == "new") && isset($ret[$key])){
196 $tmp[$key] = $ret[$key];
197 }
198 }
199 $smarty->assign("SubObjects",$tmp);
202 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
203 * If we post the escaped strings they will be escaped again
204 */
205 foreach($this->attributes as $attrs){
206 if(get_magic_quotes_gpc()){
207 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
208 }else{
209 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
210 }
211 }
213 $c_dn = $this->acl_base_for_current_object($this->dn);
214 $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
215 $smarty->assign("is_removeable", preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
217 $tmp = $this->plInfo();
218 foreach($tmp['plProvidedAcls'] as $name => $translation) {
219 $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
220 }
223 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
224 return($display);
225 }
227 /* Generate listbox friendly SubObject list
228 */
229 function getList(){
230 $a_return=array();
231 foreach($this->SubObjects as $obj){
232 if($obj['status'] != "delete"){
234 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
235 if (preg_match("/\[\*\]/", $obj['description'])){
236 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
237 } else {
238 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
239 }
240 }else{
241 if (preg_match("/\[\*\]/", $obj['description'])){
242 $a_return[$obj['cn']]= $obj['cn'];
243 } else {
244 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
245 }
246 }
247 }
248 }
249 return($a_return);
250 }
252 /* Delete me, and all my subtrees
253 */
254 function remove_from_parent()
255 {
256 if($this->acl_is_removeable()){
257 $ldap = $this->config->get_ldap_link();
258 $ldap->cd ($this->dn);
259 $release = $this->parent->parent->fai_release;
260 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
261 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
262 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
264 foreach($this->SubObjects as $name => $obj){
265 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
266 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
267 }
268 $this->handle_post_events("remove");
269 }
270 }
273 /* Save data to object
274 */
275 function save_object()
276 {
277 if((isset($_POST['FAIvariable_posted'])) && !preg_match("/freeze/", $this->FAIstate) ){
278 plugin::save_object();
279 foreach($this->attributes as $attrs){
280 if(isset($_POST[$attrs])){
281 $this->$attrs = $_POST[$attrs];
282 }
283 }
284 }
285 }
288 /* Check supplied data */
289 function check()
290 {
291 /* Call common method to give check the hook */
292 $message= plugin::check();
294 /* Ensure that we do not overwrite an allready existing entry
295 */
296 if($this->is_new){
297 $release = $this->parent->parent->fai_release;
298 $new_dn= 'cn='.$this->cn.",".get_ou('faiVariableRDN').get_ou('faiBaseRDN').$release;
299 $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
300 if(isset($res[$this->cn])){
301 $message[] = msgPool::duplicated(_("Name"));
302 }
303 }
305 return ($message);
306 }
309 /* Save to LDAP */
310 function save()
311 {
312 plugin::save();
314 $ldap = $this->config->get_ldap_link();
315 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
317 if($this->initially_was_account){
318 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
319 }else{
320 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
321 }
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 = $this->SubObjects;
329 foreach($Objects as $name => $obj){
331 foreach($this->sub64coded as $codeIt){
332 $obj[$codeIt]=base64_encode($obj[$codeIt]);
333 }
335 $tmp = array();
336 foreach($this->subAttributes as $attrs){
337 if($obj[$attrs] == ""){
338 $obj[$attrs] = array();
339 }
340 $tmp[$attrs] = $obj[$attrs];
341 }
343 $tmp['objectClass'] = $this->subClasses;
345 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
347 if($obj['status']=="new"){
348 $ldap->cat($sub_dn,array("objectClass"));
349 if($ldap->count()){
350 $obj['status']="edited";
351 }
352 }
354 /* Tag object */
355 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
357 if($obj['status'] == "delete"){
358 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
359 $this->handle_post_events("remove");
360 }elseif($obj['status'] == "edited"){
361 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
362 $this->handle_post_events("modify");
363 }elseif($obj['status']=="new"){
364 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
365 $this->handle_post_events("add");
366 }
368 }
369 }
372 function PrepareForCopyPaste($source)
373 {
374 plugin::PrepareForCopyPaste($source);
376 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
377 */
378 $res = FAI::get_all_objects_for_given_object($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
379 foreach($res as $obj){
381 $objects = array();
382 $objects['status'] = "edited";
383 $objects['dn'] = $obj['dn'];
384 $objects = $this->get_object_attributes($objects,$this->subAttributes);
385 $this->SubObjects[$objects['cn']] = $objects;
386 }
387 }
390 /* Return plugin informations for acl handling */
391 static function plInfo()
392 {
393 return (array(
394 "plShortName" => _("Variable"),
395 "plDescription" => _("FAI variable"),
396 "plSelfModify" => FALSE,
397 "plDepends" => array(),
398 "plPriority" => 22,
399 "plSection" => array("administration"),
400 "plCategory" => array("fai"),
401 "plProvidedAcls" => array(
402 "cn" => _("Name")." ("._("Read only").")",
403 "description" => _("Description"))
404 ));
405 }
408 /*! \brief Used for copy & paste.
409 Returns a HTML input mask, which allows to change the cn of this entry.
410 @param Array Array containing current status && a HTML template.
411 */
412 function getCopyDialog()
413 {
414 $vars = array("cn");
415 $smarty = get_smarty();
416 $smarty->assign("cn", htmlentities($this->cn));
417 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
418 $ret = array();
419 $ret['string'] = $str;
420 $ret['status'] = "";
421 return($ret);
422 }
425 /*! \brief Used for copy & paste.
426 Some entries must be renamed to avaoid duplicate entries.
427 */
428 function saveCopyDialog()
429 {
430 if(isset($_POST['cn'])){
431 $this->cn = get_post('cn');
432 }
433 }
435 /* Reload some attributes */
436 function get_object_attributes($object,$attributes)
437 {
438 $ldap = $this->config->get_ldap_link();
439 $ldap->cd($this->config->current['BASE']);
440 $ldap->cat($object['dn'],$attributes);
441 $tmp = $ldap->fetch();
443 foreach($attributes as $attrs){
444 if(isset($tmp[$attrs][0])){
445 $var = $tmp[$attrs][0];
447 /* Check if we must decode some attributes */
448 if(in_array_ics($attrs,$this->sub64coded)){
449 $var = base64_decode($var);
450 }
452 /* check if this is a binary entry */
453 if(in_array_ics($attrs,$this->subBinary)){
454 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
455 }
457 /* Fix slashes */
458 $var = addslashes($var);
459 $object[$attrs] = $var;
460 }
461 }
462 return($object);
463 }
465 }
467 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
468 ?>