Code

43701df19b1a3f8dec9b3a9c4662c38d79245a56
[gosa.git] / plugins / gofon / conference / class_phoneConferenceGeneric.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class conference extends plugin
22 {
23   /* department attributes */
24   var $cn                                                 = "";
25   var $description              = "";
26   var $base                               = "";
27   var $ou                                               = "";
28   var $goFonPIN                         = "";
29   var $old_cn         = "";
31   var $goFonConferenceOption    = "";
32   var $goFonConferenceOption_P  = "";   // Set PIN 
33   var $goFonConferenceOption_r  = "";   // record Conference
34   var $goFonConferenceOption_M  = "";   // Play Music opn hold
35   var $goFonConferenceOption_s  = "";   // activate menu
36   var $goFonConferenceOption_i  = "";   // announce new and leaving user
37   var $goFonConferenceOption_c  = "";   // Count User
38   var $goFonConferenceOption_D  = "";   // Conference Type, no PIN/PIN
40   var $goFonConferenceOwner     = "";
41   var $goFonHomeServer          = "0";      // Home server of the conference
42   var $init_HomeServer          = "0";      // Initial home server of the conference
43   var $goFonHomeServers         = array();  // All available home servers
45   var $error_shown = false;
47   var $goFonConferenceOptionFormat              = "";
48   var $goFonConferenceOptionLifetime    = "";
49   var $telephoneNumber          = "";
51   var $old_tele_number          = false;
52   var $generate_error           = "";
54   var $old_dn;
56   /* Headpage attributes */
57   var $last_dep_sorting= "invalid";
58   var $departments= array();
60   var $dialog ;
62   /* attribute list for save action */
63   var $attributes= array("cn","base", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
64       "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c","goFonHomeServer",
65       "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber","goFonConferenceOwner");
67   var $objectclasses= array("top", "goFonConference");
69   function conference ($config, $dn, $plugin= NULL)
70   {
71     plugin::plugin($config, $dn, $plugin);
72     $this->is_account   = TRUE;
73     $this->ui                       = get_userinfo();
74     $this->orig_dn              = $dn;
76     
77     /* Check server configurations
78      * Load all server configuration in $this->goFonHomeServers if available
79      *  and first server as default if necessary.
80      * Check if connection is successfull for the selected server $this->goFonHomeServer
81      */
82     $a_SETUP= array();
83     if(array_key_exists('config',$_SESSION) &&
84        array_key_exists('SERVERS',$_SESSION['config']->data) &&
85        array_key_exists('FON',$_SESSION['config']->data['SERVERS']) &&
86        is_callable("mysql_connect")
87        ) {
89       /* Set available server */
90       $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
92       /* Set default server */
93       if($this->dn == "new"){
94         $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
95       }
97       /* Remember inital home server, to be able to remove old entries */
98       $this->init_HomeServer = $this->goFonHomeServer;
100       /* get config */
101       if(!isset($this->goFonHomeServers[$this->goFonHomeServer])){
102         print_red(sprintf(_("The specified goFonHomeServer '%s' is not available in GOsa server configuration. Saving this account will create a new entry on the server '%s'. Use cancel if you do not want to create a new entry and ignoring old accounts."),$this->goFonHomeServer, $this->goFonHomeServers[0]['DN']));
104         $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
105         $this->init_HomeServer = $this->goFonHomeServers[0]['DN'];
106       }
107       $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer]; 
108     }
110     /* Set base */
111     if ($this->dn == "new"){
112       $ui= get_userinfo();
113       if(isset($_SESSION['CurrentMainBase'])){
114         $this->base = $_SESSION['CurrentMainBase'];
115       }else{
116         $this->base= dn2base($ui->dn);
117       }
118     } else {
120       /* The base is something like this 
121           "cn=Confis,ou=conferences,ou=asterisk,ou=configs,ou=systems,"  
122       */
123       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,[^,]+,[^,]+,/", "", $this->dn);
124     }
126     $ui= get_userinfo();
127     $acl= get_permissions ($ui->dn, $this->ui->subtreeACL);
128     $this->acl= get_module_permission($acl, "conference", $ui->dn);
129     $this->goFonConferenceOwner=$this->ui->dn;
131     /* Parse Options ... 
132      * Parameter|Lifetime|number
133      */
134     if($this->dn!="new"){
135       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
137       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
138         $varname = "goFonConferenceOption_".$tmp1[0][$i];
139         if($tmp1[0][$i]=="d"){
140           $this->goFonConferenceOption_D = $tmp1[0][$i];    
141         }else{
142           $this->$varname = $tmp1[0][$i];    
143         }
144       }
146       $this->goFonConferenceOptionLifetime  = $tmp1[1];
147       $this->old_tele_number                = $this->telephoneNumber;
148     }
149     $this->old_dn = $this->dn;
150     $this->old_cn = $this->cn;
151   }
154   function execute()
155   {
156     /* Call parent execute */
157     plugin::execute();
159     $smarty= get_smarty();
161     $smarty->assign("bases" ,$this->config->idepartments);
162     $smarty->assign("base"  ,$this->base);
164     $once = true; 
165     foreach($_POST as $name => $value){
166       if(preg_match("/^chooseBase/",$name) && $once){
167         $once = false;
168         $this->dialog = new baseSelectDialog($this->config);
169         $this->dialog->setCurrentBase($this->base);
170       }
171     }
173     /* Dialog handling */
174     if(is_object($this->dialog)){
175       /* Must be called before save_object */
176       $this->dialog->save_object();
178       if($this->dialog->isClosed()){
179         $this->dialog = false;
180       }elseif($this->dialog->isSelected()){
181         $this->base = $this->dialog->isSelected();
182         $this->dialog= false;
183       }else{
184         return($this->dialog->execute());
185       }
186     }
188     /* Create array with goFonHomeServer */
189     $tmp = array();
190     foreach($this->goFonHomeServers as $dn => $val){
191       if(!is_numeric($dn)){
192         $tmp[$dn]  = $val['SERVER'];
193       }
194     }
195     $smarty->assign("goFonHomeServers",$tmp);
196     $smarty->assign("goFonConferenceOptions",               array("D"=>"Conference ","d"=>"Conference without PIN"));
197     $smarty->assign("goFonConferenceOptionFormats",     array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
198     $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
200     foreach ($this->attributes as $val){
201       $smarty->assign("$val", $this->$val);
202       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
203       if(!$this->$val){
204         $smarty->assign($val."CHK", "");
205       }else{
206         $smarty->assign($val."CHK", " checked ");
207       }
208     }
210     if($_SESSION['js']==1){
211       if($this->goFonConferenceOption_P != "P"){
212         $smarty->assign("goFonPINACL"," disabled ");
213         $smarty->assign("goFonPIN","");
214       }
215       if($this->goFonConferenceOption_r != "r"){
216         $smarty->assign("goFonConferenceOptionFormatACL"," disabled ");
217       }
218     }
219     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
220   }
222   function remove_from_parent()
223   {
224     $str = $this->SQL_remove_me(true); 
225     if($str){
226       print_red($str);
227     }
229     $ldap= $this->config->get_ldap_link();
230     $ldap->cd ($this->dn);
231     $ldap->recursive_remove();
233     /* Optionally execute a command after we're done */
234     $this->handle_post_events('remove');
235   }
238   /* Save data to object */
239   function save_object()
240   {
241     plugin::save_object();
242     if(isset($_POST['cn'])){
243       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
244             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonPIN") as $attrs){
245         if(isset($_POST[$attrs])){
246           $this->$attrs = $_POST[$attrs];
247         }else{
248           $this->$attrs = false;
249         }
250       }
251     }
252   }
255   function check_database_accessibility()
256   {
258     /* Check if mysql extension is available */
259     if(!is_callable("mysql_pconnect")){
260       return(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
261     }
263     /********************
264      * Check currently selected home server 
265      ********************/
267     $cfg_Current  = $this->goFonHomeServers[$this->goFonHomeServer] ;
268     $r_current    =  @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
269     if(!$r_current){
270       gosa_log(@mysql_error($r_current));
271       return(sprintf(_("The MySQL home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
272             $cfg_Current['SERVER'],$cfg_Current['LOGIN']));
273     }
274     $db_current  =  @mysql_select_db($cfg_Current['DB'],$r_current);
275     if(!$db_current){
276       gosa_log(@mysql_error($r_current));
277       mysql_close($r_current);
278       return( sprintf(_("Can't select database '%s' on home server '%s'."),$cfg_Current['DB'],$cfg_Current['SERVER']));
279     }
281     /********************
282      * Check init home server 
283      ********************/
285     if($this->goFonHomeServers != $this->init_HomeServer){
286       $cfg_Init  = $this->goFonHomeServers[$this->init_HomeServer] ;
287       $r_init    =  @mysql_pconnect($cfg_Init['SERVER'],$cfg_Init['LOGIN'],$cfg_Init['PASSWORD']);
288       if(!$r_init){
289         gosa_log(@mysql_error($r_init));
290         return(sprintf(_("The MySQL initial home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
291               $cfg_Init['SERVER'],$cfg_Init['LOGIN']));
292       }
293       $db_init  =  @mysql_select_db($cfg_Init['DB'],$r_init);
294       if(!$db_init){
295         gosa_log(@mysql_error($r_init));
296         mysql_close($r_init);
297         return( sprintf(_("Can't select database '%s' on initial home server '%s'."),$cfg_Init['DB'],$cfg_Init['SERVER']));
298       }
299     }
300   }
302   /* Check values */
303   function check()
304   {
305     /* Call common method to give check the hook */
306     $message= plugin::check();
308     if($this->is_number_used()){
309       $message[] =  $this->is_number_used();
310     }
312     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
313       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
314     }
316     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
317       $message[]= _("Please enter a PIN.");
318     }
320     if(empty($this->cn)){
321       $message[] =_("Please enter a name for the conference.");
322     }
324     if(!is_numeric($this->telephoneNumber)){
325       $message[] =_("Only numeric chars are allowed in Number field.");
326     }
328     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
329       $message[] =_("Only numbers are allowed in Lifetime.");
330     }
332     $str = $this->SQL_add_me(false);
333     if(!empty($str)){
334       $message[] = $str;
335     }
337     return $message;
338   }
341   function SQL_add_me($save)
342   {
343     /* Check if there is at least on server configuration */
344     if(!count($this->goFonHomeServers)){
345       return( _("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."));
346     }
347   
348     /********************
349      * Get configuration and try to connect 
350      ********************/
352     /* Check if databases are reachable, returns an error string if anything fails  */
353     $error_str = $this->check_database_accessibility();
354     if($error_str){
355       return($error_str);
356     }
358     /* Remove old entries, returns an error string if anything fails  */
359     $error_str = $this->SQL_remove_me($save);
360     if($error_str){
361       return($error_str);
362     }
365     /* Connect to current database to be able to add new entries */
366     $cfg_Current  = $this->goFonHomeServers[$this->goFonHomeServer] ;
367     $res_cur      =  @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
368     $db_cur       =  @mysql_select_db($cfg_Current['DB'],$res_cur);
370     /********************
371      * Remove entries that could cause trouble  
372      ********************/
374     /* If the current home server is different to the initial home server,
375      *  there may be already some entries with the given telephoneNumber and/or cn.
376      * We must remove those entries to avoid duplicate use of the same extension name.
377      */
378     if($this->goFonHomeServer != $this->init_HomeServer){
379       $query = "SELECT id FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->telephoneNumber."' OR '".$this->cn."';";
380       $res = @mysql_query($query,$res_cur);
381       if(!$res){
382         gosa_log(@mysql_error($res_cur));
383         return(_("Can not check if there are already some entries with given telephone number and/or cn in the destination home server.").
384             "&nbsp;"._("Please have a look a the gosa logfiles."));
385       }
386       if($save && mysql_affected_rows($res_cur)) {
387         $SQL = "DELETE FROM ".$cfg_Current['EXT_TABLE']." 
388           WHERE   (exten='".$this->telephoneNumber."') 
389           OR    (exten='".$this->cn."')";
391         /* Query and ensure that everything went fine */
392         $res =  @mysql_query($SQL,$res_cur);
393         if(!$res){
394           gosa_log(@mysql_error($res_cur));
395           return(_("Can not remove entries with some telephone number and/or cn from destination home server.").
396               "&nbsp;"._("Please have a look a the gosa logfiles."));
397         }
398       }
399     }
401     /********************
402      * Add new conference entry  
403      ********************/
404     if((!empty($this->telephoneNumber))&&($save==true)){
406       /* Create string out of conference Flags */
407       $parameter  ="";
408       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
409             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
410         $parameter .= $this->$attrs;
411       }
413       $i=1;
414       $EXT=array();
415       $context="GOsa";
416       // Set Language to German
417       $EXT[$i]['exten']   =$this->telephoneNumber;
418       $EXT[$i]['context'] = $context;
419       $EXT[$i]['priority']= $i;
420       $EXT[$i]['app']     ="SetLanguage";
421       $EXT[$i]['appdata'] ="de";
422       $i++;      
424       if($this->goFonConferenceOption_r == "r"){
426         // Recordingformat for conference
427         $EXT[$i]['exten']   =$this->telephoneNumber;
428         $EXT[$i]['context'] =$context;
429         $EXT[$i]['priority']= $i;
430         $EXT[$i]['app']     ="Setvar";
431         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
432         $i++;      
434       }
436       // Answer Call
437       $EXT[$i]['exten']   =$this->telephoneNumber;
438       $EXT[$i]['context'] =$context;
439       $EXT[$i]['priority']=$i;
440       $EXT[$i]['app']     ="answer";
441       $EXT[$i]['appdata'] ="";
442       $i++;      
444       // Start Conference 
445       $EXT[$i]['exten']   =$this->telephoneNumber;
446       $EXT[$i]['context'] =$context;
447       $EXT[$i]['priority']=$i;
448       $EXT[$i]['app']     ="MeetMe";
450       if(empty($this->goFonPIN)) {
451         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
452       }else{
453         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
454       }
455       $i++; 
457       // Start Conference 
458       $EXT[$i]['exten']   =$this->cn;
459       $EXT[$i]['context'] =$context;
460       $EXT[$i]['priority']=1;
461       $EXT[$i]['app']     ="Goto";
462       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
463       $SQL=array();
465       foreach($EXT as $keytop => $valtop){
466         $s_keys = "";
467         $s_values = ""; 
468         foreach($valtop as $key=>$val){
469           $s_keys   .="`".$key."`,";
470           $s_values .="'".$val."',";
471         }
472         $s_keys   =preg_replace("/\,$/","",$s_keys); 
473         $s_values =preg_replace("/\,$/","",$s_values); 
474         $SQL[]="INSERT INTO ".$cfg_Current['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
475       }
476       foreach($SQL as $sqlsyn){
477         mysql_query($sqlsyn,$res_cur);
478       }
479     } 
481     @mysql_close($res_cur);
482   }
485   /* Remove initial entry from database 
486    * This function checks if there is an entry in the 
487    *  initial home server that uses this->old_cn or $this->old_tele_number
488    *  and removes this entries. 
489    * This function is called from save and remove_from parent.
490    * 
491    *  The parameter '$save' is false if we just 
492    *   want to check if a remove is possible. 
493    *  And true if we realy want to remove the entries.   
494    */
495   function SQL_remove_me($save)
496   {
497     /* check database access */
498     $str = $this->check_database_accessibility();
499     if($str){
500       return($str);
501     }
503     /* Connect to old database */
504     $cfg_Init  = $this->goFonHomeServers[$this->init_HomeServer] ;
505     $r_init    =  @mysql_pconnect($cfg_Init['SERVER'],$cfg_Init['LOGIN'],$cfg_Init['PASSWORD']);
506     $db_init   =  @mysql_select_db($cfg_Init['DB'],$r_init);
507    
508     /* Check if there is an old entry */
509     $query = "SELECT id FROM ".$cfg_Init['EXT_TABLE']." WHERE exten='".$this->old_tele_number."' OR '".$this->old_cn."';";
510     $res = @mysql_query($query,$r_init);
511     if(!$res){
512       gosa_log(@mysql_error($r_init));
513       return(_("Can not check if entry exists in old database. Please have a look a the gosa logfiles."));
514     }
515    
516     /* There are entries using this cn and/or phone number */
517     if($save && mysql_affected_rows($r_init)) {
518       $SQL = "DELETE FROM ".$cfg_Init['EXT_TABLE']." 
519       WHERE   (exten='".$this->old_tele_number."') 
520         OR    (exten='".$this->old_cn."')";
521   
522       /* Query and ensure that everything went fine */
523       $res =  @mysql_query($SQL,$r_init);
524       if(!$res){
525         gosa_log(@mysql_error($r_init));
526         return(_("Can not remove old entries from initial home server. Please have a look a the gosa logfiles."));
527       }
529     }//ENDE  old num availiable ...
530     @mysql_close($r_init);
531     return(false);
532   }
536   /* This function checks if the given phonenumbers are available or already in use*/
537   function is_number_used()
538   {
539     $ldap= $this->config->get_ldap_link();
540     $ldap->cd($this->config->current['BASE']);
541     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
542     while($attrs = $ldap->fetch()) {
543       unset($attrs['telephoneNumber']['count']);
544       foreach($attrs['telephoneNumber'] as $tele){
545         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
546         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
547         $numbers[$tele]=$attrs;
548       }
549     }
551     $num = $this->telephoneNumber;
552     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
553       if(isset($numbers[$num]['uid'][0])){
554         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
555       }else{
556         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
557       }
558     }
559   }
562   /* Save to LDAP */
563   function save()
564   {
566     if((!isset($this->attrs['goFonPIN']))){
567       $pin_use = false;
568     }else{
569       $pin_use = true;
570     }
572     plugin::save();
574     if(empty($this->old_tele_number)){
575       $this->old_tele_number= $this->telephoneNumber;
576     }
578     $this->SQL_add_me(true);
580     if(empty($this->goFonConferenceOption_P)){
581       if($pin_use){
582         $this->attrs['goFonPIN']=array();
583       }else{
584         unset($this->attrs['goFonPIN']);
585       }
586     }
587     $this->attrs['goFonConferenceOption']="";
588     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
589           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
590       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
591       unset($this->attrs[$attrs]);         
592     }
594     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
595     unset($this->attrs['goFonConferenceOptionLifetime']);
597     /* Write back to ldap */
599     unset($this->attrs['base']);
601     foreach($this->attributes as $atr){
602       if(chkacl($this->acl, $atr)!=""){
603         unset($this->attrs[$atr]);
604       }
605     }
607     $ldap= $this->config->get_ldap_link();
608     $ldap->cd($this->config->current['BASE']);
609     $ldap->cat($this->dn, array('dn'));
611     if ($ldap->count()){
612       $ldap->cd($this->dn);
613       $this->cleanup();
614       $ldap->modify ($this->attrs); 
615       $this->handle_post_events('modify');
616     } else {
617       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
618       $ldap->cd($this->dn);
619       $ldap->add($this->attrs);
620       $this->handle_post_events('add');
621     }
622     show_ldap_error($ldap->get_error(), _("Saving phone conference failed"));
624     /* Optionally execute a command after we're done */
625     $this->postcreate();
626   }
630 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
631 ?>