Merge pull request #1558 from ArthurHoaro/fix/plugins-base-path

Fix plugin base path in core plugins
This commit is contained in:
ArthurHoaro 2020-09-25 10:59:40 +02:00 committed by GitHub
commit 85b972baf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 61 additions and 27 deletions

View file

@ -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!

View file

@ -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;
}

View file

@ -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 = '<span><a href="?%s#isso-thread">';
$button = '<span><a href="'. ($data['_BASE_PATH_'] ?? '') . '/shaare/%s#isso-thread">';
// For the default theme we use a FontAwesome icon which is better than an image
if ($conf->get('resource.theme') === 'default') {
$button .= '<i class="linklist-plugin-icon fa fa-comment"></i>';

View file

@ -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;

View file

@ -1,10 +1,12 @@
<?php
namespace Shaarli\Plugin\Archiveorg;
/**
* PluginArchiveorgTest.php
*/
use PHPUnit\Framework\TestCase;
use Shaarli\Plugin\PluginManager;
require_once 'plugins/archiveorg/archiveorg.php';
@ -13,20 +15,35 @@ require_once 'plugins/archiveorg/archiveorg.php';
* Class PluginArchiveorgTest
* Unit test for the archiveorg plugin
*/
class PluginArchiveorgTest extends \PHPUnit\Framework\TestCase
class PluginArchiveorgTest extends TestCase
{
protected $savedScriptName;
/**
* Reset plugin path
*/
public function setUp()
public function setUp(): void
{
PluginManager::$PLUGINS_PATH = 'plugins';
// plugins manipulate global vars
$_SERVER['SERVER_PORT'] = '80';
$_SERVER['SERVER_NAME'] = 'shaarli.shaarli';
$this->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,

View file

@ -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('<a href="?'. $short1 .'#isso-thread">', $processed['links'][0]['link_plugin'][0]);
$this->assertContains('<a href="?'. $short2 .'#isso-thread">', $processed['links'][1]['link_plugin'][0]);
$this->assertContains('<a href="/shaare/'. $short1 .'#isso-thread">', $processed['links'][0]['link_plugin'][0]);
$this->assertContains('<a href="/shaare/'. $short2 .'#isso-thread">', $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('<a href="?'. $short1 .'#isso-thread">', $processed['links'][0]['link_plugin'][0]);
$this->assertContains('<a href="/shaare/'. $short1 .'#isso-thread">', $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('');

View file

@ -32,6 +32,6 @@
</form>
</div>
{if="$previous_page_url"} <a href="{$previous_page_url}" class="paging_older">&#x25C4;Older</a> {/if}
<div class="paging_current">page {$page_current} / {$page_max} </div>
{if="$page_max>1"}<div class="paging_current">page {$page_current} / {$page_max} </div>{/if}
{if="$next_page_url"} <a href="{$next_page_url}" class="paging_newer">Newer&#x25BA;</a> {/if}
</div>