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

Last change on this file since 7657 was 7657, checked in by Eric, 13 years ago

Merge from Trunk to Branch 2.15

  • 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  global $conf;
9
10  $conf_UAM = unserialize($conf['UserAdvManager']);
11 
12        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
13 
14        $infos1_perso = "";
15  $infos2_perso = "";
16
17/* We have to get the user's language in database */
18  $query ='
19SELECT user_id, language
20FROM '.USER_INFOS_TABLE.'
21WHERE user_id = '.$id.'
22;';
23  $data = pwg_db_fetch_assoc(pwg_query($query));
24
25/* Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language */
26  if (empty($data))
27  {
28/* And switch gallery to this language before using personalized and multilangual contents */
29    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
30    switch_lang_to($language);
31  }
32  else
33  {
34/* And switch gallery to this language before using personalized and multilangual contents */
35    $language = $data['language']; /* Usefull for debugging */
36    switch_lang_to($data['language']);
37    load_language('plugin.lang', UAM_PATH);
38  }
39
40  switch($typemail)
41  {
42    case 1:
43      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Add of %s', stripslashes($username)));
44      $password = $password <> '' ? $password : l10n('UAM_empty_pwd');
45     
46      if (isset($conf_UAM[9]) and $conf_UAM[9] <> '')
47      {
48        if (function_exists('get_user_language_desc'))
49        {
50          $infos1_perso = get_user_language_desc($conf_UAM[9])."\n\n";
51        }
52        else $infos1_perso = l10n($conf_UAM[9])."\n\n"; 
53      }
54     
55      break;
56     
57    case 2:
58      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Update of %s', stripslashes($username)));
59      $password = $password <> '' ? $password : l10n('UAM_empty_pwd');
60
61      break;
62       
63    case 3:
64      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Update of %s', stripslashes($username)));
65      $password = $password <> '' ? $password : l10n('UAM_no_update_pwd');
66
67      break;
68  }
69
70  if (isset($conf_UAM[0]) and $conf_UAM[0] == 'true')
71  {
72    $infos1 = array(
73      get_l10n_args('infos_mail %s', stripslashes($username)),
74      get_l10n_args('User: %s', stripslashes($username)),
75      get_l10n_args('Password: %s', $password),
76      get_l10n_args('Email: %s', $email),
77      get_l10n_args('', ''),
78    );
79  }
80
81
82  if ( isset($conf_UAM[1]) and $conf_UAM[1] == 'true' and $confirm)
83  {
84    $infos2 = array
85    (
86      get_l10n_args('Link: %s', AddConfirmMail($id, $email)),
87      get_l10n_args('', ''),
88    );
89
90    if (isset($conf_UAM[10]) and $conf_UAM[10] <> '')
91    {
92      if (function_exists('get_user_language_desc'))
93      {
94        $infos2_perso = get_user_language_desc($conf_UAM[10])."\n\n";
95      }
96      else $infos2_perso = l10n($conf_UAM[10])."\n\n";
97    }
98  }
99
100/* ******************************************************** */
101/* **** Pending code since to find how to make it work **** */
102/* ******************************************************** */
103// Testing if FCK Editor is used. Then decoding htmlchars to avoid problems with pwg_mail()
104/*$areas = array();
105array_push( $areas,'UAM_MailInfo_Text','UAM_ConfirmMail_Text');
106
107if (function_exists('set_fckeditor_instance'))
108{
109  $fcke_config = unserialize($conf['FCKEditor']);
110  foreach($areas as $area)
111  {
112    if (isset($fcke_config['UAM_MailInfo_Text']) and $fcke_config['UAM_MailInfo_Text'] = true)
113    {
114      $infos1_perso = html_entity_decode($infos1_perso,ENT_QUOTES,UTF-8);
115    }
116   
117    if (isset($fcke_config['UAM_ConfirmMail_Text']) and $fcke_config['UAM_ConfirmMail_Text'] = true)
118    {
119      $infos2_perso = html_entity_decode($infos2_perso,ENT_QUOTES,UTF-8);
120    }
121  }
122}*/
123
124
125/* Sending the email with subject and contents */
126  pwg_mail($email, array(
127    'subject' => $subject,
128    'content' => (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
129  ));
130
131/* ********************** */
132/* Email sending debugger */
133/* This is only to trace  */
134/* the send of emails for */
135/* debugging              */
136/* ********************** */
137//$content = (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url();   
138//MailLog($email,$subject,$content,$language);
139/* ********************** */
140
141/* Switching back to default language */
142switch_lang_back();
143}
144
145
146/* Function called from UAM_admin.php to resend validation email with or without new validation key */
147function ResendMail2User($typemail, $user_id, $username, $email, $confirm)
148{
149  /* Only available for next Piwigo release (bug in switch_lang function) */
150  global $conf;
151
152  $conf_UAM = unserialize($conf['UserAdvManager']);
153
154  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
155 
156        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
157 
158        $infos1_perso = "";
159  $infos2_perso = "";
160 
161/* We have to get the user's language in database */
162  $query ='
163SELECT user_id, language
164FROM '.USER_INFOS_TABLE.'
165WHERE user_id = '.$user_id.'
166;';
167  $data = pwg_db_fetch_assoc(pwg_query($query));
168  $language = $data['language'];
169 
170/* And switch gallery to this language before using personalized and multilangual contents */
171  switch_lang_to($data['language']);
172   
173  load_language('plugin.lang', UAM_PATH);
174
175  switch($typemail)
176  {
177    case 1:
178      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Reminder_with_key_of_%s', $username));
179     
180      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)
181      {
182        if (function_exists('get_user_language_desc'))
183        {
184          $infos1 = get_user_language_desc($conf_UAM_ConfirmMail[2])."\n\n";
185        }
186                                else $infos1 = l10n($conf_UAM_ConfirmMail[2])."\n\n";
187
188        $infos2 = array
189        (
190          get_l10n_args('Link: %s', ResetConfirmMail($user_id)),
191          get_l10n_args('', ''),
192        );       
193                        }
194
195/* Set reminder true */     
196      $query = "
197UPDATE ".USER_CONFIRM_MAIL_TABLE."
198SET reminder = 'true'
199WHERE user_id = '".$user_id."'
200;";
201      pwg_query($query);
202     
203                break;
204     
205    case 2:
206      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Reminder_without_key_of_%s',$username));
207     
208      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)
209      {
210        if (function_exists('get_user_language_desc'))
211        {
212          $infos1 = get_user_language_desc($conf_UAM_ConfirmMail[2])."\n\n";
213        }
214        else $infos1 = l10n($conf_UAM_ConfirmMail[2])."\n\n";
215      }
216     
217/* Set reminder true */     
218      $query = "
219UPDATE ".USER_CONFIRM_MAIL_TABLE."
220SET reminder = 'true'
221WHERE user_id = '".$user_id."'
222;";
223      pwg_query($query);
224     
225    break;
226        }
227 
228  pwg_mail($email, array(
229    'subject' => $subject,
230    'content' => ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
231  ));
232
233/* ********************** */
234/* Email sending debugger */
235/* This is only to trace  */
236/* the send of emails for */
237/* debugging              */
238/* ********************** */
239//$content = ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url();
240//MailLog($email,$subject,$content,$language);
241/* ********************** */
242
243/* Switching back to default language */
244switch_lang_back();
245}
246
247
248/* Function called from UAM_admin.php to send a reminder mail for ghost users */
249function ghostreminder($user_id, $username, $email)
250{
251  /* Only available for next Piwigo release (bug in switch_lang function) */
252  global $conf;
253
254  $conf_UAM = unserialize($conf['UserAdvManager']);
255 
256        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
257 
258        $infos1_perso = "";
259
260/* We have to get the user's language in database */
261  $query ='
262SELECT user_id, language
263FROM '.USER_INFOS_TABLE.'
264WHERE user_id = '.$user_id.'
265;';
266  $data = pwg_db_fetch_assoc(pwg_query($query));
267  $language = $data['language'];
268
269/* And switch gallery to this language before using personalized and multilangual contents */
270  switch_lang_to($data['language']);
271   
272  load_language('plugin.lang', UAM_PATH);
273 
274  $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Ghost_reminder_of_%s', $username));     
275
276  if (isset($conf_UAM[18]) and $conf_UAM[18] <> '' and isset($conf_UAM[16]) and $conf_UAM[16] == 'true')
277  {
278    if (function_exists('get_user_language_desc'))
279    {
280      $infos1 = get_user_language_desc($conf_UAM[18])."\n\n";
281    }
282    else
283    {
284      $infos1 = l10n($conf_UAM[18])."\n\n";
285    }
286
287    resetlastvisit($user_id);
288  }
289
290  pwg_mail($email, array(
291    'subject' => $subject,
292    'content' => $infos1.get_absolute_root_url(),
293  ));
294
295/* ********************** */
296/* Email sending debugger */
297/* This is only to trace  */
298/* the send of emails for */
299/* debugging              */
300/* ********************** */
301//$content = get_user_language_desc($conf_UAM[19])."\n\n"; 
302//MailLog($email,$subject,$content,$language);
303/* ********************** */
304
305/* Switching back to default language */
306switch_lang_back();
307}
308
309
310/* Function called from functions AddConfirmMail and ResetConfirmMail for validation key generation */
311function FindAvailableConfirmMailID()
312{
313  while (true)
314  {
315    $id = generate_key(16);
316    $query = "
317SELECT COUNT(*)
318  FROM ".USER_CONFIRM_MAIL_TABLE."
319WHERE id = '".$id."'
320;";
321    list($count) = pwg_db_fetch_row(pwg_query($query));
322
323    if ($count == 0)
324      return $id;
325  }
326}
327
328
329/* Function called from functions SendMail2User to process unvalidated users and generate validation key link */
330function AddConfirmMail($user_id, $email)
331{
332  global $conf;
333
334  $conf_UAM = unserialize($conf['UserAdvManager']);
335  $Confirm_Mail_ID = FindAvailableConfirmMailID();
336
337  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
338 
339  if (isset($Confirm_Mail_ID))
340  {
341    $query = "
342SELECT status
343  FROM ".USER_INFOS_TABLE."
344WHERE user_id = '".$user_id."'
345;";
346    list($status) = pwg_db_fetch_row(pwg_query($query));
347   
348    $query = "
349INSERT INTO ".USER_CONFIRM_MAIL_TABLE."
350  (id, user_id, mail_address, status, date_check)
351VALUES
352  ('".$Confirm_Mail_ID."', '".$user_id."', '".$email."', '".$status."', null)
353;";
354    pwg_query($query);
355
356    $query = "
357DELETE FROM ".USER_GROUP_TABLE."
358WHERE user_id = '".$user_id."'
359  AND (
360    group_id = '".$conf_UAM[2]."'
361  OR
362    group_id = '".$conf_UAM[3]."'
363  )
364;";
365    pwg_query($query);
366
367    if (!is_admin() and $conf_UAM[8] <> -1)
368    {
369      $query = "
370UPDATE ".USER_INFOS_TABLE."
371SET status = '".$conf_UAM[8]."'
372WHERE user_id = '".$user_id."'
373;";
374      pwg_query($query);
375    }
376
377    if ( $conf_UAM[2] <> -1 )
378    {
379      $query = "
380INSERT INTO ".USER_GROUP_TABLE."
381  (user_id, group_id)
382VALUES
383  ('".$user_id."', '".$conf_UAM[2]."')
384;";
385      pwg_query($query);
386    }
387   
388    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
389  }
390}
391
392
393/* Function called from main.inc.php to set group to new users if manual validation is set */
394function setgroup($user_id)
395{
396  global $conf;
397 
398  $conf_UAM = unserialize($conf['UserAdvManager']);
399
400  $query = "
401DELETE FROM ".USER_GROUP_TABLE."
402WHERE user_id = '".$user_id."'
403  AND (
404    group_id = '".$conf_UAM[2]."'
405  OR
406    group_id = '".$conf_UAM[3]."'
407  )
408;";
409  pwg_query($query);
410
411  if (!is_admin() and $conf_UAM[8] <> -1)
412  {
413    $query = "
414UPDATE ".USER_INFOS_TABLE."
415SET status = '".$conf_UAM[8]."'
416WHERE user_id = '".$user_id."'
417;";
418    pwg_query($query);
419  }
420
421  if ( $conf_UAM[2] <> -1 )
422  {
423    $query = "
424INSERT INTO ".USER_GROUP_TABLE."
425  (user_id, group_id)
426VALUES
427  ('".$user_id."', '".$conf_UAM[2]."')
428;";
429    pwg_query($query);
430  }
431}
432
433
434/* Function called from UAM_admin.php to reset validation key */
435function ResetConfirmMail($user_id)
436{
437  global $conf;
438 
439  $Confirm_Mail_ID = FindAvailableConfirmMailID();
440
441  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
442 
443  if (isset($Confirm_Mail_ID))
444  { 
445    $query = "
446UPDATE ".USER_CONFIRM_MAIL_TABLE."
447SET id = '".$Confirm_Mail_ID."'
448WHERE user_id = '".$user_id."'
449;";
450    pwg_query($query);
451
452                $query = "
453UPDATE ".USER_INFOS_TABLE."
454SET registration_date = '".$dbnow."'
455WHERE user_id = '".$user_id."'
456;";
457                pwg_query($query);
458   
459    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
460  }
461}
462
463
464/* Function called from function_UserAdvManager.inc.php to reset last visit date after sending a reminder */
465function resetlastvisit($user_id)
466{
467  global $conf;
468
469  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
470
471  $query = "
472UPDATE ".USER_LASTVISIT_TABLE."
473SET lastvisit = '".$dbnow."', reminder = 'true'
474WHERE user_id = '".$user_id."'
475;";
476  pwg_query($query);
477}
478
479
480/* Function called from main.inc.php - Triggered on user deletion */
481function DeleteConfirmMail($user_id)
482{
483  $query = "
484DELETE FROM ".USER_CONFIRM_MAIL_TABLE."
485WHERE user_id = '".$user_id."'
486;";
487  pwg_query($query);
488}
489
490/* Function called from main.inc.php - Triggered on user deletion */
491function DeleteLastVisit($user_id)
492{
493  $query = "
494DELETE FROM ".USER_LASTVISIT_TABLE."
495WHERE user_id = '".$user_id."'
496;";
497  pwg_query($query);
498}
499
500
501/* Function called from main.inc.php - Triggered on user deletion */
502function DeleteRedir($user_id)
503{
504  $tab = array();
505
506  $query = '
507SELECT value
508FROM '.CONFIG_TABLE.'
509WHERE param = "UserAdvManager_Redir"
510;';
511
512  $tab = pwg_db_fetch_row(pwg_query($query));
513 
514  $values = explode(',', $tab[0]);
515
516  unset($values[array_search($user_id, $values)]);
517     
518  $query = "
519UPDATE ".CONFIG_TABLE."
520SET value = \"".implode(',', $values)."\"
521WHERE param = 'UserAdvManager_Redir';";
522
523  pwg_query($query);
524}
525
526
527/* Function called from ConfirmMail.php to verify validation key used by user according time limit */
528/* Return true is key validation is OK else return false */
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.