source: extensions/NBC_UserAdvManager/branches/2.15/include/functions.inc.php @ 6776

Last change on this file since 6776 was 6776, checked in by Eric, 14 years ago

Merge from Trunk to Branch 2.5

  • Property svn:eol-style set to LF
File size: 32.5 KB
Line 
1<?php
2include_once (UAM_PATH.'include/constants.php');
3load_language('plugin.lang', UAM_PATH);
4
5/* Function called from main.inc.php to send validation email */
6function SendMail2User($typemail, $id, $username, $password, $email, $confirm)
7{
8  /* Only available for next Piwigo release (bug in switch_lang function) */
9  global $conf;
10
11  $conf_UAM = unserialize($conf['UserAdvManager']);
12 
13        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
14 
15        $infos1_perso = "";
16  $infos2_perso = "";
17
18/* We have to get the user's language in database */
19  $query ='
20SELECT user_id, language
21FROM '.USER_INFOS_TABLE.'
22WHERE user_id = '.$id.'
23;';
24  $data = pwg_db_fetch_assoc(pwg_query($query));
25
26/* Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language */
27  if (empty($data))
28  {
29/* And switch gallery to this language before using personalized and multilangual contents */
30    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
31    switch_lang_to($language);
32  }
33  else
34  {
35/* And switch gallery to this language before using personalized and multilangual contents */
36    $language = $data['language']; /* Usefull for debugging */
37    switch_lang_to($data['language']);
38    load_language('plugin.lang', UAM_PATH);
39  }
40
41  switch($typemail)
42  {
43    case 1:
44      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Add of %s', stripslashes($username)));
45      $password = $password <> '' ? $password : l10n('UAM_empty_pwd');
46     
47      if (isset($conf_UAM[9]) and $conf_UAM[9] <> '')
48      {
49        if (function_exists('get_user_language_desc'))
50        {
51          $infos1_perso = get_user_language_desc($conf_UAM[9])."\n\n";
52        }
53        else $infos1_perso = l10n($conf_UAM[9])."\n\n"; 
54      }
55     
56      break;
57     
58    case 2:
59      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Update of %s', stripslashes($username)));
60      $password = $password <> '' ? $password : l10n('UAM_empty_pwd');
61
62      break;
63       
64    case 3:
65      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Update of %s', stripslashes($username)));
66      $password = $password <> '' ? $password : l10n('UAM_no_update_pwd');
67
68      break;
69  }
70
71  if (isset($conf_UAM[0]) and $conf_UAM[0] == 'true')
72  {
73    $infos1 = array(
74      get_l10n_args('infos_mail %s', stripslashes($username)),
75      get_l10n_args('User: %s', stripslashes($username)),
76      get_l10n_args('Password: %s', $password),
77      get_l10n_args('Email: %s', $email),
78      get_l10n_args('', ''),
79    );
80  }
81
82
83  if ( isset($conf_UAM[1]) and $conf_UAM[1] == 'true' and $confirm)
84  {
85    $infos2 = array
86    (
87      get_l10n_args('Link: %s', AddConfirmMail($id, $email)),
88      get_l10n_args('', ''),
89    );
90
91    if (isset($conf_UAM[10]) and $conf_UAM[10] <> '')
92    {
93      if (function_exists('get_user_language_desc'))
94      {
95        $infos2_perso = get_user_language_desc($conf_UAM[10])."\n\n";
96      }
97      else $infos2_perso = l10n($conf_UAM[10])."\n\n";
98    }
99  }
100
101/* ******************************************************** */
102/* **** Pending code since to find how to make it work **** */
103/* ******************************************************** */
104// Testing if FCK Editor is used. Then decoding htmlchars to avoid problems with pwg_mail()
105/*$areas = array();
106array_push( $areas,'UAM_MailInfo_Text','UAM_ConfirmMail_Text');
107
108if (function_exists('set_fckeditor_instance'))
109{
110  $fcke_config = unserialize($conf['FCKEditor']);
111  foreach($areas as $area)
112  {
113    if (isset($fcke_config['UAM_MailInfo_Text']) and $fcke_config['UAM_MailInfo_Text'] = true)
114    {
115      $infos1_perso = html_entity_decode($infos1_perso,ENT_QUOTES,UTF-8);
116    }
117   
118    if (isset($fcke_config['UAM_ConfirmMail_Text']) and $fcke_config['UAM_ConfirmMail_Text'] = true)
119    {
120      $infos2_perso = html_entity_decode($infos2_perso,ENT_QUOTES,UTF-8);
121    }
122  }
123}*/
124
125
126/* Sending the email with subject and contents */
127  pwg_mail($email, array(
128    'subject' => $subject,
129    'content' => (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
130  ));
131
132/* ********************** */
133/* Email sending debugger */
134/* This is only to trace  */
135/* the send of emails for */
136/* debugging              */
137/* ********************** */
138//$content = (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url();   
139//MailLog($email,$subject,$content,$language);
140/* ********************** */
141
142/* Switching back to default language */
143switch_lang_back();
144}
145
146
147/* Function called from UAM_admin.php to resend validation email with or without new validation key */
148function ResendMail2User($typemail, $user_id, $username, $email, $confirm)
149{
150  /* Only available for next Piwigo release (bug in switch_lang function) */
151  global $conf;
152
153  $conf_UAM = unserialize($conf['UserAdvManager']);
154
155  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
156 
157        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
158 
159        $infos1_perso = "";
160  $infos2_perso = "";
161 
162/* We have to get the user's language in database */
163  $query ='
164SELECT user_id, language
165FROM '.USER_INFOS_TABLE.'
166WHERE user_id = '.$user_id.'
167;';
168  $data = pwg_db_fetch_assoc(pwg_query($query));
169  $language = $data['language'];
170 
171/* And switch gallery to this language before using personalized and multilangual contents */
172  switch_lang_to($data['language']);
173   
174  load_language('plugin.lang', UAM_PATH);
175
176  switch($typemail)
177  {
178    case 1:
179      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Reminder_with_key_of_%s', $username));
180     
181      if (isset($conf_UAM_ConfirmMail[2]) and $conf_UAM_ConfirmMail[2] <> '' and isset($conf_UAM_ConfirmMail[3]) and $conf_UAM_ConfirmMail[3] == 'true' and $confirm)
182      {
183        if (function_exists('get_user_language_desc'))
184        {
185          $infos1 = get_user_language_desc($conf_UAM_ConfirmMail[2])."\n\n";
186        }
187                                else $infos1 = l10n($conf_UAM_ConfirmMail[2])."\n\n";
188
189        $infos2 = array
190        (
191          get_l10n_args('Link: %s', ResetConfirmMail($user_id)),
192          get_l10n_args('', ''),
193        );       
194                        }
195
196/* Set reminder true */     
197      $query = "
198UPDATE ".USER_CONFIRM_MAIL_TABLE."
199SET reminder = 'true'
200WHERE user_id = '".$user_id."'
201;";
202      pwg_query($query);
203     
204                break;
205     
206    case 2:
207      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Reminder_without_key_of_%s',$username));
208     
209      if (isset($conf_UAM_ConfirmMail[2]) and $conf_UAM_ConfirmMail[2] <> '' and isset($conf_UAM_ConfirmMail[3]) and $conf_UAM_ConfirmMail[3] == 'true' and !$confirm)
210      {
211        if (function_exists('get_user_language_desc'))
212        {
213          $infos1 = get_user_language_desc($conf_UAM_ConfirmMail[2])."\n\n";
214        }
215        else $infos1 = l10n($conf_UAM_ConfirmMail[2])."\n\n";
216      }
217     
218/* Set reminder true */     
219      $query = "
220UPDATE ".USER_CONFIRM_MAIL_TABLE."
221SET reminder = 'true'
222WHERE user_id = '".$user_id."'
223;";
224      pwg_query($query);
225     
226    break;
227        }
228 
229  pwg_mail($email, array(
230    'subject' => $subject,
231    'content' => ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
232  ));
233
234/* ********************** */
235/* Email sending debugger */
236/* This is only to trace  */
237/* the send of emails for */
238/* debugging              */
239/* ********************** */
240//$content = ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url();
241//MailLog($email,$subject,$content,$language);
242/* ********************** */
243
244/* Switching back to default language */
245switch_lang_back();
246}
247
248
249/* Function called from UAM_admin.php to send a reminder mail for ghost users */
250function ghostreminder($user_id, $username, $email)
251{
252  /* Only available for next Piwigo release (bug in switch_lang function) */
253  global $conf;
254
255  $conf_UAM = unserialize($conf['UserAdvManager']);
256 
257        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
258 
259        $infos1_perso = "";
260
261/* We have to get the user's language in database */
262  $query ='
263SELECT user_id, language
264FROM '.USER_INFOS_TABLE.'
265WHERE user_id = '.$user_id.'
266;';
267  $data = pwg_db_fetch_assoc(pwg_query($query));
268  $language = $data['language'];
269
270/* And switch gallery to this language before using personalized and multilangual contents */
271  switch_lang_to($data['language']);
272   
273  load_language('plugin.lang', UAM_PATH);
274 
275  $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Ghost_reminder_of_%s', $username));     
276
277  if (isset($conf_UAM[18]) and $conf_UAM[18] <> '' and isset($conf_UAM[16]) and $conf_UAM[16] == 'true')
278  {
279    if (function_exists('get_user_language_desc'))
280    {
281      $infos1 = get_user_language_desc($conf_UAM[18])."\n\n";
282    }
283    else
284    {
285      $infos1 = l10n($conf_UAM[18])."\n\n";
286    }
287
288    resetlastvisit($user_id);
289  }
290
291  pwg_mail($email, array(
292    'subject' => $subject,
293    'content' => $infos1.get_absolute_root_url(),
294  ));
295
296/* ********************** */
297/* Email sending debugger */
298/* This is only to trace  */
299/* the send of emails for */
300/* debugging              */
301/* ********************** */
302//$content = get_user_language_desc($conf_UAM[19])."\n\n"; 
303//MailLog($email,$subject,$content,$language);
304/* ********************** */
305
306/* Switching back to default language */
307switch_lang_back();
308}
309
310
311/* Function called from functions AddConfirmMail and ResetConfirmMail for validation key generation */
312function FindAvailableConfirmMailID()
313{
314  while (true)
315  {
316    $id = generate_key(16);
317    $query = "
318SELECT COUNT(*)
319  FROM ".USER_CONFIRM_MAIL_TABLE."
320WHERE id = '".$id."'
321;";
322    list($count) = pwg_db_fetch_row(pwg_query($query));
323
324    if ($count == 0)
325      return $id;
326  }
327}
328
329
330/* Function called from functions SendMail2User to process unvalidated users and generate validation key link */
331function AddConfirmMail($user_id, $email)
332{
333  global $conf;
334
335  $conf_UAM = unserialize($conf['UserAdvManager']);
336  $Confirm_Mail_ID = FindAvailableConfirmMailID();
337
338  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
339 
340  if (isset($Confirm_Mail_ID))
341  {
342    $query = "
343SELECT status
344  FROM ".USER_INFOS_TABLE."
345WHERE user_id = '".$user_id."'
346;";
347    list($status) = pwg_db_fetch_row(pwg_query($query));
348   
349    $query = "
350INSERT INTO ".USER_CONFIRM_MAIL_TABLE."
351  (id, user_id, mail_address, status, date_check)
352VALUES
353  ('".$Confirm_Mail_ID."', '".$user_id."', '".$email."', '".$status."', null)
354;";
355    pwg_query($query);
356
357    $query = "
358DELETE FROM ".USER_GROUP_TABLE."
359WHERE user_id = '".$user_id."'
360  AND (
361    group_id = '".$conf_UAM[2]."'
362  OR
363    group_id = '".$conf_UAM[3]."'
364  )
365;";
366    pwg_query($query);
367
368    if (!is_admin() and $conf_UAM[8] <> -1)
369    {
370      $query = "
371UPDATE ".USER_INFOS_TABLE."
372SET status = '".$conf_UAM[8]."'
373WHERE user_id = '".$user_id."'
374;";
375      pwg_query($query);
376    }
377
378    if ( $conf_UAM[2] <> -1 )
379    {
380      $query = "
381INSERT INTO ".USER_GROUP_TABLE."
382  (user_id, group_id)
383VALUES
384  ('".$user_id."', '".$conf_UAM[2]."')
385;";
386      pwg_query($query);
387    }
388   
389    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID;
390  }
391}
392
393
394/* Function called from main.inc.php to set group to new users if manual validation is set */
395function setgroup($user_id)
396{
397  global $conf;
398 
399  $conf_UAM = unserialize($conf['UserAdvManager']);
400
401  $query = "
402DELETE FROM ".USER_GROUP_TABLE."
403WHERE user_id = '".$user_id."'
404  AND (
405    group_id = '".$conf_UAM[2]."'
406  OR
407    group_id = '".$conf_UAM[3]."'
408  )
409;";
410  pwg_query($query);
411
412  if (!is_admin() and $conf_UAM[8] <> -1)
413  {
414    $query = "
415UPDATE ".USER_INFOS_TABLE."
416SET status = '".$conf_UAM[8]."'
417WHERE user_id = '".$user_id."'
418;";
419    pwg_query($query);
420  }
421
422  if ( $conf_UAM[2] <> -1 )
423  {
424    $query = "
425INSERT INTO ".USER_GROUP_TABLE."
426  (user_id, group_id)
427VALUES
428  ('".$user_id."', '".$conf_UAM[2]."')
429;";
430    pwg_query($query);
431  }
432}
433
434
435/* Function called from UAM_admin.php to reset validation key */
436function ResetConfirmMail($user_id)
437{
438  global $conf;
439 
440  $Confirm_Mail_ID = FindAvailableConfirmMailID();
441
442  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
443 
444  if (isset($Confirm_Mail_ID))
445  { 
446    $query = "
447UPDATE ".USER_CONFIRM_MAIL_TABLE."
448SET id = '".$Confirm_Mail_ID."'
449WHERE user_id = '".$user_id."'
450;";
451    pwg_query($query);
452
453                $query = "
454UPDATE ".USER_INFOS_TABLE."
455SET registration_date = '".$dbnow."'
456WHERE user_id = '".$user_id."'
457;";
458                pwg_query($query);
459   
460    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID;
461  }
462}
463
464
465/* Function called from function_UserAdvManager.inc.php to reset last visit date after sending a reminder */
466function resetlastvisit($user_id)
467{
468  global $conf;
469
470  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
471
472  $query = "
473UPDATE ".USER_LASTVISIT_TABLE."
474SET lastvisit = '".$dbnow."', reminder = 'true'
475WHERE user_id = '".$user_id."'
476;";
477  pwg_query($query);
478}
479
480
481/* Function called from main.inc.php - Triggered on user deletion */
482function DeleteConfirmMail($user_id)
483{
484  $query = "
485DELETE FROM ".USER_CONFIRM_MAIL_TABLE."
486WHERE user_id = '".$user_id."'
487;";
488  pwg_query($query);
489}
490
491/* Function called from main.inc.php - Triggered on user deletion */
492function DeleteLastVisit($user_id)
493{
494  $query = "
495DELETE FROM ".USER_LASTVISIT_TABLE."
496WHERE user_id = '".$user_id."'
497;";
498  pwg_query($query);
499}
500
501
502/* Function called from main.inc.php - Triggered on user deletion */
503function DeleteRedir($user_id)
504{
505  $tab = array();
506
507  $query = "
508SELECT value
509FROM ".CONFIG_TABLE."
510WHERE param = 'UserAdvManager_Redir'
511;";
512
513  $tab = pwg_db_fetch_row(pwg_query($query));
514 
515  $values = explode(',', $tab[0]);
516
517  unset($values[array_search($user_id, $values)]);
518     
519  $query = "
520UPDATE ".CONFIG_TABLE."
521SET value = \"".implode(',', $values)."\"
522WHERE param = 'UserAdvManager_Redir';";
523
524  pwg_query($query);
525}
526
527
528/* Function called from ConfirmMail.php to verify validation key used by user according time limit */
529function VerifyConfirmMail($id)
530{
531  global $conf;
532
533  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
534 
535  $conf_UAM = unserialize($conf['UserAdvManager']);
536
537  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
538
539  $query = "
540SELECT COUNT(*)
541FROM ".USER_CONFIRM_MAIL_TABLE."
542WHERE id = '".$id."'
543;";
544  list($count) = pwg_db_fetch_row(pwg_query($query));
545
546  if ($count == 1)
547  {
548    $query = "
549SELECT user_id, status, date_check
550FROM ".USER_CONFIRM_MAIL_TABLE."
551WHERE id = '".$id."'
552;";
553    $data = pwg_db_fetch_assoc(pwg_query($query));
554       
555    if (!empty($data) and isset($data['user_id']) and !isset($data['date_check']))
556    {
557      $query = "
558SELECT registration_date
559FROM ".USER_INFOS_TABLE."
560WHERE user_id = '".$data['user_id']."'
561;";
562      list($registration_date) = pwg_db_fetch_row(pwg_query($query));
563
564/*              Time limit process              */
565/* ******************************************** */ 
566      if (!empty($registration_date))
567      {
568                                // Verify Confirmmail with time limit ON
569                                if (isset ($conf_UAM_ConfirmMail[1]))
570                                {
571                                        // dates formating and compare
572                                        $today = date("d-m-Y"); // Get today's date
573                                        list($day, $month, $year) = explode('-', $today); // explode date of today                                               
574                                        $daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
575                       
576                                list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
577                                        list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
578                                        $regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
579                       
580                                        $deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps     
581                                        $deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
582
583                                        // Condition with the value set for time limit
584                                        if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
585                                        {
586                                                list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
587
588                                                $query = '
589UPDATE '.USER_CONFIRM_MAIL_TABLE.'
590SET date_check="'.$dbnow.'"
591WHERE id = "'.$id.'"
592;';
593                                                pwg_query($query);
594     
595                                                if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
596                                                {
597                                                        $query = "
598DELETE FROM ".USER_GROUP_TABLE."
599WHERE user_id = '".$data['user_id']."'
600  AND group_id = '".$conf_UAM[2]."'
601;";
602                                                        pwg_query($query);
603                                                }
604           
605                                                if ($conf_UAM[3] <> -1) // Add user to validated users group
606                                                {
607                                                        $query = "
608INSERT INTO ".USER_GROUP_TABLE."
609  (user_id, group_id)
610VALUES
611  ('".$data['user_id']."', '".$conf_UAM[3]."')
612;";
613                                                        pwg_query($query);
614                                                }
615
616                                                if (($conf_UAM[4] <> -1 or isset($data['status']))) // Change user's status
617                                                {
618                                                        $query = "
619UPDATE ".USER_INFOS_TABLE."
620SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
621WHERE user_id = '".$data['user_id']."'
622;";
623                                                        pwg_query($query);
624                                                }
625                                                // Refresh user's category cache
626                                                invalidate_user_cache();
627 
628                                                return true;
629                                        }
630                                        elseif ($deltadays > $conf_UAM_ConfirmMail[1]) // If timelimit exeeds
631                                        {
632                                                return false;
633                                        }
634                                }
635                                // Verify Confirmmail with time limit OFF
636                                else
637                                {
638                                        list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
639
640                                        $query = '
641UPDATE '.USER_CONFIRM_MAIL_TABLE.'
642SET date_check="'.$dbnow.'"
643WHERE id = "'.$id.'"
644;';
645                                        pwg_query($query);
646     
647                                        if ($conf_UAM[2] <> -1)
648                                        {
649                                                $query = "
650DELETE FROM ".USER_GROUP_TABLE."
651WHERE user_id = '".$data['user_id']."'
652AND group_id = '".$conf_UAM[2]."'
653;";
654                                                pwg_query($query);
655                                        }
656   
657                                        if ($conf_UAM[3] <> -1)
658                                        {
659                                                $query = "
660DELETE FROM ".USER_GROUP_TABLE."
661WHERE user_id = '".$data['user_id']."'
662AND group_id = '".$conf_UAM[3]."'
663;";
664                                                pwg_query($query);
665
666                                                $query = "
667INSERT INTO ".USER_GROUP_TABLE."
668  (user_id, group_id)
669VALUES
670  ('".$data['user_id']."', '".$conf_UAM[3]."')
671;";
672                                                pwg_query($query);
673                                        }
674
675                                        if (($conf_UAM[4] <> -1 or isset($data['status'])))
676                                        {
677                                                $query = "
678UPDATE ".USER_INFOS_TABLE."
679SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
680WHERE user_id = '".$data['user_id']."'
681;";
682                                                pwg_query($query);
683                                        }
684                                        // Refresh user's category cache
685                                        invalidate_user_cache();
686 
687                                        return true;
688                                }
689                        }
690                }
691        }
692  else
693    return false;
694}
695
696/* Function called from UAM_admin.php to force users validation by admin */
697function ForceValidation($id)
698{
699  global $conf;
700
701  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
702
703  $conf_UAM = unserialize($conf['UserAdvManager']);
704       
705  $query = "
706SELECT COUNT(*)
707  FROM ".USER_CONFIRM_MAIL_TABLE."
708WHERE user_id = '".$id."'
709;";
710  list($count) = pwg_db_fetch_row(pwg_query($query));
711
712  if ($count == 1)
713  {
714    $query = "
715SELECT user_id, status, date_check
716  FROM ".USER_CONFIRM_MAIL_TABLE."
717WHERE user_id = '".$id."'
718;";
719    $data = pwg_db_fetch_assoc(pwg_query($query));
720
721    if (!empty($data) and isset($data['user_id']) and !isset($data['date_check']))
722    {     
723                        list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
724
725                        $query = "
726UPDATE ".USER_CONFIRM_MAIL_TABLE."
727SET date_check='".$dbnow."'
728WHERE user_id = '".$data['user_id']."'
729;";
730                        pwg_query($query);
731             
732                        if ($conf_UAM[2] <> -1)
733                        {
734                                $query = "
735DELETE FROM ".USER_GROUP_TABLE."
736WHERE user_id = '".$data['user_id']."'
737  AND group_id = '".$conf_UAM[2]."'
738;";
739                                pwg_query($query);
740                        }
741 
742                        if ($conf_UAM[3] <> -1)
743                        {
744                                $query = "
745DELETE FROM ".USER_GROUP_TABLE."
746WHERE user_id = '".$data['user_id']."'
747  AND group_id = '".$conf_UAM[3]."'
748                                ;";
749                                pwg_query($query);
750       
751                                $query = "
752INSERT INTO ".USER_GROUP_TABLE."
753  (user_id, group_id)
754VALUES
755  ('".$data['user_id']."', '".$conf_UAM[3]."')
756;";
757                                pwg_query($query);
758                        }
759
760                        if (($conf_UAM[4] <> -1 or isset($data['status'])))
761                        {
762                                $query = "
763UPDATE ".USER_INFOS_TABLE."
764SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
765WHERE user_id = '".$data['user_id']."'
766;";
767                                pwg_query($query);
768                        }
769                        // Refresh user's category cache
770                        invalidate_user_cache();
771                        return true;
772                }
773        }
774}
775
776
777/* Function called from main.inc.php - Check if username matches forbidden caracters */
778function ValidateUsername($login)
779{
780  global $conf;
781
782  $conf_UAM = unserialize($conf['UserAdvManager']);
783
784  if (isset($login) and isset($conf_UAM[7]) and $conf_UAM[7] <> '')
785  {
786    $conf_CharExclusion = preg_split("/,/",$conf_UAM[7]);
787    for ($i = 0 ; $i < count($conf_CharExclusion) ; $i++)
788    {
789      $pattern = '/'.$conf_CharExclusion[$i].'/i';
790      if (preg_match($pattern, $login))
791      {
792        return true;
793      }
794    }
795  }
796  else
797  {
798    return false;
799  }
800}
801
802
803/* Function called from main.inc.php - Check if user's email is in excluded email providers list */
804/* Doesn't work on call - Must be copied in main.inc.php to work */
805function ValidateEmailProvider($email)
806{
807  global $conf;
808
809  $conf_UAM = unserialize($conf['UserAdvManager']);
810 
811        if (isset($email) and isset($conf_UAM[12]) and $conf_UAM[12] <> '')
812        {
813                //$ncsemail = strtolower($email);
814                $conf_MailExclusion = preg_split("/[\s,]+/",$conf_UAM[12]);
815                for ($i = 0 ; $i < count($conf_MailExclusion) ; $i++)
816                {
817                        $pattern = '/'.$conf_MailExclusion[$i].'/i';
818                        if (preg_match($pattern, $email))
819      {
820                        return true;
821      }
822                }
823        }
824  else
825  {
826    return false;
827  }
828}
829
830
831/* Function called from UserAdvManager.php - Get unvalidated users according time limit */
832function get_unvalid_user_list()
833{
834        global $conf, $page;
835         
836        /* Get ConfirmMail configuration */
837  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
838  /* Get UAM configuration */
839  $conf_UAM = unserialize($conf['UserAdvManager']);
840 
841  $users = array();
842
843        /* search users depending expiration date */
844  $query = '
845SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
846                u.'.$conf['user_fields']['username'].' AS username,
847                u.'.$conf['user_fields']['email'].' AS email,
848                ui.status,
849                ui.adviser,
850                ui.enabled_high,
851                ui.level,
852                ui.registration_date
853FROM '.USERS_TABLE.' AS u
854  INNER JOIN '.USER_INFOS_TABLE.' AS ui
855    ON u.'.$conf['user_fields']['id'].' = ui.user_id
856  LEFT JOIN '.USER_GROUP_TABLE.' AS ug
857    ON u.'.$conf['user_fields']['id'].' = ug.user_id
858WHERE u.'.$conf['user_fields']['id'].' >= 3
859  AND (TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) >= "'.$conf_UAM_ConfirmMail[1].'"
860  OR TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) < "'.$conf_UAM_ConfirmMail[1].'")';
861
862        if ($conf_UAM[2] <> '-1' and $conf_UAM[8] == '-1')
863  {
864    $query.= '
865  AND ug.group_id = '.$conf_UAM[2];
866  }
867  if ($conf_UAM[2] == '-1' and $conf_UAM[8] <> '-1')
868  {
869    $query.= '
870  AND ui.status = \''.$conf_UAM[8]."'";
871  }
872  if ($conf_UAM[2] <> '-1' and $conf_UAM[8] <> '-1')
873  {
874    $query.= '
875  AND ug.group_id = \''.$conf_UAM[2]."'";
876  }
877  $query.= '
878ORDER BY ui.registration_date ASC
879;';
880
881        $result = pwg_query($query);
882     
883  while ($row = pwg_db_fetch_assoc($result))
884  {
885        $user = $row;
886    $user['groups'] = array();
887
888    array_push($users, $user);
889        }
890
891        /* add group lists */
892  $user_ids = array();
893  foreach ($users as $i => $user)
894  {
895        $user_ids[$i] = $user['id'];
896        }
897       
898        $user_nums = array_flip($user_ids);
899
900  if (count($user_ids) > 0)
901  {
902        $query = '
903SELECT user_id, group_id
904  FROM '.USER_GROUP_TABLE.'
905WHERE user_id IN ('.implode(',', $user_ids).')
906;';
907       
908                $result = pwg_query($query);
909       
910    while ($row = pwg_db_fetch_assoc($result))
911    {
912        array_push(
913        $users[$user_nums[$row['user_id']]]['groups'],
914        $row['group_id']
915                        );
916                }
917        }
918
919        return $users;
920}
921
922
923/* Function called from UserAdvManager.php - Get ghost users */
924function get_ghost_user_list()
925{
926        global $conf, $page;
927
928  $conf_UAM = unserialize($conf['UserAdvManager']);
929 
930  $users = array();
931
932        /* search users depending expiration date */
933  $query = '
934SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
935                u.'.$conf['user_fields']['username'].' AS username,
936                u.'.$conf['user_fields']['email'].' AS email,
937                lv.lastvisit,
938                lv.reminder
939FROM '.USERS_TABLE.' AS u
940  INNER JOIN '.USER_LASTVISIT_TABLE.' AS lv
941    ON u.'.$conf['user_fields']['id'].' = lv.user_id
942WHERE (TO_DAYS(NOW()) - TO_DAYS(lv.lastvisit) >= "'.$conf_UAM[17].'")
943ORDER BY lv.lastvisit ASC;';
944
945        $result = pwg_query($query);
946     
947  while ($row = pwg_db_fetch_assoc($result))
948  {
949        $user = $row;
950    $user['groups'] = array();
951
952    array_push($users, $user);
953        }
954
955        /* add group lists */
956  $user_ids = array();
957  foreach ($users as $i => $user)
958  {
959        $user_ids[$i] = $user['id'];
960        }
961
962        return $users;
963}
964
965
966/* Function called from UserAdvManager.php - Get all users to display the number of days since their last visit */
967function get_user_list()
968{
969        global $conf, $page;
970 
971  $users = array();
972
973        /* search users depending expiration date */
974  $query = '
975SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
976                u.'.$conf['user_fields']['username'].' AS username,
977                u.'.$conf['user_fields']['email'].' AS email,
978                ug.lastvisit
979FROM '.USERS_TABLE.' AS u
980  INNER JOIN '.USER_LASTVISIT_TABLE.' AS ug
981    ON u.'.$conf['user_fields']['id'].' = ug.user_id
982WHERE u.'.$conf['user_fields']['id'].' >= 3
983ORDER BY ug.lastvisit DESC
984;';
985
986        $result = pwg_query($query);
987     
988  while ($row = pwg_db_fetch_assoc($result))
989  {
990        $user = $row;
991    $user['groups'] = array();
992
993    array_push($users, $user);
994        }
995
996        /* add group lists */
997  $user_ids = array();
998  foreach ($users as $i => $user)
999  {
1000        $user_ids[$i] = $user['id'];
1001        }
1002
1003        return $users;
1004}
1005
1006
1007/* Function called from UserAdvManager.php - to determine who is expired or not and giving a different display color */
1008function expiration($id)
1009{
1010        global $conf, $page;
1011         
1012        /* Get ConfirmMail configuration */
1013  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
1014         
1015        /* Get UAM configuration */
1016  $conf_UAM = unserialize($conf['UserAdvManager']);
1017       
1018        $query = "
1019SELECT registration_date
1020  FROM ".USER_INFOS_TABLE."
1021WHERE user_id = '".$id."'
1022;";
1023        list($registration_date) = pwg_db_fetch_row(pwg_query($query));
1024
1025/*              Time limit process              */
1026/* ******************************************** */ 
1027        if (!empty($registration_date))
1028  {
1029                // dates formating and compare
1030                $today = date("d-m-Y"); // Get today's date
1031                list($day, $month, $year) = explode('-', $today); // explode date of today                                               
1032                $daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
1033               
1034          list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
1035                list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
1036                $regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
1037                       
1038                $deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps     
1039                $deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
1040
1041                // Condition with the value set for time limit
1042                if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
1043                {
1044                        return false;
1045                }
1046                else
1047                {
1048                        return true;
1049                }
1050        }
1051}
1052
1053
1054/**
1055 * Returns a password's score for password complexity check
1056 *
1057 * @param password filled by user
1058 *
1059 * Thanx to MathieuGut from http://m-gut.developpez.com
1060 */
1061function testpassword($password) // Le mot de passe passé en paramètre - $password given by user
1062{
1063
1064  // Initialisation des variables - Variables initiation
1065  $points = 0;
1066  $point_lowercase = 0;
1067  $point_uppercase = 0;
1068  $point_numbers = 0;
1069  $point_characters = 0;
1070
1071  // On récupère la longueur du mot de passe - Getting password lengh   
1072  $length = strlen($password);
1073 
1074  // On fait une boucle pour lire chaque lettre - Loop to read password characters
1075  for($i = 0; $i < $length; $i++)
1076  {
1077    // On sélectionne une à une chaque lettre - Select each letters
1078    // $i étant à 0 lors du premier passage de la boucle - $i is 0 at first turn
1079    $letters = $password[$i];
1080
1081    if ($letters>='a' && $letters<='z')
1082    {
1083      // On ajoute 1 point pour une minuscule - Adding 1 point to score for a lowercase
1084                  $points = $points + 1;
1085
1086                  // On rajoute le bonus pour une minuscule - Adding bonus points for lowercase
1087                  $point_lowercase = 1;
1088    }
1089    else if ($letters>='A' && $letters <='Z')
1090    {
1091      // On ajoute 2 points pour une majuscule - Adding 2 points to score for uppercase
1092      $points = $points + 2;
1093               
1094      // On rajoute le bonus pour une majuscule - Adding bonus points for uppercase
1095      $point_uppercase = 2;
1096    }
1097    else if ($letters>='0' && $letters<='9')
1098    {
1099      // On ajoute 3 points pour un chiffre - Adding 3 points to score for numbers
1100      $points = $points + 3;
1101               
1102      // On rajoute le bonus pour un chiffre - Adding bonus points for numbers
1103      $point_numbers = 3;
1104    }
1105    else
1106    {
1107      // On ajoute 5 points pour un caractère autre - Adding 5 points to score for special characters
1108      $points = $points + 5;
1109               
1110      // On rajoute le bonus pour un caractère autre - Adding bonus points for special characters
1111      $point_characters = 5;
1112    }
1113  }
1114
1115  // Calcul du coefficient points/longueur - calculating the coefficient points/length
1116  $step1 = $points / $length;
1117
1118  // Calcul du coefficient de la diversité des types de caractères... - Calculation of the diversity of character types...
1119  $step2 = $point_lowercase + $point_uppercase + $point_numbers + $point_characters;
1120
1121  // Multiplication du coefficient de diversité avec celui de la longueur - Multiplying the coefficient of diversity with that of the length
1122  $score = $step1 * $step2;
1123
1124  // Multiplication du résultat par la longueur de la chaîne - Multiplying the result by the length of the chain
1125  $finalscore = $score * $length;
1126
1127  return $finalscore;
1128}
1129
1130/* Function called from maintain.inc.php - to check if database upgrade is needed */
1131function table_exist($table)
1132{
1133  $query = 'DESC '.$table.';';
1134  return (bool)($res=pwg_query($query));
1135}
1136
1137/* Email sending debugger function */
1138function MailLog($to, $subject, $content, $language)
1139{
1140   $fo=fopen (UAM_PATH.'admin/maillog.txt','a') ;
1141   fwrite($fo,"======================\n") ;
1142   fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n");
1143   fwrite($fo,$to . "\n" . $subject . "\r\n") ;
1144   fwrite($fo, "\n" . $content . "\r\n") ;
1145   fwrite($fo, 'Langue : '."\n" . $language . "\r\n") ;
1146   fclose($fo) ;
1147   //return mail ($to,$subject) ;
1148}
1149
1150
1151/* Function called from UAM_admin.php and main.inc.php to get the plugin version and name */
1152function PluginInfos($dir)
1153{
1154  $path = $dir;
1155
1156  $plg_data = implode( '', file($path.'main.inc.php') );
1157  if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
1158  {
1159    $plugin['name'] = trim( $val[1] );
1160  }
1161  if (preg_match("|Version: (.*)|", $plg_data, $val))
1162  {
1163    $plugin['version'] = trim($val[1]);
1164  }
1165  if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
1166  {
1167    $plugin['uri'] = trim($val[1]);
1168  }
1169  if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
1170  {
1171    $plugin['description'] = trim($desc);
1172  }
1173  elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
1174  {
1175    $plugin['description'] = trim($val[1]);
1176  }
1177  if ( preg_match("|Author: (.*)|", $plg_data, $val) )
1178  {
1179    $plugin['author'] = trim($val[1]);
1180  }
1181  if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
1182  {
1183    $plugin['author uri'] = trim($val[1]);
1184  }
1185  if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
1186  {
1187    list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
1188    if (is_numeric($extension)) $plugin['extension'] = $extension;
1189  }
1190// IMPORTANT SECURITY !
1191  $plugin = array_map('htmlspecialchars', $plugin);
1192
1193  return $plugin ;
1194}
1195
1196
1197function clean_obsolete_files()
1198{
1199  if (file_exists(UAM_PATH.'obsolete.list')
1200    and $old_files = file(UAM_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
1201    and !empty($old_files))
1202  {
1203    array_push($old_files, 'obsolete.list');
1204    foreach($old_files as $old_file)
1205    {
1206      $path = UAM_PATH.$old_file;
1207      if (is_file($path))
1208      {
1209        @unlink($path);
1210      }
1211    }
1212  }
1213}
1214
1215
1216// check_consult - Thx to LucMorizur
1217// checks if a user id is registered as having already
1218// visited his profile.php page.
1219// @uid        : the user id
1220// @user_idsOK : (returned) array of all users ids having already visited
1221//               their profile.php pages
1222//
1223// @returns    : true or false whether the users has already visited his
1224//               profile.php page or not
1225function check_consult($uid, &$user_idsOK)
1226{
1227  $t = array();
1228  $v = false;
1229 
1230  $query = "
1231SELECT value
1232FROM ".CONFIG_TABLE."
1233WHERE param = 'UserAdvManager_Redir'
1234;";
1235 
1236  if ($v = (($t = pwg_db_fetch_row(pwg_query($query))) !== false))
1237  {
1238    $user_idsOK = explode(',', $t[0]);
1239    $v = (in_array($uid, $user_idsOK));
1240  }
1241  return $v;
1242}
1243?>
Note: See TracBrowser for help on using the repository browser.