2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
2018-11-13 22:26:56 +03: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-11-15 21:15:08 +03:00
|
|
|
* Factory class responsible for creating bridge objects from a given working
|
|
|
|
* directory, limited by a whitelist.
|
2018-11-13 22:26:56 +03:00
|
|
|
*
|
|
|
|
* This class is capable of:
|
2018-11-15 21:16:37 +03:00
|
|
|
* - Locating bridge classes in the specified working directory (see {@see Bridge::$workingDir})
|
2018-11-13 22:26:56 +03:00
|
|
|
* - 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
|
2018-11-15 21:07:33 +03:00
|
|
|
* Bridge::setWorkingDir(__DIR__ . '/../bridges/');
|
2018-11-13 22:26:56 +03:00
|
|
|
*
|
|
|
|
* // 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');
|
|
|
|
* ```
|
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
class BridgeFactory extends FactoryAbstract {
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2018-11-11 00:26:56 +03:00
|
|
|
/**
|
2018-11-15 21:15:08 +03:00
|
|
|
* Holds a list of whitelisted bridges.
|
|
|
|
*
|
2018-11-13 22:26:56 +03:00
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Bridge::getWhitelist()} instead.
|
|
|
|
*
|
|
|
|
* @var array
|
2018-11-11 00:26:56 +03:00
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
protected $whitelist = array();
|
2016-09-10 21:41:11 +03:00
|
|
|
|
|
|
|
/**
|
2018-11-15 21:15:08 +03:00
|
|
|
* Creates a new bridge object from the working directory.
|
2018-11-13 22:26:56 +03:00
|
|
|
*
|
2018-11-15 21:15:08 +03:00
|
|
|
* @throws \InvalidArgumentException if the requested bridge name is invalid.
|
|
|
|
* @throws \Exception if the requested bridge doesn't exist in the working
|
|
|
|
* directory.
|
2018-11-15 21:31:31 +03:00
|
|
|
* @param string $name Name of the bridge object.
|
2018-11-15 21:15:08 +03:00
|
|
|
* @return object|bool The bridge object or false if the class is not instantiable.
|
2018-11-13 22:26:56 +03:00
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
public function create($name){
|
|
|
|
if(!$this->isBridgeName($name)) {
|
2018-11-15 21:15:08 +03:00
|
|
|
throw new \InvalidArgumentException('Bridge name invalid!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2019-06-18 19:55:29 +03:00
|
|
|
$name = $this->sanitizeBridgeName($name) . 'Bridge';
|
|
|
|
$filePath = $this->getWorkingDir() . $name . '.php';
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2018-11-15 21:31:31 +03:00
|
|
|
if(!file_exists($filePath)) {
|
2018-11-15 21:38:14 +03:00
|
|
|
throw new \Exception('Bridge file ' . $filePath . ' does not exist!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2018-11-15 21:31:31 +03:00
|
|
|
require_once $filePath;
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2018-11-15 21:31:31 +03:00
|
|
|
if((new \ReflectionClass($name))->isInstantiable()) {
|
|
|
|
return new $name();
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2017-08-06 00:58:18 +03:00
|
|
|
|
|
|
|
return false;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2018-11-15 21:33:56 +03:00
|
|
|
/**
|
|
|
|
* Returns true if the provided name is a valid bridge name.
|
|
|
|
*
|
|
|
|
* A valid bridge name starts with a capital letter ([A-Z]), followed by
|
|
|
|
* zero or more alphanumeric characters or hyphen ([A-Za-z0-9-]).
|
|
|
|
*
|
|
|
|
* @param string $name The bridge name.
|
|
|
|
* @return bool true if the name is a valid bridge name, false otherwise.
|
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
public function isBridgeName($name){
|
2018-11-15 21:33:56 +03:00
|
|
|
return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1;
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
2018-11-15 21:42:14 +03:00
|
|
|
* Returns the list of bridge names from the working directory.
|
2018-11-13 22:26:56 +03:00
|
|
|
*
|
|
|
|
* The list is cached internally to allow for successive calls.
|
|
|
|
*
|
|
|
|
* @return array List of bridge names
|
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
public function getBridgeNames(){
|
2016-09-10 21:41:11 +03:00
|
|
|
|
2018-11-15 21:42:14 +03:00
|
|
|
static $bridgeNames = array(); // Initialized on first call
|
2018-11-11 00:31:38 +03:00
|
|
|
|
2018-11-15 21:42:14 +03:00
|
|
|
if(empty($bridgeNames)) {
|
2019-06-18 19:55:29 +03:00
|
|
|
$files = scandir($this->getWorkingDir());
|
2018-11-11 00:31:38 +03:00
|
|
|
|
2018-11-15 21:42:14 +03:00
|
|
|
if($files !== false) {
|
|
|
|
foreach($files as $file) {
|
|
|
|
if(preg_match('/^([^.]+)Bridge\.php$/U', $file, $out)) {
|
|
|
|
$bridgeNames[] = $out[1];
|
2018-11-11 00:31:38 +03:00
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:42:14 +03:00
|
|
|
return $bridgeNames;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
|
2018-11-11 00:26:56 +03:00
|
|
|
/**
|
2018-11-13 22:26:56 +03:00
|
|
|
* Checks if a bridge is whitelisted.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the bridge.
|
|
|
|
* @return bool True if the bridge is whitelisted.
|
2018-11-11 00:26:56 +03:00
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
public function isWhitelisted($name){
|
|
|
|
return in_array($this->sanitizeBridgeName($name), $this->getWhitelist());
|
2018-11-11 00:26:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-13 22:26:56 +03:00
|
|
|
* Returns the whitelist.
|
2018-11-11 00:26:56 +03:00
|
|
|
*
|
2019-06-06 21:53:44 +03:00
|
|
|
* On first call this function reads the whitelist from {@see WHITELIST} if
|
|
|
|
* the file exists, {@see WHITELIST_DEFAULT} otherwise.
|
2018-11-13 22:26:56 +03:00
|
|
|
* * 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-11 00:26:56 +03:00
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
public function getWhitelist() {
|
2018-11-11 00:26:56 +03:00
|
|
|
|
|
|
|
static $firstCall = true; // Initialized on first call
|
|
|
|
|
|
|
|
if($firstCall) {
|
|
|
|
|
2019-06-06 21:53:44 +03:00
|
|
|
if(file_exists(WHITELIST)) {
|
2018-11-11 00:26:56 +03:00
|
|
|
$contents = trim(file_get_contents(WHITELIST));
|
2019-06-06 21:53:44 +03:00
|
|
|
} elseif(file_exists(WHITELIST_DEFAULT)) {
|
|
|
|
$contents = trim(file_get_contents(WHITELIST_DEFAULT));
|
|
|
|
} else {
|
|
|
|
$contents = '';
|
|
|
|
}
|
2018-11-11 00:26:56 +03:00
|
|
|
|
2019-06-06 21:53:44 +03:00
|
|
|
if($contents === '*') { // Whitelist all bridges
|
2019-06-18 19:55:29 +03:00
|
|
|
$this->whitelist = $this->getBridgeNames();
|
2019-06-06 21:53:44 +03:00
|
|
|
} else {
|
2019-06-18 19:55:29 +03:00
|
|
|
//$this->$whitelist = array_map('$this->sanitizeBridgeName', explode("\n", $contents));
|
2019-06-06 21:53:44 +03:00
|
|
|
foreach(explode("\n", $contents) as $bridgeName) {
|
2019-06-18 19:55:29 +03:00
|
|
|
$this->whitelist[] = $this->sanitizeBridgeName($bridgeName);
|
2018-11-11 00:26:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:55:29 +03:00
|
|
|
return $this->whitelist;
|
2018-11-11 00:26:56 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-13 22:26:56 +03: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
|
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
public function setWhitelist($default = array()) {
|
|
|
|
$this->whitelist = array_map('$this->sanitizeBridgeName', $default);
|
2018-11-11 00:26:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-13 22:26:56 +03: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-11 00:26:56 +03:00
|
|
|
*/
|
2019-06-18 19:55:29 +03:00
|
|
|
protected function sanitizeBridgeName($name) {
|
2018-11-11 00:26:56 +03:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2019-06-06 21:59:29 +03:00
|
|
|
// Improve performance for correctly written bridge names
|
2019-06-18 19:55:29 +03:00
|
|
|
if(in_array($name, $this->getBridgeNames())) {
|
|
|
|
$index = array_search($name, $this->getBridgeNames());
|
|
|
|
return $this->getBridgeNames()[$index];
|
2019-06-06 21:59:29 +03:00
|
|
|
}
|
|
|
|
|
2018-11-11 00:26:56 +03:00
|
|
|
// The name is valid if a corresponding bridge file is found on disk
|
2019-06-18 19:55:29 +03:00
|
|
|
if(in_array(strtolower($name), array_map('strtolower', $this->getBridgeNames()))) {
|
|
|
|
$index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeNames()));
|
|
|
|
return $this->getBridgeNames()[$index];
|
2018-11-11 00:26:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Debug::log('Invalid bridge name specified: "' . $name . '"!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null; // Bad parameter
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2016-08-24 21:48:12 +03:00
|
|
|
}
|