2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2016-08-24 20:48:12 +02:00
|
|
|
class Bridge {
|
|
|
|
|
|
|
|
static protected $dirBridge;
|
|
|
|
|
|
|
|
public function __construct(){
|
|
|
|
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a bridge is an instantiable bridge.
|
|
|
|
* @param string $nameBridge name of the bridge that you want to use
|
|
|
|
* @return true if it is an instantiable bridge, false otherwise.
|
|
|
|
*/
|
|
|
|
static public function isInstantiable($nameBridge){
|
|
|
|
$re = new ReflectionClass($nameBridge);
|
|
|
|
return $re->IsInstantiable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new bridge object
|
|
|
|
* @param string $nameBridge Defined bridge name you want use
|
|
|
|
* @return Bridge object dedicated
|
|
|
|
*/
|
|
|
|
static public function create($nameBridge){
|
|
|
|
if(!preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameBridge)){
|
|
|
|
$message = <<<EOD
|
|
|
|
'nameBridge' must start with one uppercase character followed or not by
|
|
|
|
alphanumeric or dash characters!
|
|
|
|
EOD;
|
|
|
|
throw new \InvalidArgumentException($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$nameBridge = $nameBridge . 'Bridge';
|
|
|
|
$pathBridge = self::getDir() . $nameBridge . '.php';
|
|
|
|
|
|
|
|
if(!file_exists($pathBridge)){
|
|
|
|
throw new \Exception('The bridge you looking for does not exist. It should be at path ' . $pathBridge);
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once $pathBridge;
|
|
|
|
|
|
|
|
if(Bridge::isInstantiable($nameBridge)){
|
|
|
|
return new $nameBridge();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function setDir($dirBridge){
|
|
|
|
if(!is_string($dirBridge)){
|
|
|
|
throw new \InvalidArgumentException('Dir bridge must be a string.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!file_exists($dirBridge)){
|
|
|
|
throw new \Exception('Dir bridge does not exist.');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$dirBridge = $dirBridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function getDir(){
|
|
|
|
$dirBridge = self::$dirBridge;
|
|
|
|
|
|
|
|
if(is_null($dirBridge)){
|
|
|
|
throw new \LogicException(__CLASS__ . ' class need to know bridge path !');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dirBridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists the available bridges.
|
|
|
|
* @return array List of the bridges
|
|
|
|
*/
|
|
|
|
static public function listBridges(){
|
|
|
|
$pathDirBridge = self::getDir();
|
|
|
|
$listBridge = array();
|
|
|
|
$dirFiles = scandir($pathDirBridge);
|
|
|
|
|
|
|
|
if($dirFiles !== false){
|
|
|
|
foreach($dirFiles as $fileName){
|
|
|
|
if(preg_match('@^([^.]+)Bridge\.php$@U', $fileName, $out)){
|
|
|
|
$listBridge[] = $out[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $listBridge;
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:50:32 +02:00
|
|
|
static public function isWhitelisted($whitelist, $name){
|
2016-08-24 20:48:12 +02:00
|
|
|
if(in_array($name, $whitelist)
|
|
|
|
or in_array($name . '.php', $whitelist)
|
|
|
|
or in_array($name . 'Bridge', $whitelist) // DEPRECATED
|
|
|
|
or in_array($name . 'Bridge.php', $whitelist) // DEPRECATED
|
|
|
|
or count($whitelist) === 1 and trim($whitelist[0]) === '*'){
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
interface BridgeInterface {
|
2016-08-25 01:24:53 +02:00
|
|
|
public function collectData();
|
2013-08-11 13:30:41 +02:00
|
|
|
public function getCacheDuration();
|
2015-11-04 00:05:10 +01:00
|
|
|
public function getName();
|
|
|
|
public function getURI();
|
2013-08-11 13:30:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
abstract class BridgeAbstract implements BridgeInterface {
|
2015-11-01 12:17:36 +01:00
|
|
|
|
2013-08-11 13:30:41 +02:00
|
|
|
protected $cache;
|
|
|
|
protected $items = array();
|
|
|
|
|
2016-08-24 19:33:38 +02:00
|
|
|
public $name = 'Unnamed bridge';
|
|
|
|
public $uri = '';
|
2016-08-24 18:09:34 +02:00
|
|
|
public $description = 'No description provided';
|
|
|
|
public $maintainer = 'No maintainer';
|
|
|
|
public $useProxy = true;
|
|
|
|
public $parameters = array();
|
2016-08-28 01:25:33 +02:00
|
|
|
public $inputs = array();
|
2016-08-25 01:24:53 +02:00
|
|
|
protected $queriedContext='';
|
2015-11-01 12:17:36 +01:00
|
|
|
|
2013-08-11 13:30:41 +02:00
|
|
|
protected function returnError($message, $code){
|
|
|
|
throw new \HttpException($message, $code);
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:58:55 +02:00
|
|
|
protected function returnClientError($message){
|
|
|
|
$this->returnError($message, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function returnServerError($message){
|
|
|
|
$this->returnError($message, 500);
|
|
|
|
}
|
|
|
|
|
2013-08-11 13:30:41 +02:00
|
|
|
/**
|
2016-08-24 20:14:23 +02:00
|
|
|
* Return items stored in the bridge
|
2013-08-11 13:30:41 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-08-29 19:42:58 +02:00
|
|
|
public function getItems(){
|
2013-08-11 13:30:41 +02:00
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
2016-09-01 21:07:27 +02:00
|
|
|
protected function isValidTextValue($value, $pattern = null){
|
|
|
|
if(!is_null($pattern)){
|
2016-09-01 20:53:47 +02:00
|
|
|
$filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP,
|
|
|
|
array('options' => array(
|
|
|
|
'regexp' => '/^' . $pattern . '$/'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$filteredValue = filter_var($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($filteredValue === false)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isValidNumberValue($value){
|
|
|
|
$filteredValue = filter_var($value, FILTER_VALIDATE_INT);
|
|
|
|
|
|
|
|
if($filteredValue === false && !empty($value))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isValidCheckboxValue($value){
|
|
|
|
$filteredValue = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
|
|
|
|
|
|
|
if(is_null($filteredValue))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isValidListValue($value, $expectedValues){
|
|
|
|
$filteredValue = filter_var($value);
|
|
|
|
|
|
|
|
if($filteredValue === false)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
if(!in_array($filteredValue, $expectedValues)){ // Check sub-values?
|
|
|
|
foreach($expectedValues as $subName => $subValue){
|
|
|
|
if(is_array($subValue) && in_array($filteredValue, $subValue))
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
|
2016-08-25 01:20:12 +02:00
|
|
|
protected function validateData(&$data){
|
2016-09-01 20:53:47 +02:00
|
|
|
if(!is_array($data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
foreach($data as $name => $value){
|
|
|
|
$registered = false;
|
|
|
|
foreach($this->parameters as $context => $set){
|
|
|
|
if(array_key_exists($name, $set)){
|
|
|
|
$registered = true;
|
|
|
|
|
|
|
|
if(!isset($set[$name]['type'])){ // Default to 'text'
|
|
|
|
$set[$name]['type'] = 'text';
|
2016-08-25 01:20:12 +02:00
|
|
|
}
|
2016-09-01 20:53:47 +02:00
|
|
|
|
2016-08-25 01:20:12 +02:00
|
|
|
switch($set[$name]['type']){
|
2016-09-01 20:53:47 +02:00
|
|
|
case 'number':
|
|
|
|
$data[$name] = $this->isValidNumberValue($value);
|
2016-08-25 01:20:12 +02:00
|
|
|
break;
|
2016-09-01 20:53:47 +02:00
|
|
|
case 'checkbox':
|
|
|
|
$data[$name] = $this->isValidCheckboxValue($value);
|
2016-08-25 01:20:12 +02:00
|
|
|
break;
|
2016-09-01 20:53:47 +02:00
|
|
|
case 'list':
|
|
|
|
$data[$name] = $this->isValidListValue($value, $set[$name]['values']);
|
2016-08-25 01:20:12 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-01 20:53:47 +02:00
|
|
|
case 'text':
|
2016-09-01 21:07:27 +02:00
|
|
|
if(isset($set[$name]['pattern'])){
|
|
|
|
$data[$name] = $this->isValidTextValue($value, $set[$name]['pattern']);
|
|
|
|
} else {
|
|
|
|
$data[$name] = $this->isValidTextValue($value);
|
|
|
|
}
|
2016-08-25 01:20:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-09-01 20:53:47 +02:00
|
|
|
|
2016-09-01 20:59:49 +02:00
|
|
|
if(is_null($data[$name])){
|
|
|
|
echo 'Parameter \'' . $name . '\' is invalid!' . PHP_EOL;
|
2016-09-01 20:53:47 +02:00
|
|
|
return false;
|
2016-09-01 20:59:49 +02:00
|
|
|
}
|
2016-08-25 01:20:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-01 20:53:47 +02:00
|
|
|
|
|
|
|
if(!$registered)
|
|
|
|
return false;
|
2016-08-25 01:20:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 20:53:47 +02:00
|
|
|
return true;
|
2016-08-25 01:20:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 01:24:53 +02:00
|
|
|
protected function getQueriedContext(){
|
|
|
|
$queriedContexts=array();
|
|
|
|
foreach($this->parameters as $context=>$set){
|
|
|
|
$queriedContexts[$context]=null;
|
|
|
|
foreach($set as $id=>$properties){
|
|
|
|
if(isset($properties['value']) &&
|
|
|
|
!empty($properties['value'])){
|
|
|
|
$queriedContexts[$context]=true;
|
|
|
|
}elseif(isset($properties['required']) &&
|
|
|
|
$properties['required']===true){
|
|
|
|
$queriedContexts[$context]=false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($this->parameters['global']) &&
|
|
|
|
$queriedContexts['global']===false){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
unset($queriedContexts['global']);
|
|
|
|
|
|
|
|
switch(array_sum($queriedContexts)){
|
|
|
|
case 0:
|
|
|
|
foreach($queriedContexts as $context=>$queried){
|
|
|
|
if (is_null($queried)){
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
case 1: return array_search(true,$queriedContexts);
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 13:30:41 +02:00
|
|
|
/**
|
|
|
|
* Defined datas with parameters depending choose bridge
|
2016-08-24 20:14:23 +02:00
|
|
|
* Note : you can define a cache with "setCache"
|
2016-08-25 01:20:12 +02:00
|
|
|
* @param array array with expected bridge paramters
|
2013-08-11 13:30:41 +02:00
|
|
|
*/
|
2016-08-28 00:17:20 +02:00
|
|
|
public function setDatas(array $inputs){
|
2016-08-24 20:14:23 +02:00
|
|
|
if(!is_null($this->cache)){
|
2016-08-28 00:17:20 +02:00
|
|
|
$this->cache->prepare($inputs);
|
2013-08-11 13:30:41 +02:00
|
|
|
$time = $this->cache->getTime();
|
2016-09-01 19:43:25 +02:00
|
|
|
if($time !== false && (time() - $this->getCacheDuration() < $time)){
|
|
|
|
$this->items = $this->cache->loadData();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-27 23:52:59 +02:00
|
|
|
}
|
|
|
|
|
2016-08-28 01:42:09 +02:00
|
|
|
if(empty($this->parameters)){
|
|
|
|
if(!empty($inputs)){
|
|
|
|
$this->returnClientError('Invalid parameters value(s)');
|
|
|
|
}
|
2016-08-28 10:38:56 +02:00
|
|
|
|
|
|
|
$this->collectData();
|
|
|
|
if(!is_null($this->cache)){
|
2016-08-29 19:42:58 +02:00
|
|
|
$this->cache->saveData($this->getItems());
|
2016-08-28 10:38:56 +02:00
|
|
|
}
|
2016-08-28 01:42:09 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-27 23:52:59 +02:00
|
|
|
|
2016-08-28 00:17:20 +02:00
|
|
|
if(!$this->validateData($inputs)){
|
2016-08-27 23:52:59 +02:00
|
|
|
$this->returnClientError('Invalid parameters value(s)');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate BridgeAbstract::parameters with sanitized data
|
2016-08-28 00:17:20 +02:00
|
|
|
foreach($inputs as $name=>$value){
|
2016-08-27 23:52:59 +02:00
|
|
|
foreach($this->parameters as $context=>$set){
|
2016-08-28 00:06:13 +02:00
|
|
|
if(isset($this->parameters[$context][$name])){
|
2016-08-28 01:25:33 +02:00
|
|
|
$this->inputs[$context][$name]['value']=$value;
|
2016-08-27 23:52:59 +02:00
|
|
|
$this->parameters[$context][$name]['value']=$value;
|
2016-08-25 01:24:53 +02:00
|
|
|
}
|
2016-08-27 23:52:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess the paramter context from input data
|
|
|
|
$queriedContext=$this->getQueriedContext();
|
|
|
|
if(is_null($queriedContext)){
|
|
|
|
$this->returnClientError('Required parameter(s) missing');
|
|
|
|
}else if($queriedContext===false){
|
|
|
|
$this->returnClientError('Mixed context parameters');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->queriedContext=$queriedContext;
|
|
|
|
|
|
|
|
// Apply default values to missing data
|
|
|
|
$contexts=array($this->queriedContext);
|
|
|
|
if(isset($this->parameters['global'])){
|
|
|
|
$contexts[]='global';
|
|
|
|
}
|
|
|
|
foreach($contexts as $context){
|
2016-08-28 00:01:00 +02:00
|
|
|
foreach($this->parameters[$context] as $name=>$properties){
|
2016-08-27 23:52:59 +02:00
|
|
|
if(!isset($properties['type'])){
|
2016-08-28 00:01:00 +02:00
|
|
|
$this->parameters[$context][$name]['type']='text';
|
2016-08-27 23:52:59 +02:00
|
|
|
}
|
|
|
|
if(isset($properties['value'])){
|
|
|
|
continue;
|
2016-08-25 01:24:53 +02:00
|
|
|
}
|
2016-08-27 23:52:59 +02:00
|
|
|
switch($properties['type']){
|
|
|
|
case 'checkbox':
|
|
|
|
if(!isset($properties['defaultValue'])){
|
2016-08-28 01:25:33 +02:00
|
|
|
$this->inputs[$context][$name]['value']=false;
|
2016-08-25 01:24:53 +02:00
|
|
|
}else{
|
2016-08-28 01:25:33 +02:00
|
|
|
$this->inputs[$context][$name]['value']=$properties['defaultValue'];
|
2016-08-25 01:24:53 +02:00
|
|
|
}
|
2016-08-27 23:52:59 +02:00
|
|
|
break;
|
2016-08-28 11:03:30 +02:00
|
|
|
case 'list':
|
|
|
|
if(!isset($properties['defaultValue'])){
|
2016-08-28 11:08:32 +02:00
|
|
|
$firstItem=reset($properties['values']);
|
|
|
|
if(is_array($firstItem)){
|
|
|
|
$firstItem=reset($firstItem);
|
2016-08-25 01:24:53 +02:00
|
|
|
}
|
2016-08-28 11:08:32 +02:00
|
|
|
$this->inputs[$context][$name]['value']=$firstItem;
|
2016-08-28 11:03:30 +02:00
|
|
|
}else{
|
|
|
|
$this->inputs[$context][$name]['value']=$properties['defaultValue'];
|
2016-08-25 01:24:53 +02:00
|
|
|
}
|
2016-08-28 11:03:30 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(isset($properties['defaultValue'])){
|
|
|
|
$this->inputs[$context][$name]['value']=$properties['defaultValue'];
|
|
|
|
}
|
|
|
|
break;
|
2016-08-25 01:24:53 +02:00
|
|
|
}
|
2016-08-25 01:20:12 +02:00
|
|
|
}
|
2016-08-27 23:52:59 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-08-27 23:52:59 +02:00
|
|
|
// Copy global parameter values to the guessed context
|
2016-08-28 01:25:33 +02:00
|
|
|
if(isset($this->parameters['global'])){
|
|
|
|
foreach($this->parameters['global'] as $name=>$properties){
|
|
|
|
if(isset($inputs[$name])){
|
|
|
|
$value=$inputs[$name];
|
|
|
|
}else if(isset($properties['value'])){
|
|
|
|
$value=$properties['value'];
|
|
|
|
}else{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->inputs[$queriedContext][$name]['value']=$value;
|
2013-08-11 13:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-27 23:52:59 +02:00
|
|
|
|
2016-08-28 01:25:33 +02:00
|
|
|
// Only keep guessed context parameters values
|
2016-08-29 13:27:30 +02:00
|
|
|
if(!isset($this->inputs[$this->queriedContext])){
|
|
|
|
$this->inputs[$this->queriedContext]=array();
|
|
|
|
}
|
2016-08-28 01:25:33 +02:00
|
|
|
$this->inputs=array($this->queriedContext=>$this->inputs[$this->queriedContext]);
|
|
|
|
|
2016-08-27 23:52:59 +02:00
|
|
|
$this->collectData();
|
|
|
|
|
|
|
|
if(!is_null($this->cache)){
|
2016-08-29 19:42:58 +02:00
|
|
|
$this->cache->saveData($this->getItems());
|
2016-08-27 23:52:59 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-28 01:25:33 +02:00
|
|
|
function getInput($input){
|
|
|
|
if(!isset($this->inputs[$this->queriedContext][$input]['value'])){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->inputs[$this->queriedContext][$input]['value'];
|
2013-08-11 13:30:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 18:21:43 +02:00
|
|
|
public function getName(){
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI(){
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2013-08-11 13:30:41 +02:00
|
|
|
public function getCacheDuration(){
|
|
|
|
return 3600;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCache(\CacheAbstract $cache){
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
2014-03-26 16:04:10 +01:00
|
|
|
|
2016-08-24 20:18:59 +02:00
|
|
|
public function debugMessage($text){
|
2016-08-24 20:14:23 +02:00
|
|
|
if(!file_exists('DEBUG')) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-08 19:07:30 +02:00
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
|
|
|
|
$calling = $backtrace[2];
|
|
|
|
$message = $calling['file'] . ':'
|
2016-08-24 20:31:02 +02:00
|
|
|
. $calling['line'] . ' class '
|
|
|
|
. get_class($this) . '->'
|
|
|
|
. $calling['function'] . ' - '
|
|
|
|
. $text;
|
2016-06-25 12:12:54 +02:00
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
error_log($message);
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:31:02 +02:00
|
|
|
protected function getContents($url
|
|
|
|
, $use_include_path = false
|
|
|
|
, $context = null
|
|
|
|
, $offset = 0
|
|
|
|
, $maxlen = null){
|
2016-08-24 20:14:23 +02:00
|
|
|
$contextOptions = array(
|
|
|
|
'http' => array(
|
|
|
|
'user_agent' => ini_get('user_agent')
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if(defined('PROXY_URL') && $this->useProxy){
|
|
|
|
$contextOptions['http']['proxy'] = PROXY_URL;
|
|
|
|
$contextOptions['http']['request_fulluri'] = true;
|
|
|
|
|
|
|
|
if(is_null($context)){
|
|
|
|
$context = stream_context_create($contextOptions);
|
|
|
|
} else {
|
|
|
|
$prevContext=$context;
|
|
|
|
if(!stream_context_set_option($context, $contextOptions)){
|
|
|
|
$context = $prevContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-25 12:12:54 +02:00
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
if(is_null($maxlen)){
|
|
|
|
$content = @file_get_contents($url, $use_include_path, $context, $offset);
|
2016-06-25 12:12:54 +02:00
|
|
|
} else {
|
2016-08-24 20:14:23 +02:00
|
|
|
$content = @file_get_contents($url, $use_include_path, $context, $offset, $maxlen);
|
2016-06-25 12:12:54 +02:00
|
|
|
}
|
2016-07-08 19:05:01 +02:00
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
if($content === false)
|
2016-08-24 20:18:59 +02:00
|
|
|
$this->debugMessage('Cant\'t download ' . $url);
|
2016-08-24 20:14:23 +02:00
|
|
|
|
|
|
|
return $content;
|
2016-06-25 12:12:54 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:31:02 +02:00
|
|
|
protected function getSimpleHTMLDOM($url
|
|
|
|
, $use_include_path = false
|
|
|
|
, $context = null
|
|
|
|
, $offset = 0
|
|
|
|
, $maxLen = null
|
|
|
|
, $lowercase = true
|
|
|
|
, $forceTagsClosed = true
|
|
|
|
, $target_charset = DEFAULT_TARGET_CHARSET
|
|
|
|
, $stripRN = true
|
|
|
|
, $defaultBRText = DEFAULT_BR_TEXT
|
|
|
|
, $defaultSpanText = DEFAULT_SPAN_TEXT){
|
2016-08-24 20:14:23 +02:00
|
|
|
$content = $this->getContents($url, $use_include_path, $context, $offset, $maxLen);
|
2016-08-24 20:31:02 +02:00
|
|
|
return str_get_html($content
|
|
|
|
, $lowercase
|
|
|
|
, $forceTagsClosed
|
|
|
|
, $target_charset
|
|
|
|
, $stripRN
|
|
|
|
, $defaultBRText
|
|
|
|
, $defaultSpanText);
|
2016-08-24 20:14:23 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
}
|
|
|
|
|
2014-03-03 14:12:24 +01:00
|
|
|
/**
|
2016-08-24 20:14:23 +02:00
|
|
|
* Extension of BridgeAbstract allowing caching of files downloaded over http.
|
2016-08-27 21:03:26 +02:00
|
|
|
* TODO allow file cache invalidation by touching files on access, and removing
|
2016-08-24 20:14:23 +02:00
|
|
|
* files/directories which have not been touched since ... a long time
|
2014-03-03 14:12:24 +01:00
|
|
|
*/
|
|
|
|
abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
|
2016-07-08 19:26:07 +02:00
|
|
|
|
2014-03-03 14:12:24 +01:00
|
|
|
/**
|
2016-08-24 20:14:23 +02:00
|
|
|
* Maintain locally cached versions of pages to download, to avoid multiple downloads.
|
2014-03-03 14:12:24 +01:00
|
|
|
* @param url url to cache
|
2016-08-24 20:14:23 +02:00
|
|
|
* @return content of the file as string
|
2014-03-03 14:12:24 +01:00
|
|
|
*/
|
2016-08-24 20:14:23 +02:00
|
|
|
public function get_cached($url){
|
2016-08-24 18:09:34 +02:00
|
|
|
// TODO build this from the variable given to Cache
|
2016-08-24 20:44:21 +02:00
|
|
|
$cacheDir = __DIR__ . '/../cache/pages/';
|
|
|
|
$filepath = $this->buildCacheFilePath($url, $cacheDir);
|
2016-08-24 20:14:23 +02:00
|
|
|
|
2016-08-24 20:35:19 +02:00
|
|
|
if(file_exists($filepath)){
|
|
|
|
$this->debugMessage('loading cached file from ' . $filepath . ' for page at url ' . $url);
|
2016-08-24 18:09:34 +02:00
|
|
|
// TODO touch file and its parent, and try to do neighbour deletion
|
2016-08-24 20:44:21 +02:00
|
|
|
$this->refresh_in_cache($cacheDir, $filepath);
|
2016-08-24 20:35:19 +02:00
|
|
|
$content = file_get_contents($filepath);
|
2016-08-24 18:09:34 +02:00
|
|
|
} else {
|
2016-08-24 20:35:19 +02:00
|
|
|
$this->debugMessage('we have no local copy of ' . $url . ' Downloading to ' . $filepath);
|
|
|
|
$dir = substr($filepath, 0, strrpos($filepath, '/'));
|
2016-08-24 20:14:23 +02:00
|
|
|
|
|
|
|
if(!is_dir($dir)){
|
2016-08-24 20:18:59 +02:00
|
|
|
$this->debugMessage('creating directories for ' . $dir);
|
2014-03-03 14:12:24 +01:00
|
|
|
mkdir($dir, 0777, true);
|
|
|
|
}
|
2016-08-24 20:14:23 +02:00
|
|
|
|
|
|
|
$content = $this->getContents($url);
|
2016-08-24 20:35:19 +02:00
|
|
|
if($content !== false){
|
|
|
|
file_put_contents($filepath, $content);
|
2016-07-08 19:08:48 +02:00
|
|
|
}
|
2014-03-03 14:12:24 +01:00
|
|
|
}
|
2016-08-24 20:14:23 +02:00
|
|
|
|
2016-08-28 19:37:32 +02:00
|
|
|
return str_get_html($content);
|
2014-03-03 14:12:24 +01:00
|
|
|
}
|
2016-07-08 19:08:48 +02:00
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
public function get_cached_time($url){
|
2015-05-20 21:47:58 +02:00
|
|
|
// TODO build this from the variable given to Cache
|
2016-08-24 20:44:21 +02:00
|
|
|
$cacheDir = __DIR__ . '/../cache/pages/';
|
|
|
|
$filepath = $this->buildCacheFilePath($url, $cacheDir);
|
2016-08-24 20:14:23 +02:00
|
|
|
|
2016-08-24 20:35:19 +02:00
|
|
|
if(!file_exists($filepath)){
|
2015-05-20 21:47:58 +02:00
|
|
|
$this->get_cached($url);
|
|
|
|
}
|
2016-08-24 20:14:23 +02:00
|
|
|
|
2016-08-24 20:35:19 +02:00
|
|
|
return filectime($filepath);
|
2016-07-08 19:26:07 +02:00
|
|
|
}
|
2015-05-20 21:47:58 +02:00
|
|
|
|
2016-08-24 20:44:21 +02:00
|
|
|
private function refresh_in_cache($cacheDir, $filepath){
|
2016-08-24 20:35:19 +02:00
|
|
|
$currentPath = $filepath;
|
2016-08-24 20:44:21 +02:00
|
|
|
while(!$cacheDir == $currentPath){
|
2016-08-24 18:09:34 +02:00
|
|
|
touch($currentPath);
|
|
|
|
$currentPath = dirname($currentPath);
|
|
|
|
}
|
2014-04-02 11:55:42 +02:00
|
|
|
}
|
2014-03-03 14:12:24 +01:00
|
|
|
|
2016-08-24 20:44:21 +02:00
|
|
|
private function buildCacheFilePath($url, $cacheDir){
|
|
|
|
$simplified_url = str_replace(
|
2016-08-27 21:03:26 +02:00
|
|
|
['http://', 'https://', '?', '&', '='],
|
|
|
|
['', '', '/', '/', '/'],
|
2016-08-24 20:44:21 +02:00
|
|
|
$url);
|
2016-08-24 20:14:23 +02:00
|
|
|
|
2016-08-24 20:44:21 +02:00
|
|
|
if(substr($cacheDir, -1) !== '/'){
|
|
|
|
$cacheDir .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$filepath = $cacheDir . $simplified_url;
|
|
|
|
|
|
|
|
if(substr($filepath, -1) === '/'){
|
|
|
|
$filepath .= 'index.html';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove_from_cache($url){
|
2016-08-24 18:09:34 +02:00
|
|
|
// TODO build this from the variable given to Cache
|
2016-08-24 20:44:21 +02:00
|
|
|
$cacheDir = __DIR__ . '/../cache/pages/';
|
|
|
|
$filepath = $this->buildCacheFilePath($url, $cacheDir);
|
2016-08-28 19:47:40 +02:00
|
|
|
$this->debugMessage('removing from cache \'' . $filepath . '\'');
|
|
|
|
unlink($filepath);
|
2014-03-03 14:12:24 +01:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
abstract class RssExpander extends HttpCachingBridgeAbstract {
|
2015-11-05 21:26:48 +01:00
|
|
|
|
2016-08-25 17:11:49 +02:00
|
|
|
public function collectExpandableDatas($name){
|
2016-08-24 20:14:23 +02:00
|
|
|
if(empty($name)){
|
2016-08-17 14:45:08 +02:00
|
|
|
$this->returnServerError('There is no $name for this RSS expander');
|
2015-11-05 21:26:48 +01:00
|
|
|
}
|
2016-08-24 20:14:23 +02:00
|
|
|
|
2016-08-25 17:11:49 +02:00
|
|
|
$this->debugMessage('Loading from ' . $name);
|
2016-08-24 20:14:23 +02:00
|
|
|
|
|
|
|
/* Notice we do not use cache here on purpose:
|
|
|
|
* we want a fresh view of the RSS stream each time
|
|
|
|
*/
|
|
|
|
$content = $this->getContents($name) or $this->returnServerError('Could not request ' . $name);
|
2016-07-08 19:26:07 +02:00
|
|
|
|
|
|
|
$rssContent = simplexml_load_string($content);
|
2016-08-25 17:11:49 +02:00
|
|
|
$this->debugMessage('loaded RSS from ' . $name);
|
2015-11-05 21:26:48 +01:00
|
|
|
// TODO insert RSS format detection
|
2016-08-24 20:14:23 +02:00
|
|
|
// For now we always assume RSS 2.0
|
2015-11-05 21:26:48 +01:00
|
|
|
$this->collect_RSS_2_0_data($rssContent);
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
protected function collect_RSS_2_0_data($rssContent){
|
2015-11-05 21:26:48 +01:00
|
|
|
$rssContent = $rssContent->channel[0];
|
2016-08-24 20:18:59 +02:00
|
|
|
$this->debugMessage('RSS content is ===========\n' . var_export($rssContent, true) . '===========');
|
2015-11-05 21:26:48 +01:00
|
|
|
$this->load_RSS_2_0_feed_data($rssContent);
|
2016-08-24 20:14:23 +02:00
|
|
|
foreach($rssContent->item as $item){
|
2016-08-24 20:18:59 +02:00
|
|
|
$this->debugMessage('parsing item ' . var_export($item, true));
|
2015-11-05 21:26:48 +01:00
|
|
|
$this->items[] = $this->parseRSSItem($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
protected function RSS_2_0_time_to_timestamp($item){
|
2015-11-05 21:26:48 +01:00
|
|
|
return DateTime::createFromFormat('D, d M Y H:i:s e', $item->pubDate)->getTimestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO set title, link, description, language, and so on
|
2016-08-24 20:14:23 +02:00
|
|
|
protected function load_RSS_2_0_feed_data($rssContent){
|
2015-11-05 21:26:48 +01:00
|
|
|
$this->name = trim($rssContent->title);
|
|
|
|
$this->uri = trim($rssContent->link);
|
|
|
|
$this->description = trim($rssContent->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method should return, from a source RSS item given by lastRSS, one of our Items objects
|
|
|
|
* @param $item the input rss item
|
|
|
|
* @return a RSS-Bridge Item, with (hopefully) the whole content)
|
|
|
|
*/
|
|
|
|
abstract protected function parseRSSItem($item);
|
|
|
|
|
2016-08-24 20:14:23 +02:00
|
|
|
public function getDescription(){
|
2015-11-05 21:26:48 +01:00
|
|
|
return $this->description;
|
|
|
|
}
|
|
|
|
}
|