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 | |
---|
49 | if (isset($conf['auto_log'])) { |
---|
50 | // For tests purpose : table not in MySQL, but in $conf['xxx'] vars in |
---|
51 | // config_local.inc.php |
---|
52 | |
---|
53 | foreach ($conf['auto_log'] as $ec_current_code => $ec_entry) { |
---|
54 | $ec_current_entry = array_push($ec_lists['ec_table'], array( |
---|
55 | 'code' => $ec_current_code, |
---|
56 | 'user_id' => get_userid($ec_entry[$ec_current_code]), |
---|
57 | 'action' => 'ec_ok', |
---|
58 | 'arg1' => NULL, |
---|
59 | 'arg2' => NULL, |
---|
60 | 'forced' => 'false', |
---|
61 | )) - 1; |
---|
62 | $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry; |
---|
63 | if (isset($conf['prior_page'])) { |
---|
64 | if (array_key_exists($ec_current_code, $conf['prior_page'])) { |
---|
65 | $ec_lists['ec_table'][$ec_current_entry]['arg2'] = |
---|
66 | $conf['prior_page'][$ec_current_code]; |
---|
67 | $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true'; |
---|
68 | } |
---|
69 | } |
---|
70 | if (isset($conf['outdated_page'])) { |
---|
71 | if (array_key_exists($ec_current_code, $conf['outdated_page'])) { |
---|
72 | $ec_lists['ec_table'][$ec_current_entry]['action'] = 'ec_nok'; |
---|
73 | $ec_lists['ec_table'][$ec_current_entry]['arg2'] = |
---|
74 | $conf['outdated_page'][$ec_current_code]; |
---|
75 | $ec_lists['ec_table'][$ec_current_entry]['forced'] = 'true'; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | // Inspection of $conf['outdated_page'] |
---|
81 | if (isset($conf['outdated_page'])) { |
---|
82 | foreach ($conf['outdated_page'] as $ec_current_code => $ec_current_AP) { |
---|
83 | if (!array_key_exists($ec_current_code, $conf['auto_log'])) { |
---|
84 | $ec_current_entry = array_push($ec_lists['ec_table'], array( |
---|
85 | 'code' => $ec_current_code, |
---|
86 | 'user_id' => '', |
---|
87 | 'action' => 'ec_nok', |
---|
88 | 'arg1' => NULL, |
---|
89 | 'arg2' => $ec_current_AP, |
---|
90 | 'forced' => 'true', |
---|
91 | )) - 1; |
---|
92 | $ec_lists['ec_table'][$ec_current_entry]['id'] = $ec_current_entry; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|
98 | else { // in "normal" use ($conf['auto_log'] NOT set), $ec_lists['ec_table'] |
---|
99 | // is built thanks to below code only. A little bit more simple... |
---|
100 | $q = pwg_query(' |
---|
101 | SELECT * FROM `'.EVNTCATS_TABLE.'` |
---|
102 | WHERE `code` IS NOT NULL |
---|
103 | ORDER BY `id` |
---|
104 | '); |
---|
105 | while ($r = mysql_fetch_assoc($q)) { |
---|
106 | $ec_lists['ec_table'][intval($r['id'])] = $r; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | // Construction of behaviour |
---|
111 | |
---|
112 | /* |
---|
113 | * ec_inpect() |
---|
114 | * goes through ec_lists['ec_table'] to check errors on (multiple) entries |
---|
115 | * using the same code. Cannot be used elsewhere than here in |
---|
116 | * build_ec_table() . |
---|
117 | * |
---|
118 | * @param |
---|
119 | * |
---|
120 | * @return |
---|
121 | * (no return value) |
---|
122 | */ |
---|
123 | function ec_inpect($checked_item, $new_action, |
---|
124 | $check_ec_nok = false, $check_forced = false) { |
---|
125 | global $ec_lists; |
---|
126 | $first = array(); |
---|
127 | $to_correct = array(); |
---|
128 | |
---|
129 | // $to_correct is needed because following code would not work anywhere : |
---|
130 | /* |
---|
131 | foreach ($table as $value) { |
---|
132 | if ($value == $value_to_check and $value == $problem) { |
---|
133 | foreach ($table as &$value2) { // key index of $table can be reset |
---|
134 | if ($value2 == $value_to_check) { |
---|
135 | $value2 = $better_value; |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | // It works with WinAmp Server, but not on Apache server of Free (french |
---|
141 | // Internet provider). |
---|
142 | */ |
---|
143 | |
---|
144 | foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is not really needed |
---|
145 | $ec_current_code = $ec_entry['code']; |
---|
146 | if ( |
---|
147 | $ec_entry['action'] == 'ec_ok' or |
---|
148 | ($check_ec_nok and $ec_entry['action'] == 'ec_nok') |
---|
149 | ) { |
---|
150 | if (isset($first[$ec_current_code])) { |
---|
151 | // $first[$ec_current_code] is set <=> code has already been met. |
---|
152 | // Checked item MUST be equal to the first item (thus all items) for |
---|
153 | // this code. |
---|
154 | if ( |
---|
155 | $first[$ec_current_code] != $ec_entry[$checked_item] or |
---|
156 | ($check_forced and $ec_entry[$checked_item] == 'true') |
---|
157 | ) { |
---|
158 | $to_correct[$ec_current_code] = true; // value not used in fact |
---|
159 | } // but using $ec_current_code as a key makes a smaller table |
---|
160 | } // if the error comes back several times |
---|
161 | else { |
---|
162 | $first[$ec_current_code] = $ec_entry[$checked_item]; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | foreach ($ec_lists['ec_table'] as &$ec_entry) { // & is needed here |
---|
167 | if (isset($to_correct[$ec_entry['code']])) { |
---|
168 | $ec_entry['action'] = ($check_forced) ? |
---|
169 | $ec_entry['action'].$new_action : $new_action; |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | // Multiple action params for a single code check |
---|
175 | ec_inpect('action', 'ec_nok_action_pb', true); |
---|
176 | |
---|
177 | // Multiple user_ids for a single code check |
---|
178 | ec_inpect('user_id', 'ec_nok_userid_pb'); |
---|
179 | |
---|
180 | // Multiple "forced" params for a single code check |
---|
181 | ec_inpect('forced', '_f_pb', true, true); |
---|
182 | |
---|
183 | // User id and associated page validities checks |
---|
184 | foreach ($ec_lists['ec_table'] as &$ec_entry) { |
---|
185 | |
---|
186 | // Check if associated user_id exists |
---|
187 | if ( |
---|
188 | is_in($ec_entry['action'], 'ec_ok') and |
---|
189 | !array_key_exists($ec_entry['user_id'], $ec_lists['user_ids']) |
---|
190 | ) { |
---|
191 | $ec_entry['action'] = 'ec_nok_userid_miss'; |
---|
192 | } |
---|
193 | |
---|
194 | // Check if associated displayed page exists |
---|
195 | $a = 0; |
---|
196 | if (!empty($ec_entry['arg1']) and |
---|
197 | is_in($ec_entry['action'], 'ec_ok')) $a++; // Only arg2 |
---|
198 | // is significant if action is ec_nok[_xxx] . |
---|
199 | if (!empty($ec_entry['arg2'])) $a+= 2; |
---|
200 | switch ($a) { |
---|
201 | // case 0 : home, nothing to check |
---|
202 | |
---|
203 | case 2: // Additional Page |
---|
204 | if ( |
---|
205 | $ec_ap_ok and ( |
---|
206 | is_in($ec_entry['action'], 'ec_ok') or |
---|
207 | $ec_entry['action'] == 'ec_nok' |
---|
208 | ) and |
---|
209 | !array_key_exists($ec_entry['arg2'], $ec_lists['add_pages']) |
---|
210 | ) { |
---|
211 | $ec_entry['action'].= '_ap_pb'; |
---|
212 | } |
---|
213 | break; |
---|
214 | |
---|
215 | case 1: // Category |
---|
216 | case 3: // Image |
---|
217 | if (is_in($ec_entry['action'], 'ec_ok')) { |
---|
218 | if (array_key_exists($ec_entry['arg1'], $ec_lists['categories'])) { |
---|
219 | if ($a == 3) { // case 3: // Image |
---|
220 | if (!mysql_fetch_row(pwg_query(' |
---|
221 | SELECT * |
---|
222 | FROM `'.IMAGE_CATEGORY_TABLE.'` |
---|
223 | WHERE `category_id` = '.$ec_entry['arg1'].' |
---|
224 | AND `image_id` = '.$ec_entry['arg2'] |
---|
225 | ))) { |
---|
226 | $ec_entry['action'].= '_img_pb'; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | else { |
---|
231 | $ec_entry['action'].= '_cat_pb'; |
---|
232 | } |
---|
233 | } |
---|
234 | break; |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | /* |
---|
240 | * build_ec_lists() |
---|
241 | * builds the main array variable contaning all informations for the plugin |
---|
242 | * |
---|
243 | * @param |
---|
244 | * no parameter passed, the main material on which works the function, is |
---|
245 | * the global array variable $ec_lists. |
---|
246 | * @return |
---|
247 | * (no return value) |
---|
248 | */ |
---|
249 | function build_ec_lists() { |
---|
250 | |
---|
251 | global $ec_ap_ok, $template, $ec_lists; |
---|
252 | |
---|
253 | $ec_lists = array(); |
---|
254 | |
---|
255 | // Construction of $ec_lists['add_pages'] array var |
---|
256 | $ec_lists['add_pages'] = array(); |
---|
257 | if ($ec_ap_ok) { |
---|
258 | $res = pwg_query('SELECT id,title FROM '.ADD_PAGES_TABLE); |
---|
259 | while ($r = mysql_fetch_assoc($res)) { |
---|
260 | $c = (is_in($r['title'], '/user_id=')) ? '/user_id=' : '/group_id='; |
---|
261 | $a = explode($c ,$r['title']); |
---|
262 | $ec_lists['add_pages'][$r['id']] = $a[0]; |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | // Construction of $ec_lists['categories'] array var |
---|
267 | $c = array(); |
---|
268 | display_select_cat_wrapper(' |
---|
269 | SELECT id,name,uppercats,global_rank |
---|
270 | FROM '.CATEGORIES_TABLE, |
---|
271 | $c, 'category_options', true); |
---|
272 | $ec_lists['categories'] = $template->smarty->_tpl_vars['category_options']; |
---|
273 | |
---|
274 | // Construction of $ec_lists['user_ids'] array var |
---|
275 | $ec_lists['user_ids'] = array(); |
---|
276 | $q = pwg_query('SELECT id,username FROM '.USERS_TABLE. ' WHERE id > 2'); |
---|
277 | while ($r = mysql_fetch_assoc($q)) { |
---|
278 | $ec_lists['user_ids'][$r['id']] = $r['username']; |
---|
279 | } |
---|
280 | |
---|
281 | // Construction of $ec_lists['ec_table'] array var |
---|
282 | build_ec_table(); |
---|
283 | } |
---|
284 | |
---|
285 | /* |
---|
286 | * is_in($haystack, $needle) |
---|
287 | * returns true or false whether $needle is a string found in string $haystack |
---|
288 | * |
---|
289 | * @param |
---|
290 | * $haystack : the string in which to search |
---|
291 | * $needle : the string looked for |
---|
292 | * @return |
---|
293 | * true if $needle is found in $haystack ; false if not |
---|
294 | */ |
---|
295 | function is_in($haystack, $needle) { |
---|
296 | return (strpos($haystack, $needle) !== false); |
---|
297 | } |
---|
298 | |
---|
299 | /* |
---|
300 | * str_from_var($var) |
---|
301 | * returns a string easing array var informations displaying in Piwigo : |
---|
302 | * _ the string return value starts with"<p align="left">" ; |
---|
303 | * _ all "TAB" characters (chr(10)) are replaced by "<br>" ; |
---|
304 | * _ all spaces are replaced by " ". |
---|
305 | * |
---|
306 | * @param |
---|
307 | * $var : variable to display |
---|
308 | * @return |
---|
309 | * string easy to display in Piwigo |
---|
310 | */ |
---|
311 | function str_from_var($var) { |
---|
312 | return '<p align="left">'.str_replace(chr(10),'<br>',str_replace(' ',' ', print_r /* var_dump */ ($var,true))).'</p>'; |
---|
313 | } |
---|
314 | |
---|
315 | ?> |
---|