Skip to content

Commit 944e9ba

Browse files
committed
use a dispatcher to access static image files
This makes it possible to replace default images in an update safe way. It also addresses the issue raised in dokuwiki/docker#16 A .htaccess rewrite catches any direct accesses that might come in from plugins.
1 parent 6de67ec commit 944e9ba

35 files changed

+207
-53
lines changed

inc/ChangeLog/RevisionInfo.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function showFileIcon()
113113
return media_printicon($id);
114114
} elseif ($this->val('mode') == self::MODE_PAGE) {
115115
// page revision
116-
return '<img class="icon" src="' . DOKU_BASE . 'lib/images/fileicons/file.png" alt="' . $id . '" />';
116+
return '<img class="icon" src="' . DOKU_BASE . 'lib/exe/image.php/fileicons/file.png" alt="' . $id . '" />';
117117
}
118118
}
119119

@@ -286,11 +286,11 @@ public function showIconCompareWithPrevious()
286286

287287
if ($href) {
288288
return '<a href="' . $href . '" class="diff_link">'
289-
. '<img src="' . DOKU_BASE . 'lib/images/diff.png" width="15" height="11"'
289+
. '<img src="' . DOKU_BASE . 'lib/exe/image.php/diff.png" width="15" height="11"'
290290
. ' title="' . $lang['diff'] . '" alt="' . $lang['diff'] . '" />'
291291
. '</a>';
292292
} else {
293-
return '<img src="' . DOKU_BASE . 'lib/images/blank.gif" width="15" height="11" alt="" />';
293+
return '<img src="' . DOKU_BASE . 'lib/exe/image.php/blank.gif" width="15" height="11" alt="" />';
294294
}
295295
}
296296

@@ -322,11 +322,11 @@ public function showIconCompareWithCurrent()
322322

323323
if ($href) {
324324
return '<a href="' . $href . '" class="diff_link">'
325-
. '<img src="' . DOKU_BASE . 'lib/images/diff.png" width="15" height="11"'
325+
. '<img src="' . DOKU_BASE . 'lib/exe/image.php/diff.png" width="15" height="11"'
326326
. ' title="' . $lang['diff'] . '" alt="' . $lang['diff'] . '" />'
327327
. '</a>';
328328
} else {
329-
return '<img src="' . DOKU_BASE . 'lib/images/blank.gif" width="15" height="11" alt="" />';
329+
return '<img src="' . DOKU_BASE . 'lib/exe/image.php/blank.gif" width="15" height="11" alt="" />';
330330
}
331331
}
332332

@@ -354,7 +354,7 @@ public function showIconRevisions()
354354
$href = wl($id, ['do' => 'revisions'], false, '&');
355355
}
356356
return '<a href="' . $href . '" class="revisions_link">'
357-
. '<img src="' . DOKU_BASE . 'lib/images/history.png" width="12" height="14"'
357+
. '<img src="' . DOKU_BASE . 'lib/exe/image.php/history.png" width="12" height="14"'
358358
. ' title="' . $lang['btn_revs'] . '" alt="' . $lang['btn_revs'] . '" />'
359359
. '</a>';
360360
}

inc/File/StaticImage.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace dokuwiki\File;
4+
5+
/**
6+
* Access a static file from lib/images with optinal override in conf/images
7+
*/
8+
class StaticImage
9+
{
10+
11+
protected string $path;
12+
13+
/**
14+
* @param string $path the path relative to lib/images
15+
*/
16+
public function __construct(string $path)
17+
{
18+
require_once(DOKU_INC . 'inc/fetch.functions.php'); // late load when needed
19+
20+
$path = preg_replace('/\.\.+/', '.', $path);
21+
$path = preg_replace('/\/\/+/', '/', $path);
22+
$path = trim($path, '/');
23+
$this->path = $path;
24+
}
25+
26+
/**
27+
* Static convenience method to get the real path to an image
28+
*
29+
* @param string $path
30+
* @return string
31+
*/
32+
public static function path(string $path): string
33+
{
34+
return (new self($path))->getRealPath();
35+
}
36+
37+
/**
38+
* Static convenience method to get the URL to an image
39+
*
40+
* @param string $path
41+
* @return string
42+
*/
43+
public static function url(string $path): string
44+
{
45+
return (new self($path))->getURL();
46+
}
47+
48+
/**
49+
* @return string the mime type of the image
50+
*/
51+
public function getMimeType()
52+
{
53+
[/*ext*/, $mime] = mimetype($this->path, false);
54+
if ($mime === false) throw new \RuntimeException('Unknown mime type');
55+
return $mime;
56+
}
57+
58+
/**
59+
* @return string the real path to the image
60+
*/
61+
public function getRealPath()
62+
{
63+
// overridden image
64+
$path = DOKU_CONF . 'images/' . $this->path;
65+
if (file_exists($path)) return $path;
66+
67+
// default image
68+
$path = DOKU_INC . 'lib/images/' . $this->path;
69+
if (file_exists($path)) return $path;
70+
71+
throw new \RuntimeException('Image not found');
72+
}
73+
74+
/**
75+
* @return string the URL to the image
76+
*/
77+
public function getURL()
78+
{
79+
return DOKU_BASE . 'lib/exe/image.php/' . $this->path;
80+
}
81+
82+
/**
83+
* Serve the image to the client
84+
*/
85+
public function serve()
86+
{
87+
$path = $this->getRealPath();
88+
$mime = $this->getMimeType();
89+
sendFile($path, $mime, false, -1, true);
90+
}
91+
}

inc/Menu/Item/AbstractItem.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class AbstractItem
79
*
@@ -64,6 +66,7 @@ public function __construct()
6466
$this->id = $ID;
6567
$this->type = $this->getType();
6668
$this->params['do'] = $this->type;
69+
$this->svg = StaticImage::path('menu/00-default_checkbox-blank-circle-outline.svg');
6770

6871
if (!actionOK($this->type)) throw new \RuntimeException("action disabled: {$this->type}");
6972
}

inc/Menu/Item/Admin.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Admin
79
*
@@ -14,7 +16,7 @@ public function __construct()
1416
{
1517
parent::__construct();
1618

17-
$this->svg = DOKU_INC . 'lib/images/menu/settings.svg';
19+
$this->svg = StaticImage::path('menu/settings.svg');
1820
}
1921

2022
/** @inheritdoc */

inc/Menu/Item/Back.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Back
79
*
@@ -24,6 +26,6 @@ public function __construct()
2426
$this->id = $parent;
2527
$this->params = ['do' => ''];
2628
$this->accesskey = 'b';
27-
$this->svg = DOKU_INC . 'lib/images/menu/12-back_arrow-left.svg';
29+
$this->svg = StaticImage::path('menu/12-back_arrow-left.svg');
2830
}
2931
}

inc/Menu/Item/Backlink.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Backlink
79
*
@@ -13,6 +15,6 @@ class Backlink extends AbstractItem
1315
public function __construct()
1416
{
1517
parent::__construct();
16-
$this->svg = DOKU_INC . 'lib/images/menu/08-backlink_link-variant.svg';
18+
$this->svg = StaticImage::path('menu/08-backlink_link-variant.svg');
1719
}
1820
}

inc/Menu/Item/Edit.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Edit
79
*
@@ -61,7 +63,7 @@ protected function setIcon()
6163
'source' => '05-source_file-xml.svg'
6264
];
6365
if (isset($icons[$this->type])) {
64-
$this->svg = DOKU_INC . 'lib/images/menu/' . $icons[$this->type];
66+
$this->svg = StaticImage::path('menu/' . $icons[$this->type]);
6567
}
6668
}
6769
}

inc/Menu/Item/ImgBackto.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class ImgBackto
79
*
@@ -15,7 +17,7 @@ public function __construct()
1517
global $ID;
1618
parent::__construct();
1719

18-
$this->svg = DOKU_INC . 'lib/images/menu/12-back_arrow-left.svg';
20+
$this->svg = StaticImage::path('menu/12-back_arrow-left.svg');
1921
$this->type = 'img_backto';
2022
$this->params = [];
2123
$this->accesskey = 'b';

inc/Menu/Item/Index.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Index
79
*
@@ -17,7 +19,7 @@ public function __construct()
1719
parent::__construct();
1820

1921
$this->accesskey = 'x';
20-
$this->svg = DOKU_INC . 'lib/images/menu/file-tree.svg';
22+
$this->svg = StaticImage::path('menu/file-tree.svg');
2123

2224
// allow searchbots to get to the sitemap from the homepage (when dokuwiki isn't providing a sitemap.xml)
2325
if ($conf['start'] == $ID && !$conf['sitemap']) {

inc/Menu/Item/Login.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Login
79
*
@@ -15,15 +17,15 @@ public function __construct()
1517
global $INPUT;
1618
parent::__construct();
1719

18-
$this->svg = DOKU_INC . 'lib/images/menu/login.svg';
20+
$this->svg = StaticImage::path('menu/login.svg');
1921
$this->params['sectok'] = getSecurityToken();
2022
if ($INPUT->server->has('REMOTE_USER')) {
2123
if (!actionOK('logout')) {
2224
throw new \RuntimeException("logout disabled");
2325
}
2426
$this->params['do'] = 'logout';
2527
$this->type = 'logout';
26-
$this->svg = DOKU_INC . 'lib/images/menu/logout.svg';
28+
$this->svg = StaticImage::path('menu/logout.svg');
2729
}
2830
}
2931
}

inc/Menu/Item/Media.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Media
79
*
@@ -15,7 +17,7 @@ public function __construct()
1517
global $ID;
1618
parent::__construct();
1719

18-
$this->svg = DOKU_INC . 'lib/images/menu/folder-multiple-image.svg';
20+
$this->svg = StaticImage::path('menu/folder-multiple-image.svg');
1921
$this->params['ns'] = getNS($ID);
2022
}
2123
}

inc/Menu/Item/MediaManager.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class MediaManager
79
*
@@ -21,7 +23,7 @@ public function __construct()
2123
throw new \RuntimeException("media manager link only with upload permissions");
2224
}
2325

24-
$this->svg = DOKU_INC . 'lib/images/menu/11-mediamanager_folder-image.svg';
26+
$this->svg = StaticImage::path('menu/11-mediamanager_folder-image.svg');
2527
$this->type = 'mediaManager';
2628
$this->params = ['ns' => $imgNS, 'image' => $IMG, 'do' => 'media'];
2729
}

inc/Menu/Item/Profile.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Profile
79
*
@@ -19,6 +21,6 @@ public function __construct()
1921
throw new \RuntimeException("profile is only for logged in users");
2022
}
2123

22-
$this->svg = DOKU_INC . 'lib/images/menu/account-card-details.svg';
24+
$this->svg = StaticImage::path('menu/account-card-details.svg');
2325
}
2426
}

inc/Menu/Item/Recent.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Recent
79
*
@@ -15,6 +17,6 @@ public function __construct()
1517
parent::__construct();
1618

1719
$this->accesskey = 'r';
18-
$this->svg = DOKU_INC . 'lib/images/menu/calendar-clock.svg';
20+
$this->svg = StaticImage::path('menu/calendar-clock.svg');
1921
}
2022
}

inc/Menu/Item/Register.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Register
79
*
@@ -19,6 +21,6 @@ public function __construct()
1921
throw new \RuntimeException("no register when already logged in");
2022
}
2123

22-
$this->svg = DOKU_INC . 'lib/images/menu/account-plus.svg';
24+
$this->svg = StaticImage::path('menu/account-plus.svg');
2325
}
2426
}

inc/Menu/Item/Resendpwd.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace dokuwiki\Menu\Item;
44

5+
use dokuwiki\File\StaticImage;
6+
57
/**
68
* Class Resendpwd
79
*
@@ -19,6 +21,6 @@ public function __construct()
1921
throw new \RuntimeException("no resendpwd when already logged in");
2022
}
2123

22-
$this->svg = DOKU_INC . 'lib/images/menu/lock-reset.svg';
24+
$this->svg = StaticImage::path('menu/lock-reset.svg');
2325
}
2426
}

0 commit comments

Comments
 (0)