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

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

All files converted to UNIX return character mode

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