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 function faiVariable ($config, $dn= NULL)
38 {
39 /* Load Attributes */
40 plugin::plugin ($config, $dn);
42 /* If "dn==new" we try to create a new entry
43 * Else we must read all objects from ldap which belong to this entry.
44 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
45 */
46 if($dn != "new"){
47 $this->dn =$dn;
49 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50 */
51 $ldap = $this->config->get_ldap_link();
52 $ldap->cd ($this->dn);
53 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
55 while($object = $ldap->fetch()){
56 /* Set status for save management */
58 foreach($this->subAttributes as $attrs){
59 if(!isset($object[$attrs][0])){
60 $this->SubObjects[$object['cn'][0]][$attrs]="";
61 }else{
62 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
63 }
64 }
66 foreach($this->sub64coded as $codeIt){
67 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
68 }
70 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
71 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
72 }
73 }
74 }
76 function execute()
77 {
78 /* Call parent execute */
79 plugin::execute();
81 /* Fill templating stuff */
82 $smarty= get_smarty();
83 $display= "";
85 /* Add new sub object */
86 if(isset($_POST['AddSubObject'])){
87 $this->dialog= new $this->subClassName($this->config,"new");
88 $this->is_dialog=true;
89 }
91 if($this->dn != "new"){
92 $_SESSION['objectinfo']= $this->dn;
93 }
96 /* Edit selected Sub Object */
97 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
99 $var = $_POST['SubObject'][0];
101 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
102 $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
103 $this->is_dialog=true;
104 }
106 /* Remove Sub object */
107 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
108 foreach($_POST['SubObject'] as $var){
109 if($this->SubObjects[$var]['status'] == "edited"){
110 $this->SubObjects[$var]['status']= "delete";
111 }else{
112 unset($this->SubObjects[$var]);
113 }
114 }
115 }
117 /* Save Dialog */
118 if(isset($_POST['SaveSubObject'])){
119 $this->dialog->save_object();
120 $msgs = $this->dialog->check();
121 if(count($msgs)>0){
122 foreach($msgs as $msg){
123 print_red($msg);
124 }
125 }else{
126 $obj = $this->dialog->save();
127 if(isset($obj['remove'])){
128 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
129 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
130 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
131 unset($this->SubObjects[$obj['remove']['from']]);
132 }
133 $obj['status'] = "new";
134 $this->SubObjects[$obj['remove']['to']] = $obj;
135 unset($this->SubObjects[$obj['remove']['to']]['remove']);
136 }else{
137 $this->SubObjects[$obj['cn']]=$obj;
138 }
139 $this->is_dialog=false;
140 unset($this->dialog);
141 $this->dialog=NULL;
142 }
143 }
145 /* Sort entries */
146 $tmp = $keys = array();
147 foreach($this->SubObjects as $key => $entry){
148 $keys[$key]=$key;
149 }
150 natcasesort($keys);
151 foreach($keys as $key){
152 $tmp[$key]=$this->SubObjects[$key];
153 }
154 $this->SubObjects = $tmp;
156 /* Cancel Dialog */
157 if(isset($_POST['CancelSubObject'])){
158 $this->is_dialog=false;
159 unset($this->dialog);
160 $this->dialog=NULL;
161 }
163 /* Print dialog if $this->dialog is set */
164 if($this->dialog){
165 $this->dialog->save_object();
166 $display = $this->dialog->execute();
167 return($display);
168 }
170 $smarty->assign("SubObjects",$this->getList());
171 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
173 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
174 * If we post the escaped strings they will be escaped again
175 */
177 foreach($this->attributes as $attrs){
178 if(get_magic_quotes_gpc()){
179 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
180 }else{
181 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
182 }
183 }
185 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
186 return($display);
187 }
189 /* Generate listbox friendly SubObject list
190 */
191 function getList(){
192 $a_return=array();
193 foreach($this->SubObjects as $obj){
194 if($obj['status'] != "delete"){
196 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
197 if (preg_match("/\[\*\]/", $obj['description'])){
198 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
199 } else {
200 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
201 }
202 }else{
203 if (preg_match("/\[\*\]/", $obj['description'])){
204 $a_return[$obj['cn']]= $obj['cn'];
205 } else {
206 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
207 }
208 }
209 }
210 }
211 return($a_return);
212 }
214 /* Delete me, and all my subtrees
215 */
216 function remove_from_parent()
217 {
218 $ldap = $this->config->get_ldap_link();
219 $ldap->cd ($this->dn);
220 $ldap->rmdir_recursive($this->dn);
221 $this->handle_post_events("remove");
222 }
225 /* Save data to object
226 */
227 function save_object()
228 {
229 if(isset($_POST['FAIvariable_posted'])){
230 plugin::save_object();
231 foreach($this->attributes as $attrs){
232 if(isset($_POST[$attrs])){
233 $this->$attrs = $_POST[$attrs];
234 }
235 }
236 }
237 }
240 /* Check supplied data */
241 function check()
242 {
243 $message= array();
244 return ($message);
245 }
248 /* Save to LDAP */
249 function save()
250 {
251 plugin::save();
253 $ldap = $this->config->get_ldap_link();
255 $ldap->cat($this->dn);
256 if($ldap->count()!=0){
257 /* Write FAIscript to ldap*/
258 $ldap->cd($this->dn);
259 $ldap->modify($this->attrs);
260 }else{
261 /* Write FAIscript to ldap*/
262 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
263 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
264 $ldap->cd($this->dn);
265 $ldap->add($this->attrs);
266 }
267 show_ldap_error($ldap->get_error());
269 /* Prepare FAIscriptEntry to write it to ldap
270 * First sort array.
271 * Because we must delete old entries first.
272 * After deletion, we perform add and modify
273 */
274 $Objects = array();
275 foreach($this->SubObjects as $name => $obj){
276 if($obj['status'] == "delete"){
277 $Objects[$name] = $obj;
278 }
279 }
280 foreach($this->SubObjects as $name => $obj){
281 if($obj['status'] != "delete"){
282 $Objects[$name] = $obj;
283 }
284 }
286 foreach($Objects as $name => $obj){
288 foreach($this->sub64coded as $codeIt){
289 $obj[$codeIt]=base64_encode($obj[$codeIt]);
290 }
292 $tmp = array();
293 foreach($this->subAttributes as $attrs){
294 if(empty($obj[$attrs])){
295 $obj[$attrs] = array();
296 }
297 $tmp[$attrs] = $obj[$attrs];
298 }
300 $tmp['objectClass'] = $this->subClasses;
302 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
304 if($obj['status']=="new"){
305 $ldap->cat($sub_dn);
306 if($ldap->count()){
307 $obj['status']="modify";
308 }
309 }
311 if($obj['status'] == "delete"){
312 $ldap->cd($sub_dn);
313 $ldap->rmdir_recursive($sub_dn);
314 $this->handle_post_events("remove");
315 }elseif($obj['status'] == "edited"){
316 $ldap->cd($sub_dn);
317 $ldap->modify($tmp);
318 $this->handle_post_events("modify");
319 }elseif($obj['status']=="new"){
321 if($tmp['description'] == array()){
322 unset($tmp['description']);
323 }
325 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
326 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
327 $ldap->cd($sub_dn);
328 $ldap->add($tmp);
329 $this->handle_post_events("add");
330 }
331 show_ldap_error($ldap->get_error());
332 }
333 }
334 }
336 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
337 ?>