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_leaf_objects($this->dn, $this->cn, $this->subClass, get_ou('faiVariableRDN'));
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= $this->parent->parent->acl_base;
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 set_object_info($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 set_object_info($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 if (preg_match("/^-/", $this->cn)) {
301 $message[] = sprintf(_("The specified classname '%s' is invalid. Classnames must not start with a dash."), $this->cn);
302 }
304 if($this->cn != preg_replace("/ /","",trim($this->cn))){
305 $message[] = msgPool::invalid(_("Name"),preg_replace("/ /","_",$this->cn),"/[^_]/");
306 }
308 return ($message);
309 }
312 /* Save to LDAP */
313 function save()
314 {
315 plugin::save();
317 $ldap = $this->config->get_ldap_link();
318 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
320 if($this->initially_was_account){
321 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
322 }else{
323 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
324 }
326 /* Prepare FAIscriptEntry to write it to ldap
327 * First sort array.
328 * Because we must delete old entries first.
329 * After deletion, we perform add and modify
330 */
331 $Objects = $this->SubObjects;
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(!isset($obj[$attrs]) || $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 /* Tag object */
358 $ui= get_userinfo();
359 $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
361 if($obj['status'] == "delete"){
362 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
363 $this->handle_post_events("remove");
364 }elseif($obj['status'] == "edited"){
365 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
366 $this->handle_post_events("modify");
367 }elseif($obj['status']=="new"){
368 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
369 $this->handle_post_events("add");
370 }
372 }
373 }
376 function PrepareForCopyPaste($source)
377 {
378 plugin::PrepareForCopyPaste($source);
380 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
381 */
382 $res = FAI::get_leaf_objects($source['dn'], $source['cn'][0], $this->subClass, get_ou('faiVariableRDN'));
383 foreach($res as $obj){
384 $objects = array();
385 $objects['status'] = "edited";
386 $objects['dn'] = $obj['dn'];
387 $objects = $this->get_object_attributes($objects,$this->subAttributes);
388 $this->SubObjects[$objects['cn']] = $objects;
389 }
390 }
393 /* Return plugin informations for acl handling */
394 static function plInfo()
395 {
396 return (array(
397 "plShortName" => _("Variable"),
398 "plDescription" => _("FAI variable"),
399 "plSelfModify" => FALSE,
400 "plDepends" => array(),
401 "plPriority" => 22,
402 "plSection" => array("administration"),
403 "plCategory" => array("fai"),
404 "plProvidedAcls" => array(
405 "cn" => _("Name")." ("._("Read only").")",
406 "description" => _("Description"))
407 ));
408 }
411 /*! \brief Used for copy & paste.
412 Returns a HTML input mask, which allows to change the cn of this entry.
413 @param Array Array containing current status && a HTML template.
414 */
415 function getCopyDialog()
416 {
417 $vars = array("cn");
418 $smarty = get_smarty();
419 $smarty->assign("cn", htmlentities($this->cn));
420 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
421 $ret = array();
422 $ret['string'] = $str;
423 $ret['status'] = "";
424 return($ret);
425 }
428 /*! \brief Used for copy & paste.
429 Some entries must be renamed to avaoid duplicate entries.
430 */
431 function saveCopyDialog()
432 {
433 if(isset($_POST['cn'])){
434 $this->cn = get_post('cn');
435 }
436 }
438 /* Reload some attributes */
439 function get_object_attributes($object,$attributes)
440 {
441 $ldap = $this->config->get_ldap_link();
442 $ldap->cd($this->config->current['BASE']);
443 $ldap->cat($object['dn'],$attributes);
444 $tmp = $ldap->fetch();
446 foreach($attributes as $attrs){
447 if(isset($tmp[$attrs][0])){
448 $var = $tmp[$attrs][0];
450 /* Check if we must decode some attributes */
451 if(in_array_ics($attrs,$this->sub64coded)){
452 $var = base64_decode($var);
453 }
455 /* check if this is a binary entry */
456 if(in_array_ics($attrs,$this->subBinary)){
457 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
458 }
460 /* Fix slashes */
461 $var = addslashes($var);
462 $object[$attrs] = $var;
463 }
464 }
465 return($object);
466 }
468 }
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>