1 | <?php |
---|
2 | |
---|
3 | // +-----------------------------------------------------------------------+ |
---|
4 | // | Piwigo - a PHP based picture gallery | |
---|
5 | // +-----------------------------------------------------------------------+ |
---|
6 | // | Copyright(C) 2008-2009 Piwigo Team http://piwigo.org | |
---|
7 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
8 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
9 | // +-----------------------------------------------------------------------+ |
---|
10 | // | This program is free software; you can redistribute it and/or modify | |
---|
11 | // | it under the terms of the GNU General Public License as published by | |
---|
12 | // | the Free Software Foundation | |
---|
13 | // | | |
---|
14 | // | This program is distributed in the hope that it will be useful, but | |
---|
15 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
16 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
17 | // | General Public License for more details. | |
---|
18 | // | | |
---|
19 | // | You should have received a copy of the GNU General Public License | |
---|
20 | // | along with this program; if not, write to the Free Software | |
---|
21 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
22 | // | USA. | |
---|
23 | // +-----------------------------------------------------------------------+ |
---|
24 | |
---|
25 | // *************************************************************************** |
---|
26 | // ** evntcats_funcs.php : Functions (include) for Piwigo plugin Event Cats ** |
---|
27 | // *************************************************************************** |
---|
28 | |
---|
29 | global $conf, $ec_conf; |
---|
30 | |
---|
31 | include_once(EVNTCATS_PATH.'include/ec_conf.inc.php'); |
---|
32 | |
---|
33 | /* |
---|
34 | * build_ec_table() |
---|
35 | * builds a table showing the content of the <pwg>_event_cats database table, |
---|
36 | * and a table showing the eventual errors. |
---|
37 | * |
---|
38 | * @param |
---|
39 | * no parameters passed ; the main material on which works the function, is |
---|
40 | * the global array variable $ec_lists. |
---|
41 | * @return |
---|
42 | * (no return value) |
---|
43 | */ |
---|
44 | function build_ec_table() { |
---|
45 | global $ec_lists, $conf, $ec_ap_ok; |
---|
46 | |
---|
47 | $ec_lists['ec_table'] = array(); |
---|
48 | // $ec_lists['ec_codes'] = array(); // Useful ? |
---|
49 | |
---|
50 | if (isset($conf['auto_log'])) { |
---|
51 | // For tests purpose : table not in MySQL, but in $conf['xxx'] vars in |
---|
52 | // config_local.inc.php |
---|
53 | |
---|
54 | foreach ($conf['auto_log'] as $ec_current_code => $ec_entry) { |
---|
55 | $ec_current_entry = array_push($ec_lists['ec_table'], array( |
---|
56 | 'code' => $ec_current_code, |
---|
57 | 'user_id' => get_userid($ec_entry[$ec_current_code]), |
---|
58 | 'action' => 'ec_ok', |
---|
59 | 'arg1' => NULL, |
---|
60 | 'arg2' => NULL, |
---|
61 | 'forced' => 'false', |
---|
62 | )) - 1; // key($ec_lists['ec_table']) does not work as expected |
---|
63 | $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry; |
---|
64 | /* $ec_lists['ec_codes'][$ec_current_code] = array( |
---|
65 | 'count' => 1, |
---|
66 | 'id' => $ec_current_entry |
---|
67 | ); */ |
---|
68 | if (isset($conf['prior_page'])) { |
---|
69 | if (array_key_exists($ec_current_code, $conf['prior_page'])) { |
---|
70 | $ec_lists['ec_table'][$ec_current_entry]['arg2'] = |
---|
71 | $conf['prior_page'][$ec_current_code]; |
---|
72 | $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true'; |
---|
73 | } |
---|
74 | } |
---|
75 | if (isset($conf['outdated_page'])) { |
---|
76 | if (array_key_exists($ec_current_code, $conf['outdated_page'])) { |
---|
77 | $ec_lists['ec_table'][$ec_current_entry]['action'] = 'ec_nok'; |
---|
78 | $ec_lists['ec_table'][$ec_current_entry]['arg2'] = |
---|
79 | $conf['outdated_page'][$ec_current_code]; |
---|
80 | $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true'; |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // Inspection of $conf['outdated_page'] |
---|
86 | if (isset($conf['outdated_page'])) { |
---|
87 | foreach ($conf['outdated_page'] as $ec_current_code => $ec_current_AP) { |
---|
88 | if (!array_key_exists($ec_current_code, $conf['auto_log'])) { |
---|
89 | $ec_current_entry = array_push($ec_lists['ec_table'], array( |
---|
90 | 'code' => $ec_current_code, |
---|
91 | 'user_id' => '', |
---|
92 | 'action' => 'ec_nok', |
---|
93 | 'arg1' => NULL, |
---|
94 | 'arg2' => $ec_current_AP, |
---|
95 | 'forced' => 'true', |
---|
96 | )) - 1; // key($ec_lists['ec_table']) problem |
---|
97 | $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry; |
---|
98 | /* $ec_lists['ec_codes'][$ec_current_code] = array( |
---|
99 | 'id' => $ec_current_entry, |
---|
100 | 'count' => 1 |
---|
101 | ); */ |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | } |
---|
107 | else { // in "normal" use ($conf['auto_log'] NOT set), $ec_lists['ec_table'] |
---|
108 | // is built thanks to below code only. A little bit more simple... |
---|
109 | $q = pwg_query(' |
---|
110 | SELECT * FROM `'.EVNTCATS_TABLE.'` |
---|
111 | WHERE `code` IS NOT NULL |
---|
112 | ORDER BY `id` |
---|
113 | '); |
---|
114 | while ($r = mysql_fetch_assoc($q)) { |
---|
115 | $ec_lists['ec_table'][intval($r['id'])] = $r; |
---|
116 | /* $ec_current_entry = intval($r['id']); |
---|
117 | $ec_current_code = $r['code']; |
---|
118 | if (array_key_exists($ec_current_code, $ec_lists['ec_codes'])) { |
---|
119 | $ec_lists[$ec_current_code]['count']++; |
---|
120 | } |
---|
121 | else { |
---|
122 | $ec_lists['ec_codes'][$ec_current_code] = array( |
---|
123 | 'id' => $ec_current_entry, |
---|
124 | 'count' => 1 |
---|
125 | ); */ |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | // Construction of behaviour |
---|
130 | |
---|
131 | /* |
---|
132 | * ec_inpect() |
---|
133 | * goes through ec_lists['ec_table'] to check errors on (multiple) entries |
---|
134 | * using the same code. Cannot be used elsewhere than here in |
---|
135 | * build_ec_table() . |
---|
136 | * |
---|
137 | * @param |
---|
138 | * |
---|
139 | * @return |
---|
140 | * (no return value) |
---|
141 | */ |
---|
142 | function ec_inpect($checked_item, $new_action, |
---|
143 | $check_ec_nok = false, $check_forced = false) { |
---|
144 | global $ec_lists; |
---|
145 | $first = array(); |
---|
146 | $to_correct = array(); |
---|
147 | // $to_correct is needed because following code would not work anywhere : |
---|
148 | /* |
---|
149 | foreach ($table as $value) { |
---|
150 | if ($value == $value_to_check and $value == $problem) { |
---|
151 | foreach ($table as &$value2) { // key index of $table can be reset |
---|
152 | if ($value2 == $value_to_check) { |
---|
153 | $value2 = $better_value; |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | // It works with WinAmp Server, but not on Apache server of Free (french |
---|
159 | // Internet provider). |
---|
160 | */ |
---|
161 | foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is not really needed |
---|
162 | $ec_current_code = $ec_entry['code']; |
---|
163 | if ( |
---|
164 | $ec_entry['action'] == 'ec_ok' or |
---|
165 | ($check_ec_nok and $ec_entry['action'] == 'ec_nok') |
---|
166 | ) { |
---|
167 | if (isset($first[$ec_current_code])) { |
---|
168 | // $first[$ec_current_code] is set <=> code has already been met. |
---|
169 | // Checked item MUST be equal to the first item (thus all items) for |
---|
170 | // this code. |
---|
171 | if ( |
---|
172 | $first[$ec_current_code] != $ec_entry[$checked_item] or |
---|
173 | ($check_forced and $ec_entry[$checked_item] == 'true') |
---|
174 | ) { |
---|
175 | $to_correct[$ec_current_code] = true; // value not used in fact |
---|
176 | } // but using $ec_current_code as a key makes a smaller table |
---|
177 | } // if the error comes back several times |
---|
178 | else { |
---|
179 | $first[$ec_current_code] = $ec_entry[$checked_item]; |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is needed here |
---|
184 | if (isset($to_correct[$ec_entry['code']])) { |
---|
185 | $ec_entry['action'] = ($check_forced) ? |
---|
186 | $ec_entry['action'].$new_action : $new_action; |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | // Multiple action params for a single code check |
---|
192 | ec_inpect('action', 'ec_nok_action_pb', true); |
---|
193 | |
---|
194 | // Multiple user_ids for a single code check |
---|
195 | ec_inpect('user_id', 'ec_nok_userid_pb'); |
---|
196 | |
---|
197 | // Multiple "forced" params for a single code check |
---|
198 | ec_inpect('forced', '_f_pb', true, true); |
---|
199 | |
---|
200 | // User id and associated page validities checks |
---|
201 | foreach ($ec_lists['ec_table'] as &$ec_entry) { |
---|
202 | |
---|
203 | // Check if associated user_id exists |
---|
204 | if ( |
---|
205 | is_in($ec_entry['action'], 'ec_ok') and |
---|
206 | !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids']) |
---|
207 | ) { |
---|
208 | $ec_entry['action'] = 'ec_nok_userid_miss'; |
---|
209 | } |
---|
210 | |
---|
211 | // Check if associated displayed page exists |
---|
212 | $a = 0; |
---|
213 | if (!empty($ec_entry['arg1']) and |
---|
214 | is_in($ec_entry['action'], 'ec_ok')) $a++; // Only arg2 |
---|
215 | // is significant if action is ec_nok[_xxx] . |
---|
216 | if (!empty($ec_entry['arg2'])) $a+= 2; |
---|
217 | switch ($a) { |
---|
218 | // case 0 : home, nothing to check |
---|
219 | |
---|
220 | case 2: // Additional Page |
---|
221 | if ( |
---|
222 | $ec_ap_ok and ( |
---|
223 | is_in($ec_entry['action'], 'ec_ok') or |
---|
224 | $ec_entry['action'] == 'ec_nok' |
---|
225 | ) and |
---|
226 | !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages']) |
---|
227 | ) { |
---|
228 | $ec_entry['action'].= '_ap_pb'; |
---|
229 | } |
---|
230 | break; |
---|
231 | |
---|
232 | case 1: // Category |
---|
233 | case 3: // Image |
---|
234 | if (is_in($ec_entry['action'], 'ec_ok')) { |
---|
235 | if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) { |
---|
236 | if ($a == 3) { // case 3: // Image |
---|
237 | if (!mysql_fetch_row(pwg_query(' |
---|
238 | SELECT * |
---|
239 | FROM `'.IMAGE_CATEGORY_TABLE.'` |
---|
240 | WHERE `category_id` = '.$ec_entry['arg1'].' |
---|
241 | AND `image_id` = '.$ec_entry['arg2'] |
---|
242 | ))) { |
---|
243 | $ec_entry['action'].= '_img_pb'; |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | else { |
---|
248 | $ec_entry['action'].= '_cat_pb'; |
---|
249 | } |
---|
250 | } |
---|
251 | break; |
---|
252 | } |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | /* |
---|
257 | * build_ec_lists() |
---|
258 | * builds the main array variable contaning all informations for the plugin |
---|
259 | * |
---|
260 | * @param |
---|
261 | * no parameter passed, the main material on which works the function, is |
---|
262 | * the global array variable $ec_lists. |
---|
263 | * @return |
---|
264 | * (no return value) |
---|
265 | */ |
---|
266 | function build_ec_lists() { |
---|
267 | |
---|
268 | global $ec_ap_ok, $template, $ec_lists; |
---|
269 | |
---|
270 | $ec_lists = array(); |
---|
271 | |
---|
272 | // Construction of $ec_lists['add_pages'] array var |
---|
273 | $ec_lists['add_pages'] = array(); |
---|
274 | if ($ec_ap_ok) { |
---|
275 | $res = pwg_query('SELECT id,title FROM '.ADD_PAGES_TABLE); |
---|
276 | while ($r = mysql_fetch_assoc($res)) { |
---|
277 | $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id='; |
---|
278 | $a = explode($c ,$r['title']); |
---|
279 | $ec_lists['add_pages'][$r['id']] = $a[0]; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | // Construction of $ec_lists['categories'] array var |
---|
284 | $c = array(); |
---|
285 | display_select_cat_wrapper(' |
---|
286 | SELECT id,name,uppercats,global_rank |
---|
287 | FROM '.CATEGORIES_TABLE, |
---|
288 | $c, 'category_options', true); |
---|
289 | $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options']; |
---|
290 | |
---|
291 | // Construction of $ec_lists['user_ids'] array var |
---|
292 | $ec_lists['user_ids'] = array(); |
---|
293 | $q = pwg_query('SELECT id,username FROM '.USERS_TABLE. ' WHERE id > 2'); |
---|
294 | while ($r = mysql_fetch_assoc($q)) { |
---|
295 | $ec_lists['user_ids'][$r['id']] = $r['username']; |
---|
296 | } |
---|
297 | |
---|
298 | // Construction of $ec_lists['ec_table'] array var |
---|
299 | build_ec_table(); |
---|
300 | } |
---|
301 | |
---|
302 | /* |
---|
303 | * is_in($haystack, $needle) |
---|
304 | * returns true or false whether $needle is a string found in string $haystack |
---|
305 | * |
---|
306 | * @param |
---|
307 | * $haystack : the string in which to search |
---|
308 | * $needle : the string looked for |
---|
309 | * @return |
---|
310 | * true if $needle is found in $haystack ; false if not |
---|
311 | */ |
---|
312 | function is_in($haystack, $needle) { |
---|
313 | return (strpos($haystack, $needle) !== false); |
---|
314 | } |
---|
315 | |
---|
316 | /* |
---|
317 | * str_from_var($var) |
---|
318 | * returns a string easing array var informations displaying in Piwigo : |
---|
319 | * _ the string return value starts with"<p align="left">" ; |
---|
320 | * _ all "TAB" characters (chr(10)) are replaced by "<br>" ; |
---|
321 | * _ all spaces are replaced by " ". |
---|
322 | * |
---|
323 | * @param |
---|
324 | * $var : variable to display |
---|
325 | * @return |
---|
326 | * string easy to display in Piwigo |
---|
327 | */ |
---|
328 | function str_from_var($var) { |
---|
329 | return '<p align="left">'.str_replace(chr(10),'<br>',str_replace(' ',' ', print_r /* var_dump */ ($var,true))).'</p>'; |
---|
330 | } |
---|
331 | |
---|
332 | ?> |
---|