34bfe405a2ceb72a4d335b784a89728a365fa3a4
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 if(isset($_POST['FAIscript_posted'])){
199 plugin::save_object();
200 foreach($this->attributes as $attrs){
201 if(isset($_POST[$attrs])){
202 $this->$attrs = $_POST[$attrs];
203 }
204 }
205 }
206 }
209 /* Check supplied data */
210 function check()
211 {
212 $message= array();
213 return ($message);
214 }
217 /* Save to LDAP */
218 function save()
219 {
220 plugin::save();
222 $ldap = $this->config->get_ldap_link();
224 $ldap->cat($this->dn);
225 if($ldap->count()!=0){
226 /* Write FAIscript to ldap*/
227 $ldap->cd($this->dn);
228 $ldap->modify($this->attrs);
229 }else{
230 /* Write FAIscript to ldap*/
231 $ldap->cd($this->config->current['BASE']);
232 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
233 $ldap->cd($this->dn);
234 $ldap->add($this->attrs);
235 }
236 show_ldap_error($ldap->get_error());
238 /* Prepare FAIscriptEntry to write it to ldap
239 * First sort array.
240 * Because we must delete old entries first.
241 * After deletion, we perform add and modify
242 */
243 $Objects = array();
244 foreach($this->SubObjects as $name => $obj){
245 if($obj['status'] == "delete"){
246 $Objects[$name] = $obj;
247 }
248 }
249 foreach($this->SubObjects as $name => $obj){
250 if($obj['status'] != "delete"){
251 $Objects[$name] = $obj;
252 }
253 }
255 foreach($Objects as $name => $obj){
257 foreach($this->sub64coded as $codeIt){
258 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
259 }
261 $tmp = array();
262 foreach($this->subAttributes as $attrs){
263 if(empty($obj[$attrs])){
264 $obj[$attrs] = array();
265 }
266 if(!is_array($obj[$attrs])){
267 $tmp[$attrs] = stripslashes($obj[$attrs]);
268 }else{
269 $tmp[$attrs] = $obj[$attrs];
270 }
271 }
273 $tmp['objectClass'] = $this->subClasses;
275 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
277 if($obj['status']=="new"){
278 $ldap->cat($sub_dn);
279 if($ldap->count()){
280 $obj['status']="modify";
281 }
282 }
284 if(empty($tmp['FAIpriority'])){
285 $tmp['FAIpriority'] ="0";
286 }
288 if($obj['status'] == "delete"){
289 $ldap->cd($sub_dn);
290 $ldap->rmdir_recursive($sub_dn);
291 $this->handle_post_events("remove");
292 }elseif($obj['status'] == "edited"){
293 $ldap->cd($sub_dn);
294 $ldap->modify($tmp);
295 $this->handle_post_events("modify");
296 }elseif($obj['status']=="new"){
297 if($tmp['description']==array()){
298 unset($tmp['description']);
299 }
300 if($tmp['FAIscript']==array()){
301 $tmp['FAIscript']=" ";
302 }
303 $ldap->cd($this->config->current['BASE']);
304 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
305 $ldap->cd($sub_dn);
306 $ldap->add($tmp);
307 $this->handle_post_events("add");
308 }
309 show_ldap_error($ldap->get_error());
310 }
311 }
312 }
314 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
315 ?>