forked from danmichaelo/croptool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
110 lines (83 loc) · 3.07 KB
/
bootstrap.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
<?php
use CropTool\Controllers\AuthController;
use CropTool\Controllers\FileController;
use Slim\Http\Request;
use Slim\Http\Response;
if (!defined('ROOT_PATH')) {
define('ROOT_PATH', __DIR__);
}
/**********************************************************************************
* PHP config
*/
error_reporting(E_ALL);
// ini_set('display_errors', '1');
// ini_set('display_startup_errors', 1);
ini_set('memory_limit', '512M');
/**********************************************************************************
* The array_get method from Laravel
*
* @param array $data
* @param string $key
* @param string $default
*
* @return mixed
*/
if (! function_exists('array_get')) {
function array_get($data, $key, $default = null) {
if (!is_array($data)) {
return $default;
}
return isset($data[$key]) ? $data[$key] : $default;
}
}
/**********************************************************************************
* Middleware
*/
$authMiddleware = function (Request $request, Response $response, $next) {
$user = $this->get(\CropTool\Auth\UserService::class);
$auth = $this->get(\CropTool\Auth\AuthServiceInterface::class);
if (!$user->loggedin()) {
return $response->withStatus(401)->withJson([
'error' => 'Unauthorized',
'messages' => $auth->getMessages(),
]);
}
return $next($request, $response);
};
$pageMiddleware = function (Request $request, Response $response, $next) {
// Middleware that defines the current WikiPage
$title = $request->isGet() ? $request->getQueryParam('title') : $request->getParsedBodyParam('title');
$factory = $this->get('DI\FactoryInterface');
$this->set(CropTool\WikiPage::class, $factory->make(CropTool\WikiPage::class, [
'title' => $title,
]));
return $next($request, $response);
};
/**********************************************************************************
* Init app
*/
$app = new CropTool\App();
$app->add(CropTool\SessionInterface::class);
$app->get('/api/ping', function ($request, $response) {
return $response->write('pong');
});
/**********************************************************************************
* Auth routes
*/
$app->group('/api/auth', function () use ($authMiddleware) {
$this->get('/login', [AuthController::class, 'login']);
$this->get('/callback', [AuthController::class, 'authCallback']);
$this->get('/logout', [AuthController::class, 'logout']);
$this->get('/user', [AuthController::class, 'getUser'])->add($authMiddleware);
});
/**********************************************************************************
* Page routes
*/
$app->group('/api/file', function () use ($authMiddleware) {
$this->get('/exists', [FileController::class, 'exists']);
$this->get('/info', [FileController::class, 'info']);
$this->get('/autodetect', [FileController::class, 'autodetect']);
$this->get('/crop', [FileController::class, 'crop']);
$this->post('/publish', [FileController::class, 'publish']);
})->add($authMiddleware)->add($pageMiddleware);
return $app;