2016-01-06 20:40:17 +01:00
|
|
|
<?php
|
2020-09-22 20:25:47 +02:00
|
|
|
|
2018-12-04 23:17:23 +01:00
|
|
|
namespace Shaarli\Plugin\Wallabag;
|
2016-01-06 20:40:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WallabagInstance.
|
|
|
|
*/
|
|
|
|
class WallabagInstance
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array Static reference to differrent WB API versions.
|
|
|
|
* - key: version ID, must match plugin settings.
|
|
|
|
* - value: version name.
|
|
|
|
*/
|
2020-09-22 20:25:47 +02:00
|
|
|
private static $wallabagVersions = [
|
2016-01-06 20:40:17 +01:00
|
|
|
1 => '1.x',
|
|
|
|
2 => '2.x',
|
2020-09-22 20:25:47 +02:00
|
|
|
];
|
2016-01-06 20:40:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Static reference to WB endpoint according to the API version.
|
|
|
|
* - key: version name.
|
|
|
|
* - value: endpoint.
|
|
|
|
*/
|
2020-09-22 20:25:47 +02:00
|
|
|
private static $wallabagEndpoints = [
|
2016-01-06 20:40:17 +01:00
|
|
|
'1.x' => '?plainurl=',
|
|
|
|
'2.x' => 'bookmarklet?url=',
|
2020-09-22 20:25:47 +02:00
|
|
|
];
|
2016-01-06 20:40:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Wallabag user instance URL.
|
|
|
|
*/
|
|
|
|
private $instanceUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Wallabag user instance API version.
|
|
|
|
*/
|
|
|
|
private $apiVersion;
|
|
|
|
|
2017-01-05 19:33:06 +01:00
|
|
|
public function __construct($instance, $version)
|
2016-01-06 20:40:17 +01:00
|
|
|
{
|
|
|
|
if ($this->isVersionAllowed($version)) {
|
|
|
|
$this->apiVersion = self::$wallabagVersions[$version];
|
|
|
|
} else {
|
|
|
|
// Default API version: 1.x.
|
|
|
|
$this->apiVersion = self::$wallabagVersions[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->instanceUrl = add_trailing_slash($instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the Wallabag URL to reach from instance URL and API version endpoint.
|
|
|
|
*
|
|
|
|
* @return string wallabag url.
|
|
|
|
*/
|
|
|
|
public function getWallabagUrl()
|
|
|
|
{
|
|
|
|
return $this->instanceUrl . self::$wallabagEndpoints[$this->apiVersion];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks version configuration.
|
|
|
|
*
|
|
|
|
* @param mixed $version given version ID.
|
|
|
|
*
|
|
|
|
* @return bool true if it's valid, false otherwise.
|
|
|
|
*/
|
|
|
|
private function isVersionAllowed($version)
|
|
|
|
{
|
|
|
|
return isset(self::$wallabagVersions[$version]);
|
|
|
|
}
|
|
|
|
}
|