Code

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