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 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
75 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
76 if(!$r_con){
77 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
78 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
79 gosa_log(mysql_error());
80 return false;
81 }
82 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
83 if(!$db){
84 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
85 gosa_log(mysql_error());
86 return false;
87 }
89 $first = false;
90 foreach($this->phoneNumbers as $key => $val){
91 if(!$first){
92 $first = $key;
93 }
94 }
95 }
97 /* Load hardware list */
98 $ldap= $this->config->get_ldap_link();
99 $ldap->cd($this->config->current['BASE']);
100 $ldap->search("(objectClass=goFonHardware)", array('cn', 'description'));
101 while ($attrs= $ldap->fetch()){
102 $cn= $attrs['cn'][0];
103 if (isset($attrs['description'])){
104 $description= " - ".$attrs['description'][0];
105 } else {
106 $description= "";
107 }
108 $this->hardware_list[$cn]= "$cn$description";
110 }
112 /* Perform search, to get Macro Parameters,Name,Dn,Displayname etc*/
113 $ldap->search("(objectClass=goFonMacro)", array("*"));
115 /* Add none for no macro*/
116 $this->macros['none']=_("no macro");
117 $this->macro ="none";
120 /* Fetch all Macros*/
121 while ($attrs= $ldap->fetch()){
123 /* Only visisble */
124 if((isset($attrs['goFonMacroVisible'][0]))&&($attrs['goFonMacroVisible'][0] ==1)){
126 /* unset Count, we don't need that here */
127 unset($attrs['displayName']['count']);
129 /* fill Selectfield variable with Macros */
130 if(isset($attrs['displayName'][0])){
131 $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
132 }else{
133 $this->macros[$attrs['dn']] = _("undefined");
134 }
136 /* Parse macro data, unset count for parameterarrays */
137 if (isset($attrs['goFonMacroParameter']['count'])){
138 unset($attrs['goFonMacroParameter']['count']);
139 }
141 /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
142 if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
144 foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
145 /* Split Data in readable values, by delimiter ! */
146 $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
148 /* Set all attrs */
149 $id = $data[0];
150 $this->macroarray[$attrs['dn']][$id]['var'] ="var".$id;
151 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
152 $this->macroarray[$attrs['dn']][$id]['id'] =$id;
153 $this->macroarray[$attrs['dn']][$id]['name'] =$data[1];
154 $this->macroarray[$attrs['dn']][$id]['type'] =$data[2];
155 $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
156 if($data[2] == "bool"){
157 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
158 }
159 }//foreach
160 }//is_array
161 }//visible = 1
162 }//while
164 /* Go through already saved values, for a parameter */
165 $tmp = split("!",$this->goFonMacro);
167 /* it is possible that nothing has been saved yet */
168 if(is_array($tmp)){
170 /* First value is the macroname */
171 $this->macro = $tmp[0];
173 /* Macroname saved, delete that index */
174 unset($tmp[0]);
176 /* Check if makro has been removed */
177 if(!isset($this->macroarray[$this->macro])){
178 $this->macrostillavailable = false;
179 }else{
180 $this->macrostillavailable = true;
181 }
183 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
184 foreach($tmp as $var){
186 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
187 $varar = split("#",$var);
189 /* Only insert if the parameter still exists */
190 if(isset($this->macroarray[$this->macro][$varar[0]])){
191 /* Assign value */
192 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
193 }
194 }
195 }
198 /* Eventually colorize phones */
199 $ldap->cd($this->config->current['BASE']);
200 foreach ($this->hardware_list as $cn => $desc){
201 $ldap->search("(goFonHardware=$cn)", array('cn'));
202 if ($ldap->count() > 0){
203 $ldap->fetch();
204 if ($ldap->getDN() != $this->dn){
205 $this->used_hardware[$cn]= $ldap->getDN();
206 }
207 }
208 }
209 $this->hardware_list["automatic"]= _("automatic");
210 ksort($this->hardware_list);
211 $this->a_old_telenums = $this->phoneNumbers;
213 if($this->is_account){
214 $this->is_modified = true;
215 }
218 /* Get voicemail PIN from MySQL DB
219 * Because every user can change his PIN directly from the phone
220 * without any update to the ldap
221 * This means, the PIN in the DB is up to date
222 */
223 // Connect to DB server
224 if((isset($a_SETUP))&&(isset($a_SETUP['SERVER']))&&(isset($a_SETUP['LOGIN']))&&(isset($a_SETUP['PASSWORD']))){
225 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
226 if($r_con){
227 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
228 $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."'"));
230 if((isset($vp[0]))&&(!empty($vp[0]))){
231 $this->goFonPINVoice = $vp[0];
232 }
233 }
234 }
235 $this->lastmacro=$this->macro;
236 @mysql_close($r_con) ;
237 }
240 // Generate MySQL Syntax
241 function generate_mysql_entension_entries($save = false){
243 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
244 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."));
245 return(true);
246 }
248 // Get Configuration for Mysql database Server
249 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON']; // DB Configuration
250 $s_parameter = ""; // Contains paramter for selected Macro
251 $r_con = false; // DB connection
252 $r_db = false; // Selected DB
253 $r_res = false; // Result resource
254 $a_ldap_attrs = array(); //
256 $s_ip = NULL; // Contains ip for Sip entry
257 $s_host = NULL; // Contains host for Sip entry
258 $s_qualify = NULL; // Qualify entry
259 $s_pin = NULL; // Entry for secret
260 $s_type = NULL; // Entry for phone type (friend , peer ..)
262 $sip_data_array = array(); // Contains complete sip entry, to generate SQL syntax
263 $i_old_key = false; // Contains index for first old phonenumber, to delete old entries corectly
264 $i_new_key = false; // Contains index for first new phonenumber, to generate new entries corectly
266 $s_sip_values = ""; // Contains string with all values for given attributes in SQL syntax
267 $s_sip_keys = ""; // Contains all needed attributes to generate sip entry in DB
269 $s_sip_key = ""; // Key for SIP entry index
270 $s_sip_val = ""; // Value for SIP entry index
272 $b_first_deleted= false; // Only delete first entry,
273 $s_telenums = ""; // for each value variable
275 $i_is_accounted =false; // Ensure that extension entry, for name to number is only once in table
278 // Connect to DB server
279 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
281 // Check if we are connected correctly
282 if(!$r_con){
283 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
284 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
285 gosa_log(mysql_error());
286 return false;
287 }
289 // Select database for Extensions
290 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
292 // Test if we have the database selected correctly
293 if(!$r_db){
294 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
295 gosa_log(mysql_error());
296 return false;
297 }
299 // Get phonehardware to setup sip entry
300 $ldap = $this->config->get_ldap_link();
301 $r_res = $ldap->search("(&(objectClass=goFonHardware)(cn=".$this->goFonHardware."))", array('*'));
302 $a_ldap_attrs = $ldap->fetch();
304 if($this->is_number_used()){
305 $this->generate_error = $this->is_number_used();
306 return false;
307 }
309 /* If Save == true, we should save something.
310 * Generate SQL, for drop of old entries
311 * Generate SQL, for insert new entries
312 */
313 if($save == true){
315 foreach($this->a_old_telenums as $tele){
316 $oldnums[]= preg_replace("/[^0-9]/","",$tele);
317 }
319 foreach($this->phoneNumbers as $tele){
320 $newnums[]= preg_replace("/[^0-9]/","",$tele);
321 }
323 // Attribute GoFonDefaultIP set ?
324 if(((isset($a_ldap_attrs['goFonDefaultIP'][0]))&&($a_ldap_attrs['goFonDefaultIP'][0] != "dynamic"))){
325 $s_ip = $a_ldap_attrs['goFonDefaultIP'][0];
326 $s_host = $s_ip;
327 }else{
328 $s_ip = NULL;
329 $s_host = "dynamic";
330 }
332 // Attribute GoFonQualify set ?
333 if(isset($a_ldap_attrs['goFonQualify'])){
334 $s_qualify = $a_ldap_attrs['goFonQualify'][0];
335 }
337 // Attribute GoFonPIN set ?
338 if(isset($this->goFonPIN)){
339 $s_pin = $this->goFonPIN;
340 }
342 // Attribute GoFonType set ?
343 if(isset($a_ldap_attrs['goFonType'])){
344 $s_type = $a_ldap_attrs['goFonType'][0];
345 }
347 if(isset($a_ldap_attrs['goFonDmtfMode'][0])){
348 $sip_data_array['dtmfmode'] = $a_ldap_attrs['goFonDmtfMode'][0];
349 }else{
350 $sip_data_array['dtmfmode'] ="rfc2833";
351 }
353 // generate SIP entry
354 $sip_data_array['id'] = "";
355 $sip_data_array['name'] = $this->uid;
356 $sip_data_array['accountcode'] = NULL;
357 $sip_data_array['amaflags'] = NULL;
358 $sip_data_array['callgroup'] = NULL;
359 $sip_data_array['callerid'] = "";
360 $sip_data_array['canreinvite'] = "no";
361 $sip_data_array['context'] = "default";
362 $sip_data_array['defaultip'] = NULL;
363 $sip_data_array['fromuser'] = NULL;
364 $sip_data_array['fromdomain'] = NULL;
365 $sip_data_array['host'] = $s_host;
366 $sip_data_array['insecure'] = NULL;
367 $sip_data_array['language'] = NULL;
368 $sip_data_array['mailbox'] = "asterisk";
369 $sip_data_array['md5secret'] = NULL;
370 $sip_data_array['nat'] = "no";
371 $sip_data_array['permit'] = NULL;
372 $sip_data_array['deny'] = NULL;
373 $sip_data_array['mask'] = NULL;
374 $sip_data_array['pickupgroup'] = NULL;
375 $sip_data_array['port'] = NULL;
376 $sip_data_array['qualify'] = $s_qualify;
377 $sip_data_array['restrictcid'] = "n";
378 $sip_data_array['rtptimeout'] = NULL;
379 $sip_data_array['rtpholdtimeout']=NULL;
380 $sip_data_array['secret'] = $this->goFonPIN;
381 $sip_data_array['type'] = $s_type ;
382 $sip_data_array['username'] = $this->uid;
383 $sip_data_array['disallow'] = NULL;
384 $sip_data_array['allow'] = NULL;
385 $sip_data_array['musiconhold'] = NULL;
386 $sip_data_array['regseconds'] = NULL;
387 $sip_data_array['ipaddr'] = $s_ip;
388 $sip_data_array['regexten'] = NULL;
389 $sip_data_array['cancallforward']=NULL;
391 // Get selected Macro Parameter and create parameter entry
392 if(isset($this->macroarray[$this->macro])){
393 foreach($this->macroarray[$this->macro] as $key => $val ){
394 $s_parameter .= $val['choosen']."|";
395 }
396 $s_parameter = preg_replace("/\|$/","",$s_parameter);
397 }
399 if($this->is_number_used()){
400 $this->generate_error = $this->is_number_used();
401 return false;
402 }
404 // Create new SIP entry ...
405 $sip_entry = $sip_data_array;
406 reset($newnums);
407 $i_new_key = key($newnums);
408 $sip_entry['callerid'] =$newnums[$i_new_key];
409 $sip_entry['mailbox'] =$newnums[$i_new_key];
411 if((isset($this->parent))&&(isset($this->parent->by_object['mailAccount']))&&($this->parent->by_object['mailAccount']->is_account==true)){
412 $s_mail = $this->parent->by_object['mailAccount']->mail;
413 }else{
414 $s_mail = "";
415 }
417 // $SQL contains all queries
418 $SQL = array();
419 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
420 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
421 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$newnums[$i_new_key].";";
422 // Delete old entries
423 $b_first_deleted =false;
424 foreach($oldnums as $s_telenums){
425 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
426 if(!$b_first_deleted){
427 $b_first_deleted=true;
428 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$s_telenums.";";
429 }
430 }
432 if($this->goFonHardware=="automatic"){
433 foreach($SQL as $query ){
434 mysql_query($query) ;
435 }
436 return;
437 }
439 // Generate Strings with keys and values
440 foreach($sip_entry as $s_sip_key=>$s_sip_val){
441 if($s_sip_val == NULL) continue;
442 $s_sip_values.="'".$s_sip_val."',";
443 $s_sip_keys .="`".$s_sip_key."`,";
444 }
445 // Remove last ,
446 $s_sip_values = preg_replace("/,$/","",$s_sip_values);
447 $s_sip_keys = preg_replace("/,$/","",$s_sip_keys);
449 // Append SIP Entry
450 $SQL[] ="INSERT INTO ".$a_SETUP['SIP_TABLE']." (".$s_sip_keys.") VALUES (".$s_sip_values.");";
452 /* If deletion starts from userslist, cn uid are not set */
453 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
454 $this->uid = $this->parent->by_object['user']->uid;
455 }
457 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
458 $this->cn = $this->parent->by_object['user']->cn;
459 }
461 if((!isset($this->cn))||(empty($this->cn))){
462 $CNname= $this->uid;
463 }else{
464 $CNname= $this->cn;
465 }
467 $SQL[]= "INSERT INTO ".$a_SETUP['VOICE_TABLE']." (`customer_id`,`context`,`mailbox`,`password`,`fullname`,`email`,`pager`)
468 VALUES ('".$newnums[$i_new_key]."',
469 'default',
470 '".$newnums[$i_new_key]."',
471 '".$this->goFonVoicemailPIN."',
472 '".$CNname."',
473 '".$s_mail."',
474 '');";
475 $i_is_accounted=false;
477 $i = 0;
479 $is_inserted_once = false;
481 // Entension entries Hint / Dial / Goto
482 foreach($newnums as $s_telenums){
484 if(!$is_inserted_once){
485 $is_inserted_once = true;
486 $EXT[$i]['context'] = 'GOsa';
487 $EXT[$i]['exten'] = $this->uid;
488 $EXT[$i]['priority']= 1;
489 $EXT[$i]['app'] = "Goto";
490 $EXT[$i]['appdata'] = $s_telenums."|1";
491 $i ++;
492 }
493 /* Hint Entry */
494 $EXT[$i]['context'] = 'GOsa';
495 $EXT[$i]['exten'] = $s_telenums;
496 $EXT[$i]['priority']= "Hint";
497 $EXT[$i]['app'] = 'SIP/'.$this->uid;
498 $i ++;
499 /* SetCID */
500 //$EXT[$i]['context'] = 'GOsa';
501 //$EXT[$i]['exten'] = $s_telenums;
502 //$EXT[$i]['priority']= 1;
503 //$EXT[$i]['app'] = "SetCIDName";
504 //$EXT[$i]['appdata'] = $CNname;
505 //$i ++;
507 // If no macro is selected use Dial
508 if($this->macro!="none"){
509 $macroname = preg_replace("/,.*$/","",$this->macro);
510 $macroname = preg_replace("/^.*=/","",$macroname);
511 $s_app = "Macro";$macroname;
512 $s_par = $macroname."|".$s_parameter;
513 }else{
514 $s_app = "Dial";
515 $s_par = 'SIP/'.$this->uid."|20|r";
516 }
518 $EXT[$i]['context'] = 'GOsa';
519 $EXT[$i]['exten'] = $s_telenums;
520 $EXT[$i]['priority']= 1;
521 $EXT[$i]['app'] = $s_app;
522 $EXT[$i]['appdata'] = $s_par;
523 $i ++;
525 }
527 // Append all these Entries
528 foreach($EXT as $entr){
529 $SQL_syn = "INSERT INTO ".$a_SETUP['EXT_TABLE']." (";
530 foreach($entr as $key2 => $val2){
531 $SQL_syn.= "`".$key2."`,";
532 }
533 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
534 $SQL_syn .= ") VALUES (";
535 foreach($entr as $key2 => $val2){
536 $SQL_syn .= "'".$val2."',";
537 }
538 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
539 $SQL_syn .=");\n";
540 $SQL[] =$SQL_syn;
541 $SQL_syn ="";
542 }
544 // Perform queries ...
545 foreach($SQL as $query){
546 if(!@mysql_query($query,$r_con)){
547 print_red(_("Error while performing query:")." ".mysql_error());
548 return false;
549 }
550 }
551 }
552 @mysql_close($r_con);
553 return true;
554 }
557 function execute()
558 {
559 /* Call parent execute */
560 plugin::execute();
562 $display = "";
564 if(empty($this->macro)&&(!empty($this->goFonMacro))){
566 /* Go through already saved values, for a parameter */
567 $tmp = split("!",$this->goFonMacro);
569 /* it is possible that nothing has been saved yet */
570 if(is_array($tmp)){
572 /* First value is the macroname */
573 $this->macro = $tmp[0];
575 /* Macroname saved, delete that index */
576 unset($tmp[0]);
578 /* Check if makro has been removed */
579 if(!isset($this->macroarray[$this->macro])){
580 $this->macrostillavailable = false;
581 }else{
582 $this->macrostillavailable = true;
583 }
585 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
586 foreach($tmp as $var){
588 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
589 $varar = split("#",$var);
591 /* Only insert if the parameter still exists */
592 if(isset($this->macroarray[$this->macro][$varar[0]])){
593 /* Assign value */
594 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
595 }
596 }
597 }
598 }
600 /* Do we represent a valid account? */
601 if (!$this->is_account && $this->parent == NULL){
602 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
603 _("This account has no phone extensions.")."</b>";
604 $display.= back_to_main();
605 return ($display);
606 }
608 /* Do we need to flip is_account state? */
609 if (isset($_POST['modify_state'])){
610 $this->is_account= !$this->is_account;
611 }
613 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
614 if(empty($this->macro)){
615 $this->macro ="none";
616 }
618 /* Prepare templating */
619 $smarty= get_smarty();
621 /* tell user that the pluging selected is no longer available*/
622 if((!$this->macrostillavailable)&&($this->macro!="none")){
623 print_red(_("The macro you selected, is no longer available for you, please choose another one."));
624 }
626 /* Assing macroselectbox values */
627 $smarty->assign("macros",$this->macros);
628 $smarty->assign("macro", $this->macro);
630 /* check if there is a FON server created */
631 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
632 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."));
633 }
635 /* Create parameter table, skip if no parameters given */
636 if(!isset($this->macroarray[$this->macro])){
637 $macrotab="";
638 }else{
640 $macrotab ="<table summary=\""._("Parameter")."\">";
641 /* for every single parameter-> display textfile,combo, or true false switch*/
643 foreach($this->phoneNumbers as $phonenum){
644 $tmp[] = $phonenum;
645 }
649 if($this->macro != $this->lastmacro){
650 /* Go through all params */
651 foreach($this->macroarray[$this->macro] as $key => $paras){
653 $string = $paras['default'];
655 $string=preg_replace("/%uid/i",$this->uid,$string);
657 if(isset($this->cn)){
658 $string=preg_replace("/%cn/i",$this->cn,$string);
659 }
661 for($i = 0 ; $i < 10; $i++){
662 if(isset($tmp[$i])){
663 $string = preg_replace("/%telephoneNumber_".($i+1)."/i",$tmp[$i],$string);
664 }
665 }
666 if(isset($tmp[0])){
667 $string = preg_replace("/%telephoneNumber/i",$tmp[0],$string);
668 }
669 $this->macroarray[$this->macro][$key]['choosen']=$string;
670 }
671 }
673 foreach($this->macroarray[$this->macro] as $paras){
675 /* get al vars */
676 $var = $paras['var'];
677 $name = $paras['name'];
678 $default = $paras['default'];
679 $type = $paras['type'];
680 $choosen = $paras['choosen'] ;
681 $str = $default;
683 /* in case of a combo box display a combobox with selected attr */
684 $macrotab.= "<tr>";
685 switch ($type){
687 case "combo":
688 $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro")." ".chkacl($this->acl, "goFonMacro").">";
689 foreach(split(":",$default) as $choice){
690 if($choosen==$choice){
691 $str.= "\n<option value='".$choice."' selected>".$choice." </option>";
692 }else{
693 $str.= "\n<option value='".$choice."'>".$choice." </option>";
694 }
695 }
696 $str.="</select>";
697 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
698 break;
700 case "bool":
701 if(!$choosen){
702 $str="\n<input type='checkbox' name='".$var."' value='1' ".chkacl($this->acl, "goFonMacro")." >";
703 }else{
704 $str="\n<input type='checkbox' name='".$var."' value='1' checked ".chkacl($this->acl, "goFonMacro").">";
705 }
706 $macrotab.= "<td colspan='2'>$str ".base64_decode($name)."";
707 break;
709 case "string":
710 $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro")." style='width:340px;'>";
711 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
712 break;
714 }
715 $macrotab.= "</td></tr>";
717 }
718 $macrotab.="</table><input name='post_success' type='hidden' value='1'>";
719 }//is_array()
721 /* Give smarty the table */
722 $smarty->assign("macrotab",$macrotab);
724 /* Do we represent a valid account? */
725 if (!$this->is_account && $this->parent == NULL){
726 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
727 _("This account has no phone extensions.")."</b>";
728 $display.= back_to_main();
729 return($display);
730 }
732 $display= "";
734 /* Show tab dialog headers */
735 if ($this->parent != NULL){
736 if ($this->is_account){
737 $display= $this->show_header(_("Remove phone account"),
738 _("This account has phone features enabled. You can disable them by clicking below."));
739 } else {
740 if(empty($this->uid)){
741 $display= $this->show_header(_("Create phone account"),
742 _("This account has phone features disabled. You can't enable them while no uid is set."),TRUE,TRUE);
743 }else{
744 $display= $this->show_header(_("Create phone account"),
745 _("This account has phone features disabled. You can enable them by clicking below."));
746 }
747 return ($display);
748 }
749 }
751 /* Add phone number */
752 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
753 if (is_phone_nr($_POST['phonenumber'])){
754 $number= $_POST["phonenumber"];
755 $this->phoneNumbers[$number]= $number;
756 $this->is_modified= TRUE;
757 } else {
758 print_red(_("Please enter a valid phone number!"));
759 }
760 }
762 /* Remove phone number */
763 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
764 foreach ($_POST['phonenumber_list'] as $number){
765 unset($this->phoneNumbers[$number]);
766 $this->is_modified= TRUE;
767 }
768 }
770 /* Transfer ACL's */
771 foreach($this->attributes as $val){
772 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
773 if(isset($this->$val)){
774 $smarty->assign($val,$this->$val);
775 }else{
776 $smarty->assign($val,"");
777 }
778 }
780 /* Fill arrays */
781 $smarty->assign ("goFonHardware", $this->goFonHardware);
782 if (!count($this->phoneNumbers)){
783 $smarty->assign ("phoneNumbers", array(""));
784 } else {
785 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
786 }
787 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
788 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
789 foreach ($this->hardware_list as $cn => $description){
790 if ($cn == $this->goFonHardware){
791 $selected= "selected";
792 } else {
793 $selected= "";
794 }
795 if (isset($this->used_hardware[$cn])){
796 $color= "style=\"color:#A0A0A0\"";
797 } else {
798 $color= "";
799 }
800 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description </option>\n";
801 }
802 $hl.= "</select>\n";
803 $smarty->assign ("hardware_list", $hl);
805 /* Show main page */
806 $this->lastmacro = $this->macro;
807 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
808 return($display);
809 }
812 function save_object()
813 {
814 if (isset($_POST["phoneTab"])){
815 plugin::save_object();
817 /* Save checkbox */
818 if (isset($_POST['fon_to_mail'])){
819 $tmp= "[M]";
820 } else {
821 $tmp= "[]";
822 }
823 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
824 if ($this->goFonDeliveryMode != $tmp){
825 $this->is_modified= TRUE;
826 }
827 $this->goFonDeliveryMode= $tmp;
828 }
830 /* Every macro in the select box are available */
831 if((isset($_POST['macro']))){
832 $this->macrostillavailable=true;
833 }
835 if(is_array($this->phoneNumbers)){
836 foreach($this->phoneNumbers as $telenumms) {
837 $nummsinorder[]=$telenumms;
838 }
839 }else{
840 $nummsinorder=array("");
841 }
843 /* get all Postvars */
844 if(isset($this->macroarray[$this->macro])){
845 foreach($this->macroarray[$this->macro] as $key => $paras){
846 if(isset($_POST[$paras['var']])){
847 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
848 }
850 /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
851 We need this code below to read and save checkboxes correct
852 */
854 if(isset($_POST['post_success'])){
855 if($this->macroarray[$this->macro][$key]['type']=="bool"){
856 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
857 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
858 }else{
859 $this->macroarray[$this->macro][$key]['choosen']=false;
860 }
861 }
862 }
863 }
864 }
865 }
866 }
868 function check()
869 {
870 /* Reset message array */
871 $message= array();
873 if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
874 $message[]=(_("Voicemail PIN must be between 1-4 characters."));
875 }else{
876 if(preg_match("/[^0-9]/",$this->goFonVoicemailPIN)){
877 $message[]=(_("The specified Voicemail PIN contains invalid characters, only numeric values are allowed here."));
878 }
879 }
882 if((strlen($this->goFonPIN)!=4)){
883 $message[]=(_("Phone PIN must be 4 characters long."));
884 }else{
885 if(preg_match("/[^0-9]/",$this->goFonPIN)){
886 $message[]=(_("The specified phone PIN contains invalid characters, only numeric values are allowed here."));
887 }
888 }
890 if(!$this->generate_mysql_entension_entries()){
891 $message[] = $this->generate_error;
892 }
894 /* We need at least one phone number */
895 if (count($this->phoneNumbers) == 0){
896 $message[]= sprintf(_("You need to specify at least one phone number!"));
897 }
899 /* check for ! in any parameter setting*/
900 if(isset($this->macroarray[$this->macro])){
901 foreach($this->macroarray[$this->macro] as $val){
902 if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
903 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
904 }
905 }
906 }
907 return ($message);
908 }
912 function save()
913 {
914 plugin::save();
916 /* Save arrays */
917 $this->attrs['telephoneNumber']= array();
918 foreach ($this->phoneNumbers as $number){
919 $this->attrs['telephoneNumber'][]= $number;
920 }
922 /* Save settings, or remove goFonMacro attribute*/
923 if($this->macro!="none"){
924 $this->attrs['goFonMacro']=$this->macro;
925 if(isset($this->macroarray[$this->macro])){
926 foreach($this->macroarray[$this->macro] as $paras) {
927 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
928 }
929 }
930 }else{
931 $this->attrs['goFonMacro']=array();
932 }
933 unset($this->attrs['macro']) ;
935 $this->attrs['goFonForwarding']=array();
937 $this->generate_mysql_entension_entries(true);
939 if($this->attrs['goFonMacro']==""){
940 $this->attrs['goFonMacro']=array();
941 }
943 unset($this->attrs['cn']);
945 /* Write back to ldap */
946 $ldap= $this->config->get_ldap_link();
947 $ldap->cd($this->dn);
948 $ldap->modify($this->attrs);
949 show_ldap_error($ldap->get_error());
951 /* Optionally execute a command after we're done */
953 if ($this->initially_was_account == $this->is_account){
954 if ($this->is_modified){
955 $this->handle_post_events("modify");
956 }
957 } else {
958 $this->handle_post_events("add");
959 }
961 }
964 function insert_after($entry, $nr, $list)
965 {
966 /* Is the entry free? No? Make it free... */
967 if (isset($list[$nr])) {
968 $dest= array();
969 $newidx= 0;
971 foreach ($list as $idx => $contents){
972 $dest[$newidx++]= $contents;
973 if ($idx == $nr){
974 $dest[$newidx++]= $entry;
975 }
976 }
977 } else {
978 $dest= $list;
979 $dest[$nr]= $entry;
980 }
982 return ($dest);
983 }
986 function adapt_from_template($dn)
987 {
988 plugin::adapt_from_template($dn);
990 /* Assemble phone numbers */
991 if (isset($this->attrs['telephoneNumber'])){
992 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
993 $number= $this->attrs['telephoneNumber'][$i];
994 $this->phoneNumbers[$number]= $number;
995 }
996 }
997 }
1000 function remove_from_parent()
1001 {
1003 foreach($this->attributes as $key=>$val){
1004 if(in_array($val,array("uid","cn","mail"))){
1005 unset($this->attributes[$key]);
1006 unset($this->$val);
1007 }
1008 }
1010 if(array_key_exists('config', $_SESSION) &&
1011 array_key_exists('SERVERS', $_SESSION['config']->data) &&
1012 array_key_exists('FON', $_SESSION['config']->data['SERVERS'])) {
1013 // Get Configuration for Mysql database Server
1014 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
1015 $s_parameter ="";
1017 // Connect to DB server
1018 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
1020 // Check if we are connected correctly
1021 if(!$r_con){
1022 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
1023 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
1024 gosa_log(@mysql_error());
1025 return false;
1026 }
1028 // Select database for Extensions
1029 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
1031 // Test if we have the database selected correctly
1032 if(!$db){
1033 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
1034 gosa_log(@mysql_error());
1035 return false;
1036 }
1038 $SQL="";
1040 /* If deletion starts from userslist, cn uid are not set */
1041 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
1042 $this->uid = $this->parent->by_object['user']->uid;
1043 }
1045 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
1046 $this->cn = $this->parent->by_object['user']->cn;
1047 }
1049 $first_num = false;
1050 // Delete old entries
1051 foreach($this->a_old_telenums as $s_telenums){
1052 if(!$first_num){
1053 $first_num = $s_telenums;
1054 }
1055 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
1056 }
1058 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$first_num."';";
1059 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
1060 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
1063 foreach($SQL as $query){
1064 if(!@mysql_query($query,$r_con)){
1065 print_red(_("Stop".mysql_error()));
1066 return false;
1067 }
1068 }
1069 }
1072 /* unset macro attr, it will cause an error */
1073 $tmp = array_flip($this->attributes);
1074 unset($tmp['macro']);
1075 $this->attributes=array_flip($tmp);
1077 /* Cancel if there's nothing to do here */
1078 if (!$this->initially_was_account){
1079 return;
1080 }
1082 plugin::remove_from_parent();
1084 /* Just keep one phone number */
1085 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
1086 $this->attrs['telephoneNumber']= $this->telephoneNumber;
1087 } else {
1088 $this->attrs['telephoneNumber']= array();
1089 }
1091 $ldap= $this->config->get_ldap_link();
1092 $ldap->cd($this->config->current['BASE']);
1093 $ldap->search("(objectClass=goFonQueue)", array("member"));
1094 while($attr = $ldap->fetch()){
1095 if(in_array($this->dn,$attr['member'])){
1096 $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
1097 unset($new->by_object['ogroup']->memberList[$this->dn]);
1098 unset($new->by_object['ogroup']->member[$this->dn]);
1099 $new->save();
1100 print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->attrs['cn']));
1101 }
1102 }
1103 $ldap->cd($this->dn);
1104 $ldap->modify($this->attrs);
1105 show_ldap_error($ldap->get_error());
1107 /* Optionally execute a command after we're done */
1108 @mysql_close($r_con);
1109 $this->handle_post_events('remove');
1110 }
1114 /* This function checks if the given phonenumbers are available or already in use*/
1115 function is_number_used()
1116 {
1117 $ldap= $this->config->get_ldap_link();
1118 $ldap->cd($this->config->current['BASE']);
1119 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
1120 while($attrs = $ldap->fetch()) {
1121 unset($attrs['telephoneNumber']['count']);
1122 foreach($attrs['telephoneNumber'] as $tele){
1123 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
1124 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
1125 $numbers[$tele]=$attrs;
1126 }
1127 }
1129 foreach($this->phoneNumbers as $num){
1130 if(!isset($this->cn)) $this->cn = "";
1132 if((isset($numbers[$num]))&&(($numbers[$num]['uid'][0]!=$this->uid))){
1133 if(isset($numbers[$num]['uid'][0])){
1134 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
1135 }else{
1136 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
1137 }
1138 }
1139 }
1140 }
1141 }
1143 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1144 ?>