cf8623305d9e06c91cf0bbb3bcc274c27b86bceb
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 /* 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 $_SESSION['objectinfo'] = $this->dn;
91 /* Edit selected Sub Object */
92 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
93 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
94 $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
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 }
164 $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
165 return($display);
166 }
168 /* Generate listbox friendly SubObject list
169 */
170 function getList(){
171 $a_return=array();
172 foreach($this->SubObjects as $obj){
173 if($obj['status'] != "delete"){
174 if((isset($obj['description']))&&(!empty($obj['description']))){
175 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
176 }else{
177 $a_return[$obj['cn']]= $obj['cn'];
178 }
179 }
180 }
181 return($a_return);
182 }
184 /* Delete me, and all my subtrees
185 */
186 function remove_from_parent()
187 {
188 $ldap = $this->config->get_ldap_link();
189 $ldap->cd ($this->dn);
190 $ldap->rmdir_recursive($this->dn);
191 $this->handle_post_events("remove");
192 }
195 /* Save data to object
196 */
197 function save_object()
198 {
199 if(isset($_POST['FAItemplate_posted'])){
200 plugin::save_object();
201 foreach($this->attributes as $attrs){
202 if(isset($_POST[$attrs])){
203 $this->$attrs = $_POST[$attrs];
204 }
205 }
206 }
207 }
210 /* Check supplied data */
211 function check()
212 {
213 $message= array();
214 return ($message);
215 }
218 /* Save to LDAP */
219 function save()
220 {
221 plugin::save();
223 $ldap = $this->config->get_ldap_link();
225 $ldap->cat($this->dn);
226 if($ldap->count()!=0){
227 /* Write FAIscript to ldap*/
228 $ldap->cd($this->dn);
229 $ldap->modify($this->attrs);
230 }else{
231 /* Write FAIscript to ldap*/
232 $ldap->cd($this->config->current['BASE']);
233 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
234 $ldap->cd($this->dn);
235 $ldap->add($this->attrs);
236 }
237 show_ldap_error($ldap->get_error());
239 /* Prepare FAIscriptEntry to write it to ldap
240 * First sort array.
241 * Because we must delete old entries first.
242 * After deletion, we perform add and modify
243 */
244 $Objects = array();
245 foreach($this->SubObjects as $name => $obj){
246 if($obj['status'] == "delete"){
247 $Objects[$name] = $obj;
248 }
249 }
250 foreach($this->SubObjects as $name => $obj){
251 if($obj['status'] != "delete"){
252 $Objects[$name] = $obj;
253 }
254 }
256 foreach($Objects as $name => $obj){
258 foreach($this->sub64coded as $codeIt){
259 $obj[$codeIt]=base64_encode($obj[$codeIt]);
260 }
261 $tmp = array();
262 foreach($this->subAttributes as $attrs){
263 if(empty($obj[$attrs])){
264 $obj[$attrs] = array();
265 }
266 $tmp[$attrs] = $obj[$attrs];
267 }
269 $tmp['objectClass'] = $this->subClasses;
271 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
273 if($obj['status']=="new"){
274 $ldap->cat($sub_dn);
275 if($ldap->count()){
276 $obj['status']="modify";
277 }
278 }
280 if($obj['status'] == "delete"){
281 $ldap->cd($sub_dn);
282 $ldap->rmdir_recursive($sub_dn);
283 $this->handle_post_events("remove");
284 }elseif($obj['status'] == "edited"){
285 $ldap->cd($sub_dn);
286 $ldap->modify($tmp);
287 $this->handle_post_events("modify");
288 }elseif($obj['status']=="new"){
290 if($tmp['description']==array()){
291 unset($tmp['description']);
292 }
293 $ldap->cd($this->config->current['BASE']);
294 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
295 $ldap->cd($sub_dn);
296 $ldap->add($tmp);
297 $this->handle_post_events("add");
298 }
299 show_ldap_error($ldap->get_error());
300 }
301 }
303 function readBinary($attr,$dn){
304 $Data ="";
305 $ds= ldap_connect($this->config->current['SERVER']);
306 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
307 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
308 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
309 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
310 }
312 if(isset($this->config->current['TLS']) && $this->config->current['TLS'] == "true"){
313 ldap_start_tls($ds);
314 }
316 $r = ldap_bind($ds);
317 $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
319 if ($sr) {
320 $ei=ldap_first_entry($ds, $sr);
321 if ($ei) {
322 if ($info = ldap_get_values_len($ds, $ei, $attr)){
323 $Data= $info[0];
324 }
325 }
326 }
328 /* close conncetion */
329 ldap_unbind($ds);
330 return($Data);
331 }
332 }
334 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
335 ?>