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