Code

Added support for general check hooks
[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" ,$this->config->idepartments);
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     /* Call common method to give check the hook */
184     $message= plugin::check();
186     if($this->is_number_used()){
187       $message[] =  $this->is_number_used();
188     }
190     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
191       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
192     }
194     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
195       $message[]= _("Please enter a PIN.");
196     }
198     if(empty($this->cn)){
199       $message[] =_("Please enter a name for the conference.");
200     }
202     if(!is_numeric($this->telephoneNumber)){
203       $message[] =_("Only numeric chars are allowed in Number field.");
204     }
206     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
207       $message[] =_("Only numbers are allowed in Lifetime.");
208     }
210     $this->SQL_remove_me(false);
211     $this->SQL_add_me(false);
213     if(!empty($this->generate_error)){
214       $message[]=$this->generate_error;
215       $this->generate_error="";
216     }
218     return $message;
219   }
222   function SQL_add_me($save){
224     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
225       $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.");
226       return(false);
227     }
228   
229     // Get Configuration for Mysql database Server
230     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
232     // Connect to DB server
233     $r_con = false;    
235     if(!is_callable("mysql_pconnect")){
236       if(!$this->error_shown){
237         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
238         $this->error_shown = true;
239       }
240       return(true);
241     }
242     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
244     // Check if we are  connected correctly
245     if(!$r_con){
246       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
247           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
248       gosa_log(mysql_error());
249       return false;
250     }
252     // Select database for Extensions
253     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
255     // Test if we have the database selected correctly
256     if(!$db){
257       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
258       gosa_log(mysql_error());
259       return false;
260     }
262     if((!empty($this->telephoneNumber))&&($save==true)){
264       $EXT=array();
266       $parameter  ="";
267       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
268             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
269         $parameter .= $this->$attrs;
270       }
272       $i=1;
273       $context="GOsa";
274       // Set Language to German
275       $EXT[$i]['exten']   =$this->telephoneNumber;
276       $EXT[$i]['context'] = $context;
277       $EXT[$i]['priority']= $i;
278       $EXT[$i]['app']     ="SetLanguage";
279       $EXT[$i]['appdata'] ="de";
280       $i++;      
282       if($this->goFonConferenceOption_r == "r"){
284         // Recordingformat for conference
285         $EXT[$i]['exten']   =$this->telephoneNumber;
286         $EXT[$i]['context'] =$context;
287         $EXT[$i]['priority']= $i;
288         $EXT[$i]['app']     ="Setvar";
289         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
290         $i++;      
292       }
294       // Answer Call
295       $EXT[$i]['exten']   =$this->telephoneNumber;
296       $EXT[$i]['context'] =$context;
297       $EXT[$i]['priority']=$i;
298       $EXT[$i]['app']     ="answer";
299       $EXT[$i]['appdata'] ="";
300       $i++;      
302       // Start Conference 
303       $EXT[$i]['exten']   =$this->telephoneNumber;
304       $EXT[$i]['context'] =$context;
305       $EXT[$i]['priority']=$i;
306       $EXT[$i]['app']     ="MeetMe";
308       if(empty($this->goFonPIN)) {
309         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
310       }else{
311         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
312       }
313       $i++; 
315       // Start Conference 
316       $EXT[$i]['exten']   =$this->cn;
317       $EXT[$i]['context'] =$context;
318       $EXT[$i]['priority']=1;
319       $EXT[$i]['app']     ="Goto";
320       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
321       $SQL=array();
323       foreach($EXT as $keytop => $valtop){
324         $s_keys = "";
325         $s_values = ""; 
326         foreach($valtop as $key=>$val){
327           $s_keys   .="`".$key."`,";
328           $s_values .="'".$val."',";
329         }
330         $s_keys   =preg_replace("/\,$/","",$s_keys); 
331         $s_values =preg_replace("/\,$/","",$s_values); 
332         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
333       }
334       foreach($SQL as $sqlsyn){
335         mysql_query($sqlsyn);
336       }
337     } 
340     @mysql_close($r_con);
341     return(true);
342   }
344   function SQL_remove_me($save){
345     if(!is_callable("mysql_pconnect")){
346       if(!$this->error_shown){
347         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
348         $this->error_shown = true;
349       }
350       return(true);
351     }
353     if($this->old_tele_number){
354       // Get Configuration for Mysql database Server
355       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
357       // Connect to DB server
358       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
360       // Check if we are  connected correctly
361       if(!$r_con){
362         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
363             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
364         gosa_log(mysql_error());
365         return false;
366       }
368       // Select database for Extensions
369       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
371       // Test if we have the database selected correctly
372       if(!$db){
373         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
374         gosa_log(mysql_error());
375         return false;
376       }
378       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
379       WHERE   (exten='".$this->old_tele_number."') 
380         OR    (exten='".$this->telephoneNumber."') 
381         OR    (exten='".$this->cn."')  
382         OR    (exten='".$this->old_cn."')";
384       if($save){
385         @mysql_query($SQL);
386       } 
388     }//ENDE  old num availiable ...
389     @mysql_close($r_con);
390     return(true);
391   }
395   /* This function checks if the given phonenumbers are available or already in use*/
396   function is_number_used()
397   {
398     $ldap= $this->config->get_ldap_link();
399     $ldap->cd($this->config->current['BASE']);
400     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
401     while($attrs = $ldap->fetch()) {
402       unset($attrs['telephoneNumber']['count']);
403       foreach($attrs['telephoneNumber'] as $tele){
404         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
405         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
406         $numbers[$tele]=$attrs;
407       }
408     }
410     $num = $this->telephoneNumber;
411     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
412       if(isset($numbers[$num]['uid'][0])){
413         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
414       }else{
415         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
416       }
417     }
418   }
424   /* Save to LDAP */
425   function save()
426   {
428     if((!isset($this->attrs['goFonPIN']))){
429       $pin_use = false;
430     }else{
431       $pin_use = true;
432     }
434     plugin::save();
436     if(empty($this->old_tele_number)){
437       $this->old_tele_number= $this->telephoneNumber;
438     }
440     $this->SQL_remove_me(true);
441     $this->SQL_add_me(true);
443     if(empty($this->goFonConferenceOption_P)){
444       if($pin_use){
445         $this->attrs['goFonPIN']=array();
446       }else{
447         unset($this->attrs['goFonPIN']);
448       }
449     }
450     $this->attrs['goFonConferenceOption']="";
451     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
452           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
453       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
454       unset($this->attrs[$attrs]);         
455     }
457     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
458     unset($this->attrs['goFonConferenceOptionLifetime']);
460     /* Write back to ldap */
462     unset($this->attrs['base']);
464     foreach($this->attributes as $atr){
465       if(chkacl($this->acl, $atr)!=""){
466         unset($this->attrs[$atr]);
467       }
468     }
470     $ldap= $this->config->get_ldap_link();
472     $ldap->cat($this->dn);
473     $a= $ldap->fetch();
474     $ldap->cd($this->dn);
475     if (count($a)){
476       $this->cleanup();
477 $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 ?>