-
Notifications
You must be signed in to change notification settings - Fork 0
/
gate.php
158 lines (123 loc) · 3.83 KB
/
gate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Created by Alexander Purikov for mooServer project
* 2014 - 2015
*/
define('IN_MOOSE', true);
define('TINY_API_LOGIN', true);
header("Content-Type: text/html html; charset=UTF-8");
require_once "config.php";
require_once "php/auth.php";
require_once "php/dbase.php";
require_once "php/moosesms.php";
require_once "php/common.php";
global $auth;
global $db;
$db = new CMooseDb();
$auth = new CMooseAuth($db);
try
{
sms();
makeResponse(true);
}
catch(Exception $e)
{
makeResponse(false, $e->getMessage());
Log::e($db, $auth, "gate", $e->getMessage());
}
CScheduler::safeRun($db, $auth);
exit;
// that's all!
function makeResponse($success, $message = null)
{
$r = ['payload' =>
['success' => $success,
'error' => $message]];
echo json_encode($r);
}
function sms2() // todo add filtering
{
global $db, $auth;
$f = fopen("log.log", "a+");
if ($f === false)
throw new Exception("Error opening!");
//fprintf($f, "%s, %s, %s, %s, %s, %s, %s\n", $gate, $from, $time, $stime, $timeNow, $stn, $body);
foreach($_POST as $query_string_variable => $value)
{
fprintf($f, "$query_string_variable = $value, ");
//echo "$query_string_variable = $value <Br />";
}
fprintf($f, "\n");
fclose($f);
}
function sms() // todo add filtering
{
global $db, $auth;
$device_id = $_POST['device_id'];
$secret = $_POST['secret'];
$body = @$_POST['message']; // !!! add no body for not set
$from = @$_POST['from'];
$time = @$_POST['sent_timestamp'];
//$tn = @$_GET['t2'];
//$sTime = @$_GET['s'];
//$stn = @$_GET['s2'];
//if (@$_GET['log'] != null)
//rawlog ( $_POST);
mylog($device_id, $body, $from, $time, '' );
$time = decodeLongTime($time);
if ( is_null ( $device_id ) || !is_string ( $device_id ) )
throw new Exception("Invalid sms() 'device_id' argument\n");
if ( is_null ( $secret ) || !is_string ( $secret ) )
throw new Exception("Invalid sms() 'secret' argument\n");
if ( is_null ( $from ) || !is_string ( $from ) )
throw new Exception("Invalid sms() 'from' argument\n");
if ( is_null ( $body ) || !is_string ( $body ) )
throw new Exception("Invalid sms() 'body' argument\n");
$res = $auth->gateLogin($db, $device_id, $secret);
if ($res !== true)
throw new Exception("Login as '$device_id' error: '$res'\n");
$msg = CMooseSMS::CreateFromText($body, $time);
$res = $db->AddData($auth, $from, $msg);
Log::t($db, $auth, "addSms", "via gate for '$from' " . CMooseTools::addSmsMessage($res));
return $res;
}
/// @return: false, если не получилось, unixtimestamp -- если пришло нормальное время
function decodeLongTime($time)
{
if (PHP_INT_SIZE == 8)
{
$time = filter_var($time, FILTER_VALIDATE_INT,
['options' => ['min_range' => 946684810000, 'max_range' => 4102444810000]]);
if (is_numeric($time))
$time /= 1000; // боремся с ненужными микросекундами
}
else // на 32-битных системах timestamp с микросекундами не влезает в инт -- пользуемся мягкостью php
{
$time = preg_match('/(\d+)\d{3}/', $time, $matches);
if ($time)
$time = filter_var($matches[1], FILTER_VALIDATE_INT,
['options' => ['min_range' => 946684810, 'max_range' => PHP_INT_MAX]]);
}
return $time;
}
function mylog($device_id, $body, $from, $time, $secret)
{
$f = fopen("log.log", "a+");
if ($f === false)
throw new Exception("Error opening log!");
fprintf($f, "%s, %s, %s, %s, %s\n", $device_id, $from, $time, $body, $secret);
fclose($f);
}
function rawlog($array)
{
$f = fopen("log.log", "a+");
if ($f === false)
throw new Exception("Error opening log!");
foreach($array as $key => $value)
{
fprintf($f, "%s-%s;", $key, $value);
}
fprintf($f, "\n");
fclose($f);
}
?>