1 <?php
3 class faiScript 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","FAIscript");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIscriptEntry";
21 var $subClasses = array("top","FAIclass","FAIscriptEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiScriptEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAIpriority","FAIscript");
28 var $sub64coded = array("FAIscript");
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 faiScript ($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 foreach($this->subAttributes as $attrs){
71 $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
72 }
74 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
75 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
76 }
77 }
78 }
80 function execute()
81 {
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 }
135 }
137 /* Cancel Dialog */
138 if(isset($_POST['CancelSubObject'])){
139 $this->is_dialog=false;
140 unset($this->dialog);
141 $this->dialog=NULL;
142 }
144 /* Print dialog if $this->dialog is set */
145 if($this->dialog){
146 $this->dialog->save_object();
147 $display = $this->dialog->execute();
148 return($display);
149 }
151 $smarty->assign("SubObjects",$this->getList());
152 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
154 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
155 * If we post the escaped strings they will be escaped again
156 */
157 foreach($this->attributes as $attrs){
158 if(get_magic_quotes_gpc()){
159 $smarty->assign($attrs,stripslashes($this->$attrs));
160 }else{
161 $smarty->assign($attrs,($this->$attrs));
162 }
163 }
165 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
166 return($display);
167 }
169 /* Generate listbox friendly SubObject list
170 */
171 function getList(){
172 $a_return=array();
173 foreach($this->SubObjects as $obj){
174 if($obj['status'] != "delete"){
175 if((isset($obj['description']))&&(!empty($obj['description']))){
176 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
177 }else{
178 $a_return[$obj['cn']]= $obj['cn'];
179 }
180 }
181 }
182 return($a_return);
183 }
185 /* Delete me, and all my subtrees
186 */
187 function remove_from_parent()
188 {
189 $ldap = $this->config->get_ldap_link();
190 $ldap->cd ($this->dn);
191 $ldap->rmdir_recursive($this->dn);
192 $this->handle_post_events("remove");
193 }
196 /* Save data to object
197 */
198 function save_object()
199 {
200 if(isset($_POST['FAIscript_posted'])){
201 plugin::save_object();
202 foreach($this->attributes as $attrs){
203 if(isset($_POST[$attrs])){
204 $this->$attrs = $_POST[$attrs];
205 }
206 }
207 }
208 }
211 /* Check supplied data */
212 function check()
213 {
214 $message= array();
215 return ($message);
216 }
219 /* Save to LDAP */
220 function save()
221 {
222 plugin::save();
224 $ldap = $this->config->get_ldap_link();
226 $ldap->cat($this->dn);
227 if($ldap->count()!=0){
228 /* Write FAIscript to ldap*/
229 $ldap->cd($this->dn);
230 $ldap->modify($this->attrs);
231 }else{
232 /* Write FAIscript to ldap*/
233 $ldap->cd($this->config->current['BASE']);
234 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
235 $ldap->cd($this->dn);
236 $ldap->add($this->attrs);
237 }
238 show_ldap_error($ldap->get_error());
240 /* Prepare FAIscriptEntry to write it to ldap
241 * First sort array.
242 * Because we must delete old entries first.
243 * After deletion, we perform add and modify
244 */
245 $Objects = array();
246 foreach($this->SubObjects as $name => $obj){
247 if($obj['status'] == "delete"){
248 $Objects[$name] = $obj;
249 }
250 }
251 foreach($this->SubObjects as $name => $obj){
252 if($obj['status'] != "delete"){
253 $Objects[$name] = $obj;
254 }
255 }
257 foreach($Objects as $name => $obj){
259 foreach($this->sub64coded as $codeIt){
260 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
261 }
263 $tmp = array();
264 foreach($this->subAttributes as $attrs){
265 if(empty($obj[$attrs])){
266 $obj[$attrs] = array();
267 }
268 if(!is_array($obj[$attrs])){
269 $tmp[$attrs] = stripslashes($obj[$attrs]);
270 }else{
271 $tmp[$attrs] = $obj[$attrs];
272 }
273 }
275 $tmp['objectClass'] = $this->subClasses;
277 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
279 if($obj['status']=="new"){
280 $ldap->cat($sub_dn);
281 if($ldap->count()){
282 $obj['status']="modify";
283 }
284 }
286 if(empty($tmp['FAIpriority'])){
287 $tmp['FAIpriority'] ="0";
288 }
290 if($obj['status'] == "delete"){
291 $ldap->cd($sub_dn);
292 $ldap->rmdir_recursive($sub_dn);
293 $this->handle_post_events("remove");
294 }elseif($obj['status'] == "edited"){
295 $ldap->cd($sub_dn);
296 $ldap->modify($tmp);
297 $this->handle_post_events("modify");
298 }elseif($obj['status']=="new"){
299 if($tmp['description']==array()){
300 unset($tmp['description']);
301 }
302 if($tmp['FAIscript']==array()){
303 $tmp['FAIscript']=" ";
304 }
305 $ldap->cd($this->config->current['BASE']);
306 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
307 $ldap->cd($sub_dn);
308 $ldap->add($tmp);
309 $this->handle_post_events("add");
310 }
311 show_ldap_error($ldap->get_error());
312 }
313 }
314 }
316 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
317 ?>