register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'displayPageIcon');
$controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'injectLinkIcons');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setPageFavicon');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addUploadFormScript');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addFaviconRuntimeScript');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleAction');
$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'renderAction');
$controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addPageAction');
}
public function addPageAction(Doku_Event $event): void {
global $ID;
if (($event->data['view'] ?? '') !== 'page') return;
if ($this->isActionDisabled('pagesicon')) return;
if (auth_quickaclcheck((string)$ID) < AUTH_UPLOAD) return;
foreach (($event->data['items'] ?? []) as $item) {
if ($item instanceof \dokuwiki\Menu\Item\AbstractItem && $item->getType() === 'pagesicon') {
return;
}
}
$label = (string)$this->getLang('page_action');
if ($label === '') $label = 'Gerer l\'icone';
$title = (string)$this->getLang('page_action_title');
if ($title === '') $title = $label;
$targetPage = cleanID((string)$ID);
$event->data['items'][] = new class($targetPage, $label, $title) extends \dokuwiki\Menu\Item\AbstractItem {
public function __construct(string $targetPage, string $label, string $title) {
parent::__construct();
$this->type = 'pagesicon';
$this->id = $targetPage;
$this->params = [
'do' => 'pagesicon',
];
$this->label = $label;
$this->title = $title;
$this->svg = DOKU_INC . 'lib/images/menu/folder-multiple-image.svg';
}
};
}
private function getIconSize(): int {
return (int)$this->getConf('icon_size');
}
private function isActionDisabled(string $actionName): bool {
global $conf;
$disabled = explode(',', (string)($conf['disableactions'] ?? ''));
$disabled = array_map(static function ($value) {
return strtolower(trim((string)$value));
}, $disabled);
$actionName = strtolower(trim($actionName));
if ($actionName === '') return false;
return in_array($actionName, $disabled, true);
}
private function isLayoutIncludePage(): bool {
global $ID, $INFO;
// DokuWiki populates $INFO['id'] once for the originally requested page, but
// temporarily changes $ID while rendering layout includes (sidebar, footer, …)
// via tpl_include_page(). Comparing them detects any layout include without
// having to hardcode page names.
return isset($INFO['id']) && (string)$ID !== (string)$INFO['id'];
}
public function setPageFavicon(Doku_Event $event): void {
global $ACT, $ID;
if (!(bool)$this->getConf('show_as_favicon')) return;
if ($ACT !== 'show') return;
if ($this->isLayoutIncludePage()) return;
$helper = plugin_load('helper', 'pagesicon');
if (!$helper) return;
$namespace = getNS((string)$ID);
$pageID = noNS((string)$ID);
$size = $this->getIconSize();
$sizeMode = $size > 35 ? 'bigorsmall' : 'smallorbig';
$favicon = $helper->getPageIconUrl($namespace, $pageID, $sizeMode, ['w' => $size]);
if (!$favicon) return;
$favicon = html_entity_decode((string)$favicon, ENT_QUOTES | ENT_HTML5, 'UTF-8');
if (!isset($event->data['link']) || !is_array($event->data['link'])) {
$event->data['link'] = [];
}
$links = [];
foreach ($event->data['link'] as $link) {
if (!is_array($link)) {
$links[] = $link;
continue;
}
$rels = $link['rel'] ?? '';
if (!is_array($rels)) {
$rels = preg_split('/\s+/', strtolower(trim((string)$rels))) ?: [];
}
$rels = array_filter(array_map('strtolower', (array)$rels));
if (in_array('icon', $rels, true)) {
continue;
}
$links[] = $link;
}
$links[] = ['rel' => 'icon', 'href' => $favicon];
$links[] = ['rel' => 'shortcut icon', 'href' => $favicon]; // Kept for legacy browser compatibility.
$event->data['link'] = $links;
if (!isset($event->data['meta']) || !is_array($event->data['meta'])) {
$event->data['meta'] = [];
}
$event->data['meta'][] = ['name' => 'pagesicon-favicon', 'content' => $favicon];
}
public function addFaviconRuntimeScript(Doku_Event $event): void {
global $ACT;
if (!(bool)$this->getConf('show_as_favicon')) return;
if ($ACT !== 'show') return;
if (!isset($event->data['script']) || !is_array($event->data['script'])) {
$event->data['script'] = [];
}
$event->data['script'][] = [
'type' => 'text/javascript',
'src' => DOKU_BASE . 'lib/plugins/pagesicon/script/favicon-runtime.js',
'_data' => 'pagesicon-favicon-runtime',
];
}
public function addUploadFormScript(Doku_Event $event): void {
global $ACT;
if ($ACT !== 'pagesicon') return;
if (!isset($event->data['script']) || !is_array($event->data['script'])) {
$event->data['script'] = [];
}
$event->data['script'][] = [
'type' => 'text/javascript',
'src' => DOKU_BASE . 'lib/plugins/pagesicon/script/upload-form.js',
'_data' => 'pagesicon-upload-form',
];
}
private function hasIconAlready(string $html): bool {
return strpos($html, 'class="pagesicon-injected"') !== false;
}
private function canUploadToTarget(string $targetPage): bool {
if ($targetPage === '') return false;
return auth_quickaclcheck($targetPage) >= AUTH_UPLOAD;
}
private function getDefaultTarget(): string {
global $ID;
return cleanID((string)$ID);
}
private function getDefaultVariant(): string {
global $INPUT;
$defaultVariant = strtolower($INPUT->str('icon_variant'));
if (!in_array($defaultVariant, ['big', 'small'], true)) {
$defaultVariant = 'big';
}
return $defaultVariant;
}
private function getPostedBaseName(array $choices): string {
global $INPUT;
/** @var helper_plugin_pagesicon|null $helper */
$helper = plugin_load('helper', 'pagesicon');
$selected = $helper ? $helper->normalizeIconBaseName($INPUT->post->str('icon_filename')) : '';
if ($selected !== '' && isset($choices[$selected])) return $selected;
return (string)array_key_first($choices);
}
private function getMediaManagerUrl(string $targetPage): string {
$namespace = getNS($targetPage);
return DOKU_MEDIAMANAGER_URL_BASE . '?ns=' . rawurlencode($namespace);
}
private function renderCurrentIconPreview(string $mediaID, string $defaultTarget, string $actionPage, int $previewSize): void {
echo '';
echo '';
echo '';
echo '' . hsc(noNS($mediaID)) . '';
echo '
' . hsc($this->getLang('intro')) . '
'; echo '' . hsc(sprintf($this->getLang('allowed_extensions'), $allowed)) . '
'; echo '