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("FAItemplatePath");
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 /* Fill templating stuff */
81 $smarty= get_smarty();
82 $display= "";
84 /* Add new sub object */
85 if(isset($_POST['AddSubObject'])){
86 $this->dialog= new $this->subClassName($this->config,"new");
87 $this->is_dialog=true;
88 }
90 /* Edit selected Sub Object */
91 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
92 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
93 $this->is_dialog=true;
94 }
96 /* Remove Sub object */
97 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
98 if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
99 $this->SubObjects[$_POST['SubObject']]['status']= "delete";
100 }else{
101 unset($this->SubObjects[$_POST['SubObject']]);
102 }
103 }
105 /* Save Dialog */
106 if(isset($_POST['SaveSubObject'])){
107 $this->dialog->save_object();
108 $msgs = $this->dialog->check();
109 if(count($msgs)>0){
110 foreach($msgs as $msg){
111 print_red($msg);
112 }
113 }else{
114 $obj = $this->dialog->save();
115 if(isset($obj['remove'])){
116 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
117 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
118 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
119 unset($this->SubObjects[$obj['remove']['from']]);
120 }
121 $obj['status'] = "new";
122 $this->SubObjects[$obj['remove']['to']] = $obj;
123 unset($this->SubObjects[$obj['remove']['to']]['remove']);
124 }else{
125 $this->SubObjects[$obj['cn']]=$obj;
126 }
127 $this->is_dialog=false;
128 unset($this->dialog);
129 $this->dialog=NULL;
130 }
131 }
133 /* Cancel Dialog */
134 if(isset($_POST['CancelSubObject'])){
135 $this->is_dialog=false;
136 unset($this->dialog);
137 $this->dialog=NULL;
138 }
140 /* Print dialog if $this->dialog is set */
141 if($this->dialog){
142 $this->dialog->save_object();
143 $display = $this->dialog->execute();
144 return($display);
145 }
147 $smarty->assign("SubObjects",$this->getList());
148 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
150 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
151 * If we post the escaped strings they will be escaped again
152 */
153 foreach($this->attributes as $attrs){
154 if(get_magic_quotes_gpc()){
155 $smarty->assign($attrs,stripslashes($this->$attrs));
156 }else{
157 $smarty->assign($attrs,($this->$attrs));
158 }
159 }
162 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
163 return($display);
164 }
166 /* Generate listbox friendly SubObject list
167 */
168 function getList(){
169 $a_return=array();
170 foreach($this->SubObjects as $obj){
171 if($obj['status'] != "delete"){
172 if((isset($obj['description']))&&(!empty($obj['description']))){
173 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
174 }else{
175 $a_return[$obj['cn']]= $obj['cn'];
176 }
177 }
178 }
179 return($a_return);
180 }
182 /* Delete me, and all my subtrees
183 */
184 function remove_from_parent()
185 {
186 $ldap = $this->config->get_ldap_link();
187 $ldap->cd ($this->dn);
188 $ldap->rmdir_recursive($this->dn);
189 $this->handle_post_events("remove");
190 }
193 /* Save data to object
194 */
195 function save_object()
196 {
197 plugin::save_object();
198 foreach($this->attributes as $attrs){
199 if(isset($_POST[$attrs])){
200 $this->$attrs = $_POST[$attrs];
201 }
202 }
203 }
206 /* Check supplied data */
207 function check()
208 {
209 $message= array();
210 return ($message);
211 }
214 /* Save to LDAP */
215 function save()
216 {
217 plugin::save();
219 $ldap = $this->config->get_ldap_link();
221 $ldap->cat($this->dn);
222 if($ldap->count()!=0){
223 /* Write FAIscript to ldap*/
224 $ldap->cd($this->dn);
225 $ldap->modify($this->attrs);
226 }else{
227 /* Write FAIscript to ldap*/
228 $ldap->cd($this->config->current['BASE']);
229 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
230 $ldap->cd($this->dn);
231 $ldap->add($this->attrs);
232 }
233 show_ldap_error($ldap->get_error());
235 /* Prepare FAIscriptEntry to write it to ldap
236 * First sort array.
237 * Because we must delete old entries first.
238 * After deletion, we perform add and modify
239 */
240 $Objects = array();
241 foreach($this->SubObjects as $name => $obj){
242 if($obj['status'] == "delete"){
243 $Objects[$name] = $obj;
244 }
245 }
246 foreach($this->SubObjects as $name => $obj){
247 if($obj['status'] != "delete"){
248 $Objects[$name] = $obj;
249 }
250 }
252 foreach($Objects as $name => $obj){
254 foreach($this->sub64coded as $codeIt){
255 $obj[$codeIt]=base64_encode($obj[$codeIt]);
256 }
257 $tmp = array();
258 foreach($this->subAttributes as $attrs){
259 if(empty($obj[$attrs])){
260 $obj[$attrs] = array();
261 }
262 $tmp[$attrs] = $obj[$attrs];
263 }
265 $tmp['objectClass'] = $this->subClasses;
267 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
269 if($obj['status']=="new"){
270 $ldap->cat($sub_dn);
271 if($ldap->count()){
272 $obj['status']="modify";
273 }
274 }
276 if($obj['status'] == "delete"){
277 $ldap->cd($sub_dn);
278 $ldap->rmdir_recursive($sub_dn);
279 $this->handle_post_events("remove");
280 }elseif($obj['status'] == "edited"){
281 $ldap->cd($sub_dn);
282 $ldap->modify($tmp);
283 $this->handle_post_events("modify");
284 }elseif($obj['status']=="new"){
286 if($tmp['description']==array()){
287 unset($tmp['description']);
288 }
289 $ldap->cd($this->config->current['BASE']);
290 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
291 $ldap->cd($sub_dn);
292 $ldap->add($tmp);
293 $this->handle_post_events("add");
294 }
295 show_ldap_error($ldap->get_error());
296 }
297 }
299 function readBinary($attr,$dn){
300 $Data ="";
301 $ds= ldap_connect($this->config->current['SERVER']);
302 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
303 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
304 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
305 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
306 }
308 if(isset($this->config->current['TLS']) && $this->config->current['TLS'] == "true"){
309 ldap_start_tls($ds);
310 }
312 $r = ldap_bind($ds);
313 $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
315 if ($sr) {
316 $ei=ldap_first_entry($ds, $sr);
317 if ($ei) {
318 if ($info = ldap_get_values_len($ds, $ei, $attr)){
319 $Data= $info[0];
320 }
321 }
322 }
324 /* close conncetion */
325 ldap_unbind($ds);
326 return($Data);
327 }
328 }
330 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
331 ?>