mirror of
https://github.com/mosbth/cimage.git
synced 2025-07-31 13:40:08 +02:00
* Now returns statuscode 500 when something fails #55.
* Three different modes: strict, production, development #44. * Three files for all-in-one `imgs.php`, `imgp.php`, `imgd.php` #73.
This commit is contained in:
@@ -476,6 +476,9 @@ class CImage
|
|||||||
$cache = $this->saveFolder . "/remote/";
|
$cache = $this->saveFolder . "/remote/";
|
||||||
|
|
||||||
if (!is_dir($cache)) {
|
if (!is_dir($cache)) {
|
||||||
|
if (!is_writable($this->saveFolder)) {
|
||||||
|
throw new Exception("Can not create remote cache, cachefolder not writable.");
|
||||||
|
}
|
||||||
mkdir($cache);
|
mkdir($cache);
|
||||||
$this->log("The remote cache does not exists, creating it.");
|
$this->log("The remote cache does not exists, creating it.");
|
||||||
}
|
}
|
||||||
|
@@ -124,6 +124,23 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if cache is writable or throw exception.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws Exception if cahce folder is not writable.
|
||||||
|
*/
|
||||||
|
public function isCacheWritable()
|
||||||
|
{
|
||||||
|
if (!is_writable($this->saveFolder)) {
|
||||||
|
throw new Exception("Cache folder is not writable for downloaded files.");
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decide if the cache should be used or not before trying to download
|
* Decide if the cache should be used or not before trying to download
|
||||||
* a remote file.
|
* a remote file.
|
||||||
@@ -277,8 +294,10 @@ class CRemoteImage
|
|||||||
|
|
||||||
$this->status = $this->http->getStatus();
|
$this->status = $this->http->getStatus();
|
||||||
if ($this->status === 200) {
|
if ($this->status === 200) {
|
||||||
|
$this->isCacheWritable();
|
||||||
return $this->save();
|
return $this->save();
|
||||||
} else if ($this->status === 304) {
|
} else if ($this->status === 304) {
|
||||||
|
$this->isCacheWritable();
|
||||||
return $this->updateCacheDetails();
|
return $this->updateCacheDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -280,7 +280,10 @@ Revision history
|
|||||||
|
|
||||||
v0.6.x (latest)
|
v0.6.x (latest)
|
||||||
|
|
||||||
* Change name of script all-in-one to `webrrot/imgs.php` #73.
|
* Now returns statuscode 500 when something fails #55.
|
||||||
|
* Three different modes: strict, production, development #44.
|
||||||
|
* Three files for all-in-one `imgs.php`, `imgp.php`, `imgd.php` #73.
|
||||||
|
* Change name of script all-in-one to `webroot/imgs.php` #73.
|
||||||
* Combine all code into one singel script, `webroot/img_single.php` #73.
|
* Combine all code into one singel script, `webroot/img_single.php` #73.
|
||||||
* Disallow hotlinking/leeching by configuration #46.
|
* Disallow hotlinking/leeching by configuration #46.
|
||||||
* Alias-name is without extension #47.
|
* Alias-name is without extension #47.
|
||||||
|
@@ -3,7 +3,9 @@
|
|||||||
#
|
#
|
||||||
# Paths and settings
|
# Paths and settings
|
||||||
#
|
#
|
||||||
TARGET="webroot/imgs.php"
|
TARGET_D="webroot/imgd.php"
|
||||||
|
TARGET_P="webroot/imgp.php"
|
||||||
|
TARGET_S="webroot/imgs.php"
|
||||||
NEWLINES="\n\n\n"
|
NEWLINES="\n\n\n"
|
||||||
|
|
||||||
|
|
||||||
@@ -29,36 +31,43 @@ fi
|
|||||||
#
|
#
|
||||||
# Print out details on cache-directory
|
# Print out details on cache-directory
|
||||||
#
|
#
|
||||||
$ECHO "Creating '$TARGET' by combining the following files:"
|
$ECHO "Creating '$TARGET_D', '$TARGET_P' and '$TARGET_S' by combining the following files:"
|
||||||
$ECHO "\n"
|
$ECHO "\n"
|
||||||
$ECHO "\n webroot/img_single_header.php"
|
$ECHO "\n webroot/img_header.php"
|
||||||
$ECHO "\n CHttpGet.php"
|
$ECHO "\n CHttpGet.php"
|
||||||
$ECHO "\n CRemoteImage.php"
|
$ECHO "\n CRemoteImage.php"
|
||||||
$ECHO "\n CImage.php"
|
$ECHO "\n CImage.php"
|
||||||
$ECHO "\n webroot/img.php"
|
$ECHO "\n webroot/img.php"
|
||||||
$ECHO "\n"
|
$ECHO "\n"
|
||||||
|
$ECHO "\n'$TARGET_D' is for development mode."
|
||||||
|
$ECHO "\n'$TARGET_P' is for production mode (default mode)."
|
||||||
|
$ECHO "\n'$TARGET_S' is for strict mode."
|
||||||
|
$ECHO "\n"
|
||||||
|
|
||||||
$ECHO "\nPress enter to continue. "
|
$ECHO "\nPress enter to continue. "
|
||||||
read answer
|
read answer
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Create the $TARGET file
|
# Create the $TARGET_? files
|
||||||
#
|
#
|
||||||
cat webroot/img_single_header.php > $TARGET
|
cat webroot/img_header.php > $TARGET_P
|
||||||
$ECHO "$NEWLINES" >> $TARGET
|
cat webroot/img_header.php | sed "s|//'mode' => 'production'|'mode' => 'development'|" > $TARGET_D
|
||||||
|
cat webroot/img_header.php | sed "s|//'mode' => 'production'|'mode' => 'strict'|" > $TARGET_S
|
||||||
|
|
||||||
tail -n +2 CHttpGet.php >> $TARGET
|
$ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||||
$ECHO "$NEWLINES" >> $TARGET
|
|
||||||
|
|
||||||
tail -n +2 CRemoteImage.php >> $TARGET
|
tail -n +2 CHttpGet.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||||
$ECHO "$NEWLINES" >> $TARGET
|
$ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||||
|
|
||||||
tail -n +2 CImage.php >> $TARGET
|
tail -n +2 CRemoteImage.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||||
$ECHO "$NEWLINES" >> $TARGET
|
$ECHO "$NEWLINES" | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||||
|
|
||||||
tail -n +2 webroot/img.php >> $TARGET
|
tail -n +2 CImage.php | tee -a $TARGET_D $TARGET_P $TARGET_S > /dev/null
|
||||||
$ECHO "$NEWLINES" >> $TARGET
|
$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
|
||||||
|
|
||||||
$ECHO "\nDone."
|
$ECHO "\nDone."
|
||||||
$ECHO "\n"
|
$ECHO "\n"
|
||||||
|
@@ -19,8 +19,16 @@
|
|||||||
*/
|
*/
|
||||||
function errorPage($msg)
|
function errorPage($msg)
|
||||||
{
|
{
|
||||||
header("HTTP/1.0 404 Not Found");
|
global $mode;
|
||||||
die('img.php say 404: ' . $msg);
|
|
||||||
|
header("HTTP/1.0 500 Internal Server Error");
|
||||||
|
|
||||||
|
if ($mode == 'development') {
|
||||||
|
die("[img.php] $msg");
|
||||||
|
} else {
|
||||||
|
error_log("[img.php] $msg");
|
||||||
|
die("HTTP/1.0 500 Internal Server Error");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -131,6 +139,13 @@ if (is_file($configFile)) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* verbose, v - do a verbose dump of what happens
|
||||||
|
*/
|
||||||
|
$verbose = getDefined(array('verbose', 'v'), true, false);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set mode as strict, production or development.
|
* Set mode as strict, production or development.
|
||||||
* Default is production environment.
|
* Default is production environment.
|
||||||
@@ -147,21 +162,32 @@ if (!extension_loaded('gd')) {
|
|||||||
|
|
||||||
// Specific settings for each mode
|
// Specific settings for each mode
|
||||||
if ($mode == 'strict') {
|
if ($mode == 'strict') {
|
||||||
|
|
||||||
error_reporting(0);
|
error_reporting(0);
|
||||||
ini_set('display_errors', 0);
|
ini_set('display_errors', 0);
|
||||||
|
ini_set('log_errors', 1);
|
||||||
|
$verbose = false;
|
||||||
|
|
||||||
} else if ($mode == 'production') {
|
} else if ($mode == 'production') {
|
||||||
error_reporting(0);
|
|
||||||
|
error_reporting(-1);
|
||||||
ini_set('display_errors', 0);
|
ini_set('display_errors', 0);
|
||||||
|
ini_set('log_errors', 1);
|
||||||
|
$verbose = false;
|
||||||
|
|
||||||
} else if ($mode == 'development') {
|
} else if ($mode == 'development') {
|
||||||
|
|
||||||
error_reporting(-1);
|
error_reporting(-1);
|
||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
|
ini_set('log_errors', 0);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
errorPage("Unknown mode: $mode");
|
errorPage("Unknown mode: $mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verbose("mode = $mode");
|
||||||
|
verbose("error log = " . ini_get('error_log'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -177,13 +203,6 @@ if ($defaultTimezone) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* verbose, v - do a verbose dump of what happens
|
|
||||||
*/
|
|
||||||
$verbose = getDefined(array('verbose', 'v'), true, false);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if passwords are configured, used and match.
|
* Check if passwords are configured, used and match.
|
||||||
* Options decide themself if they require passwords to be used.
|
* Options decide themself if they require passwords to be used.
|
||||||
|
@@ -13,7 +13,7 @@ return array(
|
|||||||
* Default values:
|
* Default values:
|
||||||
* mode: 'production'
|
* mode: 'production'
|
||||||
*/
|
*/
|
||||||
//'mode' => 'production', // 'development',
|
//'mode' => 'production', // 'development', 'strict'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -19,6 +19,7 @@
|
|||||||
*/
|
*/
|
||||||
$config = array(
|
$config = array(
|
||||||
|
|
||||||
|
//'mode' => 'production', // 'production', 'development', 'strict'
|
||||||
//'image_path' => __DIR__ . '/img/',
|
//'image_path' => __DIR__ . '/img/',
|
||||||
//'cache_path' => __DIR__ . '/../cache/',
|
//'cache_path' => __DIR__ . '/../cache/',
|
||||||
//'alias_path' => __DIR__ . '/img/alias/',
|
//'alias_path' => __DIR__ . '/img/alias/',
|
3886
webroot/imgd.php
Normal file
3886
webroot/imgd.php
Normal file
File diff suppressed because it is too large
Load Diff
3886
webroot/imgp.php
Normal file
3886
webroot/imgp.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -19,6 +19,7 @@
|
|||||||
*/
|
*/
|
||||||
$config = array(
|
$config = array(
|
||||||
|
|
||||||
|
'mode' => 'strict', // 'production', 'development', 'strict'
|
||||||
//'image_path' => __DIR__ . '/img/',
|
//'image_path' => __DIR__ . '/img/',
|
||||||
//'cache_path' => __DIR__ . '/../cache/',
|
//'cache_path' => __DIR__ . '/../cache/',
|
||||||
//'alias_path' => __DIR__ . '/img/alias/',
|
//'alias_path' => __DIR__ . '/img/alias/',
|
||||||
@@ -396,6 +397,23 @@ class CRemoteImage
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if cache is writable or throw exception.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws Exception if cahce folder is not writable.
|
||||||
|
*/
|
||||||
|
public function isCacheWritable()
|
||||||
|
{
|
||||||
|
if (!is_writable($this->saveFolder)) {
|
||||||
|
throw new Exception("Cache folder is not writable for downloaded files.");
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decide if the cache should be used or not before trying to download
|
* Decide if the cache should be used or not before trying to download
|
||||||
* a remote file.
|
* a remote file.
|
||||||
@@ -549,8 +567,10 @@ class CRemoteImage
|
|||||||
|
|
||||||
$this->status = $this->http->getStatus();
|
$this->status = $this->http->getStatus();
|
||||||
if ($this->status === 200) {
|
if ($this->status === 200) {
|
||||||
|
$this->isCacheWritable();
|
||||||
return $this->save();
|
return $this->save();
|
||||||
} else if ($this->status === 304) {
|
} else if ($this->status === 304) {
|
||||||
|
$this->isCacheWritable();
|
||||||
return $this->updateCacheDetails();
|
return $this->updateCacheDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1082,6 +1102,9 @@ class CImage
|
|||||||
$cache = $this->saveFolder . "/remote/";
|
$cache = $this->saveFolder . "/remote/";
|
||||||
|
|
||||||
if (!is_dir($cache)) {
|
if (!is_dir($cache)) {
|
||||||
|
if (!is_writable($this->saveFolder)) {
|
||||||
|
throw new Exception("Can not create remote cache, cachefolder not writable.");
|
||||||
|
}
|
||||||
mkdir($cache);
|
mkdir($cache);
|
||||||
$this->log("The remote cache does not exists, creating it.");
|
$this->log("The remote cache does not exists, creating it.");
|
||||||
}
|
}
|
||||||
@@ -2979,8 +3002,16 @@ EOD;
|
|||||||
*/
|
*/
|
||||||
function errorPage($msg)
|
function errorPage($msg)
|
||||||
{
|
{
|
||||||
header("HTTP/1.0 404 Not Found");
|
global $mode;
|
||||||
die('img.php say 404: ' . $msg);
|
|
||||||
|
header("HTTP/1.0 500 Internal Server Error");
|
||||||
|
|
||||||
|
if ($mode == 'development') {
|
||||||
|
die("[img.php] $msg");
|
||||||
|
} else {
|
||||||
|
error_log("[img.php] $msg");
|
||||||
|
die("HTTP/1.0 500 Internal Server Error");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -3091,6 +3122,13 @@ if (is_file($configFile)) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* verbose, v - do a verbose dump of what happens
|
||||||
|
*/
|
||||||
|
$verbose = getDefined(array('verbose', 'v'), true, false);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set mode as strict, production or development.
|
* Set mode as strict, production or development.
|
||||||
* Default is production environment.
|
* Default is production environment.
|
||||||
@@ -3107,21 +3145,32 @@ if (!extension_loaded('gd')) {
|
|||||||
|
|
||||||
// Specific settings for each mode
|
// Specific settings for each mode
|
||||||
if ($mode == 'strict') {
|
if ($mode == 'strict') {
|
||||||
|
|
||||||
error_reporting(0);
|
error_reporting(0);
|
||||||
ini_set('display_errors', 0);
|
ini_set('display_errors', 0);
|
||||||
|
ini_set('log_errors', 1);
|
||||||
|
$verbose = false;
|
||||||
|
|
||||||
} else if ($mode == 'production') {
|
} else if ($mode == 'production') {
|
||||||
error_reporting(0);
|
|
||||||
|
error_reporting(-1);
|
||||||
ini_set('display_errors', 0);
|
ini_set('display_errors', 0);
|
||||||
|
ini_set('log_errors', 1);
|
||||||
|
$verbose = false;
|
||||||
|
|
||||||
} else if ($mode == 'development') {
|
} else if ($mode == 'development') {
|
||||||
|
|
||||||
error_reporting(-1);
|
error_reporting(-1);
|
||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
|
ini_set('log_errors', 0);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
errorPage("Unknown mode: $mode");
|
errorPage("Unknown mode: $mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verbose("mode = $mode");
|
||||||
|
verbose("error log = " . ini_get('error_log'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -3137,13 +3186,6 @@ if ($defaultTimezone) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* verbose, v - do a verbose dump of what happens
|
|
||||||
*/
|
|
||||||
$verbose = getDefined(array('verbose', 'v'), true, false);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if passwords are configured, used and match.
|
* Check if passwords are configured, used and match.
|
||||||
* Options decide themself if they require passwords to be used.
|
* Options decide themself if they require passwords to be used.
|
||||||
|
Reference in New Issue
Block a user