169 lines
4.9 KiB
PHP
169 lines
4.9 KiB
PHP
<?php
|
|
|
|
// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
|
|
|
|
use dokuwiki\plugin\luxtools\PageLink;
|
|
|
|
require_once(__DIR__ . '/autoload.php');
|
|
|
|
if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../../');
|
|
require_once(DOKU_INC . 'inc/init.php');
|
|
|
|
global $INPUT;
|
|
|
|
$syntax = plugin_load('syntax', 'luxtools');
|
|
if (!$syntax) {
|
|
http_status(500);
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['ok' => false, 'error' => 'plugin disabled']);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Send a JSON response.
|
|
*
|
|
* @param int $status
|
|
* @param array $payload
|
|
* @return void
|
|
*/
|
|
function luxtools_pagelink_json(int $status, array $payload): void
|
|
{
|
|
http_status($status);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
header('Pragma: no-cache');
|
|
echo json_encode($payload);
|
|
exit;
|
|
}
|
|
|
|
$cmd = (string)$INPUT->str('cmd');
|
|
$pageId = (string)$INPUT->str('id');
|
|
if (function_exists('cleanID')) {
|
|
$pageId = (string)cleanID($pageId);
|
|
}
|
|
|
|
if ($cmd === '' || $pageId === '') {
|
|
luxtools_pagelink_json(400, ['ok' => false, 'error' => 'missing parameters']);
|
|
}
|
|
|
|
if (!function_exists('auth_quickaclcheck')) {
|
|
luxtools_pagelink_json(403, ['ok' => false, 'error' => 'forbidden']);
|
|
}
|
|
|
|
$acl = auth_quickaclcheck($pageId);
|
|
if ($cmd === 'info' || $cmd === 'download') {
|
|
if (!defined('AUTH_READ') || $acl < AUTH_READ) {
|
|
luxtools_pagelink_json(403, ['ok' => false, 'error' => 'forbidden']);
|
|
}
|
|
} else {
|
|
if (!defined('AUTH_EDIT') || $acl < AUTH_EDIT) {
|
|
luxtools_pagelink_json(403, ['ok' => false, 'error' => 'forbidden']);
|
|
}
|
|
}
|
|
|
|
if ($cmd === 'info') {
|
|
$depth = (int)$syntax->getConf('pagelink_search_depth');
|
|
if ($depth < 0) $depth = 0;
|
|
|
|
$pageLink = new PageLink((string)$syntax->getConf('paths'), $depth);
|
|
$uuid = $pageLink->getPageUuid($pageId);
|
|
if ($uuid === null) {
|
|
luxtools_pagelink_json(200, [
|
|
'ok' => true,
|
|
'uuid' => null,
|
|
'linked' => false,
|
|
'folder' => null,
|
|
'multiple' => false,
|
|
]);
|
|
}
|
|
|
|
$info = $pageLink->resolveUuid($uuid);
|
|
$folder = $info['folder'] ?? null;
|
|
$multiple = !empty($info['multiple']);
|
|
|
|
luxtools_pagelink_json(200, [
|
|
'ok' => true,
|
|
'uuid' => $uuid,
|
|
'linked' => is_string($folder) && $folder !== '',
|
|
'folder' => is_string($folder) ? $folder : null,
|
|
'multiple' => $multiple,
|
|
]);
|
|
}
|
|
|
|
if ($cmd === 'download') {
|
|
$depth = (int)$syntax->getConf('pagelink_search_depth');
|
|
if ($depth < 0) $depth = 0;
|
|
|
|
$pageLink = new PageLink((string)$syntax->getConf('paths'), $depth);
|
|
$uuid = $pageLink->getPageUuid($pageId);
|
|
if ($uuid === null || $uuid === '') {
|
|
http_status(404);
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
header('Pragma: no-cache');
|
|
echo 'not linked';
|
|
exit;
|
|
}
|
|
|
|
http_status(200);
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
header('Content-Disposition: attachment; filename=".pagelink"; filename*=UTF-8\'\'%2Epagelink');
|
|
header('Cache-Control: no-store, no-cache, must-revalidate');
|
|
header('Pragma: no-cache');
|
|
echo $uuid;
|
|
exit;
|
|
}
|
|
|
|
if ($cmd === 'ensure') {
|
|
if (strtoupper($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
|
|
luxtools_pagelink_json(405, ['ok' => false, 'error' => 'method not allowed']);
|
|
}
|
|
|
|
if (!checkSecurityToken()) {
|
|
luxtools_pagelink_json(403, ['ok' => false, 'error' => 'bad token']);
|
|
}
|
|
|
|
$depth = (int)$syntax->getConf('pagelink_search_depth');
|
|
if ($depth < 0) $depth = 0;
|
|
|
|
$pageLink = new PageLink((string)$syntax->getConf('paths'), $depth);
|
|
$uuid = $pageLink->getPageUuid($pageId);
|
|
|
|
if ($uuid !== null) {
|
|
luxtools_pagelink_json(200, ['ok' => true, 'uuid' => $uuid, 'created' => false]);
|
|
}
|
|
|
|
$uuid = PageLink::createUuidV4();
|
|
$ok = $pageLink->setPageUuid($pageId, $uuid);
|
|
|
|
if (!$ok) {
|
|
luxtools_pagelink_json(500, ['ok' => false, 'error' => 'save failed']);
|
|
}
|
|
|
|
luxtools_pagelink_json(200, ['ok' => true, 'uuid' => $uuid, 'created' => true]);
|
|
}
|
|
|
|
if ($cmd === 'unlink') {
|
|
if (strtoupper($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
|
|
luxtools_pagelink_json(405, ['ok' => false, 'error' => 'method not allowed']);
|
|
}
|
|
|
|
if (!checkSecurityToken()) {
|
|
luxtools_pagelink_json(403, ['ok' => false, 'error' => 'bad token']);
|
|
}
|
|
|
|
$depth = (int)$syntax->getConf('pagelink_search_depth');
|
|
if ($depth < 0) $depth = 0;
|
|
|
|
$pageLink = new PageLink((string)$syntax->getConf('paths'), $depth);
|
|
$result = $pageLink->unlinkPage($pageId);
|
|
|
|
luxtools_pagelink_json(200, [
|
|
'ok' => true,
|
|
'uuid' => $result['uuid'] ?? null,
|
|
'folder' => $result['folder'] ?? null,
|
|
]);
|
|
}
|
|
|
|
luxtools_pagelink_json(400, ['ok' => false, 'error' => 'unknown command']);
|