mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-06 16:16:39 +02:00
ready for merge #43
This commit is contained in:
53
CImage.php
53
CImage.php
@@ -307,7 +307,8 @@ class CImage
|
|||||||
/**
|
/**
|
||||||
* Pattern to recognize a remote file.
|
* Pattern to recognize a remote file.
|
||||||
*/
|
*/
|
||||||
private $remotePattern = '#^[http|https]://#';
|
//private $remotePattern = '#^[http|https]://#';
|
||||||
|
private $remotePattern = '#^https?://#';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -436,11 +437,8 @@ class CImage
|
|||||||
*/
|
*/
|
||||||
public function isRemoteSource($src)
|
public function isRemoteSource($src)
|
||||||
{
|
{
|
||||||
$remote = $this->allowRemote
|
$remote = preg_match($this->remotePattern, $src);
|
||||||
&& preg_match($this->remotePattern, $src);
|
|
||||||
|
|
||||||
$this->log("Detected remote image: " . ($remote ? "true" : "false"));
|
$this->log("Detected remote image: " . ($remote ? "true" : "false"));
|
||||||
|
|
||||||
return $remote;
|
return $remote;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,6 +463,35 @@ class CImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download a remote image and return path to its local copy.
|
||||||
|
*
|
||||||
|
* @param string $src remote path to image.
|
||||||
|
*
|
||||||
|
* @return string as path to downloaded remote source.
|
||||||
|
*/
|
||||||
|
public function downloadRemoteSource($src)
|
||||||
|
{
|
||||||
|
$remote = new CRemoteImage();
|
||||||
|
|
||||||
|
$cache = $this->saveFolder . "/remote/";
|
||||||
|
if (!is_writable($cache)) {
|
||||||
|
$this->log("The remote cache is not writable.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$remote->setCache($cache);
|
||||||
|
$remote->useCache($this->useCache);
|
||||||
|
$src = $remote->download($src);
|
||||||
|
|
||||||
|
$this->log("Remote HTTP status: " . $remote->getStatus());
|
||||||
|
$this->log("Remote item has local cached file: $src");
|
||||||
|
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
||||||
|
|
||||||
|
return $src;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set src file.
|
* Set src file.
|
||||||
*
|
*
|
||||||
@@ -479,20 +506,8 @@ class CImage
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isRemoteSource($src)) {
|
if ($this->allowRemote && $this->isRemoteSource($src)) {
|
||||||
$remote = new CRemoteImage();
|
$src = $this->downloadRemoteSource($src);
|
||||||
|
|
||||||
$cache = $this->saveFolder . "/remote/";
|
|
||||||
if (!is_writable($cache)) {
|
|
||||||
$this->log("The remote cache is not writable.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$remote->setCache($cache);
|
|
||||||
$remote->useCache($this->useCache);
|
|
||||||
$src = $remote->download($src);
|
|
||||||
$this->log("Remote HTTP status: " . $remote->getStatus());
|
|
||||||
$this->log("Remote item has local cached file: $src");
|
|
||||||
$this->log("Remote details on cache:" . print_r($remote->getDetails(), true));
|
|
||||||
$dir = null;
|
$dir = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -297,7 +297,9 @@ class CRemoteImage
|
|||||||
$cacheFile = str_replace(["/", ":", "#", ".", "?"], "-", $this->url);
|
$cacheFile = str_replace(["/", ":", "#", ".", "?"], "-", $this->url);
|
||||||
$this->fileName = $this->saveFolder . $cacheFile;
|
$this->fileName = $this->saveFolder . $cacheFile;
|
||||||
$this->fileJson = $this->fileName . ".json";
|
$this->fileJson = $this->fileName . ".json";
|
||||||
$this->cache = json_decode(file_get_contents($this->fileJson), true);
|
if (is_readable($this->fileJson)) {
|
||||||
|
$this->cache = json_decode(file_get_contents($this->fileJson), true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -70,6 +70,24 @@ function getDefined($key, $defined, $undefined)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value from config array or default if key is not set in config array.
|
||||||
|
*
|
||||||
|
* @param string $key the key in the config array.
|
||||||
|
* @param mixed $default value to be default if $key is not set in config.
|
||||||
|
*
|
||||||
|
* @return mixed value as $config[$key] or $default.
|
||||||
|
*/
|
||||||
|
function getConfig($key, $default)
|
||||||
|
{
|
||||||
|
global $config;
|
||||||
|
return isset($config[$key])
|
||||||
|
? $config[$key]
|
||||||
|
: $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log when verbose mode, when used without argument it returns the result.
|
* Log when verbose mode, when used without argument it returns the result.
|
||||||
*
|
*
|
||||||
@@ -123,11 +141,23 @@ $verbose = getDefined(array('verbose', 'v'), true, false);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the source files.
|
||||||
|
*/
|
||||||
|
$autoloader = getConfig('autoloader', false);
|
||||||
|
$cimageClass = getConfig('cimage_class', false);
|
||||||
|
|
||||||
|
if ($autoloader) {
|
||||||
|
require $autoloader;
|
||||||
|
} else if ($cimageClass) {
|
||||||
|
require $cimageClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the class for the image.
|
* Create the class for the image.
|
||||||
*/
|
*/
|
||||||
require $config['cimage_class'];
|
|
||||||
|
|
||||||
$img = new CImage();
|
$img = new CImage();
|
||||||
$img->setVerbose($verbose);
|
$img->setVerbose($verbose);
|
||||||
|
|
||||||
@@ -137,12 +167,23 @@ $img->setVerbose($verbose);
|
|||||||
* Allow or disallow remote download of images from other servers.
|
* Allow or disallow remote download of images from other servers.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
$allowRemote = $config['remote_allow'];
|
$allowRemote = getConfig('remote_allow', false);
|
||||||
|
$remotePwd = getConfig('remote_password', false);
|
||||||
|
$pwd = get(array('password', 'pwd'), null);
|
||||||
|
|
||||||
if ($allowRemote) {
|
// Check if passwords match, if configured to use passwords
|
||||||
$pattern = isset($config['remote_pattern'])
|
$passwordMatch = null;
|
||||||
? $config['remote_pattern']
|
if ($remotePwd != false) {
|
||||||
: null;
|
if ($remotePwd == $pwd) {
|
||||||
|
$passwordMatch = true;
|
||||||
|
} else {
|
||||||
|
$passwordMatch = false;
|
||||||
|
errorPage('Trying to download remote image but missing password.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($allowRemote && $passwordMatch !== false) {
|
||||||
|
$pattern = getConfig('remote_pattern', null);
|
||||||
$img->setRemoteDownload($allowRemote, $pattern);
|
$img->setRemoteDownload($allowRemote, $pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -5,16 +5,19 @@
|
|||||||
* config-file imgtest_config.php.
|
* config-file imgtest_config.php.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
include __DIR__ . "/../CHttpGet.php";
|
|
||||||
include __DIR__ . "/../CRemoteImage.php";
|
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paths, where are all the stuff I should use?
|
* Where are the sources for the classfiles.
|
||||||
* Append ending slash on directories.
|
*/
|
||||||
|
'autoloader' => __DIR__ . '/../autoload.php',
|
||||||
|
//'cimage_class' => __DIR__ . '/../CImage.php',
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Paths, where are the images stored and where is the cache.
|
||||||
*/
|
*/
|
||||||
'cimage_class' => __DIR__ . '/../CImage.php',
|
|
||||||
'image_path' => __DIR__ . '/img/',
|
'image_path' => __DIR__ . '/img/',
|
||||||
'cache_path' => __DIR__ . '/../cache/',
|
'cache_path' => __DIR__ . '/../cache/',
|
||||||
|
|
||||||
@@ -39,9 +42,17 @@ return array(
|
|||||||
/**
|
/**
|
||||||
* Allow or disallow downloading of remote files, images available on
|
* Allow or disallow downloading of remote files, images available on
|
||||||
* some remote server. Default is to disallow.
|
* some remote server. Default is to disallow.
|
||||||
|
* Use password to protect from missusage, send &pwd=... or &password=..
|
||||||
|
* with the request to match the password or set to false to disable.
|
||||||
|
*
|
||||||
|
* Default values.
|
||||||
|
* remote_allow: false
|
||||||
|
* remote_password: false // as in do not use password
|
||||||
|
* remote_pattern: null // use default values from CImage
|
||||||
*/
|
*/
|
||||||
'remote_allow' => true,
|
'remote_allow' => true,
|
||||||
'remote_pattern' => '#^http#',
|
//'remote_password' => false, // "secret-password",
|
||||||
|
//'remote_pattern' => '#^https?://#',
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user