1da933f89SLORTET<?php 2da933f89SLORTETif(!defined('DOKU_INC')) die(); 3b603bbe1SLORTETif(!defined('DOKU_MEDIAMANAGER_URL_BASE')) define('DOKU_MEDIAMANAGER_URL_BASE', DOKU_BASE . 'lib/exe/mediamanager.php'); 4da933f89SLORTET 5da933f89SLORTETclass action_plugin_pagesicon extends DokuWiki_Action_Plugin { 6da933f89SLORTET public function register(Doku_Event_Handler $controller) { 7da933f89SLORTET $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, '_displaypageicon'); 8da933f89SLORTET $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setPageFavicon'); 9b603bbe1SLORTET $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addUploadFormScript'); 10b603bbe1SLORTET $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addFaviconRuntimeScript'); 11da933f89SLORTET $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleAction'); 12da933f89SLORTET $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'renderAction'); 13da933f89SLORTET $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addPageAction'); 14da933f89SLORTET } 15da933f89SLORTET 16b603bbe1SLORTET public function addPageAction(Doku_Event $event): void { 17da933f89SLORTET global $ID; 18da933f89SLORTET 19da933f89SLORTET if (($event->data['view'] ?? '') !== 'page') return; 20437e13a7SLORTET if ($this->isActionDisabled('pagesicon')) return; 21da933f89SLORTET if (auth_quickaclcheck((string)$ID) < AUTH_UPLOAD) return; 22da933f89SLORTET 23da933f89SLORTET foreach (($event->data['items'] ?? []) as $item) { 24da933f89SLORTET if ($item instanceof \dokuwiki\Menu\Item\AbstractItem && $item->getType() === 'pagesicon') { 25da933f89SLORTET return; 26da933f89SLORTET } 27da933f89SLORTET } 28da933f89SLORTET 29da933f89SLORTET $label = (string)$this->getLang('page_action'); 30da933f89SLORTET if ($label === '') $label = 'Gerer l\'icone'; 31da933f89SLORTET $title = (string)$this->getLang('page_action_title'); 32da933f89SLORTET if ($title === '') $title = $label; 33da933f89SLORTET $targetPage = cleanID((string)$ID); 34da933f89SLORTET 35da933f89SLORTET $event->data['items'][] = new class($targetPage, $label, $title) extends \dokuwiki\Menu\Item\AbstractItem { 36b603bbe1SLORTET public function __construct(string $targetPage, string $label, string $title) { 37da933f89SLORTET parent::__construct(); 38da933f89SLORTET $this->type = 'pagesicon'; 39da933f89SLORTET $this->id = $targetPage; 40da933f89SLORTET $this->params = [ 41da933f89SLORTET 'do' => 'pagesicon', 42da933f89SLORTET ]; 43da933f89SLORTET $this->label = $label; 44da933f89SLORTET $this->title = $title; 45da933f89SLORTET $this->svg = DOKU_INC . 'lib/images/menu/folder-multiple-image.svg'; 46da933f89SLORTET } 47da933f89SLORTET }; 48da933f89SLORTET } 49da933f89SLORTET 50da933f89SLORTET private function getIconSize(): int { 51b603bbe1SLORTET return (int)$this->getConf('icon_size'); 52da933f89SLORTET } 53da933f89SLORTET 54437e13a7SLORTET private function isActionDisabled(string $actionName): bool { 55437e13a7SLORTET global $conf; 56437e13a7SLORTET 57437e13a7SLORTET $disabled = explode(',', (string)($conf['disableactions'] ?? '')); 58437e13a7SLORTET $disabled = array_map(static function ($value) { 59437e13a7SLORTET return strtolower(trim((string)$value)); 60437e13a7SLORTET }, $disabled); 61437e13a7SLORTET $actionName = strtolower(trim($actionName)); 62437e13a7SLORTET if ($actionName === '') return false; 63437e13a7SLORTET 64437e13a7SLORTET return in_array($actionName, $disabled, true); 65437e13a7SLORTET } 66437e13a7SLORTET 67b603bbe1SLORTET private function isLayoutIncludePage(string $pageID): bool { 68b603bbe1SLORTET // DokuWiki may temporarily switch $ID while rendering layout includes such as 69b603bbe1SLORTET // sidebar/footer. In these hooks we have no reliable "main content only" flag, 70b603bbe1SLORTET // so we explicitly ignore those technical pages to avoid replacing the current 71b603bbe1SLORTET // page icon/favicon with the one from the layout include. 72b603bbe1SLORTET return $pageID === 'sidebar' || $pageID === 'footer'; 73b603bbe1SLORTET } 74b603bbe1SLORTET 75b603bbe1SLORTET public function setPageFavicon(Doku_Event $event): void { 76da933f89SLORTET global $ACT, $ID; 77da933f89SLORTET 78da933f89SLORTET if (!(bool)$this->getConf('show_as_favicon')) return; 79da933f89SLORTET if ($ACT !== 'show') return; 80da933f89SLORTET 81da933f89SLORTET $pageID = noNS((string)$ID); 82b603bbe1SLORTET if ($this->isLayoutIncludePage($pageID)) return; 83da933f89SLORTET 84da933f89SLORTET $helper = plugin_load('helper', 'pagesicon'); 85da933f89SLORTET if (!$helper) return; 86da933f89SLORTET 87da933f89SLORTET $namespace = getNS((string)$ID); 88b603bbe1SLORTET $favicon = $helper->getPageIconUrl($namespace, $pageID, 'smallorbig', ['w' => 32]); 89da933f89SLORTET if (!$favicon) return; 90*b09be489SLORTET $favicon = html_entity_decode((string)$favicon, ENT_QUOTES | ENT_HTML5, 'UTF-8'); 91da933f89SLORTET 92da933f89SLORTET if (!isset($event->data['link']) || !is_array($event->data['link'])) { 93da933f89SLORTET $event->data['link'] = []; 94da933f89SLORTET } 95da933f89SLORTET 96da933f89SLORTET $links = []; 97da933f89SLORTET foreach ($event->data['link'] as $link) { 98da933f89SLORTET if (!is_array($link)) { 99da933f89SLORTET $links[] = $link; 100da933f89SLORTET continue; 101da933f89SLORTET } 102da933f89SLORTET 103da933f89SLORTET $rels = $link['rel'] ?? ''; 104da933f89SLORTET if (!is_array($rels)) { 105da933f89SLORTET $rels = preg_split('/\s+/', strtolower(trim((string)$rels))) ?: []; 106da933f89SLORTET } 107da933f89SLORTET $rels = array_filter(array_map('strtolower', (array)$rels)); 108da933f89SLORTET if (in_array('icon', $rels, true)) { 109da933f89SLORTET continue; 110da933f89SLORTET } 111da933f89SLORTET $links[] = $link; 112da933f89SLORTET } 113da933f89SLORTET 114da933f89SLORTET $links[] = ['rel' => 'icon', 'href' => $favicon]; 115b603bbe1SLORTET $links[] = ['rel' => 'shortcut icon', 'href' => $favicon]; // Kept for legacy browser compatibility. 116da933f89SLORTET $event->data['link'] = $links; 117da933f89SLORTET } 118da933f89SLORTET 119b603bbe1SLORTET public function addUploadFormScript(Doku_Event $event): void { 120b603bbe1SLORTET global $ACT; 121b603bbe1SLORTET 122b603bbe1SLORTET if ($ACT !== 'pagesicon') return; 123b603bbe1SLORTET 124b603bbe1SLORTET if (!isset($event->data['script']) || !is_array($event->data['script'])) { 125b603bbe1SLORTET $event->data['script'] = []; 126b603bbe1SLORTET } 127b603bbe1SLORTET 128b603bbe1SLORTET $event->data['script'][] = [ 129b603bbe1SLORTET 'type' => 'text/javascript', 130b603bbe1SLORTET 'src' => DOKU_BASE . 'lib/plugins/pagesicon/script/upload-form.js', 131b603bbe1SLORTET '_data' => 'pagesicon-upload-form', 132b603bbe1SLORTET ]; 133b603bbe1SLORTET } 134b603bbe1SLORTET 135b603bbe1SLORTET public function addFaviconRuntimeScript(Doku_Event $event): void { 136b603bbe1SLORTET global $ACT; 137b603bbe1SLORTET 138b603bbe1SLORTET if (!(bool)$this->getConf('show_as_favicon')) return; 139b603bbe1SLORTET if ($ACT !== 'show') return; 140b603bbe1SLORTET 141b603bbe1SLORTET if (!isset($event->data['script']) || !is_array($event->data['script'])) { 142b603bbe1SLORTET $event->data['script'] = []; 143b603bbe1SLORTET } 144b603bbe1SLORTET 145b603bbe1SLORTET $event->data['script'][] = [ 146b603bbe1SLORTET 'type' => 'text/javascript', 147b603bbe1SLORTET 'src' => DOKU_BASE . 'lib/plugins/pagesicon/script/favicon-runtime.js', 148b603bbe1SLORTET '_data' => 'pagesicon-favicon-runtime', 149b603bbe1SLORTET ]; 150b603bbe1SLORTET } 151b603bbe1SLORTET 152da933f89SLORTET private function hasIconAlready(string $html, string $mediaID): bool { 153da933f89SLORTET return strpos($html, 'class="pagesicon-injected"') !== false; 154da933f89SLORTET } 155da933f89SLORTET 156b603bbe1SLORTET private function injectFaviconRuntimeScript(string &$html, string $faviconHref): void { 157da933f89SLORTET if ($faviconHref === '') return; 158*b09be489SLORTET $faviconHref = html_entity_decode($faviconHref, ENT_QUOTES | ENT_HTML5, 'UTF-8'); 159da933f89SLORTET 160b603bbe1SLORTET $marker = '<span class="pagesicon-favicon-runtime" data-href="' . hsc($faviconHref) . '" hidden></span>'; 161b603bbe1SLORTET $html = $marker . $html; 162da933f89SLORTET } 163da933f89SLORTET 164b603bbe1SLORTET private function canUploadToTarget(string $targetPage): bool { 165da933f89SLORTET if ($targetPage === '') return false; 166da933f89SLORTET return auth_quickaclcheck($targetPage) >= AUTH_UPLOAD; 167da933f89SLORTET } 168da933f89SLORTET 169b603bbe1SLORTET private function getDefaultTarget(): string { 170da933f89SLORTET global $ID; 171da933f89SLORTET return cleanID((string)$ID); 172da933f89SLORTET } 173da933f89SLORTET 174b603bbe1SLORTET private function getDefaultVariant(): string { 175da933f89SLORTET global $INPUT; 176da933f89SLORTET $defaultVariant = strtolower($INPUT->str('icon_variant')); 177da933f89SLORTET if (!in_array($defaultVariant, ['big', 'small'], true)) { 178da933f89SLORTET $defaultVariant = 'big'; 179da933f89SLORTET } 180da933f89SLORTET return $defaultVariant; 181da933f89SLORTET } 182da933f89SLORTET 183b603bbe1SLORTET private function getPostedBaseName(array $choices): string { 184da933f89SLORTET global $INPUT; 185b603bbe1SLORTET /** @var helper_plugin_pagesicon|null $helper */ 186b603bbe1SLORTET $helper = plugin_load('helper', 'pagesicon'); 187b603bbe1SLORTET $selected = $helper ? $helper->normalizeIconBaseName($INPUT->post->str('icon_filename')) : ''; 188da933f89SLORTET if ($selected !== '' && isset($choices[$selected])) return $selected; 189da933f89SLORTET return (string)array_key_first($choices); 190da933f89SLORTET } 191da933f89SLORTET 192b603bbe1SLORTET private function getMediaManagerUrl(string $targetPage): string { 193da933f89SLORTET $namespace = getNS($targetPage); 194b603bbe1SLORTET return DOKU_MEDIAMANAGER_URL_BASE . '?ns=' . rawurlencode($namespace); 195da933f89SLORTET } 196da933f89SLORTET 197b603bbe1SLORTET private function renderCurrentIconPreview(string $mediaID, string $defaultTarget, string $actionPage, int $previewSize): void { 198b603bbe1SLORTET echo '<a href="' . hsc($this->getMediaManagerUrl($defaultTarget)) . '" target="_blank" title="' . hsc($this->getLang('open_media_manager')) . '">'; 199b603bbe1SLORTET echo '<img src="' . ml($mediaID, ['w' => $previewSize]) . '" alt="" width="' . $previewSize . '" style="display:block;margin:6px 0;" />'; 200b603bbe1SLORTET echo '</a>'; 201b603bbe1SLORTET echo '<small>' . hsc(noNS($mediaID)) . '</small>'; 202b603bbe1SLORTET echo '<form action="' . wl($actionPage) . '" method="post" style="margin-top:6px;">'; 203b603bbe1SLORTET formSecurityToken(); 204b603bbe1SLORTET echo '<input type="hidden" name="do" value="pagesicon" />'; 205b603bbe1SLORTET echo '<input type="hidden" name="media_id" value="' . hsc($mediaID) . '" />'; 206b603bbe1SLORTET echo '<input type="hidden" name="pagesicon_delete_submit" value="1" />'; 207b603bbe1SLORTET echo '<button type="submit" class="button">' . hsc($this->getLang('delete_icon')) . '</button>'; 208b603bbe1SLORTET echo '</form>'; 209da933f89SLORTET } 210da933f89SLORTET 211b603bbe1SLORTET private function handleDeletePost(): void { 212da933f89SLORTET global $INPUT, $ID; 213da933f89SLORTET 214da933f89SLORTET if (!$INPUT->post->has('pagesicon_delete_submit')) return; 215da933f89SLORTET if (!checkSecurityToken()) return; 216da933f89SLORTET 217da933f89SLORTET $targetPage = cleanID((string)$ID); 218da933f89SLORTET $mediaID = cleanID($INPUT->post->str('media_id')); 219da933f89SLORTET 220da933f89SLORTET if ($targetPage === '' || $mediaID === '') { 221da933f89SLORTET msg($this->getLang('error_delete_invalid'), -1); 222da933f89SLORTET return; 223da933f89SLORTET } 224da933f89SLORTET if (!$this->canUploadToTarget($targetPage)) { 225da933f89SLORTET msg($this->getLang('error_no_upload_permission'), -1); 226da933f89SLORTET return; 227da933f89SLORTET } 228da933f89SLORTET $namespace = getNS($targetPage); 229da933f89SLORTET $pageID = noNS($targetPage); 230da933f89SLORTET $helper = plugin_load('helper', 'pagesicon'); 231b603bbe1SLORTET $currentBig = ($helper && method_exists($helper, 'getPageIconId')) ? (string)$helper->getPageIconId($namespace, $pageID, 'big') : ''; 232b603bbe1SLORTET $currentSmall = ($helper && method_exists($helper, 'getPageIconId')) ? (string)$helper->getPageIconId($namespace, $pageID, 'small') : ''; 233da933f89SLORTET $allowed = array_values(array_filter(array_unique([$currentBig, $currentSmall]))); 234da933f89SLORTET if (!$allowed || !in_array($mediaID, $allowed, true)) { 235da933f89SLORTET msg($this->getLang('error_delete_invalid'), -1); 236da933f89SLORTET return; 237da933f89SLORTET } 238da933f89SLORTET 239da933f89SLORTET $file = mediaFN($mediaID); 240da933f89SLORTET if (!@file_exists($file)) { 241da933f89SLORTET msg($this->getLang('error_delete_not_found'), -1); 242da933f89SLORTET return; 243da933f89SLORTET } 244da933f89SLORTET if (!@unlink($file)) { 245da933f89SLORTET msg($this->getLang('error_delete_failed'), -1); 246da933f89SLORTET return; 247da933f89SLORTET } 248da933f89SLORTET 249b603bbe1SLORTET if ($helper) { 250b603bbe1SLORTET $helper->notifyIconUpdated($targetPage, 'delete', $mediaID); 251b603bbe1SLORTET } 252da933f89SLORTET msg(sprintf($this->getLang('delete_success'), hsc($mediaID)), 1); 253da933f89SLORTET } 254da933f89SLORTET 255b603bbe1SLORTET private function handleUploadPost(): void { 256b603bbe1SLORTET global $INPUT, $ID, $conf; 257da933f89SLORTET 258da933f89SLORTET if (!$INPUT->post->has('pagesicon_upload_submit')) return; 259da933f89SLORTET if (!checkSecurityToken()) return; 260da933f89SLORTET 261da933f89SLORTET $targetPage = cleanID((string)$ID); 262da933f89SLORTET if (!$this->canUploadToTarget($targetPage)) { 263da933f89SLORTET msg($this->getLang('error_no_upload_permission'), -1); 264da933f89SLORTET return; 265da933f89SLORTET } 266da933f89SLORTET 267da933f89SLORTET $variant = strtolower($INPUT->post->str('icon_variant')); 268da933f89SLORTET if (!in_array($variant, ['big', 'small'], true)) { 269da933f89SLORTET $variant = 'big'; 270da933f89SLORTET } 271da933f89SLORTET 272da933f89SLORTET if (!isset($_FILES['pagesicon_file']) || !is_array($_FILES['pagesicon_file'])) { 273da933f89SLORTET msg($this->getLang('error_missing_file'), -1); 274da933f89SLORTET return; 275da933f89SLORTET } 276da933f89SLORTET 277da933f89SLORTET $upload = $_FILES['pagesicon_file']; 278da933f89SLORTET if (($upload['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) { 279da933f89SLORTET msg($this->getLang('error_upload_failed') . ' (' . (int)($upload['error'] ?? -1) . ')', -1); 280da933f89SLORTET return; 281da933f89SLORTET } 282da933f89SLORTET 283da933f89SLORTET $originalName = (string)($upload['name'] ?? ''); 284da933f89SLORTET $tmpName = (string)($upload['tmp_name'] ?? ''); 285da933f89SLORTET if ($tmpName === '' || !is_uploaded_file($tmpName)) { 286da933f89SLORTET msg($this->getLang('error_upload_failed'), -1); 287da933f89SLORTET return; 288da933f89SLORTET } 289da933f89SLORTET 290da933f89SLORTET $ext = strtolower((string)pathinfo($originalName, PATHINFO_EXTENSION)); 291da933f89SLORTET if ($ext === '') { 292da933f89SLORTET msg($this->getLang('error_extension_missing'), -1); 293da933f89SLORTET return; 294da933f89SLORTET } 295da933f89SLORTET 296b603bbe1SLORTET $helper = plugin_load('helper', 'pagesicon'); 297b603bbe1SLORTET $allowed = ($helper && method_exists($helper, 'getConfiguredExtensions')) 298b603bbe1SLORTET ? $helper->getConfiguredExtensions() 299b603bbe1SLORTET : []; 300da933f89SLORTET if (!in_array($ext, $allowed, true)) { 301da933f89SLORTET msg(sprintf($this->getLang('error_extension_not_allowed'), hsc($ext), hsc(implode(', ', $allowed))), -1); 302da933f89SLORTET return; 303da933f89SLORTET } 304da933f89SLORTET 305b603bbe1SLORTET $choices = ($helper && method_exists($helper, 'getUploadNameChoices')) 306b603bbe1SLORTET ? $helper->getUploadNameChoices($targetPage, $variant) 307b603bbe1SLORTET : []; 308da933f89SLORTET $base = $this->getPostedBaseName($choices); 309da933f89SLORTET $namespace = getNS($targetPage); 310da933f89SLORTET $mediaBase = $namespace !== '' ? ($namespace . ':' . $base) : $base; 311da933f89SLORTET $mediaID = cleanID($mediaBase . '.' . $ext); 312da933f89SLORTET $targetFile = mediaFN($mediaID); 313da933f89SLORTET 314da933f89SLORTET io_makeFileDir($targetFile); 315da933f89SLORTET if (!@is_dir(dirname($targetFile))) { 316da933f89SLORTET msg($this->getLang('error_write_dir'), -1); 317da933f89SLORTET return; 318da933f89SLORTET } 319da933f89SLORTET 320da933f89SLORTET $moved = @move_uploaded_file($tmpName, $targetFile); 321da933f89SLORTET if (!$moved) { 322da933f89SLORTET $moved = @copy($tmpName, $targetFile); 323da933f89SLORTET } 324da933f89SLORTET if (!$moved) { 325da933f89SLORTET msg($this->getLang('error_write_file'), -1); 326da933f89SLORTET return; 327da933f89SLORTET } 328da933f89SLORTET 329b603bbe1SLORTET @chmod($targetFile, $conf['fmode']); 330b603bbe1SLORTET if ($helper) { 331b603bbe1SLORTET $helper->notifyIconUpdated($targetPage, 'upload', $mediaID); 332b603bbe1SLORTET } 333da933f89SLORTET msg(sprintf($this->getLang('upload_success'), hsc($mediaID)), 1); 334da933f89SLORTET } 335da933f89SLORTET 336b603bbe1SLORTET private function renderUploadForm(): void { 337da933f89SLORTET global $ID, $INPUT; 338da933f89SLORTET 339da933f89SLORTET $defaultTarget = $this->getDefaultTarget(); 340da933f89SLORTET $defaultVariant = $this->getDefaultVariant(); 341b603bbe1SLORTET $helper = plugin_load('helper', 'pagesicon'); 342b603bbe1SLORTET $allowed = ($helper && method_exists($helper, 'getConfiguredExtensions')) 343b603bbe1SLORTET ? implode(', ', $helper->getConfiguredExtensions()) 344b603bbe1SLORTET : ''; 345b603bbe1SLORTET $currentChoices = ($helper && method_exists($helper, 'getUploadNameChoices')) 346b603bbe1SLORTET ? $helper->getUploadNameChoices($defaultTarget, $defaultVariant) 347b603bbe1SLORTET : []; 348b603bbe1SLORTET $selectedBase = $helper ? $helper->normalizeIconBaseName($INPUT->str('icon_filename')) : ''; 349da933f89SLORTET if (!isset($currentChoices[$selectedBase])) { 350da933f89SLORTET $selectedBase = (string)array_key_first($currentChoices); 351da933f89SLORTET } 352da933f89SLORTET $filenameHelp = hsc($this->getLang('icon_filename_help')); 353da933f89SLORTET $actionPage = $defaultTarget !== '' ? $defaultTarget : cleanID((string)$ID); 354da933f89SLORTET $namespace = getNS($defaultTarget); 355da933f89SLORTET $pageID = noNS($defaultTarget); 356b603bbe1SLORTET $previewSize = $this->getIconSize(); 357b603bbe1SLORTET $currentBig = ($helper && method_exists($helper, 'getPageIconId')) ? $helper->getPageIconId($namespace, $pageID, 'big') : false; 358b603bbe1SLORTET $currentSmall = ($helper && method_exists($helper, 'getPageIconId')) ? $helper->getPageIconId($namespace, $pageID, 'small') : false; 359da933f89SLORTET 360da933f89SLORTET echo '<h1>' . hsc($this->getLang('menu')) . '</h1>'; 361da933f89SLORTET echo '<p>' . hsc($this->getLang('intro')) . '</p>'; 362da933f89SLORTET echo '<p><small>' . hsc(sprintf($this->getLang('allowed_extensions'), $allowed)) . '</small></p>'; 363da933f89SLORTET echo '<div class="pagesicon-current-preview" style="display:flex;gap:24px;align-items:flex-start;flex-wrap:wrap;margin:10px 0 16px;">'; 364da933f89SLORTET echo '<div class="pagesicon-current-item">'; 365da933f89SLORTET echo '<strong>' . hsc($this->getLang('current_big_icon')) . '</strong><br />'; 366da933f89SLORTET if ($currentBig) { 367b603bbe1SLORTET $this->renderCurrentIconPreview($currentBig, $defaultTarget, $actionPage, $previewSize); 368da933f89SLORTET } else { 369da933f89SLORTET echo '<small>' . hsc($this->getLang('current_icon_none')) . '</small>'; 370da933f89SLORTET } 371da933f89SLORTET echo '</div>'; 372da933f89SLORTET echo '<div class="pagesicon-current-item">'; 373da933f89SLORTET echo '<strong>' . hsc($this->getLang('current_small_icon')) . '</strong><br />'; 374da933f89SLORTET if ($currentSmall) { 375b603bbe1SLORTET $this->renderCurrentIconPreview($currentSmall, $defaultTarget, $actionPage, $previewSize); 376da933f89SLORTET } else { 377da933f89SLORTET echo '<small>' . hsc($this->getLang('current_icon_none')) . '</small>'; 378da933f89SLORTET } 379da933f89SLORTET echo '</div>'; 380da933f89SLORTET echo '</div>'; 381da933f89SLORTET 382b603bbe1SLORTET echo '<form action="' . wl($actionPage) . '" method="post" enctype="multipart/form-data"' 383b603bbe1SLORTET . ' class="pagesicon-upload-form"' 384b603bbe1SLORTET . ' data-page-name="' . hsc(noNS($defaultTarget)) . '"' 385b603bbe1SLORTET . ' data-big-templates="' . hsc(json_encode($helper ? $helper->getVariantTemplates('big') : [])) . '"' 386b603bbe1SLORTET . ' data-small-templates="' . hsc(json_encode($helper ? $helper->getVariantTemplates('small') : [])) . '">'; 387da933f89SLORTET formSecurityToken(); 388da933f89SLORTET echo '<input type="hidden" name="do" value="pagesicon" />'; 389da933f89SLORTET echo '<input type="hidden" name="pagesicon_upload_submit" value="1" />'; 390da933f89SLORTET 391da933f89SLORTET echo '<div class="table"><table class="inline">'; 392da933f89SLORTET echo '<tr>'; 393da933f89SLORTET echo '<td class="label"><label for="pagesicon_icon_variant">' . hsc($this->getLang('icon_variant')) . '</label></td>'; 394da933f89SLORTET echo '<td>'; 395da933f89SLORTET echo '<select id="pagesicon_icon_variant" name="icon_variant" class="edit">'; 396da933f89SLORTET echo '<option value="big"' . ($defaultVariant === 'big' ? ' selected="selected"' : '') . '>' . hsc($this->getLang('icon_variant_big')) . '</option>'; 397da933f89SLORTET echo '<option value="small"' . ($defaultVariant === 'small' ? ' selected="selected"' : '') . '>' . hsc($this->getLang('icon_variant_small')) . '</option>'; 398da933f89SLORTET echo '</select>'; 399da933f89SLORTET echo '</td>'; 400da933f89SLORTET echo '</tr>'; 401da933f89SLORTET 402da933f89SLORTET echo '<tr>'; 403da933f89SLORTET echo '<td class="label"><label for="pagesicon_file">' . hsc($this->getLang('file')) . '</label></td>'; 404da933f89SLORTET echo '<td><input type="file" id="pagesicon_file" name="pagesicon_file" class="edit" required /></td>'; 405da933f89SLORTET echo '</tr>'; 406da933f89SLORTET 407da933f89SLORTET echo '<tr>'; 408da933f89SLORTET echo '<td class="label"><label for="pagesicon_icon_filename">' . hsc($this->getLang('icon_filename')) . '</label></td>'; 409da933f89SLORTET echo '<td>'; 410b603bbe1SLORTET if ($currentChoices) { 411da933f89SLORTET echo '<select id="pagesicon_icon_filename" name="icon_filename" class="edit">'; 412da933f89SLORTET foreach ($currentChoices as $value => $label) { 413da933f89SLORTET $selected = $value === $selectedBase ? ' selected="selected"' : ''; 414da933f89SLORTET echo '<option value="' . hsc($value) . '"' . $selected . '>' . hsc($label) . '</option>'; 415da933f89SLORTET } 416da933f89SLORTET echo '</select>'; 417da933f89SLORTET echo '<br /><small>' . $filenameHelp . '</small>'; 418b603bbe1SLORTET } else { 419b603bbe1SLORTET echo '<span class="error">' . hsc($this->getLang('error_no_filename_choices')) . '</span>'; 420b603bbe1SLORTET } 421da933f89SLORTET echo '</td>'; 422da933f89SLORTET echo '</tr>'; 423da933f89SLORTET echo '</table></div>'; 424da933f89SLORTET 425da933f89SLORTET echo '<p><button type="submit" class="button">' . hsc($this->getLang('upload_button')) . '</button></p>'; 426da933f89SLORTET echo '</form>'; 427da933f89SLORTET } 428da933f89SLORTET 429da933f89SLORTET public function _displaypageicon(Doku_Event &$event, $param) { 430da933f89SLORTET global $ACT, $ID; 431da933f89SLORTET 432da933f89SLORTET if($ACT !== 'show') return; 433da933f89SLORTET if(!(bool)$this->getConf('show_on_top')) return; 434da933f89SLORTET 435da933f89SLORTET $pageID = noNS($ID); 436b603bbe1SLORTET if($this->isLayoutIncludePage($pageID)) return; 437da933f89SLORTET 438da933f89SLORTET $namespace = getNS($ID); 439da933f89SLORTET $pageID = noNS((string)$ID); 440da933f89SLORTET /** @var helper_plugin_pagesicon|null $helper */ 441da933f89SLORTET $helper = plugin_load('helper', 'pagesicon'); 442da933f89SLORTET if(!$helper) return; 443da933f89SLORTET $sizeMode = $this->getIconSize() > 35 ? 'bigorsmall' : 'smallorbig'; 444b603bbe1SLORTET $logoMediaID = $helper->getPageIconId($namespace, $pageID, $sizeMode); 445da933f89SLORTET if(!$logoMediaID) return; 446da933f89SLORTET if($this->hasIconAlready($event->data, $logoMediaID)) return; 447da933f89SLORTET 448da933f89SLORTET $size = $this->getIconSize(); 449b603bbe1SLORTET $src = $helper->getPageIconUrl($namespace, $pageID, $sizeMode, ['w' => $size]); 450da933f89SLORTET if(!$src) return; 451da933f89SLORTET $iconHtml = '<img src="' . $src . '" class="media pagesicon-image" loading="lazy" alt="" width="' . $size . '" />'; 452da933f89SLORTET if ((bool)$this->getConf('show_as_favicon')) { 453b603bbe1SLORTET $favicon = $helper->getPageIconUrl($namespace, $pageID, $sizeMode, ['w' => 32]); 454da933f89SLORTET $this->injectFaviconRuntimeScript($event->data, $favicon); 455da933f89SLORTET } 456da933f89SLORTET 457da933f89SLORTET $inlineIcon = '<span class="pagesicon-injected pagesicon-injected-inline">' . $iconHtml . '</span> '; 458da933f89SLORTET $updated = preg_replace('/<h1\b([^>]*)>/i', '<h1$1>' . $inlineIcon, $event->data, 1, $count); 459da933f89SLORTET if ($count > 0 && $updated !== null) { 460da933f89SLORTET $event->data = $updated; 461da933f89SLORTET return; 462da933f89SLORTET } 463da933f89SLORTET 464da933f89SLORTET // Fallback: no H1 found, keep old behavior 465da933f89SLORTET $event->data = '<div class="pagesicon-injected">' . $iconHtml . '</div>' . "\n" . $event->data; 466da933f89SLORTET } 467da933f89SLORTET 468b603bbe1SLORTET public function handleAction(Doku_Event $event): void { 469da933f89SLORTET if ($event->data !== 'pagesicon') return; 470da933f89SLORTET $event->preventDefault(); 471da933f89SLORTET } 472da933f89SLORTET 473b603bbe1SLORTET public function renderAction(Doku_Event $event): void { 474da933f89SLORTET global $ACT; 475da933f89SLORTET if ($ACT !== 'pagesicon') return; 476da933f89SLORTET 477da933f89SLORTET $this->handleDeletePost(); 478da933f89SLORTET $this->handleUploadPost(); 479da933f89SLORTET $this->renderUploadForm(); 480da933f89SLORTET 481da933f89SLORTET $event->preventDefault(); 482da933f89SLORTET $event->stopPropagation(); 483da933f89SLORTET } 484da933f89SLORTET} 485