Code

Fixed white pages if mysql extension is missing
[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 $acl = "";
30   var $old_cn         = "";
32   var $goFonConferenceOption    = "";
33   var $goFonConferenceOption_P  = "";   // Set PIN 
34   var $goFonConferenceOption_r  = "";   // record Conference
35   var $goFonConferenceOption_M  = "";   // Play Music opn hold
36   var $goFonConferenceOption_s  = "";   // activate menu
37   var $goFonConferenceOption_i  = "";   // announce new and leaving user
38   var $goFonConferenceOption_c  = "";   // Count User
39   var $goFonConferenceOption_D  = "";   // Conference Type, no PIN/PIN
41   var $goFonConferenceOwner    = "";
43   var $error_shown = false;
45   var $goFonConferenceOptionFormat              = "";
46   var $goFonConferenceOptionLifetime    = "";
47   var $telephoneNumber          = "";
49   var $old_tele_number          = false;
50   var $generate_error           = "";
52   var $old_dn;
54   /* Headpage attributes */
55   var $last_dep_sorting= "invalid";
56   var $departments= array();
58   /* attribute list for save action */
59   var $attributes= array("cn","base", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
60       "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c",
61       "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber","goFonConferenceOwner");
63   var $objectclasses= array("top", "goFonConference");
65   function conference ($config, $dn)
66   {
67     plugin::plugin($config, $dn);
68     $this->is_account   = TRUE;
69     $this->ui                   = get_userinfo();
70     $this->dn                   = $dn;
71     $this->orig_dn              = $dn;
72     $this->config               = $config;
74     /* Set base */
75     if ($this->dn == "new"){
76       $ui= get_userinfo();
77       if(isset($_SESSION['conferencefilter']['depselect'])){
78         $this->base = $_SESSION['conferencefilter']['depselect'];
79       }else{
80         $this->base= dn2base($ui->dn);
81       }
82     } else {
83       $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
84     }
86     $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
87     $this->acl= get_module_permission($acl, "goFonConference", $this->dn);
88     $this->goFonConferenceOwner=$this->ui->dn;
90     /* Parse Options ... 
91      * Parameter|Lifetime|number
92      */
93     if($this->dn!="new"){
94       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
96       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
97         $varname = "goFonConferenceOption_".$tmp1[0][$i];
98         if($tmp1[0][$i]=="d"){
99           $this->goFonConferenceOption_D = $tmp1[0][$i];    
100         }else{
101           $this->$varname = $tmp1[0][$i];    
102         }
103       }
105       $this->goFonConferenceOptionLifetime  = $tmp1[1];
106       $this->old_tele_number                = $this->telephoneNumber;
107     }
108     $this->old_dn = $this->dn;
109     $this->old_cn = $this->cn;
110   }
112   function execute()
113   {
114         /* Call parent execute */
115         plugin::execute();
117     //$acl= get_permissions ($this->dn, $this->ui->subtreeACL);
118     //$this->acl= get_module_permission($acl, "goFonConference", $this->dn);
120     $this->config->departments    = get_departments($this->dn);
121     $this->config->make_idepartments();
122     $smarty= get_smarty();
124     $smarty->assign("bases" ,array_flip($this->config->departments));
125     $smarty->assign("base"  ,$this->base);
127     $smarty->assign("goFonConferenceOptions",               array("D"=>"Conference ","d"=>"Conference without PIN"));
128     $smarty->assign("goFonConferenceOptionFormats",     array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
129     $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
131     foreach ($this->attributes as $val){
132       $smarty->assign("$val", $this->$val);
133       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
134       if(!$this->$val){
135         $smarty->assign($val."CHK", "");
136       }else{
137         $smarty->assign($val."CHK", " checked ");
138       }
139     }
141     if($_SESSION['js']==1){
142       if($this->goFonConferenceOption_P != "P"){
143         $smarty->assign("goFonPINACL"," disabled ");
144         $smarty->assign("goFonPIN","");
145       }
146       if($this->goFonConferenceOption_r != "r"){
147         $smarty->assign("goFonConferenceOptionFormatACL"," disabled ");
148       }
149     }
150     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
151   }
153   function remove_from_parent()
154   {
155      $this->SQL_remove_me(true); 
157     $ldap= $this->config->get_ldap_link();
158     $ldap->cd ($this->dn);
159     $ldap->recursive_remove();
161     /* Optionally execute a command after we're done */
162     $this->handle_post_events('remove');
163   }
166   /* Save data to object */
167   function save_object()
168   {
169     plugin::save_object();
170     if(isset($_POST['cn'])){
171       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
172             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonPIN") as $attrs){
173         if(isset($_POST[$attrs])){
174           $this->$attrs = $_POST[$attrs];
175         }else{
176           $this->$attrs = false;
177         }
178       }
179     }
180   }
183   /* Check values */
184   function check()
185   {
186     $message= array();
188     if($this->is_number_used()){
189       $message[] =  $this->is_number_used();
190     }
192     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
193       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
194     }
196     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
197       $message[]= _("Please enter a PIN.");
198     }
200     if(empty($this->cn)){
201       $message[] =_("Please enter a name for the conference.");
202     }
204     if(!is_numeric($this->telephoneNumber)){
205       $message[] =_("Only numeric chars are allowed in Number field.");
206     }
208     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
209       $message[] =_("Only numbers are allowed in Lifetime.");
210     }
212     $this->SQL_remove_me(false);
213     $this->SQL_add_me(false);
215     if(!empty($this->generate_error)){
216       $message[]=$this->generate_error;
217       $this->generate_error="";
218     }
220     return $message;
221   }
224   function SQL_add_me($save){
226     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
227       $this->generate_error = _("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.");
228       return(false);
229     }
230   
231     // Get Configuration for Mysql database Server
232     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
234     // Connect to DB server
235     $r_con = false;    
237     if(!is_callable("mysql_pconnect")){
238       if(!$this->error_shown){
239         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
240         $this->error_shown = true;
241       }
242       return(true);
243     }
244     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
246     // Check if we are  connected correctly
247     if(!$r_con){
248       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
249           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
250       gosa_log(mysql_error());
251       return false;
252     }
254     // Select database for Extensions
255     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
257     // Test if we have the database selected correctly
258     if(!$db){
259       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
260       gosa_log(mysql_error());
261       return false;
262     }
264     if((!empty($this->telephoneNumber))&&($save==true)){
266       $EXT=array();
268       $parameter  ="";
269       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
270             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
271         $parameter .= $this->$attrs;
272       }
274       $i=1;
275       $context="GOsa";
276       // Set Language to German
277       $EXT[$i]['exten']   =$this->telephoneNumber;
278       $EXT[$i]['context'] = $context;
279       $EXT[$i]['priority']= $i;
280       $EXT[$i]['app']     ="SetLanguage";
281       $EXT[$i]['appdata'] ="de";
282       $i++;      
284       if($this->goFonConferenceOption_r == "r"){
286         // Recordingformat for conference
287         $EXT[$i]['exten']   =$this->telephoneNumber;
288         $EXT[$i]['context'] =$context;
289         $EXT[$i]['priority']= $i;
290         $EXT[$i]['app']     ="Setvar";
291         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
292         $i++;      
294       }
296       // Answer Call
297       $EXT[$i]['exten']   =$this->telephoneNumber;
298       $EXT[$i]['context'] =$context;
299       $EXT[$i]['priority']=$i;
300       $EXT[$i]['app']     ="answer";
301       $EXT[$i]['appdata'] ="";
302       $i++;      
304       // Start Conference 
305       $EXT[$i]['exten']   =$this->telephoneNumber;
306       $EXT[$i]['context'] =$context;
307       $EXT[$i]['priority']=$i;
308       $EXT[$i]['app']     ="MeetMe";
310       if(empty($this->goFonPIN)) {
311         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
312       }else{
313         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
314       }
315       $i++; 
317       // Start Conference 
318       $EXT[$i]['exten']   =$this->cn;
319       $EXT[$i]['context'] =$context;
320       $EXT[$i]['priority']=1;
321       $EXT[$i]['app']     ="Goto";
322       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
323       $SQL=array();
325       foreach($EXT as $keytop => $valtop){
326         $s_keys = "";
327         $s_values = ""; 
328         foreach($valtop as $key=>$val){
329           $s_keys   .="`".$key."`,";
330           $s_values .="'".$val."',";
331         }
332         $s_keys   =preg_replace("/\,$/","",$s_keys); 
333         $s_values =preg_replace("/\,$/","",$s_values); 
334         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
335       }
336       foreach($SQL as $sqlsyn){
337         mysql_query($sqlsyn);
338       }
339     } 
342     @mysql_close($r_con);
343     return(true);
344   }
346   function SQL_remove_me($save){
347     if(!is_callable("mysql_pconnect")){
348       if(!$this->error_shown){
349         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
350         $this->error_shown = true;
351       }
352       return(true);
353     }
355     if($this->old_tele_number){
356       // Get Configuration for Mysql database Server
357       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
359       // Connect to DB server
360       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
362       // Check if we are  connected correctly
363       if(!$r_con){
364         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
365             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
366         gosa_log(mysql_error());
367         return false;
368       }
370       // Select database for Extensions
371       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
373       // Test if we have the database selected correctly
374       if(!$db){
375         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
376         gosa_log(mysql_error());
377         return false;
378       }
380       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
381       WHERE   (exten='".$this->old_tele_number."') 
382         OR    (exten='".$this->telephoneNumber."') 
383         OR    (exten='".$this->cn."')  
384         OR    (exten='".$this->old_cn."')";
386       if($save){
387         @mysql_query($SQL);
388       } 
390     }//ENDE  old num availiable ...
391     @mysql_close($r_con);
392     return(true);
393   }
397   /* This function checks if the given phonenumbers are available or already in use*/
398   function is_number_used()
399   {
400     $ldap= $this->config->get_ldap_link();
401     $ldap->cd($this->config->current['BASE']);
402     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
403     while($attrs = $ldap->fetch()) {
404       unset($attrs['telephoneNumber']['count']);
405       foreach($attrs['telephoneNumber'] as $tele){
406         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
407         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
408         $numbers[$tele]=$attrs;
409       }
410     }
412     $num = $this->telephoneNumber;
413     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
414       if(isset($numbers[$num]['uid'][0])){
415         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
416       }else{
417         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
418       }
419     }
420   }
426   /* Save to LDAP */
427   function save()
428   {
430     if((!isset($this->attrs['goFonPIN']))){
431       $pin_use = false;
432     }else{
433       $pin_use = true;
434     }
436     plugin::save();
438     if(empty($this->old_tele_number)){
439       $this->old_tele_number= $this->telephoneNumber;
440     }
442     $this->SQL_remove_me(true);
443     $this->SQL_add_me(true);
445     if(empty($this->goFonConferenceOption_P)){
446       if($pin_use){
447         $this->attrs['goFonPIN']=array();
448       }else{
449         unset($this->attrs['goFonPIN']);
450       }
451     }
452     $this->attrs['goFonConferenceOption']="";
453     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
454           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
455       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
456       unset($this->attrs[$attrs]);         
457     }
459     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
460     unset($this->attrs['goFonConferenceOptionLifetime']);
462     /* Write back to ldap */
464     unset($this->attrs['base']);
466     foreach($this->attributes as $atr){
467       if(chkacl($this->acl, $atr)!=""){
468         unset($this->attrs[$atr]);
469       }
470     }
472     $ldap= $this->config->get_ldap_link();
474     $ldap->cat($this->dn);
475     $a= $ldap->fetch();
476     $ldap->cd($this->dn);
477     if (count($a)){
478       $ldap->modify($this->attrs);
479       $this->handle_post_events('modify');
480     } else {
481       $ldap->add($this->attrs);
482       $this->handle_post_events('add');
483     }
484     show_ldap_error($ldap->get_error());
486     /* Optionally execute a command after we're done */
487     $this->postcreate();
488   }
492 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
493 ?>