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" || $dn == ""){
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("/".preg_quote(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("/".preg_quote(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 }
280 }
283 /* Check supplied data */
284 function check()
285 {
286 /* Call common method to give check the hook */
287 $message= plugin::check();
289 /* Ensure that we do not overwrite an allready existing entry
290 */
291 if($this->is_new){
292 $release = $this->parent->parent->fai_release;
293 $new_dn= 'cn='.$this->cn.",".get_ou('faiVariableRDN').get_ou('faiBaseRDN').$release;
294 $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
295 if(isset($res[$this->cn])){
296 $message[] = msgPool::duplicated(_("Name"));
297 }
298 }
300 return ($message);
301 }
304 /* Save to LDAP */
305 function save()
306 {
307 plugin::save();
309 $ldap = $this->config->get_ldap_link();
310 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
312 if($this->initially_was_account){
313 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
314 }else{
315 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
316 }
318 /* Prepare FAIscriptEntry to write it to ldap
319 * First sort array.
320 * Because we must delete old entries first.
321 * After deletion, we perform add and modify
322 */
323 $Objects = $this->SubObjects;
324 foreach($Objects as $name => $obj){
326 foreach($this->sub64coded as $codeIt){
327 $obj[$codeIt]=base64_encode($obj[$codeIt]);
328 }
330 $tmp = array();
331 foreach($this->subAttributes as $attrs){
332 if($obj[$attrs] == ""){
333 $obj[$attrs] = array();
334 }
335 $tmp[$attrs] = $obj[$attrs];
336 }
338 $tmp['objectClass'] = $this->subClasses;
340 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
342 if($obj['status']=="new"){
343 $ldap->cat($sub_dn,array("objectClass"));
344 if($ldap->count()){
345 $obj['status']="edited";
346 }
347 }
349 /* Tag object */
350 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
352 if($obj['status'] == "delete"){
353 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
354 $this->handle_post_events("remove");
355 }elseif($obj['status'] == "edited"){
356 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
357 $this->handle_post_events("modify");
358 }elseif($obj['status']=="new"){
359 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
360 $this->handle_post_events("add");
361 }
363 }
364 }
367 function PrepareForCopyPaste($source)
368 {
369 plugin::PrepareForCopyPaste($source);
371 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
372 */
373 $res = FAI::get_all_objects_for_given_object($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
374 foreach($res as $obj){
376 $objects = array();
377 $objects['status'] = "edited";
378 $objects['dn'] = $obj['dn'];
379 $objects = $this->get_object_attributes($objects,$this->subAttributes);
380 $this->SubObjects[$objects['cn']] = $objects;
381 }
382 }
385 /* Return plugin informations for acl handling */
386 static function plInfo()
387 {
388 return (array(
389 "plShortName" => _("Variable"),
390 "plDescription" => _("FAI variable"),
391 "plSelfModify" => FALSE,
392 "plDepends" => array(),
393 "plPriority" => 22,
394 "plSection" => array("administration"),
395 "plCategory" => array("fai"),
396 "plProvidedAcls" => array(
397 "cn" => _("Name")." ("._("Read only").")",
398 "description" => _("Description"))
399 ));
400 }
403 /*! \brief Used for copy & paste.
404 Returns a HTML input mask, which allows to change the cn of this entry.
405 @param Array Array containing current status && a HTML template.
406 */
407 function getCopyDialog()
408 {
409 $vars = array("cn");
410 $smarty = get_smarty();
411 $smarty->assign("cn", htmlentities($this->cn));
412 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
413 $ret = array();
414 $ret['string'] = $str;
415 $ret['status'] = "";
416 return($ret);
417 }
420 /*! \brief Used for copy & paste.
421 Some entries must be renamed to avaoid duplicate entries.
422 */
423 function saveCopyDialog()
424 {
425 if(isset($_POST['cn'])){
426 $this->cn = get_post('cn');
427 }
428 }
430 /* Reload some attributes */
431 function get_object_attributes($object,$attributes)
432 {
433 $ldap = $this->config->get_ldap_link();
434 $ldap->cd($this->config->current['BASE']);
435 $ldap->cat($object['dn'],$attributes);
436 $tmp = $ldap->fetch();
438 foreach($attributes as $attrs){
439 if(isset($tmp[$attrs][0])){
440 $var = $tmp[$attrs][0];
442 /* Check if we must decode some attributes */
443 if(in_array_ics($attrs,$this->sub64coded)){
444 $var = base64_decode($var);
445 }
447 /* check if this is a binary entry */
448 if(in_array_ics($attrs,$this->subBinary)){
449 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
450 }
452 /* Fix slashes */
453 $var = addslashes($var);
454 $object[$attrs] = $var;
455 }
456 }
457 return($object);
458 }
460 }
462 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
463 ?>