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

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

LCAS_mbStringToArray does not require any more that extension mbstring is loaded

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