Guest User

Untitled

a guest
Nov 12th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.56 KB | None | 0 0
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  3. echo 'This script is not supposed to be viewed in a browser!';
  4. exit;
  5. }
  6.  
  7. $con = new mysqli('localhost', 'username', '', 'my_db');
  8.  
  9. $insert_sms_success = FALSE;
  10.  
  11. if (mysqli_connect_errno() == 0) {
  12.  
  13. $sent_dt = $_POST['time'];
  14. $txt = $_POST['content'];
  15. $txt = mysqli_real_escape_string($con, $txt);
  16. $sender_number = mysqli_real_escape_string($con, $sender_number);
  17.  
  18. $sql = "INSERT INTO SMS_IN(sms_text,sent_dt) VALUES ('$txt','$sent_dt')";
  19.  
  20. $insert_sms_success = mysqli_query($con, $sql);
  21. mysqli_close($con);
  22. }
  23.  
  24. if ($insert_sms_success) {
  25. } else {
  26. http_response_code(500);
  27. }
  28. ?>
  29.  
  30. function get_message()
  31. {
  32. $error = NULL;
  33. // Set success to false as the default success status
  34. $success = false;
  35. /**
  36. * Get the phone number that sent the SMS.
  37. */
  38. if (isset($_POST['from']))
  39. {
  40. $from = $_POST['from'];
  41. }
  42. else
  43. {
  44. $error = 'The from variable was not set';
  45. }
  46. /**
  47. * Get the SMS aka the message sent.
  48. */
  49. if (isset($_POST['message']))
  50. {
  51. $message = $_POST['message'];
  52. }
  53. else
  54. {
  55. $error = 'The message variable was not set';
  56. }
  57. /**
  58. * Get the secret key set on SMSsync side
  59. * for matching on the server side.
  60. */
  61. if (isset($_POST['secret']))
  62. {
  63. $secret = $_POST['secret'];
  64. }
  65. /**
  66. * Get the timestamp of the SMS
  67. */
  68. if(isset($_POST['sent_timestamp']))
  69. {
  70. $sent_timestamp = $_POST['sent_timestamp'];
  71. }
  72. /**
  73. * Get the phone number of the device SMSsync is
  74. * installed on.
  75. */
  76. if (isset($_POST['sent_to']))
  77. {
  78. $sent_to = $_POST['sent_to'];
  79. }
  80. /**
  81. * Get the unique message id
  82. */
  83. if (isset($_POST['message_id']))
  84. {
  85. $message_id = $_POST['message_id'];
  86. }
  87. /**
  88. * Get device ID
  89. */
  90. if (isset($_POST['device_id']))
  91. {
  92. $device_id = $_POST['device_id'];
  93. }
  94. /**
  95. * Now we have retrieved the data sent over by SMSsync
  96. * via HTTP. Next thing to do is to do something with
  97. * the data. Either echo it or write it to a file or even
  98. * store it in a database. This is entirely up to you.
  99. * After, return a JSON string back to SMSsync to know
  100. * if the web service received the message successfully or not.
  101. *
  102. * In this demo, we are just going to save the data
  103. * received into a text file.
  104. *
  105. */
  106. if ((strlen($from) > 0) AND (strlen($message) > 0) AND
  107. (strlen($sent_timestamp) > 0 )
  108. AND (strlen($message_id) > 0))
  109. {
  110. /* The screte key set here is 123456. Make sure you enter
  111. * that on SMSsync.
  112. */
  113. if ( ( $secret == '123456'))
  114. {
  115. $success = true;
  116. } else
  117. {
  118. $error = "The secret value sent from the device does not match the one on the server";
  119. }
  120. // now let's write the info sent by SMSsync
  121. //to a file called test.txt
  122. $string = "From: ".$from."n";
  123. $string .= "Message: ".$message."n";
  124. $string .= "Timestamp: ".$sent_timestamp."n";
  125. $string .= "Messages Id:" .$message_id."n";
  126. $string .= "Sent to: ".$sent_to."n";
  127. $string .= "Device ID: ".$device_id."nnn";
  128. write_message_to_file($string);
  129. }
  130. /**
  131. * Comment the code below out if you want to send an instant
  132. * reply as SMS to the user.
  133. *
  134. * This feature requires the "Get reply from server" checked on SMSsync.
  135. */
  136. send_instant_message($from);
  137. /**
  138. * Now send a JSON formatted string to SMSsync to
  139. * acknowledge that the web service received the message
  140. */
  141. $response = json_encode([
  142. "payload"=> [
  143. "success"=>$success,
  144. "error" => $error
  145. ]
  146. ]);
  147. //send_response($response);
  148. }
  149.  
  150.  
  151. function write_message_to_file($message)
  152. {
  153. $myFile = "test.txt";
  154. $fh = fopen($myFile, 'a') or die("can't open file");
  155. @fwrite($fh, $message);
  156. @fclose($fh);
  157. }
  158.  
  159. /**
  160. * Implements the task feature. Sends messages to SMSsync to be sent as
  161. * SMS to users.
  162. */
  163. function send_task()
  164. {
  165. /**
  166. * Comment the code below out if you want to send an instant
  167. * reply as SMS to the user.
  168. *
  169. * This feature requires the "Get reply from server" checked on SMSsync.
  170. */
  171. if (isset($_GET['task']) AND $_GET['task'] === 'send')
  172. {
  173. $m = "Sample Task Message";
  174. $f = "+000-000-0000";
  175. $s = "true";
  176. $reply[0] = [
  177. "to" => $f,
  178. "message" => $m,
  179. "uuid" => "1ba368bd-c467-4374-bf28"
  180. ];
  181. // Send JSON response back to SMSsync
  182. $response = json_encode(
  183. ["payload"=>[
  184. "success"=>$s,
  185. "task"=>"send",
  186. "secret" => "123456",
  187. "messages"=>array_values($reply)]
  188. ]);
  189. send_response($response);
  190. }
  191. }
  192.  
  193.  
  194. function send_instant_message($to)
  195. {
  196. $m = "Your message has been received";
  197. $f = "+000-000-0000";
  198. $s = true;
  199. $reply[0] = [
  200. "to" => $to,
  201. "message" => $m,
  202. "uuid" => "1ba368bd-c467-4374-bf28"
  203. ];
  204. // Send JSON response back to SMSsync
  205. $response = json_encode(
  206. ["payload"=>[
  207. "success"=>$s,
  208. "task"=>"send",
  209. "secret" => "123456",
  210. "messages"=>array_values($reply)]
  211. ]);
  212. send_response($response);
  213. }
  214.  
  215. function send_response($response)
  216. {
  217. // Avoid caching
  218. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  219. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  220. header("Content-type: application/json; charset=utf-8");
  221. echo $response;
  222. }
  223.  
  224. function get_sent_message_uuids()
  225. {
  226. $data = file_get_contents('php://input');
  227. $queued_messages = file_get_contents('php://input');
  228. // Writing this to a file for demo purposes.
  229. // In production, you will have to process the JSON string
  230. // and remove the messages from the database or where ever the
  231. // messages are stored so the next Task run, the server won't add
  232. // these messages.
  233. write_message_to_file($queued_messages."nn");
  234. send_message_uuids_waiting_for_a_delivery_report($queued_messages);
  235.  
  236. }
  237.  
  238. function send_message_uuids_waiting_for_a_delivery_report($queued_messages)
  239. {
  240. // Send back the received messages UUIDs back to SMSsync
  241. $json_obj = json_decode($queued_messages);
  242. $response = json_encode(
  243. [
  244. "message_uuids"=>$json_obj->queued_messages
  245. ]);
  246. send_response($response);
  247. }
  248.  
  249. function send_messages_uuids_for_sms_delivery_report()
  250. {
  251. if(isset($_GET['task']) AND $_GET['task'] == 'result'){
  252. $response = json_encode(
  253. [
  254. "message_uuids" => ['1ba368bd-c467-4374-bf28']
  255. ]);
  256. send_response($response);
  257. }
  258.  
  259. }
  260.  
  261. function get_sms_delivery_report()
  262. {
  263. if($_GET['task'] === 'result' AND $_GET['secret']=== '123456')
  264. {
  265. $message_results = file_get_contents('php://input');
  266. write_message_to_file("message ".$message_results."nn");
  267. }
  268. }
  269.  
  270.  
  271. if($_SERVER['REQUEST_METHOD'] === 'POST')
  272. {
  273. if(isset($_GET['task']) AND $_GET['task'] === 'result'){
  274. get_sms_delivery_report();
  275. }
  276. else if( isset($_GET['task']) && $_GET['task'] === 'sent')
  277. {
  278. get_sent_message_uuids();
  279. }
  280. else
  281. {
  282. get_message();
  283. }
  284. }
  285. else
  286. {
  287. send_task();
  288. send_messages_uuids_for_sms_delivery_report();
  289. }
  290.  
  291. <?php
  292.  
  293.  
  294. /**
  295. * Gets the messages(SMSs) sent by SMSsync as a POST request.
  296. *
  297. */
  298. function get_message()
  299. {
  300. $error = NULL;
  301. // Set success to false as the default success status
  302. $success = false;
  303. /**
  304. * Get the phone number that sent the SMS.
  305. */
  306. if (isset($_POST['from']))
  307. {
  308. $from = $_POST['from'];
  309. }
  310. else
  311. {
  312. $error = 'The from variable was not set';
  313. }
  314. /**
  315. * Get the SMS aka the message sent.
  316. */
  317. if (isset($_POST['message']))
  318. {
  319. $message = $_POST['message'];
  320. }
  321. else
  322. {
  323. $error = 'The message variable was not set';
  324. }
  325. /**
  326. * Get the secret key set on SMSsync side
  327. * for matching on the server side.
  328. */
  329. if (isset($_POST['secret']))
  330. {
  331. $secret = $_POST['secret'];
  332. }
  333. /**
  334. * Get the timestamp of the SMS
  335. */
  336. if(isset($_POST['sent_timestamp']))
  337. {
  338. $sent_timestamp = $_POST['sent_timestamp'];
  339. }
  340. /**
  341. * Get the phone number of the device SMSsync is
  342. * installed on.
  343. */
  344. if (isset($_POST['sent_to']))
  345. {
  346. $sent_to = $_POST['sent_to'];
  347. }
  348. /**
  349. * Get the unique message id
  350. */
  351. if (isset($_POST['message_id']))
  352. {
  353. $message_id = $_POST['message_id'];
  354. }
  355. /**
  356. * Get device ID
  357. */
  358. if (isset($_POST['device_id']))
  359. {
  360. $device_id = $_POST['device_id'];
  361. }
  362. /**
  363. * Now we have retrieved the data sent over by SMSsync
  364. * via HTTP. Next thing to do is to do something with
  365. * the data. Either echo it or write it to a file or even
  366. * store it in a database. This is entirely up to you.
  367. * After, return a JSON string back to SMSsync to know
  368. * if the web service received the message successfully or not.
  369. *
  370. * In this demo, we are just going to save the data
  371. * received into a text file.
  372. *
  373. */
  374. if ((strlen($from) > 0) AND (strlen($message) > 0) AND
  375. (strlen($sent_timestamp) > 0 )
  376. AND (strlen($message_id) > 0))
  377. {
  378. /* The screte key set here is 123456. Make sure you enter
  379. * that on SMSsync.
  380. */
  381. if ( ( $secret == '123456'))
  382. {
  383. $success = true;
  384. } else
  385. {
  386. $error = "The secret value sent from the device does not match the one on the server";
  387. }
  388. // now let's write the info sent by SMSsync
  389. //to a file called test.txt
  390. $string = "From: ".$from."n";
  391. $string .= "Message: ".$message."n";
  392. $string .= "Timestamp: ".$sent_timestamp."n";
  393. $string .= "Messages Id:" .$message_id."n";
  394. $string .= "Sent to: ".$sent_to."n";
  395. $string .= "Device ID: ".$device_id."nnn";
  396. write_message_to_file($string);
  397. }
  398. /**
  399. * Comment the code below out if you want to send an instant
  400. * reply as SMS to the user.
  401. *
  402. * This feature requires the "Get reply from server" checked on SMSsync.
  403. */
  404. send_instant_message($from);
  405. /**
  406. * Now send a JSON formatted string to SMSsync to
  407. * acknowledge that the web service received the message
  408. */
  409. $response = json_encode([
  410. "payload"=> [
  411. "success"=>$success,
  412. "error" => $error
  413. ]
  414. ]);
  415. //send_response($response);
  416. }
  417.  
  418. /**
  419. * Writes the received responses to a file. This acts as a database.
  420. */
  421. function write_message_to_file($message)
  422. {
  423. $myFile = "test.txt";
  424. $fh = fopen($myFile, 'a') or die("can't open file");
  425. @fwrite($fh, $message);
  426. @fclose($fh);
  427. }
  428.  
  429.  
  430.  
  431.  
  432. $servername = "localhost";
  433. $username = "username";
  434. $password = "";
  435. $dbname = "my_db";
  436.  
  437. try {
  438. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  439. // set the PDO error mode to exception
  440. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  441. $sql = "INSERT INTO my_db (sms_text, sent_dt)
  442. VALUES ('$message', '$sent_timestamp')";
  443. // use exec() because no results are returned
  444. $conn->exec($sql);
  445. echo "New record created successfully";
  446. }
  447. catch(PDOException $e)
  448. {
  449. echo $sql . "<br>" . $e->getMessage();
  450. }
  451.  
  452. $conn = null;
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459. /**
  460. * Implements the task feature. Sends messages to SMSsync to be sent as
  461. * SMS to users.
  462. */
  463. function send_task()
  464. {
  465. /**
  466. * Comment the code below out if you want to send an instant
  467. * reply as SMS to the user.
  468. *
  469. * This feature requires the "Get reply from server" checked on SMSsync.
  470. */
  471. if (isset($_GET['task']) AND $_GET['task'] === 'send')
  472. {
  473. $m = "Sample Task Message";
  474. $f = "+000-000-0000";
  475. $s = "true";
  476. $reply[0] = [
  477. "to" => $f,
  478. "message" => $m,
  479. "uuid" => "1ba368bd-c467-4374-bf28"
  480. ];
  481. // Send JSON response back to SMSsync
  482. $response = json_encode(
  483. ["payload"=>[
  484. "success"=>$s,
  485. "task"=>"send",
  486. "secret" => "123456",
  487. "messages"=>array_values($reply)]
  488. ]);
  489. send_response($response);
  490. }
  491. }
  492.  
  493. /**
  494. * This sends an instant response when the server receive messages(SMSs) from
  495. * SMSsync. This requires the settings "Get Reply from Server" enabled on
  496. * SMSsync.
  497. */
  498. function send_instant_message($to)
  499. {
  500. $m = "Your message has been received";
  501. $f = "+000-000-0000";
  502. $s = true;
  503. $reply[0] = [
  504. "to" => $to,
  505. "message" => $m,
  506. "uuid" => "1ba368bd-c467-4374-bf28"
  507. ];
  508. // Send JSON response back to SMSsync
  509. $response = json_encode(
  510. ["payload"=>[
  511. "success"=>$s,
  512. "task"=>"send",
  513. "secret" => "123456",
  514. "messages"=>array_values($reply)]
  515. ]);
  516. send_response($response);
  517. }
  518.  
  519. function send_response($response)
  520. {
  521. // Avoid caching
  522. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  523. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  524. header("Content-type: application/json; charset=utf-8");
  525. echo $response;
  526. }
  527.  
  528. function get_sent_message_uuids()
  529. {
  530. $data = file_get_contents('php://input');
  531. $queued_messages = file_get_contents('php://input');
  532. // Writing this to a file for demo purposes.
  533. // In production, you will have to process the JSON string
  534. // and remove the messages from the database or where ever the
  535. // messages are stored so the next Task run, the server won't add
  536. // these messages.
  537. write_message_to_file($queued_messages."nn");
  538. send_message_uuids_waiting_for_a_delivery_report($queued_messages);
  539.  
  540. }
  541.  
  542. /**
  543. * Sends message UUIDS to SMSsync for their sms delivery status report.
  544. * When SMSsync send messages from the server as SMS to phone numbers, SMSsync
  545. * can send back status delivery report for these messages.
  546. */
  547. function send_message_uuids_waiting_for_a_delivery_report($queued_messages)
  548. {
  549. // Send back the received messages UUIDs back to SMSsync
  550. $json_obj = json_decode($queued_messages);
  551. $response = json_encode(
  552. [
  553. "message_uuids"=>$json_obj->queued_messages
  554. ]);
  555. send_response($response);
  556. }
  557.  
  558. function send_messages_uuids_for_sms_delivery_report()
  559. {
  560. if(isset($_GET['task']) AND $_GET['task'] == 'result'){
  561. $response = json_encode(
  562. [
  563. "message_uuids" => ['1ba368bd-c467-4374-bf28']
  564. ]);
  565. send_response($response);
  566. }
  567.  
  568. }
  569.  
  570. /**
  571. * Get status delivery report on sent messages
  572. *
  573. */
  574. function get_sms_delivery_report()
  575. {
  576. if($_GET['task'] === 'result' AND $_GET['secret']=== '123456')
  577. {
  578. $message_results = file_get_contents('php://input');
  579. write_message_to_file("message ".$message_results."nn");
  580. }
  581. }
  582.  
  583. // Execute functions above
  584. if($_SERVER['REQUEST_METHOD'] === 'POST')
  585. {
  586. if(isset($_GET['task']) AND $_GET['task'] === 'result'){
  587. get_sms_delivery_report();
  588. }
  589. else if( isset($_GET['task']) && $_GET['task'] === 'sent')
  590. {
  591. get_sent_message_uuids();
  592. }
  593. else
  594. {
  595. get_message();
  596. }
  597. }
  598. else
  599. {
  600. send_task();
  601. send_messages_uuids_for_sms_delivery_report();
  602. }
  603.  
  604.  
  605. ?>
Add Comment
Please, Sign In to add comment