[12820] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | Piwigo - a PHP based photo gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[12922] | 5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
---|
[12820] | 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 | defined('PHPWG_ROOT_PATH') or trigger_error('Hacking attempt!', E_USER_ERROR); |
---|
| 25 | |
---|
| 26 | $errors = array(); |
---|
| 27 | |
---|
| 28 | if ( isset($_POST['d']) ) |
---|
| 29 | { |
---|
| 30 | $pderivatives = $_POST['d']; |
---|
[12851] | 31 | $pwatermark = $_POST['w']; |
---|
[12820] | 32 | |
---|
| 33 | // step 1 - sanitize HTML input |
---|
| 34 | foreach($pderivatives as $type => &$pderivative) |
---|
| 35 | { |
---|
| 36 | if ($pderivative['must_square'] = ($type==IMG_SQUARE ? true : false)) |
---|
| 37 | { |
---|
| 38 | $pderivative['h'] = $pderivative['w']; |
---|
| 39 | $pderivative['minh'] = $pderivative['minw'] = $pderivative['w']; |
---|
| 40 | $pderivative['crop'] = 100; |
---|
| 41 | } |
---|
| 42 | $pderivative['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; |
---|
| 43 | $pderivative['enabled'] = isset($pderivative['enabled']) || $pderivative['must_enable'] ? true : false; |
---|
| 44 | } |
---|
| 45 | unset($pderivative); |
---|
| 46 | |
---|
| 47 | // step 2 - check validity |
---|
| 48 | $prev_w = $prev_h = 0; |
---|
| 49 | foreach(ImageStdParams::get_all_types() as $type) |
---|
| 50 | { |
---|
| 51 | $pderivative = $pderivatives[$type]; |
---|
| 52 | if (!$pderivative['enabled']) |
---|
| 53 | continue; |
---|
| 54 | |
---|
[13258] | 55 | if ($type==IMG_THUMB) |
---|
| 56 | { |
---|
| 57 | $w = intval($pderivative['w']); |
---|
| 58 | if ($w<=0) |
---|
| 59 | { |
---|
| 60 | $errors[$type]['w'] = '>0'; |
---|
| 61 | } |
---|
| 62 | $h = intval($pderivative['h']); |
---|
| 63 | if ($h<=0) |
---|
| 64 | { |
---|
| 65 | $errors[$type]['h'] = '>0'; |
---|
| 66 | } |
---|
| 67 | if (max($w,$h) <= $prev_w) |
---|
| 68 | { |
---|
| 69 | $errors[$type]['w'] = $errors[$type]['h'] = '>'.$prev_w; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | else |
---|
| 73 | { |
---|
| 74 | $v = intval($pderivative['w']); |
---|
| 75 | if ($v<=0 || $v<=$prev_w) |
---|
| 76 | { |
---|
| 77 | $errors[$type]['w'] = '>'.$prev_w; |
---|
| 78 | } |
---|
| 79 | $v = intval($pderivative['h']); |
---|
| 80 | if ($v<=0 || $v<=$prev_h) |
---|
| 81 | { |
---|
| 82 | $errors[$type]['h'] = '>'.$prev_h; |
---|
| 83 | } |
---|
| 84 | } |
---|
[12820] | 85 | $v = intval($pderivative['crop']); |
---|
| 86 | if ($v<0 || $v>100) |
---|
| 87 | { |
---|
| 88 | $errors[$type]['crop'] = '[0..100]'; |
---|
| 89 | } |
---|
[13035] | 90 | |
---|
[12820] | 91 | if ($v!=0) |
---|
| 92 | { |
---|
| 93 | $v = intval($pderivative['minw']); |
---|
| 94 | if ($v<0 || $v>intval($pderivative['w'])) |
---|
| 95 | { |
---|
| 96 | $errors[$type]['minw'] = '[0..'.intval($pderivative['w']).']'; |
---|
| 97 | } |
---|
| 98 | $v = intval($pderivative['minh']); |
---|
| 99 | if ($v<0 || $v>intval($pderivative['h'])) |
---|
| 100 | { |
---|
| 101 | $errors[$type]['minh'] = '[0..'.intval($pderivative['h']).']'; |
---|
| 102 | } |
---|
| 103 | } |
---|
[13035] | 104 | |
---|
[12820] | 105 | if (count($errors)==0) |
---|
| 106 | { |
---|
| 107 | $prev_w = intval($pderivative['w']); |
---|
| 108 | $prev_h = intval($pderivative['h']); |
---|
| 109 | } |
---|
[13035] | 110 | |
---|
[12851] | 111 | $v = intval($pderivative['sharpen']); |
---|
| 112 | if ($v<0 || $v>100) |
---|
| 113 | { |
---|
| 114 | $errors[$type]['sharpen'] = '[0..100]'; |
---|
| 115 | } |
---|
| 116 | $v = intval($pderivative['quality']); |
---|
| 117 | if ($v<=0 || $v>100) |
---|
| 118 | { |
---|
| 119 | $errors[$type]['quality'] = '(0..100]'; |
---|
| 120 | } |
---|
[12820] | 121 | } |
---|
[12851] | 122 | $v = intval($pwatermark['xpos']); |
---|
| 123 | if ($v<0 || $v>100) |
---|
| 124 | { |
---|
| 125 | $errors['watermark']['xpos'] = '[0..100]'; |
---|
| 126 | } |
---|
| 127 | $v = intval($pwatermark['ypos']); |
---|
| 128 | if ($v<0 || $v>100) |
---|
| 129 | { |
---|
| 130 | $errors['watermark']['ypos'] = '[0..100]'; |
---|
| 131 | } |
---|
| 132 | $v = intval($pwatermark['opacity']); |
---|
| 133 | if ($v<=0 || $v>100) |
---|
| 134 | { |
---|
| 135 | $errors['watermark']['opacity'] = '(0..100]'; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
[12820] | 139 | // step 3 - save data |
---|
| 140 | if (count($errors)==0) |
---|
| 141 | { |
---|
[12851] | 142 | $watermark = new WatermarkParams(); |
---|
| 143 | $watermark->file = $pwatermark['file']; |
---|
| 144 | $watermark->xpos = intval($pwatermark['xpos']); |
---|
| 145 | $watermark->ypos = intval($pwatermark['ypos']); |
---|
| 146 | $watermark->xrepeat = intval($pwatermark['xrepeat']); |
---|
| 147 | $watermark->opacity = intval($pwatermark['opacity']); |
---|
| 148 | $watermark->min_size = array(intval($pwatermark['minw']),intval($pwatermark['minh'])); |
---|
[13035] | 149 | |
---|
[12851] | 150 | $old_watermark = ImageStdParams::get_watermark(); |
---|
[13035] | 151 | $watermark_changed = |
---|
[12851] | 152 | $watermark->file != $old_watermark->file |
---|
| 153 | || $watermark->xpos != $old_watermark->xpos |
---|
| 154 | || $watermark->ypos != $old_watermark->ypos |
---|
| 155 | || $watermark->xrepeat != $old_watermark->xrepeat |
---|
| 156 | || $watermark->opacity != $old_watermark->opacity; |
---|
| 157 | ImageStdParams::set_watermark($watermark); |
---|
[13035] | 158 | |
---|
[12820] | 159 | $enabled = ImageStdParams::get_defined_type_map(); |
---|
| 160 | $disabled = @unserialize( @$conf['disabled_derivatives'] ); |
---|
| 161 | if ($disabled===false) |
---|
| 162 | { |
---|
| 163 | $disabled = array(); |
---|
| 164 | } |
---|
| 165 | $changed_types = array(); |
---|
[13035] | 166 | |
---|
[12820] | 167 | foreach(ImageStdParams::get_all_types() as $type) |
---|
| 168 | { |
---|
| 169 | $pderivative = $pderivatives[$type]; |
---|
[13035] | 170 | |
---|
[12820] | 171 | if ($pderivative['enabled']) |
---|
| 172 | { |
---|
| 173 | $new_params = new DerivativeParams( |
---|
[13035] | 174 | new SizingParams( |
---|
[12851] | 175 | array(intval($pderivative['w']), intval($pderivative['h'])), |
---|
[12820] | 176 | round($pderivative['crop'] / 100, 2), |
---|
[12851] | 177 | array(intval($pderivative['minw']), intval($pderivative['minh'])) |
---|
[13035] | 178 | ) |
---|
[12820] | 179 | ); |
---|
[12851] | 180 | $new_params->sharpen = intval($pderivative['sharpen']); |
---|
| 181 | $new_params->quality = intval($pderivative['quality']); |
---|
| 182 | ImageStdParams::apply_global($new_params); |
---|
[13035] | 183 | |
---|
[12820] | 184 | if (isset($enabled[$type])) |
---|
| 185 | { |
---|
| 186 | $old_params = $enabled[$type]; |
---|
| 187 | $same = true; |
---|
| 188 | if ( !size_equals($old_params->sizing->ideal_size, $new_params->sizing->ideal_size) |
---|
| 189 | or $old_params->sizing->max_crop != $new_params->sizing->max_crop) |
---|
| 190 | { |
---|
| 191 | $same = false; |
---|
| 192 | } |
---|
| 193 | |
---|
[13035] | 194 | if ( $same && $new_params->sizing->max_crop != 0 |
---|
[12820] | 195 | && !size_equals($old_params->sizing->min_size, $new_params->sizing->min_size) ) |
---|
| 196 | { |
---|
| 197 | $same = false; |
---|
| 198 | } |
---|
[12851] | 199 | |
---|
[13035] | 200 | if ( $same && |
---|
[12851] | 201 | ( $new_params->sharpen != $old_params->sharpen |
---|
| 202 | || $new_params->quality > $old_params->quality) |
---|
| 203 | ) |
---|
| 204 | { |
---|
| 205 | $same = false; |
---|
| 206 | } |
---|
[13035] | 207 | |
---|
[12851] | 208 | if ($same && |
---|
[13035] | 209 | ( $new_params->use_watermark != $old_params->use_watermark |
---|
[12851] | 210 | || $new_params->use_watermark && $watermark_changed ) |
---|
| 211 | ) |
---|
| 212 | { |
---|
| 213 | $same = false; |
---|
| 214 | } |
---|
[13035] | 215 | |
---|
[12820] | 216 | if (!$same) |
---|
| 217 | { |
---|
| 218 | $new_params->last_mod_time = time(); |
---|
| 219 | $changed_types[] = $type; |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
| 223 | $new_params->last_mod_time = $old_params->last_mod_time; |
---|
| 224 | } |
---|
| 225 | $enabled[$type] = $new_params; |
---|
| 226 | } |
---|
| 227 | else |
---|
| 228 | {// now enabled, before was disabled |
---|
| 229 | $enabled[$type] = $new_params; |
---|
| 230 | unset($disabled[$type]); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | else |
---|
| 234 | {// disabled |
---|
| 235 | if (isset($enabled[$type])) |
---|
| 236 | {// now disabled, before was enabled |
---|
[13021] | 237 | $changed_types[] = $type; |
---|
[12820] | 238 | $disabled[$type] = $enabled[$type]; |
---|
| 239 | unset($enabled[$type]); |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | $enabled_by = array(); // keys ordered by all types |
---|
| 245 | foreach(ImageStdParams::get_all_types() as $type) |
---|
| 246 | { |
---|
| 247 | if (isset($enabled[$type])) |
---|
| 248 | { |
---|
| 249 | $enabled_by[$type] = $enabled[$type]; |
---|
| 250 | } |
---|
[13035] | 251 | } |
---|
[12820] | 252 | ImageStdParams::set_and_save($enabled_by); |
---|
| 253 | if (count($disabled)==0) |
---|
| 254 | { |
---|
| 255 | $query='DELETE FROM '.CONFIG_TABLE.' WHERE param = \'disabled_derivatives\''; |
---|
| 256 | pwg_query($query); |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | conf_update_param('disabled_derivatives', addslashes(serialize($disabled)) ); |
---|
| 261 | } |
---|
| 262 | $conf['disabled_derivatives']=serialize($disabled); |
---|
[13035] | 263 | |
---|
[12820] | 264 | if (count($changed_types)) |
---|
| 265 | { |
---|
| 266 | clear_derivative_cache($changed_types); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | else |
---|
| 270 | { |
---|
| 271 | $template->assign('derivatives', $pderivatives); |
---|
[12851] | 272 | $template->assign('watermark', $pwatermark); |
---|
[12820] | 273 | $template->assign('ferrors', $errors); |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | if (count($errors)==0) |
---|
| 278 | { |
---|
| 279 | $enabled = ImageStdParams::get_defined_type_map(); |
---|
| 280 | $disabled = @unserialize( @$conf['disabled_derivatives'] ); |
---|
| 281 | if ($disabled===false) |
---|
| 282 | { |
---|
| 283 | $disabled = array(); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | $tpl_vars = array(); |
---|
| 287 | foreach(ImageStdParams::get_all_types() as $type) |
---|
| 288 | { |
---|
| 289 | $tpl_var = array(); |
---|
| 290 | |
---|
| 291 | $tpl_var['must_square'] = ($type==IMG_SQUARE ? true : false); |
---|
| 292 | $tpl_var['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; |
---|
| 293 | |
---|
| 294 | if ($params=@$enabled[$type]) |
---|
| 295 | { |
---|
| 296 | $tpl_var['enabled']=true; |
---|
| 297 | } |
---|
| 298 | else |
---|
| 299 | { |
---|
| 300 | $tpl_var['enabled']=false; |
---|
| 301 | $params=@$disabled[$type]; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | if ($params) |
---|
| 305 | { |
---|
| 306 | list($tpl_var['w'],$tpl_var['h']) = $params->sizing->ideal_size; |
---|
| 307 | if ( ($tpl_var['crop'] = round(100*$params->sizing->max_crop)) > 0) |
---|
| 308 | { |
---|
| 309 | list($tpl_var['minw'],$tpl_var['minh']) = $params->sizing->min_size; |
---|
| 310 | } |
---|
| 311 | else |
---|
| 312 | { |
---|
| 313 | $tpl_var['minw'] = $tpl_var['minh'] = ""; |
---|
| 314 | } |
---|
[12851] | 315 | $tpl_var['sharpen'] = $params->sharpen; |
---|
| 316 | $tpl_var['quality'] = $params->quality; |
---|
[12820] | 317 | } |
---|
| 318 | $tpl_vars[$type]=$tpl_var; |
---|
| 319 | } |
---|
| 320 | $template->assign('derivatives', $tpl_vars); |
---|
[13035] | 321 | |
---|
[12851] | 322 | $wm = ImageStdParams::get_watermark(); |
---|
| 323 | $template->assign('watermark', array( |
---|
| 324 | 'file' => $wm->file, |
---|
| 325 | 'minw' => $wm->min_size[0], |
---|
| 326 | 'minh' => $wm->min_size[1], |
---|
| 327 | 'xpos' => $wm->xpos, |
---|
| 328 | 'ypos' => $wm->ypos, |
---|
| 329 | 'xrepeat' => $wm->xrepeat, |
---|
| 330 | 'opacity' => $wm->opacity, |
---|
| 331 | )); |
---|
[12820] | 332 | } |
---|
| 333 | |
---|
[12851] | 334 | $watermark_files = array(); |
---|
| 335 | foreach (glob(PHPWG_ROOT_PATH.'themes/default/watermarks/*.png') as $file) |
---|
| 336 | { |
---|
| 337 | $watermark_files[] = substr($file, strlen(PHPWG_ROOT_PATH)); |
---|
| 338 | } |
---|
[13035] | 339 | foreach (glob(PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks/*.png') as $file) |
---|
| 340 | { |
---|
| 341 | $watermark_files[] = substr($file, strlen(PHPWG_ROOT_PATH)); |
---|
| 342 | } |
---|
[12851] | 343 | $watermark_filemap = array( '' => '---' ); |
---|
| 344 | foreach( $watermark_files as $file) |
---|
| 345 | { |
---|
| 346 | $display = basename($file); |
---|
| 347 | $watermark_filemap[$file] = $display; |
---|
| 348 | } |
---|
| 349 | $template->assign('watermark_files', $watermark_filemap); |
---|
| 350 | |
---|
[12820] | 351 | $template->set_filename('derivatives', 'derivatives.tpl'); |
---|
| 352 | $template->assign_var_from_handle('ADMIN_CONTENT', 'derivatives'); |
---|
| 353 | ?> |
---|