Code

5b4716402cf0021e60091f0a83f99abe78697db8
[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   var $dialog ;
59   /* attribute list for save action */
60   var $attributes= array("cn","base", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
61       "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c",
62       "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber","goFonConferenceOwner");
64   var $objectclasses= array("top", "goFonConference");
66   function conference ($config, $dn)
67   {
68     plugin::plugin($config, $dn);
69     $this->is_account   = TRUE;
70     $this->ui                   = get_userinfo();
71     $this->dn                   = $dn;
72     $this->orig_dn              = $dn;
73     $this->config               = $config;
75     /* Set base */
76     if ($this->dn == "new"){
77       $ui= get_userinfo();
78       if(isset($_SESSION['CurrentMainBase'])){
79         $this->base = $_SESSION['CurrentMainBase'];
80       }else{
81         $this->base= dn2base($ui->dn);
82       }
83     } else {
84       $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
85     }
87     $ui= get_userinfo();
88     $acl= get_permissions ($ui->dn, $this->ui->subtreeACL);
89     $this->acl= get_module_permission($acl, "conference", $ui->dn);
90     $this->goFonConferenceOwner=$this->ui->dn;
92     /* Parse Options ... 
93      * Parameter|Lifetime|number
94      */
95     if($this->dn!="new"){
96       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
98       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
99         $varname = "goFonConferenceOption_".$tmp1[0][$i];
100         if($tmp1[0][$i]=="d"){
101           $this->goFonConferenceOption_D = $tmp1[0][$i];    
102         }else{
103           $this->$varname = $tmp1[0][$i];    
104         }
105       }
107       $this->goFonConferenceOptionLifetime  = $tmp1[1];
108       $this->old_tele_number                = $this->telephoneNumber;
109     }
110     $this->old_dn = $this->dn;
111     $this->old_cn = $this->cn;
112   }
114   function execute()
115   {
116     /* Call parent execute */
117     plugin::execute();
119     $smarty= get_smarty();
121     $smarty->assign("bases" ,$this->config->idepartments);
122     $smarty->assign("base"  ,$this->base);
124     $once = true; 
125     foreach($_POST as $name => $value){
126       if(preg_match("/^chooseBase/",$name) && $once){
127         $once = false;
128         $this->dialog = new baseSelectDialog($this->config,$this);
129         $this->dialog->setCurrentBase($this->base);
130       }
131     }
133     /* Dialog handling */
134     if(is_object($this->dialog)){
135       /* Must be called before save_object */
136       $this->dialog->save_object();
138       if($this->dialog->isClosed()){
139         $this->dialog = false;
140       }elseif($this->dialog->isSelected()){
141         $this->base = $this->dialog->isSelected();
142         $this->dialog= false;
143       }else{
144         return($this->dialog->execute());
145       }
146     }
148     $smarty->assign("goFonConferenceOptions",               array("D"=>"Conference ","d"=>"Conference without PIN"));
149     $smarty->assign("goFonConferenceOptionFormats",     array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
150     $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
152     foreach ($this->attributes as $val){
153       $smarty->assign("$val", $this->$val);
154       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
155       if(!$this->$val){
156         $smarty->assign($val."CHK", "");
157       }else{
158         $smarty->assign($val."CHK", " checked ");
159       }
160     }
162     if($_SESSION['js']==1){
163       if($this->goFonConferenceOption_P != "P"){
164         $smarty->assign("goFonPINACL"," disabled ");
165         $smarty->assign("goFonPIN","");
166       }
167       if($this->goFonConferenceOption_r != "r"){
168         $smarty->assign("goFonConferenceOptionFormatACL"," disabled ");
169       }
170     }
171     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
172   }
174   function remove_from_parent()
175   {
176     $this->SQL_remove_me(true); 
178     $ldap= $this->config->get_ldap_link();
179     $ldap->cd ($this->dn);
180     $ldap->recursive_remove();
182     show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonConference/generic with dn '%s' failed."),$this->dn));
183     /* Optionally execute a command after we're done */
184     $this->handle_post_events('remove');
185   }
188   /* Save data to object */
189   function save_object()
190   {
191     plugin::save_object();
192     if(isset($_POST['cn'])){
193       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
194             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonPIN") as $attrs){
195         if(isset($_POST[$attrs])){
196           $this->$attrs = $_POST[$attrs];
197         }else{
198           $this->$attrs = false;
199         }
200       }
201     }
202   }
205   /* Check values */
206   function check()
207   {
208     /* Call common method to give check the hook */
209     $message= plugin::check();
211     if($this->is_number_used()){
212       $message[] =  $this->is_number_used();
213     }
215     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
216       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
217     }
219     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
220       $message[]= _("Please enter a PIN.");
221     }
223     if(empty($this->cn)){
224       $message[] =_("Please enter a name for the conference.");
225     }
227     if(!is_numeric($this->telephoneNumber)){
228       $message[] =_("Only numeric chars are allowed in Number field.");
229     }
231     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
232       $message[] =_("Only numbers are allowed in Lifetime.");
233     }
235     $this->SQL_remove_me(false);
236     $this->SQL_add_me(false);
238     if(!empty($this->generate_error)){
239       $message[]=$this->generate_error;
240       $this->generate_error="";
241     }
243     return $message;
244   }
247   function SQL_add_me($save){
249     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
250       $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.");
251       return(false);
252     }
253   
254     // Get Configuration for Mysql database Server
255     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
257     // Connect to DB server
258     $r_con = false;    
260     if(!is_callable("mysql_pconnect")){
261       if(!$this->error_shown){
262         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
263         $this->error_shown = true;
264       }
265       return(true);
266     }
267     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
269     // Check if we are  connected correctly
270     if(!$r_con){
271       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
272           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
273       gosa_log(mysql_error());
274       return false;
275     }
277     // Select database for Extensions
278     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
280     // Test if we have the database selected correctly
281     if(!$db){
282       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
283       gosa_log(mysql_error());
284       return false;
285     }
287     if((!empty($this->telephoneNumber))&&($save==true)){
289       $EXT=array();
291       $parameter  ="";
292       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
293             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
294         $parameter .= $this->$attrs;
295       }
297       $i=1;
298       $context="GOsa";
299       // Set Language to German
300       $EXT[$i]['exten']   =$this->telephoneNumber;
301       $EXT[$i]['context'] = $context;
302       $EXT[$i]['priority']= $i;
303       $EXT[$i]['app']     ="SetLanguage";
304       $EXT[$i]['appdata'] ="de";
305       $i++;      
307       if($this->goFonConferenceOption_r == "r"){
309         // Recordingformat for conference
310         $EXT[$i]['exten']   =$this->telephoneNumber;
311         $EXT[$i]['context'] =$context;
312         $EXT[$i]['priority']= $i;
313         $EXT[$i]['app']     ="Setvar";
314         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
315         $i++;      
317       }
319       // Answer Call
320       $EXT[$i]['exten']   =$this->telephoneNumber;
321       $EXT[$i]['context'] =$context;
322       $EXT[$i]['priority']=$i;
323       $EXT[$i]['app']     ="answer";
324       $EXT[$i]['appdata'] ="";
325       $i++;      
327       // Start Conference 
328       $EXT[$i]['exten']   =$this->telephoneNumber;
329       $EXT[$i]['context'] =$context;
330       $EXT[$i]['priority']=$i;
331       $EXT[$i]['app']     ="MeetMe";
333       if(empty($this->goFonPIN)) {
334         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
335       }else{
336         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
337       }
338       $i++; 
340       // Start Conference 
341       $EXT[$i]['exten']   =$this->cn;
342       $EXT[$i]['context'] =$context;
343       $EXT[$i]['priority']=1;
344       $EXT[$i]['app']     ="Goto";
345       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
346       $SQL=array();
348       foreach($EXT as $keytop => $valtop){
349         $s_keys = "";
350         $s_values = ""; 
351         foreach($valtop as $key=>$val){
352           $s_keys   .="`".$key."`,";
353           $s_values .="'".$val."',";
354         }
355         $s_keys   =preg_replace("/\,$/","",$s_keys); 
356         $s_values =preg_replace("/\,$/","",$s_values); 
357         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
358       }
359       foreach($SQL as $sqlsyn){
360         mysql_query($sqlsyn);
361       }
362     } 
365     @mysql_close($r_con);
366     return(true);
367   }
369   function SQL_remove_me($save){
370     if(!is_callable("mysql_pconnect")){
371       if(!$this->error_shown){
372         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
373         $this->error_shown = true;
374       }
375       return(true);
376     }
378     if($this->old_tele_number){
379       // Get Configuration for Mysql database Server
380       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
382       // Connect to DB server
383       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
385       // Check if we are  connected correctly
386       if(!$r_con){
387         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
388             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
389         gosa_log(mysql_error());
390         return false;
391       }
393       // Select database for Extensions
394       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
396       // Test if we have the database selected correctly
397       if(!$db){
398         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
399         gosa_log(mysql_error());
400         return false;
401       }
403       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
404       WHERE   (exten='".$this->old_tele_number."') 
405         OR    (exten='".$this->telephoneNumber."') 
406         OR    (exten='".$this->cn."')  
407         OR    (exten='".$this->old_cn."')";
409       if($save){
410         @mysql_query($SQL);
411       } 
413     }//ENDE  old num availiable ...
414     @mysql_close($r_con);
415     return(true);
416   }
420   /* This function checks if the given phonenumbers are available or already in use*/
421   function is_number_used()
422   {
423     $ldap= $this->config->get_ldap_link();
424     $ldap->cd($this->config->current['BASE']);
425     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
426     while($attrs = $ldap->fetch()) {
427       unset($attrs['telephoneNumber']['count']);
428       foreach($attrs['telephoneNumber'] as $tele){
429         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
430         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
431         $numbers[$tele]=$attrs;
432       }
433     }
435     $num = $this->telephoneNumber;
436     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
437       if(isset($numbers[$num]['uid'][0])){
438         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
439       }else{
440         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
441       }
442     }
443   }
449   /* Save to LDAP */
450   function save()
451   {
453     if((!isset($this->attrs['goFonPIN']))){
454       $pin_use = false;
455     }else{
456       $pin_use = true;
457     }
459     plugin::save();
461     if(empty($this->old_tele_number)){
462       $this->old_tele_number= $this->telephoneNumber;
463     }
465     $this->SQL_remove_me(true);
466     $this->SQL_add_me(true);
468     if(empty($this->goFonConferenceOption_P)){
469       if($pin_use){
470         $this->attrs['goFonPIN']=array();
471       }else{
472         unset($this->attrs['goFonPIN']);
473       }
474     }
475     $this->attrs['goFonConferenceOption']="";
476     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
477           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
478       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
479       unset($this->attrs[$attrs]);         
480     }
482     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
483     unset($this->attrs['goFonConferenceOptionLifetime']);
485     /* Write back to ldap */
487     unset($this->attrs['base']);
489     foreach($this->attributes as $atr){
490       if(chkacl($this->acl, $atr)!=""){
491         unset($this->attrs[$atr]);
492       }
493     }
495     $ldap= $this->config->get_ldap_link();
496     $ldap->cd($this->config->current['BASE']);
497     $ldap->cat($this->dn, array('dn'));
499     if ($ldap->count()){
500       $ldap->cd($this->dn);
501       $this->cleanup();
502       $ldap->modify ($this->attrs); 
503       $this->handle_post_events('modify');
504     } else {
505       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
506       $ldap->cd($this->dn);
507       $ldap->add($this->attrs);
508       $this->handle_post_events('add');
509     }
510     show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonConference/generic with dn '%s' failed."),$this->dn));
512     /* Optionally execute a command after we're done */
513     $this->postcreate();
514   }
517   /* Return plugin informations for acl handling */
518   function plInfo()
519   {
520     return (array(
521           "plShortName"   => _("Phone conferences"),
522           "plDescription" => _("Phone conference management"),
523           "plSelfModify"  => TRUE,
524           "plDepends"     => array(),
525           "plPriority"    => 0,
526           "plSection"     => array("addons" => _("Addons")),
527           "plCategory"    => array("gofon" => array("description" => _("GOfon"),
528                                                   "objectClass" => "gofonConference")),
530           "plProvidedAcls" => array(
531             "base"                          => _("Base"), 
532             "description"                   => _("Description"), 
533             "goFonPIN"                      => _("Conference PIN"),
535             "goFonConferenceOption_P"       => _("Preset PIN"),
536             "goFonConferenceOption_r"       => _("Record conference"),
537             "goFonConferenceOption_M"       => _("Play music on hold"),
538             "goFonConferenceOption_s"       => _("Activate menu"),
539             "goFonConferenceOption_i"       => _("Announce user activity"),
540             "goFonConferenceOption_c"       => _("Count user"),
541             "goFonConferenceOption_D"       => _("Conference type"),
543             "goFonConferenceOptionFormat"   => _("Format"),
544             "goFonConferenceOptionLifetime" => _("Lifetime"),
545             "telephoneNumber"               => _("Telephone number"),
546             "goFonConferenceOwner"          => _("Owner"))
547             ));
548   }
551 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
552 ?>