10f81c719542047b246f5f68c7dd0987689b1538
1 <?php
3 class faiTemplate 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","FAItemplate");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAItemplateEntry";
21 var $subClasses = array("top","FAIclass","FAItemplateEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiTemplateEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAItemplateFile","FAItemplatePath","FAIowner","FAImode");
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 faiTemplate ($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 $this->SubObjects[$object['cn'][0]]['FAItemplateFile']= $this->readBinary("FAItemplateFile",$object['dn']);
72 $this->SubObjects[$object['cn'][0]]['status']= "edited";
73 $this->SubObjects[$object['cn'][0]]['dn']= $object['dn'];
74 }
75 }
76 }
78 function execute()
79 {
80 /* Call parent execute */
81 plugin::execute();
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 if($this->dn != "new"){
94 $_SESSION['objectinfo']= $this->dn;
95 }
98 /* Edit selected Sub Object */
99 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
101 $temp = $_POST['SubObject'][0];
103 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$temp]);
104 $_SESSION['objectinfo'] = $this->SubObjects[$temp]['dn'];
105 $this->is_dialog=true;
106 }
108 /* Remove Sub object */
109 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
110 foreach($_POST['SubObject'] as $temp){
111 if($this->SubObjects[$temp]['status'] == "edited"){
112 $this->SubObjects[$temp]['status']= "delete";
113 }else{
114 unset($this->SubObjects[$temp]);
115 }
116 }
117 }
119 /* Save Dialog */
120 if(isset($_POST['SaveSubObject'])){
121 $this->dialog->save_object();
122 $msgs = $this->dialog->check();
123 if(count($msgs)>0){
124 foreach($msgs as $msg){
125 print_red($msg);
126 }
127 }else{
128 $obj = $this->dialog->save();
129 if(isset($obj['remove'])){
130 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
131 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
132 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
133 unset($this->SubObjects[$obj['remove']['from']]);
134 }
135 $obj['status'] = "new";
136 $this->SubObjects[$obj['remove']['to']] = $obj;
137 unset($this->SubObjects[$obj['remove']['to']]['remove']);
138 }else{
139 $this->SubObjects[$obj['cn']]=$obj;
140 }
141 $this->is_dialog=false;
142 unset($this->dialog);
143 $this->dialog=NULL;
144 }
145 }
147 /* Sort entries */
148 $tmp = $keys = array();
149 foreach($this->SubObjects as $key => $entry){
150 $keys[$key]=$key;
151 }
152 natcasesort($keys);
153 foreach($keys as $key){
154 $tmp[$key]=$this->SubObjects[$key];
155 }
156 $this->SubObjects = $tmp;
158 /* Cancel Dialog */
159 if(isset($_POST['CancelSubObject'])){
160 $this->is_dialog=false;
161 unset($this->dialog);
162 $this->dialog=NULL;
163 }
165 /* Print dialog if $this->dialog is set */
166 if($this->dialog){
167 $this->dialog->save_object();
168 $display = $this->dialog->execute();
169 return($display);
170 }
172 $smarty->assign("SubObjects",$this->getList());
173 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
175 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
176 * If we post the escaped strings they will be escaped again
177 */
178 foreach($this->attributes as $attrs){
179 if(get_magic_quotes_gpc()){
180 $smarty->assign($attrs,stripslashes($this->$attrs));
181 }else{
182 $smarty->assign($attrs,($this->$attrs));
183 }
184 }
187 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
188 return($display);
189 }
191 /* Generate listbox friendly SubObject list
192 */
193 function getList(){
194 $a_return=array();
195 foreach($this->SubObjects as $obj){
196 if($obj['status'] != "delete"){
198 if((isset($obj['description']))&&(!empty($obj['description']))){
199 if(strlen($obj['description']) > 40){
200 $obj['description'] = substr($obj['description'],0,40)."...";
201 }
202 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
203 }else{
204 $a_return[$obj['cn']]= $obj['cn'];
205 }
206 }
207 }
208 return($a_return);
209 }
211 /* Delete me, and all my subtrees
212 */
213 function remove_from_parent()
214 {
215 $ldap = $this->config->get_ldap_link();
216 $ldap->cd ($this->dn);
217 $ldap->rmdir_recursive($this->dn);
218 $this->handle_post_events("remove");
219 }
222 /* Save data to object
223 */
224 function save_object()
225 {
226 if(isset($_POST['FAItemplate_posted'])){
227 plugin::save_object();
228 foreach($this->attributes as $attrs){
229 if(isset($_POST[$attrs])){
230 $this->$attrs = $_POST[$attrs];
231 }
232 }
233 }
234 }
237 /* Check supplied data */
238 function check()
239 {
240 $message= array();
241 return ($message);
242 }
245 /* Save to LDAP */
246 function save()
247 {
248 plugin::save();
250 $ldap = $this->config->get_ldap_link();
252 $ldap->cat($this->dn);
253 if($ldap->count()!=0){
254 /* Write FAIscript to ldap*/
255 $ldap->cd($this->dn);
256 $this->cleanup();
257 $ldap->modify ($this->attrs);
259 }else{
260 /* Write FAIscript to ldap*/
261 $ldap->cd($this->config->current['BASE']);
262 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
263 $ldap->cd($this->dn);
264 $ldap->add($this->attrs);
265 }
266 show_ldap_error($ldap->get_error());
268 /* Prepare FAIscriptEntry to write it to ldap
269 * First sort array.
270 * Because we must delete old entries first.
271 * After deletion, we perform add and modify
272 */
273 $Objects = array();
274 foreach($this->SubObjects as $name => $obj){
275 if($obj['status'] == "delete"){
276 $Objects[$name] = $obj;
277 }
278 }
279 foreach($this->SubObjects as $name => $obj){
280 if($obj['status'] != "delete"){
281 $Objects[$name] = $obj;
282 }
283 }
285 foreach($Objects as $name => $obj){
287 foreach($this->sub64coded as $codeIt){
288 $obj[$codeIt]=base64_encode($obj[$codeIt]);
289 }
290 $tmp = array();
291 foreach($this->subAttributes as $attrs){
292 if(empty($obj[$attrs])){
293 $obj[$attrs] = array();
294 }
295 $tmp[$attrs] = $obj[$attrs];
296 }
298 $tmp['objectClass'] = $this->subClasses;
300 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
302 if($obj['status']=="new"){
303 $ldap->cat($sub_dn);
304 if($ldap->count()){
305 $obj['status']="modify";
306 }
307 }
309 if($obj['status'] == "delete"){
310 $ldap->cd($sub_dn);
311 $ldap->rmdir_recursive($sub_dn);
312 $this->handle_post_events("remove");
313 }elseif($obj['status'] == "edited"){
314 $ldap->cd($sub_dn);
315 $this->cleanup();
316 $ldap->modify ($tmp);
318 $this->handle_post_events("modify");
319 }elseif($obj['status']=="new"){
321 if($tmp['description']==array()){
322 unset($tmp['description']);
323 }
324 $ldap->cd($this->config->current['BASE']);
325 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
326 $ldap->cd($sub_dn);
327 $ldap->add($tmp);
328 $this->handle_post_events("add");
329 }
330 show_ldap_error($ldap->get_error());
331 }
332 }
334 function readBinary($attr,$dn){
335 $Data ="";
336 $ds= ldap_connect($this->config->current['SERVER']);
337 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
338 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
339 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
340 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
341 }
343 if(isset($this->config->current['TLS']) && $this->config->current['TLS'] == "true"){
344 ldap_start_tls($ds);
345 }
347 $r = ldap_bind($ds);
348 $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
350 if ($sr) {
351 $ei=ldap_first_entry($ds, $sr);
352 if ($ei) {
353 if ($info = ldap_get_values_len($ds, $ei, $attr)){
354 $Data= $info[0];
355 }
356 }
357 }
359 /* close conncetion */
360 ldap_unbind($ds);
361 return($Data);
362 }
363 }
365 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
366 ?>