-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicoAdmin.php
456 lines (387 loc) · 17.1 KB
/
PicoAdmin.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
class PicoAdmin extends AbstractPicoPlugin
{
protected $dependsOn = array('PicoSession');
protected $session;
protected $modules = array();
protected $requestModule;
protected $requestAction;
protected $requestPayload;
protected $authenticated = false;
protected $authenticationRequired = false;
protected $adminThemeUrl;
/**
* Loads built-in admin modules as Pico plugins
*
* @see DummyPlugin::onPluginsLoaded()
*/
public function onPluginsLoaded(array &$plugins)
{
// require >= Pico 2.0; Pico::VERSION wasn't defined before Pico 2.0
if (!defined('Pico::VERSION')) {
$this->setEnabled(false);
return;
}
// load built-in admin module PicoContentAdmin
require_once(__DIR__ . '/PicoContentAdmin.php');
$this->loadPlugin(new PicoContentAdmin($this->getPico()));
}
/**
* Bootstrap the plugin's default configuration
*
* @see DummyPlugin::onConfigLoaded()
*/
public function onConfigLoaded(array &$config)
{
$defaultPluginConfig = array(
'url' => 'admin',
'auth_token' => ''
);
if (!isset($config['PicoAdmin']) || !is_array($config['PicoAdmin'])) {
$config['PicoAdmin'] = $defaultPluginConfig;
return;
}
$config['PicoAdmin'] += $defaultPluginConfig;
if (!$config['PicoAdmin']['url']) {
$config['PicoAdmin']['url'] = $defaultPluginConfig['url'];
} else {
$config['PicoAdmin']['url'] = trim($config['PicoAdmin']['url'], '/');
}
}
public function onSessionInit(PicoSession $session, &$pluginsReadOnly, &$plugins)
{
$this->session = $session;
$plugins['PicoAdmin'] = $this;
}
public function onRequestUrl(&$url)
{
$adminUrlPrefix = $this->getPluginConfig('url');
$adminUrlRegex = '#^' . preg_quote($adminUrlPrefix, '#') . '(?:/(\w+)(?:/(\w+)(?:/(.+))?)?)?$#';
if (preg_match($adminUrlRegex, $url, $adminUrlMatches)) {
$this->requestModule = isset($adminUrlMatches[1]) ? $adminUrlMatches[1] : '';
$this->requestAction = isset($adminUrlMatches[2]) ? $adminUrlMatches[2] : '';
$this->requestPayload = isset($adminUrlMatches[3]) ? $adminUrlMatches[3] : '';
$this->triggerEvent('onAdminInitializing', array($this, &$this->modules));
$this->handleAdminRequest();
$this->handleAuthentication();
$this->triggerEvent('onAdminInitialized');
return;
}
// the user wasn't requesting a page of this plugin
$this->setEnabled(false);
}
protected function handleAdminRequest()
{
$authToken = $this->getPluginConfig('auth_token');
if (!$authToken) {
// force info screen when no auth token is configured
$this->requestModule = 'info';
$this->requestAction = $this->requestPayload = '';
} elseif (!$this->requestModule) {
// when only a single module is registered, redirect to the main page of this module
if (count($this->modules) === 1) {
foreach ($this->modules as $moduleName => $module) {
header($_SERVER['SERVER_PROTOCOL'] . ' 307 Temporary Redirect');
header('Location: ' . $this->getAdminPageUrl($moduleName));
die();
}
}
// show built-in landing page when no specific module was requested
// landing page requires authentication, therefore non-authenticated users will end up on the login page
$this->requestModule = 'landing';
$this->requestAction = $this->requestPayload = '';
$this->authenticationRequired = true;
}
$this->triggerEvent('onAdminRequest', array(
&$this->requestModule,
&$this->requestAction,
&$this->requestPayload
));
}
protected function handleAuthentication()
{
// check auth token
$authToken = $this->getPluginConfig('auth_token');
if ($authToken) {
if (isset($_REQUEST['logout'])) {
// perform logout
$this->authenticated = false;
$this->session->set('PicoAdmin', 'authenticated', false);
$this->session->migrateSession();
} elseif (isset($_POST['password'])) {
// verify password
$this->authenticated = password_verify((string) $_POST['password'], $authToken);
$this->session->migrateSession();
$this->session->set('PicoAdmin', 'authenticated', $this->authenticated);
} else {
// already authenticated session
$this->authenticated = (bool) $this->session->get('PicoAdmin', 'authenticated');
}
$this->session->close('PicoAdmin');
}
$this->triggerEvent('onAdminAuthentication', array(
&$this->authenticationRequired,
&$this->authenticated
));
if ($this->authenticationRequired && !$this->authenticated) {
// force login screen when authentication was requested, but the user isn't authenticated
// admin modules which require authentication MUST disable themselves in this case!
$this->requestModule = 'login';
$this->requestAction = $this->requestPayload = '';
}
}
public function onRequestFile(&$file)
{
// reset requested file
switch ($this->requestModule) {
case 'info':
$file = __DIR__ . '/content/info.md';
break;
case 'login':
$file = __DIR__ . '/content/login.md';
break;
default:
$file = __DIR__ . '/content/admin.md';
break;
}
}
public function onMetaParsed(array &$meta)
{
switch ($this->requestModule) {
case 'info':
if (isset($_POST['password'])) {
$meta['admin_auth_token'] = $this->generateAuthToken((string) $_POST['password']);
} else {
$meta['admin_auth_token'] = '';
}
break;
}
}
public function onContentPrepared(&$content)
{
$variables = array();
$variables['%admin_url%'] = rtrim($this->getPageUrl($this->getPluginConfig('url')), '/');
$variables['%admin_theme_url%'] = rtrim($this->getAdminThemeUrl(), '/');
$content = str_replace(array_keys($variables), $variables, $content);
}
public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
{
// send "401 Unauthorized" header when authentication was requested, but user isn't authenticated
if ($this->authenticationRequired && !$this->authenticated) {
header($_SERVER['SERVER_PROTOCOL'] . ' 401 Unauthorized');
}
// requesting login page with a JSON request - send auth status
if (($this->requestModule === 'login') && $this->isJsonRequest()) {
$twig->getLoader()->addPath(__DIR__ . '/theme');
$templateName = 'admin-json.twig';
$twigVariables['json'] = array();
$twigVariables['json']['admin_auth'] = $this->authenticated;
$twigVariables['json']['admin_auth_required'] = $this->authenticationRequired;
}
// fallback to frontend theme
if (in_array($this->requestModule, array('info', 'login'), true)) {
return;
}
// landing page
if ($this->requestModule === 'landing') {
$twig->getLoader()->addPath(__DIR__ . '/theme');
$templateName = 'admin.twig';
}
// register admin_link filter
$twig->addFilter(new Twig_SimpleFilter('admin_link', array($this, 'getAdminPageUrl')));
// register include_block filter
$twig->addFunction(new Twig_SimpleFunction(
'include_block',
function (Twig_Environment $env, $context, $templateName, $blockName) {
$template = $env->loadTemplate($templateName);
return $template->renderBlock($blockName, $context);
},
array('needs_context' => true, 'needs_environment' => true)
));
// register variables
$twigVariables['admin_modules'] = $this->modules;
$twigVariables['admin_url'] = rtrim($this->getPageUrl($this->getPluginConfig('url')));
$twigVariables['admin_theme_url'] = rtrim($this->getAdminThemeUrl(), '/');
$twigVariables['admin_request'] = array(
'module' => $this->requestModule,
'action' => $this->requestAction,
'payload' => $this->requestPayload
);
$twigVariables['admin_auth'] = $this->authenticated;
$twigVariables['admin_auth_required'] = $this->authenticationRequired;
}
protected function generateAuthToken($password)
{
$authToken = null;
for ($time = 0, $timeTarget = 0.2, $cost = 10; $time < $timeTarget; $cost++) {
$startTime = microtime(true);
$authToken = password_hash($password, PASSWORD_DEFAULT, array('cost' => $cost));
$time = (microtime(true) - $startTime);
if (!$authToken) {
throw new RuntimeException('Unable to generate auth token');
}
}
return $authToken;
}
public function isAuthenticated()
{
return $this->authenticated;
}
public function getAdminPageUrl($page, $queryData = null)
{
$page = $page ? $this->getPluginConfig('url') . '/' . $page : $this->getPluginConfig('url');
return $this->getPageUrl($page, $queryData, false);
}
public function getAdminThemeUrl()
{
if ($this->adminThemeUrl) {
return $this->adminThemeUrl;
}
$basePath = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
$basePathLength = strlen($basePath);
if (substr(__DIR__, 0, $basePathLength) === $basePath) {
$this->adminThemeUrl = $this->getBaseUrl() . substr(__DIR__, $basePathLength) . '/theme/';
} else {
$this->adminThemeUrl = $this->getBaseUrl() . 'plugins/PicoAdmin/theme/';
}
return $this->adminThemeUrl;
}
public function isJsonRequest()
{
return (isset($_SERVER['HTTP_ACCEPT']) && ($_SERVER['HTTP_ACCEPT'] === 'application/json'));
}
public function writeFile($basePath, $path, $content)
{
$isWindows = (strncasecmp(PHP_OS, 'WIN', 3) === 0);
$pathComponents = explode('/', $path);
$fileName = array_pop($pathComponents);
$path = '';
if (!$fileName) {
throw new RuntimeException('The given file path is invalid');
}
// create directory structure
foreach ($pathComponents as $pathComponent) {
$parentPath = $path;
$path .= $pathComponent . '/';
if (!file_exists($basePath . $path)) {
if (!is_writable($basePath . $parentPath) || (!$isWindows && !is_executable($basePath . $parentPath))) {
throw new RuntimeException(
"You don't have permission to create files or directories in "
. ($parentPath ? '"' . $parentPath . '"' : 'the content directory')
);
}
// create directory
$mkdirResult = @mkdir($this->getConfig('content_dir') . $path);
if (!$mkdirResult) {
throw new RuntimeException('Creating directory "' . $path . '" failed for an unknown reason');
}
} elseif (!is_dir($this->getConfig('content_dir') . $path)) {
throw new RuntimeException(
"You can't create the directory \"" . $path . '", '
. 'because there is already another directory entry of this name'
);
}
}
// check file permissions
if (file_exists($basePath . $path . $fileName)) {
if (!is_file($basePath . $path . $fileName)) {
throw new RuntimeException(
"You can't create the file \"" . $path . $fileName . '", '
. 'because there is already another directory entry of this name'
);
}
if (!is_writable($basePath . $path . $fileName)) {
throw new RuntimeException("You don't have permission to write to \"" . $path . $fileName . '"');
}
} else {
if (!is_writable($basePath . $path) || (!$isWindows && !is_executable($basePath . $path))) {
$pathName = $path ? '"' . $path . '"' : 'the content directory';
throw new RuntimeException("You don't have permission to create files in " . $pathName);
}
}
// write file
// LOCK_EX delays concurrent write attempts from other PHP processes,
// but doesn't prevent Pico from reading a not completely written file
// that's because file_get_contents() doesn't observe locks
$writtenBytes = @file_put_contents($basePath . $path . $fileName, $content, LOCK_EX);
if ($writtenBytes === false) {
throw new RuntimeException('Writing to file "' . $path . $fileName . '" failed for an unknown reason');
}
}
public function deleteFile($basePath, $path)
{
$isWindows = (strncasecmp(PHP_OS, 'WIN', 3) === 0);
$pathComponents = explode('/', $path);
$fileName = array_pop($pathComponents);
$path = $pathComponents ? implode('/', $pathComponents) . '/' : '';
if (!$fileName) {
throw new RuntimeException('The given file path is invalid');
}
// check file permissions
if (!file_exists($basePath . $path . $fileName)) {
throw new RuntimeException('File "' . $path . $fileName . '" not found');
}
if (!is_writable($basePath . $path . $fileName)) {
// this permission actually isn't necessary to delete the file, it's just a safety check
throw new RuntimeException("You don't have permission to modify file \"" . $path . $fileName . '"');
}
if (!is_writable($basePath . $path) || (!$isWindows && !is_executable($basePath . $path))) {
$pathName = $path ? '"' . $path . '"' : 'the content directory';
throw new RuntimeException("You don't have permission to delete files in " . $pathName);
}
// delete file
$unlinkResult = @unlink($basePath . $path . $fileName);
if (!$unlinkResult) {
throw new RuntimeException('Deleting file "' . $path . $fileName . '" failed for an unknown reason');
}
// delete empty directories
if ($path !== '') {
do {
array_pop($pathComponents);
$parentPath = implode('/', $pathComponents);
if (!is_writable($basePath . $path)) {
// this permission actually isn't necessary to delete the directory, it's just a safety check
throw new RuntimeException("You don't have permission to modify directory \"" . $path . '"');
}
if (!is_writable($basePath . $parentPath) || (!$isWindows && !is_executable($basePath . $parentPath))) {
throw new RuntimeException(
"You don't have permission to delete files or directories in "
. ($parentPath ? '"' . $parentPath . '"' : 'the content directory')
);
}
// check whether the directory is empty
if (is_readable($basePath . $path) && (($handle = @opendir($basePath . $path)) !== false)) {
while (($file = readdir($handle)) !== false) {
if (($file !== '.') && ($file !== '..')) {
// directory not empty; abort...
closedir($handle);
return;
}
}
closedir($handle);
// directory is empty, proceed deleting it
$rmdirResult = @rmdir($basePath . $path);
if (!$rmdirResult) {
throw new RuntimeException(
'Deleting empty directory "' . $path . '" failed for an unknown reason'
);
}
} else {
// try deleting the dir anyway
// we don't care about the result because a failure is likely caused by other files
@rmdir($basePath . $path);
}
} while (($path = $parentPath) !== '');
}
}
/**
* @see PicoPluginInterface::setEnabled()
*/
public function setEnabled($enabled, $recursive = true, $auto = false)
{
if (!$enabled && $this->session) {
$this->session->close('PicoAdmin');
}
parent::setEnabled($enabled, $recursive, $auto);
}
}