1 | <?php |
---|
2 | // Keeps file coded in UTF-8 without BOM : é |
---|
3 | include_once (LCAS_PATH.'include/constants.php'); |
---|
4 | load_language('plugin.lang', LCAS_PATH); |
---|
5 | |
---|
6 | |
---|
7 | /** |
---|
8 | * Splits a multi-byte string in a array of each of its characters. |
---|
9 | * Many thanks to adjwilli in |
---|
10 | * http://www.php.net/manual/fr/function.mb-split.php#80046 |
---|
11 | * |
---|
12 | * @param string s: string to split |
---|
13 | * |
---|
14 | * @return : array of each character of s |
---|
15 | */ |
---|
16 | function LCAS_mbStringToArray($s) { |
---|
17 | $l = mb_strlen($s); $a = array(); |
---|
18 | while ($l) { |
---|
19 | $a[] = mb_substr($s, 0, 1, 'UTF-8'); |
---|
20 | $s = mb_substr($s, 1, $l, 'UTF-8'); |
---|
21 | $l = mb_strlen($s); |
---|
22 | } |
---|
23 | return $a; |
---|
24 | } |
---|
25 | |
---|
26 | |
---|
27 | /** |
---|
28 | * Changes the characters of the given string as stated by values of |
---|
29 | * $conf['insensitive_case_logon'] and $conf['LCAS_replacement_set']. |
---|
30 | * |
---|
31 | * @param mix s: string |
---|
32 | * |
---|
33 | * @return : string modified as stated |
---|
34 | */ |
---|
35 | function LCAS_change_case($Username, $Opt) |
---|
36 | { |
---|
37 | global $conf; |
---|
38 | |
---|
39 | if ($Opt == '0' and !isset($conf['LCAS_replacement_set'][0])) |
---|
40 | return $Username; |
---|
41 | if ($Opt == '0') return $Username; // to be removed once rule 0 is done |
---|
42 | $Option = intval($Opt); |
---|
43 | if (!isset($Opt) or ($Option < 0) or ($Option > 3)) return $Username; |
---|
44 | |
---|
45 | include(LCAS_PATH.'include/LCAS_replacement_set.inc.php'); |
---|
46 | |
---|
47 | // Build an array of characters that must be replaced |
---|
48 | // $rep_char is the replacement character ; $char_rep_arr is an array of |
---|
49 | // characters which have to be replaced. ie : |
---|
50 | // if |
---|
51 | // $conf['LCAS_replacement_set'][$Option]['e'] ='é è' |
---|
52 | // then |
---|
53 | // $char_rep_arr['é'] = 'e' ; $char_rep_arr['è'] = 'e'. |
---|
54 | $char_rep_arr = array(); |
---|
55 | foreach ( |
---|
56 | $conf['LCAS_replacement_set'][$Option] |
---|
57 | as $rep_char => $char_rep_list |
---|
58 | ) { |
---|
59 | $t = explode(' ', $char_rep_list); |
---|
60 | foreach ($t as $c) $char_rep_arr[$c] = $rep_char; |
---|
61 | } |
---|
62 | |
---|
63 | // Replacement in each string of $Username of the characters |
---|
64 | // that needs to be replaced |
---|
65 | $t = LCAS_mbStringToArray($Username); $r =''; |
---|
66 | foreach ($t as $c) { |
---|
67 | if (array_key_exists($c, $char_rep_arr)) |
---|
68 | $r.= $char_rep_arr[$c]; |
---|
69 | else |
---|
70 | $r.= $c; |
---|
71 | } |
---|
72 | |
---|
73 | // Return of the result |
---|
74 | return $r; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | /* Function called from LCAS_admin.php to send notification email */ |
---|
89 | function LCAS_SendMail($id, $username) |
---|
90 | { |
---|
91 | global $conf; |
---|
92 | |
---|
93 | $conf_LCAS= unserialize($conf['LoginCaseAccentsSensitivity']); |
---|
94 | |
---|
95 | include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php'); |
---|
96 | |
---|
97 | /* We have to get the user's language in database */ |
---|
98 | $query =' |
---|
99 | SELECT user_id, language |
---|
100 | FROM '.USER_INFOS_TABLE.' |
---|
101 | WHERE user_id = '.$id.' |
---|
102 | ;'; |
---|
103 | $data = pwg_db_fetch_assoc(pwg_query($query)); |
---|
104 | |
---|
105 | /* Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language */ |
---|
106 | if (empty($data)) |
---|
107 | { |
---|
108 | /* And switch gallery to this language before using personalized and multilangual contents */ |
---|
109 | $language = pwg_get_session_var( 'lang_switch', $user['language'] ); |
---|
110 | switch_lang_to($language); |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | /* And switch gallery to this language before using personalized and multilangual contents */ |
---|
115 | switch_lang_to($data['language']); |
---|
116 | load_language('plugin.lang', LCAS_PATH); |
---|
117 | } |
---|
118 | |
---|
119 | $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Username_updated_for_%s', stripslashes($username))); |
---|
120 | |
---|
121 | if (isset($conf_LCAS[2]) and $conf_LCAS[2] <> '') |
---|
122 | { |
---|
123 | if (function_exists('get_user_language_desc')) |
---|
124 | { |
---|
125 | $customtxt = get_user_language_desc($conf_LCAS[2])."\n\n"; |
---|
126 | } |
---|
127 | else $customtxt = l10n($conf_LCAS[2])."\n\n"; |
---|
128 | } |
---|
129 | |
---|
130 | /* Get user's email address */ |
---|
131 | $query =' |
---|
132 | SELECT mail_address |
---|
133 | FROM '.USERS_TABLE.' |
---|
134 | WHERE id = '.$id.' |
---|
135 | ;'; |
---|
136 | $data = pwg_db_fetch_assoc(pwg_query($query)); |
---|
137 | |
---|
138 | /* Send the email with subject and contents */ |
---|
139 | pwg_mail($data['mail_address'], array( |
---|
140 | 'subject' => $subject, |
---|
141 | 'content' => ($customtxt), |
---|
142 | )); |
---|
143 | |
---|
144 | /* Switching back to default language */ |
---|
145 | switch_lang_back(); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | /* Function called from LCAS_admin.php and main.inc.php to get the plugin version and name */ |
---|
150 | function LCAS_PluginInfos($dir) |
---|
151 | { |
---|
152 | $path = $dir; |
---|
153 | |
---|
154 | $plg_data = implode( '', file($path.'main.inc.php') ); |
---|
155 | if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) ) |
---|
156 | { |
---|
157 | $plugin['name'] = trim( $val[1] ); |
---|
158 | } |
---|
159 | if (preg_match("|Version: (.*)|", $plg_data, $val)) |
---|
160 | { |
---|
161 | $plugin['version'] = trim($val[1]); |
---|
162 | } |
---|
163 | if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) ) |
---|
164 | { |
---|
165 | $plugin['uri'] = trim($val[1]); |
---|
166 | } |
---|
167 | if ($desc = load_language('description.txt', $path.'/', array('return' => true))) |
---|
168 | { |
---|
169 | $plugin['description'] = trim($desc); |
---|
170 | } |
---|
171 | elseif ( preg_match("|Description: (.*)|", $plg_data, $val) ) |
---|
172 | { |
---|
173 | $plugin['description'] = trim($val[1]); |
---|
174 | } |
---|
175 | if ( preg_match("|Author: (.*)|", $plg_data, $val) ) |
---|
176 | { |
---|
177 | $plugin['author'] = trim($val[1]); |
---|
178 | } |
---|
179 | if ( preg_match("|Author URI: (.*)|", $plg_data, $val) ) |
---|
180 | { |
---|
181 | $plugin['author uri'] = trim($val[1]); |
---|
182 | } |
---|
183 | if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid=')) |
---|
184 | { |
---|
185 | list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']); |
---|
186 | if (is_numeric($extension)) $plugin['extension'] = $extension; |
---|
187 | } |
---|
188 | // IMPORTANT SECURITY ! |
---|
189 | $plugin = array_map('htmlspecialchars', $plugin); |
---|
190 | |
---|
191 | return $plugin ; |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | // Tri les doublons |
---|
196 | function CompareTransformedUser($a, $b) |
---|
197 | { |
---|
198 | return strcmp($a['transformed'], $b['transformed']); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | // Fonctionnel mais optimisable |
---|
203 | function LCAS_GetDuplicates($source) { |
---|
204 | $users = array(); |
---|
205 | $duplicates = array(); |
---|
206 | |
---|
207 | // Liste des utilisateurs uniques |
---|
208 | foreach($source as $user) { |
---|
209 | if (isset($users[$user['transformed']])) { |
---|
210 | $users[$user['transformed']] += 1; |
---|
211 | } |
---|
212 | else { |
---|
213 | $users[$user['transformed']] = 1; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | // On récupère les doublons |
---|
218 | foreach($source as $user) { |
---|
219 | if ($users[$user['transformed']] > 1) { |
---|
220 | array_push($duplicates, $user); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | // Trier le tableau |
---|
225 | usort($duplicates, 'CompareTransformedUser'); |
---|
226 | |
---|
227 | return $duplicates; |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | /** |
---|
232 | * Retrieve duplicate users according of case and or accent sensitivity |
---|
233 | * |
---|
234 | * @param : $rule for LCAS_change_case()) |
---|
235 | * |
---|
236 | * @return : List of duplicate $username |
---|
237 | * |
---|
238 | */ |
---|
239 | function LCAS_get_user_list($rule) |
---|
240 | { |
---|
241 | global $conf, $page; |
---|
242 | |
---|
243 | $users = array(); |
---|
244 | |
---|
245 | /* search users depending expiration date */ |
---|
246 | $query = ' |
---|
247 | SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id, |
---|
248 | u.'.$conf['user_fields']['username'].' AS username, |
---|
249 | u.'.$conf['user_fields']['email'].' AS email |
---|
250 | FROM '.USERS_TABLE.' AS u |
---|
251 | ;'; |
---|
252 | |
---|
253 | $result = pwg_query($query); |
---|
254 | |
---|
255 | while ($row = pwg_db_fetch_assoc($result)) |
---|
256 | { |
---|
257 | $user = $row; |
---|
258 | $user['transformed'] = LCAS_change_case($user['username'], $rule); |
---|
259 | array_push($users, $user); |
---|
260 | } |
---|
261 | |
---|
262 | return LCAS_GetDuplicates($users); |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | // Cleaning obsolete files at plugin upgrade |
---|
267 | function LCAS_clean_obsolete_files() |
---|
268 | { |
---|
269 | if (file_exists(LCAS_PATH.'obsolete.list') |
---|
270 | and $old_files = file(LCAS_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES) |
---|
271 | and !empty($old_files)) |
---|
272 | { |
---|
273 | array_push($old_files, 'obsolete.list'); |
---|
274 | foreach($old_files as $old_file) |
---|
275 | { |
---|
276 | $path = LCAS_PATH.$old_file; |
---|
277 | if (is_file($path)) |
---|
278 | { |
---|
279 | @unlink($path); |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | /** |
---|
287 | * Function called from main.inc.php - For test on username case sensitivity |
---|
288 | * Have to be deleted before first release build |
---|
289 | * |
---|
290 | * @param : $username typed in by user for identification |
---|
291 | * |
---|
292 | * @return : $username found in database |
---|
293 | * |
---|
294 | */ |
---|
295 | function LCAS_SearchCaseUsername($username, $rule) |
---|
296 | { |
---|
297 | global $conf; |
---|
298 | |
---|
299 | $username_c = LCAS_change_case($username, $rule); |
---|
300 | |
---|
301 | if (isset($username)) |
---|
302 | { |
---|
303 | $LCAS_users = array(); |
---|
304 | |
---|
305 | $q = pwg_query(" |
---|
306 | SELECT ".$conf['user_fields']['username']." AS username |
---|
307 | FROM `".USERS_TABLE."`; |
---|
308 | "); |
---|
309 | while ($r = pwg_db_fetch_assoc($q)) |
---|
310 | $LCAS_users[$r['username']] = |
---|
311 | LCAS_change_case($r['username'], $rule); |
---|
312 | // $LCAS_users is now an associative table where the key is the account |
---|
313 | // as registered in the DB, and the value is this same account, |
---|
314 | // transformed as stated by $rule. |
---|
315 | |
---|
316 | $users_found = array_keys($LCAS_users, $username_c); |
---|
317 | // $users_found is now a table of which the values are all the accounts |
---|
318 | // which can be written as stated by $rule the same way as $username can |
---|
319 | if (count($users_found) != 1) // If ambiguous, don't allow transformed |
---|
320 | return $username; // writing but normal writing will work |
---|
321 | else |
---|
322 | return $users_found[0]; |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | /* |
---|
328 | * str_from_var3($var) |
---|
329 | * returns a string easing array var informations displaying in Piwigo : |
---|
330 | * _ the string return value starts with"<p style = "text-align:left;">" ; |
---|
331 | * _ all "TAB" characters (chr(10)) are replaced by "<br>" ; |
---|
332 | * _ all spaces are replaced by " ". |
---|
333 | * |
---|
334 | * @param |
---|
335 | * $var : variable to display |
---|
336 | * @return |
---|
337 | * string easy to display in Piwigo |
---|
338 | */ |
---|
339 | function str_from_var3($var) { |
---|
340 | return |
---|
341 | '<p style = "text-align:left;">'. |
---|
342 | str_replace( |
---|
343 | chr(10),'<br>', |
---|
344 | str_replace(' ',' ', print_r /* var_dump */ ($var,true)) |
---|
345 | ). |
---|
346 | '</p>'; |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | // DebugLog function |
---|
351 | function DebugLog($var1, $var2, $var3, $var4, $var5) |
---|
352 | { |
---|
353 | $fo=fopen (LCAS_PATH.'admin/debuglog.txt','a') ; |
---|
354 | fwrite($fo,"======================\n") ; |
---|
355 | fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n"); |
---|
356 | fwrite($fo, "\n" . $var1 . "\r\n") ; |
---|
357 | fwrite($fo, "\n" . $var2 . "\r\n") ; |
---|
358 | fwrite($fo, "\n" . $var3 . "\r\n") ; |
---|
359 | fwrite($fo, "\n" . $var4 . "\r\n") ; |
---|
360 | fwrite($fo, "\n" . $var5 . "\r\n") ; |
---|
361 | fclose($fo) ; |
---|
362 | } |
---|
363 | ?> |
---|