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