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

Last change on this file since 10549 was 10549, checked in by LucMorizur, 13 years ago

Code cleaning

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