root/modules/branches/2.7/fax/functions.inc.php

Revision 9066, 28.9 kB (checked in by p_lindheimer, 3 years ago)

fixes #4096 Asterisk 1.6.2.something has a problem with ExecIf? + System call to fax-process.pl, used GotoIf? and System instead

Line 
1 <?php
2 /* $Id */
3
4 function fax_getdest($exten) {
5     return array("ext-fax,$exten,1");
6 }
7
8 function fax_getdestinfo($dest) {
9   global $amp_conf;
10     if (substr(trim($dest),0,8) == 'ext-fax,') {
11         $usr = explode(',',$dest);
12         $usr = $usr[1];
13         $thisusr = fax_get_user($usr);
14         if (empty($thisusr)) {
15             return array();
16         } else {
17             $display = ($amp_conf['AMPEXTENSIONS'] == "deviceanduser")?'users':'extensions';
18             return array('description' => sprintf(_("Fax user %s"),$usr),
19                          'edit_url' => 'config.php?display='.$display.'&extdisplay='.urlencode($usr),
20                                   );
21         }
22     } else {
23         return false;
24     }
25 }
26
27 function fax_check_destinations($dest=true) {
28     global $active_modules;
29
30     $destlist = array();
31     if (is_array($dest) && empty($dest)) {
32         return $destlist;
33     }
34     $sql = "SELECT a.extension, a.cidnum, b.description, a.destination FROM fax_incoming a JOIN incoming b ";
35   $sql .= "WHERE a.extension = b.extension AND a.cidnum = b.cidnum AND a.legacy_email IS NULL ";
36     if ($dest !== true) {
37         $sql .= "AND a.destination in ('".implode("','",$dest)."') ";
38     }
39     $sql .= "ORDER BY extension, cidnum";
40     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
41
42     //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
43
44     foreach ($results as $result) {
45         $thisdest = $result['destination'];
46         $thisid   = $result['extension'].'/'.$result['cidnum'];
47         $destlist[] = array(
48             'dest' => $thisdest,
49             'description' => sprintf(_("Inbound Fax Detection: %s (%s)"),$result['description'],$thisid),
50             'edit_url' => 'config.php?display=did&extdisplay='.urlencode($thisid),
51         );
52     }
53     return $destlist;
54 }
55
56 function fax_applyhooks() {
57     global $currentcomponent;
58     // Add the 'process' function - this gets called when the page is loaded, to hook into
59     // displaying stuff on the page.
60     $currentcomponent->addguifunc('fax_configpageload');
61 }
62
63 // This is called before the page is actually displayed, so we can use addguielem(). draws hook on the extensions/users page
64 function fax_configpageload() {
65     global $currentcomponent;
66     global $display;
67     $extdisplay=isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:'';
68     $extensions=isset($_REQUEST['extensions'])?$_REQUEST['extensions']:'';
69     $users=isset($_REQUEST['users'])?$_REQUEST['users']:'';
70     
71     if ($display == 'extensions' || $display == 'users') {
72         if($extdisplay!=''){
73             $fax=fax_get_user($extdisplay);
74             $faxenabled=$fax['faxenabled'];
75             $faxemail=$fax['faxemail'];
76         }//get settings in to variables
77         $section = _('Fax');
78         $toggleemail='if($(this).attr(\'checked\')){$(\'[id^=fax]\').removeAttr(\'disabled\');}else{$(\'[id^=fax]\').attr(\'disabled\',\'true\');$(this).removeAttr(\'disabled\');}';
79         //check for fax prequsits, and alert the user if something is amiss
80         $fax=fax_detect();
81         if(!$fax['module']){//missing modules
82             $currentcomponent->addguielem($section, new gui_label('error',_('<font color="red">'._('ERROR: No FAX modules detected! Fax-related dialplan will <strong>NOT</strong> be generated. This module requires Fax for Asterisk or spandsp based app_fax or app_rxfax to function.').'</font>')));
83         }elseif($fax['module'] == 'res_fax' && $fax['license'] < 1){//missing licese
84             $currentcomponent->addguielem($section, new gui_label('error',_('<font color="red">'._('ERROR: No Fax license detected. Fax-related dialplan will <strong>NOT</strong> be generated! This module has detected that Fax for Asterisk is installed without a license. At least one license is required (it is available for free) and must be installed.').'</font>')));
85         }
86         $usage_list = framework_display_destination_usage(fax_getdest($extdisplay));
87         if (!empty($usage_list)) {
88             $currentcomponent->addguielem('_top', new gui_link_label('faxdests', "&nbsp;Fax".$usage_list['text'], $usage_list['tooltip'], true), 5);
89         }
90         
91         $currentcomponent->addguielem($section, new gui_checkbox('faxenabled',$faxenabled,_('Enabled'), _('Enable this user to receive faxes'),'true','',$toggleemail));
92         $currentcomponent->addguielem($section, new gui_textbox('faxemail', $faxemail, _('Fax Email'), _('Enter an email address where faxes sent to this extension will be delivered.'), '!isEmail()', _('Please Enter a valid email address for fax delivery.'), TRUE, '', ($faxenabled == 'true')?'':'true'));
93     }
94 }
95
96 function fax_configpageinit($pagename) {
97     global $currentcomponent;
98     // On a 'new' user, 'tech_hardware' is set, and there's no extension.
99     if (($_REQUEST['display'] == 'users'||$_REQUEST['display'] == 'extensions')&& isset($_REQUEST['extdisplay']) && $_REQUEST['extdisplay'] != '') {
100     $currentcomponent->addprocessfunc('fax_configpageload', 1);
101     $currentcomponent->addprocessfunc('fax_configprocess', 1);
102     }
103 }
104
105 //prosses recived arguments
106 function fax_configprocess() {
107     $action = isset($_REQUEST['action'])?$_REQUEST['action']:null;
108     $ext = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:$_REQUEST['extension'];
109     $faxenabled = isset($_REQUEST['faxenabled'])?$_REQUEST['faxenabled']:null;
110     $faxemail = isset($_REQUEST['faxemail'])?$_REQUEST['faxemail']:null;
111     if ($action == 'edit'){fax_save_user($ext,$faxenabled,$faxemail);}
112 }
113
114 function fax_dahdi_faxdetect(){
115     /*
116      * kepping this always set to true for freepbx 2.7 as we cant currently properly detect this - MB
117      *
118      */
119   return true;
120 }
121
122 function fax_delete_incoming($extdisplay){
123     global $db;
124     $opts=explode('/', $extdisplay);$extension=$opts['0'];$cidnum=$opts['1']; //set vars
125     sql("DELETE FROM fax_incoming WHERE cidnum = '".$db->escapeSimple($cidnum)."' and extension = '".$db->escapeSimple($extension)."'");
126 }
127
128 function fax_destinations(){
129     global $module_page;
130
131     foreach (fax_get_destinations() as $row) {
132         $extens[] = array('destination' => 'ext-fax,' . $row['user'] . ',1', 'description' => $row['name'].' ('.$row['user'].')', 'category' => _('Fax Recipient'));
133     }
134     return isset($extens)?$extens:null;
135 }
136
137 //check to see if any fax modules and licenses are loaded in to asterisk
138 function fax_detect($astver=null){
139     global $amp_conf;
140     global $astman;
141
142   if ($astver === null) {
143     $engineinfo = engine_getinfo();
144     $astver $engineinfo['version'];
145   }
146   $ast_ge_14 = version_compare($astver, '1.4', 'ge');
147
148     $fax=null;
149     $appfax = $recivefax = false;//return false by default in case asterisk isnt reachable
150     if ($amp_conf['AMPENGINE'] == 'asterisk' && isset($astman) && $astman->connected()) {
151         //check for fax modules
152     $module_show_command = $ast_ge_14 ? 'module show like ' : 'show modules like ';
153         $app = $astman->send_request('Command', array('Command' => $module_show_command.'res_fax'));
154         if (preg_match('/[1-9] modules loaded/', $app['data'])){
155       $fax['module']='res_fax';
156     } else {
157           $recive = $astman->send_request('Command', array('Command' => $module_show_command.'app_fax'));
158           if (preg_match('/[1-9] modules loaded/', $recive['data'])){$fax['module']='app_fax';}
159     }
160     if (!isset($fax['module'])) {
161           $app = $astman->send_request('Command', array('Command' => $module_show_command.'app_rxfax'));
162       $fax['module'] = preg_match('/[1-9] modules loaded/', $app['data']) ? 'app_rxfax': null;
163     }
164         $response = $astman->send_request('Command', array('Command' => $module_show_command.'app_nv_faxdetect'));
165     $fax['nvfax']= preg_match('/[1-9] modules loaded/', $response['data']) ? true : false;
166
167     switch($fax['module']) {
168     case 'res_fax':
169       $fax['receivefax'] = 'receivefax';
170       break;
171     case 'app_rxfax':
172       $fax['receivefax'] = 'rxfax';
173       break;
174     case 'app_fax':
175       $application_show_command = $ast_ge_14 ? 'core show applications like ' : 'show applications like ';
176           $response = $astman->send_request('Command', array('Command' => $application_show_command.'receivefax'));
177       if (preg_match('/1 Applications Matching/', $response['data'])) {
178         $fax['receivefax'] = 'receivefax';
179       } else {
180             $response = $astman->send_request('Command', array('Command' => $application_show_command.'rxfax'));
181         if (preg_match('/1 Applications Matching/', $response['data'])) {
182           $fax['receivefax'] = 'rxfax';
183         } else {
184           $fax['receivefax'] = 'none';
185         }
186       }
187       break;
188     }
189
190         //get license count
191         $lic = $astman->send_request('Command', array('Command' => 'fax show stats'));
192         foreach(explode("\n",$lic['data']) as $licdata){
193         $d=explode(':',$licdata);
194         $data[trim($d['0'])]=isset($d['1'])?trim($d['1']):null;
195         }
196         $fax['license']=$data['Licensed Channels'];
197     }
198     return $fax;
199 }
200
201 function fax_get_config($engine){
202  global $version;
203
204     $fax=fax_detect($version);
205     if($fax['module']){ //dont continue unless we have a fax module in asterisk
206         global $ext;
207         global $amp_conf;
208       global $core_conf;
209
210       $ast_ge_16= version_compare($version, '1.6', 'ge');
211       if ($ast_ge_16 && isset($core_conf) && is_a($core_conf, "core_conf")) {
212           $core_conf->addSipGeneral('faxdetect','yes');
213       }
214
215         $context='ext-fax';
216         $dests=fax_get_destinations();
217         $sender_address=sql('SELECT value FROM fax_details WHERE `key` = \'sender_address\'','getRow');
218         if($dests){
219             foreach ($dests as $row) {
220                 $exten=$row['user'];
221                 $ext->add($context, $exten, '', new ext_noop('Receiving Fax for: '.$row['name'].' ('.$row['user'].'), From: ${CALLERID(all)}'));
222                 $ext->add($context, $exten, '', new ext_set('FAX_RX_EMAIL', '"'.$row['faxemail'].'"'));           
223             $ext->add($context, $exten, '', new ext_goto('receivefax','s'));
224             }
225         }
226     /*
227       FAX Failures are not handled well as of this coding in by ReceiveFAX. If there is a license available then it provides
228       information. If not, nothing is provided. FAXSTATUS is supported in 1.4 to handle legacy with RxFax(). In order to create
229       dialplan to try and handle all cases, we use FAXSTATUS and set it ourselves as needed. It appears that if a fax fails with
230       ReceiveFAX we can always continue execution and if it succeeds, then execution goes to hangup. So using that information
231       we try to trap and report on all cases.
232     */
233     $exten = 's';
234       $ext->add($context, $exten, '', new ext_macro('user-callerid')); // $cmd,n,Macro(user-callerid)
235     $ext->add($context, $exten, '', new ext_noop('Receiving Fax for: ${FAX_RX_EMAIL} , From: ${CALLERID(all)}'));
236     switch ($fax['module']) {
237     case 'app_rxfax':
238       $ext->add($context, $exten, 'receivefax', new ext_rxfax('${ASTSPOOLDIR}/fax/${UNIQUEID}.tif')); //recive fax, then email it on
239     break;
240     case 'app_fax':
241       // $fax['receivefax'] should be rxfax or receivefax, it could be none in which case we don't know. We'll just make it
242       // ReceiveFAX in that case since it will fail anyhow.
243       if ($fax['receivefax'] == 'rxfax') {
244         $ext->add($context, $exten, 'receivefax', new ext_rxfax('${ASTSPOOLDIR}/fax/${UNIQUEID}.tif')); //recive fax, then email it on
245       } elseif ($fax['receivefax'] == 'receivefax') {
246         $ext->add($context, $exten, 'receivefax', new ext_receivefax('${ASTSPOOLDIR}/fax/${UNIQUEID}.tif')); //recive fax, then email it on
247       } else {
248         $ext->add($context, $exten, 'receivefax', new ext_noop('ERROR: NO Receive FAX application detected, putting in dialplan for ReceiveFAX as default'));
249         $ext->add($context, $exten, '', new ext_receivefax('${ASTSPOOLDIR}/fax/${UNIQUEID}.tif')); //recive fax, then email it on
250               $ext->add($context, $exten, '', new ext_execif('$["${FAXSTATUS}" = ""]','Set','FAXSTATUS=${IF($["${FAXOPT(error)}" = ""]?"FAILED LICENSE EXCEEDED":"FAILED FAXOPT: error: ${FAXOPT(error)} status: ${FAXOPT(status)} statusstr: ${FAXOPT(statusstr)}")}'));
251       }
252     break;
253     case 'res_fax':
254       $ext->add($context, $exten, 'receivefax', new ext_receivefax('${ASTSPOOLDIR}/fax/${UNIQUEID}.tif')); //recive fax, then email it on
255       // Some versions or settings appear to have successful completions continue, so check status and goto hangup code
256       $ext->add($context, $exten, '', new ext_execif('$["${FAXOPT(error)}"=""]','Set','FAXSTATUS=FAILED LICENSE EXCEEDED'));
257       $ext->add($context, $exten, '', new ext_execif('$["${FAXOPT(error)}"!="" && "${FAXOPT(error)}"!="NO_ERROR"]','Set','FAXSTATUS="FAILED FAXOPT: error: ${FAXOPT(error)} status: ${FAXOPT(status)} statusstr: ${FAXOPT(statusstr)}"'));
258           $ext->add($context, $exten, '', new ext_goto('1','h'));
259
260     break;
261     default: // unknown
262       $ext->add($context, $exten, '', new ext_noop('No Known FAX Technology installed to receive a fax, aborting'));
263             $ext->add($context, $exten, '', new ext_set('FAXSTATUS','FAILED No Known Fax Reception Apps available to process'));
264             $ext->add($context, $exten, '', new ext_hangup());
265     }
266     $exten = 'h';
267         $ext->add($context, $exten, '', new ext_gotoif('$["${FAXSTATUS:0:6}" = "FAILED"]', 'failed'));
268     $ext->add($context, $exten, 'process', new ext_gotoif('$["${FAX_RX_EMAIL}" = ""]','end'));
269     $ext->add($context, $exten, '', new ext_system('${ASTVARLIBDIR}/bin/fax-process.pl --to ${FAX_RX_EMAIL} --from "'.$sender_address['0'].'" --dest "${FROM_DID}" --subject "New fax from ${URIENCODE(${CALLERID(name)})} ${URIENCODE(<${CALLERID(number)}>)}" --attachment fax_${URIENCODE(${CALLERID(number)})}.pdf --type application/pdf --file ${ASTSPOOLDIR}/fax/${UNIQUEID}.tif'));
270
271       $ext->add($context, $exten, 'end', new ext_macro('hangupcall'));
272     $ext->add($context, $exten, 'failed', new ext_noop('FAX ${FAXSTATUS} for: ${FAX_RX_EMAIL} , From: ${CALLERID(all)}'),'process',101);
273       $ext->add($context, $exten, '', new ext_macro('hangupcall'));
274
275         $ext->add('ext-did-0001', 'fax', '', new ext_goto('${FAX_DEST}'));
276         $ext->add('ext-did-0002', 'fax', '', new ext_goto('${FAX_DEST}'));
277
278     // Add fax extension to ivr and announcement as inbound controle may be passed quickly to them and still detection is desired
279     if (function_exists('ivr_list')) {
280             $ivrlist = ivr_list();
281             if(is_array($ivrlist)) foreach($ivrlist as $item) {
282             $ext->add("ivr-".$item['ivr_id'], 'fax', '', new ext_goto('${FAX_DEST}'));
283       }
284     }
285     if (function_exists('announcement_list')) foreach (announcement_list() as $row) {
286       $ext->add('app-announcement-'.$row['announcement_id'], 'fax', '', new ext_goto('${FAX_DEST}'));
287     }
288
289         //write out res_fax.conf and res_fax_digium.conf
290         fax_write_conf();
291  
292     $modulename = 'fax';
293     $fcc = new featurecode($modulename, 'simu_fax');
294     $fc_simu_fax = $fcc->getCodeActive();
295     unset($fcc);
296  
297     if ($fc_simu_fax != '') {
298       $default_address = sql('SELECT value FROM fax_details WHERE `key` = \'FAX_RX_EMAIL\'','getRow');
299       $ext->addInclude('from-internal-additional', 'app-fax'); // Add the include from from-internal
300       $ext->add('app-fax', $fc_simu_fax, '', new ext_setvar('FAX_RX_EMAIL', $default_address[0]));
301       $ext->add('app-fax', $fc_simu_fax, '', new ext_goto('1', 's', 'ext-fax'));
302       $ext->add('app-fax', 'h', '', new ext_macro('hangupcall'));
303     }
304     }
305 }
306
307
308 function fax_get_destinations(){
309     global $db;
310     $sql = "SELECT fax_users.user,fax_users.faxemail,users.name FROM fax_users, users where fax_users.faxenabled = 'true' and users.extension = fax_users.user";
311     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
312     if(DB::IsError($results)) {
313         die_freepbx($results->getMessage()."<br><br>Error selecting from fax");   
314     }
315     return $results;
316 }
317
318 function fax_get_incoming($extension=null,$cidnum=null){
319     global $db;
320     if($extension !== null || $cidnum !== null){
321         $sql="SELECT * FROM fax_incoming WHERE extension = ? AND cidnum = ?";
322         $settings = $db->getRow($sql, array($extension, $cidnum), DB_FETCHMODE_ASSOC);
323         if(isset($settings['legacy_email'])&&$settings['legacy_email']=='NULL'){$settings['legacy_email']=null;}//convert string to real value
324     }else{
325         $sql="SELECT fax_incoming.*, incoming.pricid FROM fax_incoming, incoming where fax_incoming.cidnum=incoming.cidnum and fax_incoming.extension=incoming.extension;";
326         $settings=$db->getAll($sql, DB_FETCHMODE_ASSOC);
327     }
328     return $settings;
329 }
330
331 function fax_get_user($faxext){
332     global $db;
333     if($faxext){
334         $sql="SELECT * FROM fax_users WHERE user = '".$faxext."'";
335         $settings = $db->getRow($sql, DB_FETCHMODE_ASSOC);
336     }else{
337         $sql="SELECT * FROM fax_users";
338         $settings = $db->getAll($sql, DB_FETCHMODE_ASSOC);
339     }
340     if(!is_array($settings)){$settings=array();}//make sure were retuning an array (even if its blank)
341     return $settings;
342 }
343
344 function fax_get_settings(){
345     $settings = sql('SELECT * FROM fax_details', 'getAssoc', 'DB_FETCHMODE_ASSOC');
346     foreach($settings as $setting => $value){
347         $set[$setting]=$value['0'];
348     }
349     if(!is_array($set)){$set=array();}//never return a null value
350     return $set;
351 }
352
353
354 function fax_hook_core($viewing_itemid, $target_menuid){
355     //hmm, not sure why engine_getinfo() isnt being called here?! should probobly read: $info=engine_getinfo();
356     //this is what serves fax code to inbound routing
357     $tabindex=null;
358     $type=isset($_REQUEST['type'])?$_REQUEST['type']:'';
359     $extension=isset($_REQUEST['extension'])?$_REQUEST['extension']:'';
360     $cidnum=isset($_REQUEST['cidnum'])?$_REQUEST['cidnum']:'';
361     $extdisplay=isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:'';
362     
363     //if were editing, get save parms. Get parms
364     if ($type != 'setup'){
365         if(!$extension && !$cidnum){//set $extension,$cidnum if we dont already have them
366             $opts=explode('/', $extdisplay);$extension=$opts['0'];$cidnum=$opts['1'];
367         }
368         $fax=fax_get_incoming($extension,$cidnum);
369     }else{
370       $fax=null;
371     }
372     $html='';
373     if($target_menuid == 'did'){
374     $fax_dahdi_faxdetect=fax_dahdi_faxdetect();
375     $fax_sip_faxdetect=fax_sip_faxdetect();
376     $dahdi=ast_with_dahdi()?_('Dahdi'):_('Zaptel');
377     $fax_detect=fax_detect();
378     $fax_settings=fax_get_settings();
379     //ensure that we are using destination for both fax detect and the regular calls
380         $html='<script type="text/javascript">$(document).ready(function(){
381         $("input[name=Submit]").click(function(){
382             if($("input[name=faxenabled]:checked").val()=="true" && !$("input[name=gotoFAX]:checked").val()){//ensure the user selected a fax destination
383             alert('._('"You have selected Fax Detection on this route. Please select a valid destination to route calls detected as faxes to."').');return false; }    }) });</script>';
384         $html .= '<tr><td colspan="2"><h5>';
385         $html.=_('Fax Detect');
386         $html.='<hr></h5></td></tr>';
387         $html.='<tr>';
388         $html.='<td><a href="#" class="info">';
389         $html.=_("Detect Faxes").'<span>'._("Attempt to detect faxes on this DID.<ul><li>No: No attempts are made to auto-determine the call type; all calls sent to destination below. Use this option if this DID is used exclusively for voice OR fax.</li><li>Yes: try to auto determine the type of call; route to the fax destination if call is a fax, otherwise send to regular destination. Use this option if you receive both voice and fax calls on this line</li>");
390         if($fax_settings['legacy_mode'] == 'yes' || $fax['legacy_email']!==null){
391         $html.=_('<li>Legacy: Same as YES, only you can enter an email address as the destination. This option is ONLY for supporting migrated legacy fax routes. You should upgrade this route by choosing YES, and selecting a valid destination!</li>');
392         }       
393         $html.='</ul></span></a>:</td>';
394         
395         //dont allow detection to be set if we have no valid detection types
396         if(!$fax_dahdi_faxdetect && !$fax_sip_faxdetect && !$fax_detect['nvfax']){
397             $js="if ($(this).val() == 'true'){alert('"._('No fax detection methods found or no valid license. Faxing cannot be enabled.')."');return false;}";
398             $html.='<td><input type="radio" name="faxenabled" value="false" CHECKED />No';
399             $html.='<input type="radio" name="faxenabled" value="true"  onclick="'.$js.'"/>Yes</td></tr>';
400             $html.='</table><table>';
401         }else{
402             /*
403              * show detection options
404              *
405              * js to show/hide the detection settings. Second slide is always in a
406              * callback so that we ait for the fits animation to complete before
407              * playing the second
408              */
409             if($fax['legacy_email']===null && $fax_settings['legacy_mode'] == 'no'){
410                 $jsno="$('.faxdetect').slideUp();";
411                 $jsyes="$('.faxdetect').slideDown();";
412             }else{
413                 $jsno="$('.faxdetect').slideUp();$('.legacyemail').slideUp();";
414                 $jsyes="$('.legacyemail').slideUp('400',function(){
415                             $('.faxdetect').slideDown()
416                         });";
417                 $jslegacy="$('.faxdest27').slideUp('400',function(){
418                                 $('.faxdetect, .legacyemail').not($('.faxdest27')).slideDown();
419                         });";
420             }
421             $html.='<td><input type="radio" name="faxenabled" value="false" CHECKED onclick="'.$jsno.'"/>No';
422             $html.='<input type="radio" name="faxenabled" value="true" '.($fax?'CHECKED':'').' onclick="'.$jsyes.'"/>Yes';
423             if($fax['legacy_email']!==null || $fax_settings['legacy_mode'] == 'yes'){
424                 $html.='<input type="radio" name="faxenabled" value="legacy"'.($fax['legacy_email'] !== null ? ' CHECKED ':'').'onclick="'.$jslegacy.'"/>Legacy';
425             }
426       $html.='</td></tr>';
427             $html.='</table>';
428         }   
429         //fax detection+destinations, hidden if there is fax is disabled
430         $html.='<table class=faxdetect '.($fax?'':'style="display: none;"').'>';   
431         $info=engine_getinfo();
432         $html.='<tr><td width="156px"><a href="#" class="info">'._('Fax Detection type').'<span>'._("Type of fax detection to use.<ul><li>".$dahdi.": use ".$dahdi." fax detection; requires 'faxdetect=' to be set to 'incoming' or 'both' in ".$dahdi.".conf</li><li>Sip: use sip fax detection (t38). Requires asterisk 1.6.2 or greater and 'faxdetect=yes' in the sip config files</li><li>NV Fax Detect: Use NV Fax Detection; Requires NV Fax Detect to be installed and recognized by asterisk</li></ul>").'.</span></a>:</td>';
433         $html.='<td><select name="faxdetection" tabindex="'.++$tabindex.'">';
434         //$html.='<option value="Auto"'.($faxdetection == 'auto' ? 'SELECTED' : '').'>'. _("Auto").'</option>';<li>Auto: allow the system to chose the best fax detection method</li>       
435         $html.='<option value="dahdi" '.($fax['detection'] == 'dahdi' ? 'SELECTED' : '').' '.($fax_dahdi_faxdetect?'':'disabled').'>'.$dahdi.'</option>';
436         $html.='<option value="nvfax"'.($fax['detection'] == 'nvfax' ? 'SELECTED' : '').($fax_detect['nvfax']?'':'disabled').'>'. _("NVFax").'</option>';
437         $html.='<option value="sip" '.($fax['detection'] == 'sip' ? 'SELECTED' : '').' '.((($info['version'] >= "1.6.2") && $fax_sip_faxdetect)?'':'disabled').'>'. _("Sip").'</option>';
438         $html.='</select></td></tr>';
439         
440         $html.='<tr><td><a href="#" class="info">'._("Fax Detection Time").'<span>'._('How long to wait and try to detect fax. Please note that callers to a '.$dahdi.' channel will hear ringing for this amount of time (i.e. the system wont "answer" the call, it will just play ringing)').'.</span></a>:</td>';
441         $html.='<td><select name="faxdetectionwait" tabindex="'.++$tabindex.'">';
442         if(!$fax['detectionwait']){$fax['detectionwait']=4;}//default wait time is 4 second
443         for($i=2;$i < 11; $i++){
444             $html.='<option value="'.$i.'" '.($fax['detectionwait']==$i?'SELECTED':'').'>'.$i.'</option>';   
445         }
446         $html.='</select></td></tr>';
447         if($fax['legacy_email']!==null || $fax_settings['legacy_mode'] == 'yes'){   
448             $html.='</table>';
449             $html.='<table class="legacyemail"'.($fax['legacy_email'] === null ? ' style="display: none;"':'').'>';
450             $html.='<tr ><td><a href="#" class="info">'._("Fax Email Destination").'<span>'._('Address to email faxes to on fax detection.<br />PLEASE NOTE: In this version of FreePBX, you can now set the fax destination from a list of destinations. Extensions/Users can be fax enabled in the user/extension screen and set an email address there. This will create a new destination type that can be selected. To upgrade this option to the full destination list, select YES to Detect Faxes and select a destination. After clicking submit, this route will be upgraded. This Legacy option will no longer be available after the change, it is provided to handle legacy migrations from previous versions of FreePBX only.').'.</span></a>:</td>';
451             $html.='<td><input name="legacy_email" value="'.$fax['legacy_email'].'"></td></tr>';
452             $html.='</table>';
453             $html.='<table class="faxdest27 faxdetect" style="display: none" >';
454     }       
455         $html.='<tr class="faxdest"><td><a href="#" class="info">'._("Fax Destination").'<span>'._('Where to send the call if we detect that its a fax').'.</span></a>:</td></tr>';
456         $html.=$fax_detect?drawselects(isset($fax['destination'])?$fax['destination']:null,'FAX'):'';
457         $html.='</table>';
458         $html.='<table>';
459     }
460     return $html;
461
462 }
463
464 function fax_hookGet_config($engine){
465   global $version;
466     $fax=fax_detect($version);
467     if($fax['module']){ //dont continue unless we have a fax module in asterisk
468         global $ext;
469         global $engine;
470         $routes=fax_get_incoming();
471         foreach($routes as $current => $route){
472       if ($route['detection'] == 'nvfax' && !$fax['nvfax']) {
473         //TODO: add notificatoin to notification panel that this was skipped because NVFaxdetec not present
474         continue; // skip this one if there is no NVFaxdetect installed on this system
475       }
476             if($route['extension']=='' && $route['cidnum']){//callerID only
477                 $extension='s/'.$route['cidnum'];
478                 $context=($route['pricid']=='CHECKED')?'ext-did-0001':'ext-did-0002';
479             }else{
480                 if(($route['extension'] && $route['cidnum'])||($route['extension']=='' && $route['cidnum']=='')){//callerid+did / any/any
481                     $context='ext-did-0001';
482                 }else{//did only
483                     $context='ext-did-0002';
484                 }
485                 $extension=($route['extension']!=''?$route['extension']:'s').($route['cidnum']==''?'':'/'.$route['cidnum']);
486             }
487       if ($route['legacy_email'] === null) {
488               $ext->splice($context, $extension, 'dest-ext', new ext_setvar('FAX_DEST','"'.$route['destination'].'"'));
489       } else {
490               $ext->splice($context, $extension, 'dest-ext', new ext_setvar('FAX_DEST','"ext-fax,s,1"'));
491         if ($route['legacy_email']) {
492                 $fax_rx_email = $route['legacy_email'];
493         } else {
494           if (!isset($default_fax_rx_email)) {
495                   $default_address = sql('SELECT value FROM fax_details WHERE `key` = \'fax_rx_email\'','getRow');
496             $default_fax_rx_email = $default_address[0];
497           }
498           $fax_rx_email = $default_fax_rx_email;
499         }
500               $ext->splice($context, $extension, 'dest-ext', new ext_setvar('FAX_RX_EMAIL',$fax_rx_email));
501       }
502           $ext->splice($context, $extension, 'dest-ext', new ext_answer(''));
503       if ($route['detection'] == 'nvfax') {
504             $ext->splice($context, $extension, 'dest-ext', new ext_playtones('ring'));
505             $ext->splice($context, $extension, 'dest-ext', new ext_nvfaxdetect($route['detectionwait']."|t"));
506       } else {
507             $ext->splice($context, $extension, 'dest-ext', new ext_wait($route['detectionwait']));
508       }
509         }
510     }
511 }
512
513 function fax_hookProcess_core(){
514     $display=isset($_REQUEST['display'])?$_REQUEST['display']:'';
515     $action=isset($_REQUEST['action'])?$_REQUEST['action']:'';
516     if ($display == 'did' && $action!=''){
517
518       $cidnum=isset($_REQUEST['cidnum'])?$_REQUEST['cidnum']:'';
519       $extension=isset($_REQUEST['extension'])?$_REQUEST['extension']:'';
520       $extdisplay=isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:'';
521       $enabled=isset($_REQUEST['faxenabled'])?$_REQUEST['faxenabled']:'false';
522       $detection=isset($_REQUEST['faxdetection'])?$_REQUEST['faxdetection']:'';
523       $detectionwait=isset($_REQUEST['faxdetectionwait'])?$_REQUEST['faxdetectionwait']:'';
524       $dest=(isset($_REQUEST['gotoFAX'])?$_REQUEST['gotoFAX'].'FAX':null);
525       $dest=isset($_REQUEST[$dest])?$_REQUEST[$dest]:'';
526     if ($enabled != 'legacy') {
527       $legacy_email = null;
528     } else {
529         $legacy_email=isset($_REQUEST['legacy_email'])?$_REQUEST['legacy_email']:'';
530     }
531         fax_delete_incoming($extdisplay);    //remove mature entry on edit or delete
532         if (($action == 'edtIncoming' || $action == 'addIncoming') && $enabled != 'false'){
533             fax_save_incoming($cidnum,$extension,$enabled,$detection,$detectionwait,$dest,$legacy_email);
534         }
535     }
536 }
537
538
539 function fax_save_incoming($cidnum,$extension,$enabled,$detection,$detectionwait,$dest,$legacy_email){
540     global $db;
541   $legacy_email $legacy_email === null ? 'NULL' : "'".$db->escapeSimple("$legacy_email")."'";
542     sql("INSERT INTO fax_incoming (cidnum, extension, detection, detectionwait, destination, legacy_email) VALUES ('".$db->escapeSimple($cidnum)."', '".$db->escapeSimple($extension)."', '".$db->escapeSimple($detection)."', '".$db->escapeSimple($detectionwait)."', '".$db->escapeSimple($dest)."',".$legacy_email.")");
543 }
544
545 function fax_save_settings($settings){
546     global $db;
547     if (is_array($settings)) foreach($settings as $key => $value){
548         sql("REPLACE INTO fax_details (`key`, `value`) VALUES ('".$key."','".$db->escapeSimple($value)."')");
549     }
550 }
551
552 function fax_save_user($faxext,$faxenabled,$faxemail){
553     global $db;
554     $faxext=$db->escapeSimple($faxext);
555     $faxenabled=$db->escapeSimple($faxenabled);
556     $faxemail=$db->escapeSimple($faxemail);
557     sql('REPLACE INTO fax_users (user, faxenabled, faxemail) VALUES ("'.$faxext.'","'.$faxenabled.'","'.$faxemail.'")');
558 }
559
560 function fax_sip_faxdetect(){
561     global $asterisk_conf;
562   return true;
563 }
564
565 //write out res_fax.conf and res_fax_digium.conf
566 function fax_write_conf(){
567     global $amp_conf, $WARNING_BANNER;
568     $set=fax_get_settings();
569     //res_fax.conf
570     $data=$WARNING_BANNER;
571     $data.="[general]\n";
572     $data.="#include res_fax_custom.conf\n";
573     $data.='minrate='.$set['minrate']."\n";
574     $data.='maxrate='.$set['maxrate']."\n";
575     $file=fopen($amp_conf['ASTETCDIR'].'/res_fax.conf','w');
576     fwrite($file, $data);
577     fclose($file);
578     
579     //res_fax_digium.conf
580     $data=$WARNING_BANNER;
581     $data.="[general]\n";
582     $data.="#include res_fax_digium_custom.conf\n";
583     $data.='ecm='.$set['ecm']."\n";
584     $file=fopen($amp_conf['ASTETCDIR'].'/res_fax_digium.conf','w');
585     fwrite($file, $data);
586     fclose($file);
587 }
588 ?>
589
Note: See TracBrowser for help on using the browser.