diff --git a/CImage.php b/CImage.php index 86c70ab..a492fcf 100644 --- a/CImage.php +++ b/CImage.php @@ -2134,6 +2134,37 @@ class CImage + /** + * Create a hard link, as an alias, to the cached file. + * + * @param string $alias where to store the link. + * + * @return $this + */ + public function linkToCacheFile($alias) + { + if ($alias === null) { + $this->log("Ignore creating alias."); + return $this; + } + + if (is_readable($alias)) { + unlink($alias); + } + + $res = link($this->cacheFileName, $alias); + + if ($res) { + $this->log("Created an alias to the cachefile: $alias"); + } else { + $this->log("Failed to create the alias: $alias"); + } + + return $this; + } + + + /** * Output image to browser using caching. * diff --git a/README.md b/README.md index d2ff94e..9a2149a 100644 --- a/README.md +++ b/README.md @@ -280,6 +280,8 @@ Revision history v0.6.x (latest) +* Support for option `password, pwd` to protect usage of `alias` and remote download. +* Added support for option `alias` that creates a link to a cached version of the image #47. * Create cache directory for remote download if it does not exists. * Cleaned up `img_config.php` and introduced default values for almost all options #72. @@ -287,6 +289,7 @@ v0.6.x (latest) v0.6.2 (2015-01-14) * Added support for download of remote images #43. +* Added autoloader. v0.6.1 (2015-01-08) diff --git a/webroot/img.php b/webroot/img.php index 8d7a17d..fdee7d6 100644 --- a/webroot/img.php +++ b/webroot/img.php @@ -166,24 +166,27 @@ $img->setVerbose($verbose); /** - * Allow or disallow remote download of images from other servers. - * + * Check if passwords are configured, used and match. + * Options decide themself if they require passwords to be used. */ -$allowRemote = getConfig('remote_allow', false); -$remotePwd = getConfig('remote_password', false); +$pwdConfig = getConfig('password', false); $pwd = get(array('password', 'pwd'), null); // Check if passwords match, if configured to use passwords $passwordMatch = null; -if ($remotePwd != false) { - if ($remotePwd == $pwd) { - $passwordMatch = true; - } else { - $passwordMatch = false; - errorPage('Trying to download remote image but missing password.'); - } +if ($pwdConfig) { + $passwordMatch = ($pwdConfig == $pwd); } + + +/** + * Allow or disallow remote download of images from other servers. + * Passwords apply if used. + * + */ +$allowRemote = getConfig('remote_allow', false); + if ($allowRemote && $passwordMatch !== false) { $pattern = getConfig('remote_pattern', null); $img->setRemoteDownload($allowRemote, $pattern); @@ -625,6 +628,33 @@ $postProcessing = getConfig('postprocessing', array( +/** + * alias - Save resulting image to another alias name. + * Password apply if defined. + */ +$alias = get('alias', null); +$aliasPath = getConfig('alias_path', null); +$aliasTarget = null; + +if ($alias && $aliasPath) { + + $aliasTarget = $aliasPath . $alias; + $useCache = false; + + ($passwordMatch !== false) + or errorPage("Alias used and password check failed."); + is_writable($aliasPath) + or errorPage("Directory for alias is not writable."); + preg_match($validFilename, $alias) + or errorPage('Filename for alias contains invalid characters.'); +} else if ($alias) { + errorPage('Alias is not enabled in the config file.'); +} + +verbose("alias = $alias"); + + + /** * Display image if verbose mode */ @@ -716,4 +746,5 @@ $img->log("Incoming arguments: " . print_r(verbose(), 1)) ->postResize() ->setPostProcessingOptions($postProcessing) ->save() + ->linkToCacheFile($aliasTarget) ->output(); diff --git a/webroot/img_config.php b/webroot/img_config.php index 0f19d31..ae2b3f4 100644 --- a/webroot/img_config.php +++ b/webroot/img_config.php @@ -17,9 +17,42 @@ return array( /** * Paths, where are the images stored and where is the cache. + * End all paths with a slash. + * + * Default values: + * image_path: No default value + * cache_path: No default value + * alias_path: null */ 'image_path' => __DIR__ . '/img/', 'cache_path' => __DIR__ . '/../cache/', + //'alias_path' => __DIR__ . '/img/alias/', + + + + /** + * Use password to protect from missusage, send &pwd=... or &password=.. + * with the request to match the password or set to false to disable. + * Passwords are only used together with the options for remote download + * and aliasing. + * + * Default values. + * password: false // as in do not use password + */ + //'password' => false, // "secret-password", + + + + /** + * Allow or disallow downloading of remote files, images available on + * some remote server. Default is to disallow. + * + * Default values. + * remote_allow: false + * remote_pattern: null // use default values from CImage + */ + //'remote_allow' => true, + //'remote_pattern' => '#^https?://#', @@ -45,23 +78,6 @@ return array( - /** - * Allow or disallow downloading of remote files, images available on - * 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_password' => false, // "secret-password", - //'remote_pattern' => '#^https?://#', - - - /** * Set default timezone. *