Code

Removed old CLI stuff - cleanup
[gosa.git] / plugins / gofon / macro / class_gofonMacro.inc
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   /*! Macro attributes,  */
13   var $generate_error= "";
14   
15   /*! The name of the Macro in the openldap drirectory */
16   var $cn               = ""; 
17  
18   /*! Display error once */
19   var $error_shown = false; 
21   /*! This ist the variable that contains the description of the macro*/
22   var $description      = "";
24   /*! The base of the macro, is used to save the macro in the correct directory tree */
25   var $base             = "";
27   /*! This is the name of the macro which the enduser will see, instead of the cn */
28   var $displayName      = "";
29     
30   /*! Here is the macro content, the real macroscript */
31   var $goFonMacroContent= "";
32   
33   /*! To allow user to use this macro this var must be true, else false */
34   var $goFonMacroVisible= 0;
36   /*! attribute list for save action */
37   var $attributes     = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
39   var $orig_cn = ""; 
40   /*! Objectclasses that this calls handles */
41   var $objectclasses  = array("top", "goFonMacro");
43   var $goFonHomeServers = array(); // Contains all available asterisk database server 
45   //! The Konstructor   
46   /*!  Konstructor, load class with  attributes of the given dn*/
47   function macro ($config, $dn= NULL, $parent= NULL)
48   {
49     plugin::plugin ($config, $dn, $parent);
51     /* This is always an account */
52     $this->is_account= TRUE;
54     /* Edit or new one ?*/
55     if ($this->dn == "new"){
56       if(isset($_SESSION['CurrentMainBase'])){
57         $this->base = $_SESSION['CurrentMainBase'];
58       }else{
59         $ui= get_userinfo();
60         $this->base= dn2base($ui->dn);
61       }
62     } else {
63       $this->orig_cn=$this->cn;
64       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,[^,]+,[^,]+,/", "", $this->dn);
65     }
67     /* Check server configurations
68      * Load all server configuration in $this->goFonHomeServers if available
69      */
70     $a_SETUP= array();
71     if(array_key_exists('config',$_SESSION) &&
72        array_key_exists('SERVERS',$_SESSION['config']->data) &&
73        count($_SESSION['config']->data['SERVERS']['FON']) && 
74        array_key_exists('FON',$_SESSION['config']->data['SERVERS'])) {
76       /* Set available server */
77       $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
78   
79       /* Remove default entry, not necessary here */
80       if(isset($this->goFonHomeServers[0])){
81         unset($this->goFonHomeServers[0]);  
82       }
83     }
85     /* Load acl */
86     $ui       = get_userinfo();
87     $acl      = get_permissions ($ui->dn, $ui->subtreeACL);
88     $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
89   }
92   /*!  Execute this plugin */
93   function execute()
94   {
95     /* Call parent execute */
96     plugin::execute();
98     /* Variables */
99     $vars       = "";
100     $tmp        = array();
101     $number = 0; 
103     /* Base select dialog */
104     $once = true;
105     foreach($_POST as $name => $value){
106       if(preg_match("/^chooseBase/",$name) && $once){
107         $once = false;
108         $this->dialog = new baseSelectDialog($this->config,$this->allowedBasesToMoveTo());
109         $this->dialog->setCurrentBase($this->base);
110       }
111     }
113     /* Dialog handling */
114     if(is_object($this->dialog)){
115       /* Must be called before save_object */
116       $this->dialog->save_object();
118       if($this->dialog->isClosed()){
119         $this->dialog = false;
120       }elseif($this->dialog->isSelected()){
121         $this->base = $this->dialog->isSelected();
122         $this->dialog= false;
123       }else{
124         return($this->dialog->execute());
125       }
126     }
128     /* Fill templating stuff */
129     $smarty= get_smarty();
130     $smarty->assign("bases", $this->config->idepartments);
132     /* Assign all vars to Smarty */
133     foreach($this->attributes as $ar){
134       $smarty->assign($ar, $this->$ar);
135       $smarty->assign($ar."ACL", chkacl($this->acl,$ar));
136     }
137     /* Checkboxes */
138     $smarty->assign("base_select", $this->base);
139     $smarty->assign("vars", $vars);
141     if($this->goFonMacroVisible){
142       $smarty->assign("goFonMacroVisibleChecked"," checked ");
143     }else{
144       $smarty->assign("goFonMacroVisibleChecked","");
145     }
147     if($this->dn != "new"){
148       $smarty->assign("disable_cn"," disabled ");
149     }else{
150       $smarty->assign("disable_cn","  ");
151     }
153     /* Ensure that macro content is displayed correctly encoded */
154     $smarty->assign("goFonMacroContent",htmlentities(utf8_decode ($this->goFonMacroContent)));
156     /* Show main page */
157     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
158   }
161   /* This method check if all databases are reachable.  
162    *  Returns with error message or an empty string on success.
163    * 
164    * - Is mysql extension available  
165    * - Is every server reachable 
166    * - Does the database exists/is accessible
167    */
168   function check_database_accessibility()
169   {
170     /* Check if mysql extension is available */
171     if(!is_callable("mysql_pconnect")){
172       return(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
173     }
175     /********************
176      * Check all home server
177      ********************/
178     foreach($this->goFonHomeServers as $goFonHomeServer => $cfg_Current){
179       $r_current    =  @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
180       if(!$r_current){
181         gosa_log(@mysql_error($r_current));
182         return(sprintf(_("The MySQL home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
183               $cfg_Current['SERVER'],$cfg_Current['LOGIN']));
184       }
185       $db_current  =  @mysql_select_db($cfg_Current['DB'],$r_current);
186       if(!$db_current){
187         gosa_log(@mysql_error($r_current));
188         mysql_close($r_current);
189         return( sprintf(_("Can't select database '%s' on home server '%s'."),$cfg_Current['DB'],$cfg_Current['SERVER']));
190       }
191     }
192   }
195   /* Remove current macro from all asterisk server.
196    * First of all check if we have access to all databases. 
197    * - Remove old entries 
198    */ 
199   function remove_from_database($save)
200   {
201     /* Check if all databases are reachable */
202     $str = $this->check_database_accessibility();
203     if($str){
204       return($str);
205     }
207     /* Create query string */
208     $context  = addslashes("macro-".$this->cn);
210     /* Remove current macro from each server available */ 
211     if($save){
212       foreach($this->goFonHomeServers as $dn => $Server){
213         $query      = "DELETE FROM ".$Server['EXT_TABLE']." WHERE context='".$context."';";
214         $r_current  =  @mysql_pconnect($Server['SERVER'],$Server['LOGIN'],$Server['PASSWORD']);
215         $db_current =  @mysql_select_db($Server['DB'],$r_current);
216         $res = @mysql_query($query,$r_current);
217         if(!$res){
218           gosa_log(@mysql_error($r_current));
219           return(sprintf(_("Removing macro from '%s' failed. Check GOsa log for mysql error."),$Server['SERVER']));
220         }
221         @mysql_close($r_current);
222       }
223     }
224   }
226  
227   /* Add current macro to all asterisk server.
228    * First of all check if we have access to all databases. 
229    * - Remove old entries 
230    * - Add new entries 
231    */ 
232   function add_to_database($save)
233   {
234     /* Check if all databases are reachable */
235     $str = $this->check_database_accessibility();
236     if($str){
237       return($str);
238     }
240     /* Remove old entries first. Else we got duplicated entries */
241     $str = $this->remove_from_database($save);
242     if($str){
243       return($str);
244     }
246     /* Create query string */
247     $context      = "macro-".$this->cn;
249     /************
250      * Parse Macro content
251      ************/
252     $sql = 
253       "INSERT INTO %TABLENAME% ".
254       " (context,exten,priority,app,appdata) ".
255       " VALUES ";
256       
257     $a_contentLines = split("\n",$this->goFonMacroContent);
258     foreach($a_contentLines as $i_linenum => $s_linestr){
260       /* Remove the 'exten => ' string in front of the macro content line 
261        *  example line  'exten => s,2,GotoIf(${ARG3}?3:5)'
262        * Remove comments introduced by ;
263        * Skip empty lines 
264        */ 
265       $s_linestr = trim($s_linestr);
266       $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
267       $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
269       if(empty($s_linestr)){
270         continue;
271       }
273       /* A line that passes the check above should look like this 
274        *  s,1,SetLanguage(de)
275        * 3 parts seperated by , 
276        * If there are more or less parts, abort.
277        * The preg_replace exclude parameters from split .. 
278        */
279       $tmp  = split(",", $s_linestr,3);
281       /* Check if there are exactly 2 , */ 
282 #      if(substr_count($s_linestr,",") !=2){
283 #        return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
284 #      }
285       /* Multiple () are not supproted currently ... */  
286       if(substr_count($s_linestr,"(") >1 ){
287         return(sprintf(_("More than one '(' is currently not supported. Line : '%s'."),$i_linenum));
288       }
289       if(substr_count($s_linestr,")") >1 ){
290         return(sprintf(_("More than one ')' is currently not supported. Line : '%s'."),$i_linenum));
291       }
292       /* Check if there is an application given */
293       if(empty($tmp[1])){
294         return(sprintf(_("There is no application given in line : '%s'."),$i_linenum));
295       } 
296       /* Check if there is an extension given */
297       if(empty($tmp[0])){
298         return(sprintf(_("There is no extension type given in line : '%s'."),$i_linenum));
299       } 
301       /* Create extension entry for current line 
302        *  and add this line to an array that will be inserted 
303        *  to each database.
304        */
305       $exten  = addslashes($tmp[0]);
306       $prio   = addslashes($tmp[1]);
307       $app    = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
308       $para   = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
309       $para   = preg_replace("/\).*$/","",$para);
310       $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
311     }
312     
313     /* Remove last , from query string */
314     $sql = preg_replace("/,$/","",$sql);
316     /* Save current changes to the database */
317     if($save){
318     
319       /* Macro are spread to each asterisk server */
320       foreach($this->goFonHomeServers as $dn => $cfg){
321         $r_con  = @mysql_pconnect($cfg['SERVER'],$cfg['LOGIN'],$cfg['PASSWORD']); 
322         $db     = @mysql_select_db($cfg['DB'],$r_con);
323         $query  = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
324         $res    = @mysql_query($query,$r_con);
325         if(!$res){
326           gosa_log(@mysql_error($r_con));
327           return(sprintf(_("Insert of new macro failed for server '%s'."),$cfg['SERVER']));
328         }
329         @mysql_close($r_con);
330       }
331     }
332   }
335   /*! Save data to object */
336   function save_object()
337   {
338     if (isset($_POST['displayName'])){
339       plugin::save_object();
341       /* The cn can't be changed if this entry is not new */
342       if($this->dn!= "new"){
343         $this->cn = $this->orig_cn;
344       }
346       if(isset($_POST['goFonMacroVisible'])) {
347         $this->goFonMacroVisible= 1 ;
348       }else  {
349         $this->goFonMacroVisible= 0 ;
350       }
351     }
352   }
355   /*! Check values */
356   function check()
357   {
358     /* Call common method to give check the hook */
359     $message= plugin::check();
361     if(!count($this->goFonHomeServers)){
362       $message[] = _("There must be at least one server with an asterisk database to save this phone macro.");
363     }
365     /* Check if insert/replace is possible and all servers are available */
366     $str = $this->add_to_database(false);
367     if($str){
368       $message[] = $str;
369     }
371     /* Check if cn is already used  */
372     if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
373       $ldap = $this->config->get_ldap_link();
374       $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
375       if($ldap->count()>0){
376         $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
377       }
378     }
379   
380     /* Check if display name is set */
381     if(empty($this->displayName)){
382       $message[] = _("You must specify the 'Display Name' in order to save this macro");
383     }  
384     /* CN is restricted to 20 chars */
385     if(strlen("Makro-".$this->cn)>20 ){
386       $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
387     }
388   
389     /* Check permissions */
390     foreach($this->attributes as $attr){
391       if(chkacl($this->acl,"edit")){
392         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
393         return(array($str));
394       }
395     }
397     /* If this macro is still in use we should not change the visible for user flag to invisible */
398     if(!$this->goFonMacroVisible){
399       $ldap = $this->config->get_ldap_link();
400       $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
401       while ($val = $ldap->fetch()){
402         if(strstr($val['goFonMacro'][0],$this->dn)){
403           $message[] = _("This macro is still in use. It is necessary to mark this macro as visible for users.");
404           return($message);
405         }
406       }
407     }
409     /* Macro content must be smaller than 100 lines */
410     if(count(split("\n",$this->goFonMacroContent))>100){
411       $message[] = _("Makro length must be lower than 100 lines");
412     }
414     return $message;
415   }
418   /*! Remove makro from all given databases 
419    *   and ldap too.
420    */
421   function remove_from_parent()
422   {
423     $ldap= $this->config->get_ldap_link();
425     /* Skip remove if this macro is still in use */
426     $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
427     while ($val = $ldap->fetch()){ 
428       if(strstr($val['goFonMacro'][0],$this->dn)){ 
429         print_red(_("This macro is still in use. To delete this Macro ensure that nobody has selected it."));
430         return false;
431       }
432     }
434     /* Try to remove from database */
435     if(count($this->goFonHomeServers)){
436       $str = $this->remove_from_database(true);
437       if($str){ 
438         print_red($str);
439         return false;
440       }
441     }else{
442       print_red(_("Could not remove the macro entry from asterisk databases. Please check your asterisk database configurations."));
443       return false;
444     }
446     /* Remove phone macro */ 
447     $ldap->rmDir($this->dn); 
448     show_ldap_error($ldap->get_error(), _("Removing phone macro failed"));
450     /* Delete references to object groups */
451     $ldap->cd ($this->config->current['BASE']);
452     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
453     while ($ldap->fetch()){
454       $og= new ogroup($this->config, $ldap->getDN());
455       unset($og->member[$this->dn]);
456       $og->save ();
457       show_ldap_error($ldap->get_error(), _("Removing phone macro reverences failed"));
458     }
459   }
462   /*! Save to LDAP */
463   function save()
464   {
465     plugin::save();
466     unset($this->attrs['base']);
468     /* Try to add entries to databases */
469     $str = $this->add_to_database(true);
470     if($str){
471       print_red($str);
472     }else{
473       /* Write back to ldap */
474       $ldap= $this->config->get_ldap_link();
475       $ldap->cat($this->dn, array('dn'));
476       $a= $ldap->fetch();
478       if (count($a)){
479         $ldap->cd($this->dn);
480         $this->cleanup();
481         $ldap->modify ($this->attrs); 
483         $this->handle_post_events("modify");
484       } else {
485         $ldap->cd($this->config->current['BASE']);
486         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
487         $ldap->cd($this->dn);
488         $ldap->add($this->attrs);
489         $this->handle_post_events("add");
490       }
491       show_ldap_error($ldap->get_error(), _("Saving phone macro failed"));
492     }
493   }
496 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
497 ?>