mirror of
https://github.com/mosbth/cimage.git
synced 2025-08-25 00:45:50 +02:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
c5cc0314c2 | ||
|
71816261f2 | ||
|
a62d7cb6c2 | ||
|
ccbd08949f | ||
|
6467fcc748 | ||
|
14d22a18e5 | ||
|
6d3687d838 | ||
|
ad8f6c12ee | ||
|
ad4930c3ae | ||
|
f250f7dff9 | ||
|
05c11ca9fc | ||
|
b069e322e9 | ||
|
6e0c775ede | ||
|
179469334a | ||
|
0b2723feee | ||
|
c009f423a2 | ||
|
79a7fd17d8 | ||
|
3271d165ff | ||
|
1dc04e7c53 | ||
|
376a40e538 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -11,3 +11,6 @@ vendor
|
||||
|
||||
# Build and test
|
||||
build/
|
||||
|
||||
# Mac OS
|
||||
.DS_Store
|
||||
|
@@ -76,6 +76,7 @@ script:
|
||||
|
||||
# Run validation & publish
|
||||
#- make phpunit
|
||||
- composer validate
|
||||
- phpunit
|
||||
#- make phpcs
|
||||
|
||||
|
@@ -175,7 +175,7 @@ class CAsciiArt
|
||||
*/
|
||||
public function getLuminance($red, $green, $blue)
|
||||
{
|
||||
switch($this->luminanceStrategy) {
|
||||
switch ($this->luminanceStrategy) {
|
||||
case 1:
|
||||
$luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255;
|
||||
break;
|
||||
|
107
CCache.php
Normal file
107
CCache.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/**
|
||||
* Deal with the cache directory and cached items.
|
||||
*
|
||||
*/
|
||||
class CCache
|
||||
{
|
||||
/**
|
||||
* Path to the cache directory.
|
||||
*/
|
||||
private $path;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set the path to the cache dir which must exist.
|
||||
*
|
||||
* @param string path to the cache dir.
|
||||
*
|
||||
* @throws Exception when $path is not a directory.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDir($path)
|
||||
{
|
||||
if (!is_dir($path)) {
|
||||
throw new Exception("Cachedir is not a directory.");
|
||||
}
|
||||
|
||||
$this->path = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the path to the cache subdir and try to create it if its not there.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
* @param array $create default is to try to create the subdir
|
||||
*
|
||||
* @return string | boolean as real path to the subdir or
|
||||
* false if it does not exists
|
||||
*/
|
||||
public function getPathToSubdir($subdir, $create = true)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
if (is_dir($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
if ($create && is_writable($this->path)) {
|
||||
$path = $this->path . "/" . $subdir;
|
||||
|
||||
if (mkdir($path)) {
|
||||
return realpath($path);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get status of the cache subdir.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
*
|
||||
* @return string with status
|
||||
*/
|
||||
public function getStatusOfSubdir($subdir)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
$exists = is_dir($path);
|
||||
$res = $exists ? "exists" : "does not exist";
|
||||
|
||||
if ($exists) {
|
||||
$res .= is_writable($path) ? ", writable" : ", not writable";
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Remove the cache subdir.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
*
|
||||
* @return null | boolean true if success else false, null if no operation
|
||||
*/
|
||||
public function removeSubdir($subdir)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
if (is_dir($path)) {
|
||||
return rmdir($path);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
32
CImage.php
32
CImage.php
@@ -344,20 +344,27 @@ class CImage
|
||||
|
||||
|
||||
/**
|
||||
* Calculate target dimension for image when using fill-to-fit resize strategy.
|
||||
*/
|
||||
* Calculate target dimension for image when using fill-to-fit resize strategy.
|
||||
*/
|
||||
private $fillWidth;
|
||||
private $fillHeight;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Allow remote file download, default is to disallow remote file download.
|
||||
*/
|
||||
* Allow remote file download, default is to disallow remote file download.
|
||||
*/
|
||||
private $allowRemote = false;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Path to cache for remote download.
|
||||
*/
|
||||
private $remoteCache;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Pattern to recognize a remote file.
|
||||
*/
|
||||
@@ -512,13 +519,15 @@ class CImage
|
||||
* Allow or disallow remote image download.
|
||||
*
|
||||
* @param boolean $allow true or false to enable and disable.
|
||||
* @param string $cache path to cache dir.
|
||||
* @param string $pattern to use to detect if its a remote file.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRemoteDownload($allow, $pattern = null)
|
||||
public function setRemoteDownload($allow, $cache, $pattern = null)
|
||||
{
|
||||
$this->allowRemote = $allow;
|
||||
$this->remoteCache = $cache;
|
||||
$this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern;
|
||||
|
||||
$this->log(
|
||||
@@ -650,21 +659,12 @@ class CImage
|
||||
}
|
||||
|
||||
$remote = new CRemoteImage();
|
||||
$cache = $this->saveFolder . "/remote/";
|
||||
|
||||
if (!is_dir($cache)) {
|
||||
if (!is_writable($this->saveFolder)) {
|
||||
throw new Exception("Can not create remote cache, cachefolder not writable.");
|
||||
}
|
||||
mkdir($cache);
|
||||
$this->log("The remote cache does not exists, creating it.");
|
||||
}
|
||||
|
||||
if (!is_writable($cache)) {
|
||||
if (!is_writable($this->remoteCache)) {
|
||||
$this->log("The remote cache is not writable.");
|
||||
}
|
||||
|
||||
$remote->setCache($cache);
|
||||
$remote->setCache($this->remoteCache);
|
||||
$remote->useCache($this->useCache);
|
||||
$src = $remote->download($src);
|
||||
|
||||
|
@@ -101,7 +101,7 @@ class CRemoteImage
|
||||
*/
|
||||
public function setCache($path)
|
||||
{
|
||||
$this->saveFolder = $path;
|
||||
$this->saveFolder = rtrim($path, "/") . "/";
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -147,7 +147,12 @@ class CRemoteImage
|
||||
*/
|
||||
public function setHeaderFields()
|
||||
{
|
||||
$this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)");
|
||||
$cimageVersion = "CImage";
|
||||
if (defined("CIMAGE_USER_AGENT")) {
|
||||
$cimageVersion = CIMAGE_USER_AGENT;
|
||||
}
|
||||
|
||||
$this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)");
|
||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||
|
||||
if ($this->useCache) {
|
||||
|
@@ -46,14 +46,14 @@ There are several ways of installing. You either install the whole project which
|
||||
|
||||
The [sourcode is available on GitHub](https://github.com/mosbth/cimage). Clone, fork or [download as zip](https://github.com/mosbth/cimage/archive/master.zip).
|
||||
|
||||
**Latest stable version is v0.7.8 released 2015-12-06.**
|
||||
**Latest stable version is v0.7.9 released 2015-12-07.**
|
||||
|
||||
I prefer cloning like this. Do switch to the latest stable version.
|
||||
|
||||
```bash
|
||||
git clone git://github.com/mosbth/cimage.git
|
||||
cd cimage
|
||||
git checkout v0.7.8
|
||||
git checkout v0.7.9
|
||||
```
|
||||
|
||||
Make the cache-directory writable by the webserver.
|
||||
@@ -76,7 +76,7 @@ There are some all-included bundles of `img.php` that can be downloaded and used
|
||||
Dowload the version of your choice like this.
|
||||
|
||||
```bash
|
||||
wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.8/webroot/imgp.php
|
||||
wget https://raw.githubusercontent.com/mosbth/cimage/v0.7.9/webroot/imgp.php
|
||||
```
|
||||
|
||||
Open up the file in your editor and edit the array `$config`. Ensure that the paths to the image directory and the cache directory matches your environment, or create an own config-file for the script.
|
||||
|
16
REVISION.md
16
REVISION.md
@@ -5,6 +5,22 @@ Revision history
|
||||
[](https://scrutinizer-ci.com/g/mosbth/cimage/build-status/master)
|
||||
|
||||
|
||||
v0.7.10 (2016-04-01)
|
||||
-------------------------------------
|
||||
|
||||
* Add backup option for images `src-alt`, #141.
|
||||
* Add require of ext-gd in composer.json, #133.
|
||||
* Fix strict mode only reporting 404 when failure, #127.
|
||||
|
||||
|
||||
v0.7.9 (2015-12-07)
|
||||
-------------------------------------
|
||||
|
||||
* Strict mode only reporting 404 when failure, #127.
|
||||
* Added correct CImage version to remote agent string, #131.
|
||||
* Adding CCache to improve cache handling of caching for dummy, remote and srgb. #130.
|
||||
|
||||
|
||||
v0.7.8 (2015-12-06)
|
||||
-------------------------------------
|
||||
|
||||
|
@@ -39,6 +39,7 @@ $ECHO "\n CRemoteImage.php"
|
||||
$ECHO "\n CWhitelist.php"
|
||||
$ECHO "\n CAsciiArt.php"
|
||||
$ECHO "\n CImage.php"
|
||||
$ECHO "\n CCache.php"
|
||||
$ECHO "\n webroot/img.php"
|
||||
$ECHO "\n"
|
||||
$ECHO "\n'$TARGET_D' is for development mode."
|
||||
@@ -74,6 +75,9 @@ $ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||
tail -n +2 CImage.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||
$ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||
|
||||
tail -n +2 CCache.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||
$ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||
|
||||
tail -n +2 webroot/img.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||
$ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||
|
||||
|
0
cache/dummy/README.md
vendored
0
cache/dummy/README.md
vendored
@@ -18,7 +18,11 @@
|
||||
"docs": "http://dbwebb.se/opensource/cimage"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3"
|
||||
"php": ">=5.3",
|
||||
"ext-gd": "*"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-exif": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
@@ -26,7 +30,8 @@
|
||||
"CHttpGet.php",
|
||||
"CRemoteImage.php",
|
||||
"CWhitelist.php",
|
||||
"CAsciiArt.php"
|
||||
"CAsciiArt.php",
|
||||
"CCache.php"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
68
test/CCacheTest.php
Normal file
68
test/CCacheTest.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* A testclass
|
||||
*
|
||||
*/
|
||||
class CCacheTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Test
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetCacheDir()
|
||||
{
|
||||
$cache = new CCache();
|
||||
$cache->setDir(CACHE_PATH);
|
||||
|
||||
$exp = "exists, writable";
|
||||
$res = $cache->getStatusOfSubdir("");
|
||||
$this->assertEquals($exp, $res, "Status of cache dir missmatch.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test
|
||||
*
|
||||
* @expectedException Exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetWrongCacheDir()
|
||||
{
|
||||
$cache = new CCache();
|
||||
$cache->setDir(CACHE_PATH . "/NO_EXISTS");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateSubdir()
|
||||
{
|
||||
$cache = new CCache();
|
||||
$cache->setDir(CACHE_PATH);
|
||||
|
||||
$subdir = "__test__";
|
||||
$cache->removeSubdir($subdir);
|
||||
|
||||
$exp = "does not exist";
|
||||
$res = $cache->getStatusOfSubdir($subdir, false);
|
||||
$this->assertEquals($exp, $res, "Subdir should not be created.");
|
||||
|
||||
$res = $cache->getPathToSubdir($subdir);
|
||||
$exp = realpath(CACHE_PATH . "/$subdir");
|
||||
$this->assertEquals($exp, $res, "Subdir path missmatch.");
|
||||
|
||||
$exp = "exists, writable";
|
||||
$res = $cache->getStatusOfSubdir($subdir);
|
||||
$this->assertEquals($exp, $res, "Subdir should exist.");
|
||||
|
||||
$res = $cache->removeSubdir($subdir);
|
||||
$this->assertTrue($res, "Remove subdir.");
|
||||
}
|
||||
}
|
@@ -5,6 +5,57 @@
|
||||
*/
|
||||
class CImageDummyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const DUMMY = "__dummy__";
|
||||
private $cachepath;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Setup environment
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$cache = new CCache();
|
||||
$cache->setDir(CACHE_PATH);
|
||||
$this->cachepath = $cache->getPathToSubdir(self::DUMMY);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Clean up cache dir content.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function removeFilesInCacheDir()
|
||||
{
|
||||
$files = glob($this->cachepath . "/*");
|
||||
foreach ($files as $file) {
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Teardown environment
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function tearDown()
|
||||
{
|
||||
$cache = new CCache();
|
||||
$cache->setDir(CACHE_PATH);
|
||||
$this->removeFilesInCacheDir();
|
||||
$cache->removeSubdir(self::DUMMY);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Test
|
||||
*
|
||||
@@ -14,15 +65,15 @@ class CImageDummyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$img = new CImage();
|
||||
|
||||
$img->setSaveFolder(CACHE_PATH . "/dummy");
|
||||
$img->setSource('dummy', CACHE_PATH . "/dummy");
|
||||
$img->setSaveFolder($this->cachepath);
|
||||
$img->setSource(self::DUMMY, $this->cachepath);
|
||||
$img->createDummyImage();
|
||||
$img->generateFilename(null, false);
|
||||
$img->save(null, null, false);
|
||||
|
||||
$filename = $img->getTarget();
|
||||
|
||||
$this->assertEquals(basename($filename), "dummy_100_100", "Filename not as expected on dummy image.");
|
||||
$this->assertEquals(basename($filename), self::DUMMY . "_100_100", "Filename not as expected on dummy image.");
|
||||
}
|
||||
|
||||
|
||||
@@ -36,14 +87,14 @@ class CImageDummyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$img = new CImage();
|
||||
|
||||
$img->setSaveFolder(CACHE_PATH . "/dummy");
|
||||
$img->setSource('dummy', CACHE_PATH . "/dummy");
|
||||
$img->setSaveFolder($this->cachepath);
|
||||
$img->setSource(self::DUMMY, $this->cachepath);
|
||||
$img->createDummyImage(200, 400);
|
||||
$img->generateFilename(null, false);
|
||||
$img->save(null, null, false);
|
||||
|
||||
$filename = $img->getTarget();
|
||||
|
||||
$this->assertEquals(basename($filename), "dummy_200_400", "Filename not as expected on dummy image.");
|
||||
$this->assertEquals(basename($filename), self::DUMMY . "_200_400", "Filename not as expected on dummy image.");
|
||||
}
|
||||
}
|
||||
|
@@ -61,7 +61,7 @@ class CImageRemoteDownloadTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAllowRemoteDownloadDefaultPatternValid($source)
|
||||
{
|
||||
$img = new CImage();
|
||||
$img->setRemoteDownload(true);
|
||||
$img->setRemoteDownload(true, "");
|
||||
|
||||
$res = $img->isRemoteSource($source);
|
||||
$this->assertTrue($res, "Should be a valid remote source: '$source'.");
|
||||
@@ -79,7 +79,7 @@ class CImageRemoteDownloadTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAllowRemoteDownloadDefaultPatternInvalid($source)
|
||||
{
|
||||
$img = new CImage();
|
||||
$img->setRemoteDownload(true);
|
||||
$img->setRemoteDownload(true, "");
|
||||
|
||||
$res = $img->isRemoteSource($source);
|
||||
$this->assertFalse($res, "Should not be a valid remote source: '$source'.");
|
||||
|
@@ -38,7 +38,7 @@ class CImageSRGBTest extends \PHPUnit_Framework_TestCase
|
||||
$img = new CImage();
|
||||
|
||||
$filename = $img->convert2sRGBColorSpace(
|
||||
'car.png',
|
||||
'car.png',
|
||||
IMAGE_PATH,
|
||||
$this->cache,
|
||||
$this->srgbColorProfile
|
||||
@@ -63,8 +63,8 @@ class CImageSRGBTest extends \PHPUnit_Framework_TestCase
|
||||
$img = new CImage();
|
||||
|
||||
$filename = $img->convert2sRGBColorSpace(
|
||||
'car.jpg',
|
||||
IMAGE_PATH,
|
||||
'car.jpg',
|
||||
IMAGE_PATH,
|
||||
$this->cache,
|
||||
$this->srgbColorProfile
|
||||
);
|
||||
|
131
webroot/img.php
131
webroot/img.php
@@ -8,7 +8,10 @@
|
||||
*
|
||||
*/
|
||||
|
||||
$version = "v0.7.8 (2015-12-06)";
|
||||
$version = "v0.7.10 (2016-04-01)";
|
||||
|
||||
// For CRemoteImage
|
||||
define("CIMAGE_USER_AGENT", "CImage/$version");
|
||||
|
||||
|
||||
|
||||
@@ -27,17 +30,21 @@ function errorPage($msg, $type = 500)
|
||||
switch ($type) {
|
||||
case 403:
|
||||
$header = "403 Forbidden";
|
||||
break;
|
||||
break;
|
||||
case 404:
|
||||
$header = "404 Not Found";
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
$header = "500 Internal Server Error";
|
||||
}
|
||||
|
||||
if ($mode == "strict") {
|
||||
$header = "404 Not Found";
|
||||
}
|
||||
|
||||
header("HTTP/1.0 $header");
|
||||
|
||||
if ($mode == 'development') {
|
||||
if ($mode == "development") {
|
||||
die("[img.php] $msg");
|
||||
}
|
||||
|
||||
@@ -56,8 +63,9 @@ set_exception_handler(function ($exception) {
|
||||
. $exception->getMessage()
|
||||
. "</p><pre>"
|
||||
. $exception->getTraceAsString()
|
||||
. "</pre>"
|
||||
, 500);
|
||||
. "</pre>",
|
||||
500
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -256,7 +264,7 @@ $pwd = get(array('password', 'pwd'), null);
|
||||
// Check if passwords match, if configured to use passwords
|
||||
$passwordMatch = null;
|
||||
if ($pwd) {
|
||||
switch($pwdType) {
|
||||
switch ($pwdType) {
|
||||
case 'md5':
|
||||
$passwordMatch = ($pwdConfig === md5($pwd));
|
||||
break;
|
||||
@@ -345,6 +353,16 @@ $img->setVerbose($verbose || $verboseFile);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
*/
|
||||
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
||||
$cache = new CCache();
|
||||
$cache->setDir($cachePath);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Allow or disallow remote download of images from other servers.
|
||||
* Passwords apply if used.
|
||||
@@ -353,8 +371,10 @@ $img->setVerbose($verbose || $verboseFile);
|
||||
$allowRemote = getConfig('remote_allow', false);
|
||||
|
||||
if ($allowRemote && $passwordMatch !== false) {
|
||||
$cacheRemote = $cache->getPathToSubdir("remote");
|
||||
|
||||
$pattern = getConfig('remote_pattern', null);
|
||||
$img->setRemoteDownload($allowRemote, $pattern);
|
||||
$img->setRemoteDownload($allowRemote, $cacheRemote, $pattern);
|
||||
|
||||
$whitelist = getConfig('remote_whitelist', null);
|
||||
$img->setRemoteHostWhitelist($whitelist);
|
||||
@@ -389,18 +409,28 @@ if (isset($shortcut)
|
||||
$srcImage = urldecode(get('src'))
|
||||
or errorPage('Must set src-attribute.', 404);
|
||||
|
||||
// Get settings for src-alt as backup image
|
||||
$srcAltImage = urldecode(get('src-alt', null));
|
||||
$srcAltConfig = getConfig('src_alt', null);
|
||||
if (empty($srcAltImage)) {
|
||||
$srcAltImage = $srcAltConfig;
|
||||
}
|
||||
|
||||
// Check for valid/invalid characters
|
||||
$imagePath = getConfig('image_path', __DIR__ . '/img/');
|
||||
$imagePathConstraint = getConfig('image_path_constraint', true);
|
||||
$validFilename = getConfig('valid_filename', '#^[a-z0-9A-Z-/_ \.:]+$#');
|
||||
|
||||
// Source is remote
|
||||
$remoteSource = false;
|
||||
|
||||
// Dummy image feature
|
||||
$dummyEnabled = getConfig('dummy_enabled', true);
|
||||
$dummyFilename = getConfig('dummy_filename', 'dummy');
|
||||
$dummyImage = false;
|
||||
|
||||
preg_match($validFilename, $srcImage)
|
||||
or errorPage('Filename contains invalid characters.', 404);
|
||||
or errorPage('Source filename contains invalid characters.', 404);
|
||||
|
||||
if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
|
||||
@@ -410,24 +440,47 @@ if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
} elseif ($allowRemote && $img->isRemoteSource($srcImage)) {
|
||||
|
||||
// If source is a remote file, ignore local file checks.
|
||||
$remoteSource = true;
|
||||
|
||||
} elseif ($imagePathConstraint) {
|
||||
} else {
|
||||
|
||||
// Check that the image is a file below the directory 'image_path'.
|
||||
// Check if file exists on disk or try using src-alt
|
||||
$pathToImage = realpath($imagePath . $srcImage);
|
||||
$imageDir = realpath($imagePath);
|
||||
|
||||
is_file($pathToImage)
|
||||
or errorPage(
|
||||
'Source image is not a valid file, check the filename and that a
|
||||
matching file exists on the filesystem.'
|
||||
, 404);
|
||||
if (!is_file($pathToImage) && !empty($srcAltImage)) {
|
||||
// Try using the src-alt instead
|
||||
$srcImage = $srcAltImage;
|
||||
$pathToImage = realpath($imagePath . $srcImage);
|
||||
|
||||
preg_match($validFilename, $srcImage)
|
||||
or errorPage('Source (alt) filename contains invalid characters.', 404);
|
||||
|
||||
if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
// Check if src-alt is the dummy image
|
||||
$dummyImage = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$dummyImage) {
|
||||
is_file($pathToImage)
|
||||
or errorPage(
|
||||
'Source image is not a valid file, check the filename and that a
|
||||
matching file exists on the filesystem.',
|
||||
404
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($imagePathConstraint && !$dummyImage && !$remoteSource) {
|
||||
// Check that the image is a file below the directory 'image_path'.
|
||||
$imageDir = realpath($imagePath);
|
||||
|
||||
substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0
|
||||
or errorPage(
|
||||
'Security constraint: Source image is not below the directory "image_path"
|
||||
as specified in the config file img_config.php.'
|
||||
, 404);
|
||||
as specified in the config file img_config.php.',
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
verbose("src = $srcImage");
|
||||
@@ -940,14 +993,7 @@ verbose("alias = $alias");
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
*/
|
||||
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
* Add cache control HTTP header.
|
||||
*/
|
||||
$cacheControl = getConfig('cache_control', null);
|
||||
|
||||
@@ -961,11 +1007,8 @@ if ($cacheControl) {
|
||||
/**
|
||||
* Prepare a dummy image and use it as source image.
|
||||
*/
|
||||
$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename);
|
||||
|
||||
if ($dummyImage === true) {
|
||||
is_writable($dummyDir)
|
||||
or verbose("dummy dir not writable = $dummyDir");
|
||||
$dummyDir = $cache->getPathToSubdir("dummy");
|
||||
|
||||
$img->setSaveFolder($dummyDir)
|
||||
->setSource($dummyFilename, $dummyDir)
|
||||
@@ -993,24 +1036,16 @@ if ($dummyImage === true) {
|
||||
/**
|
||||
* Prepare a sRGB version of the image and use it as source image.
|
||||
*/
|
||||
$srgbDirName = "/srgb";
|
||||
$srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName));
|
||||
$srgbDefault = getConfig('srgb_default', false);
|
||||
$srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc');
|
||||
$srgb = getDefined('srgb', true, null);
|
||||
|
||||
if ($srgb || $srgbDefault) {
|
||||
|
||||
if (!is_writable($srgbDir)) {
|
||||
if (is_writable($cachePath)) {
|
||||
mkdir($srgbDir);
|
||||
}
|
||||
}
|
||||
|
||||
$filename = $img->convert2sRGBColorSpace(
|
||||
$srcImage,
|
||||
$imagePath,
|
||||
$srgbDir,
|
||||
$cache->getPathToSubdir("srgb"),
|
||||
$srgbColorProfile,
|
||||
$useCache
|
||||
);
|
||||
@@ -1034,9 +1069,19 @@ if ($status) {
|
||||
$text .= "PHP version = " . PHP_VERSION . "\n";
|
||||
$text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
|
||||
$text .= "Allow remote images = $allowRemote\n";
|
||||
$text .= "Cache writable = " . is_writable($cachePath) . "\n";
|
||||
$text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n";
|
||||
$text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("");
|
||||
$text .= "Cache $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("remote");
|
||||
$text .= "Cache remote $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("dummy");
|
||||
$text .= "Cache dummy $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("srgb");
|
||||
$text .= "Cache srgb $res\n";
|
||||
|
||||
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
|
||||
|
||||
$no = extension_loaded('exif') ? null : 'NOT';
|
||||
|
BIN
webroot/img/planet.gif
Normal file
BIN
webroot/img/planet.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.6 KiB |
@@ -13,8 +13,9 @@ return array(
|
||||
* Default values:
|
||||
* mode: 'production'
|
||||
*/
|
||||
//'mode' => 'production', // 'development', 'strict'
|
||||
//'mode' => 'development', // 'development', 'strict'
|
||||
//'mode' => 'production',
|
||||
//'mode' => 'development',
|
||||
//'mode' => 'strict',
|
||||
|
||||
|
||||
|
||||
@@ -97,6 +98,20 @@ return array(
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Use backup image if src-image is not found on disk. The backup image
|
||||
* is only available for local images and based on wether the original
|
||||
* image is found on disk or not. The backup image must be a local image
|
||||
* or the dummy image.
|
||||
*
|
||||
* Default value:
|
||||
* src_alt: null //disabled by default
|
||||
*/
|
||||
//'src_alt' => 'car.png',
|
||||
//'src_alt' => 'dummy',
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A regexp for validating characters in the image or alias filename.
|
||||
*
|
||||
@@ -124,17 +139,15 @@ return array(
|
||||
|
||||
/**
|
||||
* Convert the image to srgb before processing. Saves the converted
|
||||
* image in the sub cache dir. This option is default false but can
|
||||
* image in a cache subdir 'srgb'. This option is default false but can
|
||||
* be changed to default true to do this conversion for all images.
|
||||
* This option requires PHP extension imagick and will silently fail
|
||||
* if that is not installed.
|
||||
*
|
||||
* Default value:
|
||||
* srgb_dir: $cachePath/srgb
|
||||
* srgb_default: false
|
||||
* srgb_colorprofile: __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc'
|
||||
*/
|
||||
//'srgb_dir' => __DIR__ . '/../cache/srgb/',
|
||||
//'srgb_default' => false,
|
||||
//'srgb_colorprofile' => __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc',
|
||||
|
||||
@@ -170,30 +183,27 @@ return array(
|
||||
|
||||
/**
|
||||
* The name representing a dummy image which is automatically created
|
||||
* and stored at the defined path. The dummy image can then be used
|
||||
* inplace of an original image as a placeholder.
|
||||
* The dummy_dir must be writable and it defaults to a subdir of the
|
||||
* cache directory.
|
||||
* Write protect the dummy_dir to prevent creation of new dummy images,
|
||||
* but continue to use the existing ones.
|
||||
* and stored as a image in the dir CACHE_PATH/dummy. The dummy image
|
||||
* can then be used as a placeholder image.
|
||||
* The dir CACHE_PATH/dummy is automatically created when needed.
|
||||
* Write protect the CACHE_PATH/dummy to prevent creation of new
|
||||
* dummy images, but continue to use the existing ones.
|
||||
*
|
||||
* Default value:
|
||||
* dummy_enabled: true as default, disable dummy feature by setting
|
||||
* to false.
|
||||
* dummy_filename: 'dummy' use this as ?src=dummy to create a dummy image.
|
||||
* dummy_dir: Defaults to subdirectory of 'cache_path',
|
||||
* named the same as 'dummy_filename'
|
||||
*/
|
||||
//'dummy_enabled' => true,
|
||||
//'dummy_filename' => 'dummy',
|
||||
//'dummy_dir' => 'some writable directory',
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check that the imagefile is a file below 'image_path' using realpath().
|
||||
* Security constraint to avoid reaching images outside image_path.
|
||||
* This means that symbolic links to images outside the image_path will fail.
|
||||
* This means that symbolic links to images outside the image_path will
|
||||
* fail.
|
||||
*
|
||||
* Default value:
|
||||
* image_path_constraint: true
|
||||
|
283
webroot/imgd.php
283
webroot/imgd.php
@@ -430,7 +430,7 @@ class CRemoteImage
|
||||
*/
|
||||
public function setCache($path)
|
||||
{
|
||||
$this->saveFolder = $path;
|
||||
$this->saveFolder = rtrim($path, "/") . "/";
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -476,7 +476,12 @@ class CRemoteImage
|
||||
*/
|
||||
public function setHeaderFields()
|
||||
{
|
||||
$this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)");
|
||||
$cimageVersion = "CImage";
|
||||
if (defined("CIMAGE_USER_AGENT")) {
|
||||
$cimageVersion = CIMAGE_USER_AGENT;
|
||||
}
|
||||
|
||||
$this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)");
|
||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||
|
||||
if ($this->useCache) {
|
||||
@@ -880,7 +885,7 @@ class CAsciiArt
|
||||
*/
|
||||
public function getLuminance($red, $green, $blue)
|
||||
{
|
||||
switch($this->luminanceStrategy) {
|
||||
switch ($this->luminanceStrategy) {
|
||||
case 1:
|
||||
$luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255;
|
||||
break;
|
||||
@@ -1263,20 +1268,27 @@ class CImage
|
||||
|
||||
|
||||
/**
|
||||
* Calculate target dimension for image when using fill-to-fit resize strategy.
|
||||
*/
|
||||
* Calculate target dimension for image when using fill-to-fit resize strategy.
|
||||
*/
|
||||
private $fillWidth;
|
||||
private $fillHeight;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Allow remote file download, default is to disallow remote file download.
|
||||
*/
|
||||
* Allow remote file download, default is to disallow remote file download.
|
||||
*/
|
||||
private $allowRemote = false;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Path to cache for remote download.
|
||||
*/
|
||||
private $remoteCache;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Pattern to recognize a remote file.
|
||||
*/
|
||||
@@ -1431,13 +1443,15 @@ class CImage
|
||||
* Allow or disallow remote image download.
|
||||
*
|
||||
* @param boolean $allow true or false to enable and disable.
|
||||
* @param string $cache path to cache dir.
|
||||
* @param string $pattern to use to detect if its a remote file.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRemoteDownload($allow, $pattern = null)
|
||||
public function setRemoteDownload($allow, $cache, $pattern = null)
|
||||
{
|
||||
$this->allowRemote = $allow;
|
||||
$this->remoteCache = $cache;
|
||||
$this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern;
|
||||
|
||||
$this->log(
|
||||
@@ -1569,21 +1583,12 @@ class CImage
|
||||
}
|
||||
|
||||
$remote = new CRemoteImage();
|
||||
$cache = $this->saveFolder . "/remote/";
|
||||
|
||||
if (!is_dir($cache)) {
|
||||
if (!is_writable($this->saveFolder)) {
|
||||
throw new Exception("Can not create remote cache, cachefolder not writable.");
|
||||
}
|
||||
mkdir($cache);
|
||||
$this->log("The remote cache does not exists, creating it.");
|
||||
}
|
||||
|
||||
if (!is_writable($cache)) {
|
||||
if (!is_writable($this->remoteCache)) {
|
||||
$this->log("The remote cache is not writable.");
|
||||
}
|
||||
|
||||
$remote->setCache($cache);
|
||||
$remote->setCache($this->remoteCache);
|
||||
$remote->useCache($this->useCache);
|
||||
$src = $remote->download($src);
|
||||
|
||||
@@ -3694,6 +3699,115 @@ EOD;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deal with the cache directory and cached items.
|
||||
*
|
||||
*/
|
||||
class CCache
|
||||
{
|
||||
/**
|
||||
* Path to the cache directory.
|
||||
*/
|
||||
private $path;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set the path to the cache dir which must exist.
|
||||
*
|
||||
* @param string path to the cache dir.
|
||||
*
|
||||
* @throws Exception when $path is not a directory.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDir($path)
|
||||
{
|
||||
if (!is_dir($path)) {
|
||||
throw new Exception("Cachedir is not a directory.");
|
||||
}
|
||||
|
||||
$this->path = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the path to the cache subdir and try to create it if its not there.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
* @param array $create default is to try to create the subdir
|
||||
*
|
||||
* @return string | boolean as real path to the subdir or
|
||||
* false if it does not exists
|
||||
*/
|
||||
public function getPathToSubdir($subdir, $create = true)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
if (is_dir($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
if ($create && is_writable($this->path)) {
|
||||
$path = $this->path . "/" . $subdir;
|
||||
|
||||
if (mkdir($path)) {
|
||||
return realpath($path);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get status of the cache subdir.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
*
|
||||
* @return string with status
|
||||
*/
|
||||
public function getStatusOfSubdir($subdir)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
$exists = is_dir($path);
|
||||
$res = $exists ? "exists" : "does not exist";
|
||||
|
||||
if ($exists) {
|
||||
$res .= is_writable($path) ? ", writable" : ", not writable";
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Remove the cache subdir.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
*
|
||||
* @return null | boolean true if success else false, null if no operation
|
||||
*/
|
||||
public function removeSubdir($subdir)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
if (is_dir($path)) {
|
||||
return rmdir($path);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Resize and crop images on the fly, store generated images in a cache.
|
||||
*
|
||||
@@ -3703,7 +3817,10 @@ EOD;
|
||||
*
|
||||
*/
|
||||
|
||||
$version = "v0.7.8 (2015-12-06)";
|
||||
$version = "v0.7.10 (2016-04-01)";
|
||||
|
||||
// For CRemoteImage
|
||||
define("CIMAGE_USER_AGENT", "CImage/$version");
|
||||
|
||||
|
||||
|
||||
@@ -3722,17 +3839,21 @@ function errorPage($msg, $type = 500)
|
||||
switch ($type) {
|
||||
case 403:
|
||||
$header = "403 Forbidden";
|
||||
break;
|
||||
break;
|
||||
case 404:
|
||||
$header = "404 Not Found";
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
$header = "500 Internal Server Error";
|
||||
}
|
||||
|
||||
if ($mode == "strict") {
|
||||
$header = "404 Not Found";
|
||||
}
|
||||
|
||||
header("HTTP/1.0 $header");
|
||||
|
||||
if ($mode == 'development') {
|
||||
if ($mode == "development") {
|
||||
die("[img.php] $msg");
|
||||
}
|
||||
|
||||
@@ -3751,8 +3872,9 @@ set_exception_handler(function ($exception) {
|
||||
. $exception->getMessage()
|
||||
. "</p><pre>"
|
||||
. $exception->getTraceAsString()
|
||||
. "</pre>"
|
||||
, 500);
|
||||
. "</pre>",
|
||||
500
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -3951,7 +4073,7 @@ $pwd = get(array('password', 'pwd'), null);
|
||||
// Check if passwords match, if configured to use passwords
|
||||
$passwordMatch = null;
|
||||
if ($pwd) {
|
||||
switch($pwdType) {
|
||||
switch ($pwdType) {
|
||||
case 'md5':
|
||||
$passwordMatch = ($pwdConfig === md5($pwd));
|
||||
break;
|
||||
@@ -4040,6 +4162,16 @@ $img->setVerbose($verbose || $verboseFile);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
*/
|
||||
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
||||
$cache = new CCache();
|
||||
$cache->setDir($cachePath);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Allow or disallow remote download of images from other servers.
|
||||
* Passwords apply if used.
|
||||
@@ -4048,8 +4180,10 @@ $img->setVerbose($verbose || $verboseFile);
|
||||
$allowRemote = getConfig('remote_allow', false);
|
||||
|
||||
if ($allowRemote && $passwordMatch !== false) {
|
||||
$cacheRemote = $cache->getPathToSubdir("remote");
|
||||
|
||||
$pattern = getConfig('remote_pattern', null);
|
||||
$img->setRemoteDownload($allowRemote, $pattern);
|
||||
$img->setRemoteDownload($allowRemote, $cacheRemote, $pattern);
|
||||
|
||||
$whitelist = getConfig('remote_whitelist', null);
|
||||
$img->setRemoteHostWhitelist($whitelist);
|
||||
@@ -4084,18 +4218,28 @@ if (isset($shortcut)
|
||||
$srcImage = urldecode(get('src'))
|
||||
or errorPage('Must set src-attribute.', 404);
|
||||
|
||||
// Get settings for src-alt as backup image
|
||||
$srcAltImage = urldecode(get('src-alt', null));
|
||||
$srcAltConfig = getConfig('src_alt', null);
|
||||
if (empty($srcAltImage)) {
|
||||
$srcAltImage = $srcAltConfig;
|
||||
}
|
||||
|
||||
// Check for valid/invalid characters
|
||||
$imagePath = getConfig('image_path', __DIR__ . '/img/');
|
||||
$imagePathConstraint = getConfig('image_path_constraint', true);
|
||||
$validFilename = getConfig('valid_filename', '#^[a-z0-9A-Z-/_ \.:]+$#');
|
||||
|
||||
// Source is remote
|
||||
$remoteSource = false;
|
||||
|
||||
// Dummy image feature
|
||||
$dummyEnabled = getConfig('dummy_enabled', true);
|
||||
$dummyFilename = getConfig('dummy_filename', 'dummy');
|
||||
$dummyImage = false;
|
||||
|
||||
preg_match($validFilename, $srcImage)
|
||||
or errorPage('Filename contains invalid characters.', 404);
|
||||
or errorPage('Source filename contains invalid characters.', 404);
|
||||
|
||||
if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
|
||||
@@ -4105,24 +4249,47 @@ if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
} elseif ($allowRemote && $img->isRemoteSource($srcImage)) {
|
||||
|
||||
// If source is a remote file, ignore local file checks.
|
||||
$remoteSource = true;
|
||||
|
||||
} elseif ($imagePathConstraint) {
|
||||
} else {
|
||||
|
||||
// Check that the image is a file below the directory 'image_path'.
|
||||
// Check if file exists on disk or try using src-alt
|
||||
$pathToImage = realpath($imagePath . $srcImage);
|
||||
$imageDir = realpath($imagePath);
|
||||
|
||||
is_file($pathToImage)
|
||||
or errorPage(
|
||||
'Source image is not a valid file, check the filename and that a
|
||||
matching file exists on the filesystem.'
|
||||
, 404);
|
||||
if (!is_file($pathToImage) && !empty($srcAltImage)) {
|
||||
// Try using the src-alt instead
|
||||
$srcImage = $srcAltImage;
|
||||
$pathToImage = realpath($imagePath . $srcImage);
|
||||
|
||||
preg_match($validFilename, $srcImage)
|
||||
or errorPage('Source (alt) filename contains invalid characters.', 404);
|
||||
|
||||
if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
// Check if src-alt is the dummy image
|
||||
$dummyImage = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$dummyImage) {
|
||||
is_file($pathToImage)
|
||||
or errorPage(
|
||||
'Source image is not a valid file, check the filename and that a
|
||||
matching file exists on the filesystem.',
|
||||
404
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($imagePathConstraint && !$dummyImage && !$remoteSource) {
|
||||
// Check that the image is a file below the directory 'image_path'.
|
||||
$imageDir = realpath($imagePath);
|
||||
|
||||
substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0
|
||||
or errorPage(
|
||||
'Security constraint: Source image is not below the directory "image_path"
|
||||
as specified in the config file img_config.php.'
|
||||
, 404);
|
||||
as specified in the config file img_config.php.',
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
verbose("src = $srcImage");
|
||||
@@ -4635,14 +4802,7 @@ verbose("alias = $alias");
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
*/
|
||||
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
* Add cache control HTTP header.
|
||||
*/
|
||||
$cacheControl = getConfig('cache_control', null);
|
||||
|
||||
@@ -4656,11 +4816,8 @@ if ($cacheControl) {
|
||||
/**
|
||||
* Prepare a dummy image and use it as source image.
|
||||
*/
|
||||
$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename);
|
||||
|
||||
if ($dummyImage === true) {
|
||||
is_writable($dummyDir)
|
||||
or verbose("dummy dir not writable = $dummyDir");
|
||||
$dummyDir = $cache->getPathToSubdir("dummy");
|
||||
|
||||
$img->setSaveFolder($dummyDir)
|
||||
->setSource($dummyFilename, $dummyDir)
|
||||
@@ -4688,24 +4845,16 @@ if ($dummyImage === true) {
|
||||
/**
|
||||
* Prepare a sRGB version of the image and use it as source image.
|
||||
*/
|
||||
$srgbDirName = "/srgb";
|
||||
$srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName));
|
||||
$srgbDefault = getConfig('srgb_default', false);
|
||||
$srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc');
|
||||
$srgb = getDefined('srgb', true, null);
|
||||
|
||||
if ($srgb || $srgbDefault) {
|
||||
|
||||
if (!is_writable($srgbDir)) {
|
||||
if (is_writable($cachePath)) {
|
||||
mkdir($srgbDir);
|
||||
}
|
||||
}
|
||||
|
||||
$filename = $img->convert2sRGBColorSpace(
|
||||
$srcImage,
|
||||
$imagePath,
|
||||
$srgbDir,
|
||||
$cache->getPathToSubdir("srgb"),
|
||||
$srgbColorProfile,
|
||||
$useCache
|
||||
);
|
||||
@@ -4729,9 +4878,19 @@ if ($status) {
|
||||
$text .= "PHP version = " . PHP_VERSION . "\n";
|
||||
$text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
|
||||
$text .= "Allow remote images = $allowRemote\n";
|
||||
$text .= "Cache writable = " . is_writable($cachePath) . "\n";
|
||||
$text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n";
|
||||
$text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("");
|
||||
$text .= "Cache $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("remote");
|
||||
$text .= "Cache remote $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("dummy");
|
||||
$text .= "Cache dummy $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("srgb");
|
||||
$text .= "Cache srgb $res\n";
|
||||
|
||||
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
|
||||
|
||||
$no = extension_loaded('exif') ? null : 'NOT';
|
||||
|
283
webroot/imgp.php
283
webroot/imgp.php
@@ -430,7 +430,7 @@ class CRemoteImage
|
||||
*/
|
||||
public function setCache($path)
|
||||
{
|
||||
$this->saveFolder = $path;
|
||||
$this->saveFolder = rtrim($path, "/") . "/";
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -476,7 +476,12 @@ class CRemoteImage
|
||||
*/
|
||||
public function setHeaderFields()
|
||||
{
|
||||
$this->http->setHeader("User-Agent", "CImage/0.7.2 (PHP/". phpversion() . " cURL)");
|
||||
$cimageVersion = "CImage";
|
||||
if (defined("CIMAGE_USER_AGENT")) {
|
||||
$cimageVersion = CIMAGE_USER_AGENT;
|
||||
}
|
||||
|
||||
$this->http->setHeader("User-Agent", "$cimageVersion (PHP/". phpversion() . " cURL)");
|
||||
$this->http->setHeader("Accept", "image/jpeg,image/png,image/gif");
|
||||
|
||||
if ($this->useCache) {
|
||||
@@ -880,7 +885,7 @@ class CAsciiArt
|
||||
*/
|
||||
public function getLuminance($red, $green, $blue)
|
||||
{
|
||||
switch($this->luminanceStrategy) {
|
||||
switch ($this->luminanceStrategy) {
|
||||
case 1:
|
||||
$luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255;
|
||||
break;
|
||||
@@ -1263,20 +1268,27 @@ class CImage
|
||||
|
||||
|
||||
/**
|
||||
* Calculate target dimension for image when using fill-to-fit resize strategy.
|
||||
*/
|
||||
* Calculate target dimension for image when using fill-to-fit resize strategy.
|
||||
*/
|
||||
private $fillWidth;
|
||||
private $fillHeight;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Allow remote file download, default is to disallow remote file download.
|
||||
*/
|
||||
* Allow remote file download, default is to disallow remote file download.
|
||||
*/
|
||||
private $allowRemote = false;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Path to cache for remote download.
|
||||
*/
|
||||
private $remoteCache;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Pattern to recognize a remote file.
|
||||
*/
|
||||
@@ -1431,13 +1443,15 @@ class CImage
|
||||
* Allow or disallow remote image download.
|
||||
*
|
||||
* @param boolean $allow true or false to enable and disable.
|
||||
* @param string $cache path to cache dir.
|
||||
* @param string $pattern to use to detect if its a remote file.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setRemoteDownload($allow, $pattern = null)
|
||||
public function setRemoteDownload($allow, $cache, $pattern = null)
|
||||
{
|
||||
$this->allowRemote = $allow;
|
||||
$this->remoteCache = $cache;
|
||||
$this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern;
|
||||
|
||||
$this->log(
|
||||
@@ -1569,21 +1583,12 @@ class CImage
|
||||
}
|
||||
|
||||
$remote = new CRemoteImage();
|
||||
$cache = $this->saveFolder . "/remote/";
|
||||
|
||||
if (!is_dir($cache)) {
|
||||
if (!is_writable($this->saveFolder)) {
|
||||
throw new Exception("Can not create remote cache, cachefolder not writable.");
|
||||
}
|
||||
mkdir($cache);
|
||||
$this->log("The remote cache does not exists, creating it.");
|
||||
}
|
||||
|
||||
if (!is_writable($cache)) {
|
||||
if (!is_writable($this->remoteCache)) {
|
||||
$this->log("The remote cache is not writable.");
|
||||
}
|
||||
|
||||
$remote->setCache($cache);
|
||||
$remote->setCache($this->remoteCache);
|
||||
$remote->useCache($this->useCache);
|
||||
$src = $remote->download($src);
|
||||
|
||||
@@ -3694,6 +3699,115 @@ EOD;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deal with the cache directory and cached items.
|
||||
*
|
||||
*/
|
||||
class CCache
|
||||
{
|
||||
/**
|
||||
* Path to the cache directory.
|
||||
*/
|
||||
private $path;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set the path to the cache dir which must exist.
|
||||
*
|
||||
* @param string path to the cache dir.
|
||||
*
|
||||
* @throws Exception when $path is not a directory.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDir($path)
|
||||
{
|
||||
if (!is_dir($path)) {
|
||||
throw new Exception("Cachedir is not a directory.");
|
||||
}
|
||||
|
||||
$this->path = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the path to the cache subdir and try to create it if its not there.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
* @param array $create default is to try to create the subdir
|
||||
*
|
||||
* @return string | boolean as real path to the subdir or
|
||||
* false if it does not exists
|
||||
*/
|
||||
public function getPathToSubdir($subdir, $create = true)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
if (is_dir($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
if ($create && is_writable($this->path)) {
|
||||
$path = $this->path . "/" . $subdir;
|
||||
|
||||
if (mkdir($path)) {
|
||||
return realpath($path);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get status of the cache subdir.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
*
|
||||
* @return string with status
|
||||
*/
|
||||
public function getStatusOfSubdir($subdir)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
$exists = is_dir($path);
|
||||
$res = $exists ? "exists" : "does not exist";
|
||||
|
||||
if ($exists) {
|
||||
$res .= is_writable($path) ? ", writable" : ", not writable";
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Remove the cache subdir.
|
||||
*
|
||||
* @param string $subdir name of subdir
|
||||
*
|
||||
* @return null | boolean true if success else false, null if no operation
|
||||
*/
|
||||
public function removeSubdir($subdir)
|
||||
{
|
||||
$path = realpath($this->path . "/" . $subdir);
|
||||
|
||||
if (is_dir($path)) {
|
||||
return rmdir($path);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Resize and crop images on the fly, store generated images in a cache.
|
||||
*
|
||||
@@ -3703,7 +3817,10 @@ EOD;
|
||||
*
|
||||
*/
|
||||
|
||||
$version = "v0.7.8 (2015-12-06)";
|
||||
$version = "v0.7.10 (2016-04-01)";
|
||||
|
||||
// For CRemoteImage
|
||||
define("CIMAGE_USER_AGENT", "CImage/$version");
|
||||
|
||||
|
||||
|
||||
@@ -3722,17 +3839,21 @@ function errorPage($msg, $type = 500)
|
||||
switch ($type) {
|
||||
case 403:
|
||||
$header = "403 Forbidden";
|
||||
break;
|
||||
break;
|
||||
case 404:
|
||||
$header = "404 Not Found";
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
$header = "500 Internal Server Error";
|
||||
}
|
||||
|
||||
if ($mode == "strict") {
|
||||
$header = "404 Not Found";
|
||||
}
|
||||
|
||||
header("HTTP/1.0 $header");
|
||||
|
||||
if ($mode == 'development') {
|
||||
if ($mode == "development") {
|
||||
die("[img.php] $msg");
|
||||
}
|
||||
|
||||
@@ -3751,8 +3872,9 @@ set_exception_handler(function ($exception) {
|
||||
. $exception->getMessage()
|
||||
. "</p><pre>"
|
||||
. $exception->getTraceAsString()
|
||||
. "</pre>"
|
||||
, 500);
|
||||
. "</pre>",
|
||||
500
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -3951,7 +4073,7 @@ $pwd = get(array('password', 'pwd'), null);
|
||||
// Check if passwords match, if configured to use passwords
|
||||
$passwordMatch = null;
|
||||
if ($pwd) {
|
||||
switch($pwdType) {
|
||||
switch ($pwdType) {
|
||||
case 'md5':
|
||||
$passwordMatch = ($pwdConfig === md5($pwd));
|
||||
break;
|
||||
@@ -4040,6 +4162,16 @@ $img->setVerbose($verbose || $verboseFile);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
*/
|
||||
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
||||
$cache = new CCache();
|
||||
$cache->setDir($cachePath);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Allow or disallow remote download of images from other servers.
|
||||
* Passwords apply if used.
|
||||
@@ -4048,8 +4180,10 @@ $img->setVerbose($verbose || $verboseFile);
|
||||
$allowRemote = getConfig('remote_allow', false);
|
||||
|
||||
if ($allowRemote && $passwordMatch !== false) {
|
||||
$cacheRemote = $cache->getPathToSubdir("remote");
|
||||
|
||||
$pattern = getConfig('remote_pattern', null);
|
||||
$img->setRemoteDownload($allowRemote, $pattern);
|
||||
$img->setRemoteDownload($allowRemote, $cacheRemote, $pattern);
|
||||
|
||||
$whitelist = getConfig('remote_whitelist', null);
|
||||
$img->setRemoteHostWhitelist($whitelist);
|
||||
@@ -4084,18 +4218,28 @@ if (isset($shortcut)
|
||||
$srcImage = urldecode(get('src'))
|
||||
or errorPage('Must set src-attribute.', 404);
|
||||
|
||||
// Get settings for src-alt as backup image
|
||||
$srcAltImage = urldecode(get('src-alt', null));
|
||||
$srcAltConfig = getConfig('src_alt', null);
|
||||
if (empty($srcAltImage)) {
|
||||
$srcAltImage = $srcAltConfig;
|
||||
}
|
||||
|
||||
// Check for valid/invalid characters
|
||||
$imagePath = getConfig('image_path', __DIR__ . '/img/');
|
||||
$imagePathConstraint = getConfig('image_path_constraint', true);
|
||||
$validFilename = getConfig('valid_filename', '#^[a-z0-9A-Z-/_ \.:]+$#');
|
||||
|
||||
// Source is remote
|
||||
$remoteSource = false;
|
||||
|
||||
// Dummy image feature
|
||||
$dummyEnabled = getConfig('dummy_enabled', true);
|
||||
$dummyFilename = getConfig('dummy_filename', 'dummy');
|
||||
$dummyImage = false;
|
||||
|
||||
preg_match($validFilename, $srcImage)
|
||||
or errorPage('Filename contains invalid characters.', 404);
|
||||
or errorPage('Source filename contains invalid characters.', 404);
|
||||
|
||||
if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
|
||||
@@ -4105,24 +4249,47 @@ if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
} elseif ($allowRemote && $img->isRemoteSource($srcImage)) {
|
||||
|
||||
// If source is a remote file, ignore local file checks.
|
||||
$remoteSource = true;
|
||||
|
||||
} elseif ($imagePathConstraint) {
|
||||
} else {
|
||||
|
||||
// Check that the image is a file below the directory 'image_path'.
|
||||
// Check if file exists on disk or try using src-alt
|
||||
$pathToImage = realpath($imagePath . $srcImage);
|
||||
$imageDir = realpath($imagePath);
|
||||
|
||||
is_file($pathToImage)
|
||||
or errorPage(
|
||||
'Source image is not a valid file, check the filename and that a
|
||||
matching file exists on the filesystem.'
|
||||
, 404);
|
||||
if (!is_file($pathToImage) && !empty($srcAltImage)) {
|
||||
// Try using the src-alt instead
|
||||
$srcImage = $srcAltImage;
|
||||
$pathToImage = realpath($imagePath . $srcImage);
|
||||
|
||||
preg_match($validFilename, $srcImage)
|
||||
or errorPage('Source (alt) filename contains invalid characters.', 404);
|
||||
|
||||
if ($dummyEnabled && $srcImage === $dummyFilename) {
|
||||
// Check if src-alt is the dummy image
|
||||
$dummyImage = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$dummyImage) {
|
||||
is_file($pathToImage)
|
||||
or errorPage(
|
||||
'Source image is not a valid file, check the filename and that a
|
||||
matching file exists on the filesystem.',
|
||||
404
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($imagePathConstraint && !$dummyImage && !$remoteSource) {
|
||||
// Check that the image is a file below the directory 'image_path'.
|
||||
$imageDir = realpath($imagePath);
|
||||
|
||||
substr_compare($imageDir, $pathToImage, 0, strlen($imageDir)) == 0
|
||||
or errorPage(
|
||||
'Security constraint: Source image is not below the directory "image_path"
|
||||
as specified in the config file img_config.php.'
|
||||
, 404);
|
||||
as specified in the config file img_config.php.',
|
||||
404
|
||||
);
|
||||
}
|
||||
|
||||
verbose("src = $srcImage");
|
||||
@@ -4635,14 +4802,7 @@ verbose("alias = $alias");
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
*/
|
||||
$cachePath = getConfig('cache_path', __DIR__ . '/../cache/');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the cachepath from config.
|
||||
* Add cache control HTTP header.
|
||||
*/
|
||||
$cacheControl = getConfig('cache_control', null);
|
||||
|
||||
@@ -4656,11 +4816,8 @@ if ($cacheControl) {
|
||||
/**
|
||||
* Prepare a dummy image and use it as source image.
|
||||
*/
|
||||
$dummyDir = getConfig('dummy_dir', $cachePath. "/" . $dummyFilename);
|
||||
|
||||
if ($dummyImage === true) {
|
||||
is_writable($dummyDir)
|
||||
or verbose("dummy dir not writable = $dummyDir");
|
||||
$dummyDir = $cache->getPathToSubdir("dummy");
|
||||
|
||||
$img->setSaveFolder($dummyDir)
|
||||
->setSource($dummyFilename, $dummyDir)
|
||||
@@ -4688,24 +4845,16 @@ if ($dummyImage === true) {
|
||||
/**
|
||||
* Prepare a sRGB version of the image and use it as source image.
|
||||
*/
|
||||
$srgbDirName = "/srgb";
|
||||
$srgbDir = realpath(getConfig('srgb_dir', $cachePath . $srgbDirName));
|
||||
$srgbDefault = getConfig('srgb_default', false);
|
||||
$srgbColorProfile = getConfig('srgb_colorprofile', __DIR__ . '/../icc/sRGB_IEC61966-2-1_black_scaled.icc');
|
||||
$srgb = getDefined('srgb', true, null);
|
||||
|
||||
if ($srgb || $srgbDefault) {
|
||||
|
||||
if (!is_writable($srgbDir)) {
|
||||
if (is_writable($cachePath)) {
|
||||
mkdir($srgbDir);
|
||||
}
|
||||
}
|
||||
|
||||
$filename = $img->convert2sRGBColorSpace(
|
||||
$srcImage,
|
||||
$imagePath,
|
||||
$srgbDir,
|
||||
$cache->getPathToSubdir("srgb"),
|
||||
$srgbColorProfile,
|
||||
$useCache
|
||||
);
|
||||
@@ -4729,9 +4878,19 @@ if ($status) {
|
||||
$text .= "PHP version = " . PHP_VERSION . "\n";
|
||||
$text .= "Running on: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
|
||||
$text .= "Allow remote images = $allowRemote\n";
|
||||
$text .= "Cache writable = " . is_writable($cachePath) . "\n";
|
||||
$text .= "Cache dummy writable = " . is_writable($dummyDir) . "\n";
|
||||
$text .= "Cache srgb writable = " . is_writable($srgbDir) . "\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("");
|
||||
$text .= "Cache $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("remote");
|
||||
$text .= "Cache remote $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("dummy");
|
||||
$text .= "Cache dummy $res\n";
|
||||
|
||||
$res = $cache->getStatusOfSubdir("srgb");
|
||||
$text .= "Cache srgb $res\n";
|
||||
|
||||
$text .= "Alias path writable = " . is_writable($aliasPath) . "\n";
|
||||
|
||||
$no = extension_loaded('exif') ? null : 'NOT';
|
||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user