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.

382 lines
13 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) 2015 - 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. //includes
  23. include "root.php";
  24. require_once "resources/require.php";
  25. require_once "resources/functions/object_to_array.php";
  26. require_once "resources/functions/parse_message.php";
  27. require_once "resources/classes/text.php";
  28. //get accounts to monitor
  29. $sql = "select * from v_fax ";
  30. $sql .= "where fax_email_connection_host <> '' ";
  31. $sql .= "and fax_email_connection_host is not null ";
  32. $database = new database;
  33. $result = $database->select($sql, null, 'all');
  34. unset($sql);
  35. function arr_to_map(&$arr){
  36. if(is_array($arr)){
  37. $map = Array();
  38. foreach($arr as &$val){
  39. $map[$val] = true;
  40. }
  41. return $map;
  42. }
  43. return false;
  44. }
  45. if (is_array($result) && @sizeof($result) != 0) {
  46. //load default settings
  47. $default_settings = load_default_settings();
  48. //get event socket connection parameters
  49. $sql = "select event_socket_ip_address, event_socket_port, event_socket_password from v_settings";
  50. $database = new database;
  51. $row = $database->select($sql, null, 'row');
  52. $event_socket['ip_address'] = $row['event_socket_ip_address'];
  53. $event_socket['port'] = $row['event_socket_port'];
  54. $event_socket['password'] = $row['event_socket_password'];
  55. unset($sql, $row);
  56. $fax_cover_font_default = $_SESSION['fax']['cover_font']['text'];
  57. $fax_allowed_extension_default = arr_to_map($_SESSION['fax']['allowed_extension']);
  58. if($fax_allowed_extension_default == false){
  59. $tmp = array('.pdf', '.tiff', '.tif');
  60. $fax_allowed_extension_default = arr_to_map($tmp);
  61. }
  62. foreach ($result as $row) {
  63. //get fax server and account connection details
  64. $fax_uuid = $row["fax_uuid"];
  65. $domain_uuid = $row["domain_uuid"];
  66. $fax_extension = $row["fax_extension"];
  67. $fax_email = $row["fax_email"];
  68. $fax_pin_number = $row["fax_pin_number"];
  69. $fax_caller_id_name = $row["fax_caller_id_name"];
  70. $fax_caller_id_number = $row["fax_caller_id_number"];
  71. $fax_email_connection_type = $row["fax_email_connection_type"];
  72. $fax_email_connection_host = $row["fax_email_connection_host"];
  73. $fax_email_connection_port = $row["fax_email_connection_port"];
  74. $fax_email_connection_security = $row["fax_email_connection_security"];
  75. $fax_email_connection_validate = $row["fax_email_connection_validate"];
  76. $fax_email_connection_username = $row["fax_email_connection_username"];
  77. $fax_email_connection_password = $row["fax_email_connection_password"];
  78. $fax_email_connection_mailbox = $row["fax_email_connection_mailbox"];
  79. $fax_email_outbound_subject_tag = $row["fax_email_outbound_subject_tag"];
  80. $fax_email_outbound_authorized_senders = strtolower($row["fax_email_outbound_authorized_senders"]);
  81. $fax_send_greeting = $row["fax_send_greeting"];
  82. $fax_accountcode = $row["accountcode"];
  83. $fax_toll_allow = $row["fax_toll_allow"];
  84. //load default settings, then domain settings over top
  85. unset($_SESSION);
  86. $_SESSION = $default_settings;
  87. load_domain_settings($domain_uuid);
  88. $fax_cover_font = $_SESSION['fax']['cover_font']['text'];
  89. if(strlen($fax_cover_font) == 0){
  90. $fax_cover_font = $fax_cover_font_default;
  91. }
  92. $fax_allowed_extension = arr_to_map($_SESSION['fax']['allowed_extension']);
  93. if($fax_allowed_extension == false) {
  94. $fax_allowed_extension = $fax_allowed_extension_default;
  95. }
  96. //load event socket connection parameters
  97. $_SESSION['event_socket_ip_address'] = $event_socket['ip_address'];
  98. $_SESSION['event_socket_port'] = $event_socket['port'];
  99. $_SESSION['event_socket_password'] = $event_socket['password'];
  100. //get domain name, set local and session variables
  101. $sql = "select domain_name from v_domains where domain_uuid = :domain_uuid ";
  102. $parameters['domain_uuid'] = $domain_uuid;
  103. $database = new database;
  104. $row = $database->select($sql, $parameters, 'row');
  105. $domain_name = $row['domain_name'];
  106. $_SESSION['domain_name'] = $row['domain_name'];
  107. $_SESSION['domain_uuid'] = $domain_uuid;
  108. unset($sql, $parameters, $row);
  109. //set needed variables
  110. $fax_page_size = $_SESSION['fax']['page_size']['text'];
  111. $fax_resolution = $_SESSION['fax']['resolution']['text'];
  112. $fax_header = $_SESSION['fax']['cover_header']['text'];
  113. $fax_footer = $_SESSION['fax']['cover_footer']['text'];
  114. $fax_sender = $fax_caller_id_name;
  115. //open account connection
  116. $fax_email_connection = "{".$fax_email_connection_host.":".$fax_email_connection_port."/".$fax_email_connection_type;
  117. $fax_email_connection .= ($fax_email_connection_security != '') ? "/".$fax_email_connection_security : "/notls";
  118. $fax_email_connection .= "/".(($fax_email_connection_validate == 'false') ? "no" : null)."validate-cert";
  119. $fax_email_connection .= "}".$fax_email_connection_mailbox;
  120. if (!$connection = imap_open($fax_email_connection, $fax_email_connection_username, $fax_email_connection_password)) {
  121. print_r(imap_errors());
  122. continue; // try next account
  123. }
  124. //get emails
  125. if ($emails = imap_search($connection, "SUBJECT \"[".$fax_email_outbound_subject_tag."]\"", SE_UID)) {
  126. //get authorized sender(s)
  127. if (substr_count($fax_email_outbound_authorized_senders, ',') > 0) {
  128. $authorized_senders = explode(',', $fax_email_outbound_authorized_senders);
  129. }
  130. else {
  131. $authorized_senders[] = $fax_email_outbound_authorized_senders;
  132. }
  133. sort($emails); // oldest first
  134. foreach ($emails as $email_id) {
  135. //get email meta data
  136. $metadata = object_to_array(imap_fetch_overview($connection, $email_id, FT_UID));
  137. //print_r($metadata);
  138. //format from address
  139. //$tmp = object_to_array(imap_rfc822_parse_adrlist($metadata[0]['from'], null));
  140. //$metadata[0]['from'] = strtolower($tmp[0]['mailbox']."@".$tmp[0]['host']);
  141. //$sender_email = $metadata[0]['from'];
  142. //get the sender email address
  143. if (strstr($metadata[0]['from'], '<') && strstr($metadata[0]['from'], '>')) {
  144. $sender_email = preg_match('/^.*<(.+)>/', $metadata[0]['from'], $matches) ? strtolower($matches[1]) : '';
  145. }
  146. else {
  147. $sender_email = strtolower($metadata[0]['from']);
  148. }
  149. //check sender
  150. $sender_domain = explode('@', $sender_email)[1];
  151. $sender_authorized = in_array($sender_email, $authorized_senders) || in_array($sender_domain, $authorized_senders) ? true : false;
  152. if ($sender_authorized) {
  153. //debug info
  154. //echo "authorized\n";
  155. //add multi-lingual support
  156. $language = new text;
  157. $text = $language->get();
  158. //sent sender address (used in api call)
  159. $mailto_address_user = $metadata[0]['from'];
  160. //parse recipient fax number(s)
  161. $fax_subject = $metadata[0]['subject'];
  162. $tmp = explode(']', $fax_subject); //closing bracket of subject tag
  163. $tmp = $tmp[1];
  164. $tmp = str_replace(':', ',', $tmp);
  165. $tmp = str_replace(';', ',', $tmp);
  166. $tmp = str_replace('|', ',', $tmp);
  167. if (substr_count($tmp, ',') > 0) {
  168. $fax_numbers = explode(',', $tmp);
  169. }
  170. else {
  171. $fax_numbers[] = $tmp;
  172. }
  173. unset($fax_subject); //clear so not on cover page
  174. $message = parse_message($connection, $email_id, FT_UID);
  175. //get email body (if any) for cover page
  176. $fax_message = '';
  177. //Debug print
  178. print('attachments:' . "\n");
  179. foreach($message['attachments'] as &$attachment){
  180. print(' - ' . $attachment['type'] . ' - ' . $attachment['name'] . ': ' . $attachment['size'] . ' disposition: ' . $attachment['disposition'] . "\n");
  181. }
  182. print('messages:' . "\n");
  183. foreach($message['messages'] as &$msg){
  184. print(' - ' . $msg['type'] . ' - ' . $msg['size'] . "\n");
  185. // print($msg['data']);
  186. // print("\n--------------------------------------------------------\n");
  187. }
  188. foreach($message['messages'] as &$msg){
  189. if(($msg['size'] > 0)) {
  190. $fax_message = $msg['data'];
  191. break;
  192. }
  193. }
  194. if ($fax_message != '') {
  195. $fax_message = strip_tags($fax_message);
  196. $fax_message = html_entity_decode($fax_message);
  197. $fax_message = str_replace("\r\n\r\n", "\r\n", $fax_message);
  198. }
  199. // set fax directory (used for pdf creation - cover and/or attachments)
  200. $fax_dir = $_SESSION['switch']['storage']['dir'].'/fax'.(($domain_name != '') ? '/'.$domain_name : null);
  201. //handle attachments (if any)
  202. $emailed_files = Array();
  203. $attachments = $message['attachments'];
  204. if (sizeof($attachments) > 0) {
  205. foreach ($attachments as &$attachment) {
  206. $fax_file_extension = pathinfo($attachment['name'], PATHINFO_EXTENSION);
  207. //block unknown files
  208. if ($fax_file_extension == '') {continue; }
  209. //block unauthorized files
  210. if (!$fax_allowed_extension['.' . $fax_file_extension]) { continue; }
  211. //support only attachments
  212. //if($attachment['disposition'] != 'attachment'){ continue; }
  213. //store attachment in local fax temp folder
  214. $uuid_filename = uuid();
  215. $local_filepath = $fax_dir.'/'.$fax_extension.'/temp/'.$uuid_filename."-".$attachment['name'];
  216. file_put_contents($local_filepath, $attachment['data']);
  217. //load files array with attachments
  218. $emailed_files['error'][] = 0;
  219. $emailed_files['size'][] = $attachment['size'];
  220. $emailed_files['tmp_name'][] = $uuid_filename."-".$attachment['name'];
  221. $emailed_files['name'][] = $uuid_filename."-".$attachment['name'];
  222. }
  223. }
  224. //Debug print
  225. print('***********************' . "\n");
  226. print('fax message:' . "\n");
  227. print(' - length: ' . strlen($fax_message) . "\n");
  228. print('fax files [' . sizeof($emailed_files['name']) . ']:' . "\n");
  229. for($i = 0; $i < sizeof($emailed_files['name']);++$i){
  230. print(' - ' . $emailed_files['name'][$i] . ' - ' . $emailed_files['size'][$i] . "\n");
  231. }
  232. print('***********************' . "\n");
  233. //send fax
  234. $cwd = getcwd();
  235. $included = true;
  236. require("fax_send.php");
  237. if($cwd){
  238. chdir($cwd);
  239. }
  240. //reset variables
  241. unset($fax_numbers);
  242. }
  243. //delete email
  244. if (imap_delete($connection, $email_id, FT_UID)) {
  245. imap_expunge($connection);
  246. }
  247. }
  248. unset($authorized_senders);
  249. }
  250. //close account connection
  251. imap_close($connection);
  252. }
  253. }
  254. //functions used above
  255. function load_default_settings() {
  256. $sql = "select * from v_default_settings ";
  257. $sql .= "where default_setting_enabled = 'true' ";
  258. $database = new database;
  259. $result = $database->select($sql, null, 'all');
  260. //load the settings into an array
  261. if (is_array($result) && @sizeof($result) != 0) {
  262. foreach ($result as $row) {
  263. $name = $row['default_setting_name'];
  264. $category = $row['default_setting_category'];
  265. $subcategory = $row['default_setting_subcategory'];
  266. if (strlen($subcategory) == 0) {
  267. if ($name == "array") {
  268. $settings[$category][] = $row['default_setting_value'];
  269. }
  270. else {
  271. $settings[$category][$name] = $row['default_setting_value'];
  272. }
  273. }
  274. else {
  275. if ($name == "array") {
  276. $settings[$category][$subcategory][] = $row['default_setting_value'];
  277. }
  278. else {
  279. $settings[$category][$subcategory][$name] = $row['default_setting_value'];
  280. $settings[$category][$subcategory][$name] = $row['default_setting_value'];
  281. }
  282. }
  283. }
  284. }
  285. unset($sql, $parameters, $result, $row);
  286. return $settings;
  287. }
  288. function load_domain_settings($domain_uuid) {
  289. if (is_uuid($domain_uuid)) {
  290. $sql = "select * from v_domain_settings ";
  291. $sql .= "where domain_uuid = :domain_uuid ";
  292. $sql .= "and domain_setting_enabled = 'true' ";
  293. $sql .= "order by domain_setting_order asc ";
  294. $parameters['domain_uuid'] = $domain_uuid;
  295. $database = new database;
  296. $result = $database->select($sql, $parameters, 'all');
  297. if (is_array($result) && @sizeof($result) != 0) {
  298. //unset the arrays that domains are overriding
  299. foreach ($result as $row) {
  300. $name = $row['domain_setting_name'];
  301. $category = $row['domain_setting_category'];
  302. $subcategory = $row['domain_setting_subcategory'];
  303. if ($name == "array") {
  304. unset($_SESSION[$category][$subcategory]);
  305. }
  306. }
  307. //set the settings as a session
  308. foreach ($result as $row) {
  309. $name = $row['domain_setting_name'];
  310. $category = $row['domain_setting_category'];
  311. $subcategory = $row['domain_setting_subcategory'];
  312. if (strlen($subcategory) == 0) {
  313. //$$category[$name] = $row['domain_setting_value'];
  314. if ($name == "array") {
  315. $_SESSION[$category][] = $row['domain_setting_value'];
  316. }
  317. else {
  318. $_SESSION[$category][$name] = $row['domain_setting_value'];
  319. }
  320. }
  321. else {
  322. //$$category[$subcategory][$name] = $row['domain_setting_value'];
  323. if ($name == "array") {
  324. $_SESSION[$category][$subcategory][] = $row['domain_setting_value'];
  325. }
  326. else {
  327. $_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. ?>