message_send_outbound.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. //only allow command line
  3. if (defined('STDIN')) {
  4. //set the include path
  5. $conf = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE);
  6. set_include_path(parse_ini_file($conf[0])['document.root']);
  7. }
  8. else {
  9. exit;
  10. }
  11. //increase limits
  12. //set_time_limit(0);
  13. ini_set('max_execution_time',30); //seconds
  14. ini_set('memory_limit', '512M');
  15. //includes files
  16. require_once "resources/require.php";
  17. include "resources/classes/cache.php";
  18. include "resources/classes/permissions.php";
  19. //connect to the database
  20. $database = new database;
  21. //save the arguments to variables
  22. $script_name = $argv[0];
  23. if (!empty($argv[1])) {
  24. parse_str($argv[1], $_GET);
  25. }
  26. //print_r($_GET);
  27. //set the variables
  28. if (isset($_GET['hostname'])) {
  29. $hostname = urldecode($_GET['hostname']);
  30. }
  31. if (isset($_GET['debug'])) {
  32. $debug = $_GET['debug'];
  33. }
  34. if (is_uuid($_GET['message_queue_uuid'])) {
  35. $message_queue_uuid = $_GET['message_queue_uuid'];
  36. $message_uuid = $message_queue_uuid;
  37. }
  38. if (isset($debug)) {
  39. print_r($_GET);
  40. echo "message_queue_uuid ".$message_queue_uuid."\n";
  41. echo "hostname ".$hostname."\n";
  42. }
  43. //set the hostname if it wasn't provided
  44. if (!isset($hostname) && strlen($hostname) == 0) {
  45. $hostname = system('hostname');
  46. }
  47. //get the message details to send
  48. $sql = "select * from v_message_queue ";
  49. $sql .= "where message_queue_uuid = :message_queue_uuid ";
  50. //$sql .= "and hostname = :hostname ";
  51. //$parameters['hostname'] = $hostname;
  52. $parameters['message_queue_uuid'] = $message_queue_uuid;
  53. $row = $database->select($sql, $parameters, 'row');
  54. if (isset($debug)) {
  55. view_array($row, false);
  56. }
  57. if (is_array($row)) {
  58. $domain_uuid = $row["domain_uuid"];
  59. $user_uuid = $row["user_uuid"];
  60. $group_uuid = $row["group_uuid"];
  61. $contact_uuid = $row["contact_uuid"];
  62. $provider_uuid = $row["provider_uuid"];
  63. //$hostname = $row["hostname"];
  64. $message_type = $row["message_type"];
  65. $message_direction = $row["message_direction"];
  66. $message_date = $row["message_date"];
  67. $message_from = $row["message_from"];
  68. $message_to = $row["message_to"];
  69. $message_text = $row["message_text"];
  70. $message_json = $row["message_json"];
  71. }
  72. unset($parameters);
  73. //get the message media
  74. $sql = "select * from v_message_media ";
  75. $sql .= "where message_uuid = :message_uuid ";
  76. //$sql .= "and hostname = :hostname ";
  77. //$parameters['hostname'] = $hostname;
  78. $parameters['message_uuid'] = $message_uuid;
  79. $database = new database;
  80. $message_media = $database->select($sql, $parameters, 'all');
  81. //view_array($message_media, false);
  82. unset($parameters);
  83. //version 2
  84. function get_value($data, $path) {
  85. $keys = explode('.', $path);
  86. foreach ($keys as $key) {
  87. $data = $data[$key];
  88. }
  89. return $data;
  90. }
  91. //get the values from the message array using the provider settings
  92. //$message_from = get_value($message, $setting['message_from']);
  93. //$message_to = get_value($message, $setting['message_to']);
  94. //$message_content = get_value($message, $setting['message_content']);
  95. //debug info
  96. //if ($debug) {
  97. // file_put_contents($log_file, "from: ".$message_from."\n", FILE_APPEND);
  98. // file_put_contents($log_file, "to: ".$message_to."\n", FILE_APPEND);
  99. // file_put_contents($log_file, "content: ".$message_content."\n", FILE_APPEND);
  100. //}
  101. //build an array from dot notation
  102. //$setting['data.attributes.from'] = '[email protected]';
  103. //$setting['data.attributes.to'] = '[email protected]';
  104. function build_array($array, $path, $value) {
  105. $a = explode('.', $path);
  106. if (count($a) == 1) {
  107. $array[$a[0]] = $value;
  108. }
  109. if (count($a) == 2) {
  110. $array[$a[0]][$a[1]] = $value;
  111. }
  112. if (count($a) == 3) {
  113. $array[$a[0]][$a[1]][$a[2]] = $value;
  114. }
  115. if (count($a) == 4) {
  116. $array[$a[0]][$a[1]][$a[2]][$a[3]] = $value;
  117. }
  118. if (count($a) == 5) {
  119. $array[$a[0]][$a[1]][$a[2]][$a[3]][$a[4]] = $value;
  120. }
  121. return $array;
  122. }
  123. //send context to the temp log
  124. //echo "Subject: ".$email_subject."\n";
  125. //echo "From: ".$email_from."\n";
  126. //echo "Reply-to: ".$email_from."\n";
  127. //echo "To: ".$email_to."\n";
  128. //echo "Date: ".$email_date."\n";
  129. //echo "Transcript: ".$array['message']."\n";
  130. //echo "Body: ".$email_body."\n";
  131. //get the provider settings
  132. $sql = "select provider_setting_category, provider_setting_subcategory, provider_setting_type, provider_setting_name, provider_setting_value, provider_setting_order \n";
  133. $sql .= "from v_provider_settings \n";
  134. $sql .= "where provider_uuid = :provider_uuid \n";
  135. $sql .= "and provider_setting_category = 'outbound' \n";
  136. $sql .= "and provider_setting_enabled = 'true'; \n";
  137. $parameters['provider_uuid'] = $provider_uuid;
  138. $provider_settings = $database->select($sql, $parameters, 'all');
  139. foreach ($provider_settings as $row) {
  140. //set the content array
  141. if ($row['provider_setting_subcategory'] == 'content') {
  142. $content[$row['provider_setting_name']] = $row['provider_setting_value'];
  143. }
  144. //set the format array
  145. if ($row['provider_setting_subcategory'] == 'format') {
  146. $format[$row['provider_setting_name']] = $row['provider_setting_value'];
  147. }
  148. //build the settings array
  149. $setting[$row['provider_setting_name']] = $row['provider_setting_value'];
  150. //set the message to type
  151. if ($row['provider_setting_name'] == 'message_to') {
  152. if ($row['provider_setting_type'] == 'array') {
  153. $message_to_type = 'array';
  154. }
  155. else {
  156. $message_to_type = 'text';
  157. }
  158. }
  159. }
  160. unset($parameters);
  161. //echo $sql;
  162. //print_r($parameters);
  163. //print_r($provider_settings, false);
  164. //echo "\n";
  165. //format the phone numbers
  166. if($message_type == 'mms') {
  167. //check if message_media formats are defined and non-empty, and if so, use those instead of default formats
  168. if (isset($format['message_media_message_from']) && !empty($format['message_media_message_from'])) {
  169. $message_from = format_string($format['message_media_message_from'], $message_from);
  170. }
  171. elseif (isset($format['message_from'])) {
  172. $message_from = format_string($format['message_from'], $message_from);
  173. }
  174. if (isset($format['message_media_message_to']) && !empty($format['message_media_message_to'])) {
  175. $message_to = format_string($format['message_media_message_to'], $message_to);
  176. }
  177. elseif (isset($format['message_to'])) {
  178. $message_to = format_string($format['message_to'], $message_to);
  179. }
  180. }
  181. else {
  182. //default formats. If setting is defined but format string is left blank, the format_string function
  183. //will return the data as is (No changes made)
  184. if (isset($format['message_from'])) {
  185. $message_from = format_string($format['message_from'], $message_from);
  186. }
  187. if (isset($format['message_to'])) {
  188. $message_to = format_string($format['message_to'], $message_to);
  189. }
  190. }
  191. //set http_method
  192. if ($message_type == 'mms' && isset($setting['message_media_http_method']) && !empty($setting['message_media_http_method'])) {
  193. $http_method = strtolower($setting['message_media_http_method']);
  194. }
  195. else {
  196. $http_method = strtolower($setting['http_method']);
  197. }
  198. if (empty($http_method)) {
  199. $http_method = 'POST';
  200. }
  201. //get the content location for the destination number
  202. if (isset($setting['type'])) {
  203. $content_type = strtolower($setting['type']);
  204. }
  205. //set the content_type
  206. if ($message_type == 'mms' && isset($setting['message_media_content_type']) && !empty($setting['message_media_content_type'])) {
  207. $content_type = strtolower($setting['message_media_content_type']);
  208. }
  209. else {
  210. $content_type = strtolower($setting['content_type']);
  211. }
  212. if (empty($content_type)) {
  213. $content_type = 'post';
  214. }
  215. //send information to the console
  216. if (isset($debug)) {
  217. echo "content_type $content_type\n";
  218. echo "message_from $message_from\n";
  219. echo "message_to $message_to\n";
  220. echo "message_text $message_text\n";
  221. }
  222. if ($message_type == 'mms' && isset($content['message_media_message_from']) && !empty($content['message_media_message_from'])) {
  223. $outbound_array = build_array($outbound_array ?? [], $content['message_media_message_from'], $message_from);
  224. }
  225. elseif (isset($content['message_from']) && !empty($content['message_from'])) {
  226. $outbound_array = build_array($outbound_array ?? [], $content['message_from'], $message_from);
  227. }
  228. if ($message_type == 'mms' && isset($content['message_media_message_to']) && !empty($content['message_media_message_to'])) {
  229. if ($message_to_type == 'array') {
  230. // message to json type: array
  231. $outbound_array = build_array($outbound_array ?? [], $content['message_media_message_to'], explode(",", $message_to));
  232. }
  233. else {
  234. // message to json type: text
  235. $outbound_array = build_array($outbound_array ?? [], $content['message_media_message_to'], $message_to);
  236. }
  237. }
  238. else {
  239. if ($message_to_type == 'array') {
  240. // message to json type: array
  241. $outbound_array = build_array($outbound_array ?? [], $content['message_to'], explode(",", $message_to));
  242. }
  243. else {
  244. // message to json type: text
  245. $outbound_array = build_array($outbound_array ?? [], $content['message_to'], $message_to);
  246. }
  247. }
  248. if ($message_type == 'mms' && isset($content['message_media_message_content']) && !empty($content['message_media_message_content'])) {
  249. $outbound_array = build_array($outbound_array ?? [], $content['message_media_message_content'], $message_text);
  250. }
  251. else {
  252. $outbound_array = build_array($outbound_array ?? [], $content['message_content'], $message_text);
  253. }
  254. foreach ($provider_settings as $row) {
  255. if ($row['provider_setting_subcategory'] == 'content') {
  256. if ($row['provider_setting_name'] == 'message_other') {
  257. $outbound_array = build_array($outbound_array ?? [], explode('=', $row['provider_setting_value'])[0], explode('=', $row['provider_setting_value'])[1]);
  258. }
  259. if (is_array($message_media) && @sizeof($message_media) != 0) {
  260. if ($row['provider_setting_name'] == 'message_media_other') {
  261. $outbound_array = build_array($outbound_array ?? [], explode('=', $row['provider_setting_value'])[0], explode('=', $row['provider_setting_value'])[1]);
  262. }
  263. if ($row['provider_setting_name'] == 'message_media_url') {
  264. foreach($message_media as $index => $media) {
  265. $outbound_array = build_array($outbound_array ?? [], $row['provider_setting_value'].".".$index, urldecode($media['message_media_url']));
  266. }
  267. }
  268. }
  269. }
  270. }
  271. //log info
  272. //view_array($provider_settings, false);
  273. //echo "value ".$value."\n";
  274. // view_array($outbound_array, false);
  275. //view_array($setting, false);
  276. /*
  277. view_array($outbound_array, false);
  278. exit;
  279. $setting['http_destination']
  280. */
  281. //convert the array into json
  282. if ($content_type == 'json') {
  283. //view_array($outbound_array, false);
  284. $http_content = json_encode($outbound_array);
  285. //echo $http_content."\n";
  286. }
  287. //convert fields into a http get or post query string
  288. if ($content_type == 'get' || $content_type == 'post') {
  289. //build the query string
  290. $x = 0;
  291. $query_string = '';
  292. foreach ($outbound_array as $key => $value) {
  293. if ($x != 0) { $query_string .= '&'; }
  294. if (is_array($value)){
  295. $y = 0;
  296. foreach($value as $v){
  297. if ($y != 0) { $query_string .= '&'; }
  298. $query_string .= $key.'='. urlencode($v);
  299. $y++;
  300. }
  301. }
  302. else {
  303. $query_string .= $key.'='. urlencode($value);
  304. }
  305. $x++;
  306. }
  307. $http_content = $query_string;
  308. }
  309. //exchange variable name with their values
  310. $http_destination = ($message_type == 'mms' && !empty($setting['message_media_http_destination'])) ? $setting['message_media_http_destination'] : $setting['http_destination'];
  311. $http_destination = str_replace("\${from}", urlencode($message_from), $http_destination);
  312. $http_destination = str_replace("\${message_from}", urlencode($message_from), $http_destination);
  313. $http_destination = str_replace("\${to}", urlencode($message_to), $http_destination);
  314. $http_destination = str_replace("\${message_to}", urlencode($message_to), $http_destination);
  315. $http_destination = str_replace("\${message_text}", urlencode($message_text), $http_destination);
  316. $http_destination = str_replace("\${http_auth_username}", urlencode($setting['http_auth_username']), $http_destination);
  317. $http_destination = str_replace("\${http_auth_password}", urlencode($setting['http_auth_password']), $http_destination);
  318. $setting['http_destination'] = $http_destination;
  319. //logging info
  320. //view_array($setting, false);
  321. //view_array($outbound_array, false);
  322. //build the message array
  323. $array['messages'][0]['domain_uuid'] = $domain_uuid;
  324. $array['messages'][0]['message_uuid'] = $message_uuid;
  325. $array['messages'][0]['user_uuid'] = $user_uuid;
  326. $array['messages'][0]['group_uuid'] = $group_uuid;
  327. //$array['messages'][0]['contact_uuid'] = $contact_uuid;
  328. $array['messages'][0]['provider_uuid'] = $provider_uuid;
  329. $array['messages'][0]['hostname'] = $hostname;
  330. $array['messages'][0]['message_type'] = $message_type;
  331. $array['messages'][0]['message_direction'] = $message_direction;
  332. $array['messages'][0]['message_date'] = $message_date;
  333. $array['messages'][0]['message_from'] = $message_from;
  334. $array['messages'][0]['message_to'] = $message_to;
  335. $array['messages'][0]['message_text'] = $message_text;
  336. $array['messages'][0]['message_json'] = $http_content;
  337. //add permissions
  338. $p = permissions::new();
  339. $p->add('message_add', 'temp');
  340. $p->add('message_edit', 'temp');
  341. $p->add('message_media_add', 'temp');
  342. $p->add('message_media_edit', 'temp');
  343. //save to the database
  344. $database = new database;
  345. $database->app_name = 'messages';
  346. $database->app_uuid = '4a20815d-042c-47c8-85df-085333e79b87';
  347. $database->save($array, false);
  348. view_array($database->message, false);
  349. unset($array);
  350. //remove any temporary permissions
  351. $p->delete('message_add', 'temp');
  352. $p->delete('message_edit', 'temp');
  353. $p->delete('message_media_add', 'temp');
  354. $p->delete('message_media_edit', 'temp');
  355. //send the message - working
  356. /*
  357. $cmd = "curl ". $http_destination;." -X ".$setting['http_method']." ";
  358. if (isset($setting['http_auth_username']) && isset($setting['http_auth_password'])) {
  359. $cmd .= "-u ".$setting['http_auth_username'].":".$setting['http_auth_password']." ";
  360. }
  361. foreach ($provider_settings as $row) {
  362. if ($row['provider_setting_subcategory'] == 'header' && $row['provider_setting_name'] != 'http_content_type') {
  363. $cmd .= "-H '".$row['provider_setting_name'].": ".$row['provider_setting_value']."' ";
  364. }
  365. }
  366. if (isset($setting['http_content_type'])) {
  367. $cmd .= "-H 'Content-Type: ".$setting['http_content_type']."' ";
  368. }
  369. $cmd .= "-d '$http_content' ";
  370. echo $cmd."\n";
  371. $result = system($cmd);
  372. $message_debug = $cmd;
  373. //echo $result."\n";
  374. */
  375. //prepare the http headers
  376. if (isset($setting['http_content_type'])) {
  377. $http_headers[] = 'Content-Type: '.$setting['http_content_type'];
  378. }
  379. foreach ($provider_settings as $row) {
  380. if ($row['provider_setting_subcategory'] == 'header' && $row['provider_setting_name'] != 'http_content_type') {
  381. $http_headers[] = $row['provider_setting_name'].": ".$row['provider_setting_value'];
  382. }
  383. }
  384. //print_r($provider_settings);
  385. //echo "headers_array\n";
  386. //print_r($http_headers);
  387. if (isset($debug)) {
  388. echo "http_content: ".print_r($http_content, true)."\n";
  389. echo "http_destination: {$setting['http_destination']} \n";
  390. echo "http_username: {$setting['http_auth_username']} \n";
  391. echo "http_token: {$setting['http_auth_password']} \n";
  392. echo "http_method: $http_method \n";
  393. }
  394. //create the curl resource
  395. $ch = curl_init();
  396. //set the curl options
  397. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  398. curl_setopt($ch, CURLOPT_URL, $setting['http_destination']);
  399. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  400. if (isset($setting['http_auth_username']) && isset($setting['http_auth_password'])) {
  401. curl_setopt($ch, CURLOPT_USERPWD, $setting['http_auth_username'] . ':' . $setting['http_auth_password']);
  402. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //set the authentication type
  403. }
  404. if (isset($http_headers) && is_array($http_headers)) {
  405. curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
  406. }
  407. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  408. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($http_method));
  409. curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
  410. curl_setopt($ch, CURLOPT_POSTFIELDS, $http_content); // file_get_contents($file_path.'/'.$file_name));
  411. curl_setopt($ch, CURLOPT_POST, 1);
  412. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); //The number of seconds to wait while trying to connect.
  413. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
  414. curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
  415. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //The maximum number of seconds to allow cURL functions to execute.
  416. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); //To stop cURL from verifying the peer's certificate.
  417. curl_setopt($ch, CURLOPT_HEADER, 0); //hide the headers when set to 0
  418. //execute the curl with the options
  419. $http_content = curl_exec($ch);
  420. echo $http_content."\n";
  421. //return the error
  422. if (curl_errno($ch)) {
  423. $message_debug = curl_error($ch);
  424. }
  425. else {
  426. $message_debug = '';
  427. }
  428. //close the curl resource
  429. curl_close($ch);
  430. //working
  431. /*
  432. $cmd = "curl https://api.flowroute.com/v2.1/messages -X POST ";
  433. $cmd .= "-u ".$setting['http_auth_username'].":".$setting['http_auth_password']." ";
  434. $cmd .= "-H 'Content-Type: application/vnd.api+json' ";
  435. $cmd .= "-d '$http_content' ";
  436. echo $cmd."\n";
  437. echo system($cmd);
  438. */
  439. /*
  440. //add permissions
  441. $p = permissions::new();
  442. $p->add('message_queue_add', 'temp');
  443. $p->add('message_queue_update', 'temp');
  444. //build the message array
  445. $array['message_queue'][0]['domain_uuid'] = $domain_uuid;
  446. $array['message_queue'][0]['message_queue_uuid'] = $message_queue_uuid;
  447. $array['message_queue'][0]['message_status'] = 'sent';
  448. $array['message_queue'][0]['message_json'] = $http_content;
  449. $array['message_queue'][0]['message_debug'] = $message_debug;
  450. //save to the data
  451. $database = new database;
  452. $database->app_name = 'messages';
  453. $database->app_uuid = '4a20815d-042c-47c8-85df-085333e79b87';
  454. $database->save($array, false);
  455. view_array($database->message, false);
  456. unset($array);
  457. //remove any temporary permissions
  458. $p->delete('message_queue_add', 'temp');
  459. $p->delete('message_queue_update', 'temp');
  460. */
  461. //set the message status to sent
  462. $sql = "update v_message_queue ";
  463. $sql .= "set message_status = 'sent', ";
  464. $sql .= "message_json = :message_json, ";
  465. $sql .= "message_debug = :message_debug ";
  466. $sql .= "where message_queue_uuid = :message_queue_uuid; ";
  467. $parameters['message_queue_uuid'] = $message_queue_uuid;
  468. $parameters['message_json'] = $http_content;
  469. $parameters['message_debug'] = $message_debug;
  470. $database->execute($sql, $parameters);
  471. unset($parameters);
  472. //set the last message in the cache
  473. $cache = new cache;
  474. $cache->set("messages:user:last_message:".$user_uuid, date('r'));
  475. //save the output
  476. //$fp = fopen(sys_get_temp_dir()."/messages.log", "a");
  477. //prepare the output buffers
  478. //ob_end_clean();
  479. //ob_start();
  480. //message divider for log file
  481. //echo "\n\n====================================================\n\n";
  482. //get and save the output from the buffer
  483. //$content = ob_get_contents(); //get the output from the buffer
  484. //$content = str_replace("<br />", "", $content);
  485. //ob_end_clean(); //clean the buffer
  486. //fwrite($fp, $content);
  487. //fclose($fp);
  488. //how to use it
  489. // php /var/www/fusionpbx/app/messages/resources/service/message_send_outbound.php message_queue_uuid=39402652-1475-49f8-8366-7889335edd6f&hostname=voip.fusionpbx.com
  490. ?>