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 // Attribute GoFonDefaultIP set ?
314 if(((isset($a_ldap_attrs['goFonDefaultIP'][0]))&&($a_ldap_attrs['goFonDefaultIP'][0] != "dynamic"))){
315 $s_ip = $a_ldap_attrs['goFonDefaultIP'][0];
316 $s_host = $s_ip;
317 }else{
318 $s_ip = NULL;
319 $s_host = "dynamic";
320 }
322 // Attribute GoFonQualify set ?
323 if(isset($a_ldap_attrs['goFonQualify'])){
324 $s_qualify = $a_ldap_attrs['goFonQualify'][0];
325 }
327 // Attribute GoFonPIN set ?
328 if(isset($this->goFonPIN)){
329 $s_pin = $this->goFonPIN;
330 }
332 // Attribute GoFonType set ?
333 if(isset($a_ldap_attrs['goFonType'])){
334 $s_type = $a_ldap_attrs['goFonType'][0];
335 }
337 if(isset($a_ldap_attrs['goFonDmtfMode'][0])){
338 $sip_data_array['dtmfmode'] = $a_ldap_attrs['goFonDmtfMode'][0];
339 }else{
340 $sip_data_array['dtmfmode'] ="rfc2833";
341 }
343 // generate SIP entry
344 $sip_data_array['id'] = "";
345 $sip_data_array['name'] = $this->uid;
346 $sip_data_array['accountcode'] = NULL;
347 $sip_data_array['amaflags'] = NULL;
348 $sip_data_array['callgroup'] = NULL;
349 $sip_data_array['callerid'] = "";
350 $sip_data_array['canreinvite'] = "no";
351 $sip_data_array['context'] = "default";
352 $sip_data_array['defaultip'] = NULL;
353 $sip_data_array['fromuser'] = NULL;
354 $sip_data_array['fromdomain'] = NULL;
355 $sip_data_array['host'] = $s_host;
356 $sip_data_array['insecure'] = NULL;
357 $sip_data_array['language'] = NULL;
358 $sip_data_array['mailbox'] = "asterisk";
359 $sip_data_array['md5secret'] = NULL;
360 $sip_data_array['nat'] = "no";
361 $sip_data_array['permit'] = NULL;
362 $sip_data_array['deny'] = NULL;
363 $sip_data_array['mask'] = NULL;
364 $sip_data_array['pickupgroup'] = NULL;
365 $sip_data_array['port'] = NULL;
366 $sip_data_array['qualify'] = $s_qualify;
367 $sip_data_array['restrictcid'] = "n";
368 $sip_data_array['rtptimeout'] = NULL;
369 $sip_data_array['rtpholdtimeout']=NULL;
370 $sip_data_array['secret'] = $this->goFonPIN;
371 $sip_data_array['type'] = $s_type ;
372 $sip_data_array['username'] = $this->uid;
373 $sip_data_array['disallow'] = NULL;
374 $sip_data_array['allow'] = NULL;
375 $sip_data_array['musiconhold'] = NULL;
376 $sip_data_array['regseconds'] = NULL;
377 $sip_data_array['ipaddr'] = $s_ip;
378 $sip_data_array['regexten'] = NULL;
379 $sip_data_array['cancallforward']=NULL;
381 // Get selected Macro Parameter and create parameter entry
382 if(isset($this->macroarray[$this->macro])){
383 foreach($this->macroarray[$this->macro] as $key => $val ){
384 $s_parameter .= $val['choosen']."|";
385 }
386 $s_parameter = preg_replace("/\|$/","",$s_parameter);
387 }
389 if($this->is_number_used()){
390 $this->generate_error = $this->is_number_used();
391 return false;
392 }
394 // Create new SIP entry ...
395 $sip_entry = $sip_data_array;
396 reset($this->phoneNumbers);
397 $i_new_key = key($this->phoneNumbers);
398 $sip_entry['callerid'] =$this->phoneNumbers[$i_new_key];
399 $sip_entry['mailbox'] =$this->phoneNumbers[$i_new_key];
401 if((isset($this->parent))&&(isset($this->parent->by_object['mailAccount']))&&($this->parent->by_object['mailAccount']->is_account==true)){
402 $s_mail = $this->parent->by_object['mailAccount']->mail;
403 }else{
404 $s_mail = "";
405 }
408 // $SQL contains all queries
409 $SQL = array();
410 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
411 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
412 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$this->phoneNumbers[$i_new_key].";";
413 // Delete old entries
414 $b_first_deleted =false;
415 foreach($this->a_old_telenums as $s_telenums){
416 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
417 if(!$b_first_deleted){
418 $b_first_deleted=true;
419 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$s_telenums.";";
420 }
421 }
424 if($this->goFonHardware=="automatic"){
425 foreach($SQL as $query ){
426 mysql_query($query) ;
427 }
428 return;
429 }
431 // Generate Strings with keys and values
432 foreach($sip_entry as $s_sip_key=>$s_sip_val){
433 if($s_sip_val == NULL) continue;
434 $s_sip_values.="'".$s_sip_val."',";
435 $s_sip_keys .="`".$s_sip_key."`,";
436 }
437 // Remove last ,
438 $s_sip_values = preg_replace("/,$/","",$s_sip_values);
439 $s_sip_keys = preg_replace("/,$/","",$s_sip_keys);
441 // Append SIP Entry
442 $SQL[] ="INSERT INTO ".$a_SETUP['SIP_TABLE']." (".$s_sip_keys.") VALUES (".$s_sip_values.");";
444 /* If deletion starts from userslist, cn uid are not set */
445 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
446 $this->uid = $this->parent->by_object['user']->uid;
447 }
449 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
450 $this->cn = $this->parent->by_object['user']->cn;
451 }
453 if((!isset($this->cn))||(empty($this->cn))){
454 $CNname= $this->uid;
455 }else{
456 $CNname= $this->cn;
457 }
459 $SQL[]= "INSERT INTO ".$a_SETUP['VOICE_TABLE']." (`customer_id`,`context`,`mailbox`,`password`,`fullname`,`email`,`pager`)
460 VALUES ('".$this->phoneNumbers[$i_new_key]."',
461 'default',
462 '".$this->phoneNumbers[$i_new_key]."',
463 '".$this->goFonVoicemailPIN."',
464 '".$CNname."',
465 '".$s_mail."',
466 '');";
467 $i_is_accounted=false;
469 $i = 0;
471 $is_inserted_once = false;
473 // Entension entries Hint / Dial / Goto
474 foreach($this->phoneNumbers as $s_telenums){
476 if(!$is_inserted_once){
477 $is_inserted_once = true;
478 $EXT[$i]['context'] = 'GOsa';
479 $EXT[$i]['exten'] = $this->uid;
480 $EXT[$i]['priority']= 1;
481 $EXT[$i]['app'] = "Goto";
482 $EXT[$i]['appdata'] = $s_telenums."|1";
483 $i ++;
484 }
485 /* Hint Entry */
486 $EXT[$i]['context'] = 'GOsa';
487 $EXT[$i]['exten'] = $s_telenums;
488 $EXT[$i]['priority']= "Hint";
489 $EXT[$i]['app'] = 'SIP/'.$this->uid;
490 $i ++;
491 /* SetCID */
492 //$EXT[$i]['context'] = 'GOsa';
493 //$EXT[$i]['exten'] = $s_telenums;
494 //$EXT[$i]['priority']= 1;
495 //$EXT[$i]['app'] = "SetCIDName";
496 //$EXT[$i]['appdata'] = $CNname;
497 //$i ++;
499 // If no macro is selected use Dial
500 if($this->macro!="none"){
501 $macroname = preg_replace("/,.*$/","",$this->macro);
502 $macroname = preg_replace("/^.*=/","",$macroname);
503 $s_app = "Macro";$macroname;
504 $s_par = $macroname."|".$s_parameter;
505 }else{
506 $s_app = "Dial";
507 $s_par = 'SIP/'.$this->uid."|20|r";
508 }
510 $EXT[$i]['context'] = 'GOsa';
511 $EXT[$i]['exten'] = $s_telenums;
512 $EXT[$i]['priority']= 1;
513 $EXT[$i]['app'] = $s_app;
514 $EXT[$i]['appdata'] = $s_par;
515 $i ++;
517 }
519 // Append all these Entries
520 foreach($EXT as $entr){
521 $SQL_syn = "INSERT INTO ".$a_SETUP['EXT_TABLE']." (";
522 foreach($entr as $key2 => $val2){
523 $SQL_syn.= "`".$key2."`,";
524 }
525 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
526 $SQL_syn .= ") VALUES (";
527 foreach($entr as $key2 => $val2){
528 $SQL_syn .= "'".$val2."',";
529 }
530 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
531 $SQL_syn .=");\n";
532 $SQL[] =$SQL_syn;
533 $SQL_syn ="";
534 }
536 // Perform queries ...
537 foreach($SQL as $query){
538 if(!@mysql_query($query,$r_con)){
539 print_red(_("Error while performing query:")." ".mysql_error());
540 return false;
541 }
542 }
543 }
544 @mysql_close($r_con);
545 return true;
546 }
549 function execute()
550 {
551 $display = "";
553 if(empty($this->macro)&&(!empty($this->goFonMacro))){
555 /* Go through already saved values, for a parameter */
556 $tmp = split("!",$this->goFonMacro);
558 /* it is possible that nothing has been saved yet */
559 if(is_array($tmp)){
561 /* First value is the macroname */
562 $this->macro = $tmp[0];
564 /* Macroname saved, delete that index */
565 unset($tmp[0]);
567 /* Check if makro has been removed */
568 if(!isset($this->macroarray[$this->macro])){
569 $this->macrostillavailable = false;
570 }else{
571 $this->macrostillavailable = true;
572 }
574 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
575 foreach($tmp as $var){
577 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
578 $varar = split("#",$var);
580 /* Only insert if the parameter still exists */
581 if(isset($this->macroarray[$this->macro][$varar[0]])){
582 /* Assign value */
583 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
584 }
585 }
586 }
587 }
589 /* Do we represent a valid account? */
590 if (!$this->is_account && $this->parent == NULL){
591 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
592 _("This account has no phone extensions.")."</b>";
593 $display.= back_to_main();
594 return ($display);
595 }
597 /* Do we need to flip is_account state? */
598 if (isset($_POST['modify_state'])){
599 $this->is_account= !$this->is_account;
600 }
602 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
603 if(empty($this->macro)){
604 $this->macro ="none";
605 }
607 /* Prepare templating */
608 $smarty= get_smarty();
610 /* tell user that the pluging selected is no longer available*/
611 if((!$this->macrostillavailable)&&($this->macro!="none")){
612 print_red(_("The macro you selected, is no longer available for you, please choose another one."));
613 }
615 /* Assing macroselectbox values */
616 $smarty->assign("macros",$this->macros);
617 $smarty->assign("macro", $this->macro);
619 /* check if there is a FON server created */
620 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
621 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."));
622 }
624 /* Create parameter table, skip if no parameters given */
625 if(!isset($this->macroarray[$this->macro])){
626 $macrotab="";
627 }else{
629 $macrotab ="<table summary=\""._("Parameter")."\">";
630 /* for every single parameter-> display textfile,combo, or true false switch*/
633 /* Automatic fill out */
634 if(isset($_POST['fillout'])){
636 foreach($this->phoneNumbers as $phonenum){
637 $tmp[] = $phonenum;
638 }
640 /* Go through all params */
641 foreach($this->macroarray[$this->macro] as $key => $paras){
643 $string = $paras['default'];
645 $string=preg_replace("/%uid/i",$this->uid,$string);
647 if(isset($this->cn)){
648 $string=preg_replace("/%cn/i",$this->cn,$string);
649 }
651 for($i = 0 ; $i < 10; $i++){
652 if(isset($tmp[$i])){
653 $string = preg_replace("/%telephoneNumber_".($i+1)."/i",$tmp[$i],$string);
654 }
655 }
657 $this->macroarray[$this->macro][$key]['choosen']=$string;
658 }
659 }
661 foreach($this->macroarray[$this->macro] as $paras){
663 /* get al vars */
664 $var = $paras['var'];
665 $name = $paras['name'];
666 $default = $paras['default'];
667 $type = $paras['type'];
668 $choosen = $paras['choosen'] ;
669 $str = $default;
671 /* in case of a combo box display a combobox with selected attr */
672 $macrotab.= "<tr>";
673 switch ($type){
675 case "combo":
676 $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro")." ".chkacl($this->acl, "goFonMacro").">";
677 foreach(split(":",$default) as $choice){
678 if($choosen==$choice){
679 $str.= "\n<option value='".$choice."' selected>".$choice." </option>";
680 }else{
681 $str.= "\n<option value='".$choice."'>".$choice." </option>";
682 }
683 }
684 $str.="</select>";
685 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
686 break;
688 case "bool":
689 if(!$choosen){
690 $str="\n<input type='checkbox' name='".$var."' value='1' ".chkacl($this->acl, "goFonMacro")." >";
691 }else{
692 $str="\n<input type='checkbox' name='".$var."' value='1' checked ".chkacl($this->acl, "goFonMacro").">";
693 }
694 $macrotab.= "<td colspan='2'>$str ".base64_decode($name)."";
695 break;
697 case "string":
698 $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro")." style='width:340px;'>";
699 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
700 break;
702 }
703 $macrotab.= "</td></tr>";
705 }
706 $macrotab.="</table><input name='post_success' type='hidden' value='1'>";
707 }//is_array()
709 /* Give smarty the table */
710 $smarty->assign("macrotab",$macrotab);
712 /* Do we represent a valid account? */
713 if (!$this->is_account && $this->parent == NULL){
714 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
715 _("This account has no phone extensions.")."</b>";
716 $display.= back_to_main();
717 return($display);
718 }
720 $display= "";
722 /* Show tab dialog headers */
723 if ($this->parent != NULL){
724 if ($this->is_account){
725 $display= $this->show_header(_("Remove phone account"),
726 _("This account has phone features enabled. You can disable them by clicking below."));
727 } else {
728 if(empty($this->uid)){
729 $display= $this->show_header(_("Create phone account"),
730 _("This account has phone features disabled. You can't enable them while no uid is set."),TRUE,TRUE);
731 }else{
732 $display= $this->show_header(_("Create phone account"),
733 _("This account has phone features disabled. You can enable them by clicking below."));
734 }
735 return ($display);
736 }
737 }
739 /* Add phone number */
740 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
741 if (is_phone_nr($_POST['phonenumber'])){
742 $number= $_POST["phonenumber"];
743 $this->phoneNumbers[$number]= $number;
744 $this->is_modified= TRUE;
745 } else {
746 print_red(_("Please enter a valid phone number!"));
747 }
748 }
750 /* Remove phone number */
751 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
752 foreach ($_POST['phonenumber_list'] as $number){
753 unset($this->phoneNumbers[$number]);
754 $this->is_modified= TRUE;
755 }
756 }
758 /* Transfer ACL's */
759 foreach($this->attributes as $val){
760 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
761 if(isset($this->$val)){
762 $smarty->assign($val,$this->$val);
763 }else{
764 $smarty->assign($val,"");
765 }
766 }
768 /* Fill arrays */
769 $smarty->assign ("goFonHardware", $this->goFonHardware);
770 if (!count($this->phoneNumbers)){
771 $smarty->assign ("phoneNumbers", array(""));
772 } else {
773 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
774 }
775 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
776 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
777 foreach ($this->hardware_list as $cn => $description){
778 if ($cn == $this->goFonHardware){
779 $selected= "selected";
780 } else {
781 $selected= "";
782 }
783 if (isset($this->used_hardware[$cn])){
784 $color= "style=\"color:#A0A0A0\"";
785 } else {
786 $color= "";
787 }
788 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description </option>\n";
789 }
790 $hl.= "</select>\n";
791 $smarty->assign ("hardware_list", $hl);
793 /* Show main page */
795 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
796 return($display);
797 }
800 function save_object()
801 {
802 if (isset($_POST["phoneTab"])){
803 plugin::save_object();
805 /* Save checkbox */
806 if (isset($_POST['fon_to_mail'])){
807 $tmp= "[M]";
808 } else {
809 $tmp= "[]";
810 }
811 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
812 if ($this->goFonDeliveryMode != $tmp){
813 $this->is_modified= TRUE;
814 }
815 $this->goFonDeliveryMode= $tmp;
816 }
818 /* Every macro in the select box are available */
819 if((isset($_POST['macro']))){
820 $this->macrostillavailable=true;
821 }
823 if(is_array($this->phoneNumbers)){
824 foreach($this->phoneNumbers as $telenumms) {
825 $nummsinorder[]=$telenumms;
826 }
827 }else{
828 $nummsinorder=array("");
829 }
831 /* get all Postvars */
832 if(isset($this->macroarray[$this->macro])){
833 foreach($this->macroarray[$this->macro] as $key => $paras){
834 if(isset($_POST[$paras['var']])){
835 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
836 }
838 /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
839 We need this code below to read and save checkboxes correct
840 */
842 if(isset($_POST['post_success'])){
843 if($this->macroarray[$this->macro][$key]['type']=="bool"){
844 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
845 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
846 }else{
847 $this->macroarray[$this->macro][$key]['choosen']=false;
848 }
849 }
850 }
851 }
852 }
853 }
854 }
856 function check()
857 {
858 /* Reset message array */
859 $message= array();
861 if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
862 $message[]=(_("Voicemail PIN must be between 1-4 characters."));
863 }else{
864 if(preg_match("/[^0-9]/",$this->goFonVoicemailPIN)){
865 $message[]=(_("The specified Voicemail PIN contains invalid characters, only numeric values are allowed here."));
866 }
867 }
870 if((strlen($this->goFonPIN)!=4)){
871 $message[]=(_("Phone PIN must be 4 characters long."));
872 }else{
873 if(preg_match("/[^0-9]/",$this->goFonPIN)){
874 $message[]=(_("The specified phone PIN contains invalid characters, only numeric values are allowed here."));
875 }
876 }
878 if(!$this->generate_mysql_entension_entries()){
879 $message[] = $this->generate_error;
880 }
882 /* We need at least one phone number */
883 if (count($this->phoneNumbers) == 0){
884 $message[]= sprintf(_("You need to specify at least one phone number!"));
885 }
887 /* check for ! in any parameter setting*/
888 if(isset($this->macroarray[$this->macro])){
889 foreach($this->macroarray[$this->macro] as $val){
890 if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
891 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
892 }
893 }
894 }
895 return ($message);
896 }
900 function save()
901 {
902 plugin::save();
904 /* Save arrays */
905 $this->attrs['telephoneNumber']= array();
906 foreach ($this->phoneNumbers as $number){
907 $this->attrs['telephoneNumber'][]= $number;
908 }
910 /* Save settings, or remove goFonMacro attribute*/
911 if($this->macro!="none"){
912 $this->attrs['goFonMacro']=$this->macro;
913 if(isset($this->macroarray[$this->macro])){
914 foreach($this->macroarray[$this->macro] as $paras) {
915 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
916 }
917 }
918 }else{
919 $this->attrs['goFonMacro']=array();
920 }
921 unset($this->attrs['macro']) ;
923 $this->attrs['goFonForwarding']=array();
925 $this->generate_mysql_entension_entries(true);
927 if($this->attrs['goFonMacro']==""){
928 $this->attrs['goFonMacro']=array();
929 }
931 unset($this->attrs['cn']);
933 /* Write back to ldap */
934 $ldap= $this->config->get_ldap_link();
935 $ldap->cd($this->dn);
936 $ldap->modify($this->attrs);
937 show_ldap_error($ldap->get_error());
939 /* Optionally execute a command after we're done */
941 if ($this->initially_was_account == $this->is_account){
942 if ($this->is_modified){
943 $this->handle_post_events("modify");
944 }
945 } else {
946 $this->handle_post_events("add");
947 }
949 }
952 function insert_after($entry, $nr, $list)
953 {
954 /* Is the entry free? No? Make it free... */
955 if (isset($list[$nr])) {
956 $dest= array();
957 $newidx= 0;
959 foreach ($list as $idx => $contents){
960 $dest[$newidx++]= $contents;
961 if ($idx == $nr){
962 $dest[$newidx++]= $entry;
963 }
964 }
965 } else {
966 $dest= $list;
967 $dest[$nr]= $entry;
968 }
970 return ($dest);
971 }
974 function adapt_from_template($dn)
975 {
976 plugin::adapt_from_template($dn);
978 /* Assemble phone numbers */
979 if (isset($this->attrs['telephoneNumber'])){
980 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
981 $number= $this->attrs['telephoneNumber'][$i];
982 $this->phoneNumbers[$number]= $number;
983 }
984 }
985 }
988 function remove_from_parent()
989 {
991 foreach($this->attributes as $key=>$val){
992 if(in_array($val,array("uid","cn","mail"))){
993 unset($this->attributes[$key]);
994 unset($this->$val);
995 }
996 }
998 if(array_key_exists('config', $_SESSION) &&
999 array_key_exists('SERVERS', $_SESSION['config']->data) &&
1000 array_key_exists('FON', $_SESSION['config']->data['SERVERS'])) {
1001 // Get Configuration for Mysql database Server
1002 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
1003 $s_parameter ="";
1005 // Connect to DB server
1006 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
1008 // Check if we are connected correctly
1009 if(!$r_con){
1010 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
1011 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
1012 gosa_log(@mysql_error());
1013 return false;
1014 }
1016 // Select database for Extensions
1017 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
1019 // Test if we have the database selected correctly
1020 if(!$db){
1021 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
1022 gosa_log(@mysql_error());
1023 return false;
1024 }
1026 $SQL="";
1028 /* If deletion starts from userslist, cn uid are not set */
1029 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
1030 $this->uid = $this->parent->by_object['user']->uid;
1031 }
1033 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
1034 $this->cn = $this->parent->by_object['user']->cn;
1035 }
1037 $first_num = false;
1038 // Delete old entries
1039 foreach($this->a_old_telenums as $s_telenums){
1040 if(!$first_num){
1041 $first_num = $s_telenums;
1042 }
1043 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
1044 }
1046 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$first_num."';";
1047 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
1048 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
1051 foreach($SQL as $query){
1052 if(!@mysql_query($query,$r_con)){
1053 print_red(_("Stop".mysql_error()));
1054 return false;
1055 }
1056 }
1057 }
1060 /* unset macro attr, it will cause an error */
1061 $tmp = array_flip($this->attributes);
1062 unset($tmp['macro']);
1063 $this->attributes=array_flip($tmp);
1065 /* Cancel if there's nothing to do here */
1066 if (!$this->initially_was_account){
1067 return;
1068 }
1070 plugin::remove_from_parent();
1072 /* Just keep one phone number */
1073 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
1074 $this->attrs['telephoneNumber']= $this->telephoneNumber;
1075 } else {
1076 $this->attrs['telephoneNumber']= array();
1077 }
1079 $ldap= $this->config->get_ldap_link();
1080 $ldap->cd($this->config->current['BASE']);
1081 $ldap->search("(objectClass=goFonQueue)", array("member"));
1082 while($attr = $ldap->fetch()){
1083 if(in_array($this->dn,$attr['member'])){
1084 $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
1085 unset($new->by_object['ogroup']->memberList[$this->dn]);
1086 unset($new->by_object['ogroup']->member[$this->dn]);
1087 $new->save();
1088 print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->attrs['cn']));
1089 }
1090 }
1091 $ldap->cd($this->dn);
1092 $ldap->modify($this->attrs);
1093 show_ldap_error($ldap->get_error());
1095 /* Optionally execute a command after we're done */
1096 @mysql_close($r_con);
1097 $this->handle_post_events('remove');
1098 }
1102 /* This function checks if the given phonenumbers are available or already in use*/
1103 function is_number_used()
1104 {
1105 $ldap= $this->config->get_ldap_link();
1106 $ldap->cd($this->config->current['BASE']);
1107 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
1108 while($attrs = $ldap->fetch()) {
1109 unset($attrs['telephoneNumber']['count']);
1110 foreach($attrs['telephoneNumber'] as $tele){
1111 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
1112 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
1113 $numbers[$tele]=$attrs;
1114 }
1115 }
1117 foreach($this->phoneNumbers as $num){
1118 if(!isset($this->cn)) $this->cn = "";
1120 if((isset($numbers[$num]))&&(($numbers[$num]['uid'][0]!=$this->uid))){
1121 if(isset($numbers[$num]['uid'][0])){
1122 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
1123 }else{
1124 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
1125 }
1126 }
1127 }
1128 }
1129 }
1131 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1132 ?>