1 <?php
3 class faiVariable extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIvariable");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIvariableEntry";
21 var $subClasses = array("top","FAIclass","FAIvariableEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiVariableEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAIvariableContent");
28 var $sub64coded = array();
30 /* Specific attributes */
31 var $cn = ""; // The class name for this object
32 var $description = ""; // The description for this set of partitions
33 var $is_dialog = false; // specifies which buttons will be shown to save or abort
34 var $dialog = NULL; // a dialog, e.g. new disk dialog
35 var $SubObjects = array(); // All leafobjects of this object
37 var $FAIstate = "";
38 var $ui ;
39 var $view_logged = FALSE;
41 function faiVariable ($config, $dn= NULL)
42 {
43 /* Load Attributes */
44 plugin::plugin ($config, $dn);
46 if($dn != "new"){
47 $this->dn =$dn;
49 /* Get FAIstate
50 */
51 if(isset($this->attrs['FAIstate'][0])){
52 $this->FAIstate = $this->attrs['FAIstate'][0];
53 }
55 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
56 */
57 $ldap = $this->config->get_ldap_link();
58 $ldap->cd ($this->dn);
60 $attrs_to_search = $this->subAttributes;
61 $attrs_to_search[] = "FAIstate";
62 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
64 while($object = $ldap->fetch()){
66 /* Skip objects, that are tagged as removed */
67 if(isset($object['FAIstate'][0])){
68 if(preg_match("/removed$/",$object['FAIstate'][0])){
69 continue;
70 }
71 }
73 /* Set status for save management */
74 foreach($this->subAttributes as $attrs){
75 if(!isset($object[$attrs][0])){
76 $this->SubObjects[$object['cn'][0]][$attrs]="";
77 }else{
78 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
79 }
80 }
82 foreach($this->sub64coded as $codeIt){
83 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
84 }
86 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
87 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
88 }
90 }
91 $this->ui = get_userinfo();
92 }
95 function acl_base_for_current_object($dn)
96 {
97 if($dn == "new"){
98 if($this->dn == "new"){
99 $dn= $_SESSION['CurrentMainBase'];
100 }else{
101 $dn = $this->dn;
102 }
103 }
104 return($dn);
105 }
108 function execute()
109 {
110 /* Call parent execute */
111 plugin::execute();
113 if($this->is_account && !$this->view_logged){
114 $this->view_logged = TRUE;
115 new log("view","fai/".get_class($this),$this->dn);
116 }
118 /* Fill templating stuff */
119 $smarty= get_smarty();
120 $display= "";
122 /* Add new sub object */
123 if(isset($_POST['AddSubObject'])){
124 $this->dialog= new $this->subClassName($this->config,"new");
125 $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
126 $this->dialog->set_acl_category("fai");
127 $this->is_dialog=true;
128 }
130 if($this->dn != "new"){
131 $_SESSION['objectinfo']= $this->dn;
132 }
135 /* Edit selected Sub Object */
136 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
138 $var = $_POST['SubObject'][0];
139 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
140 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
141 $this->dialog->set_acl_category("fai");
142 $this->dialog->set_acl_base($c_dn);
143 $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
144 $this->is_dialog=true;
145 }
147 /* Remove Sub object */
148 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
149 foreach($_POST['SubObject'] as $var){
151 $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
152 $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
153 if(preg_match("/d/",$acl)){
154 if($this->SubObjects[$var]['status'] == "edited"){
155 $this->SubObjects[$var]['status']= "delete";
156 }else{
157 unset($this->SubObjects[$var]);
158 }
159 }
160 }
161 }
163 /* Save Dialog */
164 if(isset($_POST['SaveSubObject'])){
165 $this->dialog->save_object();
166 $msgs = $this->dialog->check();
167 if(count($msgs)>0){
168 foreach($msgs as $msg){
169 print_red($msg);
170 }
171 }else{
172 $obj = $this->dialog->save();
173 if(isset($obj['remove'])){
174 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
175 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
176 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
177 unset($this->SubObjects[$obj['remove']['from']]);
178 }
179 $obj['status'] = "new";
180 $this->SubObjects[$obj['remove']['to']] = $obj;
181 unset($this->SubObjects[$obj['remove']['to']]['remove']);
182 }else{
183 $this->SubObjects[$obj['cn']]=$obj;
184 }
185 $this->is_dialog=false;
186 unset($this->dialog);
187 $this->dialog=NULL;
188 }
189 }
191 /* Sort entries */
192 $tmp = $keys = array();
193 foreach($this->SubObjects as $key => $entry){
194 $keys[$key]=$key;
195 }
196 natcasesort($keys);
197 foreach($keys as $key){
198 $tmp[$key]=$this->SubObjects[$key];
199 }
200 $this->SubObjects = $tmp;
202 /* Cancel Dialog */
203 if(isset($_POST['CancelSubObject'])){
204 $this->is_dialog=false;
205 unset($this->dialog);
206 $this->dialog=NULL;
207 }
209 /* Print dialog if $this->dialog is set */
210 if($this->dialog){
211 $this->dialog->save_object();
212 $display = $this->dialog->execute();
213 return($display);
214 }
216 $ui = get_userinfo();
217 $ret = $this->getList();
218 $tmp = array();
219 foreach($this->SubObjects as $key => $obj){
220 $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
221 if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
222 $tmp[$key] = $ret[$key];
223 }
224 }
225 $smarty->assign("SubObjects",$tmp);
228 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
229 * If we post the escaped strings they will be escaped again
230 */
231 foreach($this->attributes as $attrs){
232 if(get_magic_quotes_gpc()){
233 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
234 }else{
235 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
236 }
237 }
239 $c_dn = $this->acl_base_for_current_object($this->dn);
240 $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
241 $smarty->assign("is_removeable", preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
243 $tmp = $this->plInfo();
244 foreach($tmp['plProvidedAcls'] as $name => $translation) {
245 $smarty->assign($name."ACL",$this->getacl($name));
246 }
249 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
250 return($display);
251 }
253 /* Generate listbox friendly SubObject list
254 */
255 function getList(){
256 $a_return=array();
257 foreach($this->SubObjects as $obj){
258 if($obj['status'] != "delete"){
260 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
261 if (preg_match("/\[\*\]/", $obj['description'])){
262 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
263 } else {
264 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
265 }
266 }else{
267 if (preg_match("/\[\*\]/", $obj['description'])){
268 $a_return[$obj['cn']]= $obj['cn'];
269 } else {
270 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
271 }
272 }
273 }
274 }
275 return($a_return);
276 }
278 /* Delete me, and all my subtrees
279 */
280 function remove_from_parent()
281 {
282 if($this->acl_is_removeable()){
283 $ldap = $this->config->get_ldap_link();
284 $ldap->cd ($this->dn);
286 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
287 if($_SESSION['faifilter']['branch'] == "main"){
288 $use_dn = $this->dn;
289 }
291 prepare_to_save_FAI_object($use_dn,array(),true);
292 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
294 foreach($this->SubObjects as $name => $obj){
295 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
296 if($_SESSION['faifilter']['branch'] == "main"){
297 $use_dn = $obj['dn'];
298 }
299 prepare_to_save_FAI_object($use_dn,array(),true);
300 }
301 $this->handle_post_events("remove");
302 }
303 }
306 /* Save data to object
307 */
308 function save_object()
309 {
310 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
311 plugin::save_object();
312 foreach($this->attributes as $attrs){
313 if(isset($_POST[$attrs])){
314 $this->$attrs = $_POST[$attrs];
315 }
316 }
317 }
318 }
321 /* Check supplied data */
322 function check()
323 {
324 /* Call common method to give check the hook */
325 $message= plugin::check();
327 return ($message);
328 }
331 /* Save to LDAP */
332 function save()
333 {
334 plugin::save();
336 $ldap = $this->config->get_ldap_link();
337 prepare_to_save_FAI_object($this->dn,$this->attrs);
338 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
340 if($this->initially_was_account){
341 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
342 }else{
343 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
344 }
346 /* Do object tagging */
347 $this->handle_object_tagging();
349 /* Prepare FAIscriptEntry to write it to ldap
350 * First sort array.
351 * Because we must delete old entries first.
352 * After deletion, we perform add and modify
353 */
354 $Objects = array();
355 foreach($this->SubObjects as $name => $obj){
356 if($obj['status'] == "delete"){
357 $Objects[$name] = $obj;
358 }
359 }
360 foreach($this->SubObjects as $name => $obj){
361 if($obj['status'] != "delete"){
362 $Objects[$name] = $obj;
363 }
364 }
366 foreach($Objects as $name => $obj){
368 foreach($this->sub64coded as $codeIt){
369 $obj[$codeIt]=base64_encode($obj[$codeIt]);
370 }
372 $tmp = array();
373 foreach($this->subAttributes as $attrs){
374 if(empty($obj[$attrs])){
375 $obj[$attrs] = array();
376 }
377 $tmp[$attrs] = $obj[$attrs];
378 }
380 $tmp['objectClass'] = $this->subClasses;
382 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
384 if($obj['status']=="new"){
385 $ldap->cat($sub_dn,array("objectClass"));
386 if($ldap->count()){
387 $obj['status']="edited";
388 }
389 }
391 /* Check if gosaAdministrativeUnitTag is required as object class */
392 if($obj['status'] == "edited"){
393 $ldap->cat($sub_dn,array("objectClass"));
394 $attrs = $ldap->fetch();
395 if(isset($attrs['objectClass'])){
396 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
397 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
398 }
399 }
400 }
402 if($obj['status'] == "delete"){
403 prepare_to_save_FAI_object($sub_dn,array(),true);
404 $this->handle_post_events("remove");
405 }elseif($obj['status'] == "edited"){
406 prepare_to_save_FAI_object($sub_dn,$tmp);
407 $this->handle_post_events("modify");
408 }elseif($obj['status']=="new"){
409 prepare_to_save_FAI_object($sub_dn,$tmp);
410 $this->handle_post_events("add");
411 }
413 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
414 }
415 }
418 /* Return plugin informations for acl handling */
419 function plInfo()
420 {
421 return (array(
422 "plShortName" => _("Variable"),
423 "plDescription" => _("FAI variable"),
424 "plSelfModify" => FALSE,
425 "plDepends" => array(),
426 "plPriority" => 22,
427 "plSection" => array("administration"),
428 "plCategory" => array("fai"),
429 "plProvidedAcls" => array(
430 "cn" => _("Name")." ("._("Read only").")",
431 "description" => _("Description"))
432 ));
433 }
434 }
436 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
437 ?>