--- /dev/null
+<?php
+
+namespace Shaarli;
+
+
+/**
+ * URL-safe Base64 operations
+ *
+ * @see https://en.wikipedia.org/wiki/Base64#URL_applications
+ */
+class Base64Url
+{
+ /**
+ * Base64Url-encodes data
+ *
+ * @param string $data Data to encode
+ *
+ * @return string Base64Url-encoded data
+ */
+ public static function encode($data) {
+ return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
+ }
+
+ /**
+ * Decodes Base64Url-encoded data
+ *
+ * @param string $data Data to decode
+ *
+ * @return string Decoded data
+ */
+ public static function decode($data) {
+ return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
+ }
+}
// Directory containing page caches
private $cacheDir;
- // Full URL of the page to cache -typically the value returned by pageUrl()
- private $url;
-
// Should this URL be cached (boolean)?
private $shouldBeCached;
{
// TODO: check write access to the cache directory
$this->cacheDir = $cacheDir;
- $this->url = $url;
$this->filename = $this->cacheDir.'/'.sha1($url).'.cache';
$this->shouldBeCached = $shouldBeCached;
}
*
* @param ConfigManager $conf Configuration Manager instance (reference).
*/
- function __construct(&$conf)
+ public function __construct(&$conf)
{
$this->tpl = false;
$this->conf = $conf;
<?php
-
namespace Shaarli\Api;
+use Shaarli\Base64Url;
use Shaarli\Api\Exceptions\ApiAuthorizationException;
/**
- * Class ApiUtils
- *
- * Utility functions for the API.
+ * REST API utilities
*/
class ApiUtils
{
throw new ApiAuthorizationException('Malformed JWT token');
}
- $genSign = hash_hmac('sha512', $parts[0] .'.'. $parts[1], $secret);
+ $genSign = Base64Url::encode(hash_hmac('sha512', $parts[0] .'.'. $parts[1], $secret, true));
if ($parts[2] != $genSign) {
throw new ApiAuthorizationException('Invalid JWT signature');
}
- $header = json_decode(base64_decode($parts[0]));
+ $header = json_decode(Base64Url::decode($parts[0]));
if ($header === null) {
throw new ApiAuthorizationException('Invalid JWT header');
}
- $payload = json_decode(base64_decode($parts[1]));
+ $payload = json_decode(Base64Url::decode($parts[1]));
if ($payload === null) {
throw new ApiAuthorizationException('Invalid JWT payload');
}
*
* @return array All configuration in an array.
*/
- function read($filepath);
+ public function read($filepath);
/**
* Write configuration.
* @param string $filepath Config file absolute path.
* @param array $conf All configuration in an array.
*/
- function write($filepath, $conf);
+ public function write($filepath, $conf);
/**
* Get config file extension according to config type.
*
* @return string Config file extension.
*/
- function getExtension();
+ public function getExtension();
}
/**
* @inheritdoc
*/
- function read($filepath)
+ public function read($filepath)
{
if (! is_readable($filepath)) {
return array();
/**
* @inheritdoc
*/
- function write($filepath, $conf)
+ public function write($filepath, $conf)
{
// JSON_PRETTY_PRINT is available from PHP 5.4.
$print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
/**
* @inheritdoc
*/
- function getExtension()
+ public function getExtension()
{
return '.json.php';
}
/**
* @inheritdoc
*/
- function read($filepath)
+ public function read($filepath)
{
if (! file_exists($filepath) || ! is_readable($filepath)) {
return array();
/**
* @inheritdoc
*/
- function write($filepath, $conf)
+ public function write($filepath, $conf)
{
$configStr = '<?php '. PHP_EOL;
foreach (self::$ROOT_KEYS as $key) {
/**
* @inheritdoc
*/
- function getExtension()
+ public function getExtension()
{
return '.php';
}
alias /var/www/shaarli/images/favicon.ico;
}
+ location / {
+ # Slim - rewrite URLs
+ try_files $uri /index.php$is_args$args;
+ }
+
location ~ (index)\.php$ {
+ # Slim - split URL path into (script_filename, path_info)
+ try_files $uri =404;
+ fastcgi_split_path_info ^(.+\.php)(/.+)$;
+
# filter and proxy PHP requests to PHP-FPM
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
alias /var/www/shaarli/images/favicon.ico;
}
+ location / {
+ # Slim - rewrite URLs
+ try_files $uri /index.php$is_args$args;
+ }
+
location ~ (index)\.php$ {
+ # Slim - split URL path into (script_filename, path_info)
+ try_files $uri =404;
+ fastcgi_split_path_info ^(.+\.php)(/.+)$;
+
# filter and proxy PHP requests to PHP-FPM
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
alias /var/www/shaarli/images/favicon.ico;
}
+ location / {
+ # Slim - rewrite URLs
+ try_files $uri /index.php$is_args$args;
+ }
+
location ~ (index)\.php$ {
+ # Slim - split URL path into (script_filename, path_info)
+ try_files $uri =404;
+ fastcgi_split_path_info ^(.+\.php)(/.+)$;
+
# filter and proxy PHP requests to PHP-FPM
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
$tpl->assign('links', $links);
$tpl->assign('rssdate', escape($dayDate->format(DateTime::RSS)));
$tpl->assign('hide_timestamps', $conf->get('privacy.hide_timestamps', false));
- $html = $tpl->draw('dailyrss', $return_string=true);
+ $html = $tpl->draw('dailyrss', true);
echo $html . PHP_EOL;
}
*/
private $apiVersion;
- function __construct($instance, $version)
+ public function __construct($instance, $version)
{
if ($this->isVersionAllowed($version)) {
$this->apiVersion = self::$wallabagVersions[$version];
/**
* Test add trailing slash.
*/
- function testAddTrailingSlash()
+ public function testAddTrailingSlash()
{
$strOn = 'http://randomstr.com/test/';
$strOff = 'http://randomstr.com/test';
/**
* Test valid HTTP url.
*/
- function testUrlIsHttp()
+ public function testUrlIsHttp()
{
$url = new Url(self::$baseUrl);
$this->assertTrue($url->isHttp());
/**
* Test non HTTP url.
*/
- function testUrlIsNotHttp()
+ public function testUrlIsNotHttp()
{
$url = new Url('ftp://save.tld/mysave');
$this->assertFalse($url->isHttp());
/**
* Test International Domain Name to ASCII conversion
*/
- function testIdnToAscii()
+ public function testIdnToAscii()
{
$ind = 'http://www.académie-française.fr/';
$expected = 'http://www.xn--acadmie-franaise-npb1a.fr/';
namespace Shaarli\Api;
+use Shaarli\Base64Url;
+
+
/**
* Class ApiUtilsTest
*/
*/
public static function generateValidJwtToken($secret)
{
- $header = base64_encode('{
+ $header = Base64Url::encode('{
"typ": "JWT",
"alg": "HS512"
}');
- $payload = base64_encode('{
+ $payload = Base64Url::encode('{
"iat": '. time() .'
}');
- $signature = hash_hmac('sha512', $header .'.'. $payload , $secret);
+ $signature = Base64Url::encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
return $header .'.'. $payload .'.'. $signature;
}
*/
public static function generateCustomJwtToken($header, $payload, $secret)
{
- $header = base64_encode($header);
- $payload = base64_encode($payload);
- $signature = hash_hmac('sha512', $header . '.' . $payload, $secret);
+ $header = Base64Url::encode($header);
+ $payload = Base64Url::encode($payload);
+ $signature = Base64Url::encode(hash_hmac('sha512', $header . '.' . $payload, $secret, true));
return $header . '.' . $payload . '.' . $signature;
}
/**
* Reset plugin path.
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test render_header hook while logged in.
*/
- function testAddlinkHeaderLoggedIn()
+ public function testAddlinkHeaderLoggedIn()
{
$str = 'stuff';
$data = array($str => $str);
/**
* Test render_header hook while logged out.
*/
- function testAddlinkHeaderLoggedOut()
+ public function testAddlinkHeaderLoggedOut()
{
$str = 'stuff';
$data = array($str => $str);
/**
* Test render_includes hook while logged in.
*/
- function testAddlinkIncludesLoggedIn()
+ public function testAddlinkIncludesLoggedIn()
{
$str = 'stuff';
$data = array($str => $str);
* Test render_includes hook.
* Should not affect css files while logged out.
*/
- function testAddlinkIncludesLoggedOut()
+ public function testAddlinkIncludesLoggedOut()
{
$str = 'stuff';
$data = array($str => $str);
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test render_linklist hook on external links.
*/
- function testArchiveorgLinklistOnExternalLinks()
+ public function testArchiveorgLinklistOnExternalLinks()
{
$str = 'http://randomstr.com/test';
// plugin data
$this->assertEquals(1, count($link['link_plugin']));
$this->assertNotFalse(strpos($link['link_plugin'][0], $str));
-
}
/**
* Test render_linklist hook on internal links.
*/
- function testArchiveorgLinklistOnInternalLinks()
+ public function testArchiveorgLinklistOnInternalLinks()
{
$internalLink1 = 'http://shaarli.shaarli/?qvMAqg';
$internalLinkRealURL1 = '?qvMAqg';
)
);
-
$data = hook_archiveorg_render_linklist($data);
// Case n°1: first link type, public
$link = $data['links'][5];
$this->assertArrayNotHasKey('link_plugin', $link);
-
}
-
}
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test Isso init without errors.
*/
- function testWallabagInitNoError()
+ public function testWallabagInitNoError()
{
$conf = new ConfigManager('');
$conf->set('plugins.ISSO_SERVER', 'value');
/**
* Test Isso init with errors.
*/
- function testWallabagInitError()
+ public function testWallabagInitError()
{
$conf = new ConfigManager('');
$errors = isso_init($conf);
/**
* Test render_linklist hook with valid settings to display the comment form.
*/
- function testIssoDisplayed()
+ public function testIssoDisplayed()
{
$conf = new ConfigManager('');
$conf->set('plugins.ISSO_SERVER', 'value');
/**
* Test isso plugin when multiple links are displayed (shouldn't be displayed).
*/
- function testIssoMultipleLinks()
+ public function testIssoMultipleLinks()
{
$conf = new ConfigManager('');
$conf->set('plugins.ISSO_SERVER', 'value');
/**
* Test isso plugin when using search (shouldn't be displayed).
*/
- function testIssoNotDisplayedWhenSearch()
+ public function testIssoNotDisplayedWhenSearch()
{
$conf = new ConfigManager('');
$conf->set('plugins.ISSO_SERVER', 'value');
/**
* Test isso plugin without server configuration (shouldn't be displayed).
*/
- function testIssoWithoutConf()
+ public function testIssoWithoutConf()
{
$data = 'abc';
$conf = new ConfigManager('');
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
* Test render_linklist hook.
* Only check that there is basic markdown rendering.
*/
- function testMarkdownLinklist()
+ public function testMarkdownLinklist()
{
$markdown = '# My title' . PHP_EOL . 'Very interesting content.';
$data = array(
* Test render_daily hook.
* Only check that there is basic markdown rendering.
*/
- function testMarkdownDaily()
+ public function testMarkdownDaily()
{
$markdown = '# My title' . PHP_EOL . 'Very interesting content.';
$data = array(
/**
* Test reverse_text2clickable().
*/
- function testReverseText2clickable()
+ public function testReverseText2clickable()
{
$text = 'stuff http://hello.there/is=someone#here otherstuff';
$clickableText = text2clickable($text, '');
/**
* Test reverse_nl2br().
*/
- function testReverseNl2br()
+ public function testReverseNl2br()
{
$text = 'stuff' . PHP_EOL . 'otherstuff';
$processedText = nl2br($text);
/**
* Test reverse_space2nbsp().
*/
- function testReverseSpace2nbsp()
+ public function testReverseSpace2nbsp()
{
$text = ' stuff' . PHP_EOL . ' otherstuff and another';
$processedText = space2nbsp($text);
/**
* Test sanitize_html().
*/
- function testSanitizeHtml()
+ public function testSanitizeHtml()
{
$input = '< script src="js.js"/>';
$input .= '< script attr>alert(\'xss\');</script>';
/**
* Test the no markdown tag.
*/
- function testNoMarkdownTag()
+ public function testNoMarkdownTag()
{
$str = 'All _work_ and `no play` makes Jack a *dull* boy.';
$data = array(
/**
* Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
*/
- function testNoMarkdownNotExcactlyMatching()
+ public function testNoMarkdownNotExcactlyMatching()
{
$str = 'All _work_ and `no play` makes Jack a *dull* boy.';
$data = array(
/**
* Test hashtag links processed with markdown.
*/
- function testMarkdownHashtagLinks()
+ public function testMarkdownHashtagLinks()
{
$md = file_get_contents('tests/plugins/resources/markdown.md');
$md = format_description($md);
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test render_linklist hook.
*/
- function testPlayvideosHeader()
+ public function testPlayvideosHeader()
{
$str = 'stuff';
$data = array($str => $str);
/**
* Test render_footer hook.
*/
- function testPlayvideosFooter()
+ public function testPlayvideosFooter()
{
$str = 'stuff';
$data = array($str => $str);
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test render_feed hook with an RSS feed.
*/
- function testPubSubRssRenderFeed()
+ public function testPubSubRssRenderFeed()
{
$hub = 'http://domain.hub';
$conf = new ConfigManager(self::$configFile);
/**
* Test render_feed hook with an ATOM feed.
*/
- function testPubSubAtomRenderFeed()
+ public function testPubSubAtomRenderFeed()
{
$hub = 'http://domain.hub';
$conf = new ConfigManager(self::$configFile);
<?php
/**
- * PlugQrcodeTest.php
+ * PluginQrcodeTest.php
*/
require_once 'plugins/qrcode/qrcode.php';
require_once 'application/Router.php';
/**
- * Class PlugQrcodeTest
+ * Class PluginQrcodeTest
* Unit test for the QR-Code plugin
*/
-class PlugQrcodeTest extends PHPUnit_Framework_TestCase
+class PluginQrcodeTest extends PHPUnit_Framework_TestCase
{
/**
* Reset plugin path
*/
- function setUp() {
+ public function setUp() {
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test render_linklist hook.
*/
- function testQrcodeLinklist()
+ public function testQrcodeLinklist()
{
$str = 'http://randomstr.com/test';
$data = array(
/**
* Test render_footer hook.
*/
- function testQrcodeFooter()
+ public function testQrcodeFooter()
{
$str = 'stuff';
$data = array($str => $str);
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test Readityourself init without errors.
*/
- function testReadityourselfInitNoError()
+ public function testReadityourselfInitNoError()
{
$conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', 'value');
/**
* Test Readityourself init with errors.
*/
- function testReadityourselfInitError()
+ public function testReadityourselfInitError()
{
$conf = new ConfigManager('');
$errors = readityourself_init($conf);
/**
* Test render_linklist hook.
*/
- function testReadityourselfLinklist()
+ public function testReadityourselfLinklist()
{
$conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', 'value');
/**
* Test without config: nothing should happened.
*/
- function testReadityourselfLinklistWithoutConfig()
+ public function testReadityourselfLinklistWithoutConfig()
{
$conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', null);
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
PluginManager::$PLUGINS_PATH = 'plugins';
}
/**
* Test wallabag init without errors.
*/
- function testWallabagInitNoError()
+ public function testWallabagInitNoError()
{
$conf = new ConfigManager('');
$conf->set('plugins.WALLABAG_URL', 'value');
/**
* Test wallabag init with errors.
*/
- function testWallabagInitError()
+ public function testWallabagInitError()
{
$conf = new ConfigManager('');
$errors = wallabag_init($conf);
/**
* Test render_linklist hook.
*/
- function testWallabagLinklist()
+ public function testWallabagLinklist()
{
$conf = new ConfigManager('');
$conf->set('plugins.WALLABAG_URL', 'value');
/**
* Reset plugin path
*/
- function setUp()
+ public function setUp()
{
$this->instance = 'http://some.url';
}
/**
* Test WallabagInstance with API V1.
*/
- function testWallabagInstanceV1()
+ public function testWallabagInstanceV1()
{
$instance = new WallabagInstance($this->instance, 1);
$expected = $this->instance . '/?plainurl=';
/**
* Test WallabagInstance with API V2.
*/
- function testWallabagInstanceV2()
+ public function testWallabagInstanceV2()
{
$instance = new WallabagInstance($this->instance, 2);
$expected = $this->instance . '/bookmarklet?url=';
/**
* Test WallabagInstance with an invalid API version.
*/
- function testWallabagInstanceInvalidVersion()
+ public function testWallabagInstanceInvalidVersion()
{
$instance = new WallabagInstance($this->instance, false);
$expected = $this->instance . '/?plainurl=';