Code

Added checks and fixes
[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                         = "";
30   var $goFonConferenceOption    = "";
31   var $goFonConferenceOption_P  = "";   // Set PIN 
32   var $goFonConferenceOption_r  = "";   // record Conference
33   var $goFonConferenceOption_M  = "";   // Play Music opn hold
34   var $goFonConferenceOption_s  = "";   // activate menu
35   var $goFonConferenceOption_i  = "";   // announce new and leaving user
36   var $goFonConferenceOption_c  = "";   // Count User
37   var $goFonConferenceOption_D  = "";   // Conference Type, no PIN/PIN
39   var $goFonConferenceOptionFormat              = "";
40   var $goFonConferenceOptionLifetime    = "";
41   var $telephoneNumber          = "";
43   var $old_tele_number          = false;
44   var $generate_error           = "";
46   /* Headpage attributes */
47   var $last_dep_sorting= "invalid";
48   var $departments= array();
50   /* attribute list for save action */
51   var $attributes= array("cn", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
52                                                  "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c",
53                                                  "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber");
55   var $objectclasses= array("top", "goFonConference");
57   function conference ($config, $dn)
58   {
59     plugin::plugin($config, $dn);
60     $this->is_account   = TRUE;
61     $this->ui                   = get_userinfo();
62     $this->dn                   = $dn;
63     $this->orig_dn              = $dn;
64     $this->config               = $config;
66     /* Set base */
67     if ($this->dn == "new"){
68       $ui= get_userinfo();
69       if(isset($_SESSION['conferencefilter']['depselect'])){
70         $this->base = $_SESSION['conferencefilter']['depselect'];
71       }else{
72         $this->base= dn2base($ui->dn);
73       }
74     } else {
75       $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
76     }
79     /* Parse Options ... 
80      * Parameter|Lifetime|number
81      */
82     if($this->dn!="new"){
83       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
85       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
86         $varname = "goFonConferenceOption_".$tmp1[0][$i];
87         if($tmp1[0][$i]=="d"){
88           $this->goFonConferenceOption_D = $tmp1[0][$i];    
89         }else{
90           $this->$varname = $tmp1[0][$i];    
91         }
92       }
94       $this->goFonConferenceOptionLifetime  = $tmp1[1];
95       $this->old_tele_number                = $this->telephoneNumber;
96     }
97   }
99   function execute()
100   {
101         $this->config->departments    = get_departments($this->dn);
102         $this->config->make_idepartments();
103         $smarty= get_smarty();
105         $smarty->assign("goFonConferenceOptions",                   array("D"=>"Conference ","d"=>"Conference without PIN"));
106         $smarty->assign("goFonConferenceOptionFormats", array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
107         $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
108         
109         foreach ($this->attributes as $val){
110                 $smarty->assign("$val", $this->$val);
111                 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
112                 if(!$this->$val){
113                         $smarty->assign($val."CHK", "");
114                 }else{
115                         $smarty->assign($val."CHK", " checked ");
116                 }
117         }
118         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
119   }
121   function remove_from_parent()
122   {
123         $ldap= $this->config->get_ldap_link();
124         $ldap->cd ($this->dn);
125         $ldap->recursive_remove();
127         /* Optionally execute a command after we're done */
128         $this->handle_post_events('remove');
129   }
132   /* Save data to object */
133   function save_object()
134   {
135     plugin::save_object();
136     if(isset($_POST['cn'])){
137       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
138             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
139       if(isset($_POST[$attrs])){
140         $this->$attrs = $_POST[$attrs];
141       }else{
142         $this->$attrs = false;
143         }
144       }
145     }
146   }
149   /* Check values */
150   function check()
151   {
152         $message= array();
153  
154   if($this->is_number_used()){
155     $message[] =  $this->is_number_used();
156   }
158   if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
159     $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
160   }
162   if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
163     $message[]= _("Please enter a PIN.");
164   }
165  
166   if(empty($this->cn)){
167     $message[] =_("Please enter a name for the conference.");
168   }
169   
170   if(!is_numeric($this->telephoneNumber)){
171     $message[] =_("Only numeric chars are allowed in Number field.");
172   }
174   if(!is_numeric($this->goFonConferenceOptionLifetime)){
175     $message[] =_("Only numbers are allowed in Lifetime.");
176   }
177  
178   $this->SQL_remove_me(false);
179   $this->SQL_add_me(false);
180  
181   if(!empty($this->generate_error)){
182     $message[]=$this->generate_error;
183     $this->generate_error="";
184   }
186         return $message;
187   }
190   function SQL_add_me($save){
191     // Get Configuration for Mysql database Server
192     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
194     // Connect to DB server
195     $r_con =  @mysql_connect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
197     // Check if we are  connected correctly
198     if(!$r_con){
199       $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
200           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
201       gosa_log(mysql_error());
202       return false;
203     }
205     // Select database for Extensions
206     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
208     // Test if we have the database selected correctly
209     if(!$db){
210       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
211       gosa_log(mysql_error());
212       return false;
213     }
214    
215     if((!empty($this->telephoneNumber))&&($save==true)){
216       
217       $EXT=array();
219       $parameter  ="";
220       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
221             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
222         $parameter .= $this->$attrs;
223       }
225       $i=1;
226       $context="GOsa";
227       // Set Language to German
228       $EXT[$i]['exten']   =$this->telephoneNumber;
229       $EXT[$i]['context'] = $context;
230       $EXT[$i]['priority']= $i;
231       $EXT[$i]['app']     ="SetLanguage";
232       $EXT[$i]['appdata'] ="de";
233       $i++;      
235       if($this->goFonConferenceOption_r == "r"){
237       // Recordingformat for conference
238       $EXT[$i]['exten']   =$this->telephoneNumber;
239       $EXT[$i]['context'] =$context;
240       $EXT[$i]['priority']= $i;
241       $EXT[$i]['app']     ="Setvar";
242       $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
243       $i++;      
245       }
247       // Answer Call
248       $EXT[$i]['exten']   =$this->telephoneNumber;
249       $EXT[$i]['context'] =$context;
250       $EXT[$i]['priority']=$i;
251       $EXT[$i]['app']     ="answer";
252       $EXT[$i]['appdata'] ="";
253       $i++;      
255       // Start Conference 
256       $EXT[$i]['exten']   =$this->telephoneNumber;
257       $EXT[$i]['context'] =$context;
258       $EXT[$i]['priority']=$i;
259       $EXT[$i]['app']     ="MeetMe";
260       
261       if(empty($this->goFonPIN)) {
262         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
263       }else{
264         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
265       }
266       $i++; 
268       // Start Conference 
269       $EXT[$i]['exten']   =$this->cn;
270       $EXT[$i]['context'] =$context;
271       $EXT[$i]['priority']=1;
272       $EXT[$i]['app']     ="Goto";
273       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
274       $SQL=array();
275     
276       foreach($EXT as $keytop => $valtop){
277       $s_keys = "";
278       $s_values = ""; 
279         foreach($valtop as $key=>$val){
280           $s_keys   .="`".$key."`,";
281           $s_values .="'".$val."',";
282         }
283         $s_keys   =preg_replace("/\,$/","",$s_keys); 
284         $s_values =preg_replace("/\,$/","",$s_values); 
285         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
286       }
287       foreach($SQL as $sqlsyn){
288         mysql_query($sqlsyn);
289       }
290     } 
291     
294     return(true);
295   }
297   function SQL_remove_me($save){
299     if($this->old_tele_number){
300       // Get Configuration for Mysql database Server
301       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
303       // Connect to DB server
304       $r_con =  @mysql_connect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
306       // Check if we are  connected correctly
307       if(!$r_con){
308         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
309             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
310         gosa_log(mysql_error());
311         return false;
312       }
314       // Select database for Extensions
315       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
317       // Test if we have the database selected correctly
318       if(!$db){
319         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
320         gosa_log(mysql_error());
321         return false;
322       }
324       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE   (exten= '".$this->old_tele_number."') 
325                                                         OR  (exten='".$this->telephoneNumber."') 
326                                                         OR  (exten='".$this->cn."')";
327       
328       if($save){
329         @mysql_query($SQL);
330       } 
332     }//ENDE  old num availiable ...
333     return(true);
334   }
338   /* This function checks if the given phonenumbers are available or already in use*/
339   function is_number_used()
340   {
341     $ldap= $this->config->get_ldap_link();
342     $ldap->cd($this->config->current['BASE']);
343     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
344     while($attrs = $ldap->fetch()) {
345       unset($attrs['telephoneNumber']['count']);
346       foreach($attrs['telephoneNumber'] as $tele){
347         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
348         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
349         $numbers[$tele]=$attrs;
350       }
351     }
353     $num = $this->telephoneNumber;
354     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->cn))){
355       if(isset($numbers[$num]['uid'][0])){
356         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
357       }else{
358         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
359       }
360     }
361   }
367   /* Save to LDAP */
368   function save()
369   {
370         plugin::save();
372   $this->SQL_remove_me(true);
373   $this->SQL_add_me(true);
375   $this->attrs['goFonConferenceOption']="";
376   foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
377             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
378     $this->attrs['goFonConferenceOption'] .= $this->$attrs;
379     unset($this->attrs[$attrs]);         
380   }
382   $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
383   unset($this->attrs['goFonConferenceOptionLifetime']);
385   /* Write back to ldap */
386         $ldap= $this->config->get_ldap_link();
387         $ldap->cat($this->dn);
388         $a= $ldap->fetch();
389         $ldap->cd($this->dn);
390         if (count($a)){
391                 $ldap->modify($this->attrs);
392                 $this->handle_post_events('modify');
393         } else {
394                 $ldap->add($this->attrs);
395                 $this->handle_post_events('add');
396         }
397         show_ldap_error($ldap->get_error());
399         /* Optionally execute a command after we're done */
400         $this->postcreate();
401   }
405 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
406 ?>