2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2018-11-06 19:23:32 +01:00
|
|
|
|
2016-08-24 20:48:12 +02:00
|
|
|
class Bridge {
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
static protected $dirBridge;
|
|
|
|
|
2018-11-10 22:26:56 +01:00
|
|
|
/**
|
|
|
|
* Holds the active whitelist.
|
|
|
|
* Use Bridge::getWhitelist() instead of accessing this parameter directly!
|
|
|
|
*/
|
|
|
|
private static $whitelist = array();
|
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function __construct(){
|
|
|
|
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new bridge object
|
|
|
|
* @param string $nameBridge Defined bridge name you want use
|
|
|
|
* @return Bridge object dedicated
|
|
|
|
*/
|
|
|
|
static public 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$nameBridge = $nameBridge . 'Bridge';
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
static public 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists the available bridges.
|
|
|
|
* @return array List of the bridges
|
|
|
|
*/
|
|
|
|
static public function listBridges(){
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @return bool Returns true if the given bridge is whitelisted.
|
|
|
|
*/
|
|
|
|
static public function isWhitelisted($name){
|
|
|
|
return in_array(Bridge::sanitizeBridgeName($name), Bridge::getWhitelist());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On first call reads the whitelist from WHITELIST. Each line in the file
|
|
|
|
* specifies one bridge that will be placed on the whitelist. An empty file
|
|
|
|
* disables all bridges. '*' enables all bridges.
|
|
|
|
*
|
|
|
|
* @return array Returns a list of whitelisted bridges
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function setWhitelist($default = array()) {
|
|
|
|
Bridge::$whitelist = array_map('Bridge::sanitizeBridgeName', $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string Returns a sanitized bridge name if the given name has been
|
|
|
|
* found valid, null otherwise.
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|