source: extensions/LCAS/trunk/include/functions.inc.php @ 9389

Last change on this file since 9389 was 9389, checked in by Whiler, 13 years ago

Check if username already exists (depending on the rule which is set)

File size: 9.0 KB
RevLine 
[9229]1<?php
2          // Keeps file coded in UTF-8 without BOM : é
3include_once (LCAS_PATH.'include/constants.php');
4load_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*/
16function 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*/
35function 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 */
89function 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 ='
99SELECT user_id, language
100FROM '.USER_INFOS_TABLE.'
101WHERE 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
[9326]121  if (isset($conf_LCAS[2]) and $conf_LCAS[2] <> '')
[9229]122  {
123    if (function_exists('get_user_language_desc'))
124    {
[9326]125      $customtxt = get_user_language_desc($conf_LCAS[2])."\n\n";
[9229]126    }
[9326]127    else $customtxt = l10n($conf_LCAS[2])."\n\n"; 
[9229]128  }
129
130/* Send the email with subject and contents */
131  pwg_mail($email, array(
132    'subject' => $subject,
133    'content' => ($customtxt),
134  ));
135
136/* Switching back to default language */
137switch_lang_back();
138}
139
140
141/* Function called from LCAS_admin.php and main.inc.php to get the plugin version and name */
142function LCAS_PluginInfos($dir)
143{
144  $path = $dir;
145
146  $plg_data = implode( '', file($path.'main.inc.php') );
147  if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
148  {
149    $plugin['name'] = trim( $val[1] );
150  }
151  if (preg_match("|Version: (.*)|", $plg_data, $val))
152  {
153    $plugin['version'] = trim($val[1]);
154  }
155  if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
156  {
157    $plugin['uri'] = trim($val[1]);
158  }
159  if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
160  {
161    $plugin['description'] = trim($desc);
162  }
163  elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
164  {
165    $plugin['description'] = trim($val[1]);
166  }
167  if ( preg_match("|Author: (.*)|", $plg_data, $val) )
168  {
169    $plugin['author'] = trim($val[1]);
170  }
171  if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
172  {
173    $plugin['author uri'] = trim($val[1]);
174  }
175  if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
176  {
177    list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
178    if (is_numeric($extension)) $plugin['extension'] = $extension;
179  }
180// IMPORTANT SECURITY !
181  $plugin = array_map('htmlspecialchars', $plugin);
182
183  return $plugin ;
184}
185
186
187// Tri les doublons
188function CompareTransformedUser($a, $b)
189{
190        return strcmp($a['transformed'], $b['transformed']);
191}
192
193
194// Fonctionnel mais optimisable
195function LCAS_GetDuplicates($source) {
196        $users      = array();
197        $duplicates = array();
198       
199        // Liste des utilisateurs uniques
200        foreach($source as $user) {
201                if (isset($users[$user['transformed']])) {
202                        $users[$user['transformed']] += 1;
203                }
204                else {
205                        $users[$user['transformed']] = 1;
206                }
207        }
208       
209        // On récupère les doublons
210        foreach($source as $user) {
211                if ($users[$user['transformed']] > 1) {
212                        array_push($duplicates, $user);
213                }
214        }
215       
216        // Trier le tableau
217        usort($duplicates, 'CompareTransformedUser');
218       
219        return $duplicates;
220}
221
222
223/**
[9389]224 * Retrieve duplicate users according of case and or accent sensitivity
[9229]225 *
226 * @param : $rule for LCAS_change_case())
227 *
228 * @return : List of duplicate $username
229 *
230 */
231function LCAS_get_user_list($rule)
232{
233        global $conf, $page;
234 
235  $users = array();
236
237        /* search users depending expiration date */
238  $query = '
239SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
240                u.'.$conf['user_fields']['username'].' AS username,
241                u.'.$conf['user_fields']['email'].' AS email
242FROM '.USERS_TABLE.' AS u
243;';
244
245        $result = pwg_query($query);
246     
247  while ($row = pwg_db_fetch_assoc($result))
248  {
249        $user = $row;
[9389]250        $user['transformed'] = LCAS_change_case($user['username'], $rule); 
[9229]251    array_push($users, $user);
252        }
253
254        return LCAS_GetDuplicates($users);
255}
256
257
258// Cleaning obsolete files at plugin upgrade
259function LCAS_clean_obsolete_files()
260{
261  if (file_exists(LCAS_PATH.'obsolete.list')
262    and $old_files = file(LCAS_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
263    and !empty($old_files))
264  {
265    array_push($old_files, 'obsolete.list');
266    foreach($old_files as $old_file)
267    {
268      $path = LCAS_PATH.$old_file;
269      if (is_file($path))
270      {
271        @unlink($path);
272      }
273    }
274  }
275}
276
277
278/**
279 * Function called from main.inc.php - For test on username case sensitivity
280 * Have to be deleted before first release build
281 *
282 * @param : $username typed in by user for identification
283 *
284 * @return : $username found in database
285 *
286 */
287function LCAS_SearchCaseUsername($username, $rule)
288{
289  global $conf;
290
291  $username_c = LCAS_change_case($username, $rule);
292
293  if (isset($username))
294  {   
295    $LCAS_users = array();
296   
297    $q = pwg_query("
298      SELECT ".$conf['user_fields']['username']." AS username
299      FROM `".USERS_TABLE."`;
300    ");
301    while ($r = pwg_db_fetch_assoc($q))
302     $LCAS_users[$r['username']] =
303      LCAS_change_case($r['username'], $rule);
304     // $LCAS_users is now an associative table where the key is the account
305     // as registered in the DB, and the value is this same account,
306     // transformed as stated by $rule.
307
308    $users_found = array_keys($LCAS_users, $username_c);
309    // $users_found is now a table of which the values are all the accounts
310    // which can be written as stated by $rule the same way as $username can
311    if (count($users_found) != 1) // If ambiguous, don't allow transformed
312     return $username;            // writing but normal writing will work
313    else
314     return $users_found[0];
315  }
316}
317
318
319/*
320 * str_from_var3($var)
321 * returns a string easing array var informations displaying in Piwigo :
322 *   _ the string return value starts with"<p style = "text-align:left;">" ;
323 *   _ all "TAB" characters (chr(10)) are replaced by "<br>" ;
324 *   _ all spaces are replaced by "&nbsp;".
325 *
326 * @param
327 *   $var : variable to display
328 * @return
329 *   string easy to display in Piwigo
330 */
331function str_from_var3($var) {
332  return
333   '<p style = "text-align:left;">'.
334   str_replace(
335    chr(10),'<br>',
336    str_replace(' ','&nbsp;', print_r /* var_dump */ ($var,true))
337   ).
338   '</p>';
339}
340
341
342// DebugLog function
343function DebugLog($var1, $var2, $var3, $var4, $var5)
344{
345   $fo=fopen (LCAS_PATH.'admin/debuglog.txt','a') ;
346   fwrite($fo,"======================\n") ;
347   fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n");
348   fwrite($fo, "\n" . $var1 . "\r\n") ;
349   fwrite($fo, "\n" . $var2 . "\r\n") ;
350   fwrite($fo, "\n" . $var3 . "\r\n") ;
351   fwrite($fo, "\n" . $var4 . "\r\n") ;
352   fwrite($fo, "\n" . $var5 . "\r\n") ;
353   fclose($fo) ;
354}
[8141]355?>
Note: See TracBrowser for help on using the repository browser.