From 76fe68d924d424283d0a1784c5f5e7582dda3a00 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 22 Sep 2020 12:44:08 +0200 Subject: [PATCH] Fix plugin base path in core plugins Also fix note check in archiveorg plugin, and regression on vintage template. Documentation regarding relative path has been added. Fixes #1548 --- doc/md/dev/Plugin-system.md | 14 ++++++++++ plugins/archiveorg/archiveorg.php | 6 +++-- plugins/isso/isso.php | 2 +- plugins/qrcode/qrcode.php | 4 +-- tests/plugins/PluginArchiveorgTest.php | 37 +++++++++++++++++++------- tests/plugins/PluginIssoTest.php | 23 ++++++++-------- tpl/vintage/linklist.paging.html | 2 +- 7 files changed, 61 insertions(+), 27 deletions(-) diff --git a/doc/md/dev/Plugin-system.md b/doc/md/dev/Plugin-system.md index a87bd0cf..c29774de 100644 --- a/doc/md/dev/Plugin-system.md +++ b/doc/md/dev/Plugin-system.md @@ -139,6 +139,20 @@ Each file contain two keys: > Note: In PHP, `parse_ini_file()` seems to want strings to be between by quotes `"` in the ini file. +### Understanding relative paths + +Because Shaarli is a self-hosted tool, an instance can either be installed at the root directory, or under a subfolder. +This means that you can *never* use absolute paths (eg `/plugins/mything/file.png`). + +If a file needs to be included in server end, use simple relative path: +`PluginManager::$PLUGINS_PATH . '/mything/template.html'`. + +If it needs to be included in front end side (e.g. an image), +the relative path must be prefixed with special data `_BASE_PATH_`: +`($data['_BASE_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH . '/mything/picture.png`. + +Note that special placeholders for CSS and JS files (respectively `css_files` and `js_files`) are already prefixed +with the base path in template files. ### It's not working! diff --git a/plugins/archiveorg/archiveorg.php b/plugins/archiveorg/archiveorg.php index f26e6129..922b5966 100644 --- a/plugins/archiveorg/archiveorg.php +++ b/plugins/archiveorg/archiveorg.php @@ -20,10 +20,12 @@ function hook_archiveorg_render_linklist($data) $path = ($data['_BASE_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH; foreach ($data['links'] as &$value) { - if ($value['private'] && preg_match('/^\?[a-zA-Z0-9-_@]{6}($|&|#)/', $value['real_url'])) { + $isNote = startsWith($value['real_url'], '/shaare/'); + if ($value['private'] && $isNote) { continue; } - $archive = sprintf($archive_html, $value['url'], $path, t('View on archive.org')); + $url = $isNote ? rtrim(index_url($_SERVER), '/') . $value['real_url'] : $value['real_url']; + $archive = sprintf($archive_html, $url, $path, t('View on archive.org')); $value['link_plugin'][] = $archive; } diff --git a/plugins/isso/isso.php b/plugins/isso/isso.php index 16edd9a6..79e7380b 100644 --- a/plugins/isso/isso.php +++ b/plugins/isso/isso.php @@ -49,7 +49,7 @@ function hook_isso_render_linklist($data, $conf) $isso = sprintf($issoHtml, $issoUrl, $issoUrl, $link['id'], $link['id']); $data['plugin_end_zone'][] = $isso; } else { - $button = ''; + $button = ''; // For the default theme we use a FontAwesome icon which is better than an image if ($conf->get('resource.theme') === 'default') { $button .= ''; diff --git a/plugins/qrcode/qrcode.php b/plugins/qrcode/qrcode.php index 56ae47b3..95499e39 100644 --- a/plugins/qrcode/qrcode.php +++ b/plugins/qrcode/qrcode.php @@ -42,7 +42,7 @@ function hook_qrcode_render_linklist($data) function hook_qrcode_render_footer($data) { if ($data['_PAGE_'] == TemplatePage::LINKLIST) { - $data['js_files'][] = ($data['_BASE_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH . '/qrcode/shaarli-qrcode.js'; + $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/qrcode/shaarli-qrcode.js'; } return $data; @@ -58,7 +58,7 @@ function hook_qrcode_render_footer($data) function hook_qrcode_render_includes($data) { if ($data['_PAGE_'] == TemplatePage::LINKLIST) { - $data['css_files'][] = ($data['_BASE_PATH_'] ?? '') . '/' . PluginManager::$PLUGINS_PATH . '/qrcode/qrcode.css'; + $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/qrcode/qrcode.css'; } return $data; diff --git a/tests/plugins/PluginArchiveorgTest.php b/tests/plugins/PluginArchiveorgTest.php index b9a67adb..9c19752c 100644 --- a/tests/plugins/PluginArchiveorgTest.php +++ b/tests/plugins/PluginArchiveorgTest.php @@ -1,10 +1,12 @@ savedScriptName = $_SERVER['SCRIPT_NAME'] ?? null; + $_SERVER['SCRIPT_NAME'] = '/index.php'; + } + + public function tearDown(): void + { + unset($_SERVER['SERVER_PORT']); + unset($_SERVER['SERVER_NAME']); + $_SERVER['SCRIPT_NAME'] = $this->savedScriptName; } /** * Test render_linklist hook on external bookmarks. */ - public function testArchiveorgLinklistOnExternalLinks() + public function testArchiveorgLinklistOnExternalLinks(): void { $str = 'http://randomstr.com/test'; @@ -56,16 +73,16 @@ class PluginArchiveorgTest extends \PHPUnit\Framework\TestCase /** * Test render_linklist hook on internal bookmarks. */ - public function testArchiveorgLinklistOnInternalLinks() + public function testArchiveorgLinklistOnInternalLinks(): void { - $internalLink1 = 'http://shaarli.shaarli/?qvMAqg'; - $internalLinkRealURL1 = '?qvMAqg'; + $internalLink1 = 'http://shaarli.shaarli/shaare/qvMAqg'; + $internalLinkRealURL1 = '/shaare/qvMAqg'; - $internalLink2 = 'http://shaarli.shaarli/?2_7zww'; - $internalLinkRealURL2 = '?2_7zww'; + $internalLink2 = 'http://shaarli.shaarli/shaare/2_7zww'; + $internalLinkRealURL2 = '/shaare/2_7zww'; - $internalLink3 = 'http://shaarli.shaarli/?z7u-_Q'; - $internalLinkRealURL3 = '?z7u-_Q'; + $internalLink3 = 'http://shaarli.shaarli/shaare/z7u-_Q'; + $internalLinkRealURL3 = '/shaare/z7u-_Q'; $data = array( 'title' => $internalLink1, diff --git a/tests/plugins/PluginIssoTest.php b/tests/plugins/PluginIssoTest.php index 99477205..2bbb93d2 100644 --- a/tests/plugins/PluginIssoTest.php +++ b/tests/plugins/PluginIssoTest.php @@ -2,6 +2,7 @@ namespace Shaarli\Plugin\Isso; use DateTime; +use PHPUnit\Framework\TestCase; use Shaarli\Bookmark\Bookmark; use Shaarli\Config\ConfigManager; use Shaarli\Plugin\PluginManager; @@ -13,12 +14,12 @@ require_once 'plugins/isso/isso.php'; * * Test the Isso plugin (comment system). */ -class PluginIssoTest extends \PHPUnit\Framework\TestCase +class PluginIssoTest extends TestCase { /** * Reset plugin path */ - public function setUp() + public function setUp(): void { PluginManager::$PLUGINS_PATH = 'plugins'; } @@ -26,7 +27,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase /** * Test Isso init without errors. */ - public function testIssoInitNoError() + public function testIssoInitNoError(): void { $conf = new ConfigManager(''); $conf->set('plugins.ISSO_SERVER', 'value'); @@ -37,7 +38,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase /** * Test Isso init with errors. */ - public function testIssoInitError() + public function testIssoInitError(): void { $conf = new ConfigManager(''); $errors = isso_init($conf); @@ -47,7 +48,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase /** * Test render_linklist hook with valid settings to display the comment form. */ - public function testIssoDisplayed() + public function testIssoDisplayed(): void { $conf = new ConfigManager(''); $conf->set('plugins.ISSO_SERVER', 'value'); @@ -87,7 +88,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase /** * Test isso plugin when multiple bookmarks are displayed (shouldn't be displayed). */ - public function testIssoMultipleLinks() + public function testIssoMultipleLinks(): void { $conf = new ConfigManager(''); $conf->set('plugins.ISSO_SERVER', 'value'); @@ -115,14 +116,14 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase $processed = hook_isso_render_linklist($data, $conf); // link_plugin should be added for the icon - $this->assertContains('', $processed['links'][0]['link_plugin'][0]); - $this->assertContains('', $processed['links'][1]['link_plugin'][0]); + $this->assertContains('', $processed['links'][0]['link_plugin'][0]); + $this->assertContains('', $processed['links'][1]['link_plugin'][0]); } /** * Test isso plugin when using search (shouldn't be displayed). */ - public function testIssoNotDisplayedWhenSearch() + public function testIssoNotDisplayedWhenSearch(): void { $conf = new ConfigManager(''); $conf->set('plugins.ISSO_SERVER', 'value'); @@ -145,13 +146,13 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase $processed = hook_isso_render_linklist($data, $conf); // link_plugin should be added for the icon - $this->assertContains('', $processed['links'][0]['link_plugin'][0]); + $this->assertContains('', $processed['links'][0]['link_plugin'][0]); } /** * Test isso plugin without server configuration (shouldn't be displayed). */ - public function testIssoWithoutConf() + public function testIssoWithoutConf(): void { $data = 'abc'; $conf = new ConfigManager(''); diff --git a/tpl/vintage/linklist.paging.html b/tpl/vintage/linklist.paging.html index b9396df6..79daf16c 100644 --- a/tpl/vintage/linklist.paging.html +++ b/tpl/vintage/linklist.paging.html @@ -32,6 +32,6 @@ {if="$previous_page_url"} ◄Older {/if} -
page {$page_current} / {$page_max}
+ {if="$page_max>1"}
page {$page_current} / {$page_max}
{/if} {if="$next_page_url"} Newer► {/if} -- 2.41.0