1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2011 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 | 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']; |
---|
31 | |
---|
32 | // step 1 - sanitize HTML input |
---|
33 | foreach($pderivatives as $type => &$pderivative) |
---|
34 | { |
---|
35 | if ($pderivative['must_square'] = ($type==IMG_SQUARE ? true : false)) |
---|
36 | { |
---|
37 | $pderivative['h'] = $pderivative['w']; |
---|
38 | $pderivative['minh'] = $pderivative['minw'] = $pderivative['w']; |
---|
39 | $pderivative['crop'] = 100; |
---|
40 | } |
---|
41 | $pderivative['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; |
---|
42 | $pderivative['enabled'] = isset($pderivative['enabled']) || $pderivative['must_enable'] ? true : false; |
---|
43 | } |
---|
44 | unset($pderivative); |
---|
45 | |
---|
46 | // step 2 - check validity |
---|
47 | $prev_w = $prev_h = 0; |
---|
48 | foreach(ImageStdParams::get_all_types() as $type) |
---|
49 | { |
---|
50 | $pderivative = $pderivatives[$type]; |
---|
51 | if (!$pderivative['enabled']) |
---|
52 | continue; |
---|
53 | |
---|
54 | $v = intval($pderivative['w']); |
---|
55 | if ($v<=0 || $v<=$prev_w) |
---|
56 | { |
---|
57 | $errors[$type]['w'] = '>'.$prev_w; |
---|
58 | } |
---|
59 | $v = intval($pderivative['h']); |
---|
60 | if ($v<=0 || $v<=$prev_h) |
---|
61 | { |
---|
62 | $errors[$type]['h'] = '>'.$prev_h; |
---|
63 | } |
---|
64 | $v = intval($pderivative['crop']); |
---|
65 | if ($v<0 || $v>100) |
---|
66 | { |
---|
67 | $errors[$type]['crop'] = '[0..100]'; |
---|
68 | } |
---|
69 | |
---|
70 | if ($v!=0) |
---|
71 | { |
---|
72 | $v = intval($pderivative['minw']); |
---|
73 | if ($v<0 || $v>intval($pderivative['w'])) |
---|
74 | { |
---|
75 | $errors[$type]['minw'] = '[0..'.intval($pderivative['w']).']'; |
---|
76 | } |
---|
77 | $v = intval($pderivative['minh']); |
---|
78 | if ($v<0 || $v>intval($pderivative['h'])) |
---|
79 | { |
---|
80 | $errors[$type]['minh'] = '[0..'.intval($pderivative['h']).']'; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | if (count($errors)==0) |
---|
85 | { |
---|
86 | $prev_w = intval($pderivative['w']); |
---|
87 | $prev_h = intval($pderivative['h']); |
---|
88 | } |
---|
89 | } |
---|
90 | // step 3 - save data |
---|
91 | if (count($errors)==0) |
---|
92 | { |
---|
93 | $enabled = ImageStdParams::get_defined_type_map(); |
---|
94 | $disabled = @unserialize( @$conf['disabled_derivatives'] ); |
---|
95 | if ($disabled===false) |
---|
96 | { |
---|
97 | $disabled = array(); |
---|
98 | } |
---|
99 | $changed_types = array(); |
---|
100 | |
---|
101 | foreach(ImageStdParams::get_all_types() as $type) |
---|
102 | { |
---|
103 | $pderivative = $pderivatives[$type]; |
---|
104 | |
---|
105 | if ($pderivative['enabled']) |
---|
106 | { |
---|
107 | $new_params = new DerivativeParams( |
---|
108 | new SizingParams( |
---|
109 | array($pderivative['w'],$pderivative['h']), |
---|
110 | round($pderivative['crop'] / 100, 2), |
---|
111 | array($pderivative['minw'],$pderivative['minh']) |
---|
112 | ) |
---|
113 | ); |
---|
114 | if (isset($enabled[$type])) |
---|
115 | { |
---|
116 | $old_params = $enabled[$type]; |
---|
117 | $same = true; |
---|
118 | if ( !size_equals($old_params->sizing->ideal_size, $new_params->sizing->ideal_size) |
---|
119 | or $old_params->sizing->max_crop != $new_params->sizing->max_crop) |
---|
120 | { |
---|
121 | $same = false; |
---|
122 | } |
---|
123 | |
---|
124 | if ( $same && $new_params->sizing->max_crop != 0 |
---|
125 | && !size_equals($old_params->sizing->min_size, $new_params->sizing->min_size) ) |
---|
126 | { |
---|
127 | $same = false; |
---|
128 | } |
---|
129 | |
---|
130 | if (!$same) |
---|
131 | { |
---|
132 | $new_params->last_mod_time = time(); |
---|
133 | $changed_types[] = $type; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | $new_params->last_mod_time = $old_params->last_mod_time; |
---|
138 | } |
---|
139 | $enabled[$type] = $new_params; |
---|
140 | } |
---|
141 | else |
---|
142 | {// now enabled, before was disabled |
---|
143 | $enabled[$type] = $new_params; |
---|
144 | unset($disabled[$type]); |
---|
145 | } |
---|
146 | } |
---|
147 | else |
---|
148 | {// disabled |
---|
149 | if (isset($enabled[$type])) |
---|
150 | {// now disabled, before was enabled |
---|
151 | $disabled[$type] = $enabled[$type]; |
---|
152 | unset($enabled[$type]); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | $enabled_by = array(); // keys ordered by all types |
---|
158 | foreach(ImageStdParams::get_all_types() as $type) |
---|
159 | { |
---|
160 | if (isset($enabled[$type])) |
---|
161 | { |
---|
162 | $enabled_by[$type] = $enabled[$type]; |
---|
163 | } |
---|
164 | } |
---|
165 | ImageStdParams::set_and_save($enabled_by); |
---|
166 | if (count($disabled)==0) |
---|
167 | { |
---|
168 | $query='DELETE FROM '.CONFIG_TABLE.' WHERE param = \'disabled_derivatives\''; |
---|
169 | pwg_query($query); |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | conf_update_param('disabled_derivatives', addslashes(serialize($disabled)) ); |
---|
174 | } |
---|
175 | $conf['disabled_derivatives']=serialize($disabled); |
---|
176 | |
---|
177 | if (count($changed_types)) |
---|
178 | { |
---|
179 | clear_derivative_cache($changed_types); |
---|
180 | } |
---|
181 | } |
---|
182 | else |
---|
183 | { |
---|
184 | $template->assign('derivatives', $pderivatives); |
---|
185 | $template->assign('ferrors', $errors); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | if (count($errors)==0) |
---|
190 | { |
---|
191 | $enabled = ImageStdParams::get_defined_type_map(); |
---|
192 | $disabled = @unserialize( @$conf['disabled_derivatives'] ); |
---|
193 | if ($disabled===false) |
---|
194 | { |
---|
195 | $disabled = array(); |
---|
196 | } |
---|
197 | |
---|
198 | $tpl_vars = array(); |
---|
199 | foreach(ImageStdParams::get_all_types() as $type) |
---|
200 | { |
---|
201 | $tpl_var = array(); |
---|
202 | |
---|
203 | $tpl_var['must_square'] = ($type==IMG_SQUARE ? true : false); |
---|
204 | $tpl_var['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; |
---|
205 | |
---|
206 | if ($params=@$enabled[$type]) |
---|
207 | { |
---|
208 | $tpl_var['enabled']=true; |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | $tpl_var['enabled']=false; |
---|
213 | $params=@$disabled[$type]; |
---|
214 | } |
---|
215 | |
---|
216 | if ($params) |
---|
217 | { |
---|
218 | list($tpl_var['w'],$tpl_var['h']) = $params->sizing->ideal_size; |
---|
219 | if ( ($tpl_var['crop'] = round(100*$params->sizing->max_crop)) > 0) |
---|
220 | { |
---|
221 | list($tpl_var['minw'],$tpl_var['minh']) = $params->sizing->min_size; |
---|
222 | } |
---|
223 | else |
---|
224 | { |
---|
225 | $tpl_var['minw'] = $tpl_var['minh'] = ""; |
---|
226 | } |
---|
227 | } |
---|
228 | $tpl_vars[$type]=$tpl_var; |
---|
229 | } |
---|
230 | $template->assign('derivatives', $tpl_vars); |
---|
231 | } |
---|
232 | |
---|
233 | $template->set_filename('derivatives', 'derivatives.tpl'); |
---|
234 | $template->assign_var_from_handle('ADMIN_CONTENT', 'derivatives'); |
---|
235 | ?> |
---|