2a862b040f1eeb33610ffd79b98e3e156d80222f
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 Macro
23 var $macros = array(); // List of macros for smarty select box
24 var $macroarray = array(); // All needed macro informations
25 var $macrostillavailable = false;
26 var $generate_error = "";
27 var $a_old_telenums = array();
28 var $goFonPINVoice = "";
30 /* CLI vars */
31 var $cli_summary = "Manage users phone account";
32 var $cli_description = "Some longer text\nfor help";
33 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
35 /* attribute list for save action */
36 var $attributes = array("goFonDeliveryMode", "goFonFormat","uid","cn","mail",
37 "goFonHardware","goFonPIN","goFonVoicemailPIN","telephoneNumber", "goFonMacro","macro");
38 var $objectclasses= array("goFonAccount");
40 function phoneAccount ($config, $dn= NULL)
41 {
42 plugin::plugin ($config, $dn);
44 /* Set phone hardware */
45 if (!isset($this->attrs['goFonHardware'])){
46 $this->goFonHardware= "automatic";
47 }
49 /* Preset voice format */
50 if (!isset($this->attrs['goFonFormat'])){
51 $this->goFonFormat= "wav";
52 }
54 /* Assemble phone numbers */
55 if (isset($this->attrs['telephoneNumber'])){
56 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
57 $number= $this->attrs['telephoneNumber'][$i];
58 $this->phoneNumbers[$number]= $number;
59 }
60 }
62 /* Set up has_mailAccount */
63 if (isset($this->attrs['objectClass'])){
64 if (in_array("gosaMailAccount", $this->attrs['objectClass'])){
65 $this->has_mailAccount= TRUE;
66 }
67 }
69 $a_SETUP= array();
70 if(array_key_exists('config',$_SESSION) &&
71 array_key_exists('SERVERS',$_SESSION['config']->data) &&
72 array_key_exists('FON',$_SESSION['config']->data['SERVERS'])) {
73 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
74 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
75 if(!$r_con){
76 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
77 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
78 gosa_log(mysql_error());
79 return false;
80 }
81 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
82 if(!$db){
83 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
84 gosa_log(mysql_error());
85 return false;
86 }
88 $first = false;
89 foreach($this->phoneNumbers as $key => $val){
90 if(!$first){
91 $first = $key;
92 }
93 }
94 }
96 /* Load hardware list */
97 $ldap= $this->config->get_ldap_link();
98 $ldap->cd($this->config->current['BASE']);
99 $ldap->search("(objectClass=goFonHardware)", array('cn', 'description'));
100 while ($attrs= $ldap->fetch()){
101 $cn= $attrs['cn'][0];
102 if (isset($attrs['description'])){
103 $description= " - ".$attrs['description'][0];
104 } else {
105 $description= "";
106 }
107 $this->hardware_list[$cn]= "$cn$description";
109 }
111 /* Perform search, to get Macro Parameters,Name,Dn,Displayname etc*/
112 $ldap->search("(objectClass=goFonMacro)", array("*"));
114 /* Add none for no macro*/
115 $this->macros['none']=_("no macro");
116 $this->macro ="none";
119 /* Fetch all Macros*/
120 while ($attrs= $ldap->fetch()){
122 /* Only visisble */
123 if((isset($attrs['goFonMacroVisible'][0]))&&($attrs['goFonMacroVisible'][0] ==1)){
125 /* unset Count, we don't need that here */
126 unset($attrs['displayName']['count']);
128 /* fill Selectfield variable with Macros */
129 if(isset($attrs['displayName'][0])){
130 $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
131 }else{
132 $this->macros[$attrs['dn']] = _("undefined");
133 }
135 /* Parse macro data, unset count for parameterarrays */
136 if (isset($attrs['goFonMacroParameter']['count'])){
137 unset($attrs['goFonMacroParameter']['count']);
138 }
140 /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
141 if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
143 foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
144 /* Split Data in readable values, by delimiter ! */
145 $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
147 /* Set all attrs */
148 $id = $data[0];
149 $this->macroarray[$attrs['dn']][$id]['var'] ="var".$id;
150 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
151 $this->macroarray[$attrs['dn']][$id]['id'] =$id;
152 $this->macroarray[$attrs['dn']][$id]['name'] =$data[1];
153 $this->macroarray[$attrs['dn']][$id]['type'] =$data[2];
154 $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
155 if($data[2] == "bool"){
156 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
157 }
158 }//foreach
159 }//is_array
160 }//visible = 1
161 }//while
163 /* Go through already saved values, for a parameter */
164 $tmp = split("!",$this->goFonMacro);
166 /* it is possible that nothing has been saved yet */
167 if(is_array($tmp)){
169 /* First value is the macroname */
170 $this->macro = $tmp[0];
172 /* Macroname saved, delete that index */
173 unset($tmp[0]);
175 /* Check if makro has been removed */
176 if(!isset($this->macroarray[$this->macro])){
177 $this->macrostillavailable = false;
178 }else{
179 $this->macrostillavailable = true;
180 }
182 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
183 foreach($tmp as $var){
185 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
186 $varar = split("#",$var);
188 /* Only insert if the parameter still exists */
189 if(isset($this->macroarray[$this->macro][$varar[0]])){
190 /* Assign value */
191 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
192 }
193 }
194 }
197 /* Eventually colorize phones */
198 $ldap->cd($this->config->current['BASE']);
199 foreach ($this->hardware_list as $cn => $desc){
200 $ldap->search("(goFonHardware=$cn)", array('cn'));
201 if ($ldap->count() > 0){
202 $ldap->fetch();
203 if ($ldap->getDN() != $this->dn){
204 $this->used_hardware[$cn]= $ldap->getDN();
205 }
206 }
207 }
208 $this->hardware_list["automatic"]= _("automatic");
209 ksort($this->hardware_list);
210 $this->a_old_telenums = $this->phoneNumbers;
212 if($this->is_account){
213 $this->is_modified = true;
214 }
217 /* Get voicemail PIN from MySQL DB
218 * Because every user can change his PIN directly from the phone
219 * without any update to the ldap
220 * This means, the PIN in the DB is up to date
221 */
222 // Connect to DB server
223 if((isset($a_SETUP))&&(isset($a_SETUP['SERVER']))&&(isset($a_SETUP['LOGIN']))&&(isset($a_SETUP['PASSWORD']))){
224 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
225 if($r_con){
226 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
227 $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."'"));
229 if((isset($vp[0]))&&(!empty($vp[0]))){
230 $this->goFonPINVoice = $vp[0];
231 }
232 }
233 }
234 @mysql_close($r_con) ;
235 }
238 // Generate MySQL Syntax
239 function generate_mysql_entension_entries($save = false){
241 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
242 print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer).\nYour Settings can't be saved to asterisk Database."));
243 return(true);
244 }
246 // Get Configuration for Mysql database Server
247 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON']; // DB Configuration
248 $s_parameter = ""; // Contains paramter for selected Macro
249 $r_con = false; // DB connection
250 $r_db = false; // Selected DB
251 $r_res = false; // Result resource
252 $a_ldap_attrs = array(); //
254 $s_ip = NULL; // Contains ip for Sip entry
255 $s_host = NULL; // Contains host for Sip entry
256 $s_qualify = NULL; // Qualify entry
257 $s_pin = NULL; // Entry for secret
258 $s_type = NULL; // Entry for phone type (friend , peer ..)
260 $sip_data_array = array(); // Contains complete sip entry, to generate SQL syntax
261 $i_old_key = false; // Contains index for first old phonenumber, to delete old entries corectly
262 $i_new_key = false; // Contains index for first new phonenumber, to generate new entries corectly
264 $s_sip_values = ""; // Contains string with all values for given attributes in SQL syntax
265 $s_sip_keys = ""; // Contains all needed attributes to generate sip entry in DB
267 $s_sip_key = ""; // Key for SIP entry index
268 $s_sip_val = ""; // Value for SIP entry index
270 $b_first_deleted= false; // Only delete first entry,
271 $s_telenums = ""; // for each value variable
273 $i_is_accounted =false; // Ensure that extension entry, for name to number is only once in table
276 // Connect to DB server
277 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
279 // Check if we are connected correctly
280 if(!$r_con){
281 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
282 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
283 gosa_log(mysql_error());
284 return false;
285 }
287 // Select database for Extensions
288 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
290 // Test if we have the database selected correctly
291 if(!$r_db){
292 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
293 gosa_log(mysql_error());
294 return false;
295 }
297 // Get phonehardware to setup sip entry
298 $ldap = $this->config->get_ldap_link();
299 $r_res = $ldap->search("(&(objectClass=goFonHardware)(cn=".$this->goFonHardware."))", array('*'));
300 $a_ldap_attrs = $ldap->fetch();
302 if($this->is_number_used()){
303 $this->generate_error = $this->is_number_used();
304 return false;
305 }
307 /* If Save == true, we should save something.
308 * Generate SQL, for drop of old entries
309 * Generate SQL, for insert new entries
310 */
311 if($save == true){
313 foreach($this->a_old_telenums as $tele){
314 $oldnums[]= preg_replace("/[^0-9]/","",$tele);
315 }
317 foreach($this->phoneNumbers as $tele){
318 $newnums[]= preg_replace("/[^0-9]/","",$tele);
319 }
321 // Attribute GoFonDefaultIP set ?
322 if(((isset($a_ldap_attrs['goFonDefaultIP'][0]))&&($a_ldap_attrs['goFonDefaultIP'][0] != "dynamic"))){
323 $s_ip = $a_ldap_attrs['goFonDefaultIP'][0];
324 $s_host = $s_ip;
325 }else{
326 $s_ip = NULL;
327 $s_host = "dynamic";
328 }
330 // Attribute GoFonQualify set ?
331 if(isset($a_ldap_attrs['goFonQualify'])){
332 $s_qualify = $a_ldap_attrs['goFonQualify'][0];
333 }
335 // Attribute GoFonPIN set ?
336 if(isset($this->goFonPIN)){
337 $s_pin = $this->goFonPIN;
338 }
340 // Attribute GoFonType set ?
341 if(isset($a_ldap_attrs['goFonType'])){
342 $s_type = $a_ldap_attrs['goFonType'][0];
343 }
345 if(isset($a_ldap_attrs['goFonDmtfMode'][0])){
346 $sip_data_array['dtmfmode'] = $a_ldap_attrs['goFonDmtfMode'][0];
347 }else{
348 $sip_data_array['dtmfmode'] ="rfc2833";
349 }
351 // generate SIP entry
352 $sip_data_array['id'] = "";
353 $sip_data_array['name'] = $this->uid;
354 $sip_data_array['accountcode'] = NULL;
355 $sip_data_array['amaflags'] = NULL;
356 $sip_data_array['callgroup'] = NULL;
357 $sip_data_array['callerid'] = "";
358 $sip_data_array['canreinvite'] = "no";
359 $sip_data_array['context'] = "default";
360 $sip_data_array['defaultip'] = NULL;
361 $sip_data_array['fromuser'] = NULL;
362 $sip_data_array['fromdomain'] = NULL;
363 $sip_data_array['host'] = $s_host;
364 $sip_data_array['insecure'] = NULL;
365 $sip_data_array['language'] = NULL;
366 $sip_data_array['mailbox'] = "asterisk";
367 $sip_data_array['md5secret'] = NULL;
368 $sip_data_array['nat'] = "no";
369 $sip_data_array['permit'] = NULL;
370 $sip_data_array['deny'] = NULL;
371 $sip_data_array['mask'] = NULL;
372 $sip_data_array['pickupgroup'] = NULL;
373 $sip_data_array['port'] = NULL;
374 $sip_data_array['qualify'] = $s_qualify;
375 $sip_data_array['restrictcid'] = "n";
376 $sip_data_array['rtptimeout'] = NULL;
377 $sip_data_array['rtpholdtimeout']=NULL;
378 $sip_data_array['secret'] = $this->goFonPIN;
379 $sip_data_array['type'] = $s_type ;
380 $sip_data_array['username'] = $this->uid;
381 $sip_data_array['disallow'] = NULL;
382 $sip_data_array['allow'] = NULL;
383 $sip_data_array['musiconhold'] = NULL;
384 $sip_data_array['regseconds'] = NULL;
385 $sip_data_array['ipaddr'] = $s_ip;
386 $sip_data_array['regexten'] = NULL;
387 $sip_data_array['cancallforward']=NULL;
389 // Get selected Macro Parameter and create parameter entry
390 if(isset($this->macroarray[$this->macro])){
391 foreach($this->macroarray[$this->macro] as $key => $val ){
392 $s_parameter .= $val['choosen']."|";
393 }
394 $s_parameter = preg_replace("/\|$/","",$s_parameter);
395 }
397 if($this->is_number_used()){
398 $this->generate_error = $this->is_number_used();
399 return false;
400 }
402 // Create new SIP entry ...
403 $sip_entry = $sip_data_array;
404 reset($newnums);
405 $i_new_key = key($newnums);
406 $sip_entry['callerid'] =$newnums[$i_new_key];
407 $sip_entry['mailbox'] =$newnums[$i_new_key];
409 if((isset($this->parent))&&(isset($this->parent->by_object['mailAccount']))&&($this->parent->by_object['mailAccount']->is_account==true)){
410 $s_mail = $this->parent->by_object['mailAccount']->mail;
411 }else{
412 $s_mail = "";
413 }
415 // $SQL contains all queries
416 $SQL = array();
417 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
418 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
419 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$newnums[$i_new_key].";";
420 // Delete old entries
421 $b_first_deleted =false;
422 foreach($oldnums as $s_telenums){
423 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
424 if(!$b_first_deleted){
425 $b_first_deleted=true;
426 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$s_telenums.";";
427 }
428 }
430 if($this->goFonHardware=="automatic"){
431 foreach($SQL as $query ){
432 mysql_query($query) ;
433 }
434 return;
435 }
437 // Generate Strings with keys and values
438 foreach($sip_entry as $s_sip_key=>$s_sip_val){
439 if($s_sip_val == NULL) continue;
440 $s_sip_values.="'".$s_sip_val."',";
441 $s_sip_keys .="`".$s_sip_key."`,";
442 }
443 // Remove last ,
444 $s_sip_values = preg_replace("/,$/","",$s_sip_values);
445 $s_sip_keys = preg_replace("/,$/","",$s_sip_keys);
447 // Append SIP Entry
448 $SQL[] ="INSERT INTO ".$a_SETUP['SIP_TABLE']." (".$s_sip_keys.") VALUES (".$s_sip_values.");";
450 /* If deletion starts from userslist, cn uid are not set */
451 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
452 $this->uid = $this->parent->by_object['user']->uid;
453 }
455 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
456 $this->cn = $this->parent->by_object['user']->cn;
457 }
459 if((!isset($this->cn))||(empty($this->cn))){
460 $CNname= $this->uid;
461 }else{
462 $CNname= $this->cn;
463 }
465 $SQL[]= "INSERT INTO ".$a_SETUP['VOICE_TABLE']." (`customer_id`,`context`,`mailbox`,`password`,`fullname`,`email`,`pager`)
466 VALUES ('".$newnums[$i_new_key]."',
467 'default',
468 '".$newnums[$i_new_key]."',
469 '".$this->goFonVoicemailPIN."',
470 '".$CNname."',
471 '".$s_mail."',
472 '');";
473 $i_is_accounted=false;
475 $i = 0;
477 $is_inserted_once = false;
479 // Entension entries Hint / Dial / Goto
480 foreach($newnums as $s_telenums){
482 if(!$is_inserted_once){
483 $is_inserted_once = true;
484 $EXT[$i]['context'] = 'GOsa';
485 $EXT[$i]['exten'] = $this->uid;
486 $EXT[$i]['priority']= 1;
487 $EXT[$i]['app'] = "Goto";
488 $EXT[$i]['appdata'] = $s_telenums."|1";
489 $i ++;
490 }
491 /* Hint Entry */
492 $EXT[$i]['context'] = 'GOsa';
493 $EXT[$i]['exten'] = $s_telenums;
494 $EXT[$i]['priority']= "Hint";
495 $EXT[$i]['app'] = 'SIP/'.$this->uid;
496 $i ++;
497 /* SetCID */
498 //$EXT[$i]['context'] = 'GOsa';
499 //$EXT[$i]['exten'] = $s_telenums;
500 //$EXT[$i]['priority']= 1;
501 //$EXT[$i]['app'] = "SetCIDName";
502 //$EXT[$i]['appdata'] = $CNname;
503 //$i ++;
505 // If no macro is selected use Dial
506 if($this->macro!="none"){
507 $macroname = preg_replace("/,.*$/","",$this->macro);
508 $macroname = preg_replace("/^.*=/","",$macroname);
509 $s_app = "Macro";$macroname;
510 $s_par = $macroname."|".$s_parameter;
511 }else{
512 $s_app = "Dial";
513 $s_par = 'SIP/'.$this->uid."|20|r";
514 }
516 $EXT[$i]['context'] = 'GOsa';
517 $EXT[$i]['exten'] = $s_telenums;
518 $EXT[$i]['priority']= 1;
519 $EXT[$i]['app'] = $s_app;
520 $EXT[$i]['appdata'] = $s_par;
521 $i ++;
523 }
525 // Append all these Entries
526 foreach($EXT as $entr){
527 $SQL_syn = "INSERT INTO ".$a_SETUP['EXT_TABLE']." (";
528 foreach($entr as $key2 => $val2){
529 $SQL_syn.= "`".$key2."`,";
530 }
531 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
532 $SQL_syn .= ") VALUES (";
533 foreach($entr as $key2 => $val2){
534 $SQL_syn .= "'".$val2."',";
535 }
536 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
537 $SQL_syn .=");\n";
538 $SQL[] =$SQL_syn;
539 $SQL_syn ="";
540 }
542 // Perform queries ...
543 foreach($SQL as $query){
544 if(!@mysql_query($query,$r_con)){
545 print_red(_("Error while performing query:")." ".mysql_error());
546 return false;
547 }
548 }
549 }
550 @mysql_close($r_con);
551 return true;
552 }
555 function execute()
556 {
557 $display = "";
559 if(empty($this->macro)&&(!empty($this->goFonMacro))){
561 /* Go through already saved values, for a parameter */
562 $tmp = split("!",$this->goFonMacro);
564 /* it is possible that nothing has been saved yet */
565 if(is_array($tmp)){
567 /* First value is the macroname */
568 $this->macro = $tmp[0];
570 /* Macroname saved, delete that index */
571 unset($tmp[0]);
573 /* Check if makro has been removed */
574 if(!isset($this->macroarray[$this->macro])){
575 $this->macrostillavailable = false;
576 }else{
577 $this->macrostillavailable = true;
578 }
580 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
581 foreach($tmp as $var){
583 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
584 $varar = split("#",$var);
586 /* Only insert if the parameter still exists */
587 if(isset($this->macroarray[$this->macro][$varar[0]])){
588 /* Assign value */
589 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
590 }
591 }
592 }
593 }
595 /* Do we represent a valid account? */
596 if (!$this->is_account && $this->parent == NULL){
597 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
598 _("This account has no phone extensions.")."</b>";
599 $display.= back_to_main();
600 return ($display);
601 }
603 /* Do we need to flip is_account state? */
604 if (isset($_POST['modify_state'])){
605 $this->is_account= !$this->is_account;
606 }
608 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
609 if(empty($this->macro)){
610 $this->macro ="none";
611 }
613 /* Prepare templating */
614 $smarty= get_smarty();
616 /* tell user that the pluging selected is no longer available*/
617 if((!$this->macrostillavailable)&&($this->macro!="none")){
618 print_red(_("The macro you selected, is no longer available for you, please choose another one."));
619 }
621 /* Assing macroselectbox values */
622 $smarty->assign("macros",$this->macros);
623 $smarty->assign("macro", $this->macro);
625 /* check if there is a FON server created */
626 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
627 print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer).\nYour Settings can't be saved to asterisk Database."));
628 }
630 /* Create parameter table, skip if no parameters given */
631 if(!isset($this->macroarray[$this->macro])){
632 $macrotab="";
633 }else{
635 $macrotab ="<table summary=\""._("Parameter")."\">";
636 /* for every single parameter-> display textfile,combo, or true false switch*/
639 /* Automatic fill out */
640 if(isset($_POST['fillout'])){
642 foreach($this->phoneNumbers as $phonenum){
643 $tmp[] = $phonenum;
644 }
646 /* Go through all params */
647 foreach($this->macroarray[$this->macro] as $key => $paras){
649 $string = $paras['default'];
651 $string=preg_replace("/%uid/i",$this->uid,$string);
653 if(isset($this->cn)){
654 $string=preg_replace("/%cn/i",$this->cn,$string);
655 }
657 for($i = 0 ; $i < 10; $i++){
658 if(isset($tmp[$i])){
659 $string = preg_replace("/%telephoneNumber_".($i+1)."/i",$tmp[$i],$string);
660 }
661 }
663 $this->macroarray[$this->macro][$key]['choosen']=$string;
664 }
665 }
667 foreach($this->macroarray[$this->macro] as $paras){
669 /* get al vars */
670 $var = $paras['var'];
671 $name = $paras['name'];
672 $default = $paras['default'];
673 $type = $paras['type'];
674 $choosen = $paras['choosen'] ;
675 $str = $default;
677 /* in case of a combo box display a combobox with selected attr */
678 $macrotab.= "<tr>";
679 switch ($type){
681 case "combo":
682 $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro")." ".chkacl($this->acl, "goFonMacro").">";
683 foreach(split(":",$default) as $choice){
684 if($choosen==$choice){
685 $str.= "\n<option value='".$choice."' selected>".$choice." </option>";
686 }else{
687 $str.= "\n<option value='".$choice."'>".$choice." </option>";
688 }
689 }
690 $str.="</select>";
691 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
692 break;
694 case "bool":
695 if(!$choosen){
696 $str="\n<input type='checkbox' name='".$var."' value='1' ".chkacl($this->acl, "goFonMacro")." >";
697 }else{
698 $str="\n<input type='checkbox' name='".$var."' value='1' checked ".chkacl($this->acl, "goFonMacro").">";
699 }
700 $macrotab.= "<td colspan='2'>$str ".base64_decode($name)."";
701 break;
703 case "string":
704 $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro")." style='width:340px;'>";
705 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
706 break;
708 }
709 $macrotab.= "</td></tr>";
711 }
712 $macrotab.="</table><input name='post_success' type='hidden' value='1'>";
713 }//is_array()
715 /* Give smarty the table */
716 $smarty->assign("macrotab",$macrotab);
718 /* Do we represent a valid account? */
719 if (!$this->is_account && $this->parent == NULL){
720 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
721 _("This account has no phone extensions.")."</b>";
722 $display.= back_to_main();
723 return($display);
724 }
726 $display= "";
728 /* Show tab dialog headers */
729 if ($this->parent != NULL){
730 if ($this->is_account){
731 $display= $this->show_header(_("Remove phone account"),
732 _("This account has phone features enabled. You can disable them by clicking below."));
733 } else {
734 if(empty($this->uid)){
735 $display= $this->show_header(_("Create phone account"),
736 _("This account has phone features disabled. You can't enable them while no uid is set."),TRUE,TRUE);
737 }else{
738 $display= $this->show_header(_("Create phone account"),
739 _("This account has phone features disabled. You can enable them by clicking below."));
740 }
741 return ($display);
742 }
743 }
745 /* Add phone number */
746 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
747 if (is_phone_nr($_POST['phonenumber'])){
748 $number= $_POST["phonenumber"];
749 $this->phoneNumbers[$number]= $number;
750 $this->is_modified= TRUE;
751 } else {
752 print_red(_("Please enter a valid phone number!"));
753 }
754 }
756 /* Remove phone number */
757 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
758 foreach ($_POST['phonenumber_list'] as $number){
759 unset($this->phoneNumbers[$number]);
760 $this->is_modified= TRUE;
761 }
762 }
764 /* Transfer ACL's */
765 foreach($this->attributes as $val){
766 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
767 if(isset($this->$val)){
768 $smarty->assign($val,$this->$val);
769 }else{
770 $smarty->assign($val,"");
771 }
772 }
774 /* Fill arrays */
775 $smarty->assign ("goFonHardware", $this->goFonHardware);
776 if (!count($this->phoneNumbers)){
777 $smarty->assign ("phoneNumbers", array(""));
778 } else {
779 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
780 }
781 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
782 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
783 foreach ($this->hardware_list as $cn => $description){
784 if ($cn == $this->goFonHardware){
785 $selected= "selected";
786 } else {
787 $selected= "";
788 }
789 if (isset($this->used_hardware[$cn])){
790 $color= "style=\"color:#A0A0A0\"";
791 } else {
792 $color= "";
793 }
794 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description </option>\n";
795 }
796 $hl.= "</select>\n";
797 $smarty->assign ("hardware_list", $hl);
799 /* Show main page */
801 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
802 return($display);
803 }
806 function save_object()
807 {
808 if (isset($_POST["phoneTab"])){
809 plugin::save_object();
811 /* Save checkbox */
812 if (isset($_POST['fon_to_mail'])){
813 $tmp= "[M]";
814 } else {
815 $tmp= "[]";
816 }
817 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
818 if ($this->goFonDeliveryMode != $tmp){
819 $this->is_modified= TRUE;
820 }
821 $this->goFonDeliveryMode= $tmp;
822 }
824 /* Every macro in the select box are available */
825 if((isset($_POST['macro']))){
826 $this->macrostillavailable=true;
827 }
829 if(is_array($this->phoneNumbers)){
830 foreach($this->phoneNumbers as $telenumms) {
831 $nummsinorder[]=$telenumms;
832 }
833 }else{
834 $nummsinorder=array("");
835 }
837 /* get all Postvars */
838 if(isset($this->macroarray[$this->macro])){
839 foreach($this->macroarray[$this->macro] as $key => $paras){
840 if(isset($_POST[$paras['var']])){
841 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
842 }
844 /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
845 We need this code below to read and save checkboxes correct
846 */
848 if(isset($_POST['post_success'])){
849 if($this->macroarray[$this->macro][$key]['type']=="bool"){
850 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
851 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
852 }else{
853 $this->macroarray[$this->macro][$key]['choosen']=false;
854 }
855 }
856 }
857 }
858 }
859 }
860 }
862 function check()
863 {
864 /* Reset message array */
865 $message= array();
867 if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
868 $message[]=(_("Voicemail PIN must be between 1-4 characters."));
869 }else{
870 if(preg_match("/[^0-9]/",$this->goFonVoicemailPIN)){
871 $message[]=(_("The specified Voicemail PIN contains invalid characters, only numeric values are allowed here."));
872 }
873 }
876 if((strlen($this->goFonPIN)!=4)){
877 $message[]=(_("Phone PIN must be 4 characters long."));
878 }else{
879 if(preg_match("/[^0-9]/",$this->goFonPIN)){
880 $message[]=(_("The specified phone PIN contains invalid characters, only numeric values are allowed here."));
881 }
882 }
884 if(!$this->generate_mysql_entension_entries()){
885 $message[] = $this->generate_error;
886 }
888 /* We need at least one phone number */
889 if (count($this->phoneNumbers) == 0){
890 $message[]= sprintf(_("You need to specify at least one phone number!"));
891 }
893 /* check for ! in any parameter setting*/
894 if(isset($this->macroarray[$this->macro])){
895 foreach($this->macroarray[$this->macro] as $val){
896 if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
897 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
898 }
899 }
900 }
901 return ($message);
902 }
906 function save()
907 {
908 plugin::save();
910 /* Save arrays */
911 $this->attrs['telephoneNumber']= array();
912 foreach ($this->phoneNumbers as $number){
913 $this->attrs['telephoneNumber'][]= $number;
914 }
916 /* Save settings, or remove goFonMacro attribute*/
917 if($this->macro!="none"){
918 $this->attrs['goFonMacro']=$this->macro;
919 if(isset($this->macroarray[$this->macro])){
920 foreach($this->macroarray[$this->macro] as $paras) {
921 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
922 }
923 }
924 }else{
925 $this->attrs['goFonMacro']=array();
926 }
927 unset($this->attrs['macro']) ;
929 $this->attrs['goFonForwarding']=array();
931 $this->generate_mysql_entension_entries(true);
933 if($this->attrs['goFonMacro']==""){
934 $this->attrs['goFonMacro']=array();
935 }
937 unset($this->attrs['cn']);
939 /* Write back to ldap */
940 $ldap= $this->config->get_ldap_link();
941 $ldap->cd($this->dn);
942 $ldap->modify($this->attrs);
943 show_ldap_error($ldap->get_error());
945 /* Optionally execute a command after we're done */
947 if ($this->initially_was_account == $this->is_account){
948 if ($this->is_modified){
949 $this->handle_post_events("modify");
950 }
951 } else {
952 $this->handle_post_events("add");
953 }
955 }
958 function insert_after($entry, $nr, $list)
959 {
960 /* Is the entry free? No? Make it free... */
961 if (isset($list[$nr])) {
962 $dest= array();
963 $newidx= 0;
965 foreach ($list as $idx => $contents){
966 $dest[$newidx++]= $contents;
967 if ($idx == $nr){
968 $dest[$newidx++]= $entry;
969 }
970 }
971 } else {
972 $dest= $list;
973 $dest[$nr]= $entry;
974 }
976 return ($dest);
977 }
980 function adapt_from_template($dn)
981 {
982 plugin::adapt_from_template($dn);
984 /* Assemble phone numbers */
985 if (isset($this->attrs['telephoneNumber'])){
986 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
987 $number= $this->attrs['telephoneNumber'][$i];
988 $this->phoneNumbers[$number]= $number;
989 }
990 }
991 }
994 function remove_from_parent()
995 {
997 foreach($this->attributes as $key=>$val){
998 if(in_array($val,array("uid","cn","mail"))){
999 unset($this->attributes[$key]);
1000 unset($this->$val);
1001 }
1002 }
1004 if(array_key_exists('config', $_SESSION) &&
1005 array_key_exists('SERVERS', $_SESSION['config']->data) &&
1006 array_key_exists('FON', $_SESSION['config']->data['SERVERS'])) {
1007 // Get Configuration for Mysql database Server
1008 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
1009 $s_parameter ="";
1011 // Connect to DB server
1012 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
1014 // Check if we are connected correctly
1015 if(!$r_con){
1016 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
1017 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
1018 gosa_log(@mysql_error());
1019 return false;
1020 }
1022 // Select database for Extensions
1023 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
1025 // Test if we have the database selected correctly
1026 if(!$db){
1027 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
1028 gosa_log(@mysql_error());
1029 return false;
1030 }
1032 $SQL="";
1034 /* If deletion starts from userslist, cn uid are not set */
1035 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
1036 $this->uid = $this->parent->by_object['user']->uid;
1037 }
1039 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
1040 $this->cn = $this->parent->by_object['user']->cn;
1041 }
1043 $first_num = false;
1044 // Delete old entries
1045 foreach($this->a_old_telenums as $s_telenums){
1046 if(!$first_num){
1047 $first_num = $s_telenums;
1048 }
1049 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
1050 }
1052 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$first_num."';";
1053 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
1054 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
1057 foreach($SQL as $query){
1058 if(!@mysql_query($query,$r_con)){
1059 print_red(_("Stop".mysql_error()));
1060 return false;
1061 }
1062 }
1063 }
1066 /* unset macro attr, it will cause an error */
1067 $tmp = array_flip($this->attributes);
1068 unset($tmp['macro']);
1069 $this->attributes=array_flip($tmp);
1071 /* Cancel if there's nothing to do here */
1072 if (!$this->initially_was_account){
1073 return;
1074 }
1076 plugin::remove_from_parent();
1078 /* Just keep one phone number */
1079 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
1080 $this->attrs['telephoneNumber']= $this->telephoneNumber;
1081 } else {
1082 $this->attrs['telephoneNumber']= array();
1083 }
1085 $ldap= $this->config->get_ldap_link();
1086 $ldap->cd($this->config->current['BASE']);
1087 $ldap->search("(objectClass=goFonQueue)", array("member"));
1088 while($attr = $ldap->fetch()){
1089 if(in_array($this->dn,$attr['member'])){
1090 $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
1091 unset($new->by_object['ogroup']->memberList[$this->dn]);
1092 unset($new->by_object['ogroup']->member[$this->dn]);
1093 $new->save();
1094 print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->attrs['cn']));
1095 }
1096 }
1097 $ldap->cd($this->dn);
1098 $ldap->modify($this->attrs);
1099 show_ldap_error($ldap->get_error());
1101 /* Optionally execute a command after we're done */
1102 @mysql_close($r_con);
1103 $this->handle_post_events('remove');
1104 }
1108 /* This function checks if the given phonenumbers are available or already in use*/
1109 function is_number_used()
1110 {
1111 $ldap= $this->config->get_ldap_link();
1112 $ldap->cd($this->config->current['BASE']);
1113 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
1114 while($attrs = $ldap->fetch()) {
1115 unset($attrs['telephoneNumber']['count']);
1116 foreach($attrs['telephoneNumber'] as $tele){
1117 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
1118 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
1119 $numbers[$tele]=$attrs;
1120 }
1121 }
1123 foreach($this->phoneNumbers as $num){
1124 if(!isset($this->cn)) $this->cn = "";
1126 if((isset($numbers[$num]))&&(($numbers[$num]['uid'][0]!=$this->uid))){
1127 if(isset($numbers[$num]['uid'][0])){
1128 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
1129 }else{
1130 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
1131 }
1132 }
1133 }
1134 }
1135 }
1137 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1138 ?>