Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

648 lines
22 KiB

2 years ago
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <markjcrane@fusionpbx.com>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2022
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <markjcrane@fusionpbx.com>
  20. James Rose <james.o.rose@gmail.com>
  21. */
  22. $output_type = "file"; //file or console
  23. if (defined('STDIN')) {
  24. //get the document root php file must be executed with the full path
  25. $document_root = str_replace("\\", "/", $_SERVER["PHP_SELF"]);
  26. $document_root = str_replace("\\", "/", $_SERVER["PHP_SELF"]);
  27. preg_match("/^(.*)\/secure\/.*$/", $document_root, $matches);
  28. $document_root = $matches[1];
  29. //set the include path
  30. set_include_path($document_root);
  31. $_SERVER["DOCUMENT_ROOT"] = $document_root;
  32. //echo "$document_root is document_root\n";
  33. }
  34. $IS_WINDOWS = stristr(PHP_OS, 'WIN') ? true : false;
  35. if (!function_exists('exec_in_dir')) {
  36. function exec_in_dir($dir, $cmd, &$ok) {
  37. $args = func_get_args();
  38. $cwd = getcwd();
  39. chdir($dir);
  40. $output = array();
  41. $ret = 0;
  42. $result = exec($cmd, $output, $ret);
  43. if ($cwd)
  44. chdir($cwd);
  45. $ok = ($ret == 0);
  46. return implode("\n", $output);
  47. }
  48. }
  49. if (!function_exists('correct_path')) {
  50. function correct_path($p) {
  51. global $IS_WINDOWS;
  52. if ($IS_WINDOWS) {
  53. return str_replace('/', '\\', $p);
  54. }
  55. return $p;
  56. }
  57. }
  58. if (!function_exists('path_join')) {
  59. function path_join() {
  60. $args = func_get_args();
  61. $paths = array();
  62. foreach ($args as $arg) {
  63. $paths = array_merge($paths, (array)$arg);
  64. }
  65. $prefix = null;
  66. foreach($paths as &$path) {
  67. if ($prefix === null && strlen($path) > 0) {
  68. if (substr($path, 0, 1) == '/') $prefix = '/';
  69. else $prefix = '';
  70. }
  71. $path = trim( $path, '/' );
  72. }
  73. if ($prefix === null) {
  74. return '';
  75. }
  76. $paths = array_filter($paths);
  77. return $prefix . implode('/', $paths);
  78. }
  79. }
  80. if (!function_exists('tiff2pdf')) {
  81. function tiff2pdf($tiff_file_name) {
  82. //convert the tif to a pdf
  83. //Ubuntu: apt-get install libtiff-tools
  84. global $IS_WINDOWS;
  85. if (!file_exists($tiff_file_name)) {
  86. echo "tiff file does not exists";
  87. return false; // "tiff file does not exists";
  88. }
  89. $GS = $IS_WINDOWS ? 'gswin32c' : 'gs';
  90. $tiff_file = pathinfo($tiff_file_name);
  91. $dir_fax = $tiff_file['dirname'];
  92. $fax_file_name = $tiff_file['filename'];
  93. $pdf_file_name = path_join($dir_fax, $fax_file_name . '.pdf');
  94. if (file_exists($pdf_file_name)) {
  95. return $pdf_file_name;
  96. }
  97. $dir_fax_temp = $_SESSION['server']['temp']['dir'];
  98. if (!$dir_fax_temp) {
  99. $dir_fax_temp = path_join(dirname($dir_fax), 'temp');
  100. }
  101. if (!file_exists($dir_fax_temp)) {
  102. echo "can not create temporary directory";
  103. return false; //
  104. }
  105. $cmd = "tiffinfo " . correct_path($tiff_file_name) . ' | grep "Resolution:"';
  106. $ok = false;
  107. $resp = exec_in_dir($dir_fax, $cmd, $ok);
  108. if (!$ok) {
  109. echo "can not find fax resoulution";
  110. return false; // "can not find fax resoulution"
  111. }
  112. $ppi_w = 0;
  113. $ppi_h = 0;
  114. $tmp = array();
  115. if (preg_match('/Resolution.*?(\d+).*?(\d+)/', $resp, $tmp)) {
  116. $ppi_w = $tmp[1];
  117. $ppi_h = $tmp[2];
  118. }
  119. $cmd = "tiffinfo " . $tiff_file_name . ' | grep "Image Width:"';
  120. $resp = exec_in_dir($dir_fax, $cmd, $ok);
  121. if (!$ok) {
  122. echo "can not find fax size";
  123. return false; // "can not find fax size"
  124. }
  125. $pix_w = 0;
  126. $pix_h = 0;
  127. $tmp = array();
  128. if (preg_match('/Width.*?(\d+).*?Length.*?(\d+)/', $resp, $tmp)) {
  129. $pix_w = $tmp[1];
  130. $pix_h = $tmp[2];
  131. }
  132. $page_width = $pix_w / $ppi_w;
  133. $page_height = $pix_h / $ppi_h;
  134. $page_size = 'a4';
  135. if (($page_width > 8.4) && ($page_height > 13)) {
  136. $page_width = 8.5;
  137. $page_height = 14;
  138. $page_size = 'legal';
  139. }
  140. elseif (($page_width > 8.4) && ($page_height < 12)) {
  141. $page_width = 8.5;
  142. $page_height = 11;
  143. $page_size = 'letter';
  144. }
  145. elseif (($page_width < 8.4) && ($page_height > 11)) {
  146. $page_width = 8.3;
  147. $page_height = 11.7;
  148. $page_size = 'a4';
  149. }
  150. $page_width = sprintf('%.4f', $page_width);
  151. $page_height = sprintf('%.4f', $page_height);
  152. $cmd = implode(' ', array('tiff2pdf',
  153. '-o', correct_path($pdf_file_name),
  154. correct_path($tiff_file_name),
  155. ));
  156. $resp = exec_in_dir($dir_fax, $cmd, $ok);
  157. if (!file_exists($pdf_file_name)) {
  158. echo "can not create pdf: $resp";
  159. return false;
  160. }
  161. return $pdf_file_name;
  162. }
  163. }
  164. if (!function_exists('fax_split_dtmf')) {
  165. function fax_split_dtmf(&$fax_number, &$fax_dtmf) {
  166. $tmp = array();
  167. $fax_dtmf = '';
  168. if (preg_match('/^\s*(.*?)\s*\((.*)\)\s*$/', $fax_number, $tmp)) {
  169. $fax_number = $tmp[1];
  170. $fax_dtmf = $tmp[2];
  171. }
  172. }
  173. }
  174. //includes
  175. if (!defined('STDIN')) { include "root.php"; }
  176. require_once "resources/require.php";
  177. include "resources/classes/event_socket.php";
  178. include "resources/phpmailer/class.phpmailer.php";
  179. include "resources/phpmailer/class.smtp.php"; // optional, gets called from within class.phpmailer.php if not already loaded
  180. //set php ini values
  181. ini_set(max_execution_time,900); //15 minutes
  182. ini_set('memory_limit', '96M');
  183. //start the to cache the output
  184. if ($output_type == "file") {
  185. ob_end_clean();
  186. ob_start();
  187. }
  188. //add a delimeter to the log
  189. echo "\n---------------------------------\n";
  190. //get the parameters and save them as variables
  191. $php_version = substr(phpversion(), 0, 1);
  192. if ($php_version == '4') {
  193. $domain_name = $_REQUEST["domain"];
  194. $fax_email = $_REQUEST["email"];
  195. $fax_extension = $_REQUEST["extension"];
  196. $fax_file = $_REQUEST["name"];
  197. $fax_messages = $_REQUEST["messages"];
  198. $caller_id_name = $_REQUEST["caller_id_name"];
  199. $caller_id_number = $_REQUEST["caller_id_number"];
  200. $fax_relay = $_REQUEST["retry"];
  201. $mail_from_address = $_REQUEST["mailfrom_address"];
  202. }
  203. else {
  204. $tmp_array = explode("=", $_SERVER["argv"][1]);
  205. $fax_email = $tmp_array[1];
  206. unset($tmp_array);
  207. $tmp_array = explode("=", $_SERVER["argv"][2]);
  208. $fax_extension = $tmp_array[1];
  209. unset($tmp_array);
  210. $tmp_array = explode("=", $_SERVER["argv"][3]);
  211. $fax_file = $tmp_array[1];
  212. unset($tmp_array);
  213. $tmp_array = explode("=", $_SERVER["argv"][4]);
  214. $fax_messages = $tmp_array[1];
  215. unset($tmp_array);
  216. $tmp_array = explode("=", $_SERVER["argv"][5]);
  217. $domain_name = $tmp_array[1];
  218. unset($tmp_array);
  219. $tmp_array = explode("=", $_SERVER["argv"][6]);
  220. $caller_id_name = $tmp_array[1];
  221. unset($tmp_array);
  222. $tmp_array = explode("=", $_SERVER["argv"][7]);
  223. $caller_id_number = $tmp_array[1];
  224. unset($tmp_array);
  225. $tmp_array = explode("=", $_SERVER["argv"][8]);
  226. $fax_relay = $tmp_array[1];
  227. unset($tmp_array);
  228. $tmp_array = explode("=", $_SERVER["argv"][9]);
  229. $fax_prefix = $tmp_array[1];
  230. unset($tmp_array);
  231. $tmp_array = explode("=", $_SERVER["argv"][10]);
  232. $mail_from_address = $tmp_array[1];
  233. unset($tmp_array);
  234. //$tmp_array = explode("=", $_SERVER["argv"][10]);
  235. //$destination_number = $tmp_array[1];
  236. //unset($tmp_array);
  237. }
  238. //get the fax file name (only) if a full path
  239. $fax_path = pathinfo($fax_file);
  240. $fax_file_only = $fax_path['basename'];
  241. $fax_file_name = $fax_path['filename'];
  242. $dir_fax = $fax_path['dirname'];
  243. //get the domain_uuid from the database
  244. $sql = "select * from v_domains ";
  245. $sql .= "where domain_name = :domain_name ";
  246. $parameters['domain_name'] = $domain_name;
  247. $database = new database;
  248. $result = $database->select($sql, $parameters, 'all');
  249. if (is_array($result) && @sizeof($result) != 0) {
  250. foreach ($result as &$row) {
  251. //set the domain variables
  252. $domain_uuid = $row["domain_uuid"];
  253. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  254. $_SESSION["domain_name"] = $domain_name;
  255. //set the setting arrays
  256. $domain = new domains();
  257. $domain->db = $db;
  258. $domain->set();
  259. }
  260. }
  261. unset($sql, $parameters, $result);
  262. //prepare smtp server settings
  263. $email_from_address = $_SESSION['email']['smtp_from']['text'];
  264. $email_from_name = $_SESSION['email']['smtp_from_name']['text'];
  265. if (isset($_SESSION['fax']['smtp_from']['text']) && strlen($_SESSION['fax']['smtp_from']['text']) > 0) {
  266. $email_from_address = $_SESSION['fax']['smtp_from']['text'];
  267. }
  268. if (isset($_SESSION['fax']['smtp_from_name']['text']) && strlen($_SESSION['fax']['smtp_from_name']['text']) > 0) {
  269. $email_from_name = $_SESSION['fax']['smtp_from_name']['text'];
  270. }
  271. //get the fax settings from the database
  272. $sql = "select * from v_fax ";
  273. $sql .= "where domain_uuid = :domain_uuid ";
  274. $sql .= "and fax_extension = :fax_extension ";
  275. $parameters['domain_uuid'] = $domain_uuid;
  276. $parameters['fax_extension'] = $fax_extension;
  277. $database = new database;
  278. $row = $database->select($sql, $parameters, 'row');
  279. if (is_array($row) && @sizeof($row) != 0) {
  280. $fax_email = $row["fax_email"];
  281. $fax_uuid = $row["fax_uuid"];
  282. $fax_accountcode = $row["fax_accountcode"];
  283. $fax_prefix = $row["fax_prefix"];
  284. $fax_pin_number = $row["fax_pin_number"];
  285. $fax_caller_id_name = $row["fax_caller_id_name"];
  286. $fax_caller_id_number = $row["fax_caller_id_number"];
  287. $fax_forward_number = $row["fax_forward_number"];
  288. $fax_description = $row["fax_description"];
  289. $fax_email_inbound_subject_tag = $row['fax_email_inbound_subject_tag'];
  290. $mail_to_address = $fax_email;
  291. }
  292. unset($sql, $parameters, $row);
  293. //set the fax directory
  294. if (!file_exists($dir_fax) || !file_exists(path_join($dir_fax, $fax_file_only))) {
  295. $dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$domain_name.'/'.$fax_extension.'/inbox';
  296. if (!file_exists($dir_fax) || !file_exists(path_join($dir_fax, $fax_file_only))) {
  297. $dir_fax = $_SESSION['switch']['storage']['dir'].'/fax/'.$fax_extension.'/inbox';
  298. }
  299. }
  300. $fax_file = path_join($dir_fax, $fax_file_only);
  301. //used for debug
  302. echo "fax_prefix: $fax_prefix\n";
  303. echo "mail_to_adress: $mail_to_address\n";
  304. echo "fax_email: $fax_email\n";
  305. echo "fax_extension: $fax_extension\n";
  306. echo "fax_name: $fax_file_only\n";
  307. echo "dir_fax: $dir_fax\n";
  308. echo "full_path: $fax_file\n";
  309. $pdf_file = tiff2pdf($fax_file);
  310. echo "file: $pdf_file \n";
  311. if (!$pdf_file) {
  312. $fax_file_warning = 'warning: Fax image not available on server.';
  313. }
  314. else{
  315. $fax_file_warning = '';
  316. }
  317. echo "pdf file: $pdf_file\n";
  318. //forward the fax
  319. if (file_exists($fax_file)) {
  320. if (strpos($fax_file_name,'#') !== false) {
  321. $tmp = explode("#",$fax_file_name);
  322. $fax_forward_number = $fax_prefix.$tmp[0];
  323. }
  324. if (isset($fax_forward_number) && strlen($fax_forward_number) > 0) {
  325. //show info
  326. echo "fax_forward_number: $fax_forward_number\n";
  327. //add fax to the fax queue or send it directly
  328. if ($_SESSION['fax_queue']['enabled']['boolean'] == 'true') {
  329. //build an array to add the fax to the queue
  330. $array['fax_queue'][0]['fax_queue_uuid'] = uuid();
  331. $array['fax_queue'][0]['domain_uuid'] = $domain_uuid;
  332. $array['fax_queue'][0]['fax_uuid'] = $fax_uuid;
  333. $array['fax_queue'][0]['fax_date'] = 'now()';
  334. $array['fax_queue'][0]['hostname'] = gethostname();
  335. $array['fax_queue'][0]['fax_caller_id_name'] = $fax_caller_id_name;
  336. $array['fax_queue'][0]['fax_caller_id_number'] = $fax_caller_id_number;
  337. $array['fax_queue'][0]['fax_number'] = $fax_forward_number;
  338. $array['fax_queue'][0]['fax_prefix'] = $fax_prefix;
  339. $array['fax_queue'][0]['fax_email_address'] = $mail_to_address;
  340. $array['fax_queue'][0]['fax_file'] = $fax_file;
  341. $array['fax_queue'][0]['fax_status'] = 'waiting';
  342. $array['fax_queue'][0]['fax_retry_count'] = 0;
  343. $array['fax_queue'][0]['fax_accountcode'] = $fax_accountcode;
  344. //add temporary permisison
  345. $p = new permissions;
  346. $p->add('fax_queue_add', 'temp');
  347. //save the data
  348. $database = new database;
  349. $database->app_name = 'fax queue';
  350. $database->app_uuid = '3656287f-4b22-4cf1-91f6-00386bf488f4';
  351. $database->save($array);
  352. //remove temporary permisison
  353. $p->delete('fax_queue_add', 'temp');
  354. //add message to show in the browser
  355. message::add($text['confirm-queued']);
  356. }
  357. else {
  358. fax_split_dtmf($fax_forward_number, $fax_dtmf);
  359. $fax_send_mode = $_SESSION['fax']['send_mode']['text'];
  360. if (strlen($fax_send_mode) == 0) {
  361. $fax_send_mode = 'direct';
  362. }
  363. $route_array = outbound_route_to_bridge($domain_uuid, $fax_forward_number);
  364. if (count($route_array) == 0) {
  365. //send the internal call to the registered extension
  366. $fax_uri = "user/".escapeshellarg($fax_forward_number)."@".escapeshellarg($domain_name);
  367. $fax_variables = "";
  368. }
  369. else {
  370. //send the external call
  371. $fax_uri = $route_array[0];
  372. $fax_variables = "";
  373. foreach($_SESSION['fax']['variable'] as $variable) {
  374. $fax_variables .= escapeshellarg($variable).",";
  375. }
  376. }
  377. //build the dial string
  378. $dial_string = "absolute_codec_string='PCMU,PCMA',";
  379. $dial_string .= "accountcode='" . escapeshellarg($fax_accountcode) . "',";
  380. $dial_string .= "sip_h_X-accountcode='" . escapeshellarg($fax_accountcode) . "',";
  381. $dial_string .= "domain_uuid=" . escapeshellarg($domain_uuid) . ",";
  382. $dial_string .= "domain_name=" . escapeshellarg($domain_name) . ",";
  383. $dial_string .= "origination_caller_id_name='" . escapeshellarg($fax_caller_id_name) . "',";
  384. $dial_string .= "origination_caller_id_number='" . escapeshellarg($fax_caller_id_number) . "',";
  385. $dial_string .= "fax_ident='" . escapeshellarg($fax_caller_id_number) . "',";
  386. $dial_string .= "fax_header='" . escapeshellarg($fax_caller_id_name) . "',";
  387. $dial_string .= "fax_file='" . escapeshellarg($fax_file) . "',";
  388. if ($fax_send_mode != 'queue') {
  389. //add more ot the dial string
  390. $dial_string .= $fax_variables;
  391. $dial_string .= "mailto_address='" . escapeshellarg($mail_to_address) . "',";
  392. $dial_string .= "mailfrom_address='" . escapeshellarg($mail_from_address) . "',";
  393. $dial_string .= "fax_uri=" . escapeshellarg($fax_uri) . ",";
  394. $dial_string .= "fax_retry_attempts=1" . ",";
  395. $dial_string .= "fax_retry_limit=20" . ",";
  396. $dial_string .= "fax_retry_sleep=180" . ",";
  397. $dial_string .= "fax_verbose=true" . ",";
  398. $dial_string .= "fax_use_ecm=off" . ",";
  399. $dial_string .= "api_hangup_hook='lua fax_retry.lua'";
  400. $dial_string = "{" . $dial_string . "}" . escapeshellarg($fax_uri)." &txfax('".escapeshellarg($fax_file)."')";
  401. //get the event socket information
  402. $sql = "select * from v_settings ";
  403. $database = new database;
  404. $row = $database->select($sql, $parameters, 'row');
  405. if (is_array($row) && @sizeof($row) != 0) {
  406. $event_socket_ip_address = $row["event_socket_ip_address"];
  407. $event_socket_port = $row["event_socket_port"];
  408. $event_socket_password = $row["event_socket_password"];
  409. }
  410. unset($sql);
  411. //create the event socket connection
  412. $fp = event_socket_create($event_socket_ip_address, $event_socket_port, $event_socket_password);
  413. //send the command with event socket
  414. if ($fp) {
  415. //prepare the fax originate command
  416. $cmd = "api originate ".$dial_string;
  417. //send info to the log
  418. echo "fax forward\n";
  419. echo $cmd."\n";
  420. //send the command to event socket
  421. $response = event_socket_request($fp, $cmd);
  422. $response = str_replace("\n", "", $response);
  423. //send info to the log
  424. echo "response: ".$response."\n";
  425. //get the uuid
  426. $uuid = str_replace("+OK ", "", $response);
  427. //close event socket
  428. fclose($fp);
  429. }
  430. }
  431. }
  432. }
  433. }
  434. //send the email
  435. if (strlen($fax_email) > 0 && file_exists($fax_file)) {
  436. //get the language code
  437. $language_code = $_SESSION['domain']['language']['code'];
  438. //get the template subcategory
  439. if ($fax_relay == 'true') {
  440. $template_subcategory = 'relay';
  441. }
  442. else {
  443. $template_subcategory = 'inbound';
  444. }
  445. //get the email template from the database
  446. if (isset($fax_email) && strlen($fax_email) > 0) {
  447. $sql = "select template_subject, template_body from v_email_templates ";
  448. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  449. $sql .= "and template_language = :template_language ";
  450. $sql .= "and template_category = :template_category ";
  451. $sql .= "and template_subcategory = :template_subcategory ";
  452. $sql .= "and template_type = :template_type ";
  453. $sql .= "and template_enabled = 'true' ";
  454. $parameters['domain_uuid'] = $domain_uuid;
  455. $parameters['template_language'] = $language_code;
  456. $parameters['template_category'] = 'fax';
  457. $parameters['template_subcategory'] = $template_subcategory;
  458. $parameters['template_type'] = 'html';
  459. $database = new database;
  460. $row = $database->select($sql, $parameters, 'row');
  461. if (is_array($row)) {
  462. $email_subject = $row['template_subject'];
  463. $email_body = $row['template_body'];
  464. }
  465. unset($sql, $parameters);
  466. }
  467. //replace variables in email subject
  468. $email_subject = str_replace('${domain_name}', $domain_name, $email_subject);
  469. $email_subject = str_replace('${fax_file_name}', $fax_file_name, $email_subject);
  470. $email_subject = str_replace('${fax_extension}', $fax_extension, $email_subject);
  471. $email_subject = str_replace('${fax_messages}', $fax_messages, $email_subject);
  472. $email_subject = str_replace('${fax_file_warning}', $fax_file_warning, $email_subject);
  473. $email_subject = str_replace('${fax_subject_tag}', $fax_email_inbound_subject_tag, $email_subject);
  474. //replace variables in email body
  475. $email_body = str_replace('${domain_name}', $domain_name, $email_body);
  476. $email_body = str_replace('${fax_file_name}', $fax_file_name, $email_body);
  477. $email_body = str_replace('${fax_extension}', $fax_extension, $email_body);
  478. $email_body = str_replace('${fax_messages}', $fax_messages, $email_body);
  479. $email_body = str_replace('${fax_file_warning}', $fax_file_warning, $email_body);
  480. $email_body = str_replace('${fax_subject_tag}', $fax_email_inbound_subject_tag, $email_body);
  481. //debug info
  482. //echo "<hr />\n";
  483. //echo "email_address ".$fax_email."<br />\n";
  484. //echo "email_subject ".$email_subject."<br />\n";
  485. //echo "email_body ".$email_body."<br />\n";
  486. //echo "<hr />\n";
  487. //send the email
  488. if (isset($fax_email) && strlen($fax_email) > 0) {
  489. //add the attachment
  490. if (strlen($fax_file_name) > 0) {
  491. $email_attachments[0]['type'] = 'file';
  492. if ($pdf_file && file_exists($pdf_file)) {
  493. $email_attachments[0]['name'] = $fax_file_name.'.pdf';
  494. $email_attachments[0]['value'] = $pdf_file;
  495. }
  496. else {
  497. $email_attachments[0]['name'] = $fax_file_name.'.tif';
  498. $email_attachments[0]['value'] = $fax_file;
  499. }
  500. }
  501. //$email_response = send_email($email_address, $email_subject, $email_body);
  502. $email = new email;
  503. $email->recipients = $fax_email;
  504. $email->subject = $email_subject;
  505. $email->body = $email_body;
  506. $email->from_address = $email_from_address;
  507. $email->from_name = $email_from_name;
  508. $email->attachments = $email_attachments;
  509. //$email->debug_level = 3;
  510. $response = $mail->error;
  511. $sent = $email->send();
  512. }
  513. //output to the log
  514. echo "email_from_address: ".$email_from_address."\n";
  515. echo "email_from_name: ".$email_from_address."\n";
  516. echo "email_subject: $email_subject\n";
  517. //send the email
  518. if ($sent) {
  519. echo "Mailer Error";
  520. $email_status='failed';
  521. }
  522. else {
  523. echo "Message sent!";
  524. $email_status='ok';
  525. }
  526. }
  527. //when sending an email the following files are created:
  528. // /usr/local/freeswitch/storage/fax
  529. // emailed_faxes.log - this is a log of all the faxes we have successfully emailed. (note that we need to work out how to rotate this log)
  530. // failed_fax_emails.log - this is a log of all the faxes we have failed to email. This log is in the form of instructions that we can re-execute in order to retry.
  531. // Whenever this exists there should be an at job present to run it sometime in the next 3 minutes (check with atq). If we succeed in sending the messages
  532. // this file will be removed.
  533. // /tmp
  534. // fax_email_retry.sh - this is the renamed failed_fax_emails.log and is created only at the point in time that we are trying to re-send the emails. Note however
  535. // that this will continue to exist even if we succeed as we do not delete it when finished.
  536. // failed_fax_emails.sh - this is created when we have a email we need to re-send. At the time it is created, an at job is created to execute it in 3 minutes time,
  537. // this allows us to try sending the email again at that time. If the file exists but there is no at job this is because there are no longer any emails queued
  538. // as we have successfully sent them all.
  539. if ($_SESSION['fax_queue']['enabled']['boolean'] != 'true' && strlen($fax_email) > 0 && file_exists($fax_file)) {
  540. if (stristr(PHP_OS, 'WIN')) {
  541. //not compatible with windows
  542. }
  543. else {
  544. $fax_to_email_queue_dir = $_SESSION['switch']['storage']['dir']."/fax";
  545. if ($email_status == 'ok') {
  546. //log the success
  547. $fp = fopen($fax_to_email_queue_dir."/emailed_faxes.log", "a");
  548. fwrite($fp, $fax_file_name." received on ".$fax_extension." emailed to ".$fax_email." ".$fax_messages."\n");
  549. fclose($fp);
  550. }
  551. }
  552. }
  553. //open the file for writing
  554. if ($output_type == "file") {
  555. //open the file
  556. $fp = fopen($_SESSION['server']['temp']['dir']."/fax_to_email.log", "w");
  557. //get the output from the buffer
  558. $content = ob_get_contents();
  559. //clean the buffer
  560. ob_end_clean();
  561. //write the contents of the buffer
  562. fwrite($fp, $content);
  563. fclose($fp);
  564. }
  565. ?>