-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange.php
108 lines (85 loc) · 2.82 KB
/
change.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/appVars.inc.php';
function time_diff($t1, $t2)
{
return abs($t1-$t2);
}
function tokenMatchesUser($token, $username)
{
return (tokenFromUsername($username) == $token);
}
function isWpValid($wp)
{
if(!is_numeric($wp)) return false;
if(intval($wp) != floatval($wp)) return false;
if(0 > intval($wp) || intval($wp) >= WORD_COUNT) return false;
return true;
}
function isAlternativeValid($word)
{
return (mb_strlen($word) <= MAX_LEN) && (strpos($word, " ") === false);
}
function isUserOveractive($username)
{
$database = initDatabase();
$modifications = $database->select('modifications', 'change_time', ['username'=>$username]);
$userLimit = $database->select('users', 'w_limit', ['username'=>$username])[0];
rsort($modifications);
return !(count($modifications) < $userLimit || time_diff($modifications[$userLimit-1], time()) > TIME_QUANTUM);
}
function validateRequest($input)
{
//Characters need to be legal
$result = [];
if(!isset($input['username']) || !isset($input['wp']) || !isset($input['alternative']) || !isset($input['token']))
{
$result['success'] = false;
$result['errorMsg'] = 'درخواست بد';
return $result;
}
if(!tokenMatchesUser($input['token'], $input['username']))
{
$result['success'] = false;
$result['errorMsg'] = 'درخواست بد';
return $result;
}
if(!isWpValid($input['wp']))
{
$result['success'] = false;
herokuLog("invalid wordpalce!".$input['wp']);
return $result;
}
if(!isAlternativeValid($input['alternative']))
{
$result['success'] = false;
$result['errorMsg'] = 'این چیه آخه بزرگوار!';
return $result;
}
if(isUserOveractive($input['username']))
{
$result['success'] = false;
$result['errorMsg'] = 'کار و زندگی نداری رفیق؟ برو بشین پا درسات! محدودیت تعداد تغییر داریما!';
return $result;
}
$result['success'] = true;
return $result;
}
function main()
{
$input = json_decode(file_get_contents('php://input'), true);
$requestValidity = validateRequest($input);
if($requestValidity["success"] == true)
{
$database = initDatabase();
$username = $input['username'];
$word_place = $input['wp'];
$change_time = time();
$alternative = $input['alternative'];
$database->insert('modifications',
['username'=>$username, 'word_place'=>$word_place, 'change_time'=>$change_time]);
$database->update('story', ['word'=>$alternative, 'writer'=>$username], ['word_place'=>$word_place]);
}
echo(json_encode($requestValidity));
}
main();