Files
luxtools-plugin/syntax/open.php
luxick 6a396ce511
Some checks failed
DokuWiki Default Tasks / all (push) Has been cancelled
Add own admin page for the plugin
2026-01-06 22:39:21 +01:00

88 lines
2.3 KiB
PHP

<?php
use dokuwiki\Extension\SyntaxPlugin;
/**
* luxtools Plugin: Open local path syntax.
*
* Renders an inline button. Clicking it triggers client-side JS that attempts
* to open the configured path in the default file manager (best-effort).
*/
class syntax_plugin_luxtools_open extends SyntaxPlugin
{
/** @inheritdoc */
public function getType()
{
return 'substition';
}
/** @inheritdoc */
public function getPType()
{
// inline
return 'normal';
}
/** @inheritdoc */
public function getSort()
{
return 222;
}
/** @inheritdoc */
public function connectTo($mode)
{
$this->Lexer->addSpecialPattern('\{\{open>.+?\}\}', $mode, 'plugin_luxtools_open');
}
/** @inheritdoc */
public function handle($match, $state, $pos, Doku_Handler $handler)
{
$match = substr($match, strlen('{{open>'), -2);
[$path, $caption] = array_pad(explode('|', $match, 2), 2, '');
$path = trim($path);
$caption = trim($caption);
if ($caption === '') $caption = $path !== '' ? $path : 'Open';
// Basic scheme filtering to avoid javascript: style injections.
// Allow either file:// URLs, or plain paths (Windows/UNC/Linux style).
if (preg_match('/^[a-zA-Z][a-zA-Z0-9+.-]*:/', $path)) {
if (!str_starts_with(strtolower($path), 'file://')) {
return false;
}
}
return [$path, $caption];
}
/** @inheritdoc */
public function render($format, Doku_Renderer $renderer, $data)
{
if ($data === false) return false;
[$path, $caption] = $data;
if ($format !== 'xhtml') {
// no meaningful representation in non-browser formats
$renderer->cdata($caption);
return true;
}
if ($path === '') {
$renderer->cdata('[n/a]');
return true;
}
$serviceUrl = trim((string)$this->getConf('open_service_url'));
$attrs = ' type="button" class="luxtools-open"'
. ' data-path="' . hsc($path) . '"';
if ($serviceUrl !== '') {
$attrs .= ' data-service-url="' . hsc($serviceUrl) . '"';
}
$renderer->doc .= '<button' . $attrs . '>' . hsc($caption) . '</button>';
return true;
}
}