source: extensions/UserAdvManager/branches/2.20/include/functions.inc.php @ 9909

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

r9908 merged from trunk to branch 2.20

  • Property svn:eol-style set to LF
File size: 63.9 KB
Line 
1<?php
2include_once (UAM_PATH.'include/constants.php');
3load_language('plugin.lang', UAM_PATH);
4
5/**
6 * Triggered on get_admin_plugin_menu_links
7 *
8 * Plugin's administration menu
9 */
10function UAM_admin_menu($menu)
11{
12// +-----------------------------------------------------------------------+
13// |                      Getting plugin name                              |
14// +-----------------------------------------------------------------------+
15  $plugin =  PluginInfos(UAM_PATH);
16  $name = $plugin['name'];
17 
18  array_push($menu,
19    array(
20      'NAME' => $name,
21      'URL' => get_root_url().'admin.php?page=plugin-'.basename(UAM_PATH)
22    )
23  );
24
25  return $menu;
26}
27
28/**
29 * Triggered on loc_begin_index
30 *
31 * Initiating GhostTracker
32 */
33function UAM_GhostTracker()
34{
35  global $conf, $user;
36
37  $conf_UAM = unserialize($conf['UserAdvManager']);
38
39  // Admins, Guests and Adult_Content users are not tracked for Ghost Tracker or Users Tracker
40  if (!is_admin() and !is_a_guest() and $user['username'] != "16" and $user['username'] != "18")
41  {
42    if ((isset($conf_UAM[16]) and $conf_UAM[16] == 'true') or (isset($conf_UAM[19]) and $conf_UAM[19] == 'true'))
43    {
44
45      $userid = get_userid($user['username']);
46         
47      // Looking for existing entry in last visit table
48      $query = '
49SELECT *
50FROM '.USER_LASTVISIT_TABLE.'
51WHERE user_id = '.$userid.'
52;';
53       
54      $count = pwg_db_num_rows(pwg_query($query));
55         
56      if ($count == 0)
57      {
58        // If not, data are inserted in table
59        $query = '
60INSERT INTO '.USER_LASTVISIT_TABLE.' (user_id, lastvisit, reminder)
61VALUES ('.$userid.', now(), "false")
62;';
63        pwg_query($query);
64      }
65      else if ($count > 0)
66      {
67        // If yes, data are updated in table
68        $query = '
69UPDATE '.USER_LASTVISIT_TABLE.'
70SET lastvisit = now(), reminder = "false"
71WHERE user_id = '.$userid.'
72LIMIT 1
73;';
74        pwg_query($query);
75      }
76    }
77  }
78}
79
80/**
81 * Triggered on register_user
82 *
83 * Additional controls on user registration
84 */
85function UAM_Adduser($register_user)
86{
87  global $conf;
88
89  $conf_UAM = unserialize($conf['UserAdvManager']);
90
91  // Exclusion of Adult_Content users
92  if ($register_user['username'] != "16" and $register_user['username'] != "18")
93  {
94    if ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
95    {
96      // This is to send an information email and set user to "waiting" group or status until admin validation
97      $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
98      SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], false);
99      setgroup($register_user['id']);// Set to "waiting" group or status until admin validation
100    }
101    elseif ((isset($conf_UAM[0]) and $conf_UAM[0] == 'false') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
102    {
103      // This is to set user to "waiting" group or status until admin validation
104      setgroup($register_user['id']);// Set to "waiting" group or status until admin validation
105    }
106    elseif ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'false'))
107    {
108      // This is to send an information email without validation key
109      $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
110      SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], false);
111    }
112    // Sending registration confirmation by email
113    elseif ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true' or $conf_UAM[0] == 'false') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'true'))
114    {
115      if (is_admin() and isset($conf_UAM[20]) and $conf_UAM[20] == 'true')
116      {
117        $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
118        SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], true); 
119      }
120      elseif (is_admin() and isset($conf_UAM[20]) and $conf_UAM[20] == 'false')
121      {
122        $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
123        SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], false);
124      }
125      elseif (!is_admin())
126      {
127        $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
128        SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], true);
129      }
130    }
131  }
132}
133
134/**
135 * Triggered on delete_user
136 *
137 * Database cleanup on user deletion
138 */
139function UAM_Deluser($user_id)
140{
141  // Cleanup for ConfirmMail table
142  DeleteConfirmMail($user_id);
143  // Cleanup for LastVisit table
144  DeleteLastVisit($user_id);
145  // Cleanup Redirection settings
146  DeleteRedir($user_id);
147}
148
149/**
150 * Triggered on register_user_check
151 *
152 * Additional controls on user registration check
153 */
154function UAM_RegistrationCheck($errors, $user)
155{
156  global $conf;
157
158  // Exclusion of Adult_Content users
159  if ($user['username'] != "16" and $user['username'] != "18")
160  {
161    load_language('plugin.lang', UAM_PATH);
162
163    $PasswordCheck = 0;
164
165    $conf_UAM = unserialize($conf['UserAdvManager']);
166
167    // Password enforcement control
168    if (isset($conf_UAM[13]) and $conf_UAM[13] == 'true' and !empty($conf_UAM[14]))
169    {
170      if (!empty($user['password']) and !is_admin())
171      {
172        $PasswordCheck = testpassword($user['password']);
173 
174        if ($PasswordCheck < $conf_UAM[14])
175        {
176          $message = get_l10n_args('UAM_reg_err_login4_%s', $PasswordCheck);
177          $lang['reg_err_pass'] = l10n_args($message).$conf_UAM[14];
178          array_push($errors, $lang['reg_err_pass']);
179        }
180      }
181      else if (!empty($user['password']) and is_admin() and isset($conf_UAM[15]) and $conf_UAM[15] == 'true')
182      {
183        $PasswordCheck = testpassword($user['password']);
184 
185        if ($PasswordCheck < $conf_UAM[14])
186        {
187          $message = get_l10n_args('UAM_reg_err_login4_%s', $PasswordCheck);
188          $lang['reg_err_pass'] = l10n_args($message).$conf_UAM[14];
189          array_push($errors, $lang['reg_err_pass']);
190        }
191      }
192    }
193
194    // Username without forbidden keys
195    if (isset($conf_UAM[6]) and $conf_UAM[6] == 'true' and !empty($user['username']) and ValidateUsername($user['username']) and !is_admin())
196    {
197      $lang['reg_err_login1'] = l10n('UAM_reg_err_login2')."'".$conf_UAM[7]."'";
198      array_push($errors, $lang['reg_err_login1']);
199    }
200
201    // Email without forbidden domains
202    if (isset($conf_UAM[11]) and $conf_UAM[11] == 'true' and !empty($user['email']) and ValidateEmailProvider($user['email']) and !is_admin())
203    {
204      $lang['reg_err_login1'] = l10n('UAM_reg_err_login5')."'".$conf_UAM[12]."'";
205      array_push($errors, $lang['reg_err_login1']);
206    }
207    return $errors;
208  }
209}
210
211/**
212 * Triggered on loc_begin_profile
213 */
214function UAM_Profile_Init()
215{
216  global $conf, $user, $template;
217
218  $conf_UAM = unserialize($conf['UserAdvManager']);
219   
220  if ((isset($conf_UAM[21]) and $conf_UAM[21] == 'true'))
221  {
222    $user_idsOK = array();
223    if (!UAM_check_profile($user['id'], $user_idsOK))
224    {
225      $user_idsOK[] = $user['id'];
226
227      $query = "
228UPDATE ".CONFIG_TABLE."
229SET value = \"".implode(',', $user_idsOK)."\"
230WHERE param = 'UserAdvManager_Redir';";
231         
232      pwg_query($query);
233    }
234  }
235
236  if (isset($_POST['validate']) and !is_admin())
237  {
238    // Email without forbidden domains
239    if (isset($conf_UAM[11]) and $conf_UAM[11] == 'true' and !empty($_POST['mail_address']))
240    {
241      if (ValidateEmailProvider($_POST['mail_address']))
242      {
243        $template->append('errors', l10n('UAM_reg_err_login5')."'".$conf_UAM[12]."'");
244        unset($_POST['validate']);
245      }
246    }
247
248    $typemail = 3;
249
250    if (!empty($_POST['use_new_pwd']))
251    {
252      $typemail = 2;
253
254      // Password enforcement control
255      if (isset($conf_UAM[13]) and $conf_UAM[13] == 'true' and !empty($conf_UAM[14]))
256      {
257        $PasswordCheck = testpassword($_POST['use_new_pwd']);
258
259        if ($PasswordCheck < $conf_UAM[14])
260        {
261          $message = get_l10n_args('UAM_reg_err_login4_%s', $PasswordCheck);
262          $template->append('errors', l10n_args($message).$conf_UAM[14]);
263          unset($_POST['use_new_pwd']);
264          unset($_POST['validate']);
265        }
266      }
267    }
268
269    // Sending registration confirmation by email
270    if ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true') or (isset($conf_UAM[1]) and $conf_UAM[1] == 'true') or (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
271    {
272      $confirm_mail_need = false;
273
274      if (!empty($_POST['mail_address']))
275      {
276        $query = '
277SELECT '.$conf['user_fields']['email'].' AS email
278FROM '.USERS_TABLE.'
279WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
280;';
281
282        list($current_email) = pwg_db_fetch_row(pwg_query($query));
283
284        // This is to send a new validation key
285        if ($_POST['mail_address'] != $current_email and (isset($conf_UAM[1]) and $conf_UAM[1] == 'true'))
286       
287          $confirm_mail_need = true;
288
289        // This is to set the user to "waiting" group or status until admin validation
290        if ($_POST['mail_address'] != $current_email and (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
291       
292          setgroup($register_user['id']);// Set to "waiting" group or status until admin validation
293          $confirm_mail_need = false;
294      }
295       
296      if ((!empty($_POST['use_new_pwd']) and (isset($conf_UAM[0]) and $conf_UAM[0] == 'true') or $confirm_mail_need))
297      {
298        $query = '
299SELECT '.$conf['user_fields']['username'].'
300FROM '.USERS_TABLE.'
301WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
302;';
303       
304        list($username) = pwg_db_fetch_row(pwg_query($query));
305        SendMail2User($typemail, $user['id'], $username, $_POST['use_new_pwd'], $_POST['mail_address'], $confirm_mail_need);
306      }
307    }
308  }
309}
310
311/**
312 * Triggered on login_success
313 *
314 * Triggers scheduled tasks at login
315 * Redirects a visitor (except for admins, webmasters and generic statuses) to his profile.php page (Thx to LucMorizur)
316 *
317 */
318function UAM_LoginTasks()
319{
320  global $conf, $user;
321 
322  $conf_UAM = unserialize($conf['UserAdvManager']);
323 
324  // Performing GhostTracker scheduled tasks
325  if ((isset($conf_UAM[22]) and $conf_UAM[22] == 'true'))
326  {
327    UAM_GT_ScheduledTasks();
328  }
329
330  // Performing User validation scheduled tasks
331  if ((isset($conf_UAM[31]) and $conf_UAM[31] == 'true'))
332  {
333    UAM_USR_ScheduledTasks();
334  }
335
336  if ((isset($conf_UAM[21]) and $conf_UAM[21] == 'true'))
337  {
338    // Performing redirection 
339    $query ='
340SELECT user_id, status
341FROM '.USER_INFOS_TABLE.'
342WHERE user_id = '.$user['id'].'
343;';
344    $data = pwg_db_fetch_assoc(pwg_query($query));
345
346    if ($data['status'] <> "admin" and $data['status'] <> "webmaster" and $data['status'] <> "generic")
347    {
348      $user_idsOK = array();
349      if (!UAM_check_profile($user['id'], $user_idsOK))
350        redirect(PHPWG_ROOT_PATH.'profile.php');
351    }
352  }
353}
354
355
356/**
357 * Adds a new module settable in PWG_Stuffs - Triggered on get_stuffs_modules in main.inc.php
358 * Useful to inform unvalidated user for their status
359 *
360 */
361function register_UAM_stuffs_module($modules)
362{
363  array_push($modules, array(
364    'path' => UAM_PATH.'/stuffs_module',
365    'name' => l10n('UAM_Stuffs_Title'),
366    'description' => l10n('UAM_Stuffs_Desc'),
367    )
368  );
369  return $modules;
370}
371
372
373/**
374 * Triggered on UAM_LoginTasks()
375 *
376 * Executes optional post-login tasks for Ghost users
377 *
378 */
379function UAM_GT_ScheduledTasks()
380{
381  global $conf, $user, $page;
382
383  if (!defined('PHPWG_ROOT_PATH'))
384  {
385    die('Hacking attempt!');
386  }
387         
388  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
389
390  $conf_UAM = unserialize($conf['UserAdvManager']);
391 
392  $collection = array();
393  $reminder = false;
394 
395  $page['filtered_users'] = get_ghosts_autotasks();
396
397  foreach($page['filtered_users'] as $listed_user)
398  {
399    array_push($collection, $listed_user['id']);
400  }
401
402  // Ghost accounts auto group or status downgrade with or without information email sending and autodeletion if user already reminded
403  if ((isset($conf_UAM[22]) and $conf_UAM[22] == 'true') and ((isset($conf_UAM[26]) and $conf_UAM[26] <> -1) or (isset($conf_UAM[27]) and $conf_UAM[27] <> -1)))
404  {
405    if (count($collection) > 0)
406        {
407      // Process if a non-admin nor webmaster user is logged
408      if (in_array($user['id'], $collection))
409        {
410        // Check lastvisit reminder state
411        $query = '
412SELECT reminder
413FROM '.USER_LASTVISIT_TABLE.'
414WHERE user_id = '.$user['id'].';';
415
416        $result = pwg_db_fetch_assoc(pwg_query($query));
417
418        if (isset($result['reminder']) and $result['reminder'] == 'true')
419        {
420          $reminder = true;
421        }
422        else
423        {
424          $reminder = false;
425        }
426
427        // If user already reminded for ghost account
428        if ($reminder)
429        {
430          // delete account
431          delete_user($user['id']);
432
433          // Logged-in user cleanup, session destruction and redirected to custom page
434          invalidate_user_cache();
435          logout_user();
436          redirect(UAM_PATH.'GT_del_account.php');
437        }
438        }
439      else // Process if an admin or webmaster user is logged
440      {
441        foreach ($collection as $user_id)
442        {
443          // Check lastvisit reminder state
444          $query = '
445SELECT reminder
446FROM '.USER_LASTVISIT_TABLE.'
447WHERE user_id = '.$user_id.';';
448
449          $result = pwg_db_fetch_assoc(pwg_query($query));
450
451          if (isset($result['reminder']) and $result['reminder'] == 'true')
452          {
453            $reminder = true;
454          }
455          else
456          {
457            $reminder = false;
458          }
459
460          // If never reminded before
461          if (!$reminder)
462          {
463            // Reset of lastvisit date
464            list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
465
466                        $query = "
467UPDATE ".USER_LASTVISIT_TABLE."
468SET lastvisit='".$dbnow."'
469WHERE user_id = '".$user_id."'
470;";
471            pwg_query($query);
472
473          // Auto change group and / or status
474            // Delete user from all groups
475            $query = "
476DELETE FROM ".USER_GROUP_TABLE."
477WHERE user_id = '".$user_id."'
478  AND (
479    group_id = '".$conf_UAM[2]."'
480  OR
481    group_id = '".$conf_UAM[3]."'
482  )
483;";
484            pwg_query($query);
485
486            // Change user status
487            if ($conf_UAM[27] <> -1)
488            {
489              $query = "
490UPDATE ".USER_INFOS_TABLE."
491SET status = '".$conf_UAM[27]."'
492WHERE user_id = '".$user_id."'
493;";
494              pwg_query($query);
495            }
496
497            // Change user group
498            if ($conf_UAM[26] <> -1)
499            {
500              $query = "
501INSERT INTO ".USER_GROUP_TABLE."
502  (user_id, group_id)
503VALUES
504  ('".$user_id."', '".$conf_UAM[26]."')
505;";
506              pwg_query($query);
507            }
508
509            // Auto send email notification on group / status downgrade
510            if (isset($conf_UAM[23]) and $conf_UAM[23] == 'true')
511            {
512              // Set reminder true
513              $query = "
514UPDATE ".USER_LASTVISIT_TABLE."
515SET reminder = 'true'
516WHERE user_id = '".$user_id."'
517;";
518              pwg_query($query);
519           
520              // Reset confirmed user status to unvalidated
521                                                  $query = '
522UPDATE '.USER_CONFIRM_MAIL_TABLE.'
523SET date_check = NULL
524WHERE user_id = "'.$user_id.'"
525;';
526                                                  pwg_query($query);
527
528              // Get users information for email notification
529                                                  $query = '
530SELECT id, username, mail_address
531FROM '.USERS_TABLE.'
532WHERE id = '.$user_id.'
533;';
534                                                  $data = pwg_db_fetch_assoc(pwg_query($query));
535           
536              demotion_mail($user_id, $data['username'], $data['mail_address']);
537            }
538          }
539          elseif ($reminder) // If user already reminded for ghost account
540          {
541            // delete account
542            delete_user($user_id);
543          }
544        }
545      }
546    }
547  }
548}
549
550/**
551 * Triggered on UAM_LoginTasks()
552 *
553 * Executes optional post-login tasks for unvalidated users
554 *
555 */
556function UAM_USR_ScheduledTasks()
557{
558  global $conf, $user, $page;
559
560  if (!defined('PHPWG_ROOT_PATH'))
561  {
562    die('Hacking attempt!');
563  }
564         
565  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
566
567  $conf_UAM = unserialize($conf['UserAdvManager']);
568 
569  $collection = array();
570  $reminder = false;
571 
572  $page['filtered_users'] = get_unvalid_user_autotasks();
573
574  foreach($page['filtered_users'] as $listed_user)
575  {
576    array_push($collection, $listed_user['id']);
577  }
578
579  // Unvalidated accounts auto email sending and autodeletion if user already reminded
580  if ((isset($conf_UAM[31]) and $conf_UAM[31] == 'true'))
581  {
582    if (count($collection) > 0)
583        {
584      // Process if a non-admin nor webmaster user is logged
585      if (in_array($user['id'], $collection))
586        {
587        // Check ConfirmMail reminder state
588        $query = '
589SELECT reminder
590FROM '.USER_CONFIRM_MAIL_TABLE.'
591WHERE user_id = '.$user['id'].';';
592
593        $result = pwg_db_fetch_assoc(pwg_query($query));
594
595        if (isset($result['reminder']) and $result['reminder'] == 'true')
596        {
597          $reminder = true;
598        }
599        else
600        {
601          $reminder = false;
602        }
603
604        // If never reminded before, send reminder and set reminder True
605        if (!$reminder and isset($conf_UAM[33]) and $conf_UAM[33] == 'true')
606        {
607                  $typemail = 1;
608         
609          // Get current user informations
610          $query = "
611SELECT id, username, mail_address
612FROM ".USERS_TABLE."
613WHERE id = '".$user['id']."'
614;";
615          $data = pwg_db_fetch_assoc(pwg_query($query));
616
617          ResendMail2User($typemail,$user['id'],stripslashes($data['username']),$data['mail_address'],true);
618        }
619
620        // If already reminded before, delete user
621        if ($reminder)
622        {
623          // delete account
624          delete_user($user['id']);
625
626          // Logged-in user cleanup, session destruction and redirected to custom page
627          invalidate_user_cache();
628          logout_user();
629          redirect(UAM_PATH.'USR_del_account.php');
630        }
631        }
632      else // Process if an admin or webmaster user is logged
633      {
634        foreach ($collection as $user_id)
635        {
636          // Check reminder state
637          $query = '
638SELECT reminder
639FROM '.USER_CONFIRM_MAIL_TABLE.'
640WHERE user_id = '.$user_id.';';
641
642          $result = pwg_db_fetch_assoc(pwg_query($query));
643
644          if (isset($result['reminder']) and $result['reminder'] == 'true')
645          {
646            $reminder = true;
647          }
648          else
649          {
650            $reminder = false;
651          }
652
653          // If never reminded before, send reminder and set reminder True
654          if (!$reminder and isset($conf_UAM[33]) and $conf_UAM[33] == 'true')
655          {
656            $typemail = 1;
657         
658            // Get current user informations
659            $query = "
660SELECT id, username, mail_address
661FROM ".USERS_TABLE."
662WHERE id = '".$user_id."'
663;";
664            $data = pwg_db_fetch_assoc(pwg_query($query));
665
666            ResendMail2User($typemail,$user_id,stripslashes($data['username']),$data['mail_address'],true);
667          }
668          elseif ($reminder) // If user already reminded for account validation
669          {
670            // delete account
671            delete_user($user_id);
672          }
673        }
674      }
675    }
676  }
677}
678
679
680/**
681 * Triggered on init
682 *
683 * Check for forbidden email domains in admin's users management panel
684 */
685function UAM_InitPage()
686{
687  load_language('plugin.lang', UAM_PATH);
688  global $conf, $template, $page, $lang, $errors;
689
690  $conf_UAM = unserialize($conf['UserAdvManager']);
691
692// Admin user management
693  if (script_basename() == 'admin' and isset($_GET['page']) and $_GET['page'] == 'user_list')
694  {
695    if (isset($_POST['submit_add']))
696    {
697      // Email without forbidden domains
698      if (isset($conf_UAM[11]) and $conf_UAM[11] == 'true' and !empty($_POST['email']) and ValidateEmailProvider($_POST['email']))
699      {
700        $template->append('errors', l10n('UAM_reg_err_login5')."'".$conf_UAM[12]."'");
701        unset($_POST['submit_add']);
702      }
703    }
704  }
705}
706
707
708/**
709 * Triggered on render_lost_password_mail_content
710 *
711 * Adds a customized text in lost password email content
712 * Added text is inserted before users login name and new password
713 *
714 * @param : Standard Piwigo email content
715 *
716 * @return : Customized content added to standard content
717 *
718 */
719function UAM_lost_password_mail_content($infos)
720{
721  global $conf;
722 
723  load_language('plugin.lang', UAM_PATH);
724 
725  $conf_UAM = unserialize($conf['UserAdvManager']);
726 
727  if (isset($conf_UAM[29]) and $conf_UAM[29] == 'true')
728  {
729    // Management of Extension flags ([mygallery], [myurl])
730    //$patterns[] = '#\[username\]#i';
731    //$replacements[] = stripslashes($row['username']);
732    $patterns[] = '#\[mygallery\]#i';
733    $replacements[] = $conf['gallery_title'];
734    $patterns[] = '#\[myurl\]#i';
735    $replacements[] = $conf['gallery_url'];
736   
737    $infos = preg_replace($patterns, $replacements, $conf_UAM[30])."\n"."\n".$infos;
738  }
739  return $infos;
740}
741
742
743/**
744 * Triggered on user_comment_check
745 *
746 * checks if author is mandatory and set on comments post
747 *
748 * @param : comment action, comment
749 *
750 * @return : comment action
751 *
752 */
753function UAM_CheckEmptyCommentAuthor($comment_action, $comm)
754{
755  load_language('plugin.lang', UAM_PATH);
756  global $infos, $conf, $template;
757
758  $conf_UAM = unserialize($conf['UserAdvManager']);
759
760// User creation OR update
761  if (isset($conf_UAM[5]) and $conf_UAM[5] == 'true' and $conf['comments_forall'] == 'true' and $comm['author'] == 'guest')
762  {
763    $comment_action = 'reject';
764
765    array_push($infos, l10n('UAM_Empty Author'));
766  }
767
768  return $comment_action;
769}
770
771/**
772 * Function called from main.inc.php to send validation email
773 *
774 * @param : Type of email, user id, username, email address, confirmation (optional)
775 *
776 */
777function SendMail2User($typemail, $id, $username, $password, $email, $confirm)
778{
779  global $conf;
780
781  $conf_UAM = unserialize($conf['UserAdvManager']);
782 
783        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
784 
785        $infos1_perso = "";
786  $infos2_perso = "";
787
788// We have to get the user's language in database
789  $query ='
790SELECT user_id, language
791FROM '.USER_INFOS_TABLE.'
792WHERE user_id = '.$id.'
793;';
794  $data = pwg_db_fetch_assoc(pwg_query($query));
795
796// Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language
797  if (empty($data))
798  {
799// And switch gallery to this language before using personalized and multilangual contents
800    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
801    switch_lang_to($language);
802  }
803  else
804  {
805// And switch gallery to this language before using personalized and multilangual contents
806    $language = $data['language']; // Usefull for debugging
807    switch_lang_to($data['language']);
808    load_language('plugin.lang', UAM_PATH);
809  }
810
811  switch($typemail)
812  {
813    case 1:
814      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Add of %s', stripslashes($username)));
815      $password = $password <> '' ? $password : l10n('UAM_empty_pwd');
816     
817      if (isset($conf_UAM[9]) and $conf_UAM[9] <> '')
818      {
819        // Management of Extension flags ([username], [mygallery], [myurl])
820        $patterns[] = '#\[username\]#i';
821        $replacements[] = $username;
822        $patterns[] = '#\[mygallery\]#i';
823        $replacements[] = $conf['gallery_title'];
824        $patterns[] = '#\[myurl\]#i';
825        $replacements[] = $conf['gallery_url'];
826   
827        if (function_exists('get_user_language_desc'))
828        {
829          $infos1_perso = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[9]))."\n\n";
830        }
831        else $infos1_perso = l10n(preg_replace($patterns, $replacements, $conf_UAM[9]))."\n\n"; 
832      }
833     
834      break;
835     
836    case 2:
837      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Update of %s', stripslashes($username)));
838      $password = $password <> '' ? $password : l10n('UAM_empty_pwd');
839
840      break;
841       
842    case 3:
843      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Update of %s', stripslashes($username)));
844      $password = $password <> '' ? $password : l10n('UAM_no_update_pwd');
845
846      break;
847  }
848
849  if (isset($conf_UAM[0]) and $conf_UAM[0] == 'true')
850  {
851    $infos1 = array(
852      get_l10n_args('UAM_infos_mail %s', stripslashes($username)),
853      get_l10n_args('UAM_User: %s', stripslashes($username)),
854      get_l10n_args('UAM_Password: %s', $password),
855      get_l10n_args('Email: %s', $email),
856      get_l10n_args('', ''),
857    );
858  }
859
860
861  if ( isset($conf_UAM[1]) and $conf_UAM[1] == 'true' and $confirm)
862  {
863    $infos2 = array
864    (
865      get_l10n_args('UAM_Link: %s', AddConfirmMail($id, $email)),
866      get_l10n_args('', ''),
867    );
868
869    if (isset($conf_UAM[10]) and $conf_UAM[10] <> '')
870    {
871      // Management of Extension flags ([username], [mygallery], [myurl])
872      $patterns[] = '#\[username\]#i';
873      $replacements[] = $username;
874      $patterns[] = '#\[mygallery\]#i';
875      $replacements[] = $conf['gallery_title'];
876      $patterns[] = '#\[myurl\]#i';
877      $replacements[] = $conf['gallery_url'];
878     
879      if (function_exists('get_user_language_desc'))
880      {
881        $infos2_perso = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[10]))."\n\n";
882      }
883      else $infos2_perso = l10n(preg_replace($patterns, $replacements, $conf_UAM[10]))."\n\n";
884    }
885  }
886
887// ********************************************************
888// **** Pending code since to find how to make it work ****
889// ********************************************************
890// Testing if FCK Editor is used. Then decoding htmlchars to avoid problems with pwg_mail()
891/*$areas = array();
892array_push( $areas,'UAM_MailInfo_Text','UAM_ConfirmMail_Text');
893
894if (function_exists('set_fckeditor_instance'))
895{
896  $fcke_config = unserialize($conf['FCKEditor']);
897  foreach($areas as $area)
898  {
899    if (isset($fcke_config['UAM_MailInfo_Text']) and $fcke_config['UAM_MailInfo_Text'] = true)
900    {
901      $infos1_perso = html_entity_decode($infos1_perso,ENT_QUOTES,UTF-8);
902    }
903   
904    if (isset($fcke_config['UAM_ConfirmMail_Text']) and $fcke_config['UAM_ConfirmMail_Text'] = true)
905    {
906      $infos2_perso = html_entity_decode($infos2_perso,ENT_QUOTES,UTF-8);
907    }
908  }
909}*/
910
911
912// Sending the email with subject and contents
913  pwg_mail($email, array(
914    'subject' => $subject,
915    'content' => (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
916  ));
917
918// **********************
919// Email sending debugger
920// This is only to trace
921// the send of emails for
922// debugging             
923// **********************
924//$content = (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url();   
925//MailLog($email,$subject,$content,$language);
926// **********************
927
928// Switching back to default language
929switch_lang_back();
930}
931
932/**
933 * Function called from UAM_admin.php to resend validation email with or without new validation key
934 *
935 * @param : Type of email, user id, username, email address, confirmation (optional)
936 *
937 */
938function ResendMail2User($typemail, $user_id, $username, $email, $confirm)
939{
940  global $conf;
941
942  $conf_UAM = unserialize($conf['UserAdvManager']);
943
944  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
945 
946        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
947 
948// We have to get the user's language in database
949  $query ='
950SELECT user_id, language
951FROM '.USER_INFOS_TABLE.'
952WHERE user_id = '.$user_id.'
953;';
954  $data = pwg_db_fetch_assoc(pwg_query($query));
955  $language = $data['language'];
956 
957// And switch gallery to this language before using personalized and multilangual contents
958  switch_lang_to($data['language']);
959   
960  load_language('plugin.lang', UAM_PATH);
961
962  switch($typemail)
963  {
964    case 1:
965      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Reminder_with_key_of_%s', $username));
966     
967      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)
968      {
969        // Management of Extension flags ([username], [mygallery], [myurl])
970        $patterns[] = '#\[username\]#i';
971        $replacements[] = $username;
972        $patterns[] = '#\[mygallery\]#i';
973        $replacements[] = $conf['gallery_title'];
974        $patterns[] = '#\[myurl\]#i';
975        $replacements[] = $conf['gallery_url'];
976
977        if (function_exists('get_user_language_desc'))
978        {
979          $infos1 = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[2]))."\n\n";
980        }
981                                else $infos1 = l10n(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[2]))."\n\n";
982
983        $infos2 = array
984        (
985          get_l10n_args('UAM_Link: %s', ResetConfirmMail($user_id)),
986          get_l10n_args('', ''),
987        );       
988                        }
989
990// Set reminder true     
991      $query = "
992UPDATE ".USER_CONFIRM_MAIL_TABLE."
993SET reminder = 'true'
994WHERE user_id = '".$user_id."'
995;";
996      pwg_query($query);
997     
998                break;
999     
1000    case 2:
1001      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Reminder_without_key_of_%s',$username));
1002     
1003      if (isset($conf_UAM_ConfirmMail[4]) and $conf_UAM_ConfirmMail[4] <> '' and isset($conf_UAM_ConfirmMail[3]) and $conf_UAM_ConfirmMail[3] == 'true' and !$confirm)
1004      {
1005        // Management of Extension flags ([username], [mygallery], [myurl])
1006        $patterns[] = '#\[username\]#i';
1007        $replacements[] = $username;
1008        $patterns[] = '#\[mygallery\]#i';
1009        $replacements[] = $conf['gallery_title'];
1010        $patterns[] = '#\[myurl\]#i';
1011        $replacements[] = $conf['gallery_url'];
1012       
1013        if (function_exists('get_user_language_desc'))
1014        {
1015          $infos1 = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[4]))."\n\n";
1016        }
1017        else $infos1 = l10n(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[4]))."\n\n";
1018      }
1019     
1020// Set reminder true     
1021      $query = "
1022UPDATE ".USER_CONFIRM_MAIL_TABLE."
1023SET reminder = 'true'
1024WHERE user_id = '".$user_id."'
1025;";
1026      pwg_query($query);
1027     
1028    break;
1029        }
1030 
1031  pwg_mail($email, array(
1032    'subject' => $subject,
1033    'content' => ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
1034  ));
1035
1036// **********************
1037// Email sending debugger
1038// This is only to trace
1039// the send of emails for
1040// debugging             
1041// **********************
1042//$content = ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url();
1043//MailLog($email,$subject,$content,$language);
1044// **********************
1045
1046// Switching back to default language
1047switch_lang_back();
1048}
1049
1050/**
1051 * Function called from UAM_admin.php to send a reminder mail for ghost users
1052 *
1053 * @param : User id, username, email address
1054 *
1055 */
1056function ghostreminder($user_id, $username, $email)
1057{
1058  global $conf;
1059
1060  $conf_UAM = unserialize($conf['UserAdvManager']);
1061 
1062        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
1063 
1064        $infos1_perso = "";
1065
1066// We have to get the user's language in database
1067  $query ='
1068SELECT user_id, language
1069FROM '.USER_INFOS_TABLE.'
1070WHERE user_id = '.$user_id.'
1071;';
1072  $data = pwg_db_fetch_assoc(pwg_query($query));
1073  $language = $data['language'];
1074
1075// And switch gallery to this language before using personalized and multilangual contents
1076  switch_lang_to($data['language']);
1077   
1078  load_language('plugin.lang', UAM_PATH);
1079 
1080  $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Ghost_reminder_of_%s', $username));     
1081
1082  if (isset($conf_UAM[18]) and $conf_UAM[18] <> '' and isset($conf_UAM[16]) and $conf_UAM[16] == 'true')
1083  {
1084    // Management of Extension flags ([username], [mygallery], [myurl])
1085    $patterns[] = '#\[username\]#i';
1086    $replacements[] = $username;
1087    $patterns[] = '#\[mygallery\]#i';
1088    $replacements[] = $conf['gallery_title'];
1089    $patterns[] = '#\[myurl\]#i';
1090    $replacements[] = $conf['gallery_url'];
1091
1092    if (function_exists('get_user_language_desc'))
1093    {
1094      $infos1 = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[18]))."\n\n";
1095    }
1096    else
1097    {
1098      $infos1 = l10n(preg_replace($patterns, $replacements, $conf_UAM[18]))."\n\n";
1099    }
1100
1101    resetlastvisit($user_id);
1102  }
1103
1104  pwg_mail($email, array(
1105    'subject' => $subject,
1106    'content' => $infos1.get_absolute_root_url(),
1107  ));
1108
1109// **********************
1110// Email sending debugger
1111// This is only to trace
1112// the send of emails for
1113// debugging             
1114// **********************
1115//$content = $infos1.get_absolute_root_url(); 
1116//MailLog($email,$subject,$content,$language);
1117// **********************
1118
1119// Switching back to default language
1120switch_lang_back();
1121}
1122
1123/**
1124 * Function called from functions.inc.php to send notification email when user have been downgraded
1125 *
1126 * @param : user id, username, email address
1127 *
1128 */
1129function demotion_mail($id, $username, $email)
1130{
1131  global $conf;
1132
1133  $conf_UAM = unserialize($conf['UserAdvManager']);
1134 
1135        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
1136 
1137        $custom_txt = "";
1138
1139// We have to get the user's language in database
1140  $query ='
1141SELECT user_id, language
1142FROM '.USER_INFOS_TABLE.'
1143WHERE user_id = '.$id.'
1144;';
1145  $data = pwg_db_fetch_assoc(pwg_query($query));
1146
1147// Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language
1148  if (empty($data))
1149  {
1150// And switch gallery to this language before using personalized and multilangual contents
1151    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
1152    switch_lang_to($language);
1153  }
1154  else
1155  {
1156// And switch gallery to this language before using personalized and multilangual contents
1157    $language = $data['language']; // Usefull for debugging
1158    switch_lang_to($data['language']);
1159    load_language('plugin.lang', UAM_PATH);
1160  }
1161
1162  $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Demotion of %s', stripslashes($username)));
1163     
1164  if (isset($conf_UAM[25]) and $conf_UAM[25] <> '')
1165  {
1166    // Management of Extension flags ([username], [mygallery], [myurl])
1167    $patterns[] = '#\[username\]#i';
1168    $replacements[] = stripslashes($username);
1169    $patterns[] = '#\[mygallery\]#i';
1170    $replacements[] = $conf['gallery_title'];
1171    $patterns[] = '#\[myurl\]#i';
1172    $replacements[] = $conf['gallery_url'];
1173
1174    if (function_exists('get_user_language_desc'))
1175    {
1176      $custom_txt = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[25]))."\n\n";
1177    }
1178    else $custom_txt = l10n(preg_replace($patterns, $replacements, $conf_UAM[25]))."\n\n"; 
1179  }
1180
1181  $infos1 = array(
1182    get_l10n_args('UAM_User: %s', stripslashes($username)),
1183    get_l10n_args('Email: %s', $email),
1184    get_l10n_args('', ''),
1185  );
1186
1187  $infos2 = array
1188  (
1189    get_l10n_args('UAM_Link: %s', ResetConfirmMail($id)),
1190    get_l10n_args('', ''),
1191  ); 
1192
1193  resetlastvisit($id);
1194
1195// Sending the email with subject and contents
1196  pwg_mail($email, array(
1197    'subject' => $subject,
1198    'content' => ($custom_txt.l10n_args($infos1)."\n\n".l10n_args($infos2)."\n\n").get_absolute_root_url(),
1199  ));
1200
1201// **********************
1202// Email sending debugger
1203// This is only to trace
1204// the send of emails for
1205// debugging             
1206// **********************
1207//$content = ($custom_txt.l10n_args($infos1)."\n\n".l10n_args($infos2)."\n\n").get_absolute_root_url();   
1208//MailLog($email,$subject,$content,$language);
1209// **********************
1210
1211// Switching back to default language
1212switch_lang_back();
1213}
1214
1215/**
1216 * Function called from UAM_admin.php to send notification email when user registration have been manually validated by admin
1217 *
1218 * @param : user id
1219 *
1220 */
1221function validation_mail($id)
1222{
1223  global $conf;
1224
1225  $conf_UAM = unserialize($conf['UserAdvManager']);
1226 
1227        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
1228 
1229        $custom_txt = "";
1230
1231// We have to get the user's language in database
1232  $query ='
1233SELECT user_id, language
1234FROM '.USER_INFOS_TABLE.'
1235WHERE user_id = '.$id.'
1236;';
1237  $data = pwg_db_fetch_assoc(pwg_query($query));
1238
1239// Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language
1240  if (empty($data))
1241  {
1242// And switch gallery to this language before using personalized and multilangual contents
1243    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
1244    switch_lang_to($language);
1245  }
1246  else
1247  {
1248// And switch gallery to this language before using personalized and multilangual contents
1249    $language = $data['language']; // Usefull for debugging
1250    switch_lang_to($data['language']);
1251    load_language('plugin.lang', UAM_PATH);
1252  }
1253
1254// Retreive users email and user name from id
1255  $query ='
1256SELECT id, username, mail_address
1257FROM '.USERS_TABLE.'
1258WHERE id = '.$id.'
1259;';
1260  $result = pwg_db_fetch_assoc(pwg_query($query));
1261
1262  $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('UAM_Validation of %s', stripslashes($result['username'])));
1263     
1264  if (isset($conf_UAM[28]) and $conf_UAM[28] <> '')
1265  {
1266    // Management of Extension flags ([username], [mygallery], [myurl])
1267    $patterns[] = '#\[username\]#i';
1268    $replacements[] = $result['username'];
1269    $patterns[] = '#\[mygallery\]#i';
1270    $replacements[] = $conf['gallery_title'];
1271    $patterns[] = '#\[myurl\]#i';
1272    $replacements[] = $conf['gallery_url'];
1273
1274    if (function_exists('get_user_language_desc'))
1275    {
1276      $custom_txt = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[28]))."\n\n";
1277    }
1278    else $custom_txt = l10n(preg_replace($patterns, $replacements, $conf_UAM[28]))."\n\n"; 
1279  }
1280
1281  $infos = array(
1282    get_l10n_args('UAM_User: %s', stripslashes($result['username'])),
1283    get_l10n_args('Email: %s', $result['mail_address']),
1284    get_l10n_args('', ''),
1285  );
1286
1287// Sending the email with subject and contents
1288  pwg_mail($result['mail_address'], array(
1289    'subject' => $subject,
1290    'content' => (l10n_args($infos)."\n\n".$custom_txt),
1291  ));
1292
1293// **********************
1294// Email sending debugger
1295// This is only to trace
1296// the send of emails for
1297// debugging             
1298// **********************
1299//$email = $result['mail_address'];
1300//$content = (l10n_args($infos)."\n\n".$custom_txt);   
1301//MailLog($email,$subject,$content,$language);
1302// **********************
1303
1304// Switching back to default language
1305switch_lang_back();
1306}
1307
1308/**
1309 * Function called from functions AddConfirmMail and ResetConfirmMail for validation key generation
1310 *
1311 * @return : validation key
1312 *
1313 */
1314function FindAvailableConfirmMailID()
1315{
1316  while (true)
1317  {
1318    $id = generate_key(16);
1319    $query = "
1320SELECT COUNT(*)
1321  FROM ".USER_CONFIRM_MAIL_TABLE."
1322WHERE id = '".$id."'
1323;";
1324    list($count) = pwg_db_fetch_row(pwg_query($query));
1325
1326    if ($count == 0)
1327      return $id;
1328  }
1329}
1330
1331/**
1332 * Function called from functions SendMail2User to process unvalidated users and generate validation key link
1333 *
1334 * @param : User id, email address
1335 *
1336 * @return : Build validation key in URL
1337 *
1338 */
1339function AddConfirmMail($user_id, $email)
1340{
1341  global $conf;
1342
1343  $conf_UAM = unserialize($conf['UserAdvManager']);
1344  $Confirm_Mail_ID = FindAvailableConfirmMailID();
1345
1346  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
1347 
1348  if (isset($Confirm_Mail_ID))
1349  {
1350    $query = "
1351SELECT status
1352  FROM ".USER_INFOS_TABLE."
1353WHERE user_id = '".$user_id."'
1354;";
1355    list($status) = pwg_db_fetch_row(pwg_query($query));
1356   
1357    $query = "
1358INSERT INTO ".USER_CONFIRM_MAIL_TABLE."
1359  (id, user_id, mail_address, status, date_check)
1360VALUES
1361  ('".$Confirm_Mail_ID."', '".$user_id."', '".$email."', '".$status."', null)
1362;";
1363    pwg_query($query);
1364
1365    // Delete user from all groups
1366    $query = "
1367DELETE FROM ".USER_GROUP_TABLE."
1368WHERE user_id = '".$user_id."'
1369  AND (
1370    group_id = '".$conf_UAM[2]."'
1371  OR
1372    group_id = '".$conf_UAM[3]."'
1373  )
1374;";
1375    pwg_query($query);
1376
1377    // Set user unvalidated status
1378    if (!is_admin() and $conf_UAM[8] <> -1)
1379    {
1380      $query = "
1381UPDATE ".USER_INFOS_TABLE."
1382SET status = '".$conf_UAM[8]."'
1383WHERE user_id = '".$user_id."'
1384;";
1385      pwg_query($query);
1386    }
1387
1388    // Set user unvalidated group
1389    if (!is_admin() and $conf_UAM[2] <> -1)
1390    {
1391      $query = "
1392INSERT INTO ".USER_GROUP_TABLE."
1393  (user_id, group_id)
1394VALUES
1395  ('".$user_id."', '".$conf_UAM[2]."')
1396;";
1397      pwg_query($query);
1398    }
1399   
1400    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
1401  }
1402}
1403
1404/**
1405 * Function called from main.inc.php to set group to new users if manual validation is set
1406 *
1407 * @param : User id
1408 *
1409 *
1410 */
1411function setgroup($user_id)
1412{
1413  global $conf;
1414 
1415  $conf_UAM = unserialize($conf['UserAdvManager']);
1416
1417  $query = "
1418DELETE FROM ".USER_GROUP_TABLE."
1419WHERE user_id = '".$user_id."'
1420  AND (
1421    group_id = '".$conf_UAM[2]."'
1422  OR
1423    group_id = '".$conf_UAM[3]."'
1424  )
1425;";
1426  pwg_query($query);
1427
1428  if (!is_admin() and $conf_UAM[8] <> -1)
1429  {
1430    $query = "
1431UPDATE ".USER_INFOS_TABLE."
1432SET status = '".$conf_UAM[8]."'
1433WHERE user_id = '".$user_id."'
1434;";
1435    pwg_query($query);
1436  }
1437
1438  if (!is_admin() and $conf_UAM[2] <> -1)
1439  {
1440    $query = "
1441INSERT INTO ".USER_GROUP_TABLE."
1442  (user_id, group_id)
1443VALUES
1444  ('".$user_id."', '".$conf_UAM[2]."')
1445;";
1446    pwg_query($query);
1447  }
1448}
1449
1450/**
1451 * Function called from UAM_admin.php to reset validation key
1452 *
1453 * @param : User id
1454 *
1455 * @return : Build validation key in URL
1456 *
1457 */
1458function ResetConfirmMail($user_id)
1459{
1460  global $conf;
1461 
1462  $Confirm_Mail_ID = FindAvailableConfirmMailID();
1463
1464  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
1465 
1466  if (isset($Confirm_Mail_ID))
1467  { 
1468    $query = "
1469UPDATE ".USER_CONFIRM_MAIL_TABLE."
1470SET id = '".$Confirm_Mail_ID."'
1471WHERE user_id = '".$user_id."'
1472;";
1473    pwg_query($query);
1474
1475                $query = "
1476UPDATE ".USER_INFOS_TABLE."
1477SET registration_date = '".$dbnow."'
1478WHERE user_id = '".$user_id."'
1479;";
1480                pwg_query($query);
1481   
1482    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
1483  }
1484}
1485
1486/**
1487 * Function called from functions.inc.php to reset last visit date after sending a reminder
1488 *
1489 * @param : User id
1490 *
1491 */
1492function resetlastvisit($user_id)
1493{
1494  global $conf;
1495
1496  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
1497
1498  $query = "
1499UPDATE ".USER_LASTVISIT_TABLE."
1500SET lastvisit = '".$dbnow."', reminder = 'true'
1501WHERE user_id = '".$user_id."'
1502;";
1503  pwg_query($query);
1504}
1505
1506
1507/**
1508 * Function called from main.inc.php - Triggered on user deletion
1509 *
1510 */
1511function DeleteConfirmMail($user_id)
1512{
1513  $query = "
1514DELETE FROM ".USER_CONFIRM_MAIL_TABLE."
1515WHERE user_id = '".$user_id."'
1516;";
1517  pwg_query($query);
1518}
1519
1520/**
1521 * Function called from main.inc.php - Triggered on user deletion
1522 *
1523 */
1524function DeleteLastVisit($user_id)
1525{
1526  $query = "
1527DELETE FROM ".USER_LASTVISIT_TABLE."
1528WHERE user_id = '".$user_id."'
1529;";
1530  pwg_query($query);
1531}
1532
1533/**
1534 * Function called from main.inc.php - Triggered on user deletion
1535 *
1536 * @param : User id
1537 *
1538 */
1539function DeleteRedir($user_id)
1540{
1541  $tab = array();
1542
1543  $query = '
1544SELECT value
1545FROM '.CONFIG_TABLE.'
1546WHERE param = "UserAdvManager_Redir"
1547;';
1548
1549  $tab = pwg_db_fetch_row(pwg_query($query));
1550 
1551  $values = explode(',', $tab[0]);
1552
1553  unset($values[array_search($user_id, $values)]);
1554     
1555  $query = "
1556UPDATE ".CONFIG_TABLE."
1557SET value = \"".implode(',', $values)."\"
1558WHERE param = 'UserAdvManager_Redir';";
1559
1560  pwg_query($query);
1561}
1562
1563/**
1564 * Function called from ConfirmMail.php to verify validation key used by user according time limit
1565 * Return true is key validation is OK else return false
1566 *
1567 * @param : User id
1568 *
1569 * @return : Bool
1570 *
1571 */
1572function VerifyConfirmMail($id)
1573{
1574  global $conf;
1575
1576  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
1577 
1578  $conf_UAM = unserialize($conf['UserAdvManager']);
1579
1580  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
1581
1582  $query = "
1583SELECT COUNT(*)
1584FROM ".USER_CONFIRM_MAIL_TABLE."
1585WHERE id = '".$id."'
1586;";
1587  list($count) = pwg_db_fetch_row(pwg_query($query));
1588
1589  if ($count == 1)
1590  {
1591    $query = "
1592SELECT user_id, status, date_check
1593FROM ".USER_CONFIRM_MAIL_TABLE."
1594WHERE id = '".$id."'
1595;";
1596    $data = pwg_db_fetch_assoc(pwg_query($query));
1597
1598    if (!empty($data) and isset($data['user_id']))
1599    {
1600      $query = "
1601SELECT registration_date
1602FROM ".USER_INFOS_TABLE."
1603WHERE user_id = '".$data['user_id']."'
1604;";
1605      list($registration_date) = pwg_db_fetch_row(pwg_query($query));
1606
1607//              Time limit process             
1608// ******************************************** 
1609      if (!empty($registration_date))
1610      {
1611                                // Verify Confirmmail with time limit ON
1612                                if (isset ($conf_UAM_ConfirmMail[1]))
1613                                {
1614                                        // dates formating and compare
1615                                        $today = date("d-m-Y"); // Get today's date
1616                                        list($day, $month, $year) = explode('-', $today); // explode date of today                                               
1617                                        $daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
1618                       
1619                                list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
1620                                        list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
1621                                        $regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
1622                       
1623                                        $deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps     
1624                                        $deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
1625
1626                                        // Condition with the value set for time limit
1627                                        if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
1628                                        {
1629                                                list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
1630
1631                                                $query = '
1632UPDATE '.USER_CONFIRM_MAIL_TABLE.'
1633SET date_check="'.$dbnow.'", reminder="false"
1634WHERE id = "'.$id.'"
1635;';
1636                                                pwg_query($query);
1637     
1638                                                if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
1639                                                {
1640                                                        $query = "
1641DELETE FROM ".USER_GROUP_TABLE."
1642WHERE user_id = '".$data['user_id']."'
1643  AND group_id = '".$conf_UAM[2]."'
1644;";
1645                                                        pwg_query($query);
1646                                                }
1647           
1648                                                if ($conf_UAM[3] <> -1) // Add user to validated users group
1649                                                {
1650                                                        $query = "
1651INSERT INTO ".USER_GROUP_TABLE."
1652  (user_id, group_id)
1653VALUES
1654  ('".$data['user_id']."', '".$conf_UAM[3]."')
1655;";
1656                                                        pwg_query($query);
1657                                                }
1658
1659                                                if (($conf_UAM[4] <> -1 or isset($data['status']))) // Change user's status
1660                                                {
1661                                                        $query = "
1662UPDATE ".USER_INFOS_TABLE."
1663SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
1664WHERE user_id = '".$data['user_id']."'
1665;";
1666                                                        pwg_query($query);
1667                                                }
1668                                                // Refresh user's category cache
1669                                                invalidate_user_cache();
1670 
1671                                                return true;
1672                                        }
1673                                        elseif ($deltadays > $conf_UAM_ConfirmMail[1]) // If timelimit exeeds
1674                                        {
1675                                                return false;
1676                                        }
1677                                }
1678                                // Verify Confirmmail with time limit OFF
1679                                else
1680                                {
1681                                        list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
1682
1683                                        $query = '
1684UPDATE '.USER_CONFIRM_MAIL_TABLE.'
1685SET date_check="'.$dbnow.'"
1686WHERE id = "'.$id.'"
1687;';
1688                                        pwg_query($query);
1689     
1690                                        if ($conf_UAM[2] <> -1)
1691                                        {
1692                                                $query = "
1693DELETE FROM ".USER_GROUP_TABLE."
1694WHERE user_id = '".$data['user_id']."'
1695AND group_id = '".$conf_UAM[2]."'
1696;";
1697                                                pwg_query($query);
1698                                        }
1699   
1700                                        if ($conf_UAM[3] <> -1)
1701                                        {
1702                                                $query = "
1703DELETE FROM ".USER_GROUP_TABLE."
1704WHERE user_id = '".$data['user_id']."'
1705AND group_id = '".$conf_UAM[3]."'
1706;";
1707                                                pwg_query($query);
1708
1709                                                $query = "
1710INSERT INTO ".USER_GROUP_TABLE."
1711  (user_id, group_id)
1712VALUES
1713  ('".$data['user_id']."', '".$conf_UAM[3]."')
1714;";
1715                                                pwg_query($query);
1716                                        }
1717
1718                                        if (($conf_UAM[4] <> -1 or isset($data['status'])))
1719                                        {
1720                                                $query = "
1721UPDATE ".USER_INFOS_TABLE."
1722SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
1723WHERE user_id = '".$data['user_id']."'
1724;";
1725                                                pwg_query($query);
1726                                        }
1727                                        // Refresh user's category cache
1728                                        invalidate_user_cache();
1729 
1730                                        return true;
1731                                }
1732                        }
1733                }
1734        }
1735  else
1736    return false;
1737}
1738
1739/**
1740 * Function called from UAM_admin.php to force users validation by admin
1741 *
1742 * @param : User id
1743 *
1744 */
1745function ForceValidation($id)
1746{
1747  global $conf;
1748
1749  $conf_UAM = unserialize($conf['UserAdvManager']);
1750
1751  if (isset($conf_UAM[1]) and $conf_UAM[1] == 'true')
1752  {
1753    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
1754
1755                $query = "
1756UPDATE ".USER_CONFIRM_MAIL_TABLE."
1757SET date_check='".$dbnow."'
1758WHERE user_id = '".$id."'
1759;";
1760    pwg_query($query);
1761             
1762                if ($conf_UAM[2] <> -1)
1763                {
1764                        $query = "
1765DELETE FROM ".USER_GROUP_TABLE."
1766WHERE user_id = '".$id."'
1767  AND group_id = '".$conf_UAM[2]."'
1768;";
1769                        pwg_query($query);
1770                }
1771 
1772                if ($conf_UAM[3] <> -1)
1773                {
1774                        $query = "
1775DELETE FROM ".USER_GROUP_TABLE."
1776WHERE user_id = '".$id."'
1777  AND group_id = '".$conf_UAM[3]."'
1778;";
1779      pwg_query($query);
1780       
1781                        $query = "
1782INSERT INTO ".USER_GROUP_TABLE."
1783  (user_id, group_id)
1784VALUES
1785  ('".$id."', '".$conf_UAM[3]."')
1786;";
1787                        pwg_query($query);
1788    }
1789
1790                if ($conf_UAM[4] <> -1)
1791                {
1792                        $query = "
1793UPDATE ".USER_INFOS_TABLE."
1794SET status = '".$conf_UAM[4]."'
1795WHERE user_id = '".$id."'
1796;";
1797                        pwg_query($query);
1798                }
1799  }
1800  elseif (isset($conf_UAM[1]) and $conf_UAM[1] == 'local')
1801  {
1802    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
1803
1804    if ($conf_UAM[2] <> -1)
1805    {
1806                  $query = "
1807DELETE FROM ".USER_GROUP_TABLE."
1808WHERE user_id = '".$id."'
1809  AND group_id = '".$conf_UAM[2]."'
1810;";
1811                  pwg_query($query);
1812    }
1813
1814    if ($conf_UAM[3] <> -1)
1815    {
1816      $query = "
1817DELETE FROM ".USER_GROUP_TABLE."
1818WHERE user_id = '".$id."'
1819  AND group_id = '".$conf_UAM[3]."'
1820;";
1821      pwg_query($query);
1822       
1823                  $query = "
1824INSERT INTO ".USER_GROUP_TABLE."
1825  (user_id, group_id)
1826VALUES
1827  ('".$id."', '".$conf_UAM[3]."')
1828;";
1829                  pwg_query($query);
1830    }
1831
1832    if ($conf_UAM[4] <> -1)
1833    {
1834                  $query = "
1835UPDATE ".USER_INFOS_TABLE."
1836SET status = '".$conf_UAM[4]."'
1837WHERE user_id = '".$id."'
1838;";
1839      pwg_query($query);
1840    }
1841  }
1842}
1843
1844/**
1845 * Function called from main.inc.php - Check if username matches forbidden caracters
1846 *
1847 * @param : User login
1848 *
1849 * @return : Bool
1850 *
1851 */
1852function ValidateUsername($login)
1853{
1854  global $conf;
1855
1856  $conf_UAM = unserialize($conf['UserAdvManager']);
1857
1858  if (isset($login) and isset($conf_UAM[7]) and $conf_UAM[7] <> '')
1859  {
1860    $conf_CharExclusion = preg_split("/,/",$conf_UAM[7]);
1861    for ($i = 0 ; $i < count($conf_CharExclusion) ; $i++)
1862    {
1863      $pattern = '/'.$conf_CharExclusion[$i].'/i';
1864      if (preg_match($pattern, $login))
1865      {
1866        return true;
1867      }
1868    }
1869  }
1870  else
1871  {
1872    return false;
1873  }
1874}
1875
1876/**
1877 * Function called from main.inc.php - Check if user's email is in excluded email providers list
1878 * Doesn't work on call - Must be copied in main.inc.php to work
1879 *
1880 * @param : Email address
1881 *
1882 * @return : Bool
1883 *
1884 */
1885function ValidateEmailProvider($email)
1886{
1887  global $conf;
1888
1889  $conf_UAM = unserialize($conf['UserAdvManager']);
1890 
1891        if (isset($email) and isset($conf_UAM[12]) and $conf_UAM[12] <> '')
1892        {
1893                $conf_MailExclusion = preg_split("/[\s,]+/",$conf_UAM[12]);
1894                for ($i = 0 ; $i < count($conf_MailExclusion) ; $i++)
1895                {
1896                        $pattern = '/'.$conf_MailExclusion[$i].'/i';
1897                        if (preg_match($pattern, $email))
1898      {
1899                        return true;
1900      }
1901                }
1902        }
1903  else
1904  {
1905    return false;
1906  }
1907}
1908
1909/**
1910 * Function called from UAM_admin.php - Get unvalidated users according time limit
1911 *
1912 * @return : List of users
1913 *
1914 */
1915function get_unvalid_user_list()
1916{
1917        global $conf, $page;
1918         
1919        // Get ConfirmMail configuration
1920  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
1921  // Get UAM configuration
1922  $conf_UAM = unserialize($conf['UserAdvManager']);
1923 
1924  $users = array();
1925
1926        // search users depending expiration date
1927  $query = '
1928SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
1929                u.'.$conf['user_fields']['username'].' AS username,
1930                u.'.$conf['user_fields']['email'].' AS email,
1931                ui.status,
1932                ui.enabled_high,
1933                ui.level,
1934                ui.registration_date
1935FROM '.USERS_TABLE.' AS u
1936  INNER JOIN '.USER_INFOS_TABLE.' AS ui
1937    ON u.'.$conf['user_fields']['id'].' = ui.user_id
1938  LEFT JOIN '.USER_GROUP_TABLE.' AS ug
1939    ON u.'.$conf['user_fields']['id'].' = ug.user_id
1940WHERE u.'.$conf['user_fields']['id'].' >= 3
1941  AND (TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) >= "'.$conf_UAM_ConfirmMail[1].'"
1942  OR TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) < "'.$conf_UAM_ConfirmMail[1].'")';
1943
1944        if ($conf_UAM[2] <> '-1' and $conf_UAM[8] == '-1')
1945  {
1946    $query.= '
1947  AND ug.group_id = '.$conf_UAM[2];
1948  }
1949  if ($conf_UAM[2] == '-1' and $conf_UAM[8] <> '-1')
1950  {
1951    $query.= '
1952  AND ui.status = \''.$conf_UAM[8]."'";
1953  }
1954  if ($conf_UAM[2] <> '-1' and $conf_UAM[8] <> '-1')
1955  {
1956    $query.= '
1957  AND ug.group_id = \''.$conf_UAM[2]."'";
1958  }
1959  $query.= '
1960ORDER BY ui.registration_date ASC
1961;';
1962
1963        $result = pwg_query($query);
1964     
1965  while ($row = pwg_db_fetch_assoc($result))
1966  {
1967        $user = $row;
1968    $user['groups'] = array();
1969
1970    array_push($users, $user);
1971        }
1972
1973        // add group lists
1974  $user_ids = array();
1975  foreach ($users as $i => $user)
1976  {
1977        $user_ids[$i] = $user['id'];
1978        }
1979       
1980        $user_nums = array_flip($user_ids);
1981
1982  if (count($user_ids) > 0)
1983  {
1984        $query = '
1985SELECT user_id, group_id
1986  FROM '.USER_GROUP_TABLE.'
1987WHERE user_id IN ('.implode(',', $user_ids).')
1988;';
1989       
1990                $result = pwg_query($query);
1991       
1992    while ($row = pwg_db_fetch_assoc($result))
1993    {
1994        array_push(
1995        $users[$user_nums[$row['user_id']]]['groups'],
1996        $row['group_id']
1997                        );
1998                }
1999        }
2000
2001        return $users;
2002}
2003
2004/**
2005 * Function called from functions.inc.php - Get all users who haven't validate their registration in configured time
2006 * to delete or remail them automatically
2007 *
2008 * @return : List of users
2009 *
2010 */
2011function get_unvalid_user_autotasks()
2012{
2013        global $conf, $page;
2014         
2015        // Get ConfirmMail configuration
2016  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
2017 
2018  $users = array();
2019
2020        // search users depending expiration date
2021  $query = '
2022SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2023                ui.registration_date
2024FROM '.USERS_TABLE.' AS u
2025  INNER JOIN '.USER_INFOS_TABLE.' AS ui
2026    ON u.'.$conf['user_fields']['id'].' = ui.user_id
2027WHERE u.'.$conf['user_fields']['id'].' >= 3
2028  AND (TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) >= "'.$conf_UAM_ConfirmMail[1].'")
2029ORDER BY ui.registration_date ASC;';
2030
2031        $result = pwg_query($query);
2032     
2033  while ($row = pwg_db_fetch_assoc($result))
2034  {
2035    array_push($users, $row);
2036        }
2037
2038        return $users;
2039}
2040
2041/**
2042 * Function called from UAM_admin.php - Get ghost users
2043 *
2044 * @return : List of users
2045 *
2046 */
2047function get_ghost_user_list()
2048{
2049        global $conf, $page;
2050
2051  $conf_UAM = unserialize($conf['UserAdvManager']);
2052 
2053  $users = array();
2054
2055        // search users depending expiration date
2056  $query = '
2057SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2058                u.'.$conf['user_fields']['username'].' AS username,
2059                u.'.$conf['user_fields']['email'].' AS email,
2060                lv.lastvisit,
2061                lv.reminder
2062FROM '.USERS_TABLE.' AS u
2063  INNER JOIN '.USER_LASTVISIT_TABLE.' AS lv
2064    ON u.'.$conf['user_fields']['id'].' = lv.user_id
2065WHERE (TO_DAYS(NOW()) - TO_DAYS(lv.lastvisit) >= "'.$conf_UAM[17].'")
2066ORDER BY lv.lastvisit ASC;';
2067
2068        $result = pwg_query($query);
2069     
2070  while ($row = pwg_db_fetch_assoc($result))
2071  {
2072        $user = $row;
2073    $user['groups'] = array();
2074
2075    array_push($users, $user);
2076        }
2077
2078        // add group lists
2079  $user_ids = array();
2080  foreach ($users as $i => $user)
2081  {
2082        $user_ids[$i] = $user['id'];
2083        }
2084
2085        return $users;
2086}
2087
2088
2089/**
2090 * Function called from functions.inc.php - Get all ghost users to delete or downgrade automatically on any user login
2091 *
2092 * @return : List of users to delete or downgrade automatically
2093 *
2094 */
2095function get_ghosts_autotasks()
2096{
2097        global $conf, $page;
2098
2099  $conf_UAM = unserialize($conf['UserAdvManager']);
2100 
2101  $users = array();
2102 
2103        // search users depending expiration date and reminder sent
2104  $query = '
2105SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2106                lv.lastvisit
2107FROM '.USERS_TABLE.' AS u
2108  INNER JOIN '.USER_LASTVISIT_TABLE.' AS lv
2109    ON u.'.$conf['user_fields']['id'].' = lv.user_id
2110WHERE (TO_DAYS(NOW()) - TO_DAYS(lv.lastvisit) >= "'.$conf_UAM[17].'")
2111ORDER BY lv.lastvisit ASC;';
2112
2113        $result = pwg_query($query);
2114     
2115  while ($row = pwg_db_fetch_assoc($result))
2116  {
2117    array_push($users, $row);
2118        }
2119 
2120        return $users;
2121}
2122
2123
2124/**
2125 * Function called from UAM_admin.php - Get all users to display the number of days since their last visit
2126 *
2127 * @return : List of users
2128 *
2129 */
2130function get_user_list()
2131{
2132        global $conf, $page;
2133 
2134  $users = array();
2135
2136        // search users depending expiration date
2137  $query = '
2138SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2139                u.'.$conf['user_fields']['username'].' AS username,
2140                u.'.$conf['user_fields']['email'].' AS email,
2141                ug.lastvisit
2142FROM '.USERS_TABLE.' AS u
2143  INNER JOIN '.USER_LASTVISIT_TABLE.' AS ug
2144    ON u.'.$conf['user_fields']['id'].' = ug.user_id
2145WHERE u.'.$conf['user_fields']['id'].' >= 3
2146ORDER BY ug.lastvisit DESC
2147;';
2148
2149        $result = pwg_query($query);
2150     
2151  while ($row = pwg_db_fetch_assoc($result))
2152  {
2153        $user = $row;
2154    $user['groups'] = array();
2155
2156    array_push($users, $user);
2157        }
2158
2159        // add group lists
2160  $user_ids = array();
2161  foreach ($users as $i => $user)
2162  {
2163        $user_ids[$i] = $user['id'];
2164        }
2165
2166        return $users;
2167}
2168
2169/**
2170 * Function called from UAM_admin.php - to determine who is expired or not and giving a different display color
2171 *
2172 * @param : user id
2173 *
2174 * @return : Bool
2175 *
2176 */
2177function expiration($id)
2178{
2179        global $conf, $page;
2180         
2181        // Get ConfirmMail configuration
2182  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
2183         
2184        // Get UAM configuration
2185  $conf_UAM = unserialize($conf['UserAdvManager']);
2186       
2187        $query = "
2188SELECT registration_date
2189  FROM ".USER_INFOS_TABLE."
2190WHERE user_id = '".$id."'
2191;";
2192        list($registration_date) = pwg_db_fetch_row(pwg_query($query));
2193
2194//              Time limit process             
2195// ******************************************** 
2196        if (!empty($registration_date))
2197  {
2198                // dates formating and compare
2199                $today = date("d-m-Y"); // Get today's date
2200                list($day, $month, $year) = explode('-', $today); // explode date of today                                               
2201                $daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
2202               
2203          list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
2204                list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
2205                $regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
2206                       
2207                $deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps     
2208                $deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
2209
2210                // Condition with the value set for time limit
2211                if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
2212                {
2213                        return false;
2214                }
2215                else
2216                {
2217                        return true;
2218                }
2219        }
2220}
2221
2222
2223/**
2224 * Returns a password's score for password complexity check
2225 *
2226 * @param : password filled by user
2227 *
2228 * @return : Score calculation
2229 *
2230 * Thanx to MathieuGut from http://m-gut.developpez.com
2231 */
2232function testpassword($password) // Le mot de passe passé en paramètre - $password given by user
2233{
2234
2235  // Initialisation des variables - Variables initiation
2236  $points = 0;
2237  $point_lowercase = 0;
2238  $point_uppercase = 0;
2239  $point_numbers = 0;
2240  $point_characters = 0;
2241
2242  // On récupère la longueur du mot de passe - Getting password lengh   
2243  $length = strlen($password);
2244 
2245  // On fait une boucle pour lire chaque lettre - Loop to read password characters
2246  for($i = 0; $i < $length; $i++)
2247  {
2248    // On sélectionne une à une chaque lettre - Select each letters
2249    // $i étant à 0 lors du premier passage de la boucle - $i is 0 at first turn
2250    $letters = $password[$i];
2251
2252    if ($letters>='a' && $letters<='z')
2253    {
2254      // On ajoute 1 point pour une minuscule - Adding 1 point to score for a lowercase
2255                  $points = $points + 1;
2256
2257                  // On rajoute le bonus pour une minuscule - Adding bonus points for lowercase
2258                  $point_lowercase = 1;
2259    }
2260    else if ($letters>='A' && $letters <='Z')
2261    {
2262      // On ajoute 2 points pour une majuscule - Adding 2 points to score for uppercase
2263      $points = $points + 2;
2264               
2265      // On rajoute le bonus pour une majuscule - Adding bonus points for uppercase
2266      $point_uppercase = 2;
2267    }
2268    else if ($letters>='0' && $letters<='9')
2269    {
2270      // On ajoute 3 points pour un chiffre - Adding 3 points to score for numbers
2271      $points = $points + 3;
2272               
2273      // On rajoute le bonus pour un chiffre - Adding bonus points for numbers
2274      $point_numbers = 3;
2275    }
2276    else
2277    {
2278      // On ajoute 5 points pour un caractère autre - Adding 5 points to score for special characters
2279      $points = $points + 5;
2280               
2281      // On rajoute le bonus pour un caractère autre - Adding bonus points for special characters
2282      $point_characters = 5;
2283    }
2284  }
2285
2286  // Calcul du coefficient points/longueur - calculating the coefficient points/length
2287  $step1 = $points / $length;
2288
2289  // Calcul du coefficient de la diversité des types de caractères... - Calculation of the diversity of character types...
2290  $step2 = $point_lowercase + $point_uppercase + $point_numbers + $point_characters;
2291
2292  // Multiplication du coefficient de diversité avec celui de la longueur - Multiplying the coefficient of diversity with that of the length
2293  $score = $step1 * $step2;
2294
2295  // Multiplication du resultat par la longueur de la chaine - Multiplying the result by the length of the string
2296  $finalscore = $score * $length;
2297
2298  return $finalscore;
2299}
2300
2301/**
2302 * Function called from maintain.inc.php - to check if database upgrade is needed
2303 *
2304 * @param : table name
2305 *
2306 * @return : boolean
2307 *
2308 */
2309function table_exist($table)
2310{
2311  $query = 'DESC '.$table.';';
2312  return (bool)($res=pwg_query($query));
2313}
2314
2315// Email sending debugger function
2316function MailLog($to, $subject, $content, $language)
2317{
2318   $fo=fopen (UAM_PATH.'admin/maillog.txt','a') ;
2319   fwrite($fo,"======================\n") ;
2320   fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n");
2321   fwrite($fo,$to . "\n" . $subject . "\r\n") ;
2322   fwrite($fo, "\n" . $content . "\r\n") ;
2323   fwrite($fo, 'Langue : '."\n" . $language . "\r\n") ;
2324   fclose($fo) ;
2325   //return mail ($to,$subject) ;
2326}
2327
2328/**
2329 * Function called from UAM_admin.php and main.inc.php to get the plugin version and name
2330 *
2331 * @param : plugin directory
2332 *
2333 * @return : plugin's version and name
2334 *
2335 */
2336function PluginInfos($dir)
2337{
2338  $path = $dir;
2339
2340  $plg_data = implode( '', file($path.'main.inc.php') );
2341  if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
2342  {
2343    $plugin['name'] = trim( $val[1] );
2344  }
2345  if (preg_match("|Version: (.*)|", $plg_data, $val))
2346  {
2347    $plugin['version'] = trim($val[1]);
2348  }
2349  if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
2350  {
2351    $plugin['uri'] = trim($val[1]);
2352  }
2353  if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
2354  {
2355    $plugin['description'] = trim($desc);
2356  }
2357  elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
2358  {
2359    $plugin['description'] = trim($val[1]);
2360  }
2361  if ( preg_match("|Author: (.*)|", $plg_data, $val) )
2362  {
2363    $plugin['author'] = trim($val[1]);
2364  }
2365  if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
2366  {
2367    $plugin['author uri'] = trim($val[1]);
2368  }
2369  if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
2370  {
2371    list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
2372    if (is_numeric($extension)) $plugin['extension'] = $extension;
2373  }
2374// IMPORTANT SECURITY !
2375  $plugin = array_map('htmlspecialchars', $plugin);
2376
2377  return $plugin ;
2378}
2379
2380
2381function clean_obsolete_files()
2382{
2383  if (file_exists(UAM_PATH.'obsolete.list')
2384    and $old_files = file(UAM_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
2385    and !empty($old_files))
2386  {
2387    array_push($old_files, 'obsolete.list');
2388    foreach($old_files as $old_file)
2389    {
2390      $path = UAM_PATH.$old_file;
2391      if (is_file($path))
2392      {
2393        @unlink($path);
2394      }
2395    }
2396  }
2397}
2398
2399/**
2400 * UAM_check_profile - Thx to LucMorizur
2401 * checks if a user id is registered as having already
2402 * visited his profile.php page.
2403 *
2404 * @uid        : the user id
2405 *
2406 * @user_idsOK : (returned) array of all users ids having already visited
2407 *               their profile.php pages
2408 *
2409 * @returns    : true or false whether the users has already visited his
2410 *               profile.php page or not
2411 *
2412 */
2413function UAM_check_profile($uid, &$user_idsOK)
2414{
2415  $t = array();
2416  $v = false;
2417 
2418  $query = "
2419SELECT value
2420FROM ".CONFIG_TABLE."
2421WHERE param = 'UserAdvManager_Redir'
2422;";
2423 
2424  if ($v = (($t = pwg_db_fetch_row(pwg_query($query))) !== false))
2425  {
2426    $user_idsOK = explode(',', $t[0]);
2427    $v = (in_array($uid, $user_idsOK));
2428  }
2429  return $v;
2430}
2431?>
Note: See TracBrowser for help on using the repository browser.