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

Last change on this file since 9400 was 9400, checked in by Eric, 13 years ago

bug 2206 fixed : The email sent to renamed users shows standard information (new username and related email address) in addition of customizable text. The email subject shows the old username.

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