Code

Implemented multiserver to macros
[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     }
91   }
94   /*!  Execute this plugin */
95   function execute()
96   {
97     /* Call parent execute */
98     plugin::execute();
100     /* Variables */
101     $vars       = "";
102     $tmp        = array();
103     $number = 0; 
105     /* Base select dialog */
106     $once = true;
107     foreach($_POST as $name => $value){
108       if(preg_match("/^chooseBase/",$name) && $once){
109         $once = false;
110         $this->dialog = new baseSelectDialog($this->config,$this,$this->allowedBasesToMoveTo());
111         $this->dialog->setCurrentBase($this->base);
112       }
113     }
115     /* Dialog handling */
116     if(is_object($this->dialog)){
117       /* Must be called before save_object */
118       $this->dialog->save_object();
120       if($this->dialog->isClosed()){
121         $this->dialog = false;
122       }elseif($this->dialog->isSelected()){
124         /* A new base was selected, check if it is a valid one */
125         $tmp = $this->get_allowed_bases();
126         if(isset($tmp[$this->dialog->isSelected()])){
127           $this->base = $this->dialog->isSelected();
128         }
130         $this->dialog= false;
131       }else{
132         return($this->dialog->execute());
133       }
134     }
136     /* Fill templating stuff */
137     $smarty= get_smarty();
138     $smarty->assign("bases", $this->get_allowed_bases());
140     $tmp = $this->plInfo();
141     foreach($tmp['plProvidedAcls'] as $name => $translation){
142       $smarty->assign($name."ACL",$this->getacl($name));
143     }
145     if($this->acl_is_writeable("base")){
146       $smarty->assign("baseSelect",true);
147     }else{
148       $smarty->assign("baseSelect",false);
149     }
152     /* Assign all vars to Smarty */
153     foreach($this->attributes as $ar){
154       $smarty->assign($ar, $this->$ar);
155     }
156     /* Checkboxes */
157     $smarty->assign("base_select", $this->base);
158     $smarty->assign("vars", $vars);
160     if($this->goFonMacroVisible){
161       $smarty->assign("goFonMacroVisibleChecked"," checked ");
162     }else{
163       $smarty->assign("goFonMacroVisibleChecked","");
164     }
166     $smarty->assign("cnACL",$this->getacl("cn",$this->initially_was_account));
167     $smarty->assign("cn",$this->cn);
169     /* Ensure that macro content is displayed correctly encoded */
170     $smarty->assign("goFonMacroContent",htmlentities(utf8_decode ($this->goFonMacroContent)));
172     /* Show main page */
173     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
174   }
177   /* This method check if all databases are reachable.  
178    *  Returns with error message or an empty string on success.
179    * 
180    * - Is mysql extension available  
181    * - Is every server reachable 
182    * - Does the database exists/is accessible
183    */
184   function check_database_accessibility()
185   {
186     /* Check if mysql extension is available */
187     if(!is_callable("mysql_pconnect")){
188       return(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
189     }
191     /********************
192      * Check all home server
193      ********************/
194     foreach($this->goFonHomeServers as $goFonHomeServer => $cfg_Current){
195       $r_current    =  @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
196       if(!$r_current){
197         gosa_log(@mysql_error($r_current));
198         return(sprintf(_("The MySQL home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
199               $cfg_Current['SERVER'],$cfg_Current['LOGIN']));
200       }
201       $db_current  =  @mysql_select_db($cfg_Current['DB'],$r_current);
202       if(!$db_current){
203         gosa_log(@mysql_error($r_current));
204         mysql_close($r_current);
205         return( sprintf(_("Can't select database '%s' on home server '%s'."),$cfg_Current['DB'],$cfg_Current['SERVER']));
206       }
207     }
208   }
211   /* Remove current macro from all asterisk server.
212    * First of all check if we have access to all databases. 
213    * - Remove old entries 
214    */ 
215   function remove_from_database($save)
216   {
217     /* Check if all databases are reachable */
218     $str = $this->check_database_accessibility();
219     if($str){
220       return($str);
221     }
223     /* Create query string */
224     $context  = addslashes("macro-".$this->cn);
226     /* Remove current macro from each server available */ 
227     if($save){
228       foreach($this->goFonHomeServers as $dn => $Server){
229         $query      = "DELETE FROM ".$Server['EXT_TABLE']." WHERE context='".$context."';";
230         $r_current  =  @mysql_pconnect($Server['SERVER'],$Server['LOGIN'],$Server['PASSWORD']);
231         $db_current =  @mysql_select_db($Server['DB'],$r_current);
232         $res = @mysql_query($query,$r_current);
233         if(!$res){
234           gosa_log(@mysql_error($r_current));
235           return(sprintf(_("Removing marco from '%s' failed. Check GOsa log for mysql error."),$Server['SERVER']));
236         }
237         @mysql_close($r_current);
238       }
239     }
240   }
242  
243   /* Add current macro to all asterisk server.
244    * First of all check if we have access to all databases. 
245    * - Remove old entries 
246    * - Add new entries 
247    */ 
248   function add_to_database($save)
249   {
250     /* Check if all databases are reachable */
251     $str = $this->check_database_accessibility();
252     if($str){
253       return($str);
254     }
256     /* Remove old entries first. Else we got duplicated entries */
257     $str = $this->remove_from_database($save);
258     if($str){
259       return($str);
260     }
262     /* Create query string */
263     $context      = "macro-".$this->cn;
265     /************
266      * Parse Macro content
267      ************/
268     $sql = 
269       "INSERT INTO %TABLENAME% ".
270       " (context,exten,priority,app,appdata) ".
271       " VALUES ";
272       
273     $a_contentLines = split("\n",$this->goFonMacroContent);
274     foreach($a_contentLines as $i_linenum => $s_linestr){
276       /* Remove the 'exten => ' string in front of the macro content line 
277        *  example line  'exten => s,2,GotoIf(${ARG3}?3:5)'
278        * Remove comments introduced by ;
279        * Skip empty lines 
280        */ 
281       $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
282       $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
283       $s_linestr = trim($s_linestr);
284       if(empty($s_linestr)){
285         continue;
286       }
288       /* A line that passes the check above should look like this 
289        *  s,1,SetLanguage(de)
290        * 3 parts seperated by , 
291        * If there are more or less parts, abort.
292        * The preg_replace exclude parameters from split .. 
293        */
294       $tmp  = split(",", $s_linestr,3);
296       /* Check if there are exactly 2 , */ 
297 #      if(substr_count($s_linestr,",") !=2){
298 #        return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
299 #      }
300       /* Multiple () are not supproted currently ... */  
301       if(substr_count($s_linestr,"(") >1 ){
302         return(sprintf(_("More than one '(' is currently not supported. Line : '%s'."),$i_linenum));
303       }
304       if(substr_count($s_linestr,")") >1 ){
305         return(sprintf(_("More than one ')' is currently not supported. Line : '%s'."),$i_linenum));
306       }
307       /* Check if there is an application given */
308       if(empty($tmp[1])){
309         return(sprintf(_("There is no application given in line : '%s'."),$i_linenum));
310       } 
311       /* Check if there is an extension given */
312       if(empty($tmp[0])){
313         return(sprintf(_("There is no extension type given in line : '%s'."),$i_linenum));
314       } 
316       /* Create extension entry for current line 
317        *  and add this line to an array that will be inserted 
318        *  to each database.
319        */
320       $exten  = addslashes($tmp[0]);
321       $prio   = addslashes($tmp[1]);
322       $app    = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
323       $para   = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
324       $para   = preg_replace("/\).*$/","",$para);
325       $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
326     }
327     
328     /* Remove last , from query string */
329     $sql = preg_replace("/,$/","",$sql);
331     /* Save current changes to the database */
332     if($save){
333     
334       /* Macro are spread to each asterisk server */
335       foreach($this->goFonHomeServers as $dn => $cfg){
336         $r_con  = @mysql_pconnect($cfg['SERVER'],$cfg['LOGIN'],$cfg['PASSWORD']); 
337         $db     = @mysql_select_db($cfg['DB'],$r_con);
338         $query  = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
339         $res    = @mysql_query($query,$r_con);
340         if(!$res){
341           gosa_log(@mysql_error($r_con));
342           return(sprintf(_("Insert of new macro failed for server '%s'."),$cfg['SERVER']));
343         }
344         @mysql_close($r_con);
345       }
346     }
347   }
350   function save_object()
351   {
352     if (isset($_POST['gofonMacroGenericPosted'])){
354       $old_cn       = $this->cn;
355       $old_visible  = $this->goFonMacroVisible;
357       /* Create a base backup and reset the
358          base directly after calling plugin::save_object();
359          Base will be set seperatly a few lines below */
360       $base_tmp = $this->base;
361       plugin::save_object();
362       $this->base = $base_tmp;
364       /* Save base, since this is no LDAP attribute */
365       $tmp = $this->get_allowed_bases();
366       if(isset($_POST['base'])){
367         if(isset($tmp[$_POST['base']])){
368           $this->base= $_POST['base'];
369         }
370       }
372       /* Restore old cn if we have insuficient acls to change cn ... */
373       if(!$this->acl_is_writeable("cn",$this->initially_was_account)){
374         $this->cn = $old_cn;
375       }
377       /* check if we are allowed to toggle visibility */
378       if($this->acl_is_writeable("goFonMacroVisible")) {
380         /* Checkbox selected ? */
381         if(isset($_POST['goFonMacroVisible'])) {
382           $this->goFonMacroVisible= 1 ;
383         }else  {
384           if(isset($_POST['displayName'])){
385             $this->goFonMacroVisible= 0 ;
386           }
387         }
388       }else{
389         $this->goFonMacroVisible = $old_visible;
390       }
391     }
392   }
395   /*! Check values */
396   function check()
397   {
398     /* Call common method to give check the hook */
399     $message= plugin::check();
401     if(!count($this->goFonHomeServers)){
402       $message[] = _("There must be at least one server with an asterisk database to save this phone macro.");
403     }
405     /* Check if insert/replace is possible and all servers are available */
406     $str = $this->add_to_database(false);
407     if($str){
408       $message[] = $str;
409     }
411     /* Check if cn is already used  */
412     if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
413       $ldap = $this->config->get_ldap_link();
414       $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
415       if($ldap->count()>0){
416         $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
417       }
418     }
419   
420     /* Check if display name is set */
421     if(empty($this->displayName)){
422       $message[] = _("You must specify the 'Display Name' in order to save this macro");
423     }  
424     /* CN is restricted to 20 chars */
425     if(strlen("Makro-".$this->cn)>20 ){
426       $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
427     }
428   
429     /* If this macro is still in use we should not change the visible for user flag to invisible */
430     if(!$this->goFonMacroVisible){
431       $ldap = $this->config->get_ldap_link();
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           $message[] = _("This macro is still in use. It is necessary to mark this macro as visible for users.");
436           return($message);
437         }
438       }
439     }
441     /* Macro content must be smaller than 100 lines */
442     if(count(split("\n",$this->goFonMacroContent))>100){
443       $message[] = _("Makro length must be lower than 100 lines");
444     }
446     return $message;
447   }
450   /*! Remove makro from all given databases 
451    *   and ldap too.
452    */
453   function remove_from_parent()
454   {
455     $ldap= $this->config->get_ldap_link();
457     /* Skip remove if this macro is still in use */
458     $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
459     while ($val = $ldap->fetch()){ 
460       if(strstr($val['goFonMacro'][0],$this->dn)){ 
461         print_red(_("This macro is still in use. To delete this Macro ensure that nobody has selected it."));
462         return false;
463       }
464     }
466     /* Try to remove from database */
467     if(count($this->goFonHomeServers)){
468       $str = $this->remove_from_database(true);
469       if($str){ 
470         print_red($str);
471         return false;
472       }
473     }else{
474       print_red(_("Could not remove the macro entry from asterisk databases. Please check your asterisk database configurations."));
475       return false;
476     }
478     /* Remove phone macro */ 
479     $ldap->rmDir($this->dn); 
480     show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
482     /* Delete references to object groups */
483     $ldap->cd ($this->config->current['BASE']);
484     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
485     while ($ldap->fetch()){
486       $og= new ogroup($this->config, $ldap->getDN());
487       unset($og->member[$this->dn]);
488       $og->save ();
489       show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
490     }
491   }
494   /*! Save to LDAP */
495   function save()
496   {
497     plugin::save();
498     unset($this->attrs['base']);
500     /* Try to add entries to databases */
501     $str = $this->add_to_database(true);
502     if($str){
503       print_red($str);
504     }else{
505       /* Write back to ldap */
506       $ldap= $this->config->get_ldap_link();
507       $ldap->cat($this->dn, array('dn'));
508       $a= $ldap->fetch();
510       if (count($a)){
511         $ldap->cd($this->dn);
512         $this->cleanup();
513         $ldap->modify ($this->attrs); 
515         $this->handle_post_events("modify");
516       } else {
517         $ldap->cd($this->config->current['BASE']);
518         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
519         $ldap->cd($this->dn);
520         $ldap->add($this->attrs);
521         $this->handle_post_events("add");
522       }
523       show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonMacro/generic account with dn '%s' failed."),$this->dn));
524     }
525   }
528   function plInfo()
529   {
530     return (array(
531           "plShortName"   => _("Generic"),
532           "plDescription" => _("Asterisk macro management"),
533           "plSelfModify"  => FALSE,
534           "plDepends"     => array(),
535           "plPriority"    => 0,
536           "plSection"     => array("administration"),
537           "plCategory"    => array("gofonmacro" => array("description" => _("GOfon macro"),
538               "objectClass" => "gofonMacro")),
540           "plProvidedAcls" => array(
541             "cn"                            => _("Macro name"),
542             "base"                          => _("Base"),
543             "description"                   => _("Description"),
544             "displayName"                   => _("Display name"),
545             "goFonMacroContent"             => _("Macro content and parameter"),
546             "goFonMacroVisible"             => _("Visibility flag"))
547           ));
548   }
551 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
552 ?>