V1
This commit is contained in:
86
pagelink.php
Normal file
86
pagelink.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?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') || !defined('AUTH_EDIT')) {
|
||||
luxtools_pagelink_json(403, ['ok' => false, 'error' => 'forbidden']);
|
||||
}
|
||||
|
||||
if (auth_quickaclcheck($pageId) < AUTH_EDIT) {
|
||||
luxtools_pagelink_json(403, ['ok' => false, 'error' => 'forbidden']);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
luxtools_pagelink_json(400, ['ok' => false, 'error' => 'unknown command']);
|
||||
Reference in New Issue
Block a user