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 === '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 === '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']);