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

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

Small corrections on previous commit

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