| 1 | <?php |
|---|
| 2 | // +-----------------------------------------------------------------------+ |
|---|
| 3 | // | Piwigo - a PHP based photo gallery | |
|---|
| 4 | // +-----------------------------------------------------------------------+ |
|---|
| 5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
|---|
| 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
|---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
|---|
| 8 | // +-----------------------------------------------------------------------+ |
|---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
|---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
|---|
| 11 | // | the Free Software Foundation | |
|---|
| 12 | // | | |
|---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
|---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
|---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
|---|
| 16 | // | General Public License for more details. | |
|---|
| 17 | // | | |
|---|
| 18 | // | You should have received a copy of the GNU General Public License | |
|---|
| 19 | // | along with this program; if not, write to the Free Software | |
|---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
|---|
| 21 | // | USA. | |
|---|
| 22 | // +-----------------------------------------------------------------------+ |
|---|
| 23 | |
|---|
| 24 | if( !defined("PHPWG_ROOT_PATH") ) |
|---|
| 25 | { |
|---|
| 26 | die ("Hacking attempt!"); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | $errors = array(); |
|---|
| 30 | |
|---|
| 31 | // original resize |
|---|
| 32 | $original_fields = array( |
|---|
| 33 | 'original_resize', |
|---|
| 34 | 'original_resize_maxwidth', |
|---|
| 35 | 'original_resize_maxheight', |
|---|
| 36 | 'original_resize_quality', |
|---|
| 37 | ); |
|---|
| 38 | |
|---|
| 39 | $updates = array(); |
|---|
| 40 | |
|---|
| 41 | foreach ($original_fields as $field) |
|---|
| 42 | { |
|---|
| 43 | $value = !empty($_POST[$field]) ? $_POST[$field] : null; |
|---|
| 44 | $updates[$field] = $value; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | save_upload_form_config($updates, $page['errors'], $errors); |
|---|
| 48 | |
|---|
| 49 | if ($_POST['resize_quality'] < 50 or $_POST['resize_quality'] > 98) |
|---|
| 50 | { |
|---|
| 51 | $errors['resize_quality'] = '[50..98]'; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | $pderivatives = $_POST['d']; |
|---|
| 55 | |
|---|
| 56 | // step 1 - sanitize HTML input |
|---|
| 57 | foreach ($pderivatives as $type => &$pderivative) |
|---|
| 58 | { |
|---|
| 59 | if ($pderivative['must_square'] = ($type==IMG_SQUARE ? true : false)) |
|---|
| 60 | { |
|---|
| 61 | $pderivative['h'] = $pderivative['w']; |
|---|
| 62 | $pderivative['minh'] = $pderivative['minw'] = $pderivative['w']; |
|---|
| 63 | $pderivative['crop'] = 100; |
|---|
| 64 | } |
|---|
| 65 | $pderivative['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; |
|---|
| 66 | $pderivative['enabled'] = isset($pderivative['enabled']) || $pderivative['must_enable'] ? true : false; |
|---|
| 67 | |
|---|
| 68 | if (isset($pderivative['crop'])) |
|---|
| 69 | { |
|---|
| 70 | $pderivative['crop'] = 100; |
|---|
| 71 | $pderivative['minw'] = $pderivative['w']; |
|---|
| 72 | $pderivative['minh'] = $pderivative['h']; |
|---|
| 73 | } |
|---|
| 74 | else |
|---|
| 75 | { |
|---|
| 76 | $pderivative['crop'] = 0; |
|---|
| 77 | $pderivative['minw'] = null; |
|---|
| 78 | $pderivative['minh'] = null; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | unset($pderivative); |
|---|
| 82 | |
|---|
| 83 | // step 2 - check validity |
|---|
| 84 | $prev_w = $prev_h = 0; |
|---|
| 85 | foreach(ImageStdParams::get_all_types() as $type) |
|---|
| 86 | { |
|---|
| 87 | $pderivative = $pderivatives[$type]; |
|---|
| 88 | if (!$pderivative['enabled']) |
|---|
| 89 | { |
|---|
| 90 | continue; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | if ($type == IMG_THUMB) |
|---|
| 94 | { |
|---|
| 95 | $w = intval($pderivative['w']); |
|---|
| 96 | if ($w <= 0) |
|---|
| 97 | { |
|---|
| 98 | $errors[$type]['w'] = '>0'; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | $h = intval($pderivative['h']); |
|---|
| 102 | if ($h <= 0) |
|---|
| 103 | { |
|---|
| 104 | $errors[$type]['h'] = '>0'; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | if (max($w,$h) <= $prev_w) |
|---|
| 108 | { |
|---|
| 109 | $errors[$type]['w'] = $errors[$type]['h'] = '>'.$prev_w; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | else |
|---|
| 113 | { |
|---|
| 114 | $v = intval($pderivative['w']); |
|---|
| 115 | if ($v <= 0 or $v <= $prev_w) |
|---|
| 116 | { |
|---|
| 117 | $errors[$type]['w'] = '>'.$prev_w; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | $v = intval($pderivative['h']); |
|---|
| 121 | if ($v <= 0 or $v <= $prev_h) |
|---|
| 122 | { |
|---|
| 123 | $errors[$type]['h'] = '>'.$prev_h; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | if (count($errors) == 0) |
|---|
| 128 | { |
|---|
| 129 | $prev_w = intval($pderivative['w']); |
|---|
| 130 | $prev_h = intval($pderivative['h']); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | // step 3 - save data |
|---|
| 135 | if (count($errors) == 0) |
|---|
| 136 | { |
|---|
| 137 | $enabled = ImageStdParams::get_defined_type_map(); |
|---|
| 138 | $disabled = @unserialize( @$conf['disabled_derivatives'] ); |
|---|
| 139 | if ($disabled === false) |
|---|
| 140 | { |
|---|
| 141 | $disabled = array(); |
|---|
| 142 | } |
|---|
| 143 | $changed_types = array(); |
|---|
| 144 | |
|---|
| 145 | foreach (ImageStdParams::get_all_types() as $type) |
|---|
| 146 | { |
|---|
| 147 | $pderivative = $pderivatives[$type]; |
|---|
| 148 | |
|---|
| 149 | if ($pderivative['enabled']) |
|---|
| 150 | { |
|---|
| 151 | $new_params = new DerivativeParams( |
|---|
| 152 | new SizingParams( |
|---|
| 153 | array(intval($pderivative['w']), intval($pderivative['h'])), |
|---|
| 154 | round($pderivative['crop'] / 100, 2), |
|---|
| 155 | array(intval($pderivative['minw']), intval($pderivative['minh'])) |
|---|
| 156 | ) |
|---|
| 157 | ); |
|---|
| 158 | |
|---|
| 159 | $new_params->quality = intval($_POST['resize_quality']); |
|---|
| 160 | |
|---|
| 161 | ImageStdParams::apply_global($new_params); |
|---|
| 162 | |
|---|
| 163 | if (isset($enabled[$type])) |
|---|
| 164 | { |
|---|
| 165 | $old_params = $enabled[$type]; |
|---|
| 166 | $same = true; |
|---|
| 167 | if (!size_equals($old_params->sizing->ideal_size, $new_params->sizing->ideal_size) |
|---|
| 168 | or $old_params->sizing->max_crop != $new_params->sizing->max_crop) |
|---|
| 169 | { |
|---|
| 170 | $same = false; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | if ($same |
|---|
| 174 | and $new_params->sizing->max_crop != 0 |
|---|
| 175 | and !size_equals($old_params->sizing->min_size, $new_params->sizing->min_size)) |
|---|
| 176 | { |
|---|
| 177 | $same = false; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | if ($new_params->quality != $old_params->quality) |
|---|
| 181 | { |
|---|
| 182 | $same = false; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if (!$same) |
|---|
| 186 | { |
|---|
| 187 | $new_params->last_mod_time = time(); |
|---|
| 188 | $changed_types[] = $type; |
|---|
| 189 | } |
|---|
| 190 | else |
|---|
| 191 | { |
|---|
| 192 | $new_params->last_mod_time = $old_params->last_mod_time; |
|---|
| 193 | } |
|---|
| 194 | $enabled[$type] = $new_params; |
|---|
| 195 | } |
|---|
| 196 | else |
|---|
| 197 | {// now enabled, before was disabled |
|---|
| 198 | $enabled[$type] = $new_params; |
|---|
| 199 | unset($disabled[$type]); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | else |
|---|
| 203 | {// disabled |
|---|
| 204 | if (isset($enabled[$type])) |
|---|
| 205 | {// now disabled, before was enabled |
|---|
| 206 | $changed_types[] = $type; |
|---|
| 207 | $disabled[$type] = $enabled[$type]; |
|---|
| 208 | unset($enabled[$type]); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | $enabled_by = array(); // keys ordered by all types |
|---|
| 214 | foreach(ImageStdParams::get_all_types() as $type) |
|---|
| 215 | { |
|---|
| 216 | if (isset($enabled[$type])) |
|---|
| 217 | { |
|---|
| 218 | $enabled_by[$type] = $enabled[$type]; |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | ImageStdParams::set_and_save($enabled_by); |
|---|
| 223 | if (count($disabled) == 0) |
|---|
| 224 | { |
|---|
| 225 | $query='DELETE FROM '.CONFIG_TABLE.' WHERE param = \'disabled_derivatives\''; |
|---|
| 226 | pwg_query($query); |
|---|
| 227 | } |
|---|
| 228 | else |
|---|
| 229 | { |
|---|
| 230 | conf_update_param('disabled_derivatives', addslashes(serialize($disabled)) ); |
|---|
| 231 | } |
|---|
| 232 | $conf['disabled_derivatives'] = serialize($disabled); |
|---|
| 233 | |
|---|
| 234 | if (count($changed_types)) |
|---|
| 235 | { |
|---|
| 236 | clear_derivative_cache($changed_types); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | array_push( |
|---|
| 240 | $page['infos'], |
|---|
| 241 | l10n('Your configuration settings are saved') |
|---|
| 242 | ); |
|---|
| 243 | } |
|---|
| 244 | else |
|---|
| 245 | { |
|---|
| 246 | foreach ($original_fields as $field) |
|---|
| 247 | { |
|---|
| 248 | if (isset($_POST[$field])) |
|---|
| 249 | { |
|---|
| 250 | $template->append( |
|---|
| 251 | 'sizes', |
|---|
| 252 | array( |
|---|
| 253 | $field => $_POST[$field] |
|---|
| 254 | ), |
|---|
| 255 | true |
|---|
| 256 | ); |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | $template->assign('derivatives', $pderivatives); |
|---|
| 261 | $template->assign('ferrors', $errors); |
|---|
| 262 | $template->assign('resize_quality', $_POST['resize_quality']); |
|---|
| 263 | $page['sizes_loaded_in_tpl'] = true; |
|---|
| 264 | } |
|---|
| 265 | ?> |
|---|