From 18036a70fcee8e372b6b8d98fbe8a61a3685798f Mon Sep 17 00:00:00 2001 From: rvelices Date: Thu, 2 Feb 2012 20:59:41 +0000 Subject: feature 2548 multisize - custom sizes restricted to those requested by theme/plugin code refacto git-svn-id: http://piwigo.org/svn/trunk@13021 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/derivatives.php | 1 + admin/include/functions.php | 21 +++--- .../themes/default/template/derivatives_build.tpl | 2 +- i.php | 81 +++++++++++++++++----- include/derivative_params.inc.php | 62 ++++------------- include/derivative_std_params.inc.php | 31 ++++++++- include/picture_comment.inc.php | 22 ++---- include/template.class.php | 26 +++---- themes/default/template/picture.tpl | 6 +- 9 files changed, 141 insertions(+), 111 deletions(-) diff --git a/admin/derivatives.php b/admin/derivatives.php index 94a558638..a40e3f3c3 100644 --- a/admin/derivatives.php +++ b/admin/derivatives.php @@ -214,6 +214,7 @@ if ( isset($_POST['d']) ) {// disabled if (isset($enabled[$type])) {// now disabled, before was enabled + $changed_types[] = $type; $disabled[$type] = $enabled[$type]; unset($enabled[$type]); } diff --git a/admin/include/functions.php b/admin/include/functions.php index 8b455f077..6344d705f 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -202,15 +202,18 @@ SELECT } $ok = true; - foreach ($files as $path) - { - if (is_file($path) and !unlink($path)) - { - $ok = false; - trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING); - break; - } - } + if (!isset($conf['never_delete_originals'])) + { + foreach ($files as $path) + { + if (is_file($path) and !unlink($path)) + { + $ok = false; + trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING); + break; + } + } + } if ($ok) { diff --git a/admin/themes/default/template/derivatives_build.tpl b/admin/themes/default/template/derivatives_build.tpl index 6e0c8e35d..16170a65e 100644 --- a/admin/themes/default/template/derivatives_build.tpl +++ b/admin/themes/default/template/derivatives_build.tpl @@ -44,7 +44,7 @@ TABLE { {footer_script require='jquery.effects.slide'}{literal} -var loader = new ImageLoader( {onChanged: loaderChanged} ) +var loader = new ImageLoader( {onChanged: loaderChanged, maxRequests:1 } ) , pending_next_page = null , last_image_show_time = 0 , allDoneDfd, urlDfd; diff --git a/i.php b/i.php index ea521b90a..f919380ff 100644 --- a/i.php +++ b/i.php @@ -65,14 +65,9 @@ function mkgetdir($dir) function ilog() { - global $conf, $ilogfh; + global $conf; if (!$conf['enable_i_log']) return; - if(!$ilogfh) - { - $dir=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/'; - if (!mkgetdir($dir) or ! ($ilogfh=fopen($dir.'i.log', 'a')) ) - return; - } + $line = date("c"); foreach( func_get_args() as $arg) { @@ -86,7 +81,11 @@ function ilog() $line .= $arg; } } - fwrite($ilogfh, $line."\n"); + $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/i.log'; + if (false == file_put_contents($file, $line."\n", FILE_APPEND)) + { + mkgetdir(dirname($file)); + } } function ierror($msg, $code) @@ -124,6 +123,49 @@ function time_step( &$step ) return intval(1000*($step - $tmp)); } +function url_to_size($s) +{ + $pos = strpos($s, 'x'); + if ($pos===false) + { + return array((int)$s, (int)$s); + } + return array((int)substr($s,0,$pos), (int)substr($s,$pos+1)); +} + +function parse_custom_params($tokens) +{ + if (count($tokens)<1) + ierror('Empty array while parsing Sizing', 400); + + $crop = 0; + $min_size = null; + + $token = array_shift($tokens); + if ($token[0]=='s') + { + $size = url_to_size( substr($token,1) ); + } + elseif ($token[0]=='e') + { + $crop = 1; + $size = $min_size = url_to_size( substr($token,1) ); + } + else + { + $size = url_to_size( $token ); + if (count($tokens)<2) + ierror('Sizing arr', 400); + + $token = array_shift($tokens); + $crop = char_to_fraction($token); + + $token = array_shift($tokens); + $min_size = url_to_size( $token ); + } + return new DerivativeParams( new SizingParams($size, $crop, $min_size) ); +} + function parse_request() { global $conf, $page; @@ -199,14 +241,8 @@ function parse_request() if ($page['derivative_type'] == IMG_CUSTOM) { - try - { - $params = $page['derivative_params'] = DerivativeParams::from_url_tokens($deriv); - } - catch (Exception $e) - { - ierror($e->getMessage(), 400); - } + $params = $page['derivative_params'] = parse_custom_params($deriv); + if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20) { ierror('Invalid size', 400); @@ -215,6 +251,19 @@ function parse_request() { ierror('Invalid crop', 400); } + $greatest = ImageStdParams::get_by_type(IMG_XXLARGE); + if ($params->max_width() > $greatest->max_width() || $params->max_height() > $greatest->max_height()) + { + ierror('Too big', 403); + } + + $key = array(); + $params->add_url_tokens($key); + $key = implode('_', $key); + if (!isset(ImageStdParams::$custom[$key])) + { + ierror('Size not allowed', 403); + } } if (!is_file(PHPWG_ROOT_PATH.$req.$ext) and diff --git a/include/derivative_params.inc.php b/include/derivative_params.inc.php index 30497639f..ba1b1dce7 100644 --- a/include/derivative_params.inc.php +++ b/include/derivative_params.inc.php @@ -33,21 +33,20 @@ function size_to_url($s) return $s[0].'x'.$s[1]; } -function url_to_size($s) +function size_equals($s1, $s2) { - $pos = strpos($s, 'x'); - if ($pos===false) - { - return array((int)$s, (int)$s); - } - return array((int)substr($s,0,$pos), (int)substr($s,$pos+1)); + return ($s1[0]==$s2[0] && $s1[1]==$s2[1]); } -function size_equals($s1, $s2) +function char_to_fraction($c) { - return ($s1[0]==$s2[0] && $s1[1]==$s2[1]); + return (ord($c) - ord('a'))/25; } +function fraction_to_char($f) +{ + return ord('a') + round($f*25); +} /** small utility to manipulate a 'rectangle'*/ final class ImageRect @@ -79,8 +78,8 @@ final class ImageRect if (!empty($coi)) { - $coil = floor($this->r * (ord($coi[0]) - ord('a'))/25); - $coir = ceil($this->r * (ord($coi[2]) - ord('a'))/25); + $coil = floor($this->r * char_to_fraction($coi[0])); + $coir = ceil($this->r * char_to_fraction($coi[2])); $availableL = $coil > $this->l ? $coil - $this->l : 0; $availableR = $coir < $this->r ? $this->r - $coir : 0; if ($availableL + $availableR <= $pixels) @@ -115,8 +114,8 @@ final class ImageRect if (!empty($coi)) { - $coit = floor($this->b * (ord($coi[1]) - ord('a'))/25); - $coib = ceil($this->b * (ord($coi[3]) - ord('a'))/25); + $coit = floor($this->b * char_to_fraction($coi[1])); + $coib = ceil($this->b * char_to_fraction($coi[3])); $availableT = $coit > $this->t ? $coit - $this->t : 0; $availableB = $coib < $this->b ? $this->b - $coib : 0; if ($availableT + $availableB <= $pixels) @@ -179,39 +178,11 @@ final class SizingParams else { $tokens[] = size_to_url($this->ideal_size); - $tokens[] = sprintf('%02x', round(100*$this->max_crop) ); + $tokens[] = fraction_to_char($this->max_crop); $tokens[] = size_to_url($this->min_size); } } - static function from_url_tokens($tokens) - { - if (count($tokens)<1) - throw new Exception('Empty array while parsing Sizing'); - $token = array_shift($tokens); - if ($token[0]=='s') - { - return new SizingParams( url_to_size( substr($token,1) ) ); - } - if ($token[0]=='e') - { - $s = url_to_size( substr($token,1) ); - return new SizingParams($s, 1, $s); - } - - $ideal_size = url_to_size( $token ); - if (count($tokens)<2) - throw new Exception('Sizing arr'); - - $token = array_shift($tokens); - $crop = hexdec($token) / 100; - - $token = array_shift($tokens); - $min_size = url_to_size( $token ); - return new SizingParams($ideal_size, $crop, $min_size); - } - - function compute($in_size, $coi, &$crop_rect, &$scale_size) { $destCrop = new ImageRect($in_size); @@ -302,13 +273,6 @@ final class DerivativeParams $this->sizing->add_url_tokens($tokens); } - static function from_url_tokens($tokens) - { - $sizing = SizingParams::from_url_tokens($tokens); - $ret = new DerivativeParams($sizing); - return $ret; - } - function compute_final_size($in_size, $coi) { $this->sizing->compute( $in_size, $coi, $crop_rect, $scale_size ); diff --git a/include/derivative_std_params.inc.php b/include/derivative_std_params.inc.php index 9377b4c67..773866dec 100644 --- a/include/derivative_std_params.inc.php +++ b/include/derivative_std_params.inc.php @@ -46,6 +46,7 @@ final class ImageStdParams private static $type_map = array(); private static $undefined_type_map = array(); private static $watermark; + public static $custom = array(); static function get_all_types() { @@ -71,6 +72,22 @@ final class ImageStdParams { return self::$all_type_map[$type]; } + + static function get_custom($w, $h, $crop=0, $minw=null, $minh=null) + { + $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) ); + self::apply_global($params); + + $key = array(); + $params->add_url_tokens($key); + $key = implode('_',$key); + if ( @self::$custom[$key] < time() - 24*3600) + { + self::$custom[$key] = time(); + self::save(); + } + return $params; + } static function get_watermark() { @@ -103,6 +120,8 @@ final class ImageStdParams self::$type_map = $arr['d']; self::$watermark = @$arr['w']; if (!self::$watermark) self::$watermark = new WatermarkParams(); + self::$custom = @$arr['c']; + if (!self::$custom) self::$custom = array(); } else { @@ -118,16 +137,22 @@ final class ImageStdParams static function set_and_save($map) { - global $conf; self::$type_map = $map; + self::save(); + self::build_maps(); + } + + static function save() + { + global $conf; $ser = serialize( array( 'd' => self::$type_map, 'w' => self::$watermark, + 'c' => self::$custom, ) ); conf_update_param('derivatives', addslashes($ser) ); file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat', $ser); - self::build_maps(); } private static function make_default() @@ -142,7 +167,7 @@ final class ImageStdParams self::$type_map[IMG_XXLARGE] = new DerivativeParams( SizingParams::classic(1200,900) ); } - public static function apply_global($params) + static function apply_global($params) { if (!empty(self::$watermark->file) && (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0] diff --git a/include/picture_comment.inc.php b/include/picture_comment.inc.php index da674482f..7432aa8d5 100644 --- a/include/picture_comment.inc.php +++ b/include/picture_comment.inc.php @@ -126,26 +126,12 @@ SELECT if ($row['nb_comments'] > 0) { // comments order (get, session, conf) - if (!empty($_GET['comments_order'])) + if (!empty($_GET['comments_order']) && in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC'))) { - if (in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC'))) - { - $comments_order = $_GET['comments_order']; - pwg_set_session_var('comments_order', $comments_order); - } - else - { - $comments_order = $conf['comments_order']; - } - } - else if (pwg_get_session_var('comments_order') !== null) - { - $comments_order = pwg_get_session_var('comments_order'); - } - else - { - $comments_order = $conf['comments_order']; + pwg_set_session_var('comments_order', $_GET['comments_order']); } + $comments_order = pwg_get_session_var('comments_order', $conf['comments_order']); + $template->assign(array( 'COMMENTS_ORDER_URL' => duplicate_picture_url().'&comments_order='.($comments_order == 'ASC' ? 'DESC' : 'ASC'), 'COMMENTS_ORDER_TITLE' => $comments_order == 'ASC' ? l10n('ascending') : l10n('descending'), diff --git a/include/template.class.php b/include/template.class.php index 9e06b5d2d..5596c2ec9 100644 --- a/include/template.class.php +++ b/include/template.class.php @@ -559,31 +559,33 @@ class Template { !empty($params['width']) or fatal_error('define_derviative missing width'); !empty($params['height']) or fatal_error('define_derviative missing height'); - $derivative = new DerivativeParams( SizingParams::classic( intval($params['width']), intval($params['height'])) ); + $w = intval($params['width']); + $h = intval($params['height']); + $crop = 0; + $minw=null; + $minh=null; + if (isset($params['crop'])) { if (is_bool($params['crop'])) { - $derivative->sizing->max_crop = $params['crop'] ? 1:0; + $crop = $params['crop'] ? 1:0; } else { - $derivative->sizing->max_crop = round($params['crop']/100, 2); + $crop = round($params['crop']/100, 2); } - if ($derivative->sizing->max_crop) + if ($crop) { - $minw = empty($params['min_width']) ? $derivative->max_width() : intval($params['min_width']); - $minw <= $derivative->max_width() or fatal_error('define_derviative invalid min_width'); - $minh = empty($params['min_height']) ? $derivative->max_height() : intval($params['min_height']); - $minh <= $derivative->max_height() or fatal_error('define_derviative invalid min_height'); - - $derivative->sizing->min_size = array($minw, $minh); + $minw = empty($params['min_width']) ? $w : intval($params['min_width']); + $minw <= $w or fatal_error('define_derviative invalid min_width'); + $minh = empty($params['min_height']) ? $h : intval($params['min_height']); + $minh <= $h or fatal_error('define_derviative invalid min_height'); } } - ImageStdParams::apply_global($derivative); - $smarty->assign( $params['name'], $derivative); + $smarty->assign( $params['name'], ImageStdParams::get_custom($w, $h, $crop, $minw, $minh) ); } /** diff --git a/themes/default/template/picture.tpl b/themes/default/template/picture.tpl index 430d0e3c5..ff9ad2729 100644 --- a/themes/default/template/picture.tpl +++ b/themes/default/template/picture.tpl @@ -278,10 +278,10 @@ y.callService(
{if $COMMENT_COUNT > 0}

{$pwg->l10n_dec('%d comment', '%d comments',$COMMENT_COUNT)}

+ {if $COMMENT_COUNT > 2} + {'Sort order'|@translate}: {$COMMENTS_ORDER_TITLE} + {/if} {/if} - {if $COMMENT_COUNT > 2} - {'Sort order'|@translate} : {$COMMENTS_ORDER_TITLE} - {/if} {if !empty($navbar)}{include file='navigation_bar.tpl'|@get_extent:'navbar'}{/if} {if isset($comments)} -- cgit v1.2.3