source: extensions/UserAdvManager/trunk/include/functions.inc.php @ 17804

Last change on this file since 17804 was 17804, checked in by Eric, 12 years ago

Version 2.40.5 :

  • Bug 2739 fixed : Php notice on admins manual validation
  • Bug 2740 fixed : Logout on auto-login after registration if registration not validated
  • Property svn:eol-style set to LF
File size: 89.1 KB
RevLine 
[5056]1<?php
[5181]2include_once (UAM_PATH.'include/constants.php');
3load_language('plugin.lang', UAM_PATH);
[5056]4
[10144]5
[7968]6/**
7 * Triggered on get_admin_plugin_menu_links
8 *
9 * Plugin's administration menu
10 */
11function UAM_admin_menu($menu)
12{
13// +-----------------------------------------------------------------------+
14// |                      Getting plugin name                              |
15// +-----------------------------------------------------------------------+
16  $plugin =  PluginInfos(UAM_PATH);
17  $name = $plugin['name'];
18 
19  array_push($menu,
20    array(
21      'NAME' => $name,
[9827]22      'URL' => get_root_url().'admin.php?page=plugin-'.basename(UAM_PATH)
[7968]23    )
24  );
25
26  return $menu;
27}
28
[10144]29
[7968]30/**
[17507]31 * Triggered on loc_begin_admin_page
32 *
33 * Check options compatibility
34 */
35function UAM_check_compat()
36{
37  global $conf, $page;
38 
39  // Check mandatory email address for email exclusion
40  $conf_UAM = unserialize($conf['UserAdvManager']);
41  if (!$conf['obligatory_user_mail_address'] and $conf_UAM[10] = 'true')
42  {
43    array_push($page['warnings'], l10n('UAM_mail_exclusion_error'));
44  }
45}
46
47
48/**
[7968]49 * Triggered on loc_begin_index
50 *
[17804]51 * Initiating GhostTracker - Perform user logout after registration if not validated
[7968]52 */
53function UAM_GhostTracker()
54{
55  global $conf, $user;
56
[17804]57  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
58
[7968]59  $conf_UAM = unserialize($conf['UserAdvManager']);
60
61  // Admins, Guests and Adult_Content users are not tracked for Ghost Tracker or Users Tracker
[12271]62  // -----------------------------------------------------------------------------------------
[7968]63  if (!is_admin() and !is_a_guest() and $user['username'] != "16" and $user['username'] != "18")
64  {
[11018]65    if ((isset($conf_UAM[15]) and $conf_UAM[15] == 'true') or (isset($conf_UAM[18]) and $conf_UAM[18] == 'true'))
[7968]66    {
67
68      $userid = get_userid($user['username']);
69         
70      // Looking for existing entry in last visit table
[12271]71      // ----------------------------------------------
[7968]72      $query = '
73SELECT *
74FROM '.USER_LASTVISIT_TABLE.'
75WHERE user_id = '.$userid.'
76;';
77       
78      $count = pwg_db_num_rows(pwg_query($query));
79         
80      if ($count == 0)
81      {
82        // If not, data are inserted in table
[12271]83        // ----------------------------------
[7968]84        $query = '
85INSERT INTO '.USER_LASTVISIT_TABLE.' (user_id, lastvisit, reminder)
86VALUES ('.$userid.', now(), "false")
87;';
88        pwg_query($query);
89      }
90      else if ($count > 0)
91      {
92        // If yes, data are updated in table
[12271]93        // ---------------------------------
[7968]94        $query = '
95UPDATE '.USER_LASTVISIT_TABLE.'
96SET lastvisit = now(), reminder = "false"
97WHERE user_id = '.$userid.'
98LIMIT 1
99;';
100        pwg_query($query);
101      }
102    }
103  }
[17804]104
105  // Perform user logout after registration if not validated
106  if ((isset($conf_UAM[39]) and $conf_UAM[39] == 'true') and !UAM_UsrReg_Verif($user['id']))
107  {
108    invalidate_user_cache();
109    logout_user();
110    redirect(UAM_PATH.'rejected.php');
111  }
[7968]112}
113
[10144]114
[7968]115/**
116 * Triggered on register_user
117 *
118 * Additional controls on user registration
119 */
120function UAM_Adduser($register_user)
121{
122  global $conf;
123
124  $conf_UAM = unserialize($conf['UserAdvManager']);
125
126  // Exclusion of Adult_Content users
[12271]127  // --------------------------------
[7968]128  if ($register_user['username'] != "16" and $register_user['username'] != "18")
129  {
[13822]130    $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
[14366]131
132    if (isset($conf_UAM[1]) and $conf_UAM[1] == 'local')
[7968]133    {
[14366]134      // This is to set user to "waiting" group or status and without ConfirMail until admin validation
135      // ----------------------------------------------------------------------------------------------
[12189]136      SetPermission($register_user['id']);// Set to "waiting" group or status until admin validation
[7968]137    }
138    // Sending registration confirmation by email
[12271]139    // ------------------------------------------
[14366]140    elseif (isset($conf_UAM[1]) and $conf_UAM[1] == 'true')
[7968]141    {
[11018]142      if (is_admin() and isset($conf_UAM[19]) and $conf_UAM[19] == 'true')
[7968]143      {
144        SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], true); 
145      }
[11018]146      elseif (is_admin() and isset($conf_UAM[19]) and $conf_UAM[19] == 'false')
[7968]147      {
148        SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], false);
149      }
150      elseif (!is_admin())
151      {
152        SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], true);
153      }
154    }
155  }
156}
157
[10144]158
[7968]159/**
160 * Triggered on delete_user
161 *
162 * Database cleanup on user deletion
163 */
164function UAM_Deluser($user_id)
165{
166  // Cleanup for ConfirmMail table
[12271]167  // -----------------------------
[7968]168  DeleteConfirmMail($user_id);
169  // Cleanup for LastVisit table
[12271]170  // ---------------------------
[7968]171  DeleteLastVisit($user_id);
172  // Cleanup Redirection settings
[12271]173  // ----------------------------
[7968]174  DeleteRedir($user_id);
175}
176
[10144]177
[7968]178/**
179 * Triggered on register_user_check
180 *
181 * Additional controls on user registration check
182 */
[8065]183function UAM_RegistrationCheck($errors, $user)
[7968]184{
[8065]185  global $conf;
[7968]186
187  // Exclusion of Adult_Content users
[12271]188  // --------------------------------
[7968]189  if ($user['username'] != "16" and $user['username'] != "18")
190  {
[8065]191    load_language('plugin.lang', UAM_PATH);
[7968]192
193    $PasswordCheck = 0;
194
195    $conf_UAM = unserialize($conf['UserAdvManager']);
196
197    // Password enforcement control
[12271]198    // ----------------------------
[11018]199    if (isset($conf_UAM[12]) and $conf_UAM[12] == 'true' and !empty($conf_UAM[13]))
[7968]200    {
201      if (!empty($user['password']) and !is_admin())
202      {
203        $PasswordCheck = testpassword($user['password']);
204 
[11018]205        if ($PasswordCheck < $conf_UAM[13])
[7968]206        {
[9177]207          $message = get_l10n_args('UAM_reg_err_login4_%s', $PasswordCheck);
[11018]208          $lang['reg_err_pass'] = l10n_args($message).$conf_UAM[13];
[8065]209          array_push($errors, $lang['reg_err_pass']);
[7968]210        }
211      }
[11018]212      else if (!empty($user['password']) and is_admin() and isset($conf_UAM[14]) and $conf_UAM[14] == 'true')
[7968]213      {
214        $PasswordCheck = testpassword($user['password']);
215 
[11018]216        if ($PasswordCheck < $conf_UAM[13])
[7968]217        {
[9177]218          $message = get_l10n_args('UAM_reg_err_login4_%s', $PasswordCheck);
[11018]219          $lang['reg_err_pass'] = l10n_args($message).$conf_UAM[13];
[8065]220          array_push($errors, $lang['reg_err_pass']);
[7968]221        }
222      }
223    }
224
225    // Username without forbidden keys
[12271]226    // -------------------------------
[11018]227    if (isset($conf_UAM[5]) and $conf_UAM[5] == 'true' and !empty($user['username']) and ValidateUsername($user['username']) and !is_admin())
[7968]228    {
[11018]229      $lang['reg_err_login1'] = l10n('UAM_reg_err_login2')."'".$conf_UAM[6]."'";
[8065]230      array_push($errors, $lang['reg_err_login1']);
[7968]231    }
232
233    // Email without forbidden domains
[12271]234    // -------------------------------
[11018]235    if (isset($conf_UAM[10]) and $conf_UAM[10] == 'true' and !empty($user['email']) and ValidateEmailProvider($user['email']) and !is_admin())
[7968]236    {
[11018]237      $lang['reg_err_login1'] = l10n('UAM_reg_err_login5')."'".$conf_UAM[11]."'";
[8065]238      array_push($errors, $lang['reg_err_login1']);
[7968]239    }
[8065]240    return $errors;
[7968]241  }
242}
243
[10144]244
[7968]245/**
246 * Triggered on loc_begin_profile
247 */
248function UAM_Profile_Init()
249{
250  global $conf, $user, $template;
251
252  $conf_UAM = unserialize($conf['UserAdvManager']);
[12239]253
254  // Update first redirection parameter
[12271]255  // ----------------------------------
[11018]256  if ((isset($conf_UAM[20]) and $conf_UAM[20] == 'true'))
[7968]257  {
258    $user_idsOK = array();
[8065]259    if (!UAM_check_profile($user['id'], $user_idsOK))
[7968]260    {
261      $user_idsOK[] = $user['id'];
262
263      $query = "
264UPDATE ".CONFIG_TABLE."
265SET value = \"".implode(',', $user_idsOK)."\"
266WHERE param = 'UserAdvManager_Redir';";
267         
268      pwg_query($query);
269    }
270  }
271
[12239]272  // Special message display for password reset
[12271]273  // ------------------------------------------
[12239]274  if ((isset($conf_UAM[38]) and $conf_UAM[38] == 'true'))
275  {
276    if (UAM_check_pwgreset($user['id']))
277    {
278      $template->append('errors', l10n('UAM_Password_Reset_Msg'));
279    }
280  }
281
282  // Controls on profile page submission
[12271]283  // -----------------------------------
[7968]284  if (isset($_POST['validate']) and !is_admin())
285  {
286    // Email without forbidden domains
[12271]287    // -------------------------------
[11018]288    if (isset($conf_UAM[10]) and $conf_UAM[10] == 'true' and !empty($_POST['mail_address']))
[7968]289    {
290      if (ValidateEmailProvider($_POST['mail_address']))
291      {
[11018]292        $template->append('errors', l10n('UAM_reg_err_login5')."'".$conf_UAM[11]."'");
[7968]293        unset($_POST['validate']);
294      }
295    }
296
[12239]297    // Password reset control
[12271]298    // ----------------------
[12239]299    if (isset($conf_UAM[38]) and $conf_UAM[38] == 'true' and UAM_check_pwgreset($user['id']))
300    {
[13841]301      // if password not changed then pwdreset field = true else pwdreset field = false
[12271]302      // ------------------------------------------------------------------------------
[12239]303      if (!empty($_POST['use_new_pwd']))
304      {
305        $query = '
306UPDATE '.USERS_TABLE.'
307SET UAM_pwdreset = "false"
308WHERE id = '.$user['id'].'
309LIMIT 1
310;';
311        pwg_query($query);
312      }
313    }
314
[14366]315    $typemail = 3; // Only information email send to user on user profile update if checked
[7968]316
317    if (!empty($_POST['use_new_pwd']))
318    {
[14366]319      $typemail = 2; // Confirmation email on user profile update - With information email if checked
[7968]320
321      // Password enforcement control
[12271]322      // ----------------------------
[11018]323      if (isset($conf_UAM[12]) and $conf_UAM[12] == 'true' and !empty($conf_UAM[13]))
[7968]324      {
325        $PasswordCheck = testpassword($_POST['use_new_pwd']);
326
[11018]327        if ($PasswordCheck < $conf_UAM[13])
[7968]328        {
[9177]329          $message = get_l10n_args('UAM_reg_err_login4_%s', $PasswordCheck);
[11018]330          $template->append('errors', l10n_args($message).$conf_UAM[13]);
[7968]331          unset($_POST['use_new_pwd']);
332          unset($_POST['validate']);
333        }
334      }
335    }
336
337    // Sending registration confirmation by email
[12271]338    // ------------------------------------------
[14857]339    if ((isset($conf_UAM[1]) and $conf_UAM[1] == 'true') or (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
[7968]340    {
341      $confirm_mail_need = false;
342
343      if (!empty($_POST['mail_address']))
344      {
345        $query = '
346SELECT '.$conf['user_fields']['email'].' AS email
347FROM '.USERS_TABLE.'
348WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
349;';
350
351        list($current_email) = pwg_db_fetch_row(pwg_query($query));
352
353        // This is to send a new validation key
[12271]354        // ------------------------------------
[7968]355        if ($_POST['mail_address'] != $current_email and (isset($conf_UAM[1]) and $conf_UAM[1] == 'true'))
[14366]356        {
357          SetPermission($user['id']);// Set to "waiting" group or status until user validation
[7968]358          $confirm_mail_need = true;
[14366]359        }
[7968]360
361        // This is to set the user to "waiting" group or status until admin validation
[12271]362        // ---------------------------------------------------------------------------
[14366]363        elseif ($_POST['mail_address'] != $current_email and (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
364        {
[13841]365          SetPermission($user['id']);// Set to "waiting" group or status until admin validation
[7968]366          $confirm_mail_need = false;
[14366]367        }       
[7968]368      }
369       
[14366]370      if (((!empty($_POST['use_new_pwd']) and (isset($conf_UAM[0]) and $conf_UAM[0] == 'true')) or $confirm_mail_need))
[7968]371      {
372        $query = '
373SELECT '.$conf['user_fields']['username'].'
374FROM '.USERS_TABLE.'
375WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
376;';
377       
378        list($username) = pwg_db_fetch_row(pwg_query($query));
379        SendMail2User($typemail, $user['id'], $username, $_POST['use_new_pwd'], $_POST['mail_address'], $confirm_mail_need);
380      }
381    }
382  }
383}
384
[10144]385
[7968]386/**
387 * Triggered on login_success
388 *
[8065]389 * Triggers scheduled tasks at login
390 * Redirects a visitor (except for admins, webmasters and generic statuses) to his profile.php page (Thx to LucMorizur)
[7968]391 *
392 */
[8065]393function UAM_LoginTasks()
[7968]394{
395  global $conf, $user;
396 
[12661]397  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
398 
[7968]399  $conf_UAM = unserialize($conf['UserAdvManager']);
400 
[9253]401  // Performing GhostTracker scheduled tasks
[12271]402  // ---------------------------------------
[11018]403  if ((isset($conf_UAM[21]) and $conf_UAM[21] == 'true'))
[8065]404  {
[9253]405    UAM_GT_ScheduledTasks();
[8065]406  }
407
[9266]408  // Performing User validation scheduled tasks
[12271]409  // ------------------------------------------
[11018]410  if ((isset($conf_UAM[30]) and $conf_UAM[30] == 'true'))
[9266]411  {
412    UAM_USR_ScheduledTasks();
413  }
414
[12661]415  // Avoid login into public galleries until registration confirmation is done
416  if ((isset($conf_UAM[39]) and $conf_UAM[39] == 'false') or ((isset($conf_UAM[39]) and $conf_UAM[39] == 'true') and UAM_UsrReg_Verif($user['id'])))
[12314]417  {
[12661]418    // Performing redirection to profile page on first login
419    // -----------------------------------------------------
420    if ((isset($conf_UAM[20]) and $conf_UAM[20] == 'true'))
421    {
422      $query ='
[7968]423SELECT user_id, status
424FROM '.USER_INFOS_TABLE.'
425WHERE user_id = '.$user['id'].'
426;';
[12661]427      $data = pwg_db_fetch_assoc(pwg_query($query));
[8065]428
[12661]429      if ($data['status'] <> "admin" and $data['status'] <> "webmaster" and $data['status'] <> "generic") // Exclusion of specific accounts
430      {
431        $user_idsOK = array();
432        if (!UAM_check_profile($user['id'], $user_idsOK))
433          redirect(PHPWG_ROOT_PATH.'profile.php');
434      }
[8087]435    }
[12239]436
[12661]437    // Performing redirection to profile page for password reset
438    // ---------------------------------------------------------
439    if ((isset($conf_UAM[38]) and $conf_UAM[38] == 'true'))
440    {
441      $query ='
[12239]442SELECT user_id, status
443FROM '.USER_INFOS_TABLE.'
444WHERE user_id = '.$user['id'].'
445;';
[12661]446      $data = pwg_db_fetch_assoc(pwg_query($query));
[12239]447
[12661]448      if ($data['status'] <> "webmaster" and $data['status'] <> "generic") // Exclusion of specific accounts
[12239]449      {
[12661]450        if (UAM_check_pwgreset($user['id']))
451        {
452          redirect(PHPWG_ROOT_PATH.'profile.php');
453        }
[12239]454      }
455    }
456  }
[12661]457  elseif ((isset($conf_UAM[39]) and $conf_UAM[39] == 'true') and !UAM_UsrReg_Verif($user['id']))
458  {
459    // Logged-in user cleanup, session destruction and redirected to custom page
460    // -------------------------------------------------------------------------
461    invalidate_user_cache();
462    logout_user();
463    redirect(UAM_PATH.'rejected.php');
464  }
[7968]465}
466
[9908]467
[7968]468/**
[9908]469 * Adds a new module settable in PWG_Stuffs - Triggered on get_stuffs_modules in main.inc.php
470 * Useful to inform unvalidated user for their status
471 *
472 */
473function register_UAM_stuffs_module($modules)
474{
475  array_push($modules, array(
476    'path' => UAM_PATH.'/stuffs_module',
477    'name' => l10n('UAM_Stuffs_Title'),
478    'description' => l10n('UAM_Stuffs_Desc'),
479    )
480  );
481  return $modules;
482}
483
484
485/**
[8065]486 * Triggered on UAM_LoginTasks()
[7968]487 *
[9253]488 * Executes optional post-login tasks for Ghost users
[7968]489 *
490 */
[9253]491function UAM_GT_ScheduledTasks()
[7968]492{
[8065]493  global $conf, $user, $page;
[9253]494
495  if (!defined('PHPWG_ROOT_PATH'))
496  {
497    die('Hacking attempt!');
498  }
499         
500  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
501
[7968]502  $conf_UAM = unserialize($conf['UserAdvManager']);
[8065]503 
504  $collection = array();
[8087]505  $reminder = false;
[8065]506 
507  $page['filtered_users'] = get_ghosts_autotasks();
[7968]508
[8087]509  foreach($page['filtered_users'] as $listed_user)
510  {
511    array_push($collection, $listed_user['id']);
512  }
[8065]513
[12271]514  // GhostTracker auto group, status or privacy level downgrade with or without information email sending and autodeletion if user already reminded
515  // ----------------------------------------------------------------------------------------------------------------------------------------------
[12189]516  if ((isset($conf_UAM[21]) and $conf_UAM[21] == 'true') and ((isset($conf_UAM[25]) and $conf_UAM[25] <> -1) or (isset($conf_UAM[26]) and $conf_UAM[26] <> -1) or (isset($conf_UAM[37]) and $conf_UAM[37] <> -1)))
[8087]517  {
[8065]518    if (count($collection) > 0)
519        {
[8087]520      // Process if a non-admin nor webmaster user is logged
[12271]521      // ---------------------------------------------------
[8087]522      if (in_array($user['id'], $collection))
523        {
[9253]524        // Check lastvisit reminder state
[12271]525        // ------------------------------
[9253]526        $query = '
[8087]527SELECT reminder
528FROM '.USER_LASTVISIT_TABLE.'
[9253]529WHERE user_id = '.$user['id'].';';
[8087]530
[9253]531        $result = pwg_db_fetch_assoc(pwg_query($query));
[8087]532
[9253]533        if (isset($result['reminder']) and $result['reminder'] == 'true')
534        {
535          $reminder = true;
[8065]536        }
[9253]537        else
[8092]538        {
[9253]539          $reminder = false;
[8092]540        }
[9253]541
[9266]542        // If user already reminded for ghost account
[12271]543        // ------------------------------------------
[9266]544        if ($reminder)
[8092]545        {
[12271]546          // Delete account
547          // --------------
[9253]548          delete_user($user['id']);
549
550          // Logged-in user cleanup, session destruction and redirected to custom page
[12271]551          // -------------------------------------------------------------------------
[8092]552          invalidate_user_cache();
[9253]553          logout_user();
[9266]554          redirect(UAM_PATH.'GT_del_account.php');
[8092]555        }
[8065]556        }
[8087]557      else // Process if an admin or webmaster user is logged
[8065]558      {
559        foreach ($collection as $user_id)
560        {
[8087]561          // Check lastvisit reminder state
[12271]562          // ------------------------------
[8087]563          $query = '
564SELECT reminder
565FROM '.USER_LASTVISIT_TABLE.'
566WHERE user_id = '.$user_id.';';
567
568          $result = pwg_db_fetch_assoc(pwg_query($query));
569
570          if (isset($result['reminder']) and $result['reminder'] == 'true')
571          {
572            $reminder = true;
573          }
574          else
575          {
576            $reminder = false;
577          }
578
579          // If never reminded before
[12271]580          // ------------------------
[8087]581          if (!$reminder)
582          {
[12271]583            // Reset of lastvisit date
584            // -----------------------
[8087]585            list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
586
587                        $query = "
588UPDATE ".USER_LASTVISIT_TABLE."
589SET lastvisit='".$dbnow."'
590WHERE user_id = '".$user_id."'
591;";
592            pwg_query($query);
593
[8065]594          // Auto change group and / or status
[12271]595          // ---------------------------------
[8087]596            // Delete user from all groups
[12271]597            // ---------------------------
[8087]598            $query = "
[8065]599DELETE FROM ".USER_GROUP_TABLE."
600WHERE user_id = '".$user_id."'
601  AND (
602    group_id = '".$conf_UAM[2]."'
603  OR
604    group_id = '".$conf_UAM[3]."'
605  )
606;";
[8087]607            pwg_query($query);
[8065]608
[8087]609            // Change user status
[12271]610            // ------------------
[11018]611            if ($conf_UAM[26] <> -1)
[8087]612            {
613              $query = "
[8065]614UPDATE ".USER_INFOS_TABLE."
[11018]615SET status = '".$conf_UAM[26]."'
[8065]616WHERE user_id = '".$user_id."'
617;";
[8087]618              pwg_query($query);
619            }
[8065]620
[8087]621            // Change user group
[12271]622            // -----------------
[11018]623            if ($conf_UAM[25] <> -1)
[8087]624            {
625              $query = "
[8065]626INSERT INTO ".USER_GROUP_TABLE."
627  (user_id, group_id)
628VALUES
[11018]629  ('".$user_id."', '".$conf_UAM[25]."')
[8065]630;";
[8087]631              pwg_query($query);
632            }
[8065]633
[12189]634            // Change user privacy level
[12271]635            // -------------------------
[12189]636            if ($conf_UAM[37] <> -1)
637            {
638              $query = "
639UPDATE ".USER_INFOS_TABLE."
640SET level = '".$conf_UAM[37]."'
641WHERE user_id = '".$user_id."'
642;";
643              pwg_query($query);
644            }
645
[8087]646            // Auto send email notification on group / status downgrade
[12271]647            // --------------------------------------------------------
[11018]648            if (isset($conf_UAM[22]) and $conf_UAM[22] == 'true')
[8087]649            {
650              // Set reminder true
[12271]651              // -----------------
[8087]652              $query = "
653UPDATE ".USER_LASTVISIT_TABLE."
654SET reminder = 'true'
655WHERE user_id = '".$user_id."'
656;";
657              pwg_query($query);
658           
659              // Reset confirmed user status to unvalidated
[12271]660              // ------------------------------------------
[8087]661                                                  $query = '
[8065]662UPDATE '.USER_CONFIRM_MAIL_TABLE.'
663SET date_check = NULL
664WHERE user_id = "'.$user_id.'"
665;';
[8087]666                                                  pwg_query($query);
[8065]667
[8087]668              // Get users information for email notification
[12271]669              // --------------------------------------------
[8087]670                                                  $query = '
[8065]671SELECT id, username, mail_address
672FROM '.USERS_TABLE.'
673WHERE id = '.$user_id.'
674;';
[8087]675                                                  $data = pwg_db_fetch_assoc(pwg_query($query));
[8065]676           
[8087]677              demotion_mail($user_id, $data['username'], $data['mail_address']);
678            }
[8065]679          }
[8087]680          elseif ($reminder) // If user already reminded for ghost account
681          {
[12271]682            // Delete account
683            // --------------
[8087]684            delete_user($user_id);
685          }
[8065]686        }
687      }
688    }
[7968]689  }
690}
691
[10144]692
[7968]693/**
[9266]694 * Triggered on UAM_LoginTasks()
695 *
696 * Executes optional post-login tasks for unvalidated users
697 *
698 */
699function UAM_USR_ScheduledTasks()
700{
701  global $conf, $user, $page;
702
703  if (!defined('PHPWG_ROOT_PATH'))
704  {
705    die('Hacking attempt!');
706  }
707         
708  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
709
710  $conf_UAM = unserialize($conf['UserAdvManager']);
711 
712  $collection = array();
713  $reminder = false;
714 
715  $page['filtered_users'] = get_unvalid_user_autotasks();
716
717  foreach($page['filtered_users'] as $listed_user)
718  {
719    array_push($collection, $listed_user['id']);
720  }
721
722  // Unvalidated accounts auto email sending and autodeletion if user already reminded
[12271]723  // ---------------------------------------------------------------------------------
[11018]724  if ((isset($conf_UAM[30]) and $conf_UAM[30] == 'true'))
[9266]725  {
726    if (count($collection) > 0)
727        {
728      // Process if a non-admin nor webmaster user is logged
[12271]729      // ---------------------------------------------------
[9266]730      if (in_array($user['id'], $collection))
731        {
732        // Check ConfirmMail reminder state
[12271]733        // --------------------------------
[9266]734        $query = '
735SELECT reminder
736FROM '.USER_CONFIRM_MAIL_TABLE.'
737WHERE user_id = '.$user['id'].';';
738
739        $result = pwg_db_fetch_assoc(pwg_query($query));
740
741        if (isset($result['reminder']) and $result['reminder'] == 'true')
742        {
743          $reminder = true;
744        }
745        else
746        {
747          $reminder = false;
748        }
749
750        // If never reminded before, send reminder and set reminder True
[12271]751        // -------------------------------------------------------------
[11018]752        if (!$reminder and isset($conf_UAM[32]) and $conf_UAM[32] == 'true')
[9266]753        {
754                  $typemail = 1;
755         
756          // Get current user informations
[12271]757          // -----------------------------
[9266]758          $query = "
759SELECT id, username, mail_address
760FROM ".USERS_TABLE."
761WHERE id = '".$user['id']."'
762;";
763          $data = pwg_db_fetch_assoc(pwg_query($query));
764
765          ResendMail2User($typemail,$user['id'],stripslashes($data['username']),$data['mail_address'],true);
766        }
767
768        // If already reminded before, delete user
[12271]769        // ---------------------------------------
[9266]770        if ($reminder)
771        {
772          // delete account
773          delete_user($user['id']);
774
775          // Logged-in user cleanup, session destruction and redirected to custom page
[12271]776          // -------------------------------------------------------------------------
[9266]777          invalidate_user_cache();
778          logout_user();
779          redirect(UAM_PATH.'USR_del_account.php');
780        }
781        }
782      else // Process if an admin or webmaster user is logged
783      {
784        foreach ($collection as $user_id)
785        {
786          // Check reminder state
[12271]787          // --------------------
[9266]788          $query = '
789SELECT reminder
790FROM '.USER_CONFIRM_MAIL_TABLE.'
791WHERE user_id = '.$user_id.';';
792
793          $result = pwg_db_fetch_assoc(pwg_query($query));
794
795          if (isset($result['reminder']) and $result['reminder'] == 'true')
796          {
797            $reminder = true;
798          }
799          else
800          {
801            $reminder = false;
802          }
803
804          // If never reminded before, send reminder and set reminder True
[12271]805          // -------------------------------------------------------------
[11018]806          if (!$reminder and isset($conf_UAM[32]) and $conf_UAM[32] == 'true')
[9266]807          {
808            $typemail = 1;
809         
810            // Get current user informations
[12271]811            // -----------------------------
[9266]812            $query = "
813SELECT id, username, mail_address
814FROM ".USERS_TABLE."
815WHERE id = '".$user_id."'
816;";
817            $data = pwg_db_fetch_assoc(pwg_query($query));
818
819            ResendMail2User($typemail,$user_id,stripslashes($data['username']),$data['mail_address'],true);
820          }
821          elseif ($reminder) // If user already reminded for account validation
822          {
[12271]823            // Delete account
824            // --------------
[9266]825            delete_user($user_id);
826          }
827        }
828      }
829    }
830  }
831}
832
833
834/**
[7968]835 * Triggered on init
836 *
837 * Check for forbidden email domains in admin's users management panel
838 */
839function UAM_InitPage()
840{
841  load_language('plugin.lang', UAM_PATH);
842  global $conf, $template, $page, $lang, $errors;
843
844  $conf_UAM = unserialize($conf['UserAdvManager']);
845
846// Admin user management
[12271]847// ---------------------
[7968]848  if (script_basename() == 'admin' and isset($_GET['page']) and $_GET['page'] == 'user_list')
849  {
850    if (isset($_POST['submit_add']))
851    {
852      // Email without forbidden domains
[12271]853      // -------------------------------
[11018]854      if (isset($conf_UAM[10]) and $conf_UAM[10] == 'true' and !empty($_POST['email']) and ValidateEmailProvider($_POST['email']))
[7968]855      {
[11018]856        $template->append('errors', l10n('UAM_reg_err_login5')."'".$conf_UAM[11]."'");
[7968]857        unset($_POST['submit_add']);
858      }
859    }
860  }
861}
862
[9135]863
[7968]864/**
[9135]865 * Triggered on render_lost_password_mail_content
866 *
867 * Adds a customized text in lost password email content
868 * Added text is inserted before users login name and new password
869 *
870 * @param : Standard Piwigo email content
871 *
872 * @return : Customized content added to standard content
873 *
874 */
875function UAM_lost_password_mail_content($infos)
876{
877  global $conf;
878 
879  load_language('plugin.lang', UAM_PATH);
880 
881  $conf_UAM = unserialize($conf['UserAdvManager']);
882 
[11018]883  if (isset($conf_UAM[28]) and $conf_UAM[28] == 'true')
[9135]884  {
[9381]885    // Management of Extension flags ([mygallery], [myurl])
[9160]886    $patterns[] = '#\[mygallery\]#i';
887    $replacements[] = $conf['gallery_title'];
[9197]888    $patterns[] = '#\[myurl\]#i';
[14651]889    $replacements[] = get_gallery_home_url();
[9160]890   
[11018]891    $infos = preg_replace($patterns, $replacements, $conf_UAM[29])."\n"."\n".$infos;
[9135]892  }
893  return $infos;
894}
895
896
897/**
[7968]898 * Function called from main.inc.php to send validation email
899 *
900 * @param : Type of email, user id, username, email address, confirmation (optional)
901 *
902 */
[5056]903function SendMail2User($typemail, $id, $username, $password, $email, $confirm)
904{
[5064]905  global $conf;
[5056]906
[5181]907  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]908 
909        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
910 
911        $infos1_perso = "";
912  $infos2_perso = "";
[14729]913  $subject = "";
[5056]914
[7968]915// We have to get the user's language in database
[12271]916// ----------------------------------------------
[5056]917  $query ='
918SELECT user_id, language
919FROM '.USER_INFOS_TABLE.'
920WHERE user_id = '.$id.'
921;';
[5633]922  $data = pwg_db_fetch_assoc(pwg_query($query));
[5056]923
[7968]924// Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language
[12271]925// -------------------------------------------------------------------------------------------------------------------------------
[5056]926  if (empty($data))
927  {
[7968]928// And switch gallery to this language before using personalized and multilangual contents
[12271]929// ---------------------------------------------------------------------------------------
[5056]930    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
931    switch_lang_to($language);
932  }
933  else
934  {
[7968]935// And switch gallery to this language before using personalized and multilangual contents
[12271]936// ---------------------------------------------------------------------------------------
937    //$language = $data['language']; // Usefull for debugging
[5056]938    switch_lang_to($data['language']);
[5181]939    load_language('plugin.lang', UAM_PATH);
[5056]940  }
941
942  switch($typemail)
943  {
[14366]944    case 1: // Confirmation email on user registration - Without information email (already managed by Piwigo)
[14729]945      if (isset($conf_UAM[41]) and $conf_UAM[41] <> '')
946      {
947        // Management of Extension flags ([username], [mygallery])
948        // -------------------------------------------------------
949        $patterns[] = '#\[username\]#i';
950        $replacements[] = $username;
951        $patterns[] = '#\[mygallery\]#i';
952        $replacements[] = $conf['gallery_title'];
953   
954        if (function_exists('get_user_language_desc'))
955        {
956          $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[41]))."\n\n";
957        }
958        else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[41]))."\n\n"; 
959      }
[14366]960
961      break;
962     
963    case 2: // Confirmation email on user profile update - With information email if checked
[14729]964      if (isset($conf_UAM[41]) and $conf_UAM[41] <> '')
965      {
966        // Management of Extension flags ([username], [mygallery])
967        // -------------------------------------------------------
968        $patterns[] = '#\[username\]#i';
969        $replacements[] = $username;
970        $patterns[] = '#\[mygallery\]#i';
971        $replacements[] = $conf['gallery_title'];
972   
973        if (function_exists('get_user_language_desc'))
974        {
975          $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[41]))."\n\n";
976        }
977        else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[41]))."\n\n"; 
978      }
979
[5056]980      $password = $password <> '' ? $password : l10n('UAM_empty_pwd');
[14366]981
[11018]982      if (isset($conf_UAM[8]) and $conf_UAM[8] <> '')
[5056]983      {
[9197]984        // Management of Extension flags ([username], [mygallery], [myurl])
[12271]985        // ----------------------------------------------------------------
[9160]986        $patterns[] = '#\[username\]#i';
987        $replacements[] = $username;
988        $patterns[] = '#\[mygallery\]#i';
989        $replacements[] = $conf['gallery_title'];
[9197]990        $patterns[] = '#\[myurl\]#i';
[14651]991        $replacements[] = get_gallery_home_url();
[9160]992   
[5056]993        if (function_exists('get_user_language_desc'))
994        {
[11018]995          $infos1_perso = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[8]))."\n\n";
[5056]996        }
[11018]997        else $infos1_perso = l10n(preg_replace($patterns, $replacements, $conf_UAM[8]))."\n\n"; 
[5056]998      }
999
[14366]1000      if (isset($conf_UAM[0]) and $conf_UAM[0] == 'true')
1001      {
1002        if (isset($conf_UAM[34]) and $conf_UAM[34] == 'true') // Allow display of clear password in email
1003        {
1004          $infos1 = array(
1005            get_l10n_args('UAM_infos_mail %s', stripslashes($username)),
1006            get_l10n_args('UAM_User: %s', stripslashes($username)),
1007            get_l10n_args('UAM_Password: %s', $password),
1008            get_l10n_args('Email: %s', $email),
1009            get_l10n_args('', ''),
1010          );
1011        }
1012        else // Do not allow display of clear password in email
1013        {
1014          $infos1 = array(
1015            get_l10n_args('UAM_infos_mail %s', stripslashes($username)),
1016            get_l10n_args('UAM_User: %s', stripslashes($username)),
1017            get_l10n_args('Email: %s', $email),
1018            get_l10n_args('', ''),
1019          );
1020        }
1021      }
1022
[5056]1023      break;
1024       
[14366]1025    case 3: // Only information email send to user if checked
[14729]1026      if (isset($conf_UAM[43]) and $conf_UAM[43] <> '')
1027      {
1028        // Management of Extension flags ([username], [mygallery])
1029        // -------------------------------------------------------
1030        $patterns[] = '#\[username\]#i';
1031        $replacements[] = $username;
1032        $patterns[] = '#\[mygallery\]#i';
1033        $replacements[] = $conf['gallery_title'];
1034   
1035        if (function_exists('get_user_language_desc'))
1036        {
1037          $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[43]))."\n\n";
1038        }
1039        else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[43]))."\n\n"; 
1040      }
1041
[5056]1042      $password = $password <> '' ? $password : l10n('UAM_no_update_pwd');
1043
[14366]1044      if (isset($conf_UAM[8]) and $conf_UAM[8] <> '')
1045      {
1046        // Management of Extension flags ([username], [mygallery], [myurl])
1047        // ----------------------------------------------------------------
1048        $patterns[] = '#\[username\]#i';
1049        $replacements[] = $username;
1050        $patterns[] = '#\[mygallery\]#i';
1051        $replacements[] = $conf['gallery_title'];
1052        $patterns[] = '#\[myurl\]#i';
[14651]1053        $replacements[] = get_gallery_home_url();
[14366]1054   
1055        if (function_exists('get_user_language_desc'))
1056        {
1057          $infos1_perso = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[8]))."\n\n";
1058        }
1059        else $infos1_perso = l10n(preg_replace($patterns, $replacements, $conf_UAM[8]))."\n\n"; 
1060      }
1061
1062      if (isset($conf_UAM[0]) and $conf_UAM[0] == 'true')
1063      {
1064        if (isset($conf_UAM[34]) and $conf_UAM[34] == 'true') // Allow display of clear password in email
1065        {
1066          $infos1 = array(
1067            get_l10n_args('UAM_infos_mail %s', stripslashes($username)),
1068            get_l10n_args('UAM_User: %s', stripslashes($username)),
1069            get_l10n_args('UAM_Password: %s', $password),
1070            get_l10n_args('Email: %s', $email),
1071            get_l10n_args('', ''),
1072          );
1073        }
1074        else // Do not allow display of clear password in email
1075        {
1076          $infos1 = array(
1077            get_l10n_args('UAM_infos_mail %s', stripslashes($username)),
1078            get_l10n_args('UAM_User: %s', stripslashes($username)),
1079            get_l10n_args('Email: %s', $email),
1080            get_l10n_args('', ''),
1081          );
1082        }
1083      }
1084
[5056]1085      break;
1086  }
1087
[12271]1088  if ( isset($conf_UAM[1]) and $conf_UAM[1] == 'true' and $confirm) // Add confirmation link ?
[5056]1089  {
1090    $infos2 = array
1091    (
[9177]1092      get_l10n_args('UAM_Link: %s', AddConfirmMail($id, $email)),
[5056]1093      get_l10n_args('', ''),
1094    );
1095
[12271]1096    if (isset($conf_UAM[9]) and $conf_UAM[9] <> '') // Add personal text in confirmation email ?
[5056]1097    {
[11318]1098      // Management of Extension flags ([username], [mygallery], [myurl], [Kdays])
[12271]1099      // -------------------------------------------------------------------------
[9160]1100      $patterns[] = '#\[username\]#i';
1101      $replacements[] = $username;
1102      $patterns[] = '#\[mygallery\]#i';
1103      $replacements[] = $conf['gallery_title'];
[9197]1104      $patterns[] = '#\[myurl\]#i';
[14651]1105      $replacements[] = get_gallery_home_url();
[9160]1106     
[11318]1107      if (isset($conf_UAM_ConfirmMail[0]) and $conf_UAM_ConfirmMail[0] == 'true') // [Kdays] replacement only if related option is active
1108      {
1109        $patterns[] = '#\[Kdays\]#i';
1110        $replacements[] = $conf_UAM_ConfirmMail[1];
1111      }
1112     
[5056]1113      if (function_exists('get_user_language_desc'))
1114      {
[11018]1115        $infos2_perso = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[9]))."\n\n";
[5056]1116      }
[11018]1117      else $infos2_perso = l10n(preg_replace($patterns, $replacements, $conf_UAM[9]))."\n\n";
[5056]1118    }
1119  }
1120
[7968]1121// Sending the email with subject and contents
[12271]1122// -------------------------------------------
[5056]1123  pwg_mail($email, array(
1124    'subject' => $subject,
1125    'content' => (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
1126  ));
1127
[7968]1128// Switching back to default language
[12271]1129// ----------------------------------
[5056]1130switch_lang_back();
1131}
1132
[10144]1133
[7968]1134/**
1135 * Function called from UAM_admin.php to resend validation email with or without new validation key
1136 *
1137 * @param : Type of email, user id, username, email address, confirmation (optional)
1138 *
1139 */
[5056]1140function ResendMail2User($typemail, $user_id, $username, $email, $confirm)
1141{
[5064]1142  global $conf;
[14729]1143 
1144  $subject = "";
[5056]1145
[5181]1146  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]1147
[5181]1148  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
[5056]1149 
1150        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
1151 
[7968]1152// We have to get the user's language in database
[12271]1153// ----------------------------------------------
[5056]1154  $query ='
1155SELECT user_id, language
1156FROM '.USER_INFOS_TABLE.'
1157WHERE user_id = '.$user_id.'
1158;';
[5633]1159  $data = pwg_db_fetch_assoc(pwg_query($query));
[5056]1160  $language = $data['language'];
1161 
[7968]1162// And switch gallery to this language before using personalized and multilangual contents
[12271]1163// ---------------------------------------------------------------------------------------
[5056]1164  switch_lang_to($data['language']);
1165   
[5181]1166  load_language('plugin.lang', UAM_PATH);
[5056]1167
1168  switch($typemail)
1169  {
[14366]1170    case 1: //Generating email content for remind with a new key
[14729]1171      if (isset($conf_UAM[42]) and $conf_UAM[42] <> '')
1172      {
1173        // Management of Extension flags ([username], [mygallery])
1174        // -------------------------------------------------------
1175        $patterns[] = '#\[username\]#i';
1176        $replacements[] = $username;
1177        $patterns[] = '#\[mygallery\]#i';
1178        $replacements[] = $conf['gallery_title'];
1179   
1180        if (function_exists('get_user_language_desc'))
1181        {
1182          $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[42]))."\n\n";
1183        }
1184        else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[42]))."\n\n"; 
1185      }
[5056]1186     
1187      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)
1188      {
[11318]1189        // Management of Extension flags ([username], [mygallery], [myurl], [Kdays])
[12271]1190        // -------------------------------------------------------------------------
[9160]1191        $patterns[] = '#\[username\]#i';
1192        $replacements[] = $username;
1193        $patterns[] = '#\[mygallery\]#i';
1194        $replacements[] = $conf['gallery_title'];
[9197]1195        $patterns[] = '#\[myurl\]#i';
[14651]1196        $replacements[] = get_gallery_home_url();
[9160]1197
[11318]1198        if (isset($conf_UAM_ConfirmMail[0]) and $conf_UAM_ConfirmMail[0] == 'true') // [Kdays] replacement only if related option is active
1199        {
1200          $patterns[] = '#\[Kdays\]#i';
1201          $replacements[] = $conf_UAM_ConfirmMail[1];
1202        }
1203
[5056]1204        if (function_exists('get_user_language_desc'))
1205        {
[9160]1206          $infos1 = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[2]))."\n\n";
[5056]1207        }
[9160]1208                                else $infos1 = l10n(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[2]))."\n\n";
[5056]1209
1210        $infos2 = array
1211        (
[9177]1212          get_l10n_args('UAM_Link: %s', ResetConfirmMail($user_id)),
[5056]1213          get_l10n_args('', ''),
1214        );       
1215                        }
1216
[12271]1217// Set reminder true
1218// -----------------     
[5056]1219      $query = "
1220UPDATE ".USER_CONFIRM_MAIL_TABLE."
1221SET reminder = 'true'
1222WHERE user_id = '".$user_id."'
1223;";
1224      pwg_query($query);
1225     
1226                break;
1227     
[14366]1228    case 2: //Generating email content for remind without a new key
[14729]1229      if (isset($conf_UAM[42]) and $conf_UAM[42] <> '')
1230      {
1231        // Management of Extension flags ([username], [mygallery])
1232        // -------------------------------------------------------
1233        $patterns[] = '#\[username\]#i';
1234        $replacements[] = $username;
1235        $patterns[] = '#\[mygallery\]#i';
1236        $replacements[] = $conf['gallery_title'];
1237   
1238        if (function_exists('get_user_language_desc'))
1239        {
1240          $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[42]))."\n\n";
1241        }
1242        else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[42]))."\n\n"; 
1243      }
[5056]1244     
[9160]1245      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)
[5056]1246      {
[11318]1247        // Management of Extension flags ([username], [mygallery], [myurl], [Kdays])
[12271]1248        // -------------------------------------------------------------------------
[9160]1249        $patterns[] = '#\[username\]#i';
1250        $replacements[] = $username;
1251        $patterns[] = '#\[mygallery\]#i';
1252        $replacements[] = $conf['gallery_title'];
[9197]1253        $patterns[] = '#\[myurl\]#i';
[14651]1254        $replacements[] = get_gallery_home_url();
[11318]1255
1256        if (isset($conf_UAM_ConfirmMail[0]) and $conf_UAM_ConfirmMail[0] == 'true') // [Kdays] replacement only if related option is active
1257        {
1258          $patterns[] = '#\[Kdays\]#i';
1259          $replacements[] = $conf_UAM_ConfirmMail[1];
1260        }
[9160]1261       
[5056]1262        if (function_exists('get_user_language_desc'))
1263        {
[9160]1264          $infos1 = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[4]))."\n\n";
[5056]1265        }
[9160]1266        else $infos1 = l10n(preg_replace($patterns, $replacements, $conf_UAM_ConfirmMail[4]))."\n\n";
[5056]1267      }
1268     
[12271]1269// Set reminder true
1270// -----------------
[5056]1271      $query = "
1272UPDATE ".USER_CONFIRM_MAIL_TABLE."
1273SET reminder = 'true'
1274WHERE user_id = '".$user_id."'
1275;";
1276      pwg_query($query);
1277     
1278    break;
1279        }
1280 
1281  pwg_mail($email, array(
1282    'subject' => $subject,
1283    'content' => ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
1284  ));
1285
[7968]1286// Switching back to default language
[12271]1287// ----------------------------------
[5056]1288switch_lang_back();
1289}
1290
[10144]1291
[7968]1292/**
1293 * Function called from UAM_admin.php to send a reminder mail for ghost users
1294 *
1295 * @param : User id, username, email address
1296 *
1297 */
[5056]1298function ghostreminder($user_id, $username, $email)
1299{
[5064]1300  global $conf;
[5056]1301
[5181]1302  $conf_UAM = unserialize($conf['UserAdvManager']);
[14729]1303  $subject = "";
[5056]1304 
1305        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
1306
[7968]1307// We have to get the user's language in database
[12271]1308// ----------------------------------------------
[5056]1309  $query ='
1310SELECT user_id, language
1311FROM '.USER_INFOS_TABLE.'
1312WHERE user_id = '.$user_id.'
1313;';
[5633]1314  $data = pwg_db_fetch_assoc(pwg_query($query));
[5056]1315  $language = $data['language'];
1316
[7968]1317// And switch gallery to this language before using personalized and multilangual contents
[12271]1318// ---------------------------------------------------------------------------------------
[5056]1319  switch_lang_to($data['language']);
1320   
[5181]1321  load_language('plugin.lang', UAM_PATH);
[5056]1322
[14729]1323  if (isset($conf_UAM[45]) and $conf_UAM[45] <> '')
1324  {
1325    // Management of Extension flags ([username], [mygallery])
1326    // -------------------------------------------------------
1327    $patterns[] = '#\[username\]#i';
1328    $replacements[] = $username;
1329    $patterns[] = '#\[mygallery\]#i';
1330    $replacements[] = $conf['gallery_title'];
1331
1332    if (function_exists('get_user_language_desc'))
1333    {
1334      $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[45]))."\n\n";
1335    }
1336    else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[45]))."\n\n"; 
1337  }
1338
[11018]1339  if (isset($conf_UAM[17]) and $conf_UAM[17] <> '' and isset($conf_UAM[15]) and $conf_UAM[15] == 'true')
[5056]1340  {
[11255]1341    // Management of Extension flags ([username], [mygallery], [myurl], [days])
[12271]1342    // ------------------------------------------------------------------------
[9160]1343    $patterns[] = '#\[username\]#i';
1344    $replacements[] = $username;
1345    $patterns[] = '#\[mygallery\]#i';
1346    $replacements[] = $conf['gallery_title'];
[9197]1347    $patterns[] = '#\[myurl\]#i';
[14651]1348    $replacements[] = get_gallery_home_url();
[11255]1349    $patterns[] = '#\[days\]#i';
1350    $replacements[] = $conf_UAM[16];
[9160]1351
[5056]1352    if (function_exists('get_user_language_desc'))
1353    {
[11018]1354      $infos1 = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[17]))."\n\n";
[5056]1355    }
1356    else
1357    {
[11018]1358      $infos1 = l10n(preg_replace($patterns, $replacements, $conf_UAM[17]))."\n\n";
[5056]1359    }
1360
1361    resetlastvisit($user_id);
1362  }
1363
1364  pwg_mail($email, array(
1365    'subject' => $subject,
1366    'content' => $infos1.get_absolute_root_url(),
1367  ));
1368
[7968]1369// Switching back to default language
[12271]1370// ----------------------------------
[5056]1371switch_lang_back();
1372}
1373
[10144]1374
[7968]1375/**
[8065]1376 * Function called from functions.inc.php to send notification email when user have been downgraded
1377 *
[8092]1378 * @param : user id, username, email address
[8065]1379 *
1380 */
1381function demotion_mail($id, $username, $email)
1382{
1383  global $conf;
1384
1385  $conf_UAM = unserialize($conf['UserAdvManager']);
1386 
1387        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
1388 
1389        $custom_txt = "";
[14729]1390  $subject = "";
[8065]1391
1392// We have to get the user's language in database
[12271]1393// ----------------------------------------------
[8065]1394  $query ='
1395SELECT user_id, language
1396FROM '.USER_INFOS_TABLE.'
1397WHERE user_id = '.$id.'
1398;';
1399  $data = pwg_db_fetch_assoc(pwg_query($query));
1400
1401// Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language
[12271]1402// -------------------------------------------------------------------------------------------------------------------------------
[8065]1403  if (empty($data))
1404  {
1405// And switch gallery to this language before using personalized and multilangual contents
[12271]1406// ---------------------------------------------------------------------------------------
[8065]1407    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
1408    switch_lang_to($language);
1409  }
1410  else
1411  {
1412// And switch gallery to this language before using personalized and multilangual contents
[12271]1413// ---------------------------------------------------------------------------------------
[8065]1414    $language = $data['language']; // Usefull for debugging
1415    switch_lang_to($data['language']);
1416    load_language('plugin.lang', UAM_PATH);
1417  }
1418
[14729]1419  if (isset($conf_UAM[44]) and $conf_UAM[44] <> '')
1420  {
1421    // Management of Extension flags ([username], [mygallery])
1422    // -------------------------------------------------------
1423    $patterns[] = '#\[username\]#i';
1424    $replacements[] = $username;
1425    $patterns[] = '#\[mygallery\]#i';
1426    $replacements[] = $conf['gallery_title'];
1427
1428    if (function_exists('get_user_language_desc'))
1429    {
1430      $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[44]))."\n\n";
1431    }
1432    else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[44]))."\n\n"; 
1433  }
[8065]1434     
[11018]1435  if (isset($conf_UAM[24]) and $conf_UAM[24] <> '')
[8065]1436  {
[9197]1437    // Management of Extension flags ([username], [mygallery], [myurl])
[12271]1438    // ----------------------------------------------------------------
[9160]1439    $patterns[] = '#\[username\]#i';
[9253]1440    $replacements[] = stripslashes($username);
[9160]1441    $patterns[] = '#\[mygallery\]#i';
1442    $replacements[] = $conf['gallery_title'];
[9197]1443    $patterns[] = '#\[myurl\]#i';
[14651]1444    $replacements[] = get_gallery_home_url();
[9160]1445
[8065]1446    if (function_exists('get_user_language_desc'))
1447    {
[11018]1448      $custom_txt = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[24]))."\n\n";
[8065]1449    }
[11018]1450    else $custom_txt = l10n(preg_replace($patterns, $replacements, $conf_UAM[24]))."\n\n"; 
[8065]1451  }
1452
1453  $infos1 = array(
[9177]1454    get_l10n_args('UAM_User: %s', stripslashes($username)),
[8065]1455    get_l10n_args('Email: %s', $email),
1456    get_l10n_args('', ''),
1457  );
1458
1459  $infos2 = array
1460  (
[9177]1461    get_l10n_args('UAM_Link: %s', ResetConfirmMail($id)),
[8065]1462    get_l10n_args('', ''),
1463  ); 
1464
[9253]1465  resetlastvisit($id);
[8065]1466
1467// Sending the email with subject and contents
[12271]1468// -------------------------------------------
[8065]1469  pwg_mail($email, array(
1470    'subject' => $subject,
1471    'content' => ($custom_txt.l10n_args($infos1)."\n\n".l10n_args($infos2)."\n\n").get_absolute_root_url(),
1472  ));
1473
1474// Switching back to default language
[12271]1475// ----------------------------------
[8065]1476switch_lang_back();
1477}
1478
[10144]1479
[8065]1480/**
[8092]1481 * Function called from UAM_admin.php to send notification email when user registration have been manually validated by admin
1482 *
1483 * @param : user id
1484 *
1485 */
1486function validation_mail($id)
1487{
1488  global $conf;
1489
1490  $conf_UAM = unserialize($conf['UserAdvManager']);
1491 
1492        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
1493 
1494        $custom_txt = "";
[14729]1495  $subject = "";
[8092]1496
1497// We have to get the user's language in database
[12271]1498// ----------------------------------------------
[8092]1499  $query ='
1500SELECT user_id, language
1501FROM '.USER_INFOS_TABLE.'
1502WHERE user_id = '.$id.'
1503;';
1504  $data = pwg_db_fetch_assoc(pwg_query($query));
1505
1506// Check if user is already registered (profile changing) - If not (new registration), language is set to current gallery language
[12271]1507// -------------------------------------------------------------------------------------------------------------------------------
[8092]1508  if (empty($data))
1509  {
1510// And switch gallery to this language before using personalized and multilangual contents
[12271]1511// ---------------------------------------------------------------------------------------
[8092]1512    $language = pwg_get_session_var( 'lang_switch', $user['language'] );
1513    switch_lang_to($language);
1514  }
1515  else
1516  {
1517// And switch gallery to this language before using personalized and multilangual contents
[12271]1518// ---------------------------------------------------------------------------------------
[8092]1519    $language = $data['language']; // Usefull for debugging
1520    switch_lang_to($data['language']);
1521    load_language('plugin.lang', UAM_PATH);
1522  }
1523
1524// Retreive users email and user name from id
[12271]1525// ------------------------------------------
[8092]1526  $query ='
1527SELECT id, username, mail_address
1528FROM '.USERS_TABLE.'
1529WHERE id = '.$id.'
1530;';
1531  $result = pwg_db_fetch_assoc(pwg_query($query));
1532
[14729]1533  if (isset($conf_UAM[46]) and $conf_UAM[46] <> '')
1534  {
1535    // Management of Extension flags ([username], [mygallery])
1536    // -------------------------------------------------------
1537    $patterns[] = '#\[username\]#i';
[17804]1538    $replacements[] = $result['username'];
[14729]1539    $patterns[] = '#\[mygallery\]#i';
1540    $replacements[] = $conf['gallery_title'];
1541
1542    if (function_exists('get_user_language_desc'))
1543    {
1544      $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[46]))."\n\n";
1545    }
1546    else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[46]))."\n\n"; 
1547  }
[8092]1548     
[11018]1549  if (isset($conf_UAM[27]) and $conf_UAM[27] <> '')
[8092]1550  {
[9197]1551    // Management of Extension flags ([username], [mygallery], [myurl])
[12271]1552    // ----------------------------------------------------------------
[9160]1553    $patterns[] = '#\[username\]#i';
[9253]1554    $replacements[] = $result['username'];
[9160]1555    $patterns[] = '#\[mygallery\]#i';
1556    $replacements[] = $conf['gallery_title'];
[9197]1557    $patterns[] = '#\[myurl\]#i';
[14651]1558    $replacements[] = get_gallery_home_url();
[9160]1559
[8092]1560    if (function_exists('get_user_language_desc'))
1561    {
[11018]1562      $custom_txt = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[27]))."\n\n";
[8092]1563    }
[11018]1564    else $custom_txt = l10n(preg_replace($patterns, $replacements, $conf_UAM[27]))."\n\n"; 
[8092]1565  }
1566
1567  $infos = array(
[9177]1568    get_l10n_args('UAM_User: %s', stripslashes($result['username'])),
[8092]1569    get_l10n_args('Email: %s', $result['mail_address']),
1570    get_l10n_args('', ''),
1571  );
1572
1573// Sending the email with subject and contents
[12271]1574// -------------------------------------------
[8092]1575  pwg_mail($result['mail_address'], array(
1576    'subject' => $subject,
1577    'content' => (l10n_args($infos)."\n\n".$custom_txt),
1578  ));
1579
1580// Switching back to default language
[12271]1581// ----------------------------------
[8092]1582switch_lang_back();
1583}
1584
[10144]1585
[8092]1586/**
[7968]1587 * Function called from functions AddConfirmMail and ResetConfirmMail for validation key generation
1588 *
1589 * @return : validation key
1590 *
1591 */
[5056]1592function FindAvailableConfirmMailID()
1593{
1594  while (true)
1595  {
1596    $id = generate_key(16);
1597    $query = "
1598SELECT COUNT(*)
1599  FROM ".USER_CONFIRM_MAIL_TABLE."
1600WHERE id = '".$id."'
1601;";
[5633]1602    list($count) = pwg_db_fetch_row(pwg_query($query));
[5056]1603
1604    if ($count == 0)
1605      return $id;
1606  }
1607}
1608
[10144]1609
[7968]1610/**
1611 * Function called from functions SendMail2User to process unvalidated users and generate validation key link
1612 *
1613 * @param : User id, email address
1614 *
1615 * @return : Build validation key in URL
1616 *
1617 */
[5056]1618function AddConfirmMail($user_id, $email)
1619{
1620  global $conf;
1621
[5181]1622  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]1623  $Confirm_Mail_ID = FindAvailableConfirmMailID();
1624
[5633]1625  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[5056]1626 
1627  if (isset($Confirm_Mail_ID))
1628  {
1629    $query = "
1630SELECT status
1631  FROM ".USER_INFOS_TABLE."
1632WHERE user_id = '".$user_id."'
1633;";
[5633]1634    list($status) = pwg_db_fetch_row(pwg_query($query));
[5056]1635   
1636    $query = "
1637INSERT INTO ".USER_CONFIRM_MAIL_TABLE."
1638  (id, user_id, mail_address, status, date_check)
1639VALUES
1640  ('".$Confirm_Mail_ID."', '".$user_id."', '".$email."', '".$status."', null)
1641;";
1642    pwg_query($query);
1643
[8065]1644    // Delete user from all groups
[12271]1645    // ---------------------------
[5056]1646    $query = "
1647DELETE FROM ".USER_GROUP_TABLE."
1648WHERE user_id = '".$user_id."'
1649  AND (
[6354]1650    group_id = '".$conf_UAM[2]."'
1651  OR
[5056]1652    group_id = '".$conf_UAM[3]."'
1653  )
1654;";
1655    pwg_query($query);
1656
[8065]1657    // Set user unvalidated status
[12271]1658    // ---------------------------
[11018]1659    if (!is_admin() and $conf_UAM[7] <> -1)
[5056]1660    {
1661      $query = "
1662UPDATE ".USER_INFOS_TABLE."
[11018]1663SET status = '".$conf_UAM[7]."'
[5056]1664WHERE user_id = '".$user_id."'
1665;";
1666      pwg_query($query);
1667    }
1668
[8065]1669    // Set user unvalidated group
[12271]1670    // --------------------------
[8065]1671    if (!is_admin() and $conf_UAM[2] <> -1)
[5056]1672    {
1673      $query = "
1674INSERT INTO ".USER_GROUP_TABLE."
1675  (user_id, group_id)
1676VALUES
[6354]1677  ('".$user_id."', '".$conf_UAM[2]."')
[5056]1678;";
1679      pwg_query($query);
1680    }
[12189]1681
1682    // Set user unvalidated privacy level
[12271]1683    // ----------------------------------
[12189]1684    if (!is_admin() and $conf_UAM[35] <> -1)
1685    {
1686      $query = "
1687UPDATE ".USER_INFOS_TABLE."
1688SET level = '".$conf_UAM[35]."'
1689WHERE user_id = '".$user_id."'
1690;";
1691      pwg_query($query);
1692    }
[5056]1693   
[6785]1694    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
[5056]1695  }
1696}
1697
[10144]1698
[7968]1699/**
[12189]1700 * Function called from UAM_Adduser() to set group/status/level to new users if manual validation is set
[7968]1701 *
1702 * @param : User id
1703 *
1704 *
1705 */
[12189]1706function SetPermission($user_id)
[6757]1707{
1708  global $conf;
1709 
1710  $conf_UAM = unserialize($conf['UserAdvManager']);
1711
[12189]1712// Groups cleanup
[12271]1713// --------------
[6757]1714  $query = "
1715DELETE FROM ".USER_GROUP_TABLE."
1716WHERE user_id = '".$user_id."'
1717  AND (
1718    group_id = '".$conf_UAM[2]."'
1719  OR
1720    group_id = '".$conf_UAM[3]."'
1721  )
1722;";
1723  pwg_query($query);
1724
[12189]1725  if (!is_admin() and $conf_UAM[7] <> -1) // Set status
[6757]1726  {
1727    $query = "
1728UPDATE ".USER_INFOS_TABLE."
[11018]1729SET status = '".$conf_UAM[7]."'
[6757]1730WHERE user_id = '".$user_id."'
1731;";
1732    pwg_query($query);
1733  }
1734
[12189]1735  if (!is_admin() and $conf_UAM[2] <> -1) // Set group
[6757]1736  {
1737    $query = "
1738INSERT INTO ".USER_GROUP_TABLE."
1739  (user_id, group_id)
1740VALUES
1741  ('".$user_id."', '".$conf_UAM[2]."')
1742;";
1743    pwg_query($query);
1744  }
[12189]1745
[13822]1746  if (!is_admin() and $conf_UAM[35] <> -1) // Set privacy level
[12189]1747  {
1748    $query = "
1749INSERT INTO ".USER_INFOS_TABLE."
1750  (user_id, level)
1751VALUES
[13822]1752  ('".$user_id."', '".$conf_UAM[35]."')
[12189]1753;";
1754    pwg_query($query);
1755  }
[6757]1756}
1757
[10144]1758
[7968]1759/**
1760 * Function called from UAM_admin.php to reset validation key
1761 *
1762 * @param : User id
1763 *
1764 * @return : Build validation key in URL
1765 *
1766 */
[5056]1767function ResetConfirmMail($user_id)
1768{
1769  global $conf;
1770 
1771  $Confirm_Mail_ID = FindAvailableConfirmMailID();
1772
[5633]1773  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[5056]1774 
1775  if (isset($Confirm_Mail_ID))
1776  { 
1777    $query = "
1778UPDATE ".USER_CONFIRM_MAIL_TABLE."
1779SET id = '".$Confirm_Mail_ID."'
1780WHERE user_id = '".$user_id."'
1781;";
1782    pwg_query($query);
1783
1784                $query = "
1785UPDATE ".USER_INFOS_TABLE."
1786SET registration_date = '".$dbnow."'
1787WHERE user_id = '".$user_id."'
1788;";
1789                pwg_query($query);
1790   
[6785]1791    return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
[5056]1792  }
1793}
1794
[10144]1795
[7968]1796/**
1797 * Function called from functions.inc.php to reset last visit date after sending a reminder
1798 *
1799 * @param : User id
1800 *
1801 */
[5056]1802function resetlastvisit($user_id)
1803{
1804  global $conf;
1805
[5633]1806  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[5056]1807
1808  $query = "
1809UPDATE ".USER_LASTVISIT_TABLE."
1810SET lastvisit = '".$dbnow."', reminder = 'true'
1811WHERE user_id = '".$user_id."'
1812;";
1813  pwg_query($query);
1814}
1815
1816
[7968]1817/**
1818 * Function called from main.inc.php - Triggered on user deletion
1819 *
1820 */
[5056]1821function DeleteConfirmMail($user_id)
1822{
1823  $query = "
1824DELETE FROM ".USER_CONFIRM_MAIL_TABLE."
1825WHERE user_id = '".$user_id."'
1826;";
1827  pwg_query($query);
1828}
1829
[7968]1830/**
1831 * Function called from main.inc.php - Triggered on user deletion
1832 *
1833 */
[5056]1834function DeleteLastVisit($user_id)
1835{
1836  $query = "
1837DELETE FROM ".USER_LASTVISIT_TABLE."
1838WHERE user_id = '".$user_id."'
1839;";
1840  pwg_query($query);
1841}
1842
[10144]1843
[7968]1844/**
1845 * Function called from main.inc.php - Triggered on user deletion
1846 *
1847 * @param : User id
1848 *
1849 */
[6775]1850function DeleteRedir($user_id)
1851{
1852  $tab = array();
1853
[6785]1854  $query = '
[6775]1855SELECT value
[6785]1856FROM '.CONFIG_TABLE.'
1857WHERE param = "UserAdvManager_Redir"
1858;';
[6775]1859
1860  $tab = pwg_db_fetch_row(pwg_query($query));
1861 
1862  $values = explode(',', $tab[0]);
1863
1864  unset($values[array_search($user_id, $values)]);
1865     
1866  $query = "
1867UPDATE ".CONFIG_TABLE."
1868SET value = \"".implode(',', $values)."\"
1869WHERE param = 'UserAdvManager_Redir';";
1870
1871  pwg_query($query);
1872}
1873
[10144]1874
[7968]1875/**
1876 * Function called from ConfirmMail.php to verify validation key used by user according time limit
1877 * Return true is key validation is OK else return false
1878 *
1879 * @param : User id
1880 *
1881 * @return : Bool
1882 *
1883 */
[5056]1884function VerifyConfirmMail($id)
1885{
1886  global $conf;
1887
1888  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
1889 
[5181]1890  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]1891
[5181]1892  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
[5056]1893
1894  $query = "
1895SELECT COUNT(*)
1896FROM ".USER_CONFIRM_MAIL_TABLE."
1897WHERE id = '".$id."'
1898;";
[5633]1899  list($count) = pwg_db_fetch_row(pwg_query($query));
[5056]1900
1901  if ($count == 1)
1902  {
1903    $query = "
1904SELECT user_id, status, date_check
1905FROM ".USER_CONFIRM_MAIL_TABLE."
1906WHERE id = '".$id."'
1907;";
[5633]1908    $data = pwg_db_fetch_assoc(pwg_query($query));
[9266]1909
[12227]1910    if (!empty($data) and isset($data['user_id']) and is_null($data['date_check']))
[5056]1911    {
1912      $query = "
1913SELECT registration_date
1914FROM ".USER_INFOS_TABLE."
1915WHERE user_id = '".$data['user_id']."'
1916;";
[5633]1917      list($registration_date) = pwg_db_fetch_row(pwg_query($query));
[5056]1918
[7968]1919//              Time limit process             
1920// ******************************************** 
[5056]1921      if (!empty($registration_date))
1922      {
1923                                // Verify Confirmmail with time limit ON
[12271]1924        // -------------------------------------
[5056]1925                                if (isset ($conf_UAM_ConfirmMail[1]))
1926                                {
[12271]1927                                        // Dates formating and compare
1928          // ---------------------------
[5056]1929                                        $today = date("d-m-Y"); // Get today's date
1930                                        list($day, $month, $year) = explode('-', $today); // explode date of today                                               
1931                                        $daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
1932                       
1933                                list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
1934                                        list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
1935                                        $regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
1936                       
1937                                        $deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps     
1938                                        $deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
1939
1940                                        // Condition with the value set for time limit
[12271]1941          // -------------------------------------------
[5056]1942                                        if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
1943                                        {
[5633]1944                                                list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[5056]1945
[12271]1946            // Update ConfirmMail table
1947            // ------------------------
[5056]1948                                                $query = '
1949UPDATE '.USER_CONFIRM_MAIL_TABLE.'
[9266]1950SET date_check="'.$dbnow.'", reminder="false"
[5056]1951WHERE id = "'.$id.'"
1952;';
1953                                                pwg_query($query);
[12189]1954
1955            // Update LastVisit table - Force reminder field to false
[12271]1956            // Usefull when a user has been automatically downgraded and revalidate its registration
1957            // -------------------------------------------------------------------------------------
[12189]1958                                                $query = '
1959UPDATE '.USER_LASTVISIT_TABLE.'
1960SET reminder="false"
1961WHERE user_id = "'.$data['user_id'].'"
1962;';
1963                                                pwg_query($query);
[5056]1964     
[6354]1965                                                if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
[5056]1966                                                {
1967                                                        $query = "
1968DELETE FROM ".USER_GROUP_TABLE."
1969WHERE user_id = '".$data['user_id']."'
[6354]1970  AND group_id = '".$conf_UAM[2]."'
[5056]1971;";
1972                                                        pwg_query($query);
1973                                                }
1974           
[6354]1975                                                if ($conf_UAM[3] <> -1) // Add user to validated users group
[5056]1976                                                {
1977                                                        $query = "
1978INSERT INTO ".USER_GROUP_TABLE."
1979  (user_id, group_id)
1980VALUES
[6354]1981  ('".$data['user_id']."', '".$conf_UAM[3]."')
[5056]1982;";
1983                                                        pwg_query($query);
1984                                                }
1985
[6354]1986                                                if (($conf_UAM[4] <> -1 or isset($data['status']))) // Change user's status
[5056]1987                                                {
1988                                                        $query = "
1989UPDATE ".USER_INFOS_TABLE."
[6354]1990SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
[5056]1991WHERE user_id = '".$data['user_id']."'
1992;";
1993                                                        pwg_query($query);
1994                                                }
[12189]1995
1996                                                if (($conf_UAM[36] <> -1 or isset($data['level']))) // Change user's privacy level
1997                                                {
1998                                                        $query = "
1999UPDATE ".USER_INFOS_TABLE."
2000SET level = '".(isset($data['level']) ? $data['level'] : $conf_UAM[36])."'
2001WHERE user_id = '".$data['user_id']."'
2002;";
2003                                                        pwg_query($query);
2004                                                }
2005
[5056]2006                                                // Refresh user's category cache
[12271]2007            // -----------------------------
[5056]2008                                                invalidate_user_cache();
2009 
2010                                                return true;
2011                                        }
2012                                        elseif ($deltadays > $conf_UAM_ConfirmMail[1]) // If timelimit exeeds
2013                                        {
2014                                                return false;
2015                                        }
2016                                }
2017                                // Verify Confirmmail with time limit OFF
[12271]2018        // --------------------------------------
[5056]2019                                else
2020                                {
[5633]2021                                        list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[5056]2022
[12271]2023          // Update ConfirmMail table
2024          // ------------------------
[5056]2025                                        $query = '
2026UPDATE '.USER_CONFIRM_MAIL_TABLE.'
2027SET date_check="'.$dbnow.'"
2028WHERE id = "'.$id.'"
2029;';
2030                                        pwg_query($query);
[12189]2031
2032          // Update LastVisit table - Force reminder field to false
[12271]2033          // Usefull when a user has been automatically downgraded and revalidate its registration
2034          // -------------------------------------------------------------------------------------
[12189]2035                                        $query = '
2036UPDATE '.USER_LASTVISIT_TABLE.'
2037SET reminder="false"
2038WHERE user_id = "'.$data['user_id'].'"
2039;';
2040          pwg_query($query);
[5056]2041     
[12189]2042                                        if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
[5056]2043                                        {
2044                                                $query = "
2045DELETE FROM ".USER_GROUP_TABLE."
2046WHERE user_id = '".$data['user_id']."'
[6354]2047AND group_id = '".$conf_UAM[2]."'
[5056]2048;";
2049                                                pwg_query($query);
2050                                        }
2051   
[6354]2052                                        if ($conf_UAM[3] <> -1)
[5056]2053                                        {
2054                                                $query = "
2055DELETE FROM ".USER_GROUP_TABLE."
2056WHERE user_id = '".$data['user_id']."'
[6354]2057AND group_id = '".$conf_UAM[3]."'
[5056]2058;";
2059                                                pwg_query($query);
2060
2061                                                $query = "
2062INSERT INTO ".USER_GROUP_TABLE."
2063  (user_id, group_id)
2064VALUES
[6354]2065  ('".$data['user_id']."', '".$conf_UAM[3]."')
[5056]2066;";
2067                                                pwg_query($query);
2068                                        }
2069
[12189]2070                                        if (($conf_UAM[4] <> -1 or isset($data['status']))) // Change user's status
[5056]2071                                        {
2072                                                $query = "
2073UPDATE ".USER_INFOS_TABLE."
[6354]2074SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
[5056]2075WHERE user_id = '".$data['user_id']."'
2076;";
2077                                                pwg_query($query);
2078                                        }
[12189]2079
2080                                        if (($conf_UAM[36] <> -1 or isset($data['level']))) // Change user's privacy level
2081                                        {
2082                                                $query = "
2083UPDATE ".USER_INFOS_TABLE."
2084SET level = '".(isset($data['level']) ? $data['level'] : $conf_UAM[36])."'
2085WHERE user_id = '".$data['user_id']."'
2086;";
2087                                                pwg_query($query);
2088                                        }
2089
[5056]2090                                        // Refresh user's category cache
[12271]2091          // -----------------------------
[5056]2092                                        invalidate_user_cache();
2093 
2094                                        return true;
2095                                }
2096                        }
2097                }
[12227]2098    else if (!empty($data) and !is_null($data['date_check']))
2099    {
2100      return false;
2101    }
[5056]2102        }
2103  else
2104    return false;
2105}
2106
[10144]2107
[7968]2108/**
2109 * Function called from UAM_admin.php to force users validation by admin
2110 *
2111 * @param : User id
2112 *
2113 */
[5056]2114function ForceValidation($id)
2115{
2116  global $conf;
2117
[5181]2118  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]2119
[8072]2120  if (isset($conf_UAM[1]) and $conf_UAM[1] == 'true')
[5056]2121  {
[8072]2122    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[5056]2123
[8072]2124                $query = "
[5056]2125UPDATE ".USER_CONFIRM_MAIL_TABLE."
2126SET date_check='".$dbnow."'
[8072]2127WHERE user_id = '".$id."'
[5056]2128;";
[8072]2129    pwg_query($query);
[5056]2130             
[8072]2131                if ($conf_UAM[2] <> -1)
2132                {
2133                        $query = "
[5056]2134DELETE FROM ".USER_GROUP_TABLE."
[8072]2135WHERE user_id = '".$id."'
[6354]2136  AND group_id = '".$conf_UAM[2]."'
[5056]2137;";
[8072]2138                        pwg_query($query);
2139                }
[5056]2140 
[12189]2141                if ($conf_UAM[3] <> -1) // Change user's group
[8072]2142                {
2143                        $query = "
[5056]2144DELETE FROM ".USER_GROUP_TABLE."
[8072]2145WHERE user_id = '".$id."'
[6354]2146  AND group_id = '".$conf_UAM[3]."'
[8072]2147;";
2148      pwg_query($query);
[5056]2149       
[8072]2150                        $query = "
[5056]2151INSERT INTO ".USER_GROUP_TABLE."
2152  (user_id, group_id)
2153VALUES
[8072]2154  ('".$id."', '".$conf_UAM[3]."')
[5056]2155;";
[8072]2156                        pwg_query($query);
2157    }
[5056]2158
[12189]2159                if ($conf_UAM[4] <> -1) // Change user's status
[8072]2160                {
2161                        $query = "
[5056]2162UPDATE ".USER_INFOS_TABLE."
[8072]2163SET status = '".$conf_UAM[4]."'
2164WHERE user_id = '".$id."'
[5056]2165;";
[8072]2166                        pwg_query($query);
[5056]2167                }
[12189]2168
2169                if ($conf_UAM[36] <> -1) // Change user's privacy level
2170                {
2171                        $query = "
2172UPDATE ".USER_INFOS_TABLE."
2173SET level = '".$conf_UAM[36]."'
2174WHERE user_id = '".$id."'
2175;";
2176                        pwg_query($query);
2177                }
[8072]2178  }
2179  elseif (isset($conf_UAM[1]) and $conf_UAM[1] == 'local')
2180  {
2181    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
2182
[12189]2183    if ($conf_UAM[2] <> -1) // Delete user's from waiting group
[8072]2184    {
2185                  $query = "
2186DELETE FROM ".USER_GROUP_TABLE."
2187WHERE user_id = '".$id."'
2188  AND group_id = '".$conf_UAM[2]."'
2189;";
2190                  pwg_query($query);
2191    }
2192
[12189]2193    if ($conf_UAM[3] <> -1) // Change user's group
[8072]2194    {
2195      $query = "
2196DELETE FROM ".USER_GROUP_TABLE."
2197WHERE user_id = '".$id."'
2198  AND group_id = '".$conf_UAM[3]."'
2199;";
2200      pwg_query($query);
2201       
2202                  $query = "
2203INSERT INTO ".USER_GROUP_TABLE."
2204  (user_id, group_id)
2205VALUES
2206  ('".$id."', '".$conf_UAM[3]."')
2207;";
2208                  pwg_query($query);
2209    }
2210
[12189]2211    if ($conf_UAM[4] <> -1) // Change user's status
[8072]2212    {
2213                  $query = "
2214UPDATE ".USER_INFOS_TABLE."
2215SET status = '".$conf_UAM[4]."'
2216WHERE user_id = '".$id."'
2217;";
2218      pwg_query($query);
2219    }
[12189]2220
2221    if ($conf_UAM[36] <> -1) // Change user's privacy level
2222    {
2223                  $query = "
2224UPDATE ".USER_INFOS_TABLE."
2225SET level = '".$conf_UAM[36]."'
2226WHERE user_id = '".$id."'
2227;";
2228      pwg_query($query);
2229    }
[8072]2230  }
[5056]2231}
2232
[10144]2233
[7968]2234/**
[12661]2235 * Function called from functions.inc.php - Check if username matches forbidden caracters
[7968]2236 *
2237 * @param : User login
2238 *
2239 * @return : Bool
2240 *
2241 */
[5056]2242function ValidateUsername($login)
2243{
2244  global $conf;
2245
[5181]2246  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]2247
[11018]2248  if (isset($login) and isset($conf_UAM[6]) and $conf_UAM[6] <> '')
[5056]2249  {
[11018]2250    $conf_CharExclusion = preg_split("/,/",$conf_UAM[6]);
[5181]2251    for ($i = 0 ; $i < count($conf_CharExclusion) ; $i++)
[5056]2252    {
[5181]2253      $pattern = '/'.$conf_CharExclusion[$i].'/i';
[5056]2254      if (preg_match($pattern, $login))
2255      {
2256        return true;
2257      }
2258    }
2259  }
2260  else
2261  {
2262    return false;
2263  }
2264}
2265
[10144]2266
[7968]2267/**
2268 * Function called from main.inc.php - Check if user's email is in excluded email providers list
2269 * Doesn't work on call - Must be copied in main.inc.php to work
2270 *
2271 * @param : Email address
2272 *
2273 * @return : Bool
2274 *
2275 */
[5056]2276function ValidateEmailProvider($email)
2277{
2278  global $conf;
2279
[5181]2280  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]2281 
[11018]2282        if (isset($email) and isset($conf_UAM[11]) and $conf_UAM[11] <> '')
[5056]2283        {
[11018]2284                $conf_MailExclusion = preg_split("/[\s,]+/",$conf_UAM[11]);
[5181]2285                for ($i = 0 ; $i < count($conf_MailExclusion) ; $i++)
[5056]2286                {
[5181]2287                        $pattern = '/'.$conf_MailExclusion[$i].'/i';
[5056]2288                        if (preg_match($pattern, $email))
2289      {
2290                        return true;
2291      }
2292                }
2293        }
2294  else
2295  {
2296    return false;
2297  }
2298}
2299
[10144]2300
[7968]2301/**
2302 * Function called from UAM_admin.php - Get unvalidated users according time limit
2303 *
2304 * @return : List of users
2305 *
2306 */
[5056]2307function get_unvalid_user_list()
2308{
2309        global $conf, $page;
2310         
[7968]2311        // Get ConfirmMail configuration
[12271]2312  // -----------------------------
[5181]2313  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
[7968]2314  // Get UAM configuration
[12271]2315  // ---------------------
[5181]2316  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]2317 
2318  $users = array();
2319
[12271]2320        // Search users depending expiration date
2321  // --------------------------------------
[5056]2322  $query = '
2323SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2324                u.'.$conf['user_fields']['username'].' AS username,
2325                u.'.$conf['user_fields']['email'].' AS email,
2326                ui.status,
2327                ui.enabled_high,
2328                ui.level,
2329                ui.registration_date
2330FROM '.USERS_TABLE.' AS u
2331  INNER JOIN '.USER_INFOS_TABLE.' AS ui
2332    ON u.'.$conf['user_fields']['id'].' = ui.user_id
2333  LEFT JOIN '.USER_GROUP_TABLE.' AS ug
2334    ON u.'.$conf['user_fields']['id'].' = ug.user_id
2335WHERE u.'.$conf['user_fields']['id'].' >= 3
2336  AND (TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) >= "'.$conf_UAM_ConfirmMail[1].'"
2337  OR TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) < "'.$conf_UAM_ConfirmMail[1].'")';
2338
[11018]2339        if ($conf_UAM[2] <> '-1' and $conf_UAM[7] == '-1')
[5056]2340  {
2341    $query.= '
[6354]2342  AND ug.group_id = '.$conf_UAM[2];
[5056]2343  }
[11018]2344  if ($conf_UAM[2] == '-1' and $conf_UAM[7] <> '-1')
[5056]2345  {
2346    $query.= '
[11018]2347  AND ui.status = \''.$conf_UAM[7]."'";
[5056]2348  }
[11018]2349  if ($conf_UAM[2] <> '-1' and $conf_UAM[7] <> '-1')
[5056]2350  {
2351    $query.= '
[6354]2352  AND ug.group_id = \''.$conf_UAM[2]."'";
[5056]2353  }
2354  $query.= '
[6399]2355ORDER BY ui.registration_date ASC
[5056]2356;';
2357
2358        $result = pwg_query($query);
2359     
[5633]2360  while ($row = pwg_db_fetch_assoc($result))
[5056]2361  {
2362        $user = $row;
2363    $user['groups'] = array();
2364
2365    array_push($users, $user);
2366        }
2367
[12271]2368        // Add groups list
2369  // ---------------
[5056]2370  $user_ids = array();
2371  foreach ($users as $i => $user)
2372  {
2373        $user_ids[$i] = $user['id'];
2374        }
2375       
2376        $user_nums = array_flip($user_ids);
2377
2378  if (count($user_ids) > 0)
2379  {
2380        $query = '
2381SELECT user_id, group_id
2382  FROM '.USER_GROUP_TABLE.'
2383WHERE user_id IN ('.implode(',', $user_ids).')
2384;';
2385       
2386                $result = pwg_query($query);
2387       
[5633]2388    while ($row = pwg_db_fetch_assoc($result))
[5056]2389    {
2390        array_push(
2391        $users[$user_nums[$row['user_id']]]['groups'],
2392        $row['group_id']
2393                        );
2394                }
2395        }
2396
2397        return $users;
2398}
2399
[10144]2400
[7968]2401/**
[9266]2402 * Function called from functions.inc.php - Get all users who haven't validate their registration in configured time
2403 * to delete or remail them automatically
2404 *
2405 * @return : List of users
2406 *
2407 */
2408function get_unvalid_user_autotasks()
2409{
2410        global $conf, $page;
2411         
2412        // Get ConfirmMail configuration
[12271]2413  // -----------------------------
[9266]2414  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
2415 
2416  $users = array();
2417
2418        // search users depending expiration date
[12271]2419  // --------------------------------------
[9266]2420  $query = '
2421SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2422                ui.registration_date
2423FROM '.USERS_TABLE.' AS u
2424  INNER JOIN '.USER_INFOS_TABLE.' AS ui
2425    ON u.'.$conf['user_fields']['id'].' = ui.user_id
2426WHERE u.'.$conf['user_fields']['id'].' >= 3
2427  AND (TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) >= "'.$conf_UAM_ConfirmMail[1].'")
2428ORDER BY ui.registration_date ASC;';
2429
2430        $result = pwg_query($query);
2431     
2432  while ($row = pwg_db_fetch_assoc($result))
2433  {
2434    array_push($users, $row);
2435        }
2436
2437        return $users;
2438}
2439
[10144]2440
[9266]2441/**
[7968]2442 * Function called from UAM_admin.php - Get ghost users
2443 *
2444 * @return : List of users
2445 *
2446 */
[5056]2447function get_ghost_user_list()
2448{
2449        global $conf, $page;
2450
[5181]2451  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]2452 
2453  $users = array();
2454
[12271]2455        // Search users depending expiration date
2456  // --------------------------------------
[5056]2457  $query = '
2458SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2459                u.'.$conf['user_fields']['username'].' AS username,
2460                u.'.$conf['user_fields']['email'].' AS email,
2461                lv.lastvisit,
2462                lv.reminder
2463FROM '.USERS_TABLE.' AS u
2464  INNER JOIN '.USER_LASTVISIT_TABLE.' AS lv
2465    ON u.'.$conf['user_fields']['id'].' = lv.user_id
[11018]2466WHERE (TO_DAYS(NOW()) - TO_DAYS(lv.lastvisit) >= "'.$conf_UAM[16].'")
[6399]2467ORDER BY lv.lastvisit ASC;';
[5056]2468
2469        $result = pwg_query($query);
2470     
[5633]2471  while ($row = pwg_db_fetch_assoc($result))
[5056]2472  {
2473        $user = $row;
2474    $user['groups'] = array();
2475
2476    array_push($users, $user);
2477        }
2478
[12271]2479        // Add groups list
2480  // ---------------
[5056]2481  $user_ids = array();
2482  foreach ($users as $i => $user)
2483  {
2484        $user_ids[$i] = $user['id'];
2485        }
2486
2487        return $users;
2488}
2489
[8065]2490
[7968]2491/**
[8065]2492 * Function called from functions.inc.php - Get all ghost users to delete or downgrade automatically on any user login
2493 *
2494 * @return : List of users to delete or downgrade automatically
2495 *
2496 */
2497function get_ghosts_autotasks()
2498{
2499        global $conf, $page;
2500
2501  $conf_UAM = unserialize($conf['UserAdvManager']);
2502 
2503  $users = array();
2504 
[12271]2505        // Search users depending expiration date and reminder sent
2506  // --------------------------------------------------------
[8065]2507  $query = '
2508SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
[8087]2509                lv.lastvisit
[8065]2510FROM '.USERS_TABLE.' AS u
2511  INNER JOIN '.USER_LASTVISIT_TABLE.' AS lv
2512    ON u.'.$conf['user_fields']['id'].' = lv.user_id
[11018]2513WHERE (TO_DAYS(NOW()) - TO_DAYS(lv.lastvisit) >= "'.$conf_UAM[16].'")
[8065]2514ORDER BY lv.lastvisit ASC;';
2515
2516        $result = pwg_query($query);
2517     
2518  while ($row = pwg_db_fetch_assoc($result))
2519  {
[9266]2520    array_push($users, $row);
[8065]2521        }
2522 
2523        return $users;
2524}
2525
2526
2527/**
[7968]2528 * Function called from UAM_admin.php - Get all users to display the number of days since their last visit
2529 *
2530 * @return : List of users
2531 *
2532 */
[5056]2533function get_user_list()
2534{
2535        global $conf, $page;
2536 
2537  $users = array();
2538
[13528]2539        // Search users depending expiration date with exclusion of Adult_Content generic users
2540  // ------------------------------------------------------------------------------------
[5056]2541  $query = '
2542SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
2543                u.'.$conf['user_fields']['username'].' AS username,
2544                u.'.$conf['user_fields']['email'].' AS email,
2545                ug.lastvisit
2546FROM '.USERS_TABLE.' AS u
2547  INNER JOIN '.USER_LASTVISIT_TABLE.' AS ug
2548    ON u.'.$conf['user_fields']['id'].' = ug.user_id
2549WHERE u.'.$conf['user_fields']['id'].' >= 3
[13528]2550  AND u.username NOT LIKE "16"
2551  AND u.username NOT LIKE "18"
[6399]2552ORDER BY ug.lastvisit DESC
[5056]2553;';
2554
2555        $result = pwg_query($query);
2556     
[5633]2557  while ($row = pwg_db_fetch_assoc($result))
[5056]2558  {
2559        $user = $row;
2560    $user['groups'] = array();
2561
2562    array_push($users, $user);
2563        }
2564
[12271]2565        // Add groups list
2566  // ---------------
[5056]2567  $user_ids = array();
2568  foreach ($users as $i => $user)
2569  {
2570        $user_ids[$i] = $user['id'];
2571        }
2572
2573        return $users;
2574}
2575
[10144]2576
[7968]2577/**
2578 * Function called from UAM_admin.php - to determine who is expired or not and giving a different display color
2579 *
2580 * @param : user id
2581 *
2582 * @return : Bool
2583 *
2584 */
[5056]2585function expiration($id)
2586{
2587        global $conf, $page;
2588         
[7968]2589        // Get ConfirmMail configuration
[12271]2590  // -----------------------------
[5181]2591  $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
[5056]2592         
[7968]2593        // Get UAM configuration
[12271]2594  // ---------------------
[5181]2595  $conf_UAM = unserialize($conf['UserAdvManager']);
[5056]2596       
2597        $query = "
2598SELECT registration_date
2599  FROM ".USER_INFOS_TABLE."
2600WHERE user_id = '".$id."'
2601;";
[5633]2602        list($registration_date) = pwg_db_fetch_row(pwg_query($query));
[5056]2603
[7968]2604//              Time limit process             
2605// ******************************************** 
[5056]2606        if (!empty($registration_date))
2607  {
[12271]2608                // Dates formating and compare
2609    // ---------------------------
[5056]2610                $today = date("d-m-Y"); // Get today's date
2611                list($day, $month, $year) = explode('-', $today); // explode date of today                                               
2612                $daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
2613               
2614          list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
2615                list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
2616                $regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
2617                       
2618                $deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps     
2619                $deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
2620
2621                // Condition with the value set for time limit
[12271]2622    // -------------------------------------------
[5056]2623                if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
2624                {
2625                        return false;
2626                }
2627                else
2628                {
[6754]2629                        return true;
[5056]2630                }
2631        }
2632}
2633
2634
2635/**
2636 * Returns a password's score for password complexity check
2637 *
[7968]2638 * @param : password filled by user
[5056]2639 *
[7968]2640 * @return : Score calculation
2641 *
[5056]2642 * Thanx to MathieuGut from http://m-gut.developpez.com
2643 */
2644function testpassword($password) // Le mot de passe passé en paramètre - $password given by user
2645{
2646
2647  // Initialisation des variables - Variables initiation
[12271]2648  // ---------------------------------------------------
[5056]2649  $points = 0;
2650  $point_lowercase = 0;
2651  $point_uppercase = 0;
2652  $point_numbers = 0;
2653  $point_characters = 0;
2654
[12271]2655  // On récupère la longueur du mot de passe - Getting password lengh
2656  // ----------------------------------------------------------------
[5056]2657  $length = strlen($password);
2658 
2659  // On fait une boucle pour lire chaque lettre - Loop to read password characters
2660  for($i = 0; $i < $length; $i++)
2661  {
2662    // On sélectionne une à une chaque lettre - Select each letters
2663    // $i étant à 0 lors du premier passage de la boucle - $i is 0 at first turn
[12271]2664    // -------------------------------------------------------------------------
[5056]2665    $letters = $password[$i];
2666
2667    if ($letters>='a' && $letters<='z')
2668    {
2669      // On ajoute 1 point pour une minuscule - Adding 1 point to score for a lowercase
[12271]2670      // ------------------------------------------------------------------------------
[5056]2671                  $points = $points + 1;
2672
2673                  // On rajoute le bonus pour une minuscule - Adding bonus points for lowercase
[12271]2674      // --------------------------------------------------------------------------
[5056]2675                  $point_lowercase = 1;
2676    }
2677    else if ($letters>='A' && $letters <='Z')
2678    {
2679      // On ajoute 2 points pour une majuscule - Adding 2 points to score for uppercase
[12271]2680      // ------------------------------------------------------------------------------
[5056]2681      $points = $points + 2;
2682               
2683      // On rajoute le bonus pour une majuscule - Adding bonus points for uppercase
[12271]2684      // --------------------------------------------------------------------------
[5056]2685      $point_uppercase = 2;
2686    }
2687    else if ($letters>='0' && $letters<='9')
2688    {
2689      // On ajoute 3 points pour un chiffre - Adding 3 points to score for numbers
[12271]2690      // -------------------------------------------------------------------------
[5056]2691      $points = $points + 3;
2692               
2693      // On rajoute le bonus pour un chiffre - Adding bonus points for numbers
[12271]2694      // ---------------------------------------------------------------------
[5056]2695      $point_numbers = 3;
2696    }
2697    else
2698    {
2699      // On ajoute 5 points pour un caractère autre - Adding 5 points to score for special characters
[12271]2700      // --------------------------------------------------------------------------------------------
[5056]2701      $points = $points + 5;
2702               
2703      // On rajoute le bonus pour un caractère autre - Adding bonus points for special characters
[12271]2704      // ----------------------------------------------------------------------------------------
[5056]2705      $point_characters = 5;
2706    }
2707  }
2708
2709  // Calcul du coefficient points/longueur - calculating the coefficient points/length
[12271]2710  // ---------------------------------------------------------------------------------
[5056]2711  $step1 = $points / $length;
2712
2713  // Calcul du coefficient de la diversité des types de caractères... - Calculation of the diversity of character types...
[12271]2714  // ---------------------------------------------------------------------------------------------------------------------
[5056]2715  $step2 = $point_lowercase + $point_uppercase + $point_numbers + $point_characters;
2716
2717  // Multiplication du coefficient de diversité avec celui de la longueur - Multiplying the coefficient of diversity with that of the length
[12271]2718  // --------------------------------------------------------------------------------------------------------------------------------------------
[5056]2719  $score = $step1 * $step2;
2720
[9253]2721  // Multiplication du resultat par la longueur de la chaine - Multiplying the result by the length of the string
[12271]2722  // ------------------------------------------------------------------------------------------------------------
[5056]2723  $finalscore = $score * $length;
2724
2725  return $finalscore;
2726}
2727
[10144]2728
[7968]2729/**
[8065]2730 * UAM_check_profile - Thx to LucMorizur
[7968]2731 * checks if a user id is registered as having already
[12661]2732 * visited his profile page.
[7968]2733 *
2734 * @uid        : the user id
2735 *
2736 * @user_idsOK : (returned) array of all users ids having already visited
2737 *               their profile.php pages
2738 *
2739 * @returns    : true or false whether the users has already visited his
2740 *               profile.php page or not
2741 *
2742 */
[8065]2743function UAM_check_profile($uid, &$user_idsOK)
[6775]2744{
2745  $t = array();
2746  $v = false;
2747 
[12239]2748  $query = '
[6775]2749SELECT value
[12239]2750FROM '.CONFIG_TABLE.'
2751WHERE param = "UserAdvManager_Redir"
2752;';
[6775]2753 
2754  if ($v = (($t = pwg_db_fetch_row(pwg_query($query))) !== false))
2755  {
2756    $user_idsOK = explode(',', $t[0]);
2757    $v = (in_array($uid, $user_idsOK));
2758  }
2759  return $v;
2760}
[10144]2761
2762
2763/**
[12239]2764 * UAM_check_pwdreset
2765 * checks if a user id is registered as having already
[12661]2766 * changed his password.
[12239]2767 *
2768 * @uid        : the user id
2769 *
2770 * @returns    : true or false whether the users has already changed his password
2771 *
2772 */
2773function UAM_check_pwgreset($uid)
2774{
2775  $query = '
2776SELECT UAM_pwdreset
2777FROM '.USERS_TABLE.'
2778WHERE id='.$uid.'
2779;';
2780
2781  $result = pwg_db_fetch_assoc(pwg_query($query));
2782 
2783  if($result['UAM_pwdreset'] == 'true')
2784  {
2785    return true;
2786  }
2787  else return false; 
2788}
2789
[12661]2790
[12239]2791/**
[12661]2792 * UAM_UsrReg_Verif
2793 * Check if the user who logged-in have validate his registration
2794 *
2795 * @returns : True if validation is OK else False
2796 */
2797function UAM_UsrReg_Verif($user_id)
2798{
2799  global $conf;
2800
2801        // Get UAM configuration
2802  // ---------------------
2803  $conf_UAM = unserialize($conf['UserAdvManager']);
2804
2805  $query = '
2806SELECT group_id
2807  FROM '.USER_GROUP_TABLE.'
2808WHERE user_id = '.$user_id.'
2809  AND group_id = '.$conf_UAM[2].'
2810;';
2811
2812  $count = pwg_db_num_rows(pwg_query($query));
2813 
2814  if ($count == 0)
2815  {
2816    return true; // User is not in a "Waiting" group
2817  }
2818  else
2819  {
2820    return false; // User is still in a "Waiting" group
2821  }
2822}
2823
2824
2825/**
[12239]2826 * UAM_Set_PwdReset
2827 * Action in user_list to set a password reset for a user
2828 */
2829function UAM_Set_PwdReset($uid)
2830{
2831  $query ='
2832UPDATE '.USERS_TABLE.'
2833SET UAM_pwdreset = "true"
2834WHERE id = '.$uid.'
2835LIMIT 1
2836;';
2837
2838  pwg_query($query);
2839}
2840
2841
2842/**
2843 * UAM_loc_visible_user_list
2844 * Adds a new feature in user_list to allow password reset for selected users by admin
2845 *
2846 */
2847function UAM_loc_visible_user_list($visible_user_list)
2848{
2849  global $template;
2850 
2851  $template->append('plugin_user_list_column_titles', l10n('UAM_PwdReset'));
2852 
2853  $user_ids = array();
2854 
2855  foreach ($visible_user_list as $i => $user)
2856  {
2857    $user_ids[$i] = $user['id'];
2858  }
2859 
2860  $user_nums = array_flip($user_ids);
2861
2862  // Query to get informations in database
[12271]2863  // -------------------------------------
[12239]2864  if (!empty($user_ids))
2865  {
2866    $query = '
2867SELECT DISTINCT id, UAM_pwdreset
2868  FROM '.USERS_TABLE.'
2869  WHERE id IN ('.implode(',', $user_ids).')
2870;';
2871    $result = pwg_query($query);
2872   
2873    while ($row = mysql_fetch_array($result))
2874    {
2875      if ($row['UAM_pwdreset'] == 'false')
2876      {
2877        $pwdreset = l10n('UAM_PwdReset_Done');
2878      }
2879      else if ($row['UAM_pwdreset'] == 'true')
2880      {
2881        $pwdreset = l10n('UAM_PwdReset_Todo');
2882      }
2883      else $pwdreset = l10n('UAM_PwdReset_NA');
2884     
2885                  $visible_user_list[$user_nums[$row['id']]]['plugin_columns'][] = $pwdreset; // Shows users password state in user_list
2886    }
2887  }
2888  return $visible_user_list;
2889}
2890
2891
2892/**
[10342]2893 * UAM specific database dump (only for MySql !)
2894 * Creates an SQL dump of UAM specific tables and configuration settings
2895 *
2896 * @returns  : Boolean to manage appropriate message display
2897 *
2898 */
[12239]2899function UAM_dump($download)
[10342]2900{
2901  global $conf;
[12314]2902
2903  $plugin =  PluginInfos(UAM_PATH);
2904  $version = $plugin['version'];
2905
[10342]2906  // Initial backup folder creation and file initialisation
[12271]2907  // ------------------------------------------------------
[10342]2908  if (!is_dir(UAM_PATH.'/include/backup'))
2909    mkdir(UAM_PATH.'/include/backup');
2910
2911  $Backup_File = UAM_PATH.'/include/backup/UAM_dbbackup.sql';
2912
2913  $fp = fopen($Backup_File, 'w');
2914
[12314]2915  // Writing plugin version
2916  $insertions = "-- ".$version." --\n\n";
2917  fwrite($fp, $insertions);
[10342]2918
2919  // Saving UAM specific tables
[12271]2920  // --------------------------
[10342]2921  $ListTables = array(USER_CONFIRM_MAIL_TABLE, USER_LASTVISIT_TABLE);
2922  $j=0;
2923 
2924  while($j < count($ListTables))
2925  {
2926    $sql = 'SHOW CREATE TABLE '.$ListTables[$j];
2927    $res = pwg_query($sql);
2928
2929    if ($res)
2930    {
2931      $insertions = "-- -------------------------------------------------------\n";
2932      $insertions .= "-- Create ".$ListTables[$j]." table\n";
2933      $insertions .= "-- ------------------------------------------------------\n\n";
2934
2935      $insertions .= "DROP TABLE IF EXISTS ".$ListTables[$j].";\n\n";
2936
2937      $array = mysql_fetch_array($res);
2938      $array[1] .= ";\n\n";
2939      $insertions .= $array[1];
2940
2941      $req_table = pwg_query('SELECT * FROM '.$ListTables[$j]) or die(mysql_error());
2942      $nb_fields = mysql_num_fields($req_table);
2943      while ($line = mysql_fetch_array($req_table))
2944      {
2945        $insertions .= 'INSERT INTO '.$ListTables[$j].' VALUES (';
2946        for ($i=0; $i<$nb_fields; $i++)
2947        {
[12275]2948          $insertions .= '\'' . pwg_db_real_escape_string($line[$i]) . '\', ';
[10342]2949        }
2950        $insertions = substr($insertions, 0, -2);
2951        $insertions .= ");\n";
2952      }
2953      $insertions .= "\n\n";
2954    }
2955
2956    fwrite($fp, $insertions);   
2957    $j++;
2958  }
2959 
2960  // Saving UAM configuration
[12271]2961  // ------------------------
[10342]2962  $insertions = "-- -------------------------------------------------------\n";
2963  $insertions .= "-- Insert UAM configuration in ".CONFIG_TABLE."\n";
2964  $insertions .= "-- ------------------------------------------------------\n\n";
2965
2966  fwrite($fp, $insertions);
2967
2968  $pattern = "UserAdvManager%";
2969  $req_table = pwg_query('SELECT * FROM '.CONFIG_TABLE.' WHERE param LIKE "'.$pattern.'";') or die(mysql_error());
2970  $nb_fields = mysql_num_fields($req_table);
2971
2972  while ($line = mysql_fetch_array($req_table))
2973  {
2974    $insertions = 'INSERT INTO '.CONFIG_TABLE.' VALUES (';
2975    for ($i=0; $i<$nb_fields; $i++)
2976    {
[12275]2977      $insertions .= '\'' . pwg_db_real_escape_string($line[$i]) . '\', ';
[10342]2978    }
2979    $insertions = substr($insertions, 0, -2);
2980    $insertions .= ");\n";
2981
2982    fwrite($fp, $insertions);
2983  }
2984
2985  fclose($fp);
2986
2987  // Download generated dump file
[12271]2988  // ----------------------------
[10342]2989  if ($download == 'true')
2990  {
2991    if (@filesize($Backup_File))
2992    {
2993      $http_headers = array(
2994        'Content-Length: '.@filesize($Backup_File),
2995        'Content-Type: text/x-sql',
2996        'Content-Disposition: attachment; filename="UAM_dbbackup.sql";',
2997        'Content-Transfer-Encoding: binary',
2998        );
2999
3000      foreach ($http_headers as $header)
3001      {
3002        header($header);
3003      }
3004
3005      @readfile($Backup_File);
3006      exit();
3007    }
3008  }
3009
3010  return true;
3011}
3012
3013
3014/**
[12275]3015 * UAM_Restore_backup_file
3016 * Restore backup database file
3017 *
3018 * @returns : Boolean
3019 */
3020function UAM_Restore_backup_file() 
3021{
3022  global $prefixeTable, $dblayer, $conf;
3023 
3024  define('DEFAULT_PREFIX_TABLE', 'piwigo_');
3025 
3026  $Backup_File = UAM_PATH.'/include/backup/UAM_dbbackup.sql';
3027
3028  // Cleanup database before restoring
3029  // ---------------------------------
3030
3031  // Delete UserAdvManager global config in #_config table
3032  $q = '
3033DELETE FROM '.CONFIG_TABLE.'
3034WHERE param="UserAdvManager"
3035;';
3036
3037  pwg_query($q);
3038
3039  // Delete UserAdvManager_ConfirmMail global config in #_config table
3040  $q = '
3041DELETE FROM '.CONFIG_TABLE.'
3042WHERE param="UserAdvManager_ConfirmMail"
3043;';
3044
3045  pwg_query($q);
3046
3047  // Delete UserAdvManager_Redir config in #_config table
3048  $q = '
3049DELETE FROM '.CONFIG_TABLE.'
3050WHERE param="UserAdvManager_Redir"
3051;';
3052
3053  pwg_query($q);
3054
3055  // Delete UserAdvManager_Version config in #_config table
3056  $q = '
3057DELETE FROM '.CONFIG_TABLE.'
3058WHERE param="UserAdvManager_Version"
3059;';
3060
3061  pwg_query($q);
3062
3063  // Restore sql backup file - DROP TABLE queries are executed
3064  // ---------------------------------------------------------
3065  UAM_execute_sqlfile(
3066    $Backup_File,
3067    DEFAULT_PREFIX_TABLE,
3068    $prefixeTable,
3069    $dblayer
3070  );
3071}
3072
3073
3074/**
3075 * loads an sql file and executes all queries / Based on Piwigo's original install file
3076 *
3077 * Before executing a query, $replaced is... replaced by $replacing. This is
3078 * useful when the SQL file contains generic words.
3079 *
3080 * @param string filepath
3081 * @param string replaced
3082 * @param string replacing
3083 * @return void
3084 */
3085function UAM_execute_sqlfile($filepath, $replaced, $replacing, $dblayer)
3086{
3087  $sql_lines = file($filepath);
3088  $query = '';
3089  foreach ($sql_lines as $sql_line)
3090  {
3091    $sql_line = trim($sql_line);
3092    if (preg_match('/(^--|^$)/', $sql_line))
3093    {
3094      continue;
3095    }
3096   
3097    $query.= ' '.$sql_line;
3098   
3099    // if we reached the end of query, we execute it and reinitialize the
3100    // variable "query"
3101    if (preg_match('/;$/', $sql_line))
3102    {
3103      $query = trim($query);
3104      $query = str_replace($replaced, $replacing, $query);
3105      if ('mysql' == $dblayer)
3106      {
3107        if (preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches))
3108        {
3109          $query = $matches[1].' DEFAULT CHARACTER SET utf8'.';';
3110        }
3111      }
3112      pwg_query($query);
3113      $query = '';
3114    }
3115  }
3116}
3117
3118
[12661]3119/**
3120 * Delete obsolete files on plugin upgrade
3121 * Obsolete files are listed in file obsolete.list
3122 *
3123 */
3124function clean_obsolete_files()
3125{
3126  if (file_exists(UAM_PATH.'obsolete.list')
3127    and $old_files = file(UAM_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
3128    and !empty($old_files))
3129  {
3130    array_push($old_files, 'obsolete.list');
3131    foreach($old_files as $old_file)
3132    {
3133      $path = UAM_PATH.$old_file;
3134      if (is_file($path))
3135      {
3136        @unlink($path);
3137      }
3138      elseif (is_dir($path))
3139      {
3140        @rmdir($path);
3141      }
3142    }
3143  }
3144}
[12275]3145
[12661]3146
[12275]3147/**
[12661]3148 * Function called from maintain.inc.php - to check if database upgrade is needed
3149 *
3150 * @param : table name
3151 *
3152 * @return : boolean
3153 *
3154 */
3155function table_exist($table)
3156{
3157  $query = 'DESC '.$table.';';
3158  return (bool)($res=pwg_query($query));
3159}
3160
3161
3162/**
3163 * Function called from UAM_admin.php and main.inc.php to get the plugin version and name
3164 *
3165 * @param : plugin directory
3166 *
3167 * @return : plugin's version and name
3168 *
3169 */
3170function PluginInfos($dir)
3171{
3172  $path = $dir;
3173
3174  $plg_data = implode( '', file($path.'main.inc.php') );
3175  if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
3176  {
3177    $plugin['name'] = trim( $val[1] );
3178  }
3179  if (preg_match("|Version: (.*)|", $plg_data, $val))
3180  {
3181    $plugin['version'] = trim($val[1]);
3182  }
3183  if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
3184  {
3185    $plugin['uri'] = trim($val[1]);
3186  }
3187  if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
3188  {
3189    $plugin['description'] = trim($desc);
3190  }
3191  elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
3192  {
3193    $plugin['description'] = trim($val[1]);
3194  }
3195  if ( preg_match("|Author: (.*)|", $plg_data, $val) )
3196  {
3197    $plugin['author'] = trim($val[1]);
3198  }
3199  if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
3200  {
3201    $plugin['author uri'] = trim($val[1]);
3202  }
3203  if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
3204  {
3205    list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
3206    if (is_numeric($extension)) $plugin['extension'] = $extension;
3207  }
3208// IMPORTANT SECURITY !
3209// --------------------
3210  $plugin = array_map('htmlspecialchars', $plugin);
3211
3212  return $plugin ;
3213}
3214
3215
3216/**
[10144]3217 * Useful for debugging - 4 vars can be set
3218 * Output result to log.txt file
3219 *
3220 */
3221function UAMLog($var1, $var2, $var3, $var4)
3222{
3223   $fo=fopen (UAM_PATH.'log.txt','a') ;
3224   fwrite($fo,"======================\n") ;
3225   fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n");
3226   fwrite($fo,$var1 ."\r\n") ;
3227   fwrite($fo,$var2 ."\r\n") ;
3228   fwrite($fo,$var3 ."\r\n") ;
3229   fwrite($fo,$var4 ."\r\n") ;
3230   fclose($fo) ;
3231}
[5056]3232?>
Note: See TracBrowser for help on using the repository browser.