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

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

Begin feature:2100 ; and a few code cleaning

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