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 }
76 }
77 }
79 function execute()
80 {
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 $_SESSION['objectinfo']= $this->dn;
92 /* Edit selected Sub Object */
93 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
94 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
95 $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
96 $this->is_dialog=true;
97 }
99 /* Remove Sub object */
100 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
101 if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
102 $this->SubObjects[$_POST['SubObject']]['status']= "delete";
103 }else{
104 unset($this->SubObjects[$_POST['SubObject']]);
105 }
106 }
108 /* Save Dialog */
109 if(isset($_POST['SaveSubObject'])){
111 /* Perform post check*/
112 $this->dialog->save_object();
114 /* Get messages */
115 $msgs = $this->dialog->check();
117 /* print errors */
118 if(count($msgs)>0){
119 foreach($msgs as $msg){
120 print_red($msg);
121 }
122 }else{
124 /* Get return object */
125 $obj = $this->dialog->save();
126 if(isset($obj['remove'])){
128 /* Depending on status, set new status */
129 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
130 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
131 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
132 unset($this->SubObjects[$obj['remove']['from']]);
133 }
134 $obj['status'] = "new";
135 $this->SubObjects[$obj['remove']['to']] = $obj;
136 unset($this->SubObjects[$obj['remove']['to']]['remove']);
137 }else{
138 $this->SubObjects[$obj['cn']]=$obj;
139 }
140 $this->is_dialog=false;
141 unset($this->dialog);
142 $this->dialog=NULL;
143 }
144 }
146 /* Cancel Dialog */
147 if(isset($_POST['CancelSubObject'])){
148 $this->is_dialog=false;
149 unset($this->dialog);
150 $this->dialog=NULL;
151 }
153 /* Print dialog if $this->dialog is set */
154 if($this->dialog){
155 $this->dialog->save_object();
156 $display = $this->dialog->execute();
157 return($display);
158 }
160 $smarty->assign("SubObjects",$this->getList());
161 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
163 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
164 * If we post the escaped strings they will be escaped again
165 */
166 foreach($this->attributes as $attrs){
167 if(get_magic_quotes_gpc()){
168 $smarty->assign($attrs,stripslashes($this->$attrs));
169 }else{
170 $smarty->assign($attrs,($this->$attrs));
171 }
172 }
175 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
176 return($display);
177 }
179 /* Generate listbox friendly SubObject list
180 */
181 function getList(){
182 $a_return=array();
183 foreach($this->SubObjects as $obj){
184 if($obj['status'] != "delete"){
185 if((isset($obj['description']))&&(!empty($obj['description']))){
186 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
187 }else{
188 $a_return[$obj['cn']]= $obj['cn'];
189 }
190 }
191 }
192 return($a_return);
193 }
195 /* Delete me, and all my subtrees
196 */
197 function remove_from_parent()
198 {
199 $ldap = $this->config->get_ldap_link();
200 $ldap->cd ($this->dn);
201 $ldap->rmdir_recursive($this->dn);
202 $this->handle_post_events("remove");
203 }
206 /* Save data to object
207 */
208 function save_object()
209 {
210 if(isset($_POST['FAIhook_posted'])){
211 plugin::save_object();
212 foreach($this->attributes as $attrs){
213 if(isset($_POST[$attrs])){
214 $this->$attrs = $_POST[$attrs];
215 }
216 }
217 }
218 }
221 /* Check supplied data */
222 function check()
223 {
224 $message= array();
225 return ($message);
226 }
229 /* Save to LDAP */
230 function save()
231 {
232 plugin::save();
234 $ldap = $this->config->get_ldap_link();
236 $ldap->cat($this->dn);
237 if($ldap->count()!=0){
238 /* Write FAIscript to ldap*/
239 $ldap->cd($this->dn);
240 $ldap->modify($this->attrs);
241 }else{
242 /* Write FAIscript to ldap*/
243 $ldap->cd($this->config->current['BASE']);
244 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
245 $ldap->cd($this->dn);
246 $ldap->add($this->attrs);
247 }
248 show_ldap_error($ldap->get_error());
250 /* Prepare FAIscriptEntry to write it to ldap
251 * First sort array.
252 * Because we must delete old entries first.
253 * After deletion, we perform add and modify
254 */
255 $Objects = array();
256 foreach($this->SubObjects as $name => $obj){
257 if($obj['status'] == "delete"){
258 $Objects[$name] = $obj;
259 }
260 }
261 foreach($this->SubObjects as $name => $obj){
262 if($obj['status'] != "delete"){
263 $Objects[$name] = $obj;
264 }
265 }
267 foreach($Objects as $name => $obj){
269 foreach($this->sub64coded as $codeIt){
270 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
271 }
273 $tmp = array();
274 foreach($this->subAttributes as $attrs){
275 if(empty($obj[$attrs])){
276 $obj[$attrs] = array();
277 }
278 if(!is_array($obj[$attrs])){
279 $tmp[$attrs] = stripslashes($obj[$attrs]);
280 }else{
281 $tmp[$attrs] = $obj[$attrs];
282 }
283 }
285 $tmp['objectClass'] = $this->subClasses;
287 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
289 if($obj['status']=="new"){
290 $ldap->cat($sub_dn);
291 if($ldap->count()){
292 $obj['status']="modify";
293 }
294 }
296 if($obj['status'] == "delete"){
297 $ldap->cd($sub_dn);
298 $ldap->rmdir_recursive($sub_dn);
299 $this->handle_post_events("remove");
300 }elseif($obj['status'] == "edited"){
301 $ldap->cd($sub_dn);
302 $ldap->modify($tmp);
303 $this->handle_post_events("modify");
304 }elseif($obj['status']=="new"){
305 if($tmp['description']==array()){
306 unset($tmp['description']);
307 }
308 $ldap->cd($this->config->current['BASE']);
309 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
310 $ldap->cd($sub_dn);
311 $ldap->add($tmp);
312 $this->handle_post_events("add");
313 }
314 show_ldap_error($ldap->get_error());
315 }
316 }
317 }
319 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
320 ?>