/
index.php
369 lines (339 loc) · 14.5 KB
/
index.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
include_once('lib/config.php');
include_once('lib/Renderer.php');
include_once('classes/User.php');
include_once('classes/Files.php');
include_once('classes/Shares.php');
$user = new User();
$renderer = new Renderer('layouts/overall.tpl');
$path = isset($_GET['path']) ? $_GET['path'] : false;
$mod = isset($_GET['mod']) ? $_GET['mod'] : false;
$token = isset($_POST['token']) ? $_POST['token'] : false;
$hasUser = $user->loadCurrent();
if ($path)
{
if ($hasUser)
{
$renderer->setNav('nav/dashboard.tpl');
switch ($path)
{
case 'account':
$files = new Files();
$files->getBy(array('_user_id' => $user->getAttribute('user_id')));
$id = isset($_POST['id']) ? $_POST['id'] : false;
$password1 = isset($_POST['password1']) ? $_POST['password1'] : false;
$password2 = isset($_POST['password2']) ? $_POST['password2'] : false;
$passwordError = false;
$passwordMessage = false;
$deleteError = false;
if ($mod && $mod === 'update')
{
if ($password1 === $password2 && $token === $_SESSION['token'])
{
$user->setPassword($password1);
if ($user->save())
{
$passwordMessage = "Your password has been changed";
}
else
{
$passwordError = 'There was a problem saving your password';
}
}
else
{
$passwordError = "Your passwords didn't match! Give it another shot.";
}
}
else if ($mod && $mod === 'delete')
{
if ($id && $token === $_SESSION['token'] && $user->getAttribute('user_id') === $id)
{
if ($user->delete())
{
header('Location: '.WEB_ROOT.'/logout');
exit();
}
}
$deleteError = 'There was a problem deleting your account.';
}
$renderer->setTitle('Your info here');
$renderer->setKeywords(array(
'your',
'info',
'here'
));
$renderer->setDescription('Manage your account');
$renderer->addContent('account.tpl', array(
'user' => $user,
'filesCount' => count($files->getElements()),
'passwordError' => $passwordError,
'deleteError' => $deleteError,
'token' => $_SESSION['token'],
'passwordMessage' => $passwordMessage
));
break;
case 'files':
$files = new Files();
$files->orderby('created DESC');
$files->getBy(array('_user_id' => $user->getAttribute('user_id')));
if ($mod && $mod === 'json')
{
header('Content-Type: application/json');
echo $files->getJSON();
exit();
}
else if ($mod && $mod === 'delete')
{
header('Content-Type: application/json');
if ($token === $_SESSION['token'])
{
$id = isset($_POST['id']) ? $_POST['id'] : false;
$file = new File(array(
'file_id' => $id,
'_user_id' => $user->getAttribute('user_id')
));
if ($file->loadByAttributes())
{
if ($file->delete())
{
echo json_encode(array('deleted' => $file->getAttributes()));
exit();
}
}
}
echo json_encode(array('error' => 'Could not delete that file'));
exit();
}
else if ($mod && $mod === 'upload')
{
if ($token === $_SESSION['token'])
{
$target = isset($_FILES['file']) ? $_FILES['file'] : false;
if ($target)
{
$location = FILES_DIR.'/'.$user->getAttribute('user_id').'/'.$target['name'];
if (move_uploaded_file($target['tmp_name'], $location))
{
$file = new File();
$file->setAttribute('name', $target['name']);
$file->setAttribute('location', $location);
$test = $file->loadByAttributes();
$file->setAttribute('mime_type', $target['type']);
$file->setAttribute('size', $target['size']);
$file->setAttribute('_user_id', $user->getAttribute('user_id'));
$file->setAttribute('created', date('Y:m:d H:i:s'));
if ($file->save())
{
$file->loadByAttributes();
if ($test)
{
echo json_encode(array('updated' => $file->getAttributes()));
}
else
{
echo $file->getJSON();
}
exit();
}
else
{
echo json_encode(array('error' => 'Your file was uploaded, but not saved to the database.'));
exit();
}
}
}
}
echo json_encode(array('error' => 'There was a problem uploading your file'));
exit();
}
else if ($mod && $mod === 'download')
{
$id = isset($_GET['id']) ? $_GET['id'] : false;
if ($id)
{
$file = new File(array('file_id' => $id, '_user_id' => $user->getAttribute('user_id')));
if ($file->loadByAttributes())
{
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$file->getAttribute('name'));
header('Content-Type: '.$file->getAttribute('mime_type'));
header('Content-Transfer-Encodeing: binary');
readfile($file->getAttribute('location'));
exit();
}
}
header('HTTP/1.0 404 Not Found');
exit();
}
else
{
$renderer->setTitle('Your stuff');
$renderer->setKeywords(array(
'your',
'stuff',
'here'
));
$renderer->addScript('scripts/upload.js');
$renderer->addScript('scripts/json_parse.js');
$renderer->addContent('dashboard.tpl', array('files' => $files->getElements(), 'token' => $_SESSION['token'], 'filesCount' => count($files->getElements())));
}
break;
case 'logout':
session_unset();
session_destroy();
header('Location: '.WEB_ROOT);
exit();
break;
case 'about':
$renderer->setTitle('About');
$renderer->setKeywords(array(
'store',
'files',
'upload',
'easy',
'fast',
'anywhere',
'ownership',
'licensing',
'responsibility'
));
$renderer->setDescription('What is stor.me? Why should I use it? What happens when I do use it?');
$renderer->addContent('about.tpl');
break;
default:
header('Location: '.WEB_ROOT.'/files/');
exit();
}
}
else
{
$renderer->setNav('nav/index.tpl');
switch ($path)
{
case 'signup':
$renderer->setTitle('Create an account');
$renderer->setKeywords(array(
'store',
'files',
'upload',
'easy',
'fast',
'anywhere'
));
$renderer->setDescription('Sign up for easy file storage, right from your browser!');
$user = new User();
$error = false;
if (count($_POST) > 0)
{
if ($user->loadByAttributes(array('email' => $_POST['email'])))
{
$error = "There's already an account with that email";
}
else if ($_POST['password1'] !== $_POST['password2'])
{
$error = "Your passwords don't match! Give it another shot.";
}
else
{
$user->setAttributes($_POST);
$user->setPassword($_POST['password1']);
if ($user->save())
{
if ($user->login())
{
mkdir(FILES_DIR.'/'.$user->getAttribute('user_id'));
header('Location: '.WEB_ROOT.'/files');
exit();
}
else
{
$error = 'Could not log in';
}
}
else
{
$error = 'Error saving';
}
}
}
$renderer->addContent('signup.tpl', array('user' => $user, 'error' => $error));
break;
case 'login':
$renderer->setTitle('Login');
$renderer->setKeywords(array(
'store',
'files',
'upload',
'easy',
'fast',
'anywhere'
));
$renderer->setDescription('Sign up for easy file storage, right from your browser!');
if (count($_POST) > 0)
{
$user->setAttribute('email', $_POST['email']);
$user->setPassword($_POST['password']);
if ($user->login())
{
header('Location: '.WEB_ROOT.'/files');
exit();
}
else
{
$renderer->addContent('login.tpl', array('user' => $user, 'error' => 'Uh-oh. Was that the right password? Do you have an account?'));
}
}
else
{
$renderer->addContent('login.tpl', array('user' => $user));
}
break;
case 'about':
$renderer->setTitle('About');
$renderer->setKeywords(array(
'store',
'files',
'upload',
'easy',
'fast',
'anywhere',
'ownership',
'licensing',
'responsibility'
));
$renderer->setDescription('What is stor.me? Why should I use it? What happens when I do use it?');
$renderer->addContent('about.tpl');
break;
default:
header('Location: '.WEB_ROOT.'/login');
exit();
}
}
}
else
{
if ($hasUser)
{
header('Location: '.WEB_ROOT.'/files');
exit();
}
else
{
$renderer->setNav('nav/index.tpl');
$renderer->setTitle('Put your stuff here');
$renderer->setKeywords(array(
'store',
'files',
'upload',
'easy',
'fast',
'anywhere'
));
$renderer->setDescription('Easy, cloud-based storage for your stuff. Any file, any size, anywhere.');
$renderer->addContent('index.tpl');
}
}
$renderer->render();
?>