© blackforestbytes
diff --git a/web/model.php b/web/model.php
index d06d3e9..3af5cbe 100644
--- a/web/model.php
+++ b/web/model.php
@@ -17,8 +17,14 @@ function getConfig()
return Statics::$CFG = require "config.php";
}
-function reportError($msg)
+/**
+ * @param String $msg
+ * @param Exception $e
+ */
+function reportError($msg, $e = null)
{
+ if ($e != null) $msg = ($msg."\n\n[[EXCEPTION]]\n" . $e . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString());
+
$subject = "SCN_Server has encountered an Error at " . date("Y-m-d H:i:s") . "] ";
$content = "";
@@ -34,7 +40,17 @@ function reportError($msg)
$content .= '$_POST:' . "\n" . print_r($_POST, true) . "\n";
$content .= '$_FILES:' . "\n" . print_r($_FILES, true) . "\n";
- if (getConfig()['error_reporting']['send-mail'])sendMail($subject, $content, getConfig()['error_reporting']['email-error-target'], getConfig()['error_reporting']['email-error-sender']);
+ if (getConfig()['error_reporting']['send-mail']) sendMail($subject, $content, getConfig()['error_reporting']['email-error-target'], getConfig()['error_reporting']['email-error-sender']);
+}
+
+/**
+ * @param string $subject
+ * @param string $content
+ * @param string $to
+ * @param string $from
+ */
+function sendMail($subject, $content, $to, $from) {
+ mail($to, $subject, $content, 'From: ' . $from);
}
/**
@@ -93,6 +109,7 @@ function generateRandomAuthKey()
* @param $header
* @return array|object|string
* @throws \Httpful\Exception\ConnectionErrorException
+ * @throws Exception
*/
function sendPOST($url, $body, $header)
{
@@ -153,7 +170,7 @@ function verifyOrderToken($tok)
}
catch (Exception $e)
{
- reportError("VerifyOrder token threw exception: " . $e . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString());
+ reportError("VerifyOrder token threw exception", $e);
return false;
}
}
@@ -172,4 +189,11 @@ function refreshVerifyToken()
file_put_contents('.verify_accesstoken', $obj['access_token']);
return $obj->access_token;
+}
+
+function api_return($http_code, $message)
+{
+ http_response_code($http_code);
+ echo $message;
+ die();
}
\ No newline at end of file
diff --git a/web/send.php b/web/send.php
index d13acc1..be0fe83 100644
--- a/web/send.php
+++ b/web/send.php
@@ -2,100 +2,109 @@
include_once 'model.php';
-//------------------------------------------------------------------
-sleep(1);
-//------------------------------------------------------------------
-
-$INPUT = array_merge($_GET, $_POST);
-
-if (!isset($INPUT['user_id'])) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'Missing parameter [[user_id]]']));
-if (!isset($INPUT['user_key'])) die(json_encode(['success' => false, 'errhighlight' => 102, 'message' => 'Missing parameter [[user_token]]']));
-if (!isset($INPUT['title'])) die(json_encode(['success' => false, 'errhighlight' => 103, 'message' => 'Missing parameter [[title]]']));
-
-//------------------------------------------------------------------
-
-$user_id = $INPUT['user_id'];
-$user_key = $INPUT['user_key'];
-$message = $INPUT['title'];
-$content = isset($INPUT['content']) ? $INPUT['content'] : '';
-$priority = isset($INPUT['priority']) ? $INPUT['priority'] : '1';
-
-//------------------------------------------------------------------
-
-if ($priority !== '0' && $priority !== '1' && $priority !== '2') die(json_encode(['success' => false, 'errhighlight' => 105, 'message' => 'Invalid priority']));
-
-if (strlen(trim($message)) == 0) die(json_encode(['success' => false, 'errhighlight' => 103, 'message' => 'No title specified']));
-if (strlen($message) > 120) die(json_encode(['success' => false, 'errhighlight' => 103, 'message' => 'Title too long (120 characters)']));
-if (strlen($content) > 10000) die(json_encode(['success' => false, 'errhighlight' => 104, 'message' => 'Content too long (10000 characters)']));
-
-//------------------------------------------------------------------
-
-$pdo = getDatabase();
-
-$stmt = $pdo->prepare('SELECT user_id, user_key, fcm_token, messages_sent, quota_today, is_pro, quota_day FROM users WHERE user_id = :uid LIMIT 1');
-$stmt->execute(['uid' => $user_id]);
-
-$datas = $stmt->fetchAll(PDO::FETCH_ASSOC);
-if (count($datas)<=0) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'User not found']));
-$data = $datas[0];
-
-if ($data === null) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'User not found']));
-if ($data['user_id'] !== (int)$user_id) die(json_encode(['success' => false, 'errhighlight' => 101, 'message' => 'UserID not found']));
-if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'errhighlight' => 102, 'message' => 'Authentification failed']));
-
-$fcm = $data['fcm_token'];
-
-$new_quota = $data['quota_today'] + 1;
-if ($data['quota_day'] === null || $data['quota_day'] !== date("Y-m-d")) $new_quota=1;
-if ($new_quota > Statics::quota_max($data['is_pro'])) die(json_encode(['success' => false, 'errhighlight' => -1, 'message' => 'Daily quota reached ('.Statics::quota_max($data['is_pro']).')']));
-
-//------------------------------------------------------------------
-
-$url = "https://fcm.googleapis.com/fcm/send";
-$payload = json_encode(
-[
- 'to' => $fcm,
- //'dry_run' => true,
- 'android' => [ 'priority' => 'high' ],
- //'notification' =>
- //[
- // 'title' => $message,
- // 'body' => $content,
- //],
- 'data' =>
- [
- 'title' => $message,
- 'body' => $content,
- 'priority' => $priority,
- 'timestamp' => time(),
- ]
-]);
-$header=
-[
- 'Authorization' => 'key=' . getConfig()['firebase']['server_key'],
- 'Content-Type' => 'application/json',
-];
-
try
{
- $httpresult = sendPOST($url, $payload, $header);
+
+//------------------------------------------------------------------
+//sleep(1);
+//------------------------------------------------------------------
+
+ $INPUT = array_merge($_GET, $_POST);
+
+ if (!isset($INPUT['user_id'])) api_return(400, json_encode(['success' => false, 'error' => 1101, 'errhighlight' => 101, 'message' => 'Missing parameter [[user_id]]']));
+ if (!isset($INPUT['user_key'])) api_return(400, json_encode(['success' => false, 'error' => 1102, 'errhighlight' => 102, 'message' => 'Missing parameter [[user_token]]']));
+ if (!isset($INPUT['title'])) api_return(400, json_encode(['success' => false, 'error' => 1103, 'errhighlight' => 103, 'message' => 'Missing parameter [[title]]']));
+
+//------------------------------------------------------------------
+
+
+ $user_id = $INPUT['user_id'];
+ $user_key = $INPUT['user_key'];
+ $message = $INPUT['title'];
+ $content = isset($INPUT['content']) ? $INPUT['content'] : '';
+ $priority = isset($INPUT['priority']) ? $INPUT['priority'] : '1';
+
+//------------------------------------------------------------------
+
+ if ($priority !== '0' && $priority !== '1' && $priority !== '2') api_return(400, json_encode(['success' => false, 'error' => 1104, 'errhighlight' => 105, 'message' => 'Invalid priority']));
+
+ if (strlen(trim($message)) == 0) api_return(400, json_encode(['success' => false, 'error' => 1201, 'errhighlight' => 103, 'message' => 'No title specified']));
+ if (strlen($message) > 120) api_return(400, json_encode(['success' => false, 'error' => 1202, 'errhighlight' => 103, 'message' => 'Title too long (120 characters)']));
+ if (strlen($content) > 10000) api_return(400, json_encode(['success' => false, 'error' => 1203, 'errhighlight' => 104, 'message' => 'Content too long (10000 characters)']));
+
+//------------------------------------------------------------------
+
+ $pdo = getDatabase();
+
+ $stmt = $pdo->prepare('SELECT user_id, user_key, fcm_token, messages_sent, quota_today, is_pro, quota_day FROM users WHERE user_id = :uid LIMIT 1');
+ $stmt->execute(['uid' => $user_id]);
+
+ $datas = $stmt->fetchAll(PDO::FETCH_ASSOC);
+ if (count($datas)<=0) die(json_encode(['success' => false, 'error' => 1301, 'errhighlight' => 101, 'message' => 'User not found']));
+ $data = $datas[0];
+
+ if ($data === null) api_return(401, json_encode(['success' => false, 'error' => 1301, 'errhighlight' => 101, 'message' => 'User not found']));
+ if ($data['user_id'] !== (int)$user_id) api_return(401, json_encode(['success' => false, 'error' => 1302, 'errhighlight' => 101, 'message' => 'UserID not found']));
+ if ($data['user_key'] !== $user_key) api_return(401, json_encode(['success' => false, 'error' => 1303, 'errhighlight' => 102, 'message' => 'Authentification failed']));
+
+ $fcm = $data['fcm_token'];
+
+ $new_quota = $data['quota_today'] + 1;
+ if ($data['quota_day'] === null || $data['quota_day'] !== date("Y-m-d")) $new_quota=1;
+ if ($new_quota > Statics::quota_max($data['is_pro'])) api_return(403, json_encode(['success' => false, 'error' => 2101, 'errhighlight' => -1, 'message' => 'Daily quota reached ('.Statics::quota_max($data['is_pro']).')']));
+
+//------------------------------------------------------------------
+
+ $url = "https://fcm.googleapis.com/fcm/send";
+ $payload = json_encode(
+ [
+ 'to' => $fcm,
+ //'dry_run' => true,
+ 'android' => [ 'priority' => 'high' ],
+ //'notification' =>
+ //[
+ // 'title' => $message,
+ // 'body' => $content,
+ //],
+ 'data' =>
+ [
+ 'title' => $message,
+ 'body' => $content,
+ 'priority' => $priority,
+ 'timestamp' => time(),
+ ]
+ ]);
+ $header=
+ [
+ 'Authorization' => 'key=' . getConfig()['firebase']['server_key'],
+ 'Content-Type' => 'application/json',
+ ];
+
+ try
+ {
+ $httpresult = sendPOST($url, $payload, $header);
+ }
+ catch (Exception $e)
+ {
+ reportError("FCM communication failed", $e);
+ api_return(403, json_encode(['success' => false, 'error' => 9901, 'errhighlight' => -1, 'message' => 'Communication with firebase service failed.'."\n\n".'Exception: ' . $e->getMessage()]));
+ }
+
+ $stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), messages_sent=messages_sent+1, quota_today=:q, quota_day=NOW() WHERE user_id = :uid');
+ $stmt->execute(['uid' => $user_id, 'q' => $new_quota]);
+
+ api_return(200, json_encode(
+ [
+ 'success' => true,
+ 'message' => 'Message sent',
+ 'response' => $httpresult,
+ 'messagecount' => $data['messages_sent']+1,
+ 'quota' => $new_quota,
+ 'is_pro' => $data['is_pro'],
+ 'quota_max' => Statics::quota_max($data['is_pro']),
+ ]));
}
-catch (Exception $e)
+catch (Exception $mex)
{
- die(json_encode(['success' => false, 'message' => 'Exception: ' . $e->getMessage()]));
+ reportError("Root try-catch triggered", $mex);
}
-
-$stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), messages_sent=messages_sent+1, quota_today=:q, quota_day=NOW() WHERE user_id = :uid');
-$stmt->execute(['uid' => $user_id, 'q' => $new_quota]);
-
-echo (json_encode(
-[
- 'success' => true,
- 'message' => 'Message sent',
- 'response' => $httpresult,
- 'messagecount' => $data['messages_sent']+1,
- 'quota' => $new_quota,
- 'is_pro' => $data['is_pro'],
- 'quota_max' => Statics::quota_max($data['is_pro']),
-]));
-return 0;
\ No newline at end of file