47a221067c92a1d7018b05286ad8aade4513a821
1 <?php
3 class phoneAccount extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "Phone";
7 var $plDescription= "This does something";
8 var $has_mailAccount= FALSE;
10 /* Attributes */
11 var $telephoneNumber = "";
12 var $goFonHardware = "";
13 var $goFonFormat = "";
14 var $goFonPIN = "";
15 var $goFonVoicemailPIN = "";
16 var $goFonDeliveryMode = "";
17 var $phoneNumbers = array();
18 var $mail = "";
19 var $hardware_list = array();
20 var $used_hardware = array();
21 var $goFonMacro = "";
22 var $macro = 0; // Selected Macroi
23 var $lastmacro = "";
24 var $macros = array(); // List of macros for smarty select box
25 var $macroarray = array(); // All needed macro informations
26 var $macrostillavailable = false;
27 var $generate_error = "";
28 var $a_old_telenums = array();
29 var $goFonPINVoice = "";
31 /* CLI vars */
32 var $cli_summary = "Manage users phone account";
33 var $cli_description = "Some longer text\nfor help";
34 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
36 /* attribute list for save action */
37 var $attributes = array("goFonDeliveryMode", "goFonFormat","uid","cn","mail",
38 "goFonHardware","goFonPIN","goFonVoicemailPIN","telephoneNumber", "goFonMacro","macro");
39 var $objectclasses= array("goFonAccount");
41 function phoneAccount ($config, $dn= NULL)
42 {
43 plugin::plugin ($config, $dn);
45 /* Set phone hardware */
46 if (!isset($this->attrs['goFonHardware'])){
47 $this->goFonHardware= "automatic";
48 }
50 /* Preset voice format */
51 if (!isset($this->attrs['goFonFormat'])){
52 $this->goFonFormat= "wav";
53 }
55 /* Assemble phone numbers */
56 if (isset($this->attrs['telephoneNumber'])){
57 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
58 $number= $this->attrs['telephoneNumber'][$i];
59 $this->phoneNumbers[$number]= $number;
60 }
61 }
63 /* Set up has_mailAccount */
64 if (isset($this->attrs['objectClass'])){
65 if (in_array("gosaMailAccount", $this->attrs['objectClass'])){
66 $this->has_mailAccount= TRUE;
67 }
68 }
70 $a_SETUP= array();
71 if(array_key_exists('config',$_SESSION) &&
72 array_key_exists('SERVERS',$_SESSION['config']->data) &&
73 array_key_exists('FON',$_SESSION['config']->data['SERVERS']) &&
74 is_callable("mysql_connect")
75 ) {
76 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
77 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
78 if(!$r_con){
79 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
80 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
81 gosa_log(mysql_error());
82 return false;
83 }
84 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
85 if(!$db){
86 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
87 gosa_log(mysql_error());
88 return false;
89 }
91 $first = false;
92 foreach($this->phoneNumbers as $key => $val){
93 if(!$first){
94 $first = $key;
95 }
96 }
97 }
99 /* Load hardware list */
100 $ldap= $this->config->get_ldap_link();
101 $ldap->cd($this->config->current['BASE']);
102 $ldap->search("(objectClass=goFonHardware)", array('cn', 'description'));
103 while ($attrs= $ldap->fetch()){
104 $cn= $attrs['cn'][0];
105 if (isset($attrs['description'])){
106 $description= " - ".$attrs['description'][0];
107 } else {
108 $description= "";
109 }
110 $this->hardware_list[$cn]= "$cn$description";
112 }
114 /* Perform search, to get Macro Parameters,Name,Dn,Displayname etc*/
115 $ldap->search("(objectClass=goFonMacro)", array("*"));
117 /* Add none for no macro*/
118 $this->macros['none']=_("no macro");
119 $this->macro ="none";
122 /* Fetch all Macros*/
123 while ($attrs= $ldap->fetch()){
125 /* Only visisble */
126 if((isset($attrs['goFonMacroVisible'][0]))&&($attrs['goFonMacroVisible'][0] ==1)){
128 /* unset Count, we don't need that here */
129 unset($attrs['displayName']['count']);
131 /* fill Selectfield variable with Macros */
132 if(isset($attrs['displayName'][0])){
133 $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
134 }else{
135 $this->macros[$attrs['dn']] = _("undefined");
136 }
138 /* Parse macro data, unset count for parameterarrays */
139 if (isset($attrs['goFonMacroParameter']['count'])){
140 unset($attrs['goFonMacroParameter']['count']);
141 }
143 /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
144 if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
146 foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
147 /* Split Data in readable values, by delimiter ! */
148 $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
150 /* Set all attrs */
151 $id = $data[0];
152 $this->macroarray[$attrs['dn']][$id]['var'] ="var".$id;
153 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
154 $this->macroarray[$attrs['dn']][$id]['id'] =$id;
155 $this->macroarray[$attrs['dn']][$id]['name'] =$data[1];
156 $this->macroarray[$attrs['dn']][$id]['type'] =$data[2];
157 $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
158 if($data[2] == "bool"){
159 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
160 }
161 }//foreach
162 }//is_array
163 }//visible = 1
164 }//while
166 /* Go through already saved values, for a parameter */
167 $tmp = split("!",$this->goFonMacro);
169 /* it is possible that nothing has been saved yet */
170 if(is_array($tmp)){
172 /* First value is the macroname */
173 $this->macro = $tmp[0];
175 /* Macroname saved, delete that index */
176 unset($tmp[0]);
178 /* Check if makro has been removed */
179 if(!isset($this->macroarray[$this->macro])){
180 $this->macrostillavailable = false;
181 }else{
182 $this->macrostillavailable = true;
183 }
185 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
186 foreach($tmp as $var){
188 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
189 $varar = split("#",$var);
191 /* Only insert if the parameter still exists */
192 if(isset($this->macroarray[$this->macro][$varar[0]])){
193 /* Assign value */
194 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
195 }
196 }
197 }
200 /* Eventually colorize phones */
201 $ldap->cd($this->config->current['BASE']);
202 foreach ($this->hardware_list as $cn => $desc){
203 $ldap->search("(goFonHardware=$cn)", array('cn'));
204 if ($ldap->count() > 0){
205 $ldap->fetch();
206 if ($ldap->getDN() != $this->dn){
207 $this->used_hardware[$cn]= $ldap->getDN();
208 }
209 }
210 }
211 $this->hardware_list["automatic"]= _("automatic");
212 ksort($this->hardware_list);
213 $this->a_old_telenums = $this->phoneNumbers;
215 if($this->is_account){
216 $this->is_modified = true;
217 }
220 /* Get voicemail PIN from MySQL DB
221 * Because every user can change his PIN directly from the phone
222 * without any update to the ldap
223 * This means, the PIN in the DB is up to date
224 */
225 // Connect to DB server
226 if((is_callable("mysql_pconnect"))&&(isset($a_SETUP))&&(isset($a_SETUP['SERVER']))&&(isset($a_SETUP['LOGIN']))&&(isset($a_SETUP['PASSWORD']))){
227 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
228 if($r_con){
229 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
230 $vp = mysql_fetch_row(mysql_query("SELECT ".$a_SETUP['VOICE_TABLE'].".password FROM ".$a_SETUP['VOICE_TABLE'].", ".$a_SETUP['SIP_TABLE']." WHERE customer_id = sip_users.mailbox AND name='".$this->uid."'"));
232 if((isset($vp[0]))&&(!empty($vp[0]))){
233 $this->goFonPINVoice = $vp[0];
234 }
235 }
236 }
237 $this->lastmacro=$this->macro;
239 if(is_callable("mysql_close")&&(isset($r_con))&&($r_con)){
240 @mysql_close($r_con) ;
241 }
242 }
245 // Generate MySQL Syntax
246 function generate_mysql_entension_entries($save = false){
248 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
249 if($save)
250 print_red(_("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."));
251 return(true);
252 }
254 if(!is_callable("mysql_pconnect")){
255 if($save)
256 print_red(_("Can't save any changes to asterisk database, there is no mysql extension available."));
257 return(true);
258 }
260 // Get Configuration for Mysql database Server
261 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON']; // DB Configuration
262 $s_parameter = ""; // Contains paramter for selected Macro
263 $r_con = false; // DB connection
264 $r_db = false; // Selected DB
265 $r_res = false; // Result resource
266 $a_ldap_attrs = array(); //
268 $s_ip = NULL; // Contains ip for Sip entry
269 $s_host = NULL; // Contains host for Sip entry
270 $s_qualify = NULL; // Qualify entry
271 $s_pin = NULL; // Entry for secret
272 $s_type = NULL; // Entry for phone type (friend , peer ..)
274 $sip_data_array = array(); // Contains complete sip entry, to generate SQL syntax
275 $i_old_key = false; // Contains index for first old phonenumber, to delete old entries corectly
276 $i_new_key = false; // Contains index for first new phonenumber, to generate new entries corectly
278 $s_sip_values = ""; // Contains string with all values for given attributes in SQL syntax
279 $s_sip_keys = ""; // Contains all needed attributes to generate sip entry in DB
281 $s_sip_key = ""; // Key for SIP entry index
282 $s_sip_val = ""; // Value for SIP entry index
284 $b_first_deleted= false; // Only delete first entry,
285 $s_telenums = ""; // for each value variable
287 $i_is_accounted =false; // Ensure that extension entry, for name to number is only once in table
290 // Connect to DB server
291 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
293 // Check if we are connected correctly
294 if(!$r_con){
295 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
296 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
297 gosa_log(mysql_error());
298 return false;
299 }
301 // Select database for Extensions
302 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
304 // Test if we have the database selected correctly
305 if(!$r_db){
306 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
307 gosa_log(mysql_error());
308 return false;
309 }
311 // Get phonehardware to setup sip entry
312 $ldap = $this->config->get_ldap_link();
313 $r_res = $ldap->search("(&(objectClass=goFonHardware)(cn=".$this->goFonHardware."))", array('*'));
314 $a_ldap_attrs = $ldap->fetch();
316 if($this->is_number_used()){
317 $this->generate_error = $this->is_number_used();
318 return false;
319 }
321 /* If Save == true, we should save something.
322 * Generate SQL, for drop of old entries
323 * Generate SQL, for insert new entries
324 */
325 if($save == true){
327 foreach($this->a_old_telenums as $tele){
328 $oldnums[]= preg_replace("/[^0-9]/","",$tele);
329 }
331 foreach($this->phoneNumbers as $tele){
332 $newnums[]= preg_replace("/[^0-9]/","",$tele);
333 }
335 // Attribute GoFonDefaultIP set ?
336 if(((isset($a_ldap_attrs['goFonDefaultIP'][0]))&&($a_ldap_attrs['goFonDefaultIP'][0] != "dynamic"))){
337 $s_ip = $a_ldap_attrs['goFonDefaultIP'][0];
338 $s_host = $s_ip;
339 }else{
340 $s_ip = NULL;
341 $s_host = "dynamic";
342 }
344 // Attribute GoFonQualify set ?
345 if(isset($a_ldap_attrs['goFonQualify'])){
346 $s_qualify = $a_ldap_attrs['goFonQualify'][0];
347 }
349 // Attribute GoFonPIN set ?
350 if(isset($this->goFonPIN)){
351 $s_pin = $this->goFonPIN;
352 }
354 // Attribute GoFonType set ?
355 if(isset($a_ldap_attrs['goFonType'])){
356 $s_type = $a_ldap_attrs['goFonType'][0];
357 }
359 if(isset($a_ldap_attrs['goFonDmtfMode'][0])){
360 $sip_data_array['dtmfmode'] = $a_ldap_attrs['goFonDmtfMode'][0];
361 }else{
362 $sip_data_array['dtmfmode'] ="rfc2833";
363 }
365 // generate SIP entry
366 $sip_data_array['id'] = "";
367 $sip_data_array['name'] = $this->uid;
368 $sip_data_array['accountcode'] = NULL;
369 $sip_data_array['amaflags'] = NULL;
370 $sip_data_array['callgroup'] = NULL;
371 $sip_data_array['callerid'] = "";
372 $sip_data_array['canreinvite'] = "no";
373 $sip_data_array['context'] = "default";
374 $sip_data_array['defaultip'] = NULL;
375 $sip_data_array['fromuser'] = NULL;
376 $sip_data_array['fromdomain'] = NULL;
377 $sip_data_array['host'] = $s_host;
378 $sip_data_array['insecure'] = NULL;
379 $sip_data_array['language'] = NULL;
380 $sip_data_array['mailbox'] = "asterisk";
381 $sip_data_array['md5secret'] = NULL;
382 $sip_data_array['nat'] = "no";
383 $sip_data_array['permit'] = NULL;
384 $sip_data_array['deny'] = NULL;
385 $sip_data_array['mask'] = NULL;
386 $sip_data_array['pickupgroup'] = NULL;
387 $sip_data_array['port'] = NULL;
388 $sip_data_array['qualify'] = $s_qualify;
389 $sip_data_array['restrictcid'] = "n";
390 $sip_data_array['rtptimeout'] = NULL;
391 $sip_data_array['rtpholdtimeout']=NULL;
392 $sip_data_array['secret'] = $this->goFonPIN;
393 $sip_data_array['type'] = $s_type ;
394 $sip_data_array['username'] = $this->uid;
395 $sip_data_array['disallow'] = NULL;
396 $sip_data_array['allow'] = NULL;
397 $sip_data_array['musiconhold'] = NULL;
398 $sip_data_array['regseconds'] = NULL;
399 $sip_data_array['ipaddr'] = $s_ip;
400 $sip_data_array['regexten'] = NULL;
401 $sip_data_array['cancallforward']=NULL;
403 // Get selected Macro Parameter and create parameter entry
404 if(isset($this->macroarray[$this->macro])){
405 foreach($this->macroarray[$this->macro] as $key => $val ){
406 $s_parameter .= $val['choosen']."|";
407 }
408 $s_parameter = preg_replace("/\|$/","",$s_parameter);
409 }
411 if($this->is_number_used()){
412 $this->generate_error = $this->is_number_used();
413 return false;
414 }
416 // Create new SIP entry ...
417 $sip_entry = $sip_data_array;
418 reset($newnums);
419 $i_new_key = key($newnums);
420 $sip_entry['callerid'] =$newnums[$i_new_key];
421 $sip_entry['mailbox'] =$newnums[$i_new_key];
423 if((isset($this->parent))&&(isset($this->parent->by_object['mailAccount']))&&($this->parent->by_object['mailAccount']->is_account==true)){
424 $s_mail = $this->parent->by_object['mailAccount']->mail;
425 }else{
426 $s_mail = "";
427 }
429 // $SQL contains all queries
430 $SQL = array();
431 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
432 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
433 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$newnums[$i_new_key].";";
434 // Delete old entries
435 $b_first_deleted =false;
436 if(isset($oldnums) && is_array($oldnums)){
437 foreach($oldnums as $s_telenums){
438 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
439 if(!$b_first_deleted){
440 $b_first_deleted=true;
441 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$s_telenums.";";
442 }
443 }
444 }
445 if($this->goFonHardware=="automatic"){
446 foreach($SQL as $query ){
447 mysql_query($query) ;
448 }
449 return;
450 }
452 // Generate Strings with keys and values
453 foreach($sip_entry as $s_sip_key=>$s_sip_val){
454 if($s_sip_val == NULL) continue;
455 $s_sip_values.="'".$s_sip_val."',";
456 $s_sip_keys .="`".$s_sip_key."`,";
457 }
458 // Remove last ,
459 $s_sip_values = preg_replace("/,$/","",$s_sip_values);
460 $s_sip_keys = preg_replace("/,$/","",$s_sip_keys);
462 // Append SIP Entry
463 $SQL[] ="INSERT INTO ".$a_SETUP['SIP_TABLE']." (".$s_sip_keys.") VALUES (".$s_sip_values.");";
465 /* If deletion starts from userslist, cn uid are not set */
466 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
467 $this->uid = $this->parent->by_object['user']->uid;
468 }
470 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
471 $this->cn = $this->parent->by_object['user']->cn;
472 }
474 if((!isset($this->cn))||(empty($this->cn))){
475 $CNname= $this->uid;
476 }else{
477 $CNname= $this->cn;
478 }
480 $SQL[]= "INSERT INTO ".$a_SETUP['VOICE_TABLE']." (`customer_id`,`context`,`mailbox`,`password`,`fullname`,`email`,`pager`)
481 VALUES ('".$newnums[$i_new_key]."',
482 'default',
483 '".$newnums[$i_new_key]."',
484 '".$this->goFonVoicemailPIN."',
485 '".$CNname."',
486 '".$s_mail."',
487 '');";
488 $i_is_accounted=false;
490 $i = 0;
492 $is_inserted_once = false;
494 // Entension entries Hint / Dial / Goto
495 foreach($newnums as $s_telenums){
497 if(!$is_inserted_once){
498 $is_inserted_once = true;
499 $EXT[$i]['context'] = 'GOsa';
500 $EXT[$i]['exten'] = $this->uid;
501 $EXT[$i]['priority']= 1;
502 $EXT[$i]['app'] = "Goto";
503 $EXT[$i]['appdata'] = $s_telenums."|1";
504 $i ++;
505 }
506 /* Hint Entry */
507 $EXT[$i]['context'] = 'GOsa';
508 $EXT[$i]['exten'] = $s_telenums;
509 $EXT[$i]['priority']= "Hint";
510 $EXT[$i]['app'] = 'SIP/'.$this->uid;
511 $i ++;
512 /* SetCID */
513 //$EXT[$i]['context'] = 'GOsa';
514 //$EXT[$i]['exten'] = $s_telenums;
515 //$EXT[$i]['priority']= 1;
516 //$EXT[$i]['app'] = "SetCIDName";
517 //$EXT[$i]['appdata'] = $CNname;
518 //$i ++;
520 // If no macro is selected use Dial
521 if($this->macro!="none"){
522 $macroname = preg_replace("/,.*$/","",$this->macro);
523 $macroname = preg_replace("/^.*=/","",$macroname);
524 $s_app = "Macro";$macroname;
525 $s_par = $macroname."|".$s_parameter;
526 }else{
527 $s_app = "Dial";
528 $s_par = 'SIP/'.$this->uid."|20|r";
529 }
531 $EXT[$i]['context'] = 'GOsa';
532 $EXT[$i]['exten'] = $s_telenums;
533 $EXT[$i]['priority']= 1;
534 $EXT[$i]['app'] = $s_app;
535 $EXT[$i]['appdata'] = $s_par;
536 $i ++;
538 }
540 // Append all these Entries
541 foreach($EXT as $entr){
542 $SQL_syn = "INSERT INTO ".$a_SETUP['EXT_TABLE']." (";
543 foreach($entr as $key2 => $val2){
544 $SQL_syn.= "`".$key2."`,";
545 }
546 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
547 $SQL_syn .= ") VALUES (";
548 foreach($entr as $key2 => $val2){
549 $SQL_syn .= "'".$val2."',";
550 }
551 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
552 $SQL_syn .=");\n";
553 $SQL[] =$SQL_syn;
554 $SQL_syn ="";
555 }
557 // Perform queries ...
558 foreach($SQL as $query){
559 if(!@mysql_query($query,$r_con)){
560 print_red(_("Error while performing query:")." ".mysql_error());
561 return false;
562 }
563 }
564 }
565 @mysql_close($r_con);
566 return true;
567 }
570 function execute()
571 {
572 /* Call parent execute */
573 plugin::execute();
575 $display = "";
577 if(empty($this->macro)&&(!empty($this->goFonMacro))){
579 /* Go through already saved values, for a parameter */
580 $tmp = split("!",$this->goFonMacro);
582 /* it is possible that nothing has been saved yet */
583 if(is_array($tmp)){
585 /* First value is the macroname */
586 $this->macro = $tmp[0];
588 /* Macroname saved, delete that index */
589 unset($tmp[0]);
591 /* Check if makro has been removed */
592 if(!isset($this->macroarray[$this->macro])){
593 $this->macrostillavailable = false;
594 }else{
595 $this->macrostillavailable = true;
596 }
598 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
599 foreach($tmp as $var){
601 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
602 $varar = split("#",$var);
604 /* Only insert if the parameter still exists */
605 if(isset($this->macroarray[$this->macro][$varar[0]])){
606 /* Assign value */
607 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
608 }
609 }
610 }
611 }
613 /* Do we represent a valid account? */
614 if (!$this->is_account && $this->parent == NULL){
615 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
616 _("This account has no phone extensions.")."</b>";
617 $display.= back_to_main();
618 return ($display);
619 }
621 /* Do we need to flip is_account state? */
622 if (isset($_POST['modify_state'])){
623 $this->is_account= !$this->is_account;
624 }
626 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
627 if(empty($this->macro)){
628 $this->macro ="none";
629 }
631 /* Prepare templating */
632 $smarty= get_smarty();
634 /* tell user that the pluging selected is no longer available*/
635 if((!$this->macrostillavailable)&&($this->macro!="none")){
636 print_red(_("The macro you selected, is no longer available for you, please choose another one."));
637 }
639 /* Assing macroselectbox values */
640 $smarty->assign("macros",$this->macros);
641 $smarty->assign("macro", $this->macro);
643 /* check if there is a FON server created */
644 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
645 print_red(_("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."));
646 }
648 /* Create parameter table, skip if no parameters given */
649 if(!isset($this->macroarray[$this->macro])){
650 $macrotab="";
651 }else{
653 $macrotab ="<table summary=\""._("Parameter")."\">";
654 /* for every single parameter-> display textfile,combo, or true false switch*/
656 foreach($this->phoneNumbers as $phonenum){
657 $tmp[] = $phonenum;
658 }
662 if($this->macro != $this->lastmacro){
663 /* Go through all params */
664 foreach($this->macroarray[$this->macro] as $key => $paras){
666 $string = $paras['default'];
668 $string=preg_replace("/%uid/i",$this->uid,$string);
670 if(isset($this->cn)){
671 $string=preg_replace("/%cn/i",$this->cn,$string);
672 }
674 for($i = 0 ; $i < 10; $i++){
675 if(isset($tmp[$i])){
676 $string = preg_replace("/%telephoneNumber_".($i+1)."/i",$tmp[$i],$string);
677 }
678 }
679 if(isset($tmp[0])){
680 $string = preg_replace("/%telephoneNumber/i",$tmp[0],$string);
681 }
682 $this->macroarray[$this->macro][$key]['choosen']=$string;
683 }
684 }
686 foreach($this->macroarray[$this->macro] as $paras){
688 /* get al vars */
689 $var = $paras['var'];
690 $name = $paras['name'];
691 $default = $paras['default'];
692 $type = $paras['type'];
693 $choosen = $paras['choosen'] ;
694 $str = $default;
696 /* in case of a combo box display a combobox with selected attr */
697 $macrotab.= "<tr>";
698 switch ($type){
700 case "combo":
701 $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro")." >";
702 foreach(split(":",$default) as $choice){
703 if($choosen==$choice){
704 $str.= "\n<option value='".$choice."' selected>".$choice." </option>";
705 }else{
706 $str.= "\n<option value='".$choice."'>".$choice." </option>";
707 }
708 }
709 $str.="</select>";
710 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
711 break;
713 case "bool":
714 if(!$choosen){
715 $str="\n<input type='checkbox' name='".$var."' value='1' ".chkacl($this->acl, "goFonMacro")." >";
716 }else{
717 $str="\n<input type='checkbox' name='".$var."' value='1' checked ".chkacl($this->acl, "goFonMacro").">";
718 }
719 $macrotab.= "<td colspan='2'>$str ".base64_decode($name)."";
720 break;
722 case "string":
723 $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro")." style='width:340px;'>";
724 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
725 break;
727 }
728 $macrotab.= "</td></tr>";
730 }
731 $macrotab.="</table><input name='post_success' type='hidden' value='1'>";
732 }//is_array()
734 /* Give smarty the table */
735 $smarty->assign("macrotab",$macrotab);
737 /* Do we represent a valid account? */
738 if (!$this->is_account && $this->parent == NULL){
739 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
740 _("This account has no phone extensions.")."</b>";
741 $display.= back_to_main();
742 return($display);
743 }
745 $display= "";
747 /* Show tab dialog headers */
748 if ($this->parent != NULL){
749 if ($this->is_account){
750 $display= $this->show_header(_("Remove phone account"),
751 _("This account has phone features enabled. You can disable them by clicking below."));
752 } else {
753 if(empty($this->uid)){
754 $display= $this->show_header(_("Create phone account"),
755 _("This account has phone features disabled. You can't enable them while no uid is set."),TRUE,TRUE);
756 }else{
757 $display= $this->show_header(_("Create phone account"),
758 _("This account has phone features disabled. You can enable them by clicking below."));
759 }
760 return ($display);
761 }
762 }
764 /* Add phone number */
765 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
766 if (is_phone_nr($_POST['phonenumber'])){
767 $number= $_POST["phonenumber"];
768 $this->phoneNumbers[$number]= $number;
769 $this->is_modified= TRUE;
770 } else {
771 print_red(_("Please enter a valid phone number!"));
772 }
773 }
775 /* Remove phone number */
776 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
777 foreach ($_POST['phonenumber_list'] as $number){
778 unset($this->phoneNumbers[$number]);
779 $this->is_modified= TRUE;
780 }
781 }
783 /* Transfer ACL's */
784 foreach($this->attributes as $val){
785 $smarty->assign($val."ACL", chkacl($this->acl,$val));
786 if(isset($this->$val)){
787 $smarty->assign($val,$this->$val);
788 }else{
789 $smarty->assign($val,"");
790 }
791 }
793 /* Fill arrays */
794 $smarty->assign ("goFonHardware", $this->goFonHardware);
795 if (!count($this->phoneNumbers)){
796 $smarty->assign ("phoneNumbers", array(""));
797 } else {
798 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
799 }
800 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
801 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
802 foreach ($this->hardware_list as $cn => $description){
803 if ($cn == $this->goFonHardware){
804 $selected= "selected";
805 } else {
806 $selected= "";
807 }
808 if (isset($this->used_hardware[$cn])){
809 $color= "style=\"color:#A0A0A0\"";
810 } else {
811 $color= "";
812 }
813 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description </option>\n";
814 }
815 $hl.= "</select>\n";
816 $smarty->assign ("hardware_list", $hl);
818 /* Show main page */
819 $this->lastmacro = $this->macro;
820 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
821 return($display);
822 }
825 function save_object()
826 {
827 if (isset($_POST["phoneTab"])){
828 plugin::save_object();
830 /* Save checkbox */
831 if (isset($_POST['fon_to_mail'])){
832 $tmp= "[M]";
833 } else {
834 $tmp= "[]";
835 }
836 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
837 if ($this->goFonDeliveryMode != $tmp){
838 $this->is_modified= TRUE;
839 }
840 $this->goFonDeliveryMode= $tmp;
841 }
843 /* Every macro in the select box are available */
844 if((isset($_POST['macro']))){
845 $this->macrostillavailable=true;
846 }
848 if(is_array($this->phoneNumbers)){
849 foreach($this->phoneNumbers as $telenumms) {
850 $nummsinorder[]=$telenumms;
851 }
852 }else{
853 $nummsinorder=array("");
854 }
856 /* get all Postvars */
857 if(isset($this->macroarray[$this->macro])){
858 foreach($this->macroarray[$this->macro] as $key => $paras){
859 if(isset($_POST[$paras['var']])){
860 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
861 }
863 /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
864 We need this code below to read and save checkboxes correct
865 */
867 if(isset($_POST['post_success'])){
868 if($this->macroarray[$this->macro][$key]['type']=="bool"){
869 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
870 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
871 }else{
872 $this->macroarray[$this->macro][$key]['choosen']=false;
873 }
874 }
875 }
876 }
877 }
878 }
879 }
881 function check()
882 {
883 /* Reset message array */
884 $message= array();
886 if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
887 $message[]=(_("Voicemail PIN must be between 1-4 characters."));
888 }else{
889 if(preg_match("/[^0-9]/",$this->goFonVoicemailPIN)){
890 $message[]=(_("The specified Voicemail PIN contains invalid characters, only numeric values are allowed here."));
891 }
892 }
894 if((strlen($this->goFonPIN)<=0)){
895 $message[]=(_("Phone PIN must be at least one character long."));
896 }else{
897 if(preg_match("/[^0-9a-z]/i",$this->goFonPIN)){
898 $message[]=(_("The specified phone PIN contains invalid characters, only aphanumeric values are allowed here."));
899 }
900 }
902 if(!$this->generate_mysql_entension_entries()){
903 $message[] = $this->generate_error;
904 }
906 /* We need at least one phone number */
907 if (count($this->phoneNumbers) == 0){
908 $message[]= sprintf(_("You need to specify at least one phone number!"));
909 }
911 /* check for ! in any parameter setting*/
912 if(isset($this->macroarray[$this->macro])){
913 foreach($this->macroarray[$this->macro] as $val){
914 if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
915 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
916 }
917 }
918 }
919 return ($message);
920 }
924 function save()
925 {
926 plugin::save();
928 /* Save arrays */
929 $this->attrs['telephoneNumber']= array();
930 foreach ($this->phoneNumbers as $number){
931 $this->attrs['telephoneNumber'][]= $number;
932 }
934 /* Save settings, or remove goFonMacro attribute*/
935 if($this->macro!="none"){
936 $this->attrs['goFonMacro']=$this->macro;
937 if(isset($this->macroarray[$this->macro])){
938 foreach($this->macroarray[$this->macro] as $paras) {
939 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
940 }
941 }
942 }else{
943 $this->attrs['goFonMacro']=array();
944 }
945 unset($this->attrs['macro']) ;
947 $this->attrs['goFonForwarding']=array();
949 $this->generate_mysql_entension_entries(true);
951 if($this->attrs['goFonMacro']==""){
952 $this->attrs['goFonMacro']=array();
953 }
955 unset($this->attrs['cn']);
957 /* Write back to ldap */
958 $ldap= $this->config->get_ldap_link();
959 $ldap->cd($this->dn);
960 $this->cleanup();
961 $ldap->modify ($this->attrs);
963 show_ldap_error($ldap->get_error());
965 /* Optionally execute a command after we're done */
967 if ($this->initially_was_account == $this->is_account){
968 if ($this->is_modified){
969 $this->handle_post_events("modify");
970 }
971 } else {
972 $this->handle_post_events("add");
973 }
975 }
978 function insert_after($entry, $nr, $list)
979 {
980 /* Is the entry free? No? Make it free... */
981 if (isset($list[$nr])) {
982 $dest= array();
983 $newidx= 0;
985 foreach ($list as $idx => $contents){
986 $dest[$newidx++]= $contents;
987 if ($idx == $nr){
988 $dest[$newidx++]= $entry;
989 }
990 }
991 } else {
992 $dest= $list;
993 $dest[$nr]= $entry;
994 }
996 return ($dest);
997 }
1000 function adapt_from_template($dn)
1001 {
1002 plugin::adapt_from_template($dn);
1004 /* Assemble phone numbers */
1005 if (isset($this->attrs['telephoneNumber'])){
1006 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
1007 $number= $this->attrs['telephoneNumber'][$i];
1008 $this->phoneNumbers[$number]= $number;
1009 }
1010 }
1011 }
1014 function remove_from_parent()
1015 {
1016 foreach($this->attributes as $key=>$val){
1017 if(in_array($val,array("uid","cn","mail"))){
1018 unset($this->attributes[$key]);
1019 unset($this->$val);
1020 }
1021 }
1023 if(array_key_exists('config', $_SESSION) &&
1024 array_key_exists('SERVERS', $_SESSION['config']->data) &&
1025 array_key_exists('FON', $_SESSION['config']->data['SERVERS']) &&
1026 is_callable("mysql_pconnect")) {
1027 // Get Configuration for Mysql database Server
1028 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
1029 $s_parameter ="";
1031 // Connect to DB server
1032 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
1034 // Check if we are connected correctly
1035 if(!$r_con){
1036 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
1037 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
1038 gosa_log(@mysql_error());
1039 return false;
1040 }
1042 // Select database for Extensions
1043 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
1045 // Test if we have the database selected correctly
1046 if(!$db){
1047 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
1048 gosa_log(@mysql_error());
1049 return false;
1050 }
1052 $SQL="";
1054 /* If deletion starts from userslist, cn uid are not set */
1055 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
1056 $this->uid = $this->parent->by_object['user']->uid;
1057 }
1059 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
1060 $this->cn = $this->parent->by_object['user']->cn;
1061 }
1063 $first_num = false;
1064 // Delete old entries
1065 foreach($this->a_old_telenums as $s_telenums){
1066 if(!$first_num){
1067 $first_num = $s_telenums;
1068 }
1069 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
1070 }
1072 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$first_num."';";
1073 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
1074 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
1076 foreach($SQL as $query){
1077 if(!@mysql_query($query,$r_con)){
1078 print_red(_("Stop".mysql_error()));
1079 return false;
1080 }
1081 }
1082 }else{
1083 print_red(_("Can't remove phone account, the mysql extension is not present in php configuration."));
1084 return false;
1085 }
1087 /* unset macro attr, it will cause an error */
1088 $tmp = array_flip($this->attributes);
1089 unset($tmp['macro']);
1090 $this->attributes=array_flip($tmp);
1092 /* Cancel if there's nothing to do here */
1093 if (!$this->initially_was_account){
1094 return;
1095 }
1097 plugin::remove_from_parent();
1099 /* Just keep one phone number */
1100 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
1101 $this->attrs['telephoneNumber']= $this->telephoneNumber;
1102 } else {
1103 $this->attrs['telephoneNumber']= array();
1104 }
1107 $ldap= $this->config->get_ldap_link();
1108 $ldap->cd($this->config->current['BASE']);
1109 $ldap->search("(objectClass=goFonQueue)", array("member"));
1110 while($attr = $ldap->fetch()){
1111 if(in_array($this->dn,$attr['member'])){
1112 $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
1113 unset($new->by_object['ogroup']->memberList[$this->dn]);
1114 unset($new->by_object['ogroup']->member[$this->dn]);
1115 $new->save();
1116 print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->attrs['cn']));
1117 }
1118 }
1119 $ldap->cd($this->dn);
1120 $this->cleanup();
1121 $ldap->modify ($this->attrs);
1123 show_ldap_error($ldap->get_error());
1125 /* Optionally execute a command after we're done */
1126 @mysql_close($r_con);
1127 echo "was successfull";
1128 $this->handle_post_events('remove');
1129 }
1133 /* This function checks if the given phonenumbers are available or already in use*/
1134 function is_number_used()
1135 {
1136 $ldap= $this->config->get_ldap_link();
1137 $ldap->cd($this->config->current['BASE']);
1138 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
1139 while($attrs = $ldap->fetch()) {
1140 unset($attrs['telephoneNumber']['count']);
1141 foreach($attrs['telephoneNumber'] as $tele){
1142 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
1143 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
1144 $numbers[$tele]=$attrs;
1145 }
1146 }
1148 foreach($this->phoneNumbers as $num){
1149 if(!isset($this->cn)) $this->cn = "";
1151 if((isset($numbers[$num]))&&(($numbers[$num]['uid'][0]!=$this->uid))){
1152 if(isset($numbers[$num]['uid'][0])){
1153 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
1154 }else{
1155 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
1156 }
1157 }
1158 }
1159 }
1161 function getCopyDialog()
1162 {
1163 $smarty = get_smarty();
1164 if (!count($this->phoneNumbers)){
1165 $smarty->assign ("phoneNumbers", array(""));
1166 } else {
1167 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
1168 }
1170 $smarty->assign("goFonVoicemailPIN",$this->goFonVoicemailPIN);
1171 $smarty->assign("goFonPIN",$this->goFonPIN);
1173 $display= $smarty->fetch(get_template_path('paste_generic.tpl', TRUE, dirname(__FILE__)));
1174 return($display);
1175 }
1177 function saveCopyDialog()
1178 {
1179 $this->execute();
1180 if(isset($_POST['goFonVoicemailPIN'])) {
1181 $this->goFonVoicemailPIN = $_POST['goFonVoicemailPIN'];
1182 }
1183 if(isset($_POST['goFonPIN'])){
1184 $this->goFonPIN = $_POST['goFonPIN'];
1185 }
1186 }
1187 }
1189 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1190 ?>