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