Code

some updates for acls
[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){
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     plugin::save_object();
206     if(isset($_POST['cn'])){
207       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
208             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonPIN") as $attrs){
209         if(isset($_POST[$attrs])){
210           $this->$attrs = $_POST[$attrs];
211         }else{
212           $this->$attrs = false;
213         }
214       }
215     }
216   }
219   /* Check values */
220   function check()
221   {
222     /* Call common method to give check the hook */
223     $message= plugin::check();
225     if($this->is_number_used()){
226       $message[] =  $this->is_number_used();
227     }
229     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
230       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
231     }
233     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
234       $message[]= _("Please enter a PIN.");
235     }
237     if(empty($this->cn)){
238       $message[] =_("Please enter a name for the conference.");
239     }
241     if(!is_numeric($this->telephoneNumber)){
242       $message[] =_("Only numeric chars are allowed in Number field.");
243     }
245     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
246       $message[] =_("Only numbers are allowed in Lifetime.");
247     }
249     $this->SQL_remove_me(false);
250     $this->SQL_add_me(false);
252     if(!empty($this->generate_error)){
253       $message[]=$this->generate_error;
254       $this->generate_error="";
255     }
257     return $message;
258   }
261   function SQL_add_me($save){
263     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
264       $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.");
265       return(false);
266     }
267   
268     // Get Configuration for Mysql database Server
269     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
271     // Connect to DB server
272     $r_con = false;    
274     if(!is_callable("mysql_pconnect")){
275       if(!$this->error_shown){
276         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
277         $this->error_shown = true;
278       }
279       return(true);
280     }
281     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
283     // Check if we are  connected correctly
284     if(!$r_con){
285       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
286           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
287       gosa_log(mysql_error());
288       return false;
289     }
291     // Select database for Extensions
292     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
294     // Test if we have the database selected correctly
295     if(!$db){
296       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
297       gosa_log(mysql_error());
298       return false;
299     }
301     if((!empty($this->telephoneNumber))&&($save==true)){
303       $EXT=array();
305       $parameter  ="";
306       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
307             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
308         $parameter .= $this->$attrs;
309       }
311       $i=1;
312       $context="GOsa";
313       // Set Language to German
314       $EXT[$i]['exten']   =$this->telephoneNumber;
315       $EXT[$i]['context'] = $context;
316       $EXT[$i]['priority']= $i;
317       $EXT[$i]['app']     ="SetLanguage";
318       $EXT[$i]['appdata'] ="de";
319       $i++;      
321       if($this->goFonConferenceOption_r == "r"){
323         // Recordingformat for conference
324         $EXT[$i]['exten']   =$this->telephoneNumber;
325         $EXT[$i]['context'] =$context;
326         $EXT[$i]['priority']= $i;
327         $EXT[$i]['app']     ="Setvar";
328         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
329         $i++;      
331       }
333       // Answer Call
334       $EXT[$i]['exten']   =$this->telephoneNumber;
335       $EXT[$i]['context'] =$context;
336       $EXT[$i]['priority']=$i;
337       $EXT[$i]['app']     ="answer";
338       $EXT[$i]['appdata'] ="";
339       $i++;      
341       // Start Conference 
342       $EXT[$i]['exten']   =$this->telephoneNumber;
343       $EXT[$i]['context'] =$context;
344       $EXT[$i]['priority']=$i;
345       $EXT[$i]['app']     ="MeetMe";
347       if(empty($this->goFonPIN)) {
348         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
349       }else{
350         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
351       }
352       $i++; 
354       // Start Conference 
355       $EXT[$i]['exten']   =$this->cn;
356       $EXT[$i]['context'] =$context;
357       $EXT[$i]['priority']=1;
358       $EXT[$i]['app']     ="Goto";
359       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
360       $SQL=array();
362       foreach($EXT as $keytop => $valtop){
363         $s_keys = "";
364         $s_values = ""; 
365         foreach($valtop as $key=>$val){
366           $s_keys   .="`".$key."`,";
367           $s_values .="'".$val."',";
368         }
369         $s_keys   =preg_replace("/\,$/","",$s_keys); 
370         $s_values =preg_replace("/\,$/","",$s_values); 
371         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
372       }
373       foreach($SQL as $sqlsyn){
374         mysql_query($sqlsyn);
375       }
376     } 
379     @mysql_close($r_con);
380     return(true);
381   }
383   function SQL_remove_me($save){
384     if(!is_callable("mysql_pconnect")){
385       if(!$this->error_shown){
386         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
387         $this->error_shown = true;
388       }
389       return(true);
390     }
392     if($this->old_tele_number){
393       // Get Configuration for Mysql database Server
394       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
396       // Connect to DB server
397       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
399       // Check if we are  connected correctly
400       if(!$r_con){
401         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
402             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
403         gosa_log(mysql_error());
404         return false;
405       }
407       // Select database for Extensions
408       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
410       // Test if we have the database selected correctly
411       if(!$db){
412         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
413         gosa_log(mysql_error());
414         return false;
415       }
417       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
418       WHERE   (exten='".$this->old_tele_number."') 
419         OR    (exten='".$this->telephoneNumber."') 
420         OR    (exten='".$this->cn."')  
421         OR    (exten='".$this->old_cn."')";
423       if($save){
424         @mysql_query($SQL);
425       } 
427     }//ENDE  old num availiable ...
428     @mysql_close($r_con);
429     return(true);
430   }
434   /* This function checks if the given phonenumbers are available or already in use*/
435   function is_number_used()
436   {
437     $ldap= $this->config->get_ldap_link();
438     $ldap->cd($this->config->current['BASE']);
439     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
440     while($attrs = $ldap->fetch()) {
441       unset($attrs['telephoneNumber']['count']);
442       foreach($attrs['telephoneNumber'] as $tele){
443         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
444         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
445         $numbers[$tele]=$attrs;
446       }
447     }
449     $num = $this->telephoneNumber;
450     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
451       if(isset($numbers[$num]['uid'][0])){
452         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
453       }else{
454         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
455       }
456     }
457   }
463   /* Save to LDAP */
464   function save()
465   {
467     if((!isset($this->attrs['goFonPIN']))){
468       $pin_use = false;
469     }else{
470       $pin_use = true;
471     }
473     plugin::save();
475     if(empty($this->old_tele_number)){
476       $this->old_tele_number= $this->telephoneNumber;
477     }
479     $this->SQL_remove_me(true);
480     $this->SQL_add_me(true);
482     if(empty($this->goFonConferenceOption_P)){
483       if($pin_use){
484         $this->attrs['goFonPIN']=array();
485       }else{
486         unset($this->attrs['goFonPIN']);
487       }
488     }
489     $this->attrs['goFonConferenceOption']="";
490     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
491           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
492       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
493       unset($this->attrs[$attrs]);         
494     }
496     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
497     unset($this->attrs['goFonConferenceOptionLifetime']);
499     /* Write back to ldap */
501     unset($this->attrs['base']);
503     $ldap= $this->config->get_ldap_link();
504     $ldap->cd($this->config->current['BASE']);
505     $ldap->cat($this->dn, array('dn'));
507     if ($ldap->count()){
508       $ldap->cd($this->dn);
509       $this->cleanup();
510       $ldap->modify ($this->attrs); 
511       $this->handle_post_events('modify');
512     } else {
513       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
514       $ldap->cd($this->dn);
515       $ldap->add($this->attrs);
516       $this->handle_post_events('add');
517     }
518     show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonConference/generic with dn '%s' failed."),$this->dn));
520     /* Optionally execute a command after we're done */
521     $this->postcreate();
522   }
525   /* Return plugin informations for acl handling */
526   function plInfo()
527   {
528     return (array(
529           "plShortName"   => _("Conference"),
530           "plDescription" => _("Phone conference management"),
531           "plSelfModify"  => TRUE,
532           "plDepends"     => array(),
533           "plPriority"    => 0,
534           "plSection"     => array("addons" => _("Addons")),
535           "plCategory"    => array("gofon" => array("description" => _("GOfon"),
536                                                   "objectClass" => "gofonConference")),
538           "plProvidedAcls" => array(
539             "cn"                            => _("Name"),
540             "base"                          => _("Base"), 
541             "description"                   => _("Description"), 
542             "goFonPIN"                      => _("Conference PIN"),
544             "goFonConferenceOptionP"       => _("Preset PIN"),
545             "goFonConferenceOptionr"       => _("Record conference"),
546             "goFonConferenceOptionM"       => _("Play music on hold"),
547             "goFonConferenceOptions"       => _("Activate menu"),
548             "goFonConferenceOptioni"       => _("Announce user activity"),
549             "goFonConferenceOptionc"       => _("Count user"),
550             "goFonConferenceOptionD"       => _("Conference type"),
552             "goFonConferenceOptionFormat"   => _("Format"),
553             "goFonConferenceOptionLifetime" => _("Lifetime"),
554             "telephoneNumber"               => _("Telephone number"),
555             "goFonConferenceOwner"          => _("Owner"))
556             ));
557   }
560 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
561 ?>