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'] =addslashes( $this->readBinary("FAItemplateFile",$object['dn']));
72 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
73 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
74 }
75 ksort($this->SubObjects);
76 }
77 }
79 function execute()
80 {
81 /* Fill templating stuff */
82 $smarty= get_smarty();
83 $display= "";
85 /* Add new sub object */
86 if(isset($_POST['AddSubObject'])){
87 $this->dialog= new $this->subClassName($this->config,"new");
88 $this->is_dialog=true;
89 }
91 $_SESSION['objectinfo'] = $this->dn;
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 $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
96 $this->is_dialog=true;
97 }
99 /* Remove Sub object */
100 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
101 if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
102 $this->SubObjects[$_POST['SubObject']]['status']= "delete";
103 }else{
104 unset($this->SubObjects[$_POST['SubObject']]);
105 }
106 }
108 /* Save Dialog */
109 if(isset($_POST['SaveSubObject'])){
110 $this->dialog->save_object();
111 $msgs = $this->dialog->check();
112 if(count($msgs)>0){
113 foreach($msgs as $msg){
114 print_red($msg);
115 }
116 }else{
117 $obj = $this->dialog->save();
118 if(isset($obj['remove'])){
119 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
120 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
121 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
122 unset($this->SubObjects[$obj['remove']['from']]);
123 }
124 $obj['status'] = "new";
125 $this->SubObjects[$obj['remove']['to']] = $obj;
126 unset($this->SubObjects[$obj['remove']['to']]['remove']);
127 }else{
128 $this->SubObjects[$obj['cn']]=$obj;
129 }
130 $this->is_dialog=false;
131 unset($this->dialog);
132 $this->dialog=NULL;
133 ksort($this->SubObjects);
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 }
166 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
167 return($display);
168 }
170 /* Generate listbox friendly SubObject list
171 */
172 function getList(){
173 $a_return=array();
174 foreach($this->SubObjects as $obj){
175 if($obj['status'] != "delete"){
176 if((isset($obj['description']))&&(!empty($obj['description']))){
177 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
178 }else{
179 $a_return[$obj['cn']]= $obj['cn'];
180 }
181 }
182 }
183 return($a_return);
184 }
186 /* Delete me, and all my subtrees
187 */
188 function remove_from_parent()
189 {
190 $ldap = $this->config->get_ldap_link();
191 $ldap->cd ($this->dn);
192 $ldap->rmdir_recursive($this->dn);
193 $this->handle_post_events("remove");
194 }
197 /* Save data to object
198 */
199 function save_object()
200 {
201 if(isset($_POST['FAItemplate_posted'])){
202 plugin::save_object();
203 foreach($this->attributes as $attrs){
204 if(isset($_POST[$attrs])){
205 $this->$attrs = $_POST[$attrs];
206 }
207 }
208 }
209 }
212 /* Check supplied data */
213 function check()
214 {
215 $message= array();
216 return ($message);
217 }
220 /* Save to LDAP */
221 function save()
222 {
223 plugin::save();
225 $ldap = $this->config->get_ldap_link();
227 $ldap->cat($this->dn);
228 if($ldap->count()!=0){
229 /* Write FAIscript to ldap*/
230 $ldap->cd($this->dn);
231 $ldap->modify($this->attrs);
232 }else{
233 /* Write FAIscript to ldap*/
234 $ldap->cd($this->config->current['BASE']);
235 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
236 $ldap->cd($this->dn);
237 $ldap->add($this->attrs);
238 }
239 show_ldap_error($ldap->get_error());
241 /* Prepare FAIscriptEntry to write it to ldap
242 * First sort array.
243 * Because we must delete old entries first.
244 * After deletion, we perform add and modify
245 */
246 $Objects = array();
247 foreach($this->SubObjects as $name => $obj){
248 if($obj['status'] == "delete"){
249 $Objects[$name] = $obj;
250 }
251 }
252 foreach($this->SubObjects as $name => $obj){
253 if($obj['status'] != "delete"){
254 $Objects[$name] = $obj;
255 }
256 }
258 foreach($Objects as $name => $obj){
260 foreach($this->sub64coded as $codeIt){
261 $obj[$codeIt]=base64_encode($obj[$codeIt]);
262 }
263 $tmp = array();
264 foreach($this->subAttributes as $attrs){
265 if(empty($obj[$attrs])){
266 $obj[$attrs] = array();
267 }
268 $tmp[$attrs] = $obj[$attrs];
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"){
292 if($tmp['description']==array()){
293 unset($tmp['description']);
294 }
295 $ldap->cd($this->config->current['BASE']);
296 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
297 $ldap->cd($sub_dn);
298 $ldap->add($tmp);
299 $this->handle_post_events("add");
300 }
301 show_ldap_error($ldap->get_error());
302 }
303 }
305 function readBinary($attr,$dn){
306 $Data ="";
307 $ds= ldap_connect($this->config->current['SERVER']);
308 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
309 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
310 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
311 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
312 }
314 if(isset($this->config->current['TLS']) && $this->config->current['TLS'] == "true"){
315 ldap_start_tls($ds);
316 }
318 $r = ldap_bind($ds);
319 $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
321 if ($sr) {
322 $ei=ldap_first_entry($ds, $sr);
323 if ($ei) {
324 if ($info = ldap_get_values_len($ds, $ei, $attr)){
325 $Data= $info[0];
326 }
327 }
328 }
330 /* close conncetion */
331 ldap_unbind($ds);
332 return($Data);
333 }
334 }
336 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
337 ?>