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 $goFonForwarding = "";
14 var $goFonFormat = "";
15 var $goFonPIN = "";
16 var $goFonDeliveryMode = "";
17 var $phoneNumbers = array();
18 var $forwarders = array();
19 var $mail = "";
20 var $hardware_list = array();
21 var $used_hardware = array();
22 var $goFonMacro = "";
23 var $macro = 0; // Selected Macor
24 var $macros = array(); // List of macros for smarty select box
25 var $macroarray = array(); // All needed macro informations
26 var $macrostillavailable = false;
27 var $generate_error = "";
28 var $a_old_telenums = array();
29 var $uid;
30 var $cn;
32 /* CLI vars */
33 var $cli_summary = "Manage users phone account";
34 var $cli_description = "Some longer text\nfor help";
35 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
37 /* attribute list for save action */
38 var $attributes = array("goFonDeliveryMode", "goFonForwarding", "goFonFormat",
39 "goFonHardware", "goFonPIN", "telephoneNumber", "goFonMacro","macro");
40 var $objectclasses= array("goFonAccount");
42 function phoneAccount ($config, $dn= NULL)
43 {
44 plugin::plugin ($config, $dn);
46 /* Set phone hardware */
47 if (!isset($this->attrs['goFonHardware'])){
48 $this->goFonHardware= "automatic";
49 }
51 /* Preset voice format */
52 if (!isset($this->attrs['goFonFormat'])){
53 $this->goFonFormat= "wav";
54 }
56 /* Assemble phone numbers */
57 if (isset($this->attrs['telephoneNumber'])){
58 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
59 $number= $this->attrs['telephoneNumber'][$i];
60 $this->phoneNumbers[$number]= $number;
61 }
62 }
63 /* Assemble forwarders */
64 if (isset($this->attrs['goFonForwarding'])){
65 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
66 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
67 $this->forwarders[$num]= "$v1;$v2";
68 }
69 } else {
70 $this->forwarders= array("");
71 }
73 /* Set up has_mailAccount */
74 if (isset($this->attrs['objectClass'])){
75 if (in_array("gosaMailAccount", $this->attrs['objectClass'])){
76 $this->has_mailAccount= TRUE;
77 }
78 }
81 /* Load hardware list */
82 $ldap= $this->config->get_ldap_link();
83 $ldap->cd($this->config->current['BASE']);
84 $ldap->search("(objectClass=goFonHardware)", array('cn', 'description'));
85 while ($attrs= $ldap->fetch()){
86 $cn= $attrs['cn'][0];
87 if (isset($attrs['description'])){
88 $description= " - ".$attrs['description'][0];
89 } else {
90 $description= "";
91 }
92 $this->hardware_list[$cn]= "$cn$description";
94 }
96 /* Prepare templating */
97 $smarty= get_smarty();
100 /* Perform search, to get Macro Parameters,Name,Dn,Displayname etc*/
101 $ldap->search("(objectClass=goFonMacro)", array("*"));
103 /* Add none for no macro*/
104 $this->macros['none']=_("no macro");
105 $this->macro ="none";
108 /* Fetch all Macros*/
109 while ($attrs= $ldap->fetch()){
111 /* Only visisble */
112 if((isset($attrs['goFonMacroVisible'][0]))&&($attrs['goFonMacroVisible'][0] ==1)){
114 /* unset Count, we don't need that here */
115 unset($attrs['displayName']['count']);
117 /* fill Selectfield variable with Macros */
118 if(isset($attrs['displayName'][0])){
119 $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
120 }else{
121 $this->macros[$attrs['dn']] = _("undefined");
122 }
124 /* Parse macro data, unset count for parameterarrays */
125 unset($attrs['goFonMacroParameter']['count']);
127 /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
128 if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
130 foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
131 /* Split Data in readable values, by delimiter ! */
132 $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
134 /* Set all attrs */
135 $id = $data[0];
136 $this->macroarray[$attrs['dn']][$id]['var'] ="var".$id;
137 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
138 $this->macroarray[$attrs['dn']][$id]['id'] =$id;
139 $this->macroarray[$attrs['dn']][$id]['name'] =$data[1];
140 $this->macroarray[$attrs['dn']][$id]['type'] =$data[2];
141 $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
142 if($data[2] == "bool"){
143 $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
144 }
145 }//foreach
146 }//is_array
147 }//visible = 1
148 }//while
150 /* Go through already saved values, for a parameter */
151 $tmp = split("!",$this->goFonMacro);
153 /* it is possible that nothing has been saved yet */
154 if(is_array($tmp)){
156 /* First value is the macroname */
157 $this->macro = $tmp[0];
159 /* Macroname saved, delete that index */
160 unset($tmp[0]);
162 /* Check if makro has been removed */
163 if(!isset($this->macroarray[$this->macro])){
164 $this->macrostillavailable = false;
165 }else{
166 $this->macrostillavailable = true;
167 }
169 /* for each parametervalues ( parameterID#value like 25#twentyfive) */
170 foreach($tmp as $var){
172 /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
173 $varar = split("#",$var);
175 /* Only insert if the parameter still exists */
176 if(isset($this->macroarray[$this->macro][$varar[0]])){
177 /* Assign value */
178 $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
179 }
180 }
181 }
184 /* Eventually colorize phones */
185 $ldap->cd($this->config->current['BASE']);
186 foreach ($this->hardware_list as $cn => $desc){
187 $ldap->search("(goFonHardware=$cn)", array('cn'));
188 if ($ldap->count() > 0){
189 $ldap->fetch();
190 if ($ldap->getDN() != $this->dn){
191 $this->used_hardware[$cn]= $ldap->getDN();
192 }
193 }
194 }
195 $this->hardware_list["automatic"]= _("automatic");
196 ksort($this->hardware_list);
197 $this->a_old_telenums = $this->phoneNumbers;
198 }
203 // Generate MySQL Syntax
204 function generate_mysql_entension_entries($save = false){
206 // Get Configuration for Mysql database Server
207 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
208 $s_parameter ="";
210 // Connect to DB server
211 $r_con = @mysql_connect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
213 // Check if we are connected correctly
214 if(!$r_con){
215 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
216 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
217 gosa_log(mysql_error());
218 return false;
219 }
221 // Select database for Extensions
222 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
224 // Test if we have the database selected correctly
225 if(!$db){
226 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
227 gosa_log(mysql_error());
228 return false;
229 }
231 // Get phonehardware to setup sip entry
232 $ldap= $this->config->get_ldap_link();
233 $res = $ldap->search("(&(objectClass=goFonHardware)(cn=".$this->goFonHardware."))", array('*'));
234 $attrs = $ldap->fetch();
236 // Attribute GoFonDefaultIP set ?
237 if(((isset($attrs['goFonDefaultIP'][0]))&&($attrs['goFonDefaultIP'][0] != "dynamic"))){
238 $ip = $attrs['goFonDefaultIP'][0];
239 $host = $ip;
240 }else{
241 $ip = NULL;
242 $host = "dynamic";
243 }
245 // Attribute GoFonQualify set ?
246 if(!isset($attrs['goFonQualify'])){
247 $qualify = NULL;
248 }else{
249 $qualify = $attrs['goFonQualify'][0];
250 }
252 // Attribute GoFonPIN set ?
253 if(!isset($this->goFonPIN)){
254 $pin = NULL;
255 }else{
256 $pin = $this->goFonPIN;
257 }
259 // Attribute GoFonType set ?
260 if(!isset($attrs['goFonType'])){
261 $type = NULL;
262 }else{
263 $type = $attrs['goFonType'][0];
264 }
266 // generate SIP entry
267 $sip_data_array['id'] = "";
268 $sip_data_array['name'] = $this->uid;
269 $sip_data_array['accountcode'] = NULL;
270 $sip_data_array['amaflags'] = NULL;
271 $sip_data_array['callgroup'] = NULL;
272 $sip_data_array['callerid'] = "";
273 $sip_data_array['canreinvite'] = "yes";
275 // Must be default and the name of an entry that already exists
276 $sip_data_array['context'] = "default";
277 $sip_data_array['defaultip'] = NULL;
279 if(isset($attrs['goFonDmtfMode'][0])){
280 $sip_data_array['dtmfmode'] = $attrs['goFonDmtfMode'][0];
281 }else{
282 $sip_data_array['dtmfmode'] ="rfc2833";
283 }
284 $sip_data_array['fromuser'] = NULL;
285 $sip_data_array['fromdomain'] = NULL;
286 $sip_data_array['host'] = $host;
287 $sip_data_array['insecure'] = NULL;
288 $sip_data_array['language'] = NULL;
289 $sip_data_array['mailbox'] = "asterisk";
290 $sip_data_array['md5secret'] = NULL;
291 $sip_data_array['nat'] = "no";
292 $sip_data_array['permit'] = NULL;
293 $sip_data_array['deny'] = NULL;
294 $sip_data_array['mask'] = NULL;
295 $sip_data_array['pickupgroup'] = NULL;
296 $sip_data_array['port'] = NULL;
297 $sip_data_array['qualify'] = $qualify;
298 $sip_data_array['restrictcid'] = "n";
299 $sip_data_array['rtptimeout'] = NULL;
300 $sip_data_array['rtpholdtimeout']=NULL;
301 $sip_data_array['secret'] = $pin;
302 $sip_data_array['type'] = $type ;
303 $sip_data_array['username'] = $this->uid;
304 $sip_data_array['disallow'] = NULL;
305 $sip_data_array['allow'] = NULL;
306 $sip_data_array['musiconhold'] = NULL;
307 $sip_data_array['regseconds'] = NULL;
308 $sip_data_array['ipaddr'] = $ip;
309 $sip_data_array['regexten'] = NULL;
310 $sip_data_array['cancallforward']=NULL;
312 // Get selected Macro Parameter and create parameter entry
313 if(isset($this->macroarray[$this->macro])){
314 foreach($this->macroarray[$this->macro] as $key => $val ){
315 $s_parameter .= $val['choosen']."|";
316 }
317 $s_parameter = preg_replace("/\|$/","",$s_parameter);
318 }
326 // $SQL contains all queries
327 $SQL = array();
328 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
329 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
331 // Create new SIP entry ...
332 $sip_entry = $sip_data_array;
334 reset($this->phoneNumbers);
336 $key = key($this->phoneNumbers);
337 $sip_entry['callerid'] =$this->phoneNumbers[$key];
338 $sip_entry['mailbox'] =$this->phoneNumbers[$key];
340 if($this->is_number_used()){
341 $this->generate_error = $this->is_number_used();
342 return false;
343 }
345 if($save == true){
346 if(isset($this->parent->by_object['mailAccount']->mail)){
347 $mail = $this->parent->by_object['mailAccount']->mail;
348 }else{
349 $mail = "";
350 }
353 $SQL[]= "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$this->phoneNumbers[$key]."';";
354 $SQL[]= "INSERT INTO ".$a_SETUP['VOICE_TABLE']."
355 (`customer_id`,`context`,`mailbox`,`password`,`fullname`,`email`,`pager`)
356 VALUES
357 ('".$this->phoneNumbers[$key]."','default','".$this->phoneNumbers[$key]."','".$this->goFonPIN."','".$this->sn."','".$mail."','');";
359 // Generate Strings with keys and values
360 $values = "";
361 $keys = "";
362 foreach($sip_entry as $key=>$val){
363 if($val == NULL) continue;
364 $values.="'".$val."',";
365 $keys .="`".$key."`,";
366 }
367 // Remove last ,
368 $values = preg_replace("/,$/","",$values);
369 $keys = preg_replace("/,$/","",$keys);
371 // Append SIP Entry
372 $SQL[] ="INSERT INTO ".$a_SETUP['SIP_TABLE']." (".$keys.") VALUES (".$values.");";
374 // Delete old entries
375 foreach($this->a_old_telenums as $s_telenums){
376 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
377 }
379 $i_is_accounted=false;
381 // Entension entries Hint / Dial / Goto
382 foreach($this->phoneNumbers as $s_telenums){
383 // Entry to call by name
384 $s_entry_name['context'] = 'GOsa';
385 $s_entry_name['exten'] = $this->uid;
386 $s_entry_name['priority'] = 1;
387 $s_entry_name['app'] = 'Goto';
388 $s_entry_name['appdata'] = $s_telenums."|1";
390 // hint
391 $s_entry_hint['context'] = 'GOsa';
392 $s_entry_hint['exten'] = $s_telenums;
393 $s_entry_hint['priority'] = 'hint';
394 $s_entry_hint['app'] = 'SIP/'.$this->uid;
396 // If no macro is selected use Dial
397 if($this->macro!="none"){
398 $macroname = preg_replace("/,.*$/","",$this->macro);
399 $macroname = preg_replace("/^.*=/","",$macroname);
400 $s_app = "Macro";$macroname;
401 $s_par = $macroname."|".$s_parameter;
402 }else{
403 $s_app = "Dial";
404 $s_par = 'SIP/'.$this->uid;
405 }
407 // Entry to call by number
408 $s_entry_phone['context'] = 'GOsa';
409 $s_entry_phone['exten'] = $s_telenums;
410 $s_entry_phone['priority'] = 1;
411 $s_entry_phone['app'] = $s_app;
412 $s_entry_phone['appdata'] = $s_par;
414 // append name entry only once
415 if(!$i_is_accounted){
416 $i_is_accounted = true;
417 $entries[]=array("hint"=>$s_entry_hint,"phone"=>$s_entry_phone,"name"=>$s_entry_name);
418 }else{
419 $entries[]=array("hint"=>$s_entry_hint,"phone"=>$s_entry_phone);
420 }
421 }
423 // Append all these Entries
424 foreach($entries as $num => $val){
425 foreach($val as $entr){
426 $SQL_syn = "INSERT INTO ".$a_SETUP['EXT_TABLE']." (";
427 foreach($entr as $key2 => $val2){
428 $SQL_syn.= "`".$key2."`,";
429 }
430 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
431 $SQL_syn .= ") VALUES (";
432 foreach($entr as $key2 => $val2){
433 $SQL_syn .= "'".$val2."',";
434 }
435 $SQL_syn = preg_replace("/,$/","",$SQL_syn);
436 $SQL_syn .=");\n";
437 $SQL[] =$SQL_syn;
438 $SQL_syn ="";
439 }
440 }
442 // Perform queries ...
443 foreach($SQL as $query){
444 if(!mysql_query($query,$r_con)){
445 print_red(_("Error while performing query ".mysql_error()));
446 return false;
447 }
448 }
449 }
450 return true;
451 }
466 function execute()
467 {
468 /* force postmodify event, to restart phones */
470 $this->parent->by_object['user']->is_modified=TRUE;
471 $this->is_modified=TRUE;
473 $this->uid = $this->parent->by_object['user']->uid;
474 $this->cn = $this->parent->by_object['user']->cn;
477 /* Do we need to flip is_account state? */
478 if (isset($_POST['modify_state'])){
479 $this->is_account= !$this->is_account;
480 }
482 /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
483 if(empty($this->macro)){
484 $this->macro ="none";
485 }
487 /* tell user that the pluging selected is no longer available*/
488 if((!$this->macrostillavailable)&&($this->macro!="none")){
489 print_red(_("The macro you selected in the past, is no longer available for you, please choose another one."));
490 }
492 /* Prepare templating */
493 $smarty= get_smarty();
495 /* Assing macroselectbox values */
496 $smarty->assign("macros",$this->macros);
497 $smarty->assign("macro", $this->macro);
499 /* Create parameter table, skip if no parameters given */
500 if(!isset($this->macroarray[$this->macro])){
501 $macrotab="";
502 }else{
504 $macrotab ="<table summary=\""._("Parameter")."\">";
505 /* for every single parameter-> display textfile,combo, or true false switch*/
508 /* Automatic fill out */
509 if(isset($_POST['fillout'])){
511 foreach($this->phoneNumbers as $phonenum){
512 $tmp[] = $phonenum;
513 }
515 /* Go through all params */
516 foreach($this->macroarray[$this->macro] as $key => $paras){
518 $string = $paras['default'];
520 $string=str_replace("%uid",$this->uid,$string);
521 $string=str_replace("%cn",$this->cn,$string);
523 for($i = 0 ; $i < 10; $i++){
524 if(isset($tmp[$i])){
525 $string = preg_replace("/%telephoneNumber_".($i+1)."/",$tmp[$i],$string);
526 }
527 }
529 $this->macroarray[$this->macro][$key]['choosen']=$string;
530 }
531 }
533 foreach($this->macroarray[$this->macro] as $paras){
535 /* get al vars */
536 $var = $paras['var'];
537 $name = $paras['name'];
538 $default = $paras['default'];
539 $type = $paras['type'];
540 $choosen = $paras['choosen'] ;
541 $str = $default;
543 /* in case of a combo box display a combobox with selected attr */
544 $macrotab.= "<tr>";
545 switch ($type){
547 case "combo":
548 $str= "<select name='".$var."' ".chkacl($this->acl, "goFonMacro")." ".chkacl($this->acl, "goFonMacro").">";
549 foreach(split(":",$default) as $choice){
550 if($choosen==$choice){
551 $str.= "\n<option value='".$choice."' selected>".$choice." </option>";
552 }else{
553 $str.= "\n<option value='".$choice."'>".$choice." </option>";
554 }
555 }
556 $str.="</select>";
557 $macrotab.= "<td>$name</td><td>$str";
558 break;
560 case "bool":
561 if(!$choosen){
562 $str="\n<input type='checkbox' name='".$var."' value='1' ".chkacl($this->acl, "goFonMacro")." >";
563 }else{
564 $str="\n<input type='checkbox' name='".$var."' value='1' checked ".chkacl($this->acl, "goFonMacro").">";
565 }
566 $macrotab.= "<td colspan='2'>$str $name";
567 break;
569 case "string":
570 $str="<input name='".$var."' value='".$choosen."' ".chkacl($this->acl, "goFonMacro").">";
571 $macrotab.= "<td>$name</td><td>$str";
572 break;
574 }
575 $macrotab.= "</td></tr>";
577 }
578 $macrotab.="</table><input name='post_success' type='hidden' value='1'>";
579 }//is_array()
581 /* Give smarty the table */
582 $smarty->assign("macrotab",$macrotab);
584 /* Do we represent a valid account? */
585 if (!$this->is_account && $this->parent == NULL){
586 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
587 _("This account has no phone extensions.")."</b>";
588 $display.= back_to_main();
589 return($display);
590 }
592 $display= "";
594 /* Show tab dialog headers */
595 if ($this->parent != NULL){
596 if ($this->is_account){
597 $display= $this->show_header(_("Remove phone account"),
598 _("This account has phone features enabled. You can disable them by clicking below."));
599 } else {
600 if(empty($this->uid)){
601 $display= $this->show_header(_("Create phone account"),
602 _("This account has phone features disabled. You can't enable them while no uid is set."),TRUE,TRUE);
603 }else{
604 $display= $this->show_header(_("Create phone account"),
605 _("This account has phone features disabled. You can enable them by clicking below."));
606 }
607 return ($display);
608 }
609 }
611 /* Add phone number */
612 if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
613 if (is_phone_nr($_POST['phonenumber'])){
614 $number= $_POST["phonenumber"];
615 $this->phoneNumbers[$number]= $number;
616 $this->is_modified= TRUE;
617 } else {
618 print_red(_("Please enter a valid phone number!"));
619 }
620 }
622 /* Remove phone number */
623 if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
624 foreach ($_POST['phonenumber_list'] as $number){
625 unset($this->phoneNumbers[$number]);
626 $this->is_modified= TRUE;
627 }
628 }
630 /* Check for forwarding action */
631 foreach ($this->forwarders as $nr => $fw){
633 /* Buttons pressed? */
634 if (isset($_POST["add_fw$nr"])){
635 $this->forwarders= $this->insert_after("", $nr, $this->forwarders);
636 }
637 if (isset($_POST["remove_fw$nr"])){
638 unset($this->forwarders[$nr]);
639 }
640 }
642 /* Transfer ACL's */
643 foreach($this->attributes as $val){
644 $smarty->assign($val."ACL", chkacl($this->acl, "$val"));
645 $smarty->assign($val,$this->$val);
646 }
648 /* Fill arrays */
649 $smarty->assign ("goFonHardware", $this->goFonHardware);
650 if (!count($this->phoneNumbers)){
651 $smarty->assign ("phoneNumbers", array(""));
652 } else {
653 $smarty->assign ("phoneNumbers", $this->phoneNumbers);
654 }
655 $hl= "<select size=\"1\" name=\"goFonHardware\" title=\"".
656 _("Choose your private phone")."\" ".chkacl($this->acl, "goFonHardware").">\n";
657 foreach ($this->hardware_list as $cn => $description){
658 if ($cn == $this->goFonHardware){
659 $selected= "selected";
660 } else {
661 $selected= "";
662 }
663 if (isset($this->used_hardware[$cn])){
664 $color= "style=\"color:#A0A0A0\"";
665 } else {
666 $color= "";
667 }
668 $hl.= " <option $color label=\"$cn\" value=\"$cn\" $selected>$description </option>\n";
669 }
670 $hl.= "</select>\n";
671 $smarty->assign ("hardware_list", $hl);
673 /* Generate forwarder view */
674 $forwarder_list="";
675 $acl= chkacl($this->acl, "goFonForwaring");
676 foreach ($this->forwarders as $nr => $fw){
677 if ($fw == ""){
678 $number= ""; $timeout= "";
679 } else {
680 list($number, $timeout)= split(";", $fw);
681 }
682 $forwarder_list.= "<tr><td>";
683 $forwarder_list.= "<input name=\"fwn$nr\" size=25 align=\"middle\" maxlength=60 value=\"$number\" $acl>";
684 $forwarder_list.= "</td><td>";
685 $forwarder_list.= "<input name=\"fwt$nr\" size=5 align=\"middle\" maxlength=5 value=\"$timeout\" $acl>";
686 $forwarder_list.= "</td><td>";
687 $forwarder_list.= "<input type=\"submit\" value=\""._("Add")."\" name=\"add_fw$nr\" $acl>";
688 if (count($this->forwarders) > 1){
689 $forwarder_list.= "<input type=\"submit\" value=\""._("Remove")."\" name=\"remove_fw$nr\" $acl>";
690 }
691 $forwarder_list.= "</td></tr>";
692 }
693 $smarty->assign("forwarder_list", $forwarder_list);
695 /* Show main page */
696 $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
697 return($display);
698 }
701 function save_object()
702 {
703 if (isset($_POST["phoneTab"])){
704 plugin::save_object();
706 /* Save checkbox */
707 if (isset($_POST['fon_to_mail'])){
708 $tmp= "[M]";
709 } else {
710 $tmp= "[]";
711 }
712 if (chkacl ($this->acl, "goFonDeliveryMode") == ""){
713 if ($this->goFonDeliveryMode != $tmp){
714 $this->is_modified= TRUE;
715 }
716 $this->goFonDeliveryMode= $tmp;
717 }
719 /* Save forwarding numbers and timeouts */
720 if (chkacl ($this->acl, "goFonForwarder") == ""){
721 foreach ($this->forwarders as $nr => $fw){
722 $tmp= $_POST["fwn$nr"].";".$_POST["fwt$nr"];
723 if ($this->forwarders[$nr] != $tmp){
724 $this->is_modified= TRUE;
725 }
726 $this->forwarders[$nr]= $tmp;
727 }
728 }
730 /* Every macro in the select box are available */
731 if((isset($_POST['macro']))){
732 $this->macrostillavailable=true;
733 }
735 if(is_array($this->phoneNumbers)){
736 foreach($this->phoneNumbers as $telenumms) {
737 $nummsinorder[]=$telenumms;
738 }
739 }else{
740 $nummsinorder=array("");
741 }
743 /* get all Postvars */
744 if(isset($this->macroarray[$this->macro])){
745 foreach($this->macroarray[$this->macro] as $key => $paras){
746 if(isset($_POST[$paras['var']])){
747 // $par = $this->macroarray[$this->macro][$key];
748 // $string = "";
749 // if(preg_match("/.*%telephoneNumber_.*/",$par['default'])){
750 // $string = $par['default'];
751 // foreach($nummsinorder as $nummsinorderkey=> $nummsinorderval){
752 // $string = (str_replace("%telephoneNumber_".($nummsinorderkey+1),$nummsinorderval,$string));
753 // }
754 // }
756 // if(preg_match("/.*%uid.*/",$par['default'])){
757 // if(empty($string)) $string = $par['default'];
758 // $string = str_replace("%uid",$this->uid,$string);
759 // }
761 // if(!empty($string)){
762 // $this->macroarray[$this->macro][$key]['choosen'] = $string;
763 // }else{
764 $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
765 // }
766 }
768 /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
769 We need this code below to read and save checkboxes correct
770 */
772 if(isset($_POST['post_success'])){
773 if($this->macroarray[$this->macro][$key]['type']=="bool"){
774 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
775 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
776 }else{
777 $this->macroarray[$this->macro][$key]['choosen']=false;
778 }
779 }
780 }
781 }
782 }
783 }
784 }
786 function check()
787 {
788 /* Reset message array */
789 $message= array();
791 if(!$this->generate_mysql_entension_entries()){
792 $message[] = $this->generate_error;
793 }
795 /* We need at least one phone number */
796 if (count($this->phoneNumbers) == 0){
797 $message[]= sprintf(_("You need to specify at least one phone number!"));
798 }
800 if(($this->goFonPIN)==""){
801 $message[]= sprintf(_("You need to specify a Phone PIN."));
802 }else{
803 if(strcmp ((int)($this->goFonPIN),($this->goFonPIN))){
804 $message[] = sprintf(_("The given PIN is not valid, only numbers are allowed for this type."));
805 }elseif(strlen($this->goFonPIN) < 4){
806 $message[] = sprintf(_("The given PIN is too short"));
807 }
809 }
810 /* Check timestamps and phonenumbers */
811 foreach ($this->forwarders as $fw){
813 /* Skip empty values */
814 if ($fw == ";"){
815 continue;
816 }
818 /* Check */
819 list($number, $timeout)= split(";", $fw);
820 if (!is_phone_nr($number)){
821 $message[]= sprintf(_("The number '%s' is no valid phone number!"), $number);
822 }
823 if (!is_id($timeout)){
824 $message[]= sprintf(_("The timeout '%s' contains invalid characters!"), $timeout);
825 }
826 }
828 /* check for ! in any parameter setting*/
829 if(isset($this->macroarray[$this->macro])){
830 foreach($this->macroarray[$this->macro] as $val){
831 if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
832 $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
833 }
834 }
835 }
836 return ($message);
837 }
841 function save()
842 {
843 plugin::save();
845 /* Save arrays */
846 $this->attrs['telephoneNumber']= array();
847 foreach ($this->phoneNumbers as $number){
848 $this->attrs['telephoneNumber'][]= $number;
849 }
850 $this->attrs['goFonForwarding']= array();
851 foreach ($this->forwarders as $index => $number){
852 $this->attrs['goFonForwarding'][]= "$index;$number";
853 }
855 /* Save settings, or remove goFonMacro attribute*/
856 if($this->macro!="none"){
857 $this->attrs['goFonMacro']=$this->macro;
858 if(isset($this->macroarray[$this->macro])){
859 foreach($this->macroarray[$this->macro] as $paras) {
860 $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
861 }
862 }
863 }else{
864 $this->attrs['goFonMacro']=array();
865 }
866 unset($this->attrs['macro']) ;
868 $this->generate_mysql_entension_entries(true);
870 if($this->attrs['goFonMacro']==""){
871 $this->attrs['goFonMacro']=array();
872 }
873 /* Write back to ldap */
874 $ldap= $this->config->get_ldap_link();
875 $ldap->cd($this->dn);
876 $ldap->modify($this->attrs);
877 show_ldap_error($ldap->get_error());
879 /* Optionally execute a command after we're done */
881 if ($this->initially_was_account == $this->is_account){
882 if ($this->is_modified){
883 $this->handle_post_events("modify");
884 }
885 } else {
886 $this->handle_post_events("add");
887 }
889 }
892 function insert_after($entry, $nr, $list)
893 {
894 /* Is the entry free? No? Make it free... */
895 if (isset($list[$nr])) {
896 $dest= array();
897 $newidx= 0;
899 foreach ($list as $idx => $contents){
900 $dest[$newidx++]= $contents;
901 if ($idx == $nr){
902 $dest[$newidx++]= $entry;
903 }
904 }
905 } else {
906 $dest= $list;
907 $dest[$nr]= $entry;
908 }
910 return ($dest);
911 }
914 function adapt_from_template($dn)
915 {
916 plugin::adapt_from_template($dn);
918 /* Assemble phone numbers */
919 if (isset($this->attrs['telephoneNumber'])){
920 for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
921 $number= $this->attrs['telephoneNumber'][$i];
922 $this->phoneNumbers[$number]= $number;
923 }
924 }
926 /* Assemble forwarders */
927 if (isset($this->attrs['goFonForwarding'])){
928 for ($i= 0; $i<$this->attrs['goFonForwarding']['count']; $i++){
929 list($num, $v1, $v2) =split(';', $this->attrs['goFonForwarding'][$i]);
930 $this->forwarders[$num]= "$v1;$v2";
931 }
932 } else {
933 $this->forwarders= array("");
934 }
935 }
938 function remove_from_parent()
939 {
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_connect($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 $this->uid = $this->parent->by_object['user']->uid;
969 $this->cn = $this->parent->by_object['user']->cn;
971 $first_num = false;
972 // Delete old entries
973 foreach($this->a_old_telenums as $s_telenums){
974 if(!$first_num){
975 $first_num = $s_telenums;
976 }
977 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
978 }
980 $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$first_num."';";
981 $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
982 $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
985 foreach($SQL as $query){
986 if(!mysql_query($query,$r_con)){
987 print_red(_("Stop".mysql_error()));
988 return false;
989 }
990 }
994 /* unset macro attr, it will cause an error */
995 $tmp = array_flip($this->attributes);
996 unset($tmp['macro']);
997 $this->attributes=array_flip($tmp);
999 /* Cancel if there's nothing to do here */
1000 if (!$this->initially_was_account){
1001 return;
1002 }
1004 plugin::remove_from_parent();
1006 /* Just keep one phone number */
1007 if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
1008 $this->attrs['telephoneNumber']= $this->telephoneNumber[0];
1009 } else {
1010 $this->attrs['telephoneNumber']= array();
1011 }
1013 $ldap= $this->config->get_ldap_link();
1014 $ldap->cd($this->config->current['BASE']);
1015 $ldap->search("(objectClass=goFonQueue)", array("member"));
1016 while($attr = $ldap->fetch()){
1017 if(in_array($this->dn,$attr['member'])){
1018 $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
1019 unset($new->by_object['ogroup']->memberList[$this->dn]);
1020 unset($new->by_object['ogroup']->member[$this->dn]);
1021 $new->save();
1022 print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->attrs['cn']));
1023 }
1024 }
1025 $ldap->cd($this->dn);
1026 $ldap->modify($this->attrs);
1027 show_ldap_error($ldap->get_error());
1029 /* Optionally execute a command after we're done */
1030 $this->handle_post_events('remove');
1031 }
1035 /* This function checks if the given phonenumbers are available or already in use*/
1036 function is_number_used()
1037 {
1038 $ldap= $this->config->get_ldap_link();
1039 $ldap->cd($this->config->current['BASE']);
1040 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue))", array("telephoneNumber","cn","uid"));
1041 while($attrs = $ldap->fetch()) {
1042 unset($attrs['telephoneNumber']['count']);
1043 foreach($attrs['telephoneNumber'] as $tele){
1044 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
1045 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
1046 $numbers[$tele]=$attrs;
1047 }
1048 }
1050 foreach($this->phoneNumbers as $num){
1051 if(!isset($this->cn)) $this->cn = "";
1053 if((isset($numbers[$num]))&&(($numbers[$num]['uid'][0]!=$this->uid))){
1054 if(isset($numbers[$num]['uid'][0])){
1055 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
1056 }else{
1057 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
1058 }
1059 }
1060 }
1061 }
1064 }
1066 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1067 ?>