81a411dc725e6aa5dcf3bbf469b3833b88abcb20
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);
71 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
73 while($object = $ldap->fetch()){
74 /* Set status for save management */
76 foreach($this->subAttributes as $attrs){
77 if(!isset($object[$attrs][0])){
78 $this->SubObjects[$object['cn'][0]][$attrs]="";
79 }else{
80 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
81 }
82 }
84 foreach($this->sub64coded as $codeIt){
85 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
86 }
88 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
89 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
90 }
91 }
92 }
94 function execute()
95 {
96 /* Call parent execute */
97 plugin::execute();
99 /* Fill templating stuff */
100 $smarty= get_smarty();
101 $display= "";
103 /* Add new sub object */
104 if(isset($_POST['AddSubObject'])){
105 $this->dialog= new $this->subClassName($this->config,"new");
106 $this->dialog->acl = $this->acl;
107 $this->is_dialog=true;
108 }
110 if($this->dn != "new"){
111 $_SESSION['objectinfo']= $this->dn;
112 }
115 /* Edit selected Sub Object */
116 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
118 $var = $_POST['SubObject'][0];
120 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
121 $this->dialog->acl = $this->acl;
122 $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
123 $this->is_dialog=true;
124 }
126 /* Remove Sub object */
127 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
128 foreach($_POST['SubObject'] as $var){
129 if($this->SubObjects[$var]['status'] == "edited"){
130 $this->SubObjects[$var]['status']= "delete";
131 }else{
132 unset($this->SubObjects[$var]);
133 }
134 }
135 }
137 /* Save Dialog */
138 if(isset($_POST['SaveSubObject'])){
139 $this->dialog->save_object();
140 $msgs = $this->dialog->check();
141 if(count($msgs)>0){
142 foreach($msgs as $msg){
143 print_red($msg);
144 }
145 }else{
146 $obj = $this->dialog->save();
147 if(isset($obj['remove'])){
148 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
149 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
150 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
151 unset($this->SubObjects[$obj['remove']['from']]);
152 }
153 $obj['status'] = "new";
154 $this->SubObjects[$obj['remove']['to']] = $obj;
155 unset($this->SubObjects[$obj['remove']['to']]['remove']);
156 }else{
157 $this->SubObjects[$obj['cn']]=$obj;
158 }
159 $this->is_dialog=false;
160 unset($this->dialog);
161 $this->dialog=NULL;
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=NULL;
181 }
183 /* Print dialog if $this->dialog is set */
184 if($this->dialog){
185 $this->dialog->save_object();
186 $display = $this->dialog->execute();
187 return($display);
188 }
190 $smarty->assign("SubObjects",$this->getList());
191 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
193 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
194 * If we post the escaped strings they will be escaped again
195 */
197 foreach($this->attributes as $attrs){
198 if(get_magic_quotes_gpc()){
199 $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
200 }else{
201 $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
202 }
203 }
205 foreach($this->attributes as $attr){
206 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
207 }
210 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
211 return($display);
212 }
214 /* Generate listbox friendly SubObject list
215 */
216 function getList(){
217 $a_return=array();
218 foreach($this->SubObjects as $obj){
219 if($obj['status'] != "delete"){
221 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
222 if (preg_match("/\[\*\]/", $obj['description'])){
223 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
224 } else {
225 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
226 }
227 }else{
228 if (preg_match("/\[\*\]/", $obj['description'])){
229 $a_return[$obj['cn']]= $obj['cn'];
230 } else {
231 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
232 }
233 }
234 }
235 }
236 return($a_return);
237 }
239 /* Delete me, and all my subtrees
240 */
241 function remove_from_parent()
242 {
243 $ldap = $this->config->get_ldap_link();
244 $ldap->cd ($this->dn);
245 $ldap->rmdir_recursive($this->dn);
246 $this->handle_post_events("remove");
247 }
250 /* Save data to object
251 */
252 function save_object()
253 {
254 if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
255 plugin::save_object();
256 foreach($this->attributes as $attrs){
257 if(isset($_POST[$attrs])){
258 $this->$attrs = $_POST[$attrs];
259 }
260 }
261 }
262 }
265 /* Check supplied data */
266 function check()
267 {
268 /* Call common method to give check the hook */
269 $message= plugin::check();
271 return ($message);
272 }
275 /* Save to LDAP */
276 function save()
277 {
278 plugin::save();
280 $ldap = $this->config->get_ldap_link();
282 $ldap->cat($this->dn);
283 if($ldap->count()!=0){
284 /* Write FAIscript to ldap*/
285 $ldap->cd($this->dn);
286 $this->cleanup();
287 $ldap->modify ($this->attrs);
289 }else{
290 /* Write FAIscript to ldap*/
291 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
292 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
293 $ldap->cd($this->dn);
294 $ldap->add($this->attrs);
295 }
296 show_ldap_error($ldap->get_error());
298 /* Prepare FAIscriptEntry to write it to ldap
299 * First sort array.
300 * Because we must delete old entries first.
301 * After deletion, we perform add and modify
302 */
303 $Objects = array();
304 foreach($this->SubObjects as $name => $obj){
305 if($obj['status'] == "delete"){
306 $Objects[$name] = $obj;
307 }
308 }
309 foreach($this->SubObjects as $name => $obj){
310 if($obj['status'] != "delete"){
311 $Objects[$name] = $obj;
312 }
313 }
315 foreach($Objects as $name => $obj){
317 foreach($this->sub64coded as $codeIt){
318 $obj[$codeIt]=base64_encode($obj[$codeIt]);
319 }
321 $tmp = array();
322 foreach($this->subAttributes as $attrs){
323 if(empty($obj[$attrs])){
324 $obj[$attrs] = array();
325 }
326 $tmp[$attrs] = $obj[$attrs];
327 }
329 $tmp['objectClass'] = $this->subClasses;
331 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
333 if($obj['status']=="new"){
334 $ldap->cat($sub_dn);
335 if($ldap->count()){
336 $obj['status']="modify";
337 }
338 }
340 if($obj['status'] == "delete"){
341 $ldap->cd($sub_dn);
342 $ldap->rmdir_recursive($sub_dn);
343 $this->handle_post_events("remove");
344 }elseif($obj['status'] == "edited"){
345 $ldap->cd($sub_dn);
346 $this->cleanup();
347 $ldap->modify ($tmp);
349 $this->handle_post_events("modify");
350 }elseif($obj['status']=="new"){
352 if($tmp['description'] == array()){
353 unset($tmp['description']);
354 }
356 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
357 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
358 $ldap->cd($sub_dn);
359 $ldap->add($tmp);
360 $this->handle_post_events("add");
361 }
362 show_ldap_error($ldap->get_error());
363 }
364 }
365 }
367 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
368 ?>