AVE.CMS v3.28
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
8.1 KiB

7 years ago
<?php
/**
* AVE.cms
*
* @package AVE.cms
* @version 3.x
* @filesource
* @copyright © 2007-2014 AVE.cms, http://www.ave-cms.ru
*
* @license GPL v.2
*/
4 years ago
if (! defined('BASE_DIR'))
7 years ago
define('BASE_DIR', str_replace("\\", "/", dirname(dirname(__FILE__))));
if (! function_exists('iptc_make_tag'))
{
4 years ago
function iptc_make_tag ($rec, $data, $value)
7 years ago
{
$length = strlen($value);
$retval = chr(0x1C) . chr($rec) . chr($data);
if($length < 0x8000)
{
$retval .= chr($length >> 8) . chr($length & 0xFF);
}
else
7 years ago
{
$retval .= chr(0x80) .
chr(0x04) .
chr(($length >> 24) & 0xFF) .
chr(($length >> 16) & 0xFF) .
chr(($length >> 8) & 0xFF) .
chr($length & 0xFF);
}
7 years ago
return $retval . $value;
}
}
/**
* Creates directory
*
* @param string $path Path to create
* @param integer $mode Optional permissions
* @return boolean Success
*/
4 years ago
function _mkdir ($path, $mode = 0777)
7 years ago
{
$old = umask(0);
$res = @mkdir($path, $mode);
umask($old);
return $res;
}
/**
* Creates directories recursively
*
* @param string $path Path to create
* @param integer $mode Optional permissions
* @return boolean Success
*/
4 years ago
function rmkdir ($path, $mode = 0777)
7 years ago
{
return is_dir($path) || (mkdir(dirname($path), $mode) && _mkdir($path, $mode));
}
4 years ago
if (filesize(BASE_DIR . '/config/config.inc.php'))
require_once BASE_DIR . '/config/config.inc.php';
7 years ago
4 years ago
require_once BASE_DIR . '/inc/config.php';
7 years ago
7 years ago
//-- Разрешенные расширения файлов
4 years ago
$allowedExt = ['jpg', 'jpeg', 'png', 'gif', 'JPG', 'JPEG', 'PNG', 'GIF'];
7 years ago
7 years ago
//-- Разрешенные размеры миниатюр
6 years ago
$allowedSize = (defined('THUMBNAIL_SIZES') && THUMBNAIL_SIZES != '')
7 years ago
? explode(',', trim(THUMBNAIL_SIZES))
4 years ago
: [];
7 years ago
//-- Разрешения для админпанели
4 years ago
$allowedAdmin = [
7 years ago
't128x128',
7 years ago
'f128x128'
4 years ago
];
7 years ago
//-- Ссылка на файл
7 years ago
$imagefile = urldecode($_SERVER['REQUEST_URI']);
7 years ago
//-- Вызов чере $_GET параметры
//-- ToDo
if (! empty($_REQUEST['thumb']))
7 years ago
{
$imagefile = '/'.
rtrim(
dirname($_REQUEST['thumb'])
7 years ago
. '/' . THUMBNAIL_DIR . '/'
. (str_replace(
7 years ago
'.',
7 years ago
(empty($_REQUEST['mode'])
? '-t'
: '-' . $_REQUEST['mode']) . ((empty($_REQUEST['width']) && empty($_REQUEST['height']))
? '128'
: intval(@$_REQUEST['width'])) . 'x' . ((empty($_REQUEST['width']) && empty($_REQUEST['height']))
? '128'
: intval(@$_REQUEST['height'])) . '.',
7 years ago
basename($_REQUEST['thumb'])
)
),
'/');
}
7 years ago
//-- Если пришел прямой вызов файла, то сразу отрубаем его
if ($_SERVER['REQUEST_URI'] == '/inc/thumb.php')
7 years ago
die('No image');
7 years ago
//-- Базовая папка
$baseDir = str_replace('\\', '/', dirname(dirname(__FILE__)));
7 years ago
7 years ago
//-- Если файл существует, показываем его
if (file_exists($baseDir . $imagefile))
7 years ago
{
$img_data = @getimagesize($baseDir . $imagefile);
7 years ago
7 years ago
header('Content-Type:' . $img_data['mime'], true);
7 years ago
header("Last-Modified: " . gmdate("D, d M Y H:i:s" . filemtime($baseDir . $imagefile)) . " GMT");
7 years ago
header("Content-Length: " . (string) filesize($baseDir . $imagefile), true);
readfile($baseDir . $imagefile);
7 years ago
7 years ago
exit;
}
4 years ago
require_once BASE_DIR . '/inc/init.php';
7 years ago
list(, $thumbPath) = explode('/' . UPLOAD_DIR . '/', dirname($imagefile), 2);
$lenThumbDir = strlen(THUMBNAIL_DIR);
7 years ago
// --
7 years ago
if ($lenThumbDir && substr($thumbPath, -$lenThumbDir) != THUMBNAIL_DIR)
7 years ago
{
7 years ago
if (! file_exists($baseDir . $imagefile))
{
report404();
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
}
7 years ago
exit(0);
7 years ago
}
7 years ago
$thumbPath = $baseDir . '/' . UPLOAD_DIR . '/' . $thumbPath;
$imagePath = $lenThumbDir ? dirname($thumbPath) : $thumbPath;
$thumbName = basename($imagefile);
$nameParts = explode('.', $thumbName);
$countParts = count($nameParts);
7 years ago
if ($countParts < 2 || ! in_array(strtolower(end($nameParts)), $allowedExt))
7 years ago
exit(0);
4 years ago
$matches = [];
7 years ago
7 years ago
//-- Смотрим переданные параметры
preg_match('/-(r|c|f|t|s)(\d+)x(\d+)(r)*$/i', $nameParts[$countParts-2], $matches);
7 years ago
7 years ago
//-- Если нет параметров, отдаем 404
if (! isset($matches[0]))
7 years ago
{
7 years ago
report404();
7 years ago
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
exit(0);
}
7 years ago
$check = ltrim($matches[0], '-');
//-- Проверяем разрешен ли данный размер для миниатюры
if (! empty($allowedSize) && ! in_array($check, $allowedSize))
7 years ago
{
7 years ago
if (! in_array($check, $allowedAdmin))
{
7 years ago
report404();
7 years ago
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
exit(0);
}
7 years ago
}
7 years ago
//-- Если есть параметр rotate
if (isset($matches[4]))
7 years ago
{
7 years ago
list ($size, $method, $width, $height, $rotate) = $matches;
7 years ago
}
7 years ago
//-- Иначе
else
{
list ($size, $method, $width, $height) = $matches;
$rotate = false;
}
7 years ago
$nameParts[$countParts-2] = substr($nameParts[$countParts-2], 0, -strlen($size));
$imageName = implode('.', $nameParts);
$save = true;
if (! file_exists("$imagePath/$imageName"))
{
7 years ago
$l = "$imagePath/$imageName";
7 years ago
7 years ago
if (file_exists($l . '.tmp'))
7 years ago
{
7 years ago
$url = trim(file_get_contents($l . '.tmp'), ABS_PATH);
7 years ago
7 years ago
$img = CURL_file_get_contents($url);
7 years ago
7 years ago
if ($img)
7 years ago
{
7 years ago
file_put_contents("$imagePath/$imageName", $img);
7 years ago
4 years ago
//setEXIFF("$imagePath/$imageName");
7 years ago
7 years ago
$save = true;
}
7 years ago
@unlink($l . '.tmp');
7 years ago
}
}
if (! file_exists("$imagePath/$imageName"))
{
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
7 years ago
7 years ago
report404();
7 years ago
$imageName = 'noimage.png';
7 years ago
7 years ago
if (! file_exists("$imagePath/$imageName"))
7 years ago
$imagePath = $baseDir . '/' . UPLOAD_DIR . '/images';
7 years ago
7 years ago
if (! file_exists("$imagePath/$imageName"))
7 years ago
exit(0);
7 years ago
$save = false;
}
7 years ago
require $baseDir . '/class/class.thumbnail.php';
7 years ago
$thumb = new Image_Toolbox("$imagePath/$imageName");
7 years ago
//-- Методы генерации миниатюр
7 years ago
switch ($method)
{
case 'r':
$thumb->newOutputSize((int)$width, (int)$height, 0, (boolean)$rotate);
break;
case 'c':
$thumb->newOutputSize((int)$width, (int)$height, 1, (boolean)$rotate);
break;
case 'f':
$thumb->newOutputSize((int)$width, (int)$height, 2, false, '#ffffff');
break;
7 years ago
7 years ago
case 't':
$thumb->newOutputSize((int)$width, (int)$height, 3, false);
break;
7 years ago
case 's':
$thumb->newOutputSize((int)$width, (int)$height, 4, (boolean)$rotate);
break;
7 years ago
}
4 years ago
//Blend
//$thumb->addImage(BASE_DIR . '/' . 'uploads/gallery/watermark.gif');
//$thumb->blend('right -10', 'bottom -10', IMAGE_TOOLBOX_BLEND_COPY, 70);
7 years ago
4 years ago
//Text
//$thumb->addText('Мой текст', BASE_DIR . '/inc/fonts/ft16.ttf', 16, '#709536', 'right -10', 'bottom -10');
//if ($width > 200){
// $thumb->addImage(BASE_DIR . '/' . 'uploads/gallery/watermark.gif');
// $thumb->blend('right -10', 'bottom -10', IMAGE_TOOLBOX_BLEND_COPY, 70);
//}
7 years ago
$thumb->output();
7 years ago
//-- Если можно сохранять миниатюру
7 years ago
if ($save)
{
7 years ago
if (! file_exists($thumbPath) && ! mkdir($thumbPath, 0777, true))
7 years ago
exit(0);
7 years ago
if ($thumb->save("$thumbPath/$thumbName"))
{
$old = umask(0);
chmod("$thumbPath/$thumbName", 0777);
umask($old);
}
4 years ago
if ($thumb->_img['main']['type'] == 2)
7 years ago
{
$image = getimagesize("$thumbPath/$thumbName", $info);
7 years ago
7 years ago
if (! isset($info['APP13']))
{
//-- Если в настройках разрешена генерация IPTC тегов для миниатюр
if (THUMBNAIL_IPTC)
7 years ago
{
4 years ago
$sitename= get_settings('site_name');
7 years ago
// установка IPTC тэгов
4 years ago
$iptc = [
7 years ago
'2#120' => iconv("UTF-8", "WINDOWS-1251", $sitename),
4 years ago
'2#116' => HOST
];
7 years ago
// Преобразование IPTC тэгов в двоичный код
$data = '';
4 years ago
foreach($iptc AS $tag => $string)
7 years ago
{
$tag = substr($tag, 2);
$data .= iptc_make_tag(2, $tag, $string);
}
// Встраивание IPTC данных
$content = iptcembed($data, "$thumbPath/$thumbName");
// запись нового изображения в файл
$fp = fopen("$thumbPath/$thumbName", "wb");
fwrite($fp, $content);
fclose($fp);
7 years ago
}
}
}
}
?>