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 unset($attrs['goFonMacroParameter']['count']);
138 /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
139 if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
141 foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
142 /* Split Data in readable values, by delimiter ! */
143 $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
145 /* Set all attrs */
146 $id = $data[0];
147 $this->macroarray[$attrs['dn']][$id]['var'] ="var".$id;
148 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
149 $this->macroarray[$attrs['dn']][$id]['id'] =$id;
150 $this->macroarray[$attrs['dn']][$id]['name'] =$data[1];
151 $this->macroarray[$attrs['dn']][$id]['type'] =$data[2];
152 $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
153 if($data[2] == "bool"){
154 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
155 }
156 }//foreach
157 }//is_array
158 }//visible = 1
159 }//while
161 /* Go through already saved values, for a parameter */
162 $tmp = split("!",$this->goFonMacro);
164 /* it is possible that nothing has been saved yet */
165 if(is_array($tmp)){
167 /* First value is the macroname */
168 $this->macro = $tmp[0];
170 /* Macroname saved, delete that index */
171 unset($tmp[0]);
173 /* Check if makro has been removed */
174 if(!isset($this->macroarray[$this->macro])){
175 $this->macrostillavailable = false;
176 }else{
177 $this->macrostillavailable = true;
178 }
180 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
181 foreach($tmp as $var){
183 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
184 $varar = split("#",$var);
186 /* Only insert if the parameter still exists */
187 if(isset($this->macroarray[$this->macro][$varar[0]])){
188 /* Assign value */
189 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
190 }
191 }
192 }
195 /* Eventually colorize phones */
196 $ldap->cd($this->config->current['BASE']);
197 foreach ($this->hardware_list as $cn => $desc){
198 $ldap->search("(goFonHardware=$cn)", array('cn'));
199 if ($ldap->count() > 0){
200 $ldap->fetch();
201 if ($ldap->getDN() != $this->dn){
202 $this->used_hardware[$cn]= $ldap->getDN();
203 }
204 }
205 }
206 $this->hardware_list["automatic"]= _("automatic");
207 ksort($this->hardware_list);
208 $this->a_old_telenums = $this->phoneNumbers;
210 if($this->is_account){
211 $this->is_modified = true;
212 }
215 /* Get voicemail PIN from MySQL DB
216 * Because every user can change his PIN directly from the phone
217 * without any update to the ldap
218 * This means, the PIN in the DB is up to date
219 */
220 // Connect to DB server
221 if((isset($a_SETUP))&&(isset($a_SETUP['SERVER']))&&(isset($a_SETUP['LOGIN']))&&(isset($a_SETUP['PASSWORD']))){
222 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
223 if($r_con){
224 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
225 $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."'"));
227 if((isset($vp[0]))&&(!empty($vp[0]))){
228 $this->goFonPINVoice = $vp[0];
229 }
230 }
231 }
232 @mysql_close($r_con) ;
233 }
236 // Generate MySQL Syntax
237 function generate_mysql_entension_entries($save = false){
239 // Get Configuration for Mysql database Server
240 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON']; // DB Configuration
241 $s_parameter = ""; // Contains paramter for selected Macro
242 $r_con = false; // DB connection
243 $r_db = false; // Selected DB
244 $r_res = false; // Result resource
245 $a_ldap_attrs = array(); //
247 $s_ip = NULL; // Contains ip for Sip entry
248 $s_host = NULL; // Contains host for Sip entry
249 $s_qualify = NULL; // Qualify entry
250 $s_pin = NULL; // Entry for secret
251 $s_type = NULL; // Entry for phone type (friend , peer ..)
253 $sip_data_array = array(); // Contains complete sip entry, to generate SQL syntax
254 $i_old_key = false; // Contains index for first old phonenumber, to delete old entries corectly
255 $i_new_key = false; // Contains index for first new phonenumber, to generate new entries corectly
257 $s_sip_values = ""; // Contains string with all values for given attributes in SQL syntax
258 $s_sip_keys = ""; // Contains all needed attributes to generate sip entry in DB
260 $s_sip_key = ""; // Key for SIP entry index
261 $s_sip_val = ""; // Value for SIP entry index
263 $b_first_deleted= false; // Only delete first entry,
264 $s_telenums = ""; // for each value variable
266 $i_is_accounted =false; // Ensure that extension entry, for name to number is only once in table
269 // Connect to DB server
270 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
272 // Check if we are connected correctly
273 if(!$r_con){
274 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
275 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
276 gosa_log(mysql_error());
277 return false;
278 }
280 // Select database for Extensions
281 $r_db = @mysql_select_db($a_SETUP['DB'],$r_con);
283 // Test if we have the database selected correctly
284 if(!$r_db){
285 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
286 gosa_log(mysql_error());
287 return false;
288 }
290 // Get phonehardware to setup sip entry
291 $ldap = $this->config->get_ldap_link();
292 $r_res = $ldap->search("(&(objectClass=goFonHardware)(cn=".$this->goFonHardware."))", array('*'));
293 $a_ldap_attrs = $ldap->fetch();
295 if($this->is_number_used()){
296 $this->generate_error = $this->is_number_used();
297 return false;
298 }
300 /* If Save == true, we should save something.
301 * Generate SQL, for drop of old entries
302 * Generate SQL, for insert new entries
303 */
304 if($save == true){
306 // Attribute GoFonDefaultIP set ?
307 if(((isset($a_ldap_attrs['goFonDefaultIP'][0]))&&($a_ldap_attrs['goFonDefaultIP'][0] != "dynamic"))){
308 $s_ip = $a_ldap_attrs['goFonDefaultIP'][0];
309 $s_host = $s_ip;
310 }else{
311 $s_ip = NULL;
312 $s_host = "dynamic";
313 }
315 // Attribute GoFonQualify set ?
316 if(isset($a_ldap_attrs['goFonQualify'])){
317 $s_qualify = $a_ldap_attrs['goFonQualify'][0];
318 }
320 // Attribute GoFonPIN set ?
321 if(isset($this->goFonPIN)){
322 $s_pin = $this->goFonPIN;
323 }
325 // Attribute GoFonType set ?
326 if(isset($a_ldap_attrs['goFonType'])){
327 $s_type = $a_ldap_attrs['goFonType'][0];
328 }
330 if(isset($a_ldap_attrs['goFonDmtfMode'][0])){
331 $sip_data_array['dtmfmode'] = $a_ldap_attrs['goFonDmtfMode'][0];
332 }else{
333 $sip_data_array['dtmfmode'] ="rfc2833";
334 }
336 // generate SIP entry
337 $sip_data_array['id'] = "";
338 $sip_data_array['name'] = $this->uid;
339 $sip_data_array['accountcode'] = NULL;
340 $sip_data_array['amaflags'] = NULL;
341 $sip_data_array['callgroup'] = NULL;
342 $sip_data_array['callerid'] = "";
343 $sip_data_array['canreinvite'] = "no";
344 $sip_data_array['context'] = "default";
345 $sip_data_array['defaultip'] = NULL;
346 $sip_data_array['fromuser'] = NULL;
347 $sip_data_array['fromdomain'] = NULL;
348 $sip_data_array['host'] = $s_host;
349 $sip_data_array['insecure'] = NULL;
350 $sip_data_array['language'] = NULL;
351 $sip_data_array['mailbox'] = "asterisk";
352 $sip_data_array['md5secret'] = NULL;
353 $sip_data_array['nat'] = "no";
354 $sip_data_array['permit'] = NULL;
355 $sip_data_array['deny'] = NULL;
356 $sip_data_array['mask'] = NULL;
357 $sip_data_array['pickupgroup'] = NULL;
358 $sip_data_array['port'] = NULL;
359 $sip_data_array['qualify'] = $s_qualify;
360 $sip_data_array['restrictcid'] = "n";
361 $sip_data_array['rtptimeout'] = NULL;
362 $sip_data_array['rtpholdtimeout']=NULL;
363 $sip_data_array['secret'] = $this->goFonPIN;
364 $sip_data_array['type'] = $s_type ;
365 $sip_data_array['username'] = $this->uid;
366 $sip_data_array['disallow'] = NULL;
367 $sip_data_array['allow'] = NULL;
368 $sip_data_array['musiconhold'] = NULL;
369 $sip_data_array['regseconds'] = NULL;
370 $sip_data_array['ipaddr'] = $s_ip;
371 $sip_data_array['regexten'] = NULL;
372 $sip_data_array['cancallforward']=NULL;
374 // Get selected Macro Parameter and create parameter entry
375 if(isset($this->macroarray[$this->macro])){
376 foreach($this->macroarray[$this->macro] as $key => $val ){
377 $s_parameter .= $val['choosen']."|";
378 }
379 $s_parameter = preg_replace("/\|$/","",$s_parameter);
380 }
382 if($this->is_number_used()){
383 $this->generate_error = $this->is_number_used();
384 return false;
385 }
387 // Create new SIP entry ...
388 $sip_entry = $sip_data_array;
389 reset($this->phoneNumbers);
390 $i_new_key = key($this->phoneNumbers);
391 $sip_entry['callerid'] =$this->phoneNumbers[$i_new_key];
392 $sip_entry['mailbox'] =$this->phoneNumbers[$i_new_key];
394 if((isset($this->parent))&&(isset($this->parent->by_object['mailAccount']))&&($this->parent->by_object['mailAccount']->is_account==true)){
395 $s_mail = $this->parent->by_object['mailAccount']->mail;
396 }else{
397 $s_mail = "";
398 }
401 // $SQL contains all queries
402 $SQL = array();
403 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
404 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
405 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id=".$this->phoneNumbers[$i_new_key].";";
407 // Generate Strings with keys and values
408 foreach($sip_entry as $s_sip_key=>$s_sip_val){
409 if($s_sip_val == NULL) continue;
410 $s_sip_values.="'".$s_sip_val."',";
411 $s_sip_keys .="`".$s_sip_key."`,";
412 }
413 // Remove last ,
414 $s_sip_values = preg_replace("/,$/","",$s_sip_values);
415 $s_sip_keys = preg_replace("/,$/","",$s_sip_keys);
417 // Append SIP Entry
418 $SQL[] ="INSERT INTO ".$a_SETUP['SIP_TABLE']." (".$s_sip_keys.") VALUES (".$s_sip_values.");";
420 // Delete old entries
421 $b_first_deleted =false;
422 foreach($this->a_old_telenums 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 deletion starts from userslist, cn uid are not set */
431 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
432 $this->uid = $this->parent->by_object['user']->uid;
433 }
435 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
436 $this->cn = $this->parent->by_object['user']->cn;
437 }
439 if((!isset($this->cn))||(empty($this->cn))){
440 $CNname= $this->uid;
441 }else{
442 $CNname= $this->cn;
443 }
445 $SQL[]= "INSERT INTO ".$a_SETUP['VOICE_TABLE']." (`customer_id`,`context`,`mailbox`,`password`,`fullname`,`email`,`pager`)
446 VALUES ('".$this->phoneNumbers[$i_new_key]."',
447 'default',
448 '".$this->phoneNumbers[$i_new_key]."',
449 '".$this->goFonVoicemailPIN."',
450 '".$CNname."',
451 '".$s_mail."',
452 '');";
453 $i_is_accounted=false;
455 $i = 0;
457 // Entension entries Hint / Dial / Goto
458 foreach($this->phoneNumbers as $s_telenums){
460 /* Mapping : Name TO Number*/
461 $EXT[$i]['context'] = 'GOsa';
462 $EXT[$i]['exten'] = $this->uid;
463 $EXT[$i]['priority']= 1;
464 $EXT[$i]['app'] = "Goto";
465 $EXT[$i]['appdata'] = $s_telenums."|1";
466 $i ++;
467 /* Hint Entry */
468 $EXT[$i]['context'] = 'GOsa';
469 $EXT[$i]['exten'] = $s_telenums;
470 $EXT[$i]['priority']= "Hint";
471 $EXT[$i]['app'] = 'SIP/'.$this->uid;
472 $i ++;
473 /* SetCID */
474 $EXT[$i]['context'] = 'GOsa';
475 $EXT[$i]['exten'] = $s_telenums;
476 $EXT[$i]['priority']= 1;
477 $EXT[$i]['app'] = "SetCIDName";
478 $EXT[$i]['appdata'] = $CNname;
479 $i ++;
481 // If no macro is selected use Dial
482 if($this->macro!="none"){
483 $macroname = preg_replace("/,.*$/","",$this->macro);
484 $macroname = preg_replace("/^.*=/","",$macroname);
485 $s_app = "Macro";$macroname;
486 $s_par = $macroname."|".$s_parameter;
487 }else{
488 $s_app = "Dial";
489 $s_par = 'SIP/'.$this->uid;
490 }
492 $EXT[$i]['context'] = 'GOsa';
493 $EXT[$i]['exten'] = $s_telenums;
494 $EXT[$i]['priority']= 2;
495 $EXT[$i]['app'] = $s_app;
496 $EXT[$i]['appdata'] = $s_par;
497 $i ++;
499 }
501 // Append all these Entries
502 foreach($EXT as $entr){
503 $SQL_syn = "INSERT INTO ".$a_SETUP['EXT_TABLE']." (";
504 foreach($entr as $key2 => $val2){
505 $SQL_syn.= "`".$key2."`,";
506 }
507 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
508 $SQL_syn .= ") VALUES (";
509 foreach($entr as $key2 => $val2){
510 $SQL_syn .= "'".$val2."',";
511 }
512 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
513 $SQL_syn .=");\n";
514 $SQL[] =$SQL_syn;
515 $SQL_syn ="";
516 }
518 // Perform queries ...
519 foreach($SQL as $query){
520 if(!@mysql_query($query,$r_con)){
521 print_red(_("Error while performing query ".mysql_error()));
522 return false;
523 }
524 }
525 }
526 @mysql_close($r_con);
527 return true;
528 }
531 function execute()
532 {
533 /* Do we represent a valid account? */
534 if (!$this->is_account && $this->parent == NULL){
535 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
536 _("This account has no phone extensions.")."</b>";
537 $display.= back_to_main();
538 return ($display);
539 }
541 /* Do we need to flip is_account state? */
542 if (isset($_POST['modify_state'])){
543 $this->is_account= !$this->is_account;
544 }
546 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
547 if(empty($this->macro)){
548 $this->macro ="none";
549 }
551 /* tell user that the pluging selected is no longer available*/
552 if((!$this->macrostillavailable)&&($this->macro!="none")){
553 print_red(_("The macro you selected, is no longer available for you, please choose another one."));
554 }
556 /* Prepare templating */
557 $smarty= get_smarty();
559 /* Assing macroselectbox values */
560 $smarty->assign("macros",$this->macros);
561 $smarty->assign("macro", $this->macro);
563 /* Create parameter table, skip if no parameters given */
564 if(!isset($this->macroarray[$this->macro])){
565 $macrotab="";
566 }else{
568 $macrotab ="<table summary=\""._("Parameter")."\">";
569 /* for every single parameter-> display textfile,combo, or true false switch*/
572 /* Automatic fill out */
573 if(isset($_POST['fillout'])){
575 foreach($this->phoneNumbers as $phonenum){
576 $tmp[] = $phonenum;
577 }
579 /* Go through all params */
580 foreach($this->macroarray[$this->macro] as $key => $paras){
582 $string = $paras['default'];
584 $string=preg_replace("/%uid/i",$this->uid,$string);
586 if(isset($this->cn)){
587 $string=preg_replace("/%cn/i",$this->cn,$string);
588 }
590 for($i = 0 ; $i < 10; $i++){
591 if(isset($tmp[$i])){
592 $string = preg_replace("/%telephoneNumber_".($i+1)."/i",$tmp[$i],$string);
593 }
594 }
596 $this->macroarray[$this->macro][$key]['choosen']=$string;
597 }
598 }
600 foreach($this->macroarray[$this->macro] as $paras){
602 /* get al vars */
603 $var = $paras['var'];
604 $name = $paras['name'];
605 $default = $paras['default'];
606 $type = $paras['type'];
607 $choosen = $paras['choosen'] ;
608 $str = $default;
610 /* in case of a combo box display a combobox with selected attr */
611 $macrotab.= "<tr>";
612 switch ($type){
614 case "combo":
615 $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro")." ".chkacl($this->acl, "goFonMacro").">";
616 foreach(split(":",$default) as $choice){
617 if($choosen==$choice){
618 $str.= "\n<option value='".$choice."' selected>".$choice." </option>";
619 }else{
620 $str.= "\n<option value='".$choice."'>".$choice." </option>";
621 }
622 }
623 $str.="</select>";
624 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
625 break;
627 case "bool":
628 if(!$choosen){
629 $str="\n<input type='checkbox' name='".$var."' value='1' ".chkacl($this->acl, "goFonMacro")." >";
630 }else{
631 $str="\n<input type='checkbox' name='".$var."' value='1' checked ".chkacl($this->acl, "goFonMacro").">";
632 }
633 $macrotab.= "<td colspan='2'>$str ".base64_decode($name)."";
634 break;
636 case "string":
637 $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro")." style='width:340px;'>";
638 $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
639 break;
641 }
642 $macrotab.= "</td></tr>";
644 }
645 $macrotab.="</table><input name='post_success' type='hidden' value='1'>";
646 }//is_array()
648 /* Give smarty the table */
649 $smarty->assign("macrotab",$macrotab);
651 /* Do we represent a valid account? */
652 if (!$this->is_account && $this->parent == NULL){
653 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
654 _("This account has no phone extensions.")."</b>";
655 $display.= back_to_main();
656 return($display);
657 }
659 $display= "";
661 /* Show tab dialog headers */
662 if ($this->parent != NULL){
663 if ($this->is_account){
664 $display= $this->show_header(_("Remove phone account"),
665 _("This account has phone features enabled. You can disable them by clicking below."));
666 } else {
667 if(empty($this->uid)){
668 $display= $this->show_header(_("Create phone account"),
669 _("This account has phone features disabled. You can't enable them while no uid is set."),TRUE,TRUE);
670 }else{
671 $display= $this->show_header(_("Create phone account"),
672 _("This account has phone features disabled. You can enable them by clicking below."));
673 }
674 return ($display);
675 }
676 }
678 /* Add phone number */
679 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
680 if (is_phone_nr($_POST['phonenumber'])){
681 $number= $_POST["phonenumber"];
682 $this->phoneNumbers[$number]= $number;
683 $this->is_modified= TRUE;
684 } else {
685 print_red(_("Please enter a valid phone number!"));
686 }
687 }
689 /* Remove phone number */
690 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
691 foreach ($_POST['phonenumber_list'] as $number){
692 unset($this->phoneNumbers[$number]);
693 $this->is_modified= TRUE;
694 }
695 }
697 /* Transfer ACL's */
698 foreach($this->attributes as $val){
699 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
700 if(isset($this->$val)){
701 $smarty->assign($val,$this->$val);
702 }else{
703 $smarty->assign($val,"");
704 }
705 }
707 /* Fill arrays */
708 $smarty->assign ("goFonHardware", $this->goFonHardware);
709 if (!count($this->phoneNumbers)){
710 $smarty->assign ("phoneNumbers", array(""));
711 } else {
712 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
713 }
714 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
715 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
716 foreach ($this->hardware_list as $cn => $description){
717 if ($cn == $this->goFonHardware){
718 $selected= "selected";
719 } else {
720 $selected= "";
721 }
722 if (isset($this->used_hardware[$cn])){
723 $color= "style=\"color:#A0A0A0\"";
724 } else {
725 $color= "";
726 }
727 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description </option>\n";
728 }
729 $hl.= "</select>\n";
730 $smarty->assign ("hardware_list", $hl);
732 /* Show main page */
734 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
735 return($display);
736 }
739 function save_object()
740 {
741 if (isset($_POST["phoneTab"])){
742 plugin::save_object();
744 /* Save checkbox */
745 if (isset($_POST['fon_to_mail'])){
746 $tmp= "[M]";
747 } else {
748 $tmp= "[]";
749 }
750 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
751 if ($this->goFonDeliveryMode != $tmp){
752 $this->is_modified= TRUE;
753 }
754 $this->goFonDeliveryMode= $tmp;
755 }
757 /* Every macro in the select box are available */
758 if((isset($_POST['macro']))){
759 $this->macrostillavailable=true;
760 }
762 if(is_array($this->phoneNumbers)){
763 foreach($this->phoneNumbers as $telenumms) {
764 $nummsinorder[]=$telenumms;
765 }
766 }else{
767 $nummsinorder=array("");
768 }
770 /* get all Postvars */
771 if(isset($this->macroarray[$this->macro])){
772 foreach($this->macroarray[$this->macro] as $key => $paras){
773 if(isset($_POST[$paras['var']])){
774 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
775 }
777 /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
778 We need this code below to read and save checkboxes correct
779 */
781 if(isset($_POST['post_success'])){
782 if($this->macroarray[$this->macro][$key]['type']=="bool"){
783 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
784 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
785 }else{
786 $this->macroarray[$this->macro][$key]['choosen']=false;
787 }
788 }
789 }
790 }
791 }
792 }
793 }
795 function check()
796 {
797 /* Reset message array */
798 $message= array();
800 if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
801 $message[]=(_("Voicemail PIN must be between 1-4 characters."));
802 }else{
803 if(preg_match("/[^0-9]/",$this->goFonVoicemailPIN)){
804 $message[]=(_("The specified oicemail PIN contains invalid characters, only numeric values are allowed here."));
805 }
806 }
809 if((strlen($this->goFonPIN)!=4)){
810 $message[]=(_("Phone PIN must be 4 characters long."));
811 }else{
812 if(preg_match("/[^0-9]/",$this->goFonPIN)){
813 $message[]=(_("The specified phone PIN contains invalid characters, only numeric values are allowed here."));
814 }
815 }
817 if(!$this->generate_mysql_entension_entries()){
818 $message[] = $this->generate_error;
819 }
821 /* We need at least one phone number */
822 if (count($this->phoneNumbers) == 0){
823 $message[]= sprintf(_("You need to specify at least one phone number!"));
824 }
826 /* check for ! in any parameter setting*/
827 if(isset($this->macroarray[$this->macro])){
828 foreach($this->macroarray[$this->macro] as $val){
829 if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
830 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
831 }
832 }
833 }
834 return ($message);
835 }
839 function save()
840 {
841 plugin::save();
843 /* Save arrays */
844 $this->attrs['telephoneNumber']= array();
845 foreach ($this->phoneNumbers as $number){
846 $this->attrs['telephoneNumber'][]= $number;
847 }
849 /* Save settings, or remove goFonMacro attribute*/
850 if($this->macro!="none"){
851 $this->attrs['goFonMacro']=$this->macro;
852 if(isset($this->macroarray[$this->macro])){
853 foreach($this->macroarray[$this->macro] as $paras) {
854 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
855 }
856 }
857 }else{
858 $this->attrs['goFonMacro']=array();
859 }
860 unset($this->attrs['macro']) ;
862 $this->attrs['goFonForwarding']=array();
864 $this->generate_mysql_entension_entries(true);
866 if($this->attrs['goFonMacro']==""){
867 $this->attrs['goFonMacro']=array();
868 }
870 unset($this->attrs['cn']);
872 /* Write back to ldap */
873 $ldap= $this->config->get_ldap_link();
874 $ldap->cd($this->dn);
875 $ldap->modify($this->attrs);
876 show_ldap_error($ldap->get_error());
878 /* Optionally execute a command after we're done */
880 if ($this->initially_was_account == $this->is_account){
881 if ($this->is_modified){
882 $this->handle_post_events("modify");
883 }
884 } else {
885 $this->handle_post_events("add");
886 }
888 }
891 function insert_after($entry, $nr, $list)
892 {
893 /* Is the entry free? No? Make it free... */
894 if (isset($list[$nr])) {
895 $dest= array();
896 $newidx= 0;
898 foreach ($list as $idx => $contents){
899 $dest[$newidx++]= $contents;
900 if ($idx == $nr){
901 $dest[$newidx++]= $entry;
902 }
903 }
904 } else {
905 $dest= $list;
906 $dest[$nr]= $entry;
907 }
909 return ($dest);
910 }
913 function adapt_from_template($dn)
914 {
915 plugin::adapt_from_template($dn);
917 /* Assemble phone numbers */
918 if (isset($this->attrs['telephoneNumber'])){
919 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
920 $number= $this->attrs['telephoneNumber'][$i];
921 $this->phoneNumbers[$number]= $number;
922 }
923 }
924 }
927 function remove_from_parent()
928 {
930 foreach($this->attributes as $key=>$val){
931 if(in_array($val,array("uid","cn","mail"))){
932 unset($this->attributes[$key]);
933 unset($this->$val);
934 }
935 }
937 if(array_key_exists('config', $_SESSION) &&
938 array_key_exists('SERVERS', $_SESSION['config']->data) &&
939 array_key_exists('FON', $_SESSION['config']->data['SERVERS'])) {
940 // Get Configuration for Mysql database Server
941 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
942 $s_parameter ="";
944 // Connect to DB server
945 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
947 // Check if we are connected correctly
948 if(!$r_con){
949 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
950 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
951 gosa_log(@mysql_error());
952 return false;
953 }
955 // Select database for Extensions
956 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
958 // Test if we have the database selected correctly
959 if(!$db){
960 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
961 gosa_log(@mysql_error());
962 return false;
963 }
965 $SQL="";
967 /* If deletion starts from userslist, cn uid are not set */
968 if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
969 $this->uid = $this->parent->by_object['user']->uid;
970 }
972 if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
973 $this->cn = $this->parent->by_object['user']->cn;
974 }
976 $first_num = false;
977 // Delete old entries
978 foreach($this->a_old_telenums as $s_telenums){
979 if(!$first_num){
980 $first_num = $s_telenums;
981 }
982 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
983 }
985 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$first_num."';";
986 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
987 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
990 foreach($SQL as $query){
991 if(!@mysql_query($query,$r_con)){
992 print_red(_("Stop".mysql_error()));
993 return false;
994 }
995 }
996 }
999 /* unset macro attr, it will cause an error */
1000 $tmp = array_flip($this->attributes);
1001 unset($tmp['macro']);
1002 $this->attributes=array_flip($tmp);
1004 /* Cancel if there's nothing to do here */
1005 if (!$this->initially_was_account){
1006 return;
1007 }
1009 plugin::remove_from_parent();
1011 /* Just keep one phone number */
1012 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
1013 $this->attrs['telephoneNumber']= $this->telephoneNumber;
1014 } else {
1015 $this->attrs['telephoneNumber']= array();
1016 }
1018 $ldap= $this->config->get_ldap_link();
1019 $ldap->cd($this->config->current['BASE']);
1020 $ldap->search("(objectClass=goFonQueue)", array("member"));
1021 while($attr = $ldap->fetch()){
1022 if(in_array($this->dn,$attr['member'])){
1023 $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
1024 unset($new->by_object['ogroup']->memberList[$this->dn]);
1025 unset($new->by_object['ogroup']->member[$this->dn]);
1026 $new->save();
1027 print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->attrs['cn']));
1028 }
1029 }
1030 $ldap->cd($this->dn);
1031 $ldap->modify($this->attrs);
1032 show_ldap_error($ldap->get_error());
1034 /* Optionally execute a command after we're done */
1035 @mysql_close($r_con);
1036 $this->handle_post_events('remove');
1037 }
1041 /* This function checks if the given phonenumbers are available or already in use*/
1042 function is_number_used()
1043 {
1044 $ldap= $this->config->get_ldap_link();
1045 $ldap->cd($this->config->current['BASE']);
1046 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
1047 while($attrs = $ldap->fetch()) {
1048 unset($attrs['telephoneNumber']['count']);
1049 foreach($attrs['telephoneNumber'] as $tele){
1050 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
1051 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
1052 $numbers[$tele]=$attrs;
1053 }
1054 }
1056 foreach($this->phoneNumbers as $num){
1057 if(!isset($this->cn)) $this->cn = "";
1059 if((isset($numbers[$num]))&&(($numbers[$num]['uid'][0]!=$this->uid))){
1060 if(isset($numbers[$num]['uid'][0])){
1061 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
1062 }else{
1063 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
1064 }
1065 }
1066 }
1067 }
1068 }
1070 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1071 ?>