1 <?php
2 //! This class handles the goFonMacroParameter
3 /*! In this class all parameters from goFonMacroContent \n
4 and all (if given) already defined parameters are managed \n
5 \n
6 Parameters will be saved to openldap like this :\n
7 goFonMacroParameter: ID!NAME:!TYPE(string:combo:bool)!DEFAULT_VALUE\n
8 In Case of type=combo the DEFAULT_VALUE specifies the entries in the listbox, like this\n
9 "first:second:third:last" \n
10 */
11 class macroParameter extends plugin
12 {
13 /*! CLI vars */
14 var $cli_summary= "Handling of GOsa's application object";
15 /*! CLI vars */
16 var $cli_description= "Some longer text\nfor help";
17 /*! CLI vars */
18 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
20 /* Parameter Count*/
21 var $para_count = 0 ;
22 /*! macro base */
23 var $base= "";
25 /*! This array contains all Parameter defined for the macro*/
26 var $goFonMacroParameter =array();
28 /*! This are the available types for a macro */
29 var $type_shortcut= array("string" => array("selected", "", ""),
30 "combo" => array("", "selected", ""),
31 "bool" => array("", "", "selected"));
33 /*! attribute list for save action */
34 var $attributes= array("base","goFonMacroParameter");
36 /*! Objectclasses needed by the class*/
37 var $objectclasses= array("top", "goFonMacro");
39 //! The konstructor of macroParameter
40 /*! The konstructor of macroParameter...
41 - reads goFonMacroParameter and parses them to an array
42 - Set attributes from openldap (edit)
43 - Set attributes from default (new)
44 */
45 function macroParameter ($config, $dn= NULL)
46 {
47 plugin::plugin ($config, $dn);
49 $tmp = array(); // temporary Var
50 $tmp2 = array(); // temporary Var ...
51 $tmp3 = "";
52 $ldap= $config->get_ldap_link();
54 $this->dn = $dn;
56 /* This is always an account */
57 $this->is_account= TRUE;
59 /* Edit or new one ?*/
60 if ($this->dn == "new"){
61 $ui= get_userinfo();
62 $this->base= dn2base($ui->dn);
63 } else {
64 $this->base= dn2base($this->dn);
65 }
67 /* initialising macro parameter */
68 if(isset($this->attrs['goFonMacroParameter']) &&
69 isset($this->attrs['goFonMacroParameter']['count'])){
70 unset($this->attrs['goFonMacroParameter']['count']);
71 }
73 /* Set Parameters, or a new array if ther are no parameters */
74 if(isset($this->attrs['goFonMacroParameter'])){
75 $this->goFonMacroParameter = $this->attrs['goFonMacroParameter'];
76 }else{
77 $this->goFonMacroParameter =array();
78 }
80 /* Create an array for parameters if not given yet */
81 if(!is_array($this->goFonMacroParameter)){
82 $tmp3 = $this->goFonMacroParameter;
83 $this->goFonMacroParameter =array();
84 if(!empty($tmp3)) {
85 $this->goFonMacroParameter[] = $tmp3;
86 }
87 }
89 /* Load parametersettings*/
90 foreach($this->goFonMacroParameter as $para){
91 $tmp = split("!",$para);
92 $num = $tmp[0];
93 $tmp2[$num]['name'] = base64_decode($tmp[1]);
94 $tmp2[$num]['type'] = $tmp[2];
95 $tmp2[$num]['default'] = $tmp[3];
96 $tmp2[$num]['var'] = "var".$num;
97 }
100 /* Assign this array */
101 $this->goFonMacroParameter = $tmp2;
103 $this->para_count = count ($tmp2);
105 $ui= get_userinfo();
106 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
107 $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
108 }
110 //! Perform Parameter check
111 /*!
112 Compares the given parameters (goFonMacroParameters) with the parameters defined in goFonContent\n
113 -> Decide which attrs are new and which are unused\n
114 -> Sort result array (containing both parameters 'goFonMacroParameters/goFonContent' and new / unused info)\n
115 \param $content The given goFonContent for this macro\n
116 \param $goFonMacroParameter Array with the already given parameters \n
117 */
118 function check_paras($content,$goFonMacroParameter)
119 {
120 /* Check contents for parameters */
121 preg_match_all("/[$]\{ARG[0-9]*\}/",$content,$res,PREG_OFFSET_CAPTURE);
123 $new = array();
125 /* Detect parameters with positions */
126 foreach($res[0] as $val){
127 $num = preg_replace("/[^0-9]/","",$val[0]);
128 $new[$num]['val'] = $val[0];
129 $new[$num]['num'] = $num;
130 }
132 /* Compare content parameter and macro parameter */
133 foreach($goFonMacroParameter as $gokey => $goval){
134 foreach($new as $nkey => $nval){
135 if($gokey == $nval['num']){
136 /* sign this as OK */
137 $goFonMacroParameter[$gokey]['check']= true;
138 }
139 }
140 }
142 /* Now check if there is new parameter in the content, which is not assigned yet */
143 foreach($new as $key => $val){
144 /* Assign std values */
145 $goFonMacroParameter[$key]['var']="var".$key;
146 $goFonMacroParameter[$key]['check']= true;
148 /* If this is a new Parameter, name it ${ARG#} by default*/
149 if((!isset($goFonMacroParameter[$key]['name']))||(empty($goFonMacroParameter[$key]['name']))){
150 $goFonMacroParameter[$key]['name']="\${ARG".$key."}";
151 }
152 }
154 foreach($goFonMacroParameter as $key => $val){
155 /* All attributes with check == false, are unneeded so mark them with ['check']= false */
156 if(!isset($goFonMacroParameter[$key]['check'])){
157 $goFonMacroParameter[$key]['check']= false;
158 }
159 /* Ah no default given assign ="" to prevent unsigned index */
160 if(!isset($goFonMacroParameter[$key]['default'])){
161 $goFonMacroParameter[$key]['default'] = "";
162 }
163 }
165 /* Sort output for better reading */
166 asort($goFonMacroParameter);
167 return($goFonMacroParameter);
169 }
171 //! Execute this Plugin
172 /*!
173 Perform Parameter check \n
174 Draw paramter table\n
175 Show tpl \n
176 */
177 function execute()
178 {
179 /* Call parent execute */
180 plugin::execute();
182 /* Variables */
183 $vars = "";
184 $tmp = array();
185 $number = 0;
187 $content = $_SESSION['macroManagment']->macrotabs->by_object['macro']->goFonMacroContent;
189 if(strstr($content,"ARG")){
190 $vorpos = strpos($content,"ARG");
191 $rest = substr($content,$vorpos, strlen($content));
192 }
194 /* Do we represent a valid group? */
195 if (!$this->is_account && $this->parent == NULL){
196 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
197 _("This 'dn' is no phone macro.")."</b>";
198 return ($display);
199 }
201 /* Fill templating stuff */
202 $smarty= get_smarty();
204 /* Assign all vars to Smarty */
205 foreach($this->attributes as $ar){
206 $smarty->assign($ar, $this->$ar);
207 }
209 /* Add an empty Parameter */
210 if(isset($_POST['addvar'])){
211 if(!is_array($this->goFonMacroParameter)){
212 $vars = $this->goFonMacroParameter;
213 $this->goFonMacroParameter = array();
214 $this->goFonMacroParameter[]= $vars;
215 }
216 $number= count($this->goFonMacroParameter);
217 $number++;
218 $this->goFonMacroParameter[]=array("var"=>"var","name"=>"new","type"=>"string","default"=>"test");
219 }
221 /*generate Table which shows als parameters */
222 $FonParas = $this->check_paras($content,$this->goFonMacroParameter);
224 /* Sort by Parameterid, and keep keys */
225 ksort($FonParas);
227 foreach($FonParas as $key=>$para) {
229 /* Select correct item of combobox */
230 if(isset($para['type'])){
231 list($sel1, $sel2, $sel3)= $this->type_shortcut[$para['type']];
232 }else{
233 list($sel1, $sel2, $sel3)= array("", "", "");
234 }
236 /* Assemble output table */
237 $vars .="<tr>
238 <td>
239 <input name=\"number".$key."\" value='".$key."' type='hidden'>
240 <input name='var".$key."' type='hidden' value='".$para['var']."'>ARG".$key."
241 </td>
242 <td><input size=\"45\" name='varname".$key."' value='".$para['name']."'></td>
243 <td>
244 <select name='vartype".$key."'>
245 <option value='string' ".$sel1.">"._("String")." </option>
246 <option value='combo' ".$sel2.">"._("Combobox")." </option>
247 <option value='bool' ".$sel3.">"._("Bool")." </option>
248 </select>
249 </td>
250 <td><input size=\"45\" name='default".$key."' value='".$para['default']."'></td>
251 <td> ";
252 if($para['check']==false) {
253 $vars.="<input name='del".$key."' value='"._("Delete unused")."' type='submit'>";
254 }
256 $vars.=" </td></tr>";
257 }
259 /* Checkboxes */
260 $smarty->assign("base_select", $this->base);
261 $smarty->assign("vars", $vars);
263 /* Show main page */
264 return($smarty->fetch (get_template_path('parameter.tpl', TRUE)));
265 }
267 //! Unused here
268 /*!
269 Unused here because goFonMacro will remove this Macro\n
270 */
271 function remove_from_parent()
272 {
273 }
275 //! Save our data
276 /*!
277 Save POST data to object \n
278 This gives us the possibility to leave a tab, without losing our typed informations\n
279 \n
280 Read the POST fields for the parameters and saves their info the the class\n
281 */
282 function save_object()
283 {
284 if (isset($_POST['phoneparameters'])){
285 plugin::save_object();
286 }
287 /* read out post data, and assign it to the parameters */
288 /* And or delete */
289 foreach($_POST as $name=>$value){
291 /* Test if there is a variable begining with "del" */
292 if(preg_match("/del/",$name)){
294 /* Extract entry id to delete */
295 $nr = str_replace("del","",$name) ;
297 /* unset entry */
298 unset($this->goFonMacroParameter[$nr]);
300 }elseif(preg_match("/number/",$name)){
302 /* Set Post vars */
303 $key = $_POST[$name];
305 $this->goFonMacroParameter[$key]['var'] = $_POST["var".$key];
306 $this->goFonMacroParameter[$key]['name'] = $_POST["varname".$key];
307 $this->goFonMacroParameter[$key]['type'] = $_POST["vartype".$key];
308 $this->goFonMacroParameter[$key]['default']= $_POST["default".$key];
309 }
310 }
312 }
315 //! Checks given values
316 /*!
317 Check values\n
318 If a user enters an invalid value, then this function will output an error msg\n
319 (In better words :prepare the errormessages that will be put out )\n
320 */
321 function check()
322 {
323 $message = array();
325 foreach($this->attributes as $attr){
326 if(chkacl($this->acl,"edit")){
327 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro."),$attr) ;
328 return(array($str));
329 }
330 }
333 foreach($this->goFonMacroParameter as $key=>$val){
334 if((strstr($val['default'],"!"))||(strstr($val['default'],"#"))) {
335 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
336 }
337 switch($val['type']){
338 case 'bool' : $possible = array("","0","1");
339 if(!in_array($val['default'],$possible)) {
340 $message[] = sprintf(_("The parameter %s has incorrect value for type bool."),$val['name']);
341 };break;
342 case 'string' :
343 case 'combo' :
344 default : ;
346 }
347 }
348 return $message;
349 }
351 //! Save changes to openldap
352 /*!
353 Save to LDAP
354 This function saves given attributes to the ldap
355 */
356 function save()
357 {
358 /* Post checks */
360 plugin::save();
362 $this->attrs['goFonMacroParameter']=array();
364 foreach($this->goFonMacroParameter as $key=>$fonpara){
365 $this->attrs['goFonMacroParameter'][]=$key."!".base64_encode($fonpara['name'])."!".$fonpara['type']."!".$fonpara['default'];
366 }
368 if($this->para_count != count($this->attrs['goFonMacroParameter'])){
369 print_red(_("Parameter count of the macro changed, you must update each user which are using this macro '%s'."),$this->dn);
370 }
372 unset($this->attrs['base']);
374 /* Write back to ldap */
375 $ldap= $this->config->get_ldap_link();
376 $ldap->cat($this->dn);
377 $a= $ldap->fetch();
379 if (count($a)){
380 $ldap->cd($this->dn);
381 $this->cleanup();
382 $ldap->modify ($this->attrs);
384 $this->handle_post_events("modify");
385 } else {
386 if(count($this->attrs['goFonMacroParameter']==0)){
387 unset($this->attrs['goFonMacroParameter']);
388 }
389 $ldap->cd($this->dn);
390 $ldap->create_missing_trees( $this->dn);
391 $ldap->cd($this->dn);
392 $ldap->add($this->attrs);
393 $this->handle_post_events("add");
394 }
395 show_ldap_error($ldap->get_error());
396 }
398 }
399 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
400 ?>