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 = "";
39 function faiVariable ($config, $dn= NULL)
40 {
41 /* Load Attributes */
42 plugin::plugin ($config, $dn);
44 $this->acl ="#all#";
46 /* If "dn==new" we try to create a new entry
47 * Else we must read all objects from ldap which belong to this entry.
48 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
49 */
51 if($dn != "new"){
52 $this->dn =$dn;
54 /* Set acls
55 */
56 $ui = get_userinfo();
57 $acl = get_permissions ($this->dn, $ui->subtreeACL);
58 $acli = get_module_permission($acl, "FAIclass", $this->dn);
59 $this->acl=$acli;
61 /* Get FAIstate
62 */
63 if(isset($this->attrs['FAIstate'][0])){
64 $this->FAIstate = $this->attrs['FAIstate'][0];
65 }
67 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68 */
69 $ldap = $this->config->get_ldap_link();
70 $ldap->cd ($this->dn);
72 $attrs_to_search = $this->subAttributes;
73 $attrs_to_search[] = "FAIstate";
74 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
76 while($object = $ldap->fetch()){
78 /* Skip objects, that are tagged as removed */
79 if(isset($object['FAIstate'][0])){
80 if(preg_match("/removed$/",$object['FAIstate'][0])){
81 continue;
82 }
83 }
85 /* Set status for save management */
86 foreach($this->subAttributes as $attrs){
87 if(!isset($object[$attrs][0])){
88 $this->SubObjects[$object['cn'][0]][$attrs]="";
89 }else{
90 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
91 }
92 }
94 foreach($this->sub64coded as $codeIt){
95 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
96 }
98 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
99 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
100 }
101 }
102 }
104 function execute()
105 {
106 /* Call parent execute */
107 plugin::execute();
109 /* Fill templating stuff */
110 $smarty= get_smarty();
111 $display= "";
113 /* Add new sub object */
114 if(isset($_POST['AddSubObject'])){
115 $this->dialog= new $this->subClassName($this->config,"new");
116 $this->dialog->acl = $this->acl;
117 $this->is_dialog=true;
118 }
120 if($this->dn != "new"){
121 $_SESSION['objectinfo']= $this->dn;
122 }
125 /* Edit selected Sub Object */
126 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
128 $var = $_POST['SubObject'][0];
129 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
130 $this->dialog->acl = $this->acl;
131 $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
132 $this->is_dialog=true;
133 }
135 /* Remove Sub object */
136 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
137 foreach($_POST['SubObject'] as $var){
138 if($this->SubObjects[$var]['status'] == "edited"){
139 $this->SubObjects[$var]['status']= "delete";
140 }else{
141 unset($this->SubObjects[$var]);
142 }
143 }
144 }
146 /* Save Dialog */
147 if(isset($_POST['SaveSubObject'])){
148 $this->dialog->save_object();
149 $msgs = $this->dialog->check();
150 if(count($msgs)>0){
151 foreach($msgs as $msg){
152 print_red($msg);
153 }
154 }else{
155 $obj = $this->dialog->save();
156 if(isset($obj['remove'])){
157 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
158 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
159 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
160 unset($this->SubObjects[$obj['remove']['from']]);
161 }
162 $obj['status'] = "new";
163 $this->SubObjects[$obj['remove']['to']] = $obj;
164 unset($this->SubObjects[$obj['remove']['to']]['remove']);
165 }else{
166 $this->SubObjects[$obj['cn']]=$obj;
167 }
168 $this->is_dialog=false;
169 unset($this->dialog);
170 $this->dialog=NULL;
171 }
172 }
174 /* Sort entries */
175 $tmp = $keys = array();
176 foreach($this->SubObjects as $key => $entry){
177 $keys[$key]=$key;
178 }
179 natcasesort($keys);
180 foreach($keys as $key){
181 $tmp[$key]=$this->SubObjects[$key];
182 }
183 $this->SubObjects = $tmp;
185 /* Cancel Dialog */
186 if(isset($_POST['CancelSubObject'])){
187 $this->is_dialog=false;
188 unset($this->dialog);
189 $this->dialog=NULL;
190 }
192 /* Print dialog if $this->dialog is set */
193 if($this->dialog){
194 $this->dialog->save_object();
195 $display = $this->dialog->execute();
196 return($display);
197 }
199 $smarty->assign("SubObjects",$this->getList());
201 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
202 * If we post the escaped strings they will be escaped again
203 */
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 foreach($this->attributes as $attr){
214 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
215 }
218 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
219 return($display);
220 }
222 /* Generate listbox friendly SubObject list
223 */
224 function getList(){
225 $a_return=array();
226 foreach($this->SubObjects as $obj){
227 if($obj['status'] != "delete"){
229 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
230 if (preg_match("/\[\*\]/", $obj['description'])){
231 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
232 } else {
233 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
234 }
235 }else{
236 if (preg_match("/\[\*\]/", $obj['description'])){
237 $a_return[$obj['cn']]= $obj['cn'];
238 } else {
239 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
240 }
241 }
242 }
243 }
244 return($a_return);
245 }
247 /* Delete me, and all my subtrees
248 */
249 function remove_from_parent()
250 {
251 $ldap = $this->config->get_ldap_link();
252 $ldap->cd ($this->dn);
254 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
255 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
256 if($_SESSION['faifilter']['branch'] == "main"){
257 $use_dn = $this->dn;
258 }
260 prepare_to_save_FAI_object($use_dn,array(),true);
262 foreach($this->SubObjects as $name => $obj){
263 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
264 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
265 if($_SESSION['faifilter']['branch'] == "main"){
266 $use_dn = $obj['dn'];
267 }
268 prepare_to_save_FAI_object($use_dn,array(),true);
269 }
270 $this->handle_post_events("remove");
271 }
274 /* Save data to object
275 */
276 function save_object()
277 {
278 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
279 plugin::save_object();
280 foreach($this->attributes as $attrs){
281 if(isset($_POST[$attrs])){
282 $this->$attrs = $_POST[$attrs];
283 }
284 }
285 }
286 }
289 /* Check supplied data */
290 function check()
291 {
292 /* Call common method to give check the hook */
293 $message= plugin::check();
295 return ($message);
296 }
299 /* Save to LDAP */
300 function save()
301 {
302 plugin::save();
304 $ldap = $this->config->get_ldap_link();
305 prepare_to_save_FAI_object($this->dn,$this->attrs);
306 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
308 /* Do object tagging */
309 $this->handle_object_tagging();
311 /* Prepare FAIscriptEntry to write it to ldap
312 * First sort array.
313 * Because we must delete old entries first.
314 * After deletion, we perform add and modify
315 */
316 $Objects = array();
317 foreach($this->SubObjects as $name => $obj){
318 if($obj['status'] == "delete"){
319 $Objects[$name] = $obj;
320 }
321 }
322 foreach($this->SubObjects as $name => $obj){
323 if($obj['status'] != "delete"){
324 $Objects[$name] = $obj;
325 }
326 }
328 foreach($Objects as $name => $obj){
330 foreach($this->sub64coded as $codeIt){
331 $obj[$codeIt]=base64_encode($obj[$codeIt]);
332 }
334 $tmp = array();
335 foreach($this->subAttributes as $attrs){
336 if(empty($obj[$attrs])){
337 $obj[$attrs] = array();
338 }
339 $tmp[$attrs] = $obj[$attrs];
340 }
342 $tmp['objectClass'] = $this->subClasses;
344 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
346 if($obj['status']=="new"){
347 $ldap->cat($sub_dn,array("objectClass"));
348 if($ldap->count()){
349 $obj['status']="edited";
350 }
351 }
353 /* Check if gosaAdministrativeUnitTag is required as object class */
354 if($obj['status'] == "edited"){
355 $ldap->cat($sub_dn,array("objectClass"));
356 $attrs = $ldap->fetch();
357 if(isset($attrs['objectClass'])){
358 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
359 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
360 }
361 }
362 }
364 if($obj['status'] == "delete"){
365 prepare_to_save_FAI_object($sub_dn,array(),true);
366 $this->handle_post_events("remove");
367 }elseif($obj['status'] == "edited"){
368 prepare_to_save_FAI_object($sub_dn,$tmp);
369 $this->handle_post_events("modify");
370 }elseif($obj['status']=="new"){
371 prepare_to_save_FAI_object($sub_dn,$tmp);
372 $this->handle_post_events("add");
373 }
375 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
376 }
377 }
380 /* Return plugin informations for acl handling */
381 function plInfo()
382 {
383 return (array(
384 "plShortName" => _("Variable"),
385 "plDescription" => _("FAI variable"),
386 "plSelfModify" => FALSE,
387 "plDepends" => array(),
388 "plPriority" => 0,
389 "plSection" => array("administration"),
390 "plCategory" => array("fai"),
391 "plProvidedAcls" => array(
392 "cn" => _("Name"),
393 "description" => _("Description"),
394 "FAIvariableContent"=> _("Variable content"))
395 ));
396 }
397 }
399 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
400 ?>