index.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. <?php
  2. //set the include path
  3. $conf = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE);
  4. set_include_path(parse_ini_file($conf[0])['document.root']);
  5. //includes files
  6. require_once "resources/require.php";
  7. require_once "resources/functions.php";
  8. require_once "resources/functions/message_media_builder.php";
  9. require_once "resources/pdo.php";
  10. //connect to the database
  11. $database = database::new();
  12. //initialize database and settings
  13. $settings = new settings(['database' => $database, $_SESSION['domain_uuid'] ?? '']);
  14. //debug
  15. $debug = $settings->get('message','debug', false);
  16. //log file
  17. $log_file = '/tmp/message.log';
  18. //write the remote address to the log
  19. if ($debug) {
  20. file_put_contents($log_file, "Remote Address: ".$_SERVER['REMOTE_ADDR']."\n", FILE_APPEND);
  21. }
  22. //get the provider addresses
  23. $sql = "select provider_uuid, provider_address_cidr ";
  24. $sql .= "from v_provider_addresses ";
  25. $sql .= "where provider_address_cidr is not null ";
  26. $sql .= "and provider_address_enabled = true ";
  27. $parameters = null;
  28. $provider_addresses = $database->select($sql, $parameters, 'all');
  29. //default authorized to false
  30. $authorized = false;
  31. //use the ip address to get the provider uuid and determine if request is authorized
  32. foreach($provider_addresses as $row) {
  33. if (check_cidr($row['provider_address_cidr'], $_SERVER['REMOTE_ADDR'])) {
  34. $provider_uuid = $row['provider_uuid'];
  35. $authorized = true;
  36. break;
  37. }
  38. }
  39. //authorization failed
  40. if ($authorized) {
  41. if ($debug) {
  42. file_put_contents($log_file, "authorized\n", FILE_APPEND);
  43. file_put_contents($log_file, "provider_uuid ".$provider_uuid."\n", FILE_APPEND);
  44. }
  45. }
  46. else {
  47. //log the failed auth attempt to the system, to be available for fail2ban.
  48. if ($debug) {
  49. file_put_contents($log_file, "unauthorized\n", FILE_APPEND);
  50. }
  51. openlog('FusionPBX', LOG_NDELAY, LOG_AUTH);
  52. syslog(LOG_WARNING, '['.$_SERVER['REMOTE_ADDR']."] authentication failed for ".($_GET['key'] ?? ''));
  53. closelog();
  54. //send http 403
  55. header("HTTP/1.0 403 Forbidden");
  56. echo "Forbidden\n";
  57. exit();
  58. }
  59. //check if string is url encoded
  60. function is_urlencoded($string) {
  61. $urlencoded = preg_match('~%[0-9A-F]{2}~i', $string);
  62. if ($urlencoded) {
  63. return true;
  64. }
  65. else {
  66. return false;
  67. }
  68. }
  69. //use the provider uuid to get the provider_settings
  70. $sql = "select provider_setting_category, provider_setting_subcategory, \n";
  71. $sql .= "provider_setting_name, provider_setting_value, provider_setting_order \n";
  72. $sql .= "from v_provider_settings \n";
  73. $sql .= "where provider_uuid = :provider_uuid \n";
  74. $sql .= "and provider_setting_enabled = 'true' \n";
  75. $sql .= "and provider_setting_category = 'inbound' \n";
  76. $parameters['provider_uuid'] = $provider_uuid;
  77. $provider_settings = $database->select($sql, $parameters, 'all');
  78. foreach ($provider_settings as $row) {
  79. if ($row['provider_setting_subcategory'] == 'content') {
  80. $setting[$row['provider_setting_name']] = $row['provider_setting_value'];
  81. }
  82. elseif ($row['provider_setting_subcategory'] == 'format') {
  83. $format[$row['provider_setting_name']] = $row['provider_setting_value'];
  84. }
  85. }
  86. unset($parameters);
  87. //view_array($settings, false);
  88. //set the default content type to json
  89. $content_type = 'json';
  90. //get the content location for the destination number
  91. if (isset($setting['content_type'])) {
  92. $content_type = strtolower($setting['content_type']);
  93. }
  94. if ($debug) {
  95. file_put_contents($log_file, "Server CONTENT_TYPE ".$_SERVER['CONTENT_TYPE']."\n", FILE_APPEND);
  96. file_put_contents($log_file, "content_type: $content_type\n", FILE_APPEND);
  97. }
  98. //get the content
  99. if ($content_type == 'json') {
  100. $message_json = file_get_contents("php://input");
  101. }
  102. elseif ($content_type == 'get') {
  103. $message_json = json_encode($_GET);
  104. }
  105. elseif ($content_type == 'post') {
  106. $message_json = json_encode($_POST);
  107. }
  108. // Used for Providers that send HTTP requests with mixed http method and http variables
  109. // For example, a Provider that sends a POST with a query string would populate $_GET, not $_POST
  110. elseif ($content_type == 'mixed') {
  111. if(!empty($_GET)) {
  112. $message_json = json_encode($_GET);
  113. $content_type = 'get';
  114. }
  115. elseif (!empty($_POST)) {
  116. $message_json = json_encode($_POST);
  117. $content_type = 'post';
  118. }
  119. else {
  120. $message_json = file_get_contents("php://input");
  121. $content_type = 'json';
  122. }
  123. }
  124. //write content to the logs
  125. if ($debug) {
  126. if ($content_type == 'json') {
  127. file_put_contents($log_file, $message_json, FILE_APPEND);
  128. }
  129. }
  130. //save the http post to the log
  131. if ($debug) {
  132. if (count($_POST)) {
  133. file_put_contents($log_file, json_encode($_POST)."\n", FILE_APPEND);
  134. }
  135. if (count($_GET)) {
  136. file_put_contents($log_file, json_encode($_GET)."\n", FILE_APPEND);
  137. }
  138. }
  139. //decode the json into array
  140. if ($content_type == 'json') {
  141. $message = json_decode($message_json, true);
  142. }
  143. //ignore inbound delivery receipt - used by bulkvs
  144. if (isset($message['DeliveryReceipt']) && $message['DeliveryReceipt'] == 'true') {
  145. exit;
  146. }
  147. //send print_r to the log
  148. //if ($debug) {
  149. // file_put_contents($log_file, print_r($message, true)."\n", FILE_APPEND);
  150. //}
  151. //get the content location for the destination number
  152. $message_to = $setting['content']['message_to'] ?? null;
  153. //debug info
  154. if ($debug) {
  155. file_put_contents($log_file, "--------------\n", FILE_APPEND);
  156. file_put_contents($log_file, print_r($setting, true)."\n", FILE_APPEND);
  157. file_put_contents($log_file, "message_to $message_to\n", FILE_APPEND);
  158. file_put_contents($log_file, "--------------\n", FILE_APPEND);
  159. }
  160. /*
  161. $setting['message_from'] => data.attributes.from
  162. $setting['message_to'] => data.attributes.to
  163. $setting['message_content'] => data.attributes.body
  164. $from_array = explode('.', $setting['message_from']);
  165. $to_array = explode('.', $setting['message_to']);
  166. $content_array = explode('.', $setting['message_content']);
  167. */
  168. //version 3
  169. /*
  170. function get_value($array, $key) {
  171. $keys = explode('.', $key);
  172. $segment = array_shift($keys);
  173. if (isset($array[$segment]) && is_array($array[$segment])) {
  174. $next_key = substr($key, strpos($key, '.') + 1);
  175. $data = get_value($array[$segment], $next_key);
  176. }
  177. else {
  178. $data = $array[$segment] ?? '';
  179. }
  180. return $data;
  181. }
  182. */
  183. //version 2
  184. function get_value($data, $path) {
  185. $keys = explode('.', $path);
  186. foreach ($keys as $key) {
  187. $data = $data[$key];
  188. }
  189. return $data;
  190. }
  191. //version 1
  192. /*
  193. function get_value($data, $path) {
  194. $keys = explode('.', $path);
  195. if (count($keys) == 1) {
  196. return $data[$keys[0]];
  197. }
  198. if (count($keys) == 2) {
  199. return $data[$keys[0]][$keys[1]];
  200. }
  201. if (count($keys) == 3) {
  202. return $data[$keys[0]][$keys[1]][$keys[2]];
  203. }
  204. if (count($keys) == 4) {
  205. return $data[$key_array[0]][$keys[1]][$keys[2]][$keys[3]];
  206. }
  207. if (count($keys) == 5) {
  208. return $data[$keys[0]][$keys[1]][$keys[2]][$keys[3]][$keys[4]];
  209. }
  210. }
  211. */
  212. /*
  213. if (count($from_array) == 3) {
  214. $message_from = $message[$from_array[0]][$from_array[1]][$from_array[2]];
  215. }
  216. if (count($to_array) == 3) {
  217. $message_to = $message[$to_array[0]][$to_array[1]][$to_array[2]];
  218. }
  219. if (count($message_content) == 3) {
  220. $message_content = $message[$message_content[0]][$message_content[1]][$message_content[2]];
  221. }
  222. */
  223. //get the values from the message array using the provider settings
  224. if ($content_type == 'json') {
  225. $message_from = get_value($message, $setting['message_from']);
  226. $message_to = get_value($message, $setting['message_to']);
  227. $message_content = get_value($message, $setting['message_content']);
  228. $message_media_array = !empty($setting['message_media_array']) ? get_value($message, $setting['message_media_array']) : null;
  229. //get the message_type options: sms, mms
  230. if (isset($setting['message_type'])) {
  231. $message_type = strtolower($setting['message_type']);
  232. }
  233. else {
  234. $message_type = !empty($message_media_array) && is_array($message_media_array) ? 'mms' : 'sms';
  235. }
  236. }
  237. elseif ($content_type == 'post') {
  238. if (!empty($setting['message_media_array']) && isset($_POST[$setting['message_media_array']])){
  239. $message_media_array = $_POST[$setting['message_media_array']] ;
  240. }
  241. else {
  242. $message_media_array = message_media_builder($_POST, [$setting['message_media_url'], $setting['message_media_type']]);
  243. }
  244. //get the message_type options: sms, mms
  245. if (isset($setting['message_type'])) {
  246. $message_type = strtolower($setting['message_type']);
  247. }
  248. else {
  249. $message_type = !empty($message_media_array) && is_array($message_media_array) ? 'mms' : 'sms';
  250. }
  251. $message_from = ($message_type == 'mms') ? $_POST[$setting['message_media_from']] : $_POST[$setting['message_from']];
  252. $message_to = ($message_type == 'mms') ? $_POST[$setting['message_media_to']] : $_POST[$setting['message_to']];
  253. $message_content = ($message_type == 'mms') ? $_POST[$setting['message_media_content']] : $_POST[$setting['message_content']];
  254. $message_content = preg_replace('/<smil[^>]*>([\s\S]*?)<\/smil[^>]*>/', '', $message_content);
  255. }
  256. elseif ($content_type == 'get') {
  257. if (!empty($setting['message_media_array']) && isset($_GET[$setting['message_media_array']])){
  258. $message_media_array = $_GET[$setting['message_media_array']] ;
  259. }
  260. else {
  261. $message_media_array = message_media_builder($_GET, [$setting['message_media_url'], $setting['message_media_type']]);
  262. }
  263. //get the message_type options: sms, mms
  264. if (isset($setting['message_type'])) {
  265. $message_type = strtolower($setting['message_type']);
  266. }
  267. else {
  268. $message_type = !empty($message_media_array) && is_array($message_media_array) ? 'mms' : 'sms';
  269. }
  270. $message_from = ($message_type == 'mms') ? $_GET[$setting['message_media_from']] : $_GET[$setting['message_from']];
  271. $message_to = ($message_type == 'mms') ? $_GET[$setting['message_media_to']] : $_GET[$setting['message_to']];
  272. $message_content = ($message_type == 'mms') ? $_GET[$setting['message_media_content']] : $_GET[$setting['message_content']];
  273. $message_content = preg_replace('/<smil[^>]*>([\s\S]*?)<\/smil[^>]*>/', '', $message_content);
  274. }
  275. //message to is an array get first number in the array
  276. if (is_array($message_to)) {
  277. $message_to = $message_to['0'];
  278. }
  279. //decode the content if it is encoded
  280. if (isset($message_content)) {
  281. if (is_urlencoded($message_content)) {
  282. $message_content = urldecode($message_content);
  283. }
  284. }
  285. //format the phone numbers
  286. if($message_type == 'mms') {
  287. //check if message_media formats are defined and non-empty, and if so, use those instead of default formats
  288. if (isset($format['message_media_message_from']) && !empty($format['message_media_message_from'])) {
  289. $message_from = format_string($format['message_media_message_from'], $message_from);
  290. }
  291. elseif (isset($format['message_from'])) {
  292. $message_from = format_string($format['message_from'], $message_from);
  293. }
  294. if (isset($format['message_media_message_to']) && !empty($format['message_media_message_to'])) {
  295. $message_to = format_string($format['message_media_message_to'], $message_to);
  296. }
  297. elseif (isset($format['message_to'])) {
  298. $message_to = format_string($format['message_to'], $message_to);
  299. }
  300. }
  301. else {
  302. //default formats. If setting is defined but format string is left blank, the format_string function
  303. //will return the data as is (No changes made)
  304. if (isset($format['message_from'])) {
  305. $message_from = format_string($format['message_from'], $message_from);
  306. }
  307. if (isset($format['message_to'])) {
  308. $message_to = format_string($format['message_to'], $message_to);
  309. }
  310. }
  311. //debug info
  312. if ($debug) {
  313. file_put_contents($log_file, "setting.message_from: ".$setting['message_from']."\n", FILE_APPEND);
  314. file_put_contents($log_file, "setting.message_to: ".$setting['message_to']."\n", FILE_APPEND);
  315. file_put_contents($log_file, "setting.message_content: ".$setting['message_content']."\n", FILE_APPEND);
  316. file_put_contents($log_file, "content_type: $content_type\n", FILE_APPEND);
  317. file_put_contents($log_file, "provider_uuid: $provider_uuid\n", FILE_APPEND);
  318. file_put_contents($log_file, "from: ".$message_from."\n", FILE_APPEND);
  319. file_put_contents($log_file, "to: ".$message_to."\n", FILE_APPEND);
  320. file_put_contents($log_file, "content: ".$message_content."\n", FILE_APPEND);
  321. file_put_contents($log_file, "message_media_array: ".print_r($message_media_array, true)."\n", FILE_APPEND);
  322. }
  323. /*
  324. ()
  325. [data] => Array
  326. (
  327. [attributes] => Array
  328. (
  329. [status] => delivered
  330. [body] => Ddd
  331. [direction] => inbound
  332. [amount_nanodollars] => 4000000
  333. [message_encoding] => 0
  334. [timestamp] => 2021-05-16T06:12:59.88Z
  335. [to] => 12089068227
  336. [amount_display] => $0.0040
  337. [from] => 12088058985
  338. [is_mms] =>
  339. [message_callback_url] => https://voip.fusionpbx.com/app/messages/index.php
  340. [message_type] => longcode
  341. )
  342. [type] => message
  343. [id] => mdr2-c3afc962b60d11ebb748aecb682882cc
  344. )
  345. )
  346. */
  347. //set the hostname if it wasn't provided
  348. $hostname = gethostname();
  349. //get the source phone number
  350. $destination_number = preg_replace('{[\D]}', '', $message_to);
  351. //use the phone number to get the destination details
  352. $sql = "SELECT * FROM v_destinations ";
  353. $sql .= "WHERE ( ";
  354. $sql .= " destination_prefix || destination_area_code || destination_number = :destination_number ";
  355. $sql .= " OR destination_trunk_prefix || destination_area_code || destination_number = :destination_number ";
  356. $sql .= " OR destination_prefix || destination_number = :destination_number ";
  357. $sql .= " OR '+' || destination_prefix || destination_number = :destination_number ";
  358. $sql .= " OR '+' || destination_prefix || destination_area_code || destination_number = :destination_number ";
  359. $sql .= " OR destination_area_code || destination_number = :destination_number ";
  360. $sql .= " OR destination_number = :destination_number ";
  361. $sql .= ") ";
  362. $sql .= "and provider_uuid is not null ";
  363. $sql .= "and destination_enabled = 'true'; ";
  364. $parameters['destination_number'] = $destination_number;
  365. if ($debug) {
  366. file_put_contents($log_file, "sql: ".$sql."\n", FILE_APPEND);
  367. file_put_contents($log_file, print_r($parameters, true)."\n", FILE_APPEND);
  368. }
  369. $row = $database->select($sql, $parameters, 'row');
  370. $domain_uuid = $row['domain_uuid'];
  371. $user_uuid = $row['user_uuid'];
  372. $group_uuid = $row['group_uuid'];
  373. if ($debug) {
  374. file_put_contents($log_file, print_r($row, true)."\n", FILE_APPEND);
  375. }
  376. //check if message to email is enabled
  377. $destination_email_enabled = $row['destination_email'];
  378. unset($sql, $parameters, $row);
  379. //get the contact uuid
  380. $sql = "select c.contact_uuid ";
  381. $sql .= "from v_contacts as c, v_contact_phones as p ";
  382. $sql .= "where p.contact_uuid = c.contact_uuid ";
  383. $sql .= "and p.phone_number = :phone_number ";
  384. $sql .= "and c.domain_uuid = :domain_uuid ";
  385. $parameters['phone_number'] = $destination_number;
  386. $parameters['domain_uuid'] = $domain_uuid;
  387. $contact_uuid = $database->select($sql, $parameters, 'column');
  388. unset($sql, $parameters);
  389. //add to the messages array
  390. $message_uuid = uuid();
  391. $array['messages'][0]['message_uuid'] = $message_uuid;
  392. $array['messages'][0]['domain_uuid'] = $domain_uuid;
  393. $array['messages'][0]['provider_uuid'] = $provider_uuid;
  394. if (is_uuid($user_uuid)) {
  395. $array['messages'][0]['user_uuid'] = $user_uuid;
  396. }
  397. if (is_uuid($group_uuid)) {
  398. $array['messages'][0]['group_uuid'] = $group_uuid;
  399. }
  400. if (is_uuid($contact_uuid)) {
  401. $array['messages'][0]['contact_uuid'] = $contact_uuid;
  402. }
  403. $array['messages'][0]['message_type'] = $message_type;
  404. $array['messages'][0]['message_direction'] = 'inbound';
  405. $array['messages'][0]['message_date'] = 'now()';
  406. $array['messages'][0]['message_from'] = $message_from;
  407. $array['messages'][0]['message_to'] = $message_to;
  408. $array['messages'][0]['message_text'] = $message_content;
  409. $array['messages'][0]['message_json'] = $message_json;
  410. //add to message queue array
  411. $message_queue_uuid = uuid();
  412. $array['message_queue'][0]['message_queue_uuid'] = $message_queue_uuid;
  413. $array['message_queue'][0]['domain_uuid'] = $domain_uuid;
  414. if (is_uuid($user_uuid)) {
  415. $array['message_queue'][0]['user_uuid'] = $user_uuid;
  416. }
  417. if (is_uuid($group_uuid)) {
  418. $array['message_queue'][0]['group_uuid'] = $group_uuid;
  419. }
  420. $array['message_queue'][0]['provider_uuid'] = $provider_uuid;
  421. $array['message_queue'][0]['hostname'] = $hostname;
  422. if (is_uuid($contact_uuid)) {
  423. $array['message_queue'][0]['contact_uuid'] = $contact_uuid;
  424. }
  425. $array['message_queue'][0]['message_type'] = $message_type;
  426. $array['message_queue'][0]['message_direction'] = 'inbound';
  427. $array['message_queue'][0]['message_status'] = 'waiting';
  428. $array['message_queue'][0]['message_date'] = 'now()';
  429. $array['message_queue'][0]['message_from'] = $message_from;
  430. $array['message_queue'][0]['message_to'] = $message_to;
  431. $array['message_queue'][0]['message_text'] = $message_content;
  432. $array['message_queue'][0]['message_json'] = $message_json;
  433. //add the required permission
  434. $p = permissions::new();
  435. $p->add("message_add", "temp");
  436. $p->add("message_queue_add", "temp");
  437. $p->add("message_media_add", "temp");
  438. //build message media array (if necessary)
  439. if (is_array($message_media_array)) {
  440. foreach($message_media_array as $index => $media_row) {
  441. //get the value out of the array using dot notation
  442. if (isset($setting['message_media_url'])) {
  443. $message_media_url = get_value($media_row, $setting['message_media_url']);
  444. }
  445. if (isset($setting['message_media_type'])) {
  446. $message_media_type = get_value($media_row, $setting['message_media_type']);
  447. }
  448. //get the file extension
  449. if (isset($message_media_type)) {
  450. if ($message_media_type == 'image/jpg') { $message_media_type = 'jpg'; }
  451. if ($message_media_type == 'image/jpeg') { $message_media_type = 'jpg'; }
  452. if ($message_media_type == 'image/png') { $message_media_type = 'png'; }
  453. if ($message_media_type == 'image/gif') { $message_media_type = 'gif'; }
  454. }
  455. //get the media url
  456. if (!isset($message_media_url)) {
  457. $message_media_url = $media_row;
  458. }
  459. //get the media type from the URL
  460. if (!isset($message_media_type)) {
  461. $message_media_type = pathinfo($message_media_url, PATHINFO_EXTENSION);
  462. }
  463. //get the file name from the URL
  464. if (!isset($message_media_name)) {
  465. $message_media_name = pathinfo($message_media_url, PATHINFO_FILENAME).'.'.$message_media_type;
  466. }
  467. if ($debug) {
  468. file_put_contents($log_file, "media_row: ".print_r($media_row, true)."\n", FILE_APPEND);
  469. file_put_contents($log_file, "message_media_url: ".$message_media_url."\n", FILE_APPEND);
  470. file_put_contents($log_file, "message_media_name: ".$message_media_name."\n", FILE_APPEND);
  471. file_put_contents($log_file, "message_media_type: ".$message_media_type."\n", FILE_APPEND);
  472. }
  473. //build the array for the media
  474. if ($message_media_type !== 'xml' && strlen($message_media_url) > 0) {
  475. //build the message media
  476. $array['message_media'][$index]['message_media_uuid'] = uuid();
  477. $array['message_media'][$index]['message_uuid'] = $message_uuid;
  478. $array['message_media'][$index]['domain_uuid'] = $domain_uuid;
  479. $array['message_media'][$index]['user_uuid'] = $user_uuid;
  480. $array['message_media'][$index]['message_media_type'] = $message_media_type;
  481. $array['message_media'][$index]['message_media_name'] = $message_media_name;
  482. $array['message_media'][$index]['message_media_date'] = 'now()';
  483. $array['message_media'][$index]['message_media_url'] = $message_media_url;
  484. $array['message_media'][$index]['message_media_content'] = base64_encode(url_get_contents($message_media_url));
  485. //get email attachments
  486. if($destination_email_enabled == 'true'){
  487. $email_attachments[$index]['base64'] = $array['message_media'][$index]['message_media_content'];
  488. $email_attachments[$index]['name'] = $message_media_name;
  489. $email_attachments[$index]['type'] = $message_media_type;
  490. }
  491. }
  492. }
  493. }
  494. else {
  495. //get the value out of the array using dot notation
  496. if (isset($setting['message_media_url'])) {
  497. $message_media_url = get_value($message, $setting['message_media_url']);
  498. }
  499. if (isset($setting['message_media_type'])) {
  500. $message_media_type = get_value($message, $setting['message_media_type']);
  501. }
  502. //get the media type from the URL
  503. if (!isset($message_media_type) && !empty($message_media_url)) {
  504. $message_media_type = pathinfo($message_media_url, PATHINFO_EXTENSION);
  505. }
  506. //get the file extension
  507. if (!empty($message_media_type)) {
  508. if ($message_media_type == 'image/jpeg') { $message_media_type = 'jpg'; }
  509. if ($message_media_type == 'image/png') { $message_media_type = 'png'; }
  510. if ($message_media_type == 'image/gif') { $message_media_type = 'gif'; }
  511. }
  512. //build the array for the media
  513. if (!empty($message_media_url) && strlen($message_media_url) > 0 && $message_media_type !== 'xml') {
  514. //build the message media array
  515. $index = 0;
  516. $array['message_media'][$index]['message_media_uuid'] = uuid();
  517. $array['message_media'][$index]['message_uuid'] = $message_uuid;
  518. $array['message_media'][$index]['domain_uuid'] = $domain_uuid;
  519. $array['message_media'][$index]['user_uuid'] = $user_uuid;
  520. $array['message_media'][$index]['message_media_type'] = $message_media_type;
  521. $array['message_media'][$index]['message_media_url'] = $message_media_url;
  522. $array['message_media'][$index]['message_media_content'] = base64_encode(url_get_contents($message_media_url));
  523. //prepare the email attachments array
  524. if($destination_email_enabled == 'true'){
  525. $email_attachments[$index]['base64'] = $array['message_media'][$index]['message_media_content'];
  526. $email_attachments[$index]['name'] = $message_media_name;
  527. $email_attachments[$index]['type'] = $message_media_type;
  528. }
  529. }
  530. }
  531. //if ($debug) {
  532. // file_put_contents($log_file, print_r($array, true), FILE_APPEND);
  533. //}
  534. //save message to the database;
  535. $database->app_name = 'messages';
  536. $database->app_uuid = '4a20815d-042c-47c8-85df-085333e79b87';
  537. $database->save($array, false);
  538. $result = $database->message;
  539. //if ($debug) {
  540. // file_put_contents($log_file, print_r($result, true), FILE_APPEND);
  541. //}
  542. //remove the temporary permission
  543. $p->delete("message_add", "temp");
  544. $p->delete("message_queue_add", "temp");
  545. $p->delete("message_media_add", "temp");
  546. //convert the array to json
  547. //$array_json = json_encode($array);
  548. //get the list of extensions using the user_uuid
  549. $sql = "select * from v_domains as d, v_extensions as e ";
  550. $sql .= "where extension_uuid in ( ";
  551. $sql .= " select extension_uuid ";
  552. $sql .= " from v_extension_users ";
  553. $sql .= " where user_uuid = :user_uuid ";
  554. $sql .= ") ";
  555. $sql .= "and e.domain_uuid = d.domain_uuid ";
  556. $sql .= "and e.enabled = 'true' ";
  557. $parameters['user_uuid'] = $user_uuid;
  558. $extensions = $database->select($sql, $parameters, 'all');
  559. unset($sql, $parameters);
  560. //send the sip message
  561. if (is_array($extensions) && @sizeof($extensions) != 0) {
  562. //create the event socket connection
  563. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  564. //loop through assigned extensions
  565. foreach ($extensions as $row) {
  566. //get variables from the array
  567. $domain_name = $row['domain_name'];
  568. $extension = $row['extension'];
  569. $number_alias = $row['number_alias'];
  570. //get the sip profile
  571. $command = "sofia_contact ".$extension."@".$domain_name;
  572. $response = event_socket_request($fp, "api ".$command);
  573. if ($response != 'error/user_not_registered') {
  574. $sip_profile = explode("/", $response)[1];
  575. }
  576. //send the sip messages
  577. //$command = "luarun app/messages/resources/send.lua ".$message["from"]."@".$domain_name." ".$extension."@".$domain_name." '".$message["text"]."'";
  578. //$message_from_orig = $message_from;
  579. //original number with the domain name
  580. $message_from = $message_from .'@'.$domain_name;
  581. //$message_to_orig = $message_to;
  582. //send to the assigned extension(s)
  583. $message_to = $extension . '@'.$domain_name;
  584. //$message_to = '[email protected]';
  585. //add debug info to the message
  586. //$message_content = $message_content . ' - ' .$message_to_orig;
  587. //send the SIP message (working)
  588. $event = "sendevent CUSTOM\n";
  589. $event .= "Event-Subclass: SMS::SEND_MESSAGE\n";
  590. $event .= "proto: sip\n";
  591. $event .= "dest_proto: sip\n";
  592. $event .= "from: ".$message_from."\n";
  593. $event .= "from_full: sip:".$message_from."\n";
  594. $event .= "to: ".$message_to."\n";
  595. $event .= "subject: sip:".$message_to."\n";
  596. //$event .= "type: text/html\n";
  597. $event .= "type: text/plain\n";
  598. $event .= "hint: the hint\n";
  599. $event .= "replying: true\n";
  600. $event .= "sip_profile: ".$sip_profile."\n";
  601. $event .= "_body: ". $message_content;
  602. event_socket_request($fp, $event);
  603. }
  604. }
  605. if ($destination_email_enabled == 'true') {
  606. //get the email template from the database - Category: message; Subcategory:new
  607. $sql = "select template_subcategory, template_subject, template_body from v_email_templates ";
  608. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  609. $sql .= "and template_language = :template_language ";
  610. $sql .= "and template_category = :template_category ";
  611. $sql .= "and template_subcategory = :template_subcategory ";
  612. $sql .= "and template_enabled = 'true' ";
  613. $parameters['domain_uuid'] = $domain_uuid;
  614. $parameters['template_language'] = $settings->get('domain','language');
  615. $parameters['template_category'] = 'message';
  616. $parameters['template_subcategory'] = 'inbound';
  617. $message_inbound_template = $database->select($sql, $parameters, 'row');
  618. unset($sql, $parameters);
  619. $email_subject = $message_inbound_template['template_subject'] ?? "";
  620. $email_body = $message_inbound_template['template_body'] ?? "";
  621. //add placeholders as needed
  622. $email_subject = str_replace('${message_to}', $message_to, $email_subject);
  623. $email_subject = str_replace('${message_from}', $message_from, $email_subject);
  624. $email_body = str_replace('${message_to}', $message_to, $email_body);
  625. $email_body = str_replace('${message_from}', $message_from, $email_body);
  626. $email_body = str_replace('${message_text}', $message_content, $email_body);
  627. $time_zone = $settings->get('domain','time_zone');
  628. $dt = new DateTime("now", new DateTimeZone($time_zone));
  629. $email_body = str_replace('${message_date}', $dt->format('m/d/Y, H:i:s').' '.$time_zone, $email_body);
  630. $sql = "select user_email from v_users ";
  631. $sql .= "where user_uuid = :user_uuid";
  632. $parameters['user_uuid'] = $user_uuid;
  633. $user_email = $database->select($sql, $parameters, 'column');
  634. unset($sql, $parameters);
  635. $email = new email;
  636. $email->domain_uuid = $domain_uuid;
  637. $email->recipients = $user_email;
  638. $email->subject = $email_subject;
  639. $email->body = $email_body;
  640. $email->from_address = $settings->get('email','smtp_from');
  641. $email->from_name = $settings->get('email','smtp_from_name');
  642. $email->debug_level = 3;
  643. $email->attachments = $email_attachments;
  644. $email->send();
  645. }
  646. //set the file
  647. //$file = '/tmp/sms.txt';
  648. //save the file
  649. //file_put_contents($file, $json);
  650. //save the data to the file system
  651. //file_put_contents($file, $json."\n");
  652. //file_put_contents($file, $array_json."\nfrom: ".$message["from"]." to: ".$message["to"]." text: ".$message["text"]."\n$sql_test\njson: ".$json."\n".$saved_result."\n");
  653. //send response to provider, if defined
  654. foreach ($provider_settings as $row) {
  655. if ($row['provider_setting_subcategory'] == 'response' && $row['provider_setting_name'] == 'message_content' && !empty($row['provider_setting_value'])) {
  656. $message_content = $row['provider_setting_value'];
  657. if ($debug) {
  658. file_put_contents($log_file, "Response...\n".$row['provider_setting_value']."\n\n", FILE_APPEND);
  659. }
  660. echo $row['provider_setting_value'];
  661. break;
  662. }
  663. }
  664. ?>