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