1 <?php
3 class faiHook 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","FAIhook");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIhookEntry";
21 var $subClasses = array("top","FAIclass","FAIhookEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiHookEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAItask","FAIscript");
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 faiHook ($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 */
45 if($dn != "new"){
46 $this->dn =$dn;
48 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
49 */
50 $ldap = $this->config->get_ldap_link();
51 $ldap->cd ($this->dn);
52 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
54 while($object = $ldap->fetch()){
55 /* Set status for save management */
57 foreach($this->subAttributes as $attrs){
58 if(!isset($object[$attrs][0])){
59 $this->SubObjects[$object['cn'][0]][$attrs]="";
60 }else{
61 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
62 }
63 }
65 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
66 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
68 foreach($this->sub64coded as $codeIt){
69 $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
70 }
72 foreach($this->subAttributes as $attrs){
73 $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
74 }
75 $this->SubObjects[$object['cn'][0]]['FAIscript'] = addslashes($this->readBinary("FAIscript",$object['dn']));
76 }
77 ksort($this->SubObjects);
78 }
79 }
81 function execute()
82 {
83 /* Fill templating stuff */
84 $smarty= get_smarty();
85 $display= "";
87 /* Add new sub object */
88 if(isset($_POST['AddSubObject'])){
89 $this->dialog= new $this->subClassName($this->config,"new");
90 $this->is_dialog=true;
91 }
93 $_SESSION['objectinfo']= $this->dn;
94 /* Edit selected Sub Object */
95 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
96 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
97 $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
98 $this->is_dialog=true;
99 }
101 /* Remove Sub object */
102 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
103 if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
104 $this->SubObjects[$_POST['SubObject']]['status']= "delete";
105 }else{
106 unset($this->SubObjects[$_POST['SubObject']]);
107 }
108 }
110 /* Save Dialog */
111 if(isset($_POST['SaveSubObject'])){
113 /* Perform post check*/
114 $this->dialog->save_object();
116 /* Get messages */
117 $msgs = $this->dialog->check();
119 /* print errors */
120 if(count($msgs)>0){
121 foreach($msgs as $msg){
122 print_red($msg);
123 }
124 }else{
126 /* Get return object */
127 $obj = $this->dialog->save();
128 if(isset($obj['remove'])){
130 /* Depending on status, set new status */
131 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
132 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
133 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
134 unset($this->SubObjects[$obj['remove']['from']]);
135 }
136 $obj['status'] = "new";
137 $this->SubObjects[$obj['remove']['to']] = $obj;
138 unset($this->SubObjects[$obj['remove']['to']]['remove']);
139 }else{
140 $this->SubObjects[$obj['cn']]=$obj;
141 }
142 $this->is_dialog=false;
143 unset($this->dialog);
144 $this->dialog=NULL;
145 ksort($this->SubObjects);
146 }
147 }
149 /* Cancel Dialog */
150 if(isset($_POST['CancelSubObject'])){
151 $this->is_dialog=false;
152 unset($this->dialog);
153 $this->dialog=NULL;
154 }
156 /* Print dialog if $this->dialog is set */
157 if($this->dialog){
158 $this->dialog->save_object();
159 $display = $this->dialog->execute();
160 return($display);
161 }
163 $smarty->assign("SubObjects",$this->getList());
164 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
166 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
167 * If we post the escaped strings they will be escaped again
168 */
169 foreach($this->attributes as $attrs){
170 if(get_magic_quotes_gpc()){
171 $smarty->assign($attrs,stripslashes($this->$attrs));
172 }else{
173 $smarty->assign($attrs,($this->$attrs));
174 }
175 }
178 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
179 return($display);
180 }
182 /* Generate listbox friendly SubObject list
183 */
184 function getList(){
185 $a_return=array();
186 foreach($this->SubObjects as $obj){
187 if($obj['status'] != "delete"){
188 if((isset($obj['description']))&&(!empty($obj['description']))){
189 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
190 }else{
191 $a_return[$obj['cn']]= $obj['cn'];
192 }
193 }
194 }
195 return($a_return);
196 }
198 /* Delete me, and all my subtrees
199 */
200 function remove_from_parent()
201 {
202 $ldap = $this->config->get_ldap_link();
203 $ldap->cd ($this->dn);
204 $ldap->rmdir_recursive($this->dn);
205 $this->handle_post_events("remove");
206 }
209 /* Save data to object
210 */
211 function save_object()
212 {
213 if(isset($_POST['FAIhook_posted'])){
214 plugin::save_object();
215 foreach($this->attributes as $attrs){
216 if(isset($_POST[$attrs])){
217 $this->$attrs = $_POST[$attrs];
218 }
219 }
220 }
221 }
224 /* Check supplied data */
225 function check()
226 {
227 $message= array();
228 return ($message);
229 }
232 /* Save to LDAP */
233 function save()
234 {
235 plugin::save();
237 $ldap = $this->config->get_ldap_link();
239 $ldap->cat($this->dn);
240 if($ldap->count()!=0){
241 /* Write FAIscript to ldap*/
242 $ldap->cd($this->dn);
243 $ldap->modify($this->attrs);
244 }else{
245 /* Write FAIscript to ldap*/
246 $ldap->cd($this->config->current['BASE']);
247 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
248 $ldap->cd($this->dn);
249 $ldap->add($this->attrs);
250 }
251 show_ldap_error($ldap->get_error());
253 /* Prepare FAIscriptEntry to write it to ldap
254 * First sort array.
255 * Because we must delete old entries first.
256 * After deletion, we perform add and modify
257 */
258 $Objects = array();
259 foreach($this->SubObjects as $name => $obj){
260 if($obj['status'] == "delete"){
261 $Objects[$name] = $obj;
262 }
263 }
264 foreach($this->SubObjects as $name => $obj){
265 if($obj['status'] != "delete"){
266 $Objects[$name] = $obj;
267 }
268 }
270 foreach($Objects as $name => $obj){
272 foreach($this->sub64coded as $codeIt){
273 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
274 }
276 $tmp = array();
277 foreach($this->subAttributes as $attrs){
278 if(empty($obj[$attrs])){
279 $obj[$attrs] = array();
280 }
281 if(!is_array($obj[$attrs])){
282 $tmp[$attrs] = stripslashes($obj[$attrs]);
283 }else{
284 $tmp[$attrs] = $obj[$attrs];
285 }
286 }
288 $tmp['objectClass'] = $this->subClasses;
290 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
292 if($obj['status']=="new"){
293 $ldap->cat($sub_dn);
294 if($ldap->count()){
295 $obj['status']="modify";
296 }
297 }
299 if($obj['status'] == "delete"){
300 $ldap->cd($sub_dn);
301 $ldap->rmdir_recursive($sub_dn);
302 $this->handle_post_events("remove");
303 }elseif($obj['status'] == "edited"){
304 $ldap->cd($sub_dn);
305 $ldap->modify($tmp);
306 $this->handle_post_events("modify");
307 }elseif($obj['status']=="new"){
308 if($tmp['description']==array()){
309 unset($tmp['description']);
310 }
311 $ldap->cd($this->config->current['BASE']);
312 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
313 $ldap->cd($sub_dn);
314 $ldap->add($tmp);
315 $this->handle_post_events("add");
316 }
317 show_ldap_error($ldap->get_error());
318 }
319 }
321 function readBinary($attr,$dn){
322 $Data ="";
323 $ds= ldap_connect($this->config->current['SERVER']);
324 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
325 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
326 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
327 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
328 }
330 if(isset($this->config->current['TLS']) && $this->config->current['TLS'] == "true"){
331 ldap_start_tls($ds);
332 }
334 $r = ldap_bind($ds);
335 $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
337 if ($sr) {
338 $ei=ldap_first_entry($ds, $sr);
339 if ($ei) {
340 if ($info = ldap_get_values_len($ds, $ei, $attr)){
341 $Data= $info[0];
342 }
343 }
344 }
346 /* close conncetion */
347 ldap_unbind($ds);
348 return($Data);
349 }
352 }
354 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
355 ?>