2010-03-08 16:15:45 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* e107 website system
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008-2010 e107 Inc (e107.org)
|
|
|
|
* Released under the terms and conditions of the
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
|
|
|
*
|
|
|
|
* On-the-fly thumbnail generator
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package e107
|
|
|
|
* @subpackage core
|
2010-03-09 16:05:41 +00:00
|
|
|
* @author secretr
|
2010-03-08 16:15:45 +00:00
|
|
|
* @version $Id$
|
|
|
|
*
|
2010-03-09 16:05:41 +00:00
|
|
|
* @todo cache management - max age, max size, image cache manager (?), cron (?)
|
|
|
|
*
|
2010-03-08 16:15:45 +00:00
|
|
|
* On-the-fly thumbnail generator
|
|
|
|
*/
|
|
|
|
|
|
|
|
define('e107_INIT', true);
|
|
|
|
|
2010-03-09 16:05:41 +00:00
|
|
|
//error_reporting(E_ALL);
|
|
|
|
//require_once './e107_handlers/benchmark.php';
|
|
|
|
//$bench = new e_benchmark();
|
|
|
|
//$bench->start();
|
|
|
|
|
2010-03-08 16:15:45 +00:00
|
|
|
$thumbpage = new e_thumbpage();
|
|
|
|
|
|
|
|
if(!$thumbpage->checkSrc())
|
|
|
|
{
|
|
|
|
die(' Access denied!');
|
|
|
|
}
|
|
|
|
$thumbpage->sendImage();
|
|
|
|
|
2010-03-09 16:05:41 +00:00
|
|
|
// Check your e_LOG folder
|
|
|
|
//$bench->end()->logResult('thumb.php', $_GET['src'].' - no cache');
|
|
|
|
exit;
|
|
|
|
|
2010-03-08 16:15:45 +00:00
|
|
|
class e_thumbpage
|
|
|
|
{
|
2010-03-09 16:05:41 +00:00
|
|
|
/**
|
|
|
|
* Page request
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-03-08 16:15:45 +00:00
|
|
|
protected $_request = array();
|
2010-03-09 16:05:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string image source path (e107 path shortcode)
|
|
|
|
*/
|
2010-03-08 16:15:45 +00:00
|
|
|
protected $_src = null;
|
|
|
|
|
2010-03-09 16:05:41 +00:00
|
|
|
/**
|
|
|
|
* @var string source path modified/sanitized
|
|
|
|
*/
|
|
|
|
protected $_src_path = null;
|
|
|
|
|
2010-03-08 16:15:45 +00:00
|
|
|
/**
|
|
|
|
* Constructor - init paths
|
|
|
|
* @todo FIX e107 (new folder structure), simplify all this, e.g. e107::getInstance()->initMinimal($path_to_e107_config);
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// initial path
|
|
|
|
$self = realpath(dirname(__FILE__));
|
|
|
|
|
|
|
|
// Config
|
|
|
|
include($self.'/e107_config.php');
|
|
|
|
$tmp = $self.'/'.$HANDLERS_DIRECTORY;
|
|
|
|
|
|
|
|
//Core functions - now API independent
|
|
|
|
@require($tmp.'/core_functions.php');
|
|
|
|
//e107 class
|
|
|
|
@require($tmp.'/e107_class.php');
|
|
|
|
|
|
|
|
$e107_paths = compact(
|
|
|
|
'ADMIN_DIRECTORY',
|
|
|
|
'FILES_DIRECTORY',
|
|
|
|
'IMAGES_DIRECTORY',
|
|
|
|
'THEMES_DIRECTORY',
|
|
|
|
'PLUGINS_DIRECTORY',
|
|
|
|
'HANDLERS_DIRECTORY',
|
|
|
|
'LANGUAGES_DIRECTORY',
|
|
|
|
'HELP_DIRECTORY',
|
|
|
|
'DOWNLOADS_DIRECTORY',
|
|
|
|
'UPLOADS_DIRECTORY',
|
|
|
|
'MEDIA_DIRECTORY',
|
|
|
|
'CACHE_DIRECTORY',
|
|
|
|
'LOGS_DIRECTORY',
|
|
|
|
'WEB_DIRECTORY',
|
|
|
|
'SYSTEM_DIRECTORY',
|
|
|
|
'CORE_DIRECTORY'
|
|
|
|
);
|
|
|
|
$sql_info = array(); //compact('mySQLserver', 'mySQLuser', 'mySQLpassword', 'mySQLdefaultdb', 'mySQLprefix', 'mySQLcharset');
|
2010-03-09 16:05:41 +00:00
|
|
|
//e107::getInstance()->initCore($e107_paths, $self, $sql_info, varset($e107_CONFIG, array()));
|
|
|
|
$e107 = e107::getInstance();
|
2012-06-08 06:03:07 +00:00
|
|
|
|
|
|
|
$e107->site_path = substr(md5($mySQLdefaultdb.".".$mySQLprefix),0,10);
|
|
|
|
|
2010-12-13 11:22:57 +00:00
|
|
|
$e107->prepare_request();
|
2010-12-03 11:56:55 +00:00
|
|
|
$e107->setDirs($e107_paths, varset($E107_CONFIG, array()));
|
2010-03-09 16:05:41 +00:00
|
|
|
$e107->set_constants();
|
|
|
|
$e107->set_paths();
|
|
|
|
$e107->file_path = $e107->fix_windows_paths($self)."/";
|
|
|
|
$e107->set_base_path();
|
2010-12-13 11:22:57 +00:00
|
|
|
$e107->set_request(false);
|
|
|
|
$e107->set_urls(false);
|
2010-03-08 16:15:45 +00:00
|
|
|
unset($tmp, $self);
|
2012-06-08 06:03:07 +00:00
|
|
|
|
2011-01-03 13:02:43 +00:00
|
|
|
// basic Admin area detection - required for proper path parsing
|
|
|
|
define('ADMIN', strpos(e_SELF, ($e107->getFolder('admin')) !== false || strpos(e_PAGE, 'admin') !== false));
|
|
|
|
|
2010-03-08 16:15:45 +00:00
|
|
|
// parse request
|
|
|
|
$this->parseRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseRequest()
|
|
|
|
{
|
2012-07-02 01:32:56 +00:00
|
|
|
//echo 'e_query='.str_replace('&', '&', e_QUERY);
|
2010-12-13 11:22:57 +00:00
|
|
|
parse_str(str_replace('&', '&', e_QUERY), $this->_request);
|
2012-07-02 01:32:56 +00:00
|
|
|
|
|
|
|
// parse_str($_SERVER['QUERY_STRING'], $this->_request);
|
2010-03-09 16:05:41 +00:00
|
|
|
return $this;
|
2010-03-08 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkSrc()
|
|
|
|
{
|
|
|
|
if(!vartrue($this->_request['src']))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tp = e107::getParser();
|
|
|
|
|
2010-03-10 12:48:05 +00:00
|
|
|
// convert raw to SC path
|
|
|
|
$this->_request['src'] = str_replace($tp->getUrlConstants('raw'), $tp->getUrlConstants('sc'), $this->_request['src']);
|
|
|
|
|
|
|
|
// convert absolute and full url to SC URL
|
2010-03-08 16:15:45 +00:00
|
|
|
$this->_src = $tp->createConstants($this->_request['src'], 'mix');
|
|
|
|
|
|
|
|
if(preg_match('#^(https?|ftps?|file)://#i', $this->_request['src']))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_writeable(e_CACHE_IMAGE))
|
|
|
|
{
|
|
|
|
echo 'Cache folder not writeable! ';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-10 12:48:05 +00:00
|
|
|
// convert to relative server path
|
|
|
|
$path = $tp->replaceConstants(str_replace('..', '', $this->_src)); //should be safe enough
|
2010-03-08 16:15:45 +00:00
|
|
|
|
|
|
|
if(is_file($path) && is_readable($path))
|
|
|
|
{
|
|
|
|
$this->_src_path = $path;
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-08 06:03:07 +00:00
|
|
|
|
|
|
|
// echo "path=".$path."<br />";
|
2010-03-08 16:15:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendImage()
|
|
|
|
{
|
2010-03-09 16:05:41 +00:00
|
|
|
//global $bench;
|
2010-03-08 16:15:45 +00:00
|
|
|
if(!$this->_src_path)
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$thumbnfo = pathinfo($this->_src_path);
|
|
|
|
$options = $this->getRequestOptions();
|
|
|
|
|
2010-03-09 16:05:41 +00:00
|
|
|
$cache_str = md5(serialize($options).$this->_src_path);
|
2010-03-08 16:15:45 +00:00
|
|
|
$fname = strtolower('Thumb_'.$thumbnfo['filename'].'_'.$cache_str.'.'.$thumbnfo['extension']).'.cache.bin';
|
|
|
|
|
|
|
|
if(is_file(e_CACHE_IMAGE.$fname) && is_readable(e_CACHE_IMAGE.$fname))
|
|
|
|
{
|
|
|
|
$thumbnfo['lmodified'] = filemtime(e_CACHE_IMAGE.$fname);
|
|
|
|
$thumbnfo['md5s'] = md5_file(e_CACHE_IMAGE.$fname);
|
|
|
|
$thumbnfo['fsize'] = filesize(e_CACHE_IMAGE.$fname);
|
2010-12-17 17:21:48 +00:00
|
|
|
|
|
|
|
// Send required headers
|
|
|
|
$this->sendHeaders($thumbnfo);
|
2010-03-08 16:15:45 +00:00
|
|
|
|
|
|
|
// check browser cache
|
|
|
|
if (@$_SERVER['HTTP_IF_MODIFIED_SINCE'] && ($thumbnfo['lmodified'] <= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) && (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == $thumbnfo['md5s']))
|
|
|
|
{
|
|
|
|
header('HTTP/1.1 304 Not Modified');
|
2010-03-09 16:05:41 +00:00
|
|
|
//$bench->end()->logResult('thumb.php', $_GET['src'].' - 304 not modified');
|
2010-03-08 16:15:45 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send required headers
|
2010-12-17 17:21:48 +00:00
|
|
|
//$this->sendHeaders($thumbnfo);
|
2010-03-08 16:15:45 +00:00
|
|
|
|
|
|
|
@readfile(e_CACHE_IMAGE.$fname);
|
2010-03-09 16:05:41 +00:00
|
|
|
//$bench->end()->logResult('thumb.php', $_GET['src'].' - retrieve cache');
|
2010-03-08 16:15:45 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - wrap it around generic e107 thumb handler
|
|
|
|
@require(e_HANDLER.'phpthumb/ThumbLib.inc.php');
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$thumb = PhpThumbFactory::create($this->_src_path);
|
|
|
|
$thumb->setOptions(array('correctPermissions' => true));
|
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
echo $e->getMessage();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2010-03-10 10:00:37 +00:00
|
|
|
if(isset($this->_request['w']) || isset($this->_request['h']))
|
2010-03-08 16:15:45 +00:00
|
|
|
{
|
2010-03-10 10:00:37 +00:00
|
|
|
$thumb->resize((integer) vartrue($this->_request['w'], 0), (integer) vartrue($this->_request['h'], 0));
|
2010-03-08 16:15:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-10 10:00:37 +00:00
|
|
|
$thumb->adaptiveResize((integer) vartrue($this->_request['aw'], 0), (integer) vartrue($this->_request['ah'], 0));
|
2010-03-08 16:15:45 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 01:32:56 +00:00
|
|
|
//TODO Move to $_SESSION and activate based on width/height ie. watermark all images larger than....
|
|
|
|
// ie. prevent user tampering with URLs.
|
|
|
|
if(isset($this->_request['wm']))
|
|
|
|
{
|
|
|
|
$tp = e107::getParser();
|
|
|
|
|
|
|
|
$tmp = explode("|",$this->_request['wm']);
|
|
|
|
|
|
|
|
$tmp[4] = $tp->createConstants($tmp[4], 'mix');
|
|
|
|
$tmp[4] = realpath($tp->replaceConstants($tmp[4],'rel'));
|
|
|
|
|
|
|
|
$thumb->WatermarkText($tmp);
|
|
|
|
// $alignment='BR', $hex_color='000000', $ttffont='', $opacity=100, $margin=5, $angle=0, $bg_color=false, $bg_opacity=0, $fillextend=''
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-08 16:15:45 +00:00
|
|
|
// set cache
|
|
|
|
$thumb->save(e_CACHE_IMAGE.$fname);
|
|
|
|
|
|
|
|
// show thumb
|
|
|
|
$thumb->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRequestOptions()
|
|
|
|
{
|
|
|
|
$ret = array();
|
|
|
|
$ret['w'] = isset($this->_request['w']) ? intval($this->_request['w']) : false;
|
|
|
|
$ret['h'] = isset($this->_request['h']) ? intval($this->_request['h']) : $ret['w'];
|
|
|
|
$ret['aw'] = isset($this->_request['aw']) ? intval($this->_request['aw']) : false;
|
|
|
|
$ret['ah'] = isset($this->_request['ah']) ? intval($this->_request['ah']) : $ret['aw'];
|
2012-07-02 01:32:56 +00:00
|
|
|
$ret['wm'] = isset($this->_request['wm']) ? intval($this->_request['wm']) : $ret['wm'];
|
2010-03-08 16:15:45 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendHeaders($thumbnfo)
|
|
|
|
{
|
|
|
|
if(headers_sent())
|
|
|
|
{
|
|
|
|
echo 'Headers already sent! ';
|
|
|
|
exit;
|
|
|
|
}
|
2010-12-17 17:21:48 +00:00
|
|
|
if (function_exists('date_default_timezone_set'))
|
|
|
|
{
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
}
|
|
|
|
//header('Pragma:');
|
2010-03-08 16:15:45 +00:00
|
|
|
header('Cache-Control: must-revalidate');
|
|
|
|
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $thumbnfo['lmodified']).' GMT');
|
|
|
|
header('Content-Length: '.$thumbnfo['fsize']);
|
|
|
|
$ctype = self::ctype($thumbnfo['extension']);
|
|
|
|
if(null !== $ctype)
|
|
|
|
{
|
|
|
|
header('Content-Type: '.$ctype);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expire header - 1 year
|
|
|
|
$time = time() + 365 * 86400;
|
|
|
|
header('Expires: '.gmdate("D, d M Y H:i:s", $time).' GMT');
|
|
|
|
header("Etag: ".$thumbnfo['md5s']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function ctype($ftype)
|
|
|
|
{
|
|
|
|
static $known_types = array(
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
'jpg' => 'image/jpeg',
|
|
|
|
'jpeg' => 'image/jpeg',
|
|
|
|
'png' => 'image/png',
|
|
|
|
//'bmp' => 'image/bmp',
|
|
|
|
);
|
|
|
|
|
|
|
|
$ftype = strtolower($ftype);
|
|
|
|
if(isset($known_types[$ftype]))
|
|
|
|
{
|
|
|
|
return $known_types[$ftype];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2010-03-09 16:05:41 +00:00
|
|
|
|
|
|
|
?>
|