Code

Several gofon acl updated.
[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, $plugin= NULL)
67   {
68     plugin::plugin($config, $dn, $plugin);
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 ("/^[^,]+,ou=conferences,ou=asterisk,ou=configs,ou=systems,/", "", $this->dn);
85     }
87     $this->goFonConferenceOwner=$this->ui->dn;
89     /* Parse Options ... 
90      * Parameter|Lifetime|number
91      */
92     if($this->dn!="new"){
93       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
95       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
96         $varname = "goFonConferenceOption_".$tmp1[0][$i];
97         if($tmp1[0][$i]=="d"){
98           $this->goFonConferenceOption_D = $tmp1[0][$i];    
99         }else{
100           $this->$varname = $tmp1[0][$i];    
101         }
102       }
104       $this->goFonConferenceOptionLifetime  = $tmp1[1];
105       $this->old_tele_number                = $this->telephoneNumber;
106     }
107     $this->old_dn = $this->dn;
108     $this->old_cn = $this->cn;
109   }
111   function execute()
112   {
113     /* Call parent execute */
114     plugin::execute();
116     $smarty= get_smarty();
118     /* Assign base ACL */
119     $baseACL = $this->getacl("base");
120     if(!$this->acl_is_moveable()) {
121       $baseACL = preg_replace("/w/","",$baseACL);
122     }
123     $smarty->assign("baseACL",          $baseACL);
125     $tmp = $this->plInfo();
126     foreach($tmp['plProvidedAcls'] as $name => $translation){
127       $smarty->assign($name."ACL",$this->getacl($name));
128     }
130     if($this->acl_is_writeable("base")){
131       $smarty->assign("baseSelect",true);
132     }else{
133       $smarty->assign("baseSelect",false);
134     }
136     $smarty->assign("bases" ,$this->get_allowed_bases());
137     $smarty->assign("base_select"  ,$this->base);
139     $once = true; 
140     foreach($_POST as $name => $value){
141       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
142         $once = false;
143         $this->dialog = new baseSelectDialog($this->config,$this);
144         $this->dialog->setCurrentBase($this->base);
145       }
146     }
148     /* Dialog handling */
149     if(is_object($this->dialog)){
150       /* Must be called before save_object */
151       $this->dialog->save_object();
153       if($this->dialog->isClosed()){
154         $this->dialog = false;
155       }elseif($this->dialog->isSelected()){
156         $this->base = $this->dialog->isSelected();
157         $this->dialog= false;
158       }else{
159         return($this->dialog->execute());
160       }
161     }
163     $smarty->assign("goFonConferenceOptions",               array("D"=>"Conference ","d"=>"Conference without PIN"));
164     $smarty->assign("goFonConferenceOptionFormats",     array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
165     $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
167     foreach ($this->attributes as $val){
168       $smarty->assign("$val", $this->$val);
169       if(!$this->$val){
170         $smarty->assign($val."CHK", "");
171       }else{
172         $smarty->assign($val."CHK", " checked ");
173       }
174     }
176     if($_SESSION['js']==1){
177       if($this->goFonConferenceOption_P != "P"){
178         $smarty->assign("goFonPINACL", $this->getacl("goFonPIN",TRUE));
179         $smarty->assign("goFonPIN","");
180       }
181       if($this->goFonConferenceOption_r != "r"){
182         $smarty->assign("goFonConferenceOptionFormatACL", $this->getacl("goFonConferenceOptionr",TRUE));
183       }
184     }
185     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
186   }
188   function remove_from_parent()
189   {
190     $this->SQL_remove_me(true); 
192     $ldap= $this->config->get_ldap_link();
193     $ldap->cd ($this->dn);
194     $ldap->recursive_remove();
196     show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonConference/generic with dn '%s' failed."),$this->dn));
197     /* Optionally execute a command after we're done */
198     $this->handle_post_events('remove');
199   }
202   /* Save data to object */
203   function save_object()
204   {
205     if(isset($_POST['phoneConferenceGeneric'])){
207       plugin::save_object();
209       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
210             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
212         /* Acl can't contain _ so we remove it here. */
213         $acl_name = preg_replace("/_/","",$attrs);
215         /* Attribute writeable ? */
216         if($this->acl_is_writeable($acl_name)){
217           if(isset($_POST[$attrs])){
218             $this->$attrs = $_POST[$attrs];
219           }else{
220             $this->$attrs = false;
221           }
222         }
223       }
224     }
225   }
228   /* Check values */
229   function check()
230   {
231     /* Call common method to give check the hook */
232     $message= plugin::check();
234     if($this->is_number_used()){
235       $message[] =  $this->is_number_used();
236     }
238     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
239       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
240     }
242     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
243       $message[]= _("Please enter a PIN.");
244     }
246     if(empty($this->cn)){
247       $message[] =_("Please enter a name for the conference.");
248     }
250     if(!is_numeric($this->telephoneNumber)){
251       $message[] =_("Only numeric chars are allowed in Number field.");
252     }
254     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
255       $message[] =_("Only numbers are allowed in Lifetime.");
256     }
258     $this->SQL_remove_me(false);
259     $this->SQL_add_me(false);
261     if(!empty($this->generate_error)){
262       $message[]=$this->generate_error;
263       $this->generate_error="";
264     }
266     return $message;
267   }
270   function SQL_add_me($save){
272     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
273       $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.");
274       return(false);
275     }
276   
277     // Get Configuration for Mysql database Server
278     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
280     // Connect to DB server
281     $r_con = false;    
283     if(!is_callable("mysql_pconnect")){
284       if(!$this->error_shown){
285         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
286         $this->error_shown = true;
287       }
288       return(true);
289     }
290     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
292     // Check if we are  connected correctly
293     if(!$r_con){
294       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
295           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
296       gosa_log(mysql_error());
297       return false;
298     }
300     // Select database for Extensions
301     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
303     // Test if we have the database selected correctly
304     if(!$db){
305       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
306       gosa_log(mysql_error());
307       return false;
308     }
310     if((!empty($this->telephoneNumber))&&($save==true)){
312       $EXT=array();
314       $parameter  ="";
315       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
316             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
317         $parameter .= $this->$attrs;
318       }
320       $i=1;
321       $context="GOsa";
322       // Set Language to German
323       $EXT[$i]['exten']   =$this->telephoneNumber;
324       $EXT[$i]['context'] = $context;
325       $EXT[$i]['priority']= $i;
326       $EXT[$i]['app']     ="SetLanguage";
327       $EXT[$i]['appdata'] ="de";
328       $i++;      
330       if($this->goFonConferenceOption_r == "r"){
332         // Recordingformat for conference
333         $EXT[$i]['exten']   =$this->telephoneNumber;
334         $EXT[$i]['context'] =$context;
335         $EXT[$i]['priority']= $i;
336         $EXT[$i]['app']     ="Setvar";
337         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
338         $i++;      
340       }
342       // Answer Call
343       $EXT[$i]['exten']   =$this->telephoneNumber;
344       $EXT[$i]['context'] =$context;
345       $EXT[$i]['priority']=$i;
346       $EXT[$i]['app']     ="answer";
347       $EXT[$i]['appdata'] ="";
348       $i++;      
350       // Start Conference 
351       $EXT[$i]['exten']   =$this->telephoneNumber;
352       $EXT[$i]['context'] =$context;
353       $EXT[$i]['priority']=$i;
354       $EXT[$i]['app']     ="MeetMe";
356       if(empty($this->goFonPIN)) {
357         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
358       }else{
359         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
360       }
361       $i++; 
363       // Start Conference 
364       $EXT[$i]['exten']   =$this->cn;
365       $EXT[$i]['context'] =$context;
366       $EXT[$i]['priority']=1;
367       $EXT[$i]['app']     ="Goto";
368       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
369       $SQL=array();
371       foreach($EXT as $keytop => $valtop){
372         $s_keys = "";
373         $s_values = ""; 
374         foreach($valtop as $key=>$val){
375           $s_keys   .="`".$key."`,";
376           $s_values .="'".$val."',";
377         }
378         $s_keys   =preg_replace("/\,$/","",$s_keys); 
379         $s_values =preg_replace("/\,$/","",$s_values); 
380         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
381       }
382       foreach($SQL as $sqlsyn){
383         mysql_query($sqlsyn);
384       }
385     } 
388     @mysql_close($r_con);
389     return(true);
390   }
392   function SQL_remove_me($save){
393     if(!is_callable("mysql_pconnect")){
394       if(!$this->error_shown){
395         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
396         $this->error_shown = true;
397       }
398       return(true);
399     }
401     if($this->old_tele_number){
402       // Get Configuration for Mysql database Server
403       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
405       // Connect to DB server
406       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
408       // Check if we are  connected correctly
409       if(!$r_con){
410         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
411             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
412         gosa_log(mysql_error());
413         return false;
414       }
416       // Select database for Extensions
417       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
419       // Test if we have the database selected correctly
420       if(!$db){
421         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
422         gosa_log(mysql_error());
423         return false;
424       }
426       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
427       WHERE   (exten='".$this->old_tele_number."') 
428         OR    (exten='".$this->telephoneNumber."') 
429         OR    (exten='".$this->cn."')  
430         OR    (exten='".$this->old_cn."')";
432       if($save){
433         @mysql_query($SQL);
434       } 
436     }//ENDE  old num availiable ...
437     @mysql_close($r_con);
438     return(true);
439   }
443   /* This function checks if the given phonenumbers are available or already in use*/
444   function is_number_used()
445   {
446     $ldap= $this->config->get_ldap_link();
447     $ldap->cd($this->config->current['BASE']);
448     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
449     while($attrs = $ldap->fetch()) {
450       unset($attrs['telephoneNumber']['count']);
451       foreach($attrs['telephoneNumber'] as $tele){
452         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
453         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
454         $numbers[$tele]=$attrs;
455       }
456     }
458     $num = $this->telephoneNumber;
459     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
460       if(isset($numbers[$num]['uid'][0])){
461         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
462       }else{
463         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
464       }
465     }
466   }
472   /* Save to LDAP */
473   function save()
474   {
476     if((!isset($this->attrs['goFonPIN']))){
477       $pin_use = false;
478     }else{
479       $pin_use = true;
480     }
482     plugin::save();
484     if(empty($this->old_tele_number)){
485       $this->old_tele_number= $this->telephoneNumber;
486     }
488     $this->SQL_remove_me(true);
489     $this->SQL_add_me(true);
491     if(empty($this->goFonConferenceOption_P)){
492       if($pin_use){
493         $this->attrs['goFonPIN']=array();
494       }else{
495         unset($this->attrs['goFonPIN']);
496       }
497     }
498     $this->attrs['goFonConferenceOption']="";
499     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
500           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
501       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
502       unset($this->attrs[$attrs]);         
503     }
505     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
506     unset($this->attrs['goFonConferenceOptionLifetime']);
508     /* Write back to ldap */
510     unset($this->attrs['base']);
512     $ldap= $this->config->get_ldap_link();
513     $ldap->cd($this->config->current['BASE']);
514     $ldap->cat($this->dn, array('dn'));
516     if ($ldap->count()){
517       $ldap->cd($this->dn);
518       $this->cleanup();
519       $ldap->modify ($this->attrs); 
520       $this->handle_post_events('modify');
521     } else {
522       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
523       $ldap->cd($this->dn);
524       $ldap->add($this->attrs);
525       $this->handle_post_events('add');
526     }
527     show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonConference/generic with dn '%s' failed."),$this->dn));
529     /* Optionally execute a command after we're done */
530     $this->postcreate();
531   }
534   /* Return plugin informations for acl handling */
535   function plInfo()
536   {
537     return (array(
538           "plShortName"   => _("Conference"),
539           "plDescription" => _("Phone conference management"),
540           "plSelfModify"  => TRUE,
541           "plDepends"     => array(),
542           "plPriority"    => 0,
543           "plSection"     => array("addons" => _("Addons")),
544           "plCategory"    => array("gofonconference" => array("description" => _("GOfon conference"),
545                                                   "objectClass" => "gofonConference")),
547           "plProvidedAcls" => array(
548             "cn"                            => _("Name"),
549             "base"                          => _("Base"), 
550             "description"                   => _("Description"), 
551             "goFonPIN"                      => _("Conference PIN"),
553             "goFonConferenceOptionP"       => _("Preset PIN"),
554             "goFonConferenceOptionr"       => _("Record conference"),
555             "goFonConferenceOptionM"       => _("Play music on hold"),
556             "goFonConferenceOptions"       => _("Activate menu"),
557             "goFonConferenceOptioni"       => _("Announce user activity"),
558             "goFonConferenceOptionc"       => _("Count user"),
559             "goFonConferenceOptionD"       => _("Conference type"),
561             "goFonConferenceOptionFormat"   => _("Format"),
562             "goFonConferenceOptionLifetime" => _("Lifetime"),
563             "telephoneNumber"               => _("Telephone number"),
564             "goFonConferenceOwner"          => _("Owner"))
565             ));
566   }
569 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
570 ?>