This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax.php
216 lines (181 loc) · 5.64 KB
/
ajax.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
//ob_start('gzheader');
require_once('small/small.php');
error_reporting(E_ALL);
ini_set('display_errors','On');
if (isset($_GET['action'])) {
// create a user
if ($_GET['action'] == 'create_user') {
if (
isset($_POST['email']) &&
isset($_POST['password']) &&
isset($_SESSION['nonce'])
) {
// check nonce
$passIn = filter_input(INPUT_POST, 'password');
$pass = substr($passIn, 0, 64);
$nonce = substr($passIn, 64, 128);
if ($nonce != hash('sha256', $_SESSION['nonce'])) {
echo '{"error": "incorrect nonce"}';
return;
}
if (!isset($_POST['name'])) { $_POST['name'] = ""; }
if (!isset($_POST['bio'])) { $_POST['bio'] = ""; }
User::create(
filter_input(INPUT_POST, 'email'),
$pass,
filter_input(INPUT_POST, 'name'),
filter_input(INPUT_POST, 'bio')
);
echo '{"notice" : "SUCCESS", "error": null}';
return;
}
else {
if (!isset($_SESSION['nonce'])) {
echo '{"error" : "no nonce on server"}';
return;
}
echo '{"error" : "required fields not supplied"}';
return;
}
}
// logging in
else if ($_GET['action'] == 'login') {
if (isset($_POST['email']) && isset($_POST['password'])) {
if (!isset($_SESSION['nonce'])) {
echo '{"error": "no nonce on server"}';
return;
}
/*else */
else {
$passIn = filter_input(INPUT_POST, 'password');
$pass = substr($passIn, 0, 64);
$nonce = substr($passIn, 64, 128);
if ($nonce != hash('sha256', $_SESSION['nonce'])) {
echo '{"error" : "incorrect nonce"}';
return;
}
if (User::login(
filter_input(INPUT_POST, 'email'),
$pass
)) {
echo '{"error": null, "notice": "You are being logged in"}';
return;
}
echo '{"error": "Incorrect email/password combo"}';
return;
}
}
else {
echo '{"error" : "No username or password"}';
return;
}
echo '{"error": "function not implmented yet"}';
return;
}
// logging out
else if ($_GET['action'] == 'logout') {
User::logout();
}
// create post
else if ($_GET['action'] == 'create_post') {
if (isset($_POST['title']) && isset($_POST['content']) && isset($_POST['author'])) {
$post_id = Post::create(
filter_input(INPUT_POST, 'title'),
filter_input(INPUT_POST, 'content'),
filter_input(INPUT_POST, 'author')
);
if ($post_id) {
echo "{'error' null, 'notice': 'post created', 'post_id': $post_id}";
return;
}
else {
echo '{"error": "unknown issues occured..."}';
return;
}
}
else {
echo '{"error" : "No title, content, or author id"}' . "\n\n";
var_dump($_POST);
return;
}
echo '{"error": "function not implemented yet"}';
return;
}
// delete post
else if ($_GET['action'] == 'delete_post') {
if (isset($_POST['id'])) {
Post::delete($_POST['id']);
echo '{"error": null, "notice": "successfully deleted"}';
return;
}
else {
echo '{"error" : "no post id specified"}';
return;
}
echo '{"error" : "function not implemented yet"}';
return;
}
// get markdown content
else if ($_GET['action'] == 'post_markdown') {
echo '{"error" : "function not implemented yet"}';
return;
}
// get nonce
else if ($_GET['action'] == 'get_nonce') {
$_SESSION['nonce'] = randString(32);
echo '{"error": null, "nonce": "' . $_SESSION['nonce'] . '"}';
return;
}
// change a password
else if ($_GET['action'] == 'change_password') {
if (!isset($_SESSION['nonce'])) {
echo '{"error": "no nonce on server"}';
return;
}
else if (isset($_POST['username']) && isset($_POST['password'])) {
$passIn = filter_input(INPUT_POST, 'password');
$nonce = substr($passIn, 64, 128);
$pass = substr($passIn, 0, 64);
if ($nonce != hash('sha256', $_SESSION['nonce'])) {
echo '{' . "\n\t\"error\" : \"incorrect nonce\",";
echo "\n\t\"client\" : \"$nonce\", \n\t\"server\" : \"";
echo hash('sha256', $_SESSION['nonce']);
echo '"\n}';
//echo '{"error" : "incorrect nonce", "cNonce" : "' . $nonce '", "sNonce": "' . hash('sha256', $_SESSION['nonce']) . '"}';
return;
}
User::changePassword(filter_input(INPUT_POST, 'username'), $pass);
echo '{"error": null, "notice": "password changed"}';
return;
}
echo '{"error": "function not implemented yet"}';
return;
}
// get users in json string
else if ($_GET['action'] == 'get_users') {
echo '{';
$i = 0;
foreach (User::all() as $user) {
$i++;
echo "\n\t\"{$user->username}\": {";
echo "\n\t\t\"id\": \"{$user->id}\", ";
echo "\n\t\t\"username\": \"{$user->username}\", ";
echo "\n\t\t\"email\": \"{$user->email}\", ";
echo "\n\t\t\"name\": \"{$user->name}\", ";
echo "\n\t\t\"biography\": \"{$user->biography}\"";
echo "\n\t}";
if ($i != count(User::all())) {
echo ',';
}
echo "\n";
}
echo '}';
return;
}
}
else {
echo "{'error': 'No action'}";
}
if (isset($_SESSION['nonce'])) { unset($_SESSION['nonce']); }
?>