1 <?php
3 //! The Phone Macro Class: Handles Macro Contents, and some attributes.
4 /*!
5 This class handles the basic information about phone macros, like
6 cn base description displayName goFonMacroContent goFonMacroVisible
8 This is not the only Class that manages phone Macros, there ist also the class_goFonMacroParameter.
9 */
10 class macro extends plugin
11 {
12 /*! CLI vars */
13 var $cli_summary= "Handling of GOsa's macro object";
14 /*! CLI vars */
15 var $cli_description= "Some longer text\nfor help";
16 /*! CLI vars */
17 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
19 /*! Macro attributes, */
20 var $generate_error= "";
22 /*! The name of the Macro in the openldap drirectory */
23 var $cn = "";
25 /*! Display error once */
26 var $error_shown = false;
28 /*! This ist the variable that contains the description of the macro*/
29 var $description = "";
31 /*! The base of the macro, is used to save the macro in the correct directory tree */
32 var $base = "";
34 /*! This is the name of the macro which the enduser will see, instead of the cn */
35 var $displayName = "";
37 /*! Here is the macro content, the real macroscript */
38 var $goFonMacroContent= "";
40 /*! To allow user to use this macro this var must be true, else false */
41 var $goFonMacroVisible= 0;
43 /*! attribute list for save action */
44 var $attributes = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
46 var $is_new=false;
47 var $orig_cn = "";
48 /*! Objectclasses that this calls handles */
49 var $objectclasses = array("top", "goFonMacro");
51 //! The Konstructor
52 /*! Konstructor, load class with attributes of the given dn*/
53 function macro ($config, $dn= NULL)
54 {
55 plugin::plugin ($config, $dn);
57 $ldap= $config->get_ldap_link();
59 $this->dn = $dn;
61 /* This is always an account */
62 $this->is_account= TRUE;
65 /* Edit or new one ?*/
66 if ($this->dn == "new"){
67 if(isset($_SESSION['macrofilter']['depselect'])){
68 $this->base = $_SESSION['macrofilter']['depselect'];
69 $this->is_new = true;
70 }else{
71 $this->is_new = true;
72 $ui= get_userinfo();
73 $this->base= dn2base($ui->dn);
74 }
75 } else {
76 $this->is_new = false;
77 $this->orig_cn=$this->cn;
78 $this->base= dn2base($this->dn);
79 }
80 $ui= get_userinfo();
81 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
82 $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
83 }
85 /*! Execute this plugin */
86 function execute()
87 {
88 /* Call parent execute */
89 plugin::execute();
91 /* Variables */
92 $vars = "";
93 $tmp = array();
94 $number = 0;
96 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
97 print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). Your settings can't be saved to asterisk database."));
98 }
100 /* Do we represent a valid group? */
101 if (!$this->is_account && $this->parent == NULL){
102 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
103 _("This 'dn' is no phone macro.")."</b>";
104 return ($display);
105 }
107 /* Fill templating stuff */
108 $smarty= get_smarty();
109 $smarty->assign("bases", $this->config->idepartments);
111 /* Assign all vars to Smarty */
112 foreach($this->attributes as $ar){
113 $smarty->assign($ar, $this->$ar);
114 }
115 /* Checkboxes */
116 $smarty->assign("base_select", $this->base);
117 $smarty->assign("vars", $vars);
119 if($this->goFonMacroVisible){
120 $smarty->assign("goFonMacroVisibleChecked"," checked ");
121 }else{
122 $smarty->assign("goFonMacroVisibleChecked","");
123 }
125 if(isset($_POST['goFonMacroVisible'])) {
126 $this->goFonMacroVisible= 1 ;
127 $smarty->assign("goFonMacroVisibleChecked"," checked ");
128 }else {
129 if(isset($_POST['displayName'])){
130 $this->goFonMacroVisible= 0 ;
131 $smarty->assign("goFonMacroVisibleChecked","");
132 }
133 }
135 if(!$this->is_new){
136 $smarty->assign("disable_cn"," disabled ");
137 $smarty->assign("cn",$this->orig_cn);
138 }else{
139 $smarty->assign("disable_cn"," ");
140 $smarty->assign("cn",$this->cn);
141 }
142 $this->generate_mysql_entension_entries();
143 /* Show main page */
144 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
145 }
147 /*!
148 Remove this Object
149 */
150 function remove_from_parent()
151 {
152 $ldap= $this->config->get_ldap_link();
154 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
156 while ($val = $ldap->fetch()){
157 if(isset($val['goFonMacro'])){
158 if(strstr($val['goFonMacro'][0],$this->dn)){
159 print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
160 return false;
161 }
162 }
163 }
165 $ldap->rmDir($this->dn);
166 if(isset($this->orig_cn)){
167 $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
168 }else{
169 $this->generate_mysql_entension_entries(false,true);
170 }
172 /* Delete references to object groups */
173 $ldap->cd ($this->config->current['BASE']);
174 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
175 while ($ldap->fetch()){
176 $og= new ogroup($this->config, $ldap->getDN());
177 unset($og->member[$this->dn]);
178 $og->save ();
179 }
180 }
182 // Generate MySQL Syntax
183 function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
185 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
186 return(true);
187 }
189 if(!is_callable("mysql_connect")){
190 if((!$this->error_shown)&&($save)){
191 print_red(_("There is no mysql extension available in your php setup, can't save any changes to asterisk db."));
192 $this->error_shown = true;
193 }
194 return(true);
195 }
197 // Get Configuration for Mysql database Server
198 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
200 // Connect to DB server
201 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
203 // Check if we are connected correctly
204 if(!$r_con){
205 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
206 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
207 gosa_log(mysql_error());
208 return false;
209 }
211 // Select database for Extensions
212 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
214 // Test if we have the database selected correctly
215 if(!$db){
216 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
217 gosa_log(mysql_error());
218 return false;
219 }
222 // Context def
223 $context = "macro-".$this->cn;
225 // Parse Content if we connected correctly
226 if($db && $r_con ){
228 // Split Content into lines
229 $a_contentLines = split ("\n",$this->goFonMacroContent);
231 // Foreach single line ...
232 foreach($a_contentLines as $i_linenum => $s_linestr){
234 // Remove unwanted exten => tag
235 $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
237 // Remove spaces
238 $s_linestr = trim ( $s_linestr);
240 // Skip empty lines
241 if ($s_linestr == ""){
242 continue;
243 }
245 // If not empty or linebreak at [0]
246 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
248 // Set general SQL statement
249 $SQL[$i_linenum] =
250 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
251 " (context,exten,priority,app,appdata) ".
252 " VALUES ".
253 " (";
255 // Parse linestr to entry data
256 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
258 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
260 if(!isset($tmp[2])){
261 $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
262 return false;
263 }
264 $exten = $tmp[0];
265 $prio = $tmp[1];
266 $app = $tmp[2];
267 $para = $linecontents;
268 $para = preg_replace("/^.*\(/","",$para);
269 $para = preg_replace("/\)$/","",$para);
271 // Append SQL syntax
272 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
273 }
274 }
276 if(($save)||($delete_only)){
277 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
278 if(!$res){
279 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
280 gosa_log(mysql_error());
281 return false;
282 }
283 $cnt = mysql_fetch_row($res);
284 $cnt = $cnt[0];
286 if($cnt != 0) {
287 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
288 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
289 gosa_log(mysql_error());
290 return false;
291 }
292 }
294 if($remove_old_macroname!="false"){
295 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
296 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
297 gosa_log(mysql_error());
298 return false;
299 }
300 }
304 }
306 // We want to save this
307 if(($save)&&(isset($SQL))){
308 foreach($SQL as $entry){
309 if(!mysql_query($entry)){
310 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
311 gosa_log(mysql_error());
312 return false;
313 }
314 }
315 }
316 }
317 if((isset($r_con))&&($r_con)){
318 @mysql_close($r_con);
319 }
320 return true;
321 }
326 /*!
327 Save data to object
328 */
329 function save_object()
330 {
331 if (isset($_POST['displayName'])){
332 plugin::save_object();
333 }
334 foreach($this->attributes as $attr){
335 if(isset($_POST[$attr])){
336 $this->$attr= $_POST [$attr];
337 }
338 }
339 }
342 /*!
343 Check values
344 */
345 function check()
346 {
348 $message = array();
349 if(!$this->generate_mysql_entension_entries()){
350 $message[] = $this->generate_error;
351 }
353 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
354 $ldap = $this->config->get_ldap_link();
355 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
356 if($ldap->count()>0){
357 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
358 }
359 }
361 if(empty($this->displayName)){
362 $message[] = _("You must specify the 'Display Name' in order to save this macro");
363 }
365 if(strlen("Makro-".$this->cn)>20 ){
366 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
367 }
369 foreach($this->attributes as $attr){
370 if(chkacl($this->acl,"edit")){
371 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
372 return(array($str));
373 }
374 }
376 if(count(split("\n",$this->goFonMacroContent))>100){
377 $message[] = _("Makro length must be lower than 100 lines");
378 }
380 /*Some stupid IE fixes again*/
381 if(empty ($this->base)) {
382 $message[] = _("Please choose a valid base.");
383 }
385 return $message;
386 }
389 /*!
390 Save to LDAP
391 */
392 function save()
393 {
394 /* Post checks */
395 $this->execute();
397 plugin::save();
398 unset($this->attrs['base']);
400 /* Write back to ldap */
401 $ldap= $this->config->get_ldap_link();
402 $ldap->cat($this->dn);
403 $a= $ldap->fetch();
405 if(isset($this->orig_cn)){
406 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
407 }else{
408 $this-> generate_mysql_entension_entries(true);
409 }
411 if($this->generate_mysql_entension_entries()){
412 if (count($a)){
413 $ldap->cd($this->dn);
414 $this->cleanup();
415 $ldap->modify ($this->attrs);
417 $this->handle_post_events("modify");
418 } else {
419 $ldap->cd($this->config->current['BASE']);
420 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
421 $ldap->cd($this->dn);
422 $ldap->add($this->attrs);
423 $this->handle_post_events("add");
424 }
425 }
426 show_ldap_error($ldap->get_error());
427 }
429 }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>