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 ksort($this->SubObjects);
74 }
75 }
77 function execute()
78 {
79 /* Call parent execute */
80 plugin::execute();
82 /* Fill templating stuff */
83 $smarty= get_smarty();
84 $display= "";
86 /* Add new sub object */
87 if(isset($_POST['AddSubObject'])){
88 $this->dialog= new $this->subClassName($this->config,"new");
89 $this->is_dialog=true;
90 }
92 $_SESSION['objectinfo'] = $this->dn;
93 /* Edit selected Sub Object */
94 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
95 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
96 $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
97 $this->is_dialog=true;
98 }
100 /* Remove Sub object */
101 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
102 if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
103 $this->SubObjects[$_POST['SubObject']]['status']= "delete";
104 }else{
105 unset($this->SubObjects[$_POST['SubObject']]);
106 }
107 }
109 /* Save Dialog */
110 if(isset($_POST['SaveSubObject'])){
111 $this->dialog->save_object();
112 $msgs = $this->dialog->check();
113 if(count($msgs)>0){
114 foreach($msgs as $msg){
115 print_red($msg);
116 }
117 }else{
118 $obj = $this->dialog->save();
119 if(isset($obj['remove'])){
120 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
121 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
122 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
123 unset($this->SubObjects[$obj['remove']['from']]);
124 }
125 $obj['status'] = "new";
126 $this->SubObjects[$obj['remove']['to']] = $obj;
127 unset($this->SubObjects[$obj['remove']['to']]['remove']);
128 }else{
129 $this->SubObjects[$obj['cn']]=$obj;
130 }
131 $this->is_dialog=false;
132 unset($this->dialog);
133 $this->dialog=NULL;
134 ksort($this->SubObjects);
135 }
136 }
138 /* Cancel Dialog */
139 if(isset($_POST['CancelSubObject'])){
140 $this->is_dialog=false;
141 unset($this->dialog);
142 $this->dialog=NULL;
143 }
145 /* Print dialog if $this->dialog is set */
146 if($this->dialog){
147 $this->dialog->save_object();
148 $display = $this->dialog->execute();
149 return($display);
150 }
152 $smarty->assign("SubObjects",$this->getList());
153 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
155 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
156 * If we post the escaped strings they will be escaped again
157 */
158 foreach($this->attributes as $attrs){
159 if(get_magic_quotes_gpc()){
160 $smarty->assign($attrs,stripslashes($this->$attrs));
161 }else{
162 $smarty->assign($attrs,($this->$attrs));
163 }
164 }
167 $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
168 return($display);
169 }
171 /* Generate listbox friendly SubObject list
172 */
173 function getList(){
174 $a_return=array();
175 foreach($this->SubObjects as $obj){
176 if($obj['status'] != "delete"){
178 if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
179 if (preg_match("/\[\*\]/", $obj['description'])){
180 $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
181 } else {
182 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
183 }
184 }else{
185 if (preg_match("/\[\*\]/", $obj['description'])){
186 $a_return[$obj['cn']]= $obj['cn'];
187 } else {
188 $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
189 }
190 }
191 }
192 }
193 return($a_return);
194 }
196 /* Delete me, and all my subtrees
197 */
198 function remove_from_parent()
199 {
200 $ldap = $this->config->get_ldap_link();
201 $ldap->cd ($this->dn);
202 $ldap->rmdir_recursive($this->dn);
203 $this->handle_post_events("remove");
204 }
207 /* Save data to object
208 */
209 function save_object()
210 {
211 if(isset($_POST['FAIvariable_posted'])){
212 plugin::save_object();
213 foreach($this->attributes as $attrs){
214 if(isset($_POST[$attrs])){
215 $this->$attrs = $_POST[$attrs];
216 }
217 }
218 }
219 }
222 /* Check supplied data */
223 function check()
224 {
225 $message= array();
226 return ($message);
227 }
230 /* Save to LDAP */
231 function save()
232 {
233 plugin::save();
235 $ldap = $this->config->get_ldap_link();
237 $ldap->cat($this->dn);
238 if($ldap->count()!=0){
239 /* Write FAIscript to ldap*/
240 $ldap->cd($this->dn);
241 $ldap->modify($this->attrs);
242 }else{
243 /* Write FAIscript to ldap*/
244 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
245 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
246 $ldap->cd($this->dn);
247 $ldap->add($this->attrs);
248 }
249 show_ldap_error($ldap->get_error());
251 /* Prepare FAIscriptEntry to write it to ldap
252 * First sort array.
253 * Because we must delete old entries first.
254 * After deletion, we perform add and modify
255 */
256 $Objects = array();
257 foreach($this->SubObjects as $name => $obj){
258 if($obj['status'] == "delete"){
259 $Objects[$name] = $obj;
260 }
261 }
262 foreach($this->SubObjects as $name => $obj){
263 if($obj['status'] != "delete"){
264 $Objects[$name] = $obj;
265 }
266 }
268 foreach($Objects as $name => $obj){
270 foreach($this->sub64coded as $codeIt){
271 $obj[$codeIt]=base64_encode($obj[$codeIt]);
272 }
274 $tmp = array();
275 foreach($this->subAttributes as $attrs){
276 if(empty($obj[$attrs])){
277 $obj[$attrs] = array();
278 }
279 $tmp[$attrs] = $obj[$attrs];
280 }
282 $tmp['objectClass'] = $this->subClasses;
284 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
286 if($obj['status']=="new"){
287 $ldap->cat($sub_dn);
288 if($ldap->count()){
289 $obj['status']="modify";
290 }
291 }
293 if($obj['status'] == "delete"){
294 $ldap->cd($sub_dn);
295 $ldap->rmdir_recursive($sub_dn);
296 $this->handle_post_events("remove");
297 }elseif($obj['status'] == "edited"){
298 $ldap->cd($sub_dn);
299 $ldap->modify($tmp);
300 $this->handle_post_events("modify");
301 }elseif($obj['status']=="new"){
303 if($tmp['description'] == array()){
304 unset($tmp['description']);
305 }
307 $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
308 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
309 $ldap->cd($sub_dn);
310 $ldap->add($tmp);
311 $this->handle_post_events("add");
312 }
313 show_ldap_error($ldap->get_error());
314 }
315 }
316 }
318 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
319 ?>