|
|
@@ -4,6 +4,7 @@
|
|
|
define('PARAMS', array('to', 'from', 'body', 'is_mms'));
|
|
|
define('PARAMS_MMS', array('url', 'file_name', 'mime_type', 'file_size'));
|
|
|
define('DEFAULT_NUM', '8002221222');
|
|
|
+ define('MAX_ATTACHMENT_SIZE', 25*(1024*1024));
|
|
|
|
|
|
debug_log('INIT');
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
@@ -13,16 +14,19 @@
|
|
|
require_once 'PHPMailer/src/SMTP.php';
|
|
|
|
|
|
debug_log('READ');
|
|
|
- $data = @file_get_contents('php://input');
|
|
|
- if (!$data) {
|
|
|
+ $raw = @file_get_contents('php://input');
|
|
|
+ if (!$raw) {
|
|
|
fail('READ');
|
|
|
}
|
|
|
|
|
|
debug_log('JSON');
|
|
|
- $data = @json_decode($data);
|
|
|
+ $data = @json_decode($raw);
|
|
|
if (!$data || !is_object($data)) {
|
|
|
fail('JSON');
|
|
|
}
|
|
|
+ if (!DEBUG) {
|
|
|
+ unset($raw);
|
|
|
+ }
|
|
|
|
|
|
debug_log('FIND');
|
|
|
if (!property_exists($data, 'data') || !is_object($data->{'data'}) ||
|
|
|
@@ -44,9 +48,11 @@
|
|
|
$sms[$param] = $msg->{$param};
|
|
|
}
|
|
|
unset($msg);
|
|
|
+ cleanNum($sms, 'to');
|
|
|
+ cleanNum($sms, 'from');
|
|
|
|
|
|
+ debug_log('FIND_MMS');
|
|
|
if ($sms['is_mms']) {
|
|
|
- debug_log('FIND_MMS');
|
|
|
if (!property_exists($data, 'included') || !is_array($data->{'included'})) {
|
|
|
fail('FIND_MMS');
|
|
|
}
|
|
|
@@ -71,38 +77,54 @@
|
|
|
unset($msg);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- debug_log('CLEANUP');
|
|
|
- if (preg_match('/\d*(\d{10})\D*$/', $sms['from'], $matches)) {
|
|
|
- $sms['from'] = $matches[1];
|
|
|
- } else {
|
|
|
- $sms['from'] = DEFAULT_NUM;
|
|
|
- }
|
|
|
- if (preg_match('/\d*(\d{10})\D*$/', $sms['to'], $matches)) {
|
|
|
- $sms['to'] = $matches[1];
|
|
|
- } else {
|
|
|
- $sms['to'] = DEFAULT_NUM;
|
|
|
+ if (!DEBUG) {
|
|
|
+ unset($data);
|
|
|
}
|
|
|
- unset($matches);
|
|
|
|
|
|
debug_log('MAIL');
|
|
|
$mail = new PHPMailer();
|
|
|
if (DEBUG) {
|
|
|
$mail->SMTPDebug = 2;
|
|
|
}
|
|
|
- $mail->setFrom($sms['from'] . '@' . FROM_DOMAIN, 'SMS ' . $sms['from']);
|
|
|
+ $mail->setFrom($sms['from'] . '@' . FROM_DOMAIN, 'SMS ' . $sms['from_pretty']);
|
|
|
$mail->addAddress(TO_ADDR, TO_NAME);
|
|
|
- $mail->Subject = 'SMS Message ' . $sms['to'] . ' => ' . $sms['from'];
|
|
|
+ $mail->Subject = 'SMS Message ' . $sms['from_pretty'] . ' => ' . $sms['to_pretty'];
|
|
|
$mail->Body = $sms['body'];
|
|
|
+ $files = array();
|
|
|
foreach ($sms['media'] as $file) {
|
|
|
- $mail->Body .= "\n\n" . $file['file_name'] . '(' . $file['mime_type'] . '): ' . $file['url'] . "\n";
|
|
|
+ debug_log('MEDIA');
|
|
|
+ if ($file['file_size'] < 1 || $file['file_size'] > MAX_ATTACHMENT_SIZE) {
|
|
|
+ debug_log('FILE SIZE: ' . $file['file_size']);
|
|
|
+ $mail->Body .= "\nWill not fetch " .
|
|
|
+ '(' . $file['file_size'] . '/' . MAX_ATTACHMENT_SIZE . '): ' .
|
|
|
+ $file['url'] . "\n";
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $path = download($file['url']);
|
|
|
+ if (!$path) {
|
|
|
+ debug_log('DOWNLOAD FAILED: ' . $file['url']);
|
|
|
+ $mail->Body .= "\nCould not fetch: " . $file['url'] . "\n";
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ debug_log('ADDING: ' . $path);
|
|
|
+ $files[] = $path;
|
|
|
+ $mail->addAttachment($path, $file['file_name'], 'base64', $file['mime_type'], 'attachment');
|
|
|
}
|
|
|
if (!$mail->send()) {
|
|
|
debug_log('Mail error: ' . $mail->ErrorInfo);
|
|
|
fail('MAIL');
|
|
|
}
|
|
|
+ foreach ($files as $path) {
|
|
|
+ debug_log('REMOVING: ' . $path);
|
|
|
+ @unlink($path);
|
|
|
+ }
|
|
|
unset($mail);
|
|
|
|
|
|
+ debug_log('CLEANUP');
|
|
|
+ if (!DEBUG) {
|
|
|
+ unset($sms);
|
|
|
+ }
|
|
|
+
|
|
|
if (DEBUG) {
|
|
|
debug_log('DEBUG');
|
|
|
debug_log(print_r($sms, true));
|
|
|
@@ -113,14 +135,53 @@
|
|
|
}
|
|
|
debug_log('File: ' . $file);
|
|
|
|
|
|
- if (!@file_put_contents($file . '.raw', print_r($data, true))) {
|
|
|
+ if (!@file_put_contents($file . '.raw', print_r($raw, true))) {
|
|
|
fail('STORE_RAW');
|
|
|
}
|
|
|
+ if (!@file_put_contents($file . '.decoded', print_r($data, true))) {
|
|
|
+ fail('STORE_DECODED');
|
|
|
+ }
|
|
|
if (!@file_put_contents($file, print_r($sms, true))) {
|
|
|
fail('STORE_PARSED');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ function cleanNum(&$sms, $field) {
|
|
|
+ if (preg_match('/\d*(\d{3})(\d{3})(\d{4})\D*$/', $sms[$field], $matches)) {
|
|
|
+ $sms[$field] = $matches[1] . $matches[2] . $matches[3];
|
|
|
+ $sms[$field . '_pretty'] = '(' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
|
|
|
+ } else {
|
|
|
+ debug_log($field . ' number format error: ' . $sms[$field]);
|
|
|
+ $sms[$field] = DEFAULT_NUM;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function download($url) {
|
|
|
+ $file = @tempnam(sys_get_temp_dir(), 'SMS_media.');
|
|
|
+ if (!$file) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $fp = @fopen($file, 'w+');
|
|
|
+ if (!$fp) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $ch = curl_init($url);
|
|
|
+ curl_setopt($ch, CURLOPT_FILE, $fp);
|
|
|
+ curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_MAX_DEFAULT');
|
|
|
+ curl_exec($ch);
|
|
|
+ $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
+ curl_close($ch);
|
|
|
+ fclose($fp);
|
|
|
+
|
|
|
+ if ($code == 200) {
|
|
|
+ return $file;
|
|
|
+ } else {
|
|
|
+ unlink($file);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
function fail($msg = 'Unknown error', $code = 500) {
|
|
|
http_response_code($code);
|
|
|
echo 'Unable to process request: ' . $msg . "\n";
|