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);
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 /* Optionally execute a command after we're done */
183 $this->handle_post_events('remove');
184 }
187 /* Save data to object */
188 function save_object()
189 {
190 plugin::save_object();
191 if(isset($_POST['cn'])){
192 foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
193 "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonPIN") as $attrs){
194 if(isset($_POST[$attrs])){
195 $this->$attrs = $_POST[$attrs];
196 }else{
197 $this->$attrs = false;
198 }
199 }
200 }
201 }
204 /* Check values */
205 function check()
206 {
207 /* Call common method to give check the hook */
208 $message= plugin::check();
210 if($this->is_number_used()){
211 $message[] = $this->is_number_used();
212 }
214 if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
215 $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
216 }
218 if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
219 $message[]= _("Please enter a PIN.");
220 }
222 if(empty($this->cn)){
223 $message[] =_("Please enter a name for the conference.");
224 }
226 if(!is_numeric($this->telephoneNumber)){
227 $message[] =_("Only numeric chars are allowed in Number field.");
228 }
230 if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
231 $message[] =_("Only numbers are allowed in Lifetime.");
232 }
234 $this->SQL_remove_me(false);
235 $this->SQL_add_me(false);
237 if(!empty($this->generate_error)){
238 $message[]=$this->generate_error;
239 $this->generate_error="";
240 }
242 return $message;
243 }
246 function SQL_add_me($save){
248 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
249 $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.");
250 return(false);
251 }
253 // Get Configuration for Mysql database Server
254 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
256 // Connect to DB server
257 $r_con = false;
259 if(!is_callable("mysql_pconnect")){
260 if(!$this->error_shown){
261 print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
262 $this->error_shown = true;
263 }
264 return(true);
265 }
266 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
268 // Check if we are connected correctly
269 if(!$r_con){
270 $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
271 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
272 gosa_log(mysql_error());
273 return false;
274 }
276 // Select database for Extensions
277 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
279 // Test if we have the database selected correctly
280 if(!$db){
281 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
282 gosa_log(mysql_error());
283 return false;
284 }
286 if((!empty($this->telephoneNumber))&&($save==true)){
288 $EXT=array();
290 $parameter ="";
291 foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
292 "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
293 $parameter .= $this->$attrs;
294 }
296 $i=1;
297 $context="GOsa";
298 // Set Language to German
299 $EXT[$i]['exten'] =$this->telephoneNumber;
300 $EXT[$i]['context'] = $context;
301 $EXT[$i]['priority']= $i;
302 $EXT[$i]['app'] ="SetLanguage";
303 $EXT[$i]['appdata'] ="de";
304 $i++;
306 if($this->goFonConferenceOption_r == "r"){
308 // Recordingformat for conference
309 $EXT[$i]['exten'] =$this->telephoneNumber;
310 $EXT[$i]['context'] =$context;
311 $EXT[$i]['priority']= $i;
312 $EXT[$i]['app'] ="Setvar";
313 $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
314 $i++;
316 }
318 // Answer Call
319 $EXT[$i]['exten'] =$this->telephoneNumber;
320 $EXT[$i]['context'] =$context;
321 $EXT[$i]['priority']=$i;
322 $EXT[$i]['app'] ="answer";
323 $EXT[$i]['appdata'] ="";
324 $i++;
326 // Start Conference
327 $EXT[$i]['exten'] =$this->telephoneNumber;
328 $EXT[$i]['context'] =$context;
329 $EXT[$i]['priority']=$i;
330 $EXT[$i]['app'] ="MeetMe";
332 if(empty($this->goFonPIN)) {
333 $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
334 }else{
335 $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
336 }
337 $i++;
339 // Start Conference
340 $EXT[$i]['exten'] =$this->cn;
341 $EXT[$i]['context'] =$context;
342 $EXT[$i]['priority']=1;
343 $EXT[$i]['app'] ="Goto";
344 $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
345 $SQL=array();
347 foreach($EXT as $keytop => $valtop){
348 $s_keys = "";
349 $s_values = "";
350 foreach($valtop as $key=>$val){
351 $s_keys .="`".$key."`,";
352 $s_values .="'".$val."',";
353 }
354 $s_keys =preg_replace("/\,$/","",$s_keys);
355 $s_values =preg_replace("/\,$/","",$s_values);
356 $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");";
357 }
358 foreach($SQL as $sqlsyn){
359 mysql_query($sqlsyn);
360 }
361 }
364 @mysql_close($r_con);
365 return(true);
366 }
368 function SQL_remove_me($save){
369 if(!is_callable("mysql_pconnect")){
370 if(!$this->error_shown){
371 print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
372 $this->error_shown = true;
373 }
374 return(true);
375 }
377 if($this->old_tele_number){
378 // Get Configuration for Mysql database Server
379 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
381 // Connect to DB server
382 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
384 // Check if we are connected correctly
385 if(!$r_con){
386 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
387 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
388 gosa_log(mysql_error());
389 return false;
390 }
392 // Select database for Extensions
393 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
395 // Test if we have the database selected correctly
396 if(!$db){
397 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
398 gosa_log(mysql_error());
399 return false;
400 }
402 $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']."
403 WHERE (exten='".$this->old_tele_number."')
404 OR (exten='".$this->telephoneNumber."')
405 OR (exten='".$this->cn."')
406 OR (exten='".$this->old_cn."')";
408 if($save){
409 @mysql_query($SQL);
410 }
412 }//ENDE old num availiable ...
413 @mysql_close($r_con);
414 return(true);
415 }
419 /* This function checks if the given phonenumbers are available or already in use*/
420 function is_number_used()
421 {
422 $ldap= $this->config->get_ldap_link();
423 $ldap->cd($this->config->current['BASE']);
424 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
425 while($attrs = $ldap->fetch()) {
426 unset($attrs['telephoneNumber']['count']);
427 foreach($attrs['telephoneNumber'] as $tele){
428 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
429 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
430 $numbers[$tele]=$attrs;
431 }
432 }
434 $num = $this->telephoneNumber;
435 if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
436 if(isset($numbers[$num]['uid'][0])){
437 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
438 }else{
439 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
440 }
441 }
442 }
448 /* Save to LDAP */
449 function save()
450 {
452 if((!isset($this->attrs['goFonPIN']))){
453 $pin_use = false;
454 }else{
455 $pin_use = true;
456 }
458 plugin::save();
460 if(empty($this->old_tele_number)){
461 $this->old_tele_number= $this->telephoneNumber;
462 }
464 $this->SQL_remove_me(true);
465 $this->SQL_add_me(true);
467 if(empty($this->goFonConferenceOption_P)){
468 if($pin_use){
469 $this->attrs['goFonPIN']=array();
470 }else{
471 unset($this->attrs['goFonPIN']);
472 }
473 }
474 $this->attrs['goFonConferenceOption']="";
475 foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
476 "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
477 $this->attrs['goFonConferenceOption'] .= $this->$attrs;
478 unset($this->attrs[$attrs]);
479 }
481 $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
482 unset($this->attrs['goFonConferenceOptionLifetime']);
484 /* Write back to ldap */
486 unset($this->attrs['base']);
488 foreach($this->attributes as $atr){
489 if(chkacl($this->acl, $atr)!=""){
490 unset($this->attrs[$atr]);
491 }
492 }
494 $ldap= $this->config->get_ldap_link();
495 $ldap->cd($this->config->current['BASE']);
496 $ldap->cat($this->dn, array('dn'));
498 if ($ldap->count()){
499 $ldap->cd($this->dn);
500 $this->cleanup();
501 $ldap->modify ($this->attrs);
502 $this->handle_post_events('modify');
503 } else {
504 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
505 $ldap->cd($this->dn);
506 $ldap->add($this->attrs);
507 $this->handle_post_events('add');
508 }
509 show_ldap_error($ldap->get_error(), _("Saving phone conference failed"));
511 /* Optionally execute a command after we're done */
512 $this->postcreate();
513 }
515 }
517 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
518 ?>