Code

fixed conference acls
[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    = "";
42   var $error_shown = false;
44   var $goFonConferenceOptionFormat              = "";
45   var $goFonConferenceOptionLifetime    = "";
46   var $telephoneNumber          = "";
48   var $old_tele_number          = false;
49   var $generate_error           = "";
51   var $old_dn;
53   /* Headpage attributes */
54   var $last_dep_sorting= "invalid";
55   var $departments= array();
57   /* attribute list for save action */
58   var $attributes= array("cn","base", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
59       "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c",
60       "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber","goFonConferenceOwner");
62   var $objectclasses= array("top", "goFonConference");
64   function conference ($config, $dn)
65   {
66     plugin::plugin($config, $dn);
67     $this->is_account   = TRUE;
68     $this->ui                   = get_userinfo();
69     $this->dn                   = $dn;
70     $this->orig_dn              = $dn;
71     $this->config               = $config;
73     /* Set base */
74     if ($this->dn == "new"){
75       $ui= get_userinfo();
76       if(isset($_SESSION['conferencefilter']['depselect'])){
77         $this->base = $_SESSION['conferencefilter']['depselect'];
78       }else{
79         $this->base= dn2base($ui->dn);
80       }
81     } else {
82       $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
83     }
85     $ui= get_userinfo();
86     $acl= get_permissions ($ui->dn, $this->ui->subtreeACL);
87     $this->acl= get_module_permission($acl, "conference", $ui->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     $this->config->departments    = get_departments($this->dn);
118     $this->config->make_idepartments();
119     $smarty= get_smarty();
121     $smarty->assign("bases" ,array_flip($this->config->departments));
122     $smarty->assign("base"  ,$this->base);
124     $smarty->assign("goFonConferenceOptions",               array("D"=>"Conference ","d"=>"Conference without PIN"));
125     $smarty->assign("goFonConferenceOptionFormats",     array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
126     $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
128     foreach ($this->attributes as $val){
129       $smarty->assign("$val", $this->$val);
130       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
131       if(!$this->$val){
132         $smarty->assign($val."CHK", "");
133       }else{
134         $smarty->assign($val."CHK", " checked ");
135       }
136     }
138     if($_SESSION['js']==1){
139       if($this->goFonConferenceOption_P != "P"){
140         $smarty->assign("goFonPINACL"," disabled ");
141         $smarty->assign("goFonPIN","");
142       }
143       if($this->goFonConferenceOption_r != "r"){
144         $smarty->assign("goFonConferenceOptionFormatACL"," disabled ");
145       }
146     }
147     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
148   }
150   function remove_from_parent()
151   {
152     $this->SQL_remove_me(true); 
154     $ldap= $this->config->get_ldap_link();
155     $ldap->cd ($this->dn);
156     $ldap->recursive_remove();
158     /* Optionally execute a command after we're done */
159     $this->handle_post_events('remove');
160   }
163   /* Save data to object */
164   function save_object()
165   {
166     plugin::save_object();
167     if(isset($_POST['cn'])){
168       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
169             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonPIN") as $attrs){
170         if(isset($_POST[$attrs])){
171           $this->$attrs = $_POST[$attrs];
172         }else{
173           $this->$attrs = false;
174         }
175       }
176     }
177   }
180   /* Check values */
181   function check()
182   {
183     $message= array();
185     if($this->is_number_used()){
186       $message[] =  $this->is_number_used();
187     }
189     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
190       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
191     }
193     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
194       $message[]= _("Please enter a PIN.");
195     }
197     if(empty($this->cn)){
198       $message[] =_("Please enter a name for the conference.");
199     }
201     if(!is_numeric($this->telephoneNumber)){
202       $message[] =_("Only numeric chars are allowed in Number field.");
203     }
205     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
206       $message[] =_("Only numbers are allowed in Lifetime.");
207     }
209     $this->SQL_remove_me(false);
210     $this->SQL_add_me(false);
212     if(!empty($this->generate_error)){
213       $message[]=$this->generate_error;
214       $this->generate_error="";
215     }
217     return $message;
218   }
221   function SQL_add_me($save){
223     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
224       $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.");
225       return(false);
226     }
227   
228     // Get Configuration for Mysql database Server
229     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
231     // Connect to DB server
232     $r_con = false;    
234     if(!is_callable("mysql_pconnect")){
235       if(!$this->error_shown){
236         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
237         $this->error_shown = true;
238       }
239       return(true);
240     }
241     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
243     // Check if we are  connected correctly
244     if(!$r_con){
245       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
246           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
247       gosa_log(mysql_error());
248       return false;
249     }
251     // Select database for Extensions
252     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
254     // Test if we have the database selected correctly
255     if(!$db){
256       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
257       gosa_log(mysql_error());
258       return false;
259     }
261     if((!empty($this->telephoneNumber))&&($save==true)){
263       $EXT=array();
265       $parameter  ="";
266       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
267             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
268         $parameter .= $this->$attrs;
269       }
271       $i=1;
272       $context="GOsa";
273       // Set Language to German
274       $EXT[$i]['exten']   =$this->telephoneNumber;
275       $EXT[$i]['context'] = $context;
276       $EXT[$i]['priority']= $i;
277       $EXT[$i]['app']     ="SetLanguage";
278       $EXT[$i]['appdata'] ="de";
279       $i++;      
281       if($this->goFonConferenceOption_r == "r"){
283         // Recordingformat for conference
284         $EXT[$i]['exten']   =$this->telephoneNumber;
285         $EXT[$i]['context'] =$context;
286         $EXT[$i]['priority']= $i;
287         $EXT[$i]['app']     ="Setvar";
288         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
289         $i++;      
291       }
293       // Answer Call
294       $EXT[$i]['exten']   =$this->telephoneNumber;
295       $EXT[$i]['context'] =$context;
296       $EXT[$i]['priority']=$i;
297       $EXT[$i]['app']     ="answer";
298       $EXT[$i]['appdata'] ="";
299       $i++;      
301       // Start Conference 
302       $EXT[$i]['exten']   =$this->telephoneNumber;
303       $EXT[$i]['context'] =$context;
304       $EXT[$i]['priority']=$i;
305       $EXT[$i]['app']     ="MeetMe";
307       if(empty($this->goFonPIN)) {
308         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
309       }else{
310         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
311       }
312       $i++; 
314       // Start Conference 
315       $EXT[$i]['exten']   =$this->cn;
316       $EXT[$i]['context'] =$context;
317       $EXT[$i]['priority']=1;
318       $EXT[$i]['app']     ="Goto";
319       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
320       $SQL=array();
322       foreach($EXT as $keytop => $valtop){
323         $s_keys = "";
324         $s_values = ""; 
325         foreach($valtop as $key=>$val){
326           $s_keys   .="`".$key."`,";
327           $s_values .="'".$val."',";
328         }
329         $s_keys   =preg_replace("/\,$/","",$s_keys); 
330         $s_values =preg_replace("/\,$/","",$s_values); 
331         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
332       }
333       foreach($SQL as $sqlsyn){
334         mysql_query($sqlsyn);
335       }
336     } 
339     @mysql_close($r_con);
340     return(true);
341   }
343   function SQL_remove_me($save){
344     if(!is_callable("mysql_pconnect")){
345       if(!$this->error_shown){
346         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
347         $this->error_shown = true;
348       }
349       return(true);
350     }
352     if($this->old_tele_number){
353       // Get Configuration for Mysql database Server
354       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
356       // Connect to DB server
357       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
359       // Check if we are  connected correctly
360       if(!$r_con){
361         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
362             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
363         gosa_log(mysql_error());
364         return false;
365       }
367       // Select database for Extensions
368       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
370       // Test if we have the database selected correctly
371       if(!$db){
372         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
373         gosa_log(mysql_error());
374         return false;
375       }
377       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
378       WHERE   (exten='".$this->old_tele_number."') 
379         OR    (exten='".$this->telephoneNumber."') 
380         OR    (exten='".$this->cn."')  
381         OR    (exten='".$this->old_cn."')";
383       if($save){
384         @mysql_query($SQL);
385       } 
387     }//ENDE  old num availiable ...
388     @mysql_close($r_con);
389     return(true);
390   }
394   /* This function checks if the given phonenumbers are available or already in use*/
395   function is_number_used()
396   {
397     $ldap= $this->config->get_ldap_link();
398     $ldap->cd($this->config->current['BASE']);
399     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
400     while($attrs = $ldap->fetch()) {
401       unset($attrs['telephoneNumber']['count']);
402       foreach($attrs['telephoneNumber'] as $tele){
403         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
404         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
405         $numbers[$tele]=$attrs;
406       }
407     }
409     $num = $this->telephoneNumber;
410     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
411       if(isset($numbers[$num]['uid'][0])){
412         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
413       }else{
414         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
415       }
416     }
417   }
423   /* Save to LDAP */
424   function save()
425   {
427     if((!isset($this->attrs['goFonPIN']))){
428       $pin_use = false;
429     }else{
430       $pin_use = true;
431     }
433     plugin::save();
435     if(empty($this->old_tele_number)){
436       $this->old_tele_number= $this->telephoneNumber;
437     }
439     $this->SQL_remove_me(true);
440     $this->SQL_add_me(true);
442     if(empty($this->goFonConferenceOption_P)){
443       if($pin_use){
444         $this->attrs['goFonPIN']=array();
445       }else{
446         unset($this->attrs['goFonPIN']);
447       }
448     }
449     $this->attrs['goFonConferenceOption']="";
450     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
451           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
452       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
453       unset($this->attrs[$attrs]);         
454     }
456     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
457     unset($this->attrs['goFonConferenceOptionLifetime']);
459     /* Write back to ldap */
461     unset($this->attrs['base']);
463     foreach($this->attributes as $atr){
464       if(chkacl($this->acl, $atr)!=""){
465         unset($this->attrs[$atr]);
466       }
467     }
469     $ldap= $this->config->get_ldap_link();
471     $ldap->cat($this->dn);
472     $a= $ldap->fetch();
473     $ldap->cd($this->dn);
474     if (count($a)){
475       $ldap->modify($this->attrs);
476       $this->handle_post_events('modify');
477     } else {
478       $ldap->add($this->attrs);
479       $this->handle_post_events('add');
480     }
481     show_ldap_error($ldap->get_error());
483     /* Optionally execute a command after we're done */
484     $this->postcreate();
485   }
489 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
490 ?>