1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2013 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 | $v = intval($pderivative['sharpen']); |
---|
134 | if ($v<0 || $v>100) |
---|
135 | { |
---|
136 | $errors[$type]['sharpen'] = '[0..100]'; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | // step 3 - save data |
---|
141 | if (count($errors) == 0) |
---|
142 | { |
---|
143 | $quality_changed = ImageStdParams::$quality != intval($_POST['resize_quality']); |
---|
144 | ImageStdParams::$quality = intval($_POST['resize_quality']); |
---|
145 | |
---|
146 | $enabled = ImageStdParams::get_defined_type_map(); |
---|
147 | $disabled = @unserialize( @$conf['disabled_derivatives'] ); |
---|
148 | if ($disabled === false) |
---|
149 | { |
---|
150 | $disabled = array(); |
---|
151 | } |
---|
152 | $changed_types = array(); |
---|
153 | |
---|
154 | foreach (ImageStdParams::get_all_types() as $type) |
---|
155 | { |
---|
156 | $pderivative = $pderivatives[$type]; |
---|
157 | |
---|
158 | if ($pderivative['enabled']) |
---|
159 | { |
---|
160 | $new_params = new DerivativeParams( |
---|
161 | new SizingParams( |
---|
162 | array(intval($pderivative['w']), intval($pderivative['h'])), |
---|
163 | round($pderivative['crop'] / 100, 2), |
---|
164 | array(intval($pderivative['minw']), intval($pderivative['minh'])) |
---|
165 | ) |
---|
166 | ); |
---|
167 | $new_params->sharpen = intval($pderivative['sharpen']); |
---|
168 | |
---|
169 | ImageStdParams::apply_global($new_params); |
---|
170 | |
---|
171 | if (isset($enabled[$type])) |
---|
172 | { |
---|
173 | $old_params = $enabled[$type]; |
---|
174 | $same = true; |
---|
175 | if (!size_equals($old_params->sizing->ideal_size, $new_params->sizing->ideal_size) |
---|
176 | or $old_params->sizing->max_crop != $new_params->sizing->max_crop) |
---|
177 | { |
---|
178 | $same = false; |
---|
179 | } |
---|
180 | |
---|
181 | if ($same |
---|
182 | and $new_params->sizing->max_crop != 0 |
---|
183 | and !size_equals($old_params->sizing->min_size, $new_params->sizing->min_size)) |
---|
184 | { |
---|
185 | $same = false; |
---|
186 | } |
---|
187 | |
---|
188 | if ($quality_changed |
---|
189 | || $new_params->sharpen != $old_params->sharpen) |
---|
190 | { |
---|
191 | $same = false; |
---|
192 | } |
---|
193 | |
---|
194 | if (!$same) |
---|
195 | { |
---|
196 | $new_params->last_mod_time = time(); |
---|
197 | $changed_types[] = $type; |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | $new_params->last_mod_time = $old_params->last_mod_time; |
---|
202 | } |
---|
203 | $enabled[$type] = $new_params; |
---|
204 | } |
---|
205 | else |
---|
206 | {// now enabled, before was disabled |
---|
207 | $enabled[$type] = $new_params; |
---|
208 | unset($disabled[$type]); |
---|
209 | } |
---|
210 | } |
---|
211 | else |
---|
212 | {// disabled |
---|
213 | if (isset($enabled[$type])) |
---|
214 | {// now disabled, before was enabled |
---|
215 | $changed_types[] = $type; |
---|
216 | $disabled[$type] = $enabled[$type]; |
---|
217 | unset($enabled[$type]); |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | $enabled_by = array(); // keys ordered by all types |
---|
223 | foreach(ImageStdParams::get_all_types() as $type) |
---|
224 | { |
---|
225 | if (isset($enabled[$type])) |
---|
226 | { |
---|
227 | $enabled_by[$type] = $enabled[$type]; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | foreach( array_keys(ImageStdParams::$custom) as $custom) |
---|
232 | { |
---|
233 | if (isset($_POST['delete_custom_derivative_'.$custom])) |
---|
234 | { |
---|
235 | $changed_types[] = $custom; |
---|
236 | unset(ImageStdParams::$custom[$custom]); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | ImageStdParams::set_and_save($enabled_by); |
---|
241 | if (count($disabled) == 0) |
---|
242 | { |
---|
243 | $query='DELETE FROM '.CONFIG_TABLE.' WHERE param = \'disabled_derivatives\''; |
---|
244 | pwg_query($query); |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | conf_update_param('disabled_derivatives', addslashes(serialize($disabled)) ); |
---|
249 | } |
---|
250 | $conf['disabled_derivatives'] = serialize($disabled); |
---|
251 | |
---|
252 | if (count($changed_types)) |
---|
253 | { |
---|
254 | clear_derivative_cache($changed_types); |
---|
255 | } |
---|
256 | |
---|
257 | array_push( |
---|
258 | $page['infos'], |
---|
259 | l10n('Your configuration settings are saved') |
---|
260 | ); |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | foreach ($original_fields as $field) |
---|
265 | { |
---|
266 | if (isset($_POST[$field])) |
---|
267 | { |
---|
268 | $template->append( |
---|
269 | 'sizes', |
---|
270 | array( |
---|
271 | $field => $_POST[$field] |
---|
272 | ), |
---|
273 | true |
---|
274 | ); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | $template->assign('derivatives', $pderivatives); |
---|
279 | $template->assign('ferrors', $errors); |
---|
280 | $template->assign('resize_quality', $_POST['resize_quality']); |
---|
281 | $page['sizes_loaded_in_tpl'] = true; |
---|
282 | } |
---|
283 | ?> |
---|