Code

fe1cc27295160fc97de1bfdf3eb3474f00858e04
[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 $goFonConferenceOptionFormat              = "";
44   var $goFonConferenceOptionLifetime    = "";
45   var $telephoneNumber          = "";
47   var $old_tele_number          = false;
48   var $generate_error           = "";
50   var $old_dn;
52   /* Headpage attributes */
53   var $last_dep_sorting= "invalid";
54   var $departments= array();
56   /* attribute list for save action */
57   var $attributes= array("cn","base", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
58       "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c",
59       "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber","goFonConferenceOwner");
61   var $objectclasses= array("top", "goFonConference");
63   function conference ($config, $dn)
64   {
65     plugin::plugin($config, $dn);
66     $this->is_account   = TRUE;
67     $this->ui                   = get_userinfo();
68     $this->dn                   = $dn;
69     $this->orig_dn              = $dn;
70     $this->config               = $config;
72     /* Set base */
73     if ($this->dn == "new"){
74       $ui= get_userinfo();
75       if(isset($_SESSION['conferencefilter']['depselect'])){
76         $this->base = $_SESSION['conferencefilter']['depselect'];
77       }else{
78         $this->base= dn2base($ui->dn);
79       }
80     } else {
81       $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
82     }
84     $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
85     $this->acl= get_module_permission($acl, "goFonConference", $this->dn);
86     $this->goFonConferenceOwner=$this->ui->dn;
88     /* Parse Options ... 
89      * Parameter|Lifetime|number
90      */
91     if($this->dn!="new"){
92       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
94       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
95         $varname = "goFonConferenceOption_".$tmp1[0][$i];
96         if($tmp1[0][$i]=="d"){
97           $this->goFonConferenceOption_D = $tmp1[0][$i];    
98         }else{
99           $this->$varname = $tmp1[0][$i];    
100         }
101       }
103       $this->goFonConferenceOptionLifetime  = $tmp1[1];
104       $this->old_tele_number                = $this->telephoneNumber;
105     }
106     $this->old_dn = $this->dn;
107     $this->old_cn = $this->cn;
108   }
110   function execute()
111   {
112         /* Call parent execute */
113         plugin::execute();
114     //$acl= get_permissions ($this->dn, $this->ui->subtreeACL);
115     //$this->acl= get_module_permission($acl, "goFonConference", $this->dn);
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 =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
234     // Check if we are  connected correctly
235     if(!$r_con){
236       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
237           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
238       gosa_log(mysql_error());
239       return false;
240     }
242     // Select database for Extensions
243     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
245     // Test if we have the database selected correctly
246     if(!$db){
247       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
248       gosa_log(mysql_error());
249       return false;
250     }
252     if((!empty($this->telephoneNumber))&&($save==true)){
254       $EXT=array();
256       $parameter  ="";
257       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
258             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
259         $parameter .= $this->$attrs;
260       }
262       $i=1;
263       $context="GOsa";
264       // Set Language to German
265       $EXT[$i]['exten']   =$this->telephoneNumber;
266       $EXT[$i]['context'] = $context;
267       $EXT[$i]['priority']= $i;
268       $EXT[$i]['app']     ="SetLanguage";
269       $EXT[$i]['appdata'] ="de";
270       $i++;      
272       if($this->goFonConferenceOption_r == "r"){
274         // Recordingformat for conference
275         $EXT[$i]['exten']   =$this->telephoneNumber;
276         $EXT[$i]['context'] =$context;
277         $EXT[$i]['priority']= $i;
278         $EXT[$i]['app']     ="Setvar";
279         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
280         $i++;      
282       }
284       // Answer Call
285       $EXT[$i]['exten']   =$this->telephoneNumber;
286       $EXT[$i]['context'] =$context;
287       $EXT[$i]['priority']=$i;
288       $EXT[$i]['app']     ="answer";
289       $EXT[$i]['appdata'] ="";
290       $i++;      
292       // Start Conference 
293       $EXT[$i]['exten']   =$this->telephoneNumber;
294       $EXT[$i]['context'] =$context;
295       $EXT[$i]['priority']=$i;
296       $EXT[$i]['app']     ="MeetMe";
298       if(empty($this->goFonPIN)) {
299         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
300       }else{
301         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
302       }
303       $i++; 
305       // Start Conference 
306       $EXT[$i]['exten']   =$this->cn;
307       $EXT[$i]['context'] =$context;
308       $EXT[$i]['priority']=1;
309       $EXT[$i]['app']     ="Goto";
310       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
311       $SQL=array();
313       foreach($EXT as $keytop => $valtop){
314         $s_keys = "";
315         $s_values = ""; 
316         foreach($valtop as $key=>$val){
317           $s_keys   .="`".$key."`,";
318           $s_values .="'".$val."',";
319         }
320         $s_keys   =preg_replace("/\,$/","",$s_keys); 
321         $s_values =preg_replace("/\,$/","",$s_values); 
322         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
323       }
324       foreach($SQL as $sqlsyn){
325         mysql_query($sqlsyn);
326       }
327     } 
330     @mysql_close($r_con);
331     return(true);
332   }
334   function SQL_remove_me($save){
336     if($this->old_tele_number){
337       // Get Configuration for Mysql database Server
338       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
340       // Connect to DB server
341       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
343       // Check if we are  connected correctly
344       if(!$r_con){
345         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
346             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
347         gosa_log(mysql_error());
348         return false;
349       }
351       // Select database for Extensions
352       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
354       // Test if we have the database selected correctly
355       if(!$db){
356         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
357         gosa_log(mysql_error());
358         return false;
359       }
361       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
362       WHERE   (exten='".$this->old_tele_number."') 
363         OR    (exten='".$this->telephoneNumber."') 
364         OR    (exten='".$this->cn."')  
365         OR    (exten='".$this->old_cn."')";
367       if($save){
368         @mysql_query($SQL);
369       } 
371     }//ENDE  old num availiable ...
372     @mysql_close($r_con);
373     return(true);
374   }
378   /* This function checks if the given phonenumbers are available or already in use*/
379   function is_number_used()
380   {
381     $ldap= $this->config->get_ldap_link();
382     $ldap->cd($this->config->current['BASE']);
383     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
384     while($attrs = $ldap->fetch()) {
385       unset($attrs['telephoneNumber']['count']);
386       foreach($attrs['telephoneNumber'] as $tele){
387         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
388         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
389         $numbers[$tele]=$attrs;
390       }
391     }
393     $num = $this->telephoneNumber;
394     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
395       if(isset($numbers[$num]['uid'][0])){
396         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
397       }else{
398         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
399       }
400     }
401   }
407   /* Save to LDAP */
408   function save()
409   {
411     if((!isset($this->attrs['goFonPIN']))){
412       $pin_use = false;
413     }else{
414       $pin_use = true;
415     }
417     plugin::save();
419     if(empty($this->old_tele_number)){
420       $this->old_tele_number= $this->telephoneNumber;
421     }
423     $this->SQL_remove_me(true);
424     $this->SQL_add_me(true);
426     if(empty($this->goFonConferenceOption_P)){
427       if($pin_use){
428         $this->attrs['goFonPIN']=array();
429       }else{
430         unset($this->attrs['goFonPIN']);
431       }
432     }
433     $this->attrs['goFonConferenceOption']="";
434     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
435           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
436       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
437       unset($this->attrs[$attrs]);         
438     }
440     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
441     unset($this->attrs['goFonConferenceOptionLifetime']);
443     /* Write back to ldap */
445     unset($this->attrs['base']);
447     foreach($this->attributes as $atr){
448       if(chkacl($this->acl, $atr)!=""){
449         unset($this->attrs[$atr]);
450       }
451     }
453     $ldap= $this->config->get_ldap_link();
455     $ldap->cat($this->dn);
456     $a= $ldap->fetch();
457     $ldap->cd($this->dn);
458     if (count($a)){
459       $ldap->modify($this->attrs);
460       $this->handle_post_events('modify');
461     } else {
462       $ldap->add($this->attrs);
463       $this->handle_post_events('add');
464     }
465     show_ldap_error($ldap->get_error());
467     /* Optionally execute a command after we're done */
468     $this->postcreate();
469   }
473 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
474 ?>