فهرست منبع

Initial commit

Zachary Kotlarek 7 سال پیش
والد
کامیت
11d225b1f9
2فایلهای تغییر یافته به همراه110 افزوده شده و 0 حذف شده
  1. 96 0
      sms.php
  2. 14 0
      test.sh

+ 96 - 0
sms.php

@@ -0,0 +1,96 @@
+<?php
+	define('DEBUG', true);
+	define('PARAMS', array('to', 'from', 'body', 'is_mms'));
+	define('PARAMS_MMS', array('url', 'file_name', 'mime_type', 'file_size'));
+
+	debug_log('READ');
+	$data = @file_get_contents('php://input');
+	if (!$data) {
+		fail('READ');
+	}
+
+	debug_log('JSON');
+	$data = @json_decode($data);
+	if (!$data || !is_object($data)) {
+		fail('JSON');
+	}
+
+	debug_log('FIND');
+	if (!property_exists($data, 'data') || !is_object($data->{'data'}) || 
+		!property_exists($data->{'data'}, 'attributes') ||
+		!is_object($data->{'data'}->{'attributes'})
+	) {
+		fail('FIND');
+	}
+	$msg = $data->{'data'}->{'attributes'};
+
+	debug_log('PARSE');
+	foreach (PARAMS as $param) {
+		if (!property_exists($msg, $param)) {
+			fail('PARSE: ' . $param);
+		}
+	}
+	$sms = array();
+	foreach (PARAMS as $param) {
+		$sms[$param] = $msg->{$param};
+	}
+	unset($msg);
+
+	if ($sms['is_mms']) {
+		debug_log('FIND_MMS');
+		if (!property_exists($data, 'included') || !is_array($data->{'included'})) {
+			fail('FIND_MMS');
+		}
+		$msg = $data->{'included'};
+		foreach ($msg as $part) {
+			if (!property_exists($part, 'attributes') || !is_object($part->{'attributes'})) {
+				fail('FIND_MMS_PART');
+			}
+			$part = $part->{'attributes'};
+
+			debug_log('PARSE_MMS');
+			foreach (PARAMS_MMS as $param) {
+				if (!property_exists($part, $param)) {
+					fail('PARSE_MMS: ' . $param);
+				}
+			}
+			$media = array();
+			foreach (PARAMS_MMS as $param) {
+				$media[$param] = $part->{$param};
+			}
+			$sms['media'][] = $media;
+			unset($msg);
+		}
+	}
+
+	if (DEBUG) {
+		debug_log('DEBUG');
+		debug_log(print_r($sms, true));
+		$file = @tempnam('/var/tmp', 'sms.');
+
+		if (!$file) {
+			fail('STORE_INIT');
+		}
+		debug_log('File: ' . $file);
+
+		if (!@file_put_contents($file . '.raw', print_r($data, true))) {
+			fail('STORE_RAW');
+		}
+		if (!@file_put_contents($file, print_r($sms, true))) {
+			fail('STORE_PARSED');
+		}
+	}
+
+	function fail($msg = 'Unknown error', $code = 500) {
+		http_response_code($code);
+		echo 'Unable to process request: ' . $msg . "\n";
+		exit(0);
+	}
+
+	function debug_log($msg) {
+		if (! DEBUG) {
+			return;
+		}
+		echo $msg . "\n";
+	}
+?>

+ 14 - 0
test.sh

@@ -0,0 +1,14 @@
+#!/bin/bashw
+
+DATA='{"included": [{"attributes": {"url": "https://mms-media-prod.s3.amazonaws.com/FOOBAR-FOO_123.jpg?AWSAccessKeyId=foobar&Expires=1553126125&x-amz-security-token=fooba%3D&Signature=foobar", "file_name": "FOO_123.jpg", "mime_type": "image/jpeg", "file_size": 226639}, "type": "media", "id": "FOOBAR-FOO_123.jpg", "links": {"self": "https://api.flowroute.com/v2.1/media/FOOBAR-FOO_123.jpg"}}, {"attributes": {"url": "https://mms-media-prod.s3.amazonaws.com/FOOBAR-BAR_123.jpg?AWSAccessKeyId=foobar&Expires=1553126125&x-amz-security-token=fooba%3D&Signature=foobar", "file_name": "BAR_123.jpg", "mime_type": "image/jpeg", "file_size": 204756}, "type": "media", "id": "FOOBAR-BAR_123.jpg", "links": {"self": "https://api.flowroute.com/v2.1/media/FOOBAR-BAR_123.jpg"}}], "data": {"relationships": {"media": {"data": [{"type": "media", "id": "FOOBAR-BAR_123.jpg"}, {"type": "media", "id": "FOOBAR-FOO_123.jpg"}]}}, "attributes": {"status": "", "body": "Some text ", "direction": "inbound", "amount_nanodollars": 9500000, "to": "11234567890", "message_encoding": 0, "timestamp": "2019-02-18T23:55:24.00Z", "delivery_receipts": [], "amount_display": "$0.0095", "from": "10987654321", "is_mms": true, "message_type": "longcode"}, "type": "message", "id": "mdr2-foobar"}}'
+
+curl -k -v --header 'Content-Type:application/xml' -d "${DATA}" https://localhost/sms
+
+# SMS Example
+# {"data": {"attributes": {"status": "delivered", "body": "Test 4D", "direction": "inbound", "amount_nanodollars": "4000000", "message_encoding": 0, "timestamp": "2019-02-18T23:00:33.13Z", "to": "11234567890", "amount_display": "$0.0040", "from": "10987654321", "is_mms": false, "message_callback_url": "https://vera.uberzach.com/sms", "message_type": "longcode"}, "type": "message", "id": "mdr2-foobar"}}
+
+# MMS Image Example
+# {"included": [{"attributes": {"url": "https://mms-media-prod.s3.amazonaws.com/FOOBAR-572226513.jpg?AWSAccessKeyId=foobar&Expires=1553125728&x-amz-security-token=fooba%3D&Signature=c32wXzxW%2BiaVhYvwsn7ZOAGjUDo%3D", "file_name": "572226513.jpg", "mime_type": "image/jpeg", "file_size": 659749}, "type": "media", "id": "FOOBAR-572226513.jpg", "links": {"self": "https://api.flowroute.com/v2.1/media/FOOBAR-572226513.jpg"}}], "data": {"relationships": {"media": {"data": [{"type": "media", "id": "FOOBAR-572226513.jpg"}]}}, "attributes": {"status": "", "body": null, "direction": "inbound", "amount_nanodollars": 9500000, "to": "11234567890", "message_encoding": 0, "timestamp": "2019-02-18T23:48:48.00Z", "delivery_receipts": [], "amount_display": "$0.0095", "from": "10987654321", "is_mms": true, "message_type": "longcode"}, "type": "message", "id": "mdr2-foobar"}}
+
+# MMS Text + Multiple Image Example
+# {"included": [{"attributes": {"url": "https://mms-media-prod.s3.amazonaws.com/FOOBAR-FOO_123.jpg?AWSAccessKeyId=foobar&Expires=1553126125&x-amz-security-token=fooba%3D&Signature=foobar", "file_name": "FOO_123.jpg", "mime_type": "image/jpeg", "file_size": 226639}, "type": "media", "id": "FOOBAR-FOO_123.jpg", "links": {"self": "https://api.flowroute.com/v2.1/media/FOOBAR-FOO_123.jpg"}}, {"attributes": {"url": "https://mms-media-prod.s3.amazonaws.com/FOOBAR-BAR_123.jpg?AWSAccessKeyId=foobar&Expires=1553126125&x-amz-security-token=fooba%3D&Signature=foobar", "file_name": "BAR_123.jpg", "mime_type": "image/jpeg", "file_size": 204756}, "type": "media", "id": "FOOBAR-BAR_123.jpg", "links": {"self": "https://api.flowroute.com/v2.1/media/FOOBAR-BAR_123.jpg"}}], "data": {"relationships": {"media": {"data": [{"type": "media", "id": "FOOBAR-BAR_123.jpg"}, {"type": "media", "id": "FOOBAR-FOO_123.jpg"}]}}, "attributes": {"status": "", "body": "Some text ", "direction": "inbound", "amount_nanodollars": 9500000, "to": "11234567890", "message_encoding": 0, "timestamp": "2019-02-18T23:55:24.00Z", "delivery_receipts": [], "amount_display": "$0.0095", "from": "10987654321", "is_mms": true, "message_type": "longcode"}, "type": "message", "id": "mdr2-foobar"}}