2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2018-11-13 20:26:56 +01:00
|
|
|
/**
|
|
|
|
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
|
|
|
|
* Atom feeds for websites that don't have one.
|
|
|
|
*
|
|
|
|
* For the full license information, please view the UNLICENSE file distributed
|
|
|
|
* with this source code.
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @license http://unlicense.org/ UNLICENSE
|
|
|
|
* @link https://github.com/rss-bridge/rss-bridge
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory class responsible for creating new instances of bridges from a given
|
|
|
|
* working directory, limited by a whitelist.
|
|
|
|
*
|
|
|
|
* This class is capable of:
|
|
|
|
* - Locating bridges in the specified working directory (see {@see Bridge::$dirBridge})
|
|
|
|
* - Filtering bridges based on a whitelist (see {@see Bridge::$whitelist})
|
|
|
|
* - Creating new bridge instances based on the bridge's name (see {@see Bridge::create()})
|
|
|
|
*
|
|
|
|
* The following example illustrates the intended use for this class.
|
|
|
|
*
|
|
|
|
* ```PHP
|
|
|
|
* require_once __DIR__ . '/rssbridge.php';
|
|
|
|
*
|
|
|
|
* // Step 1: Set the working directory
|
|
|
|
* Bridge::setDir(__DIR__ . '/../bridges/');
|
|
|
|
*
|
|
|
|
* // Step 2: Add bridges to the whitelist
|
|
|
|
* Bridge::setWhitelist(array('GitHubIssue', 'GoogleSearch', 'Facebook', 'Twitter'));
|
|
|
|
*
|
|
|
|
* // Step 3: Create a new instance of a bridge (based on the name)
|
|
|
|
* $bridge = Bridge::create('GitHubIssue');
|
|
|
|
* ```
|
|
|
|
*/
|
2016-08-24 20:48:12 +02:00
|
|
|
class Bridge {
|
|
|
|
|
2018-11-13 20:26:56 +01:00
|
|
|
/**
|
|
|
|
* Holds the working directory.
|
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Bridge::setDir()} and {@see Bridge::getDir()} instead.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
static protected $dirBridge;
|
|
|
|
|
2018-11-10 22:26:56 +01:00
|
|
|
/**
|
2018-11-13 20:26:56 +01:00
|
|
|
* Holds the whitelist.
|
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Bridge::getWhitelist()} instead.
|
|
|
|
*
|
|
|
|
* @var array
|
2018-11-10 22:26:56 +01:00
|
|
|
*/
|
|
|
|
private static $whitelist = array();
|
|
|
|
|
2018-11-13 20:26:56 +01:00
|
|
|
/**
|
|
|
|
* Throws an exception when trying to create a new instance of this class.
|
|
|
|
* Use {@see Bridge::create()} to instanciate a new bridge from the working
|
|
|
|
* directory.
|
|
|
|
*
|
|
|
|
* @throws LogicException if called.
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
public function __construct(){
|
|
|
|
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-13 20:26:56 +01:00
|
|
|
* Creates a new instance of a bridge in the working directory.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException if the provided bridge name is invalid.
|
|
|
|
* @throws Exception if a bridge with the given name does not exist in the
|
|
|
|
* working directory.
|
|
|
|
* @param string $nameBridge Name of the bridge.
|
|
|
|
* @return object|bool Instance of the bridge or false if the bridge is not instantiable.
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function create($nameBridge){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
$message = <<<EOD
|
2016-08-24 20:48:12 +02:00
|
|
|
'nameBridge' must start with one uppercase character followed or not by
|
|
|
|
alphanumeric or dash characters!
|
|
|
|
EOD;
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \InvalidArgumentException($message);
|
|
|
|
}
|
|
|
|
|
2018-11-13 17:36:03 +01:00
|
|
|
$nameBridge = Bridge::sanitizeBridgeName($nameBridge) . 'Bridge';
|
2016-09-10 20:41:11 +02:00
|
|
|
$pathBridge = self::getDir() . $nameBridge . '.php';
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($pathBridge)) {
|
2018-11-13 17:24:36 +01:00
|
|
|
throw new \Exception('The bridge you are looking for does not exist. It should be at path '
|
2016-09-10 20:41:11 +02:00
|
|
|
. $pathBridge);
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once $pathBridge;
|
|
|
|
|
2017-08-05 23:58:18 +02:00
|
|
|
if((new ReflectionClass($nameBridge))->isInstantiable()) {
|
2016-09-10 20:41:11 +02:00
|
|
|
return new $nameBridge();
|
|
|
|
}
|
2017-08-05 23:58:18 +02:00
|
|
|
|
|
|
|
return false;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-13 20:26:56 +01:00
|
|
|
/**
|
|
|
|
* Sets the current working directory.
|
|
|
|
*
|
|
|
|
* @param string $dirBridge Path to the directory containing bridges.
|
|
|
|
* @throws LogicException if the provided path is not a valid string.
|
|
|
|
* @throws Exception if the provided path does not exist.
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function setDir($dirBridge){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_string($dirBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \InvalidArgumentException('Dir bridge must be a string.');
|
|
|
|
}
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($dirBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \Exception('Dir bridge does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$dirBridge = $dirBridge;
|
|
|
|
}
|
|
|
|
|
2018-11-13 20:26:56 +01:00
|
|
|
/**
|
|
|
|
* Returns the current working directory.
|
|
|
|
* The working directory must be specified with {@see Bridge::setDir()}!
|
|
|
|
*
|
|
|
|
* @throws LogicException if the working directory was not specified.
|
|
|
|
* @return string The current working directory.
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function getDir(){
|
2017-08-05 23:58:18 +02:00
|
|
|
if(is_null(self::$dirBridge)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
|
|
|
|
}
|
|
|
|
|
2017-08-05 23:58:18 +02:00
|
|
|
return self::$dirBridge;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-13 20:26:56 +01:00
|
|
|
* Returns the list of bridge names based on the working directory.
|
|
|
|
*
|
|
|
|
* The list is cached internally to allow for successive calls.
|
|
|
|
*
|
|
|
|
* @return array List of bridge names
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function listBridges(){
|
2016-09-10 20:41:11 +02:00
|
|
|
|
2018-11-10 22:31:38 +01:00
|
|
|
static $listBridge = array(); // Initialized on first call
|
|
|
|
|
|
|
|
if(empty($listBridge)) {
|
|
|
|
$dirFiles = scandir(self::getDir());
|
|
|
|
|
|
|
|
if($dirFiles !== false) {
|
|
|
|
foreach($dirFiles as $fileName) {
|
|
|
|
if(preg_match('@^([^.]+)Bridge\.php$@U', $fileName, $out)) {
|
|
|
|
$listBridge[] = $out[1];
|
|
|
|
}
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $listBridge;
|
|
|
|
}
|
|
|
|
|
2018-11-10 22:26:56 +01:00
|
|
|
/**
|
2018-11-13 20:26:56 +01:00
|
|
|
* Checks if a bridge is whitelisted.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the bridge.
|
|
|
|
* @return bool True if the bridge is whitelisted.
|
2018-11-10 22:26:56 +01:00
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function isWhitelisted($name){
|
2018-11-10 22:26:56 +01:00
|
|
|
return in_array(Bridge::sanitizeBridgeName($name), Bridge::getWhitelist());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-13 20:26:56 +01:00
|
|
|
* Returns the whitelist.
|
2018-11-10 22:26:56 +01:00
|
|
|
*
|
2018-11-13 20:26:56 +01:00
|
|
|
* On first call this function reads the whitelist from {@see WHITELIST}.
|
|
|
|
* * Each line in the file specifies one bridge on the whitelist.
|
|
|
|
* * An empty file disables all bridges.
|
|
|
|
* * If the file only only contains `*`, all bridges are whitelisted.
|
|
|
|
*
|
|
|
|
* Use {@see Bridge::setWhitelist()} to specify a default whitelist **before**
|
|
|
|
* calling this function! The list is cached internally to allow for
|
|
|
|
* successive calls. If {@see Bridge::setWhitelist()} gets called after this
|
|
|
|
* function, the whitelist is **not** updated again!
|
|
|
|
*
|
|
|
|
* @return array Array of whitelisted bridges
|
2018-11-10 22:26:56 +01:00
|
|
|
*/
|
|
|
|
public static function getWhitelist() {
|
|
|
|
|
|
|
|
static $firstCall = true; // Initialized on first call
|
|
|
|
|
|
|
|
if($firstCall) {
|
|
|
|
|
|
|
|
// Create initial whitelist or load from disk
|
|
|
|
if (!file_exists(WHITELIST) && !empty(Bridge::$whitelist)) {
|
|
|
|
file_put_contents(WHITELIST, implode("\n", Bridge::$whitelist));
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$contents = trim(file_get_contents(WHITELIST));
|
|
|
|
|
|
|
|
if($contents === '*') { // Whitelist all bridges
|
|
|
|
Bridge::$whitelist = Bridge::listBridges();
|
|
|
|
} else {
|
|
|
|
Bridge::$whitelist = array_map('Bridge::sanitizeBridgeName', explode("\n", $contents));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Bridge::$whitelist;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-13 20:26:56 +01:00
|
|
|
/**
|
|
|
|
* Sets the (default) whitelist.
|
|
|
|
*
|
|
|
|
* If this function is called **before** {@see Bridge::getWhitelist()}, the
|
|
|
|
* provided whitelist will be replaced by a custom whitelist specified in
|
|
|
|
* {@see WHITELIST} (if it exists).
|
|
|
|
*
|
|
|
|
* If this function is called **after** {@see Bridge::getWhitelist()}, the
|
|
|
|
* provided whitelist is taken as is (not updated by the custom whitelist
|
|
|
|
* again).
|
|
|
|
*
|
|
|
|
* @param array $default The whitelist as array of bridge names.
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-10 22:26:56 +01:00
|
|
|
public static function setWhitelist($default = array()) {
|
|
|
|
Bridge::$whitelist = array_map('Bridge::sanitizeBridgeName', $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-13 20:26:56 +01:00
|
|
|
* Returns the sanitized bridge name.
|
|
|
|
*
|
|
|
|
* The bridge name can be specified in various ways:
|
|
|
|
* * The PHP file name (i.e. `GitHubIssueBridge.php`)
|
|
|
|
* * The PHP file name without file extension (i.e. `GitHubIssueBridge`)
|
|
|
|
* * The bridge name (i.e. `GitHubIssue`)
|
|
|
|
*
|
|
|
|
* Casing is ignored (i.e. `GITHUBISSUE` and `githubissue` are the same).
|
|
|
|
*
|
|
|
|
* A bridge file matching the given bridge name must exist in the working
|
|
|
|
* directory!
|
|
|
|
*
|
|
|
|
* @param string $name The bridge name
|
|
|
|
* @return string|null The sanitized bridge name if the provided name is
|
|
|
|
* valid, null otherwise.
|
2018-11-10 22:26:56 +01:00
|
|
|
*/
|
|
|
|
private static function sanitizeBridgeName($name) {
|
|
|
|
|
|
|
|
if(is_string($name)) {
|
|
|
|
|
|
|
|
// Trim trailing '.php' if exists
|
|
|
|
if(preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim trailing 'Bridge' if exists
|
|
|
|
if(preg_match('/(.+)(?:Bridge)/i', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// The name is valid if a corresponding bridge file is found on disk
|
|
|
|
if(in_array(strtolower($name), array_map('strtolower', Bridge::listBridges()))) {
|
|
|
|
$index = array_search(strtolower($name), array_map('strtolower', Bridge::listBridges()));
|
|
|
|
return Bridge::listBridges()[$index];
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug::log('Invalid bridge name specified: "' . $name . '"!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null; // Bad parameter
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2016-08-24 20:48:12 +02:00
|
|
|
}
|