Index: extensions/UserAdvManager/branches/2.40/include/functions.inc.php
===================================================================
--- extensions/UserAdvManager/branches/2.40/include/functions.inc.php	(revision 17805)
+++ extensions/UserAdvManager/branches/2.40/include/functions.inc.php	(revision 18160)
@@ -19,6 +19,6 @@
   array_push($menu,
     array(
-      'NAME' => $name,
-      'URL' => get_root_url().'admin.php?page=plugin-'.basename(UAM_PATH)
+    		'NAME' => $name,
+    		'URL' => get_root_url().'admin.php?page=plugin-'.basename(UAM_PATH)
     )
   );
@@ -36,7 +36,8 @@
 {
   global $conf, $page;
+  
+  $conf_UAM = unserialize($conf['UserAdvManager']);
  
   // Check mandatory email address for email exclusion
-  $conf_UAM = unserialize($conf['UserAdvManager']);
   if (!$conf['obligatory_user_mail_address'] and $conf_UAM[10] = 'true')
   {
@@ -51,5 +52,5 @@
  * Initiating GhostTracker - Perform user logout after registration if not validated
  */
-function UAM_GhostTracker()
+function UAM_Init()
 {
   global $conf, $user;
@@ -66,5 +67,5 @@
     {
 
-      $userid = get_userid($user['username']);
+						$userid = get_userid($user['username']);
      	  
       // Looking for existing entry in last visit table
@@ -101,12 +102,19 @@
       }
     }
-  }
-
-  // Perform user logout after registration if not validated
-  if ((isset($conf_UAM[39]) and $conf_UAM[39] == 'true') and !UAM_UsrReg_Verif($user['id']))
-  {
-    invalidate_user_cache();
-    logout_user();
-    redirect(UAM_PATH.'rejected.php');
+
+    // Perform user logout after registration if not validated
+    if ((isset($conf_UAM[39]) and $conf_UAM[39] == 'true') and !UAM_UsrReg_Verif($user['id']) and !is_admin() and !is_webmaster() )
+    {
+      invalidate_user_cache();
+      logout_user();
+      if ( $conf['guest_access'] )
+      {
+        redirect( make_index_url().'?UAM_msg=rejected', 0);
+      }
+      else
+      {
+        redirect( get_root_url().'identification.php?UAM_msg=rejected' , 0);
+      }
+    }
   }
 }
@@ -135,4 +143,12 @@
       // ----------------------------------------------------------------------------------------------
       SetPermission($register_user['id']);// Set to "waiting" group or status until admin validation
+      
+      // This is to set UAM_validated field to false in #_users table - Usefull if no "waiting" group or status is set
+      // -------------------------------------------------------------------------------------------------------------
+      SetUnvalidated($register_user['id']);
+
+      // This is to send the validation key by email to admins for their manual validation without having to connect the gallery
+      // -----------------------------------------------------------------------------------------------------------------------
+      SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], true);
     }
     // Sending registration confirmation by email
@@ -261,8 +277,8 @@
       $user_idsOK[] = $user['id'];
 
-      $query = "
-UPDATE ".CONFIG_TABLE."
-SET value = \"".implode(',', $user_idsOK)."\"
-WHERE param = 'UserAdvManager_Redir';";
+      $query = '
+UPDATE '.CONFIG_TABLE.'
+SET value = "'.implode(',', $user_idsOK).'"
+WHERE param = "UserAdvManager_Redir";';
           
       pwg_query($query);
@@ -317,5 +333,5 @@
     if (!empty($_POST['use_new_pwd']))
     {
-      $typemail = 2; // Confirmation email on user profile update - With information email if checked
+      $typemail = 2; // Confirmation email on user profile update - With information email
 
       // Password enforcement control
@@ -356,4 +372,5 @@
         {
           SetPermission($user['id']);// Set to "waiting" group or status until user validation
+          SetUnvalidated($user['id']); // Set UAM_validated field to false in #_users table
           $confirm_mail_need = true;
         }
@@ -364,4 +381,5 @@
         {
           SetPermission($user['id']);// Set to "waiting" group or status until admin validation
+          SetUnvalidated($user['id']); // Set UAM_validated field to false in #_users table
           $confirm_mail_need = false;
         }       
@@ -455,5 +473,5 @@
     }
   }
-  elseif ((isset($conf_UAM[39]) and $conf_UAM[39] == 'true') and !UAM_UsrReg_Verif($user['id']))
+  elseif ((isset($conf_UAM[39]) and $conf_UAM[39] == 'true') and !UAM_UsrReg_Verif($user['id']) and !is_admin() and !is_webmaster())
   {
     // Logged-in user cleanup, session destruction and redirected to custom page
@@ -461,5 +479,12 @@
     invalidate_user_cache();
     logout_user();
-    redirect(UAM_PATH.'rejected.php');
+    if ( $conf['guest_access'] )
+    {
+      redirect( make_index_url().'?UAM_msg=rejected', 0);
+    }
+    else
+    {
+      redirect( get_root_url().'identification.php?UAM_msg=rejected' , 0);
+    }
   }
 }
@@ -512,14 +537,14 @@
   }
 
-  // GhostTracker auto group, status or privacy level downgrade with or without information email sending and autodeletion if user already reminded
-  // ----------------------------------------------------------------------------------------------------------------------------------------------
+  // Auto group, status or privacy level downgrade and autodeletion if user already reminded
+  // ---------------------------------------------------------------------------------------
   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)))
   {
     if (count($collection) > 0)
-  	{
+  	 {
       // Process if a non-admin nor webmaster user is logged
       // ---------------------------------------------------
       if (in_array($user['id'], $collection))
-    	{
+   			{
         // Check lastvisit reminder state
         // ------------------------------
@@ -554,5 +579,5 @@
           redirect(UAM_PATH.'GT_del_account.php');
         }
-    	}
+    		}
       else // Process if an admin or webmaster user is logged
       {
@@ -585,9 +610,9 @@
             list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 
-		        $query = "
-UPDATE ".USER_LASTVISIT_TABLE."
-SET lastvisit='".$dbnow."'
-WHERE user_id = '".$user_id."'
-;";
+		        $query = '
+UPDATE '.USER_LASTVISIT_TABLE.'
+SET lastvisit="'.$dbnow.'"
+WHERE user_id = '.$user_id.'
+;';
             pwg_query($query);
 
@@ -596,14 +621,17 @@
             // Delete user from all groups
             // ---------------------------
-            $query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$user_id."'
+            if ($conf_UAM[2] <> -1 and $conf_UAM[3] <> -1)
+            {
+            		$query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$user_id.'
   AND (
-    group_id = '".$conf_UAM[2]."'
+    group_id = '.$conf_UAM[2].'
   OR 
-    group_id = '".$conf_UAM[3]."'
+    group_id = '.$conf_UAM[3].'
   )
-;";
-            pwg_query($query);
+;';
+            		pwg_query($query);
+												}
 
             // Change user status
@@ -611,9 +639,9 @@
             if ($conf_UAM[26] <> -1)
             {
-              $query = "
-UPDATE ".USER_INFOS_TABLE."
-SET status = '".$conf_UAM[26]."'
-WHERE user_id = '".$user_id."'
-;";
+              $query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET status = "'.$conf_UAM[26].'"
+WHERE user_id = '.$user_id.'
+;';
               pwg_query($query);
             }
@@ -623,10 +651,10 @@
             if ($conf_UAM[25] <> -1)
             {
-              $query = "
-INSERT INTO ".USER_GROUP_TABLE."
+              $query = '
+INSERT INTO '.USER_GROUP_TABLE.'
   (user_id, group_id)
 VALUES
-  ('".$user_id."', '".$conf_UAM[25]."')
-;";
+  ('.$user_id.', "'.$conf_UAM[25].'")
+;';
               pwg_query($query);
             }
@@ -636,9 +664,9 @@
             if ($conf_UAM[37] <> -1)
             {
-              $query = "
-UPDATE ".USER_INFOS_TABLE."
-SET level = '".$conf_UAM[37]."'
-WHERE user_id = '".$user_id."'
-;";
+              $query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET level = "'.$conf_UAM[37].'"
+WHERE user_id = '.$user_id.'
+;';
               pwg_query($query);
             }
@@ -650,28 +678,28 @@
               // Set reminder true
               // -----------------
-              $query = "
-UPDATE ".USER_LASTVISIT_TABLE."
-SET reminder = 'true'
-WHERE user_id = '".$user_id."'
-;";
+              $query = '
+UPDATE '.USER_LASTVISIT_TABLE.'
+SET reminder = "true"
+WHERE user_id = '.$user_id.'
+;';
               pwg_query($query);
             
               // Reset confirmed user status to unvalidated
               // ------------------------------------------
-						  $query = '
+						  						$query = '
 UPDATE '.USER_CONFIRM_MAIL_TABLE.'
 SET date_check = NULL
-WHERE user_id = "'.$user_id.'"
-;';
-						  pwg_query($query);
+WHERE user_id = '.$user_id.'
+;';
+						  						pwg_query($query);
 
               // Get users information for email notification
               // --------------------------------------------
-						  $query = '
+						  						$query = '
 SELECT id, username, mail_address
 FROM '.USERS_TABLE.'
 WHERE id = '.$user_id.'
 ;';
-						  $data = pwg_db_fetch_assoc(pwg_query($query));
+						  						$data = pwg_db_fetch_assoc(pwg_query($query));
             
               demotion_mail($user_id, $data['username'], $data['mail_address']);
@@ -725,9 +753,9 @@
   {
     if (count($collection) > 0)
-  	{
+  		{
       // Process if a non-admin nor webmaster user is logged
       // ---------------------------------------------------
       if (in_array($user['id'], $collection))
-    	{
+    		{
         // Check ConfirmMail reminder state
         // --------------------------------
@@ -752,13 +780,13 @@
         if (!$reminder and isset($conf_UAM[32]) and $conf_UAM[32] == 'true')
         {
-     		  $typemail = 1;
+     		  	$typemail = 1;
           
           // Get current user informations
           // -----------------------------
-          $query = "
+          $query = '
 SELECT id, username, mail_address
-FROM ".USERS_TABLE."
-WHERE id = '".$user['id']."'
-;";
+FROM '.USERS_TABLE.'
+WHERE id = '.$user['id'].'
+;';
           $data = pwg_db_fetch_assoc(pwg_query($query));
 
@@ -779,5 +807,5 @@
           redirect(UAM_PATH.'USR_del_account.php');
         }
-    	}
+    		}
       else // Process if an admin or webmaster user is logged
       {
@@ -810,9 +838,9 @@
             // Get current user informations
             // -----------------------------
-            $query = "
+            $query = '
 SELECT id, username, mail_address
-FROM ".USERS_TABLE."
-WHERE id = '".$user_id."'
-;";
+FROM '.USERS_TABLE.'
+WHERE id = '.$user_id.'
+;';
             $data = pwg_db_fetch_assoc(pwg_query($query));
 
@@ -861,4 +889,34 @@
 }
 
+/**
+ * Triggered on init
+ * 
+ * Display a message according to $_GET['UAM_msg']
+ */
+function UAM_DisplayMsg()
+{
+  if( isset($_GET['UAM_msg']))
+  {
+    global $user, $lang, $conf, $page;
+    $conf_UAM = unserialize($conf['UserAdvManager']);
+    
+    if (isset($conf_UAM[40]) and $conf_UAM[40] <> '' and $_GET['UAM_msg']="rejected")
+    {
+      // Management of Extension flags ([mygallery], [myurl])
+      // ---------------------------------------------------
+      $patterns[] = '#\[mygallery\]#i';
+      $replacements[] = $conf['gallery_title'];
+      $patterns[] = '#\[myurl\]#i';
+      $replacements[] = get_gallery_home_url();
+    
+      if (function_exists('get_user_language_desc'))
+      {
+        $custom_text = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[40]));
+      }
+      else $custom_text = l10n(preg_replace($patterns, $replacements, $conf_UAM[40]));
+      $page["errors"][]=$custom_text;
+    }
+  }
+}
 
 /**
@@ -906,8 +964,8 @@
 
   $conf_UAM = unserialize($conf['UserAdvManager']);
-  
-	include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
-  
-	$infos1_perso = "";
+
+		include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
+
+		$infos1_perso = "";
   $infos2_perso = "";
   $subject = "";
@@ -961,5 +1019,5 @@
       break;
       
-    case 2: // Confirmation email on user profile update - With information email if checked
+    case 2: // Confirmation email on user profile update - With information email if modification done in user profile
       if (isset($conf_UAM[41]) and $conf_UAM[41] <> '')
       {
@@ -1086,5 +1144,5 @@
   }
 
-  if ( isset($conf_UAM[1]) and $conf_UAM[1] == 'true' and $confirm) // Add confirmation link ?
+  if (isset($conf_UAM[1]) and ($conf_UAM[1] == 'true' or $conf_UAM[1] == 'local')  and $confirm) // Add confirmation link ?
   {
     $infos2 = array
@@ -1121,9 +1179,26 @@
 // Sending the email with subject and contents
 // -------------------------------------------
-  pwg_mail($email, array(
-    'subject' => $subject,
-    'content' => (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
-  ));
-
+		if (isset($conf_UAM[1]) and $conf_UAM[1] == 'local')
+		{
+  		switch_lang_to(get_default_language());
+  		
+  		load_language('plugin.lang', UAM_PATH);
+  		$subject = get_l10n_args('UAM_Subject admin validation for %s',$username);
+
+		  $content = array(
+  		get_l10n_args('UAM_Manual_validation_needed_for %s', stripslashes($username)),
+    get_l10n_args('', ''),
+    get_l10n_args('UAM_Link: %s', AddConfirmMail($id, $email)),
+    );
+
+    UAM_mail_notification_admins($subject, $content);
+		}
+		else
+		{
+  		pwg_mail($email, array(
+    		'subject' => $subject,
+    		'content' => (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
+  		));
+		}
 // Switching back to default language
 // ----------------------------------
@@ -1133,4 +1208,70 @@
 
 /**
+ * Function called from SendMail2User in functions.inc.php to notify admins on user registration validation with validation key
+ *
+ * @param:
+ *   - keyargs_subject: mail subject on l10n_args format
+ *   - keyargs_content: mail content on l10n_args format
+ *
+ * @return boolean (Ok or not)
+ */
+function UAM_mail_notification_admins($keyargs_subject, $keyargs_content)
+{
+  global $conf, $user;
+  
+  // Check arguments
+  if (empty($keyargs_subject) or empty($keyargs_content))
+  {
+    return false;
+  }
+
+  $return = true;
+
+  $admins = array();
+
+  $query = '
+SELECT
+    u.'.$conf['user_fields']['username'].' AS username,
+    u.'.$conf['user_fields']['email'].' AS mail_address
+  FROM '.USERS_TABLE.' AS u
+    JOIN '.USER_INFOS_TABLE.' AS i ON i.user_id =  u.'.$conf['user_fields']['id'].'
+  WHERE i.status in (\'webmaster\',  \'admin\')
+    AND '.$conf['user_fields']['email'].' IS NOT NULL
+    AND i.user_id <> '.$user['id'].'
+  ORDER BY username
+;';
+
+  $datas = pwg_query($query);
+  if (!empty($datas))
+  {
+    while ($admin = pwg_db_fetch_assoc($datas))
+    {
+      if (!empty($admin['mail_address']))
+      {
+        array_push($admins, format_email($admin['username'], $admin['mail_address']));
+      }
+    }
+  }
+
+  if (count($admins) > 0)
+  {
+    $content = l10n_args($keyargs_content)."\n";
+
+    $return = pwg_mail(
+      implode(', ', $admins),
+      array(
+        'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject),
+        'content' => $content,
+        'content_format' => 'text/plain',
+        'email_format' => 'text/plain',
+        )
+      );
+  }
+
+  return $return;
+}
+
+
+/**
  * Function called from UAM_admin.php to resend validation email with or without new validation key
  *
@@ -1148,5 +1289,5 @@
   $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
   
-	include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
+		include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
   
 // We have to get the user's language in database
@@ -1187,5 +1328,5 @@
       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)
       {
-        // Management of Extension flags ([username], [mygallery], [myurl], [Kdays])
+      		// Management of Extension flags ([username], [mygallery], [myurl], [Kdays])
         // -------------------------------------------------------------------------
         $patterns[] = '#\[username\]#i';
@@ -1213,16 +1354,16 @@
           get_l10n_args('', ''),
         );        
-			}
+						}
 
 // Set reminder true 
 // -----------------     
-      $query = "
-UPDATE ".USER_CONFIRM_MAIL_TABLE."
-SET reminder = 'true'
-WHERE user_id = '".$user_id."'
-;";
+      $query = '
+UPDATE '.USER_CONFIRM_MAIL_TABLE.'
+SET reminder = "true"
+WHERE user_id = '.$user_id.'
+;';
       pwg_query($query);
       
-		break;
+				break;
       
     case 2: //Generating email content for remind without a new key
@@ -1269,9 +1410,9 @@
 // Set reminder true
 // -----------------
-      $query = "
-UPDATE ".USER_CONFIRM_MAIL_TABLE."
-SET reminder = 'true'
-WHERE user_id = '".$user_id."'
-;";
+      $query = '
+UPDATE '.USER_CONFIRM_MAIL_TABLE.'
+SET reminder = "true"
+WHERE user_id = '.$user_id.'
+;';
       pwg_query($query);
       
@@ -1284,7 +1425,7 @@
   ));
 
-// Switching back to default language
-// ----------------------------------
-switch_lang_back();
+		// Switching back to default language
+		// ----------------------------------
+		switch_lang_back();
 }
 
@@ -1303,5 +1444,5 @@
   $subject = "";
   
-	include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
+		include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
 
 // We have to get the user's language in database
@@ -1367,7 +1508,7 @@
   ));
 
-// Switching back to default language
-// ----------------------------------
-switch_lang_back();
+		// Switching back to default language
+		// ----------------------------------
+		switch_lang_back();
 }
 
@@ -1385,12 +1526,12 @@
   $conf_UAM = unserialize($conf['UserAdvManager']);
   
-	include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
-  
-	$custom_txt = "";
-  $subject = "";
+		include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
+  
+		$custom_txt = "";
+		$subject = "";
 
 // We have to get the user's language in database
 // ----------------------------------------------
-  $query ='
+  $query = '
 SELECT user_id, language
 FROM '.USER_INFOS_TABLE.'
@@ -1472,7 +1613,7 @@
   ));
 
-// Switching back to default language
-// ----------------------------------
-switch_lang_back();
+		// Switching back to default language
+		// ----------------------------------
+		switch_lang_back();
 }
 
@@ -1490,7 +1631,7 @@
   $conf_UAM = unserialize($conf['UserAdvManager']);
   
-	include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
-  
-	$custom_txt = "";
+		include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
+  
+		$custom_txt = "";
   $subject = "";
 
@@ -1544,5 +1685,5 @@
       $subject = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[46]))."\n\n";
     }
-    else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[46]))."\n\n"; 
+    else $subject = l10n(preg_replace($patterns, $replacements, $conf_UAM[46]))."\n\n";
   }
       
@@ -1557,10 +1698,9 @@
     $patterns[] = '#\[myurl\]#i';
     $replacements[] = get_gallery_home_url();
-
     if (function_exists('get_user_language_desc'))
     {
       $custom_txt = get_user_language_desc(preg_replace($patterns, $replacements, $conf_UAM[27]))."\n\n";
     }
-    else $custom_txt = l10n(preg_replace($patterns, $replacements, $conf_UAM[27]))."\n\n"; 
+    else $custom_txt = l10n(preg_replace($patterns, $replacements, $conf_UAM[27]))."\n\n";
   }
 
@@ -1578,7 +1718,7 @@
   ));
 
-// Switching back to default language
-// ----------------------------------
-switch_lang_back();
+		// Switching back to default language
+		// ----------------------------------
+		switch_lang_back();
 }
 
@@ -1595,9 +1735,9 @@
   {
     $id = generate_key(16);
-    $query = "
+    $query = '
 SELECT COUNT(*)
-  FROM ".USER_CONFIRM_MAIL_TABLE."
-WHERE id = '".$id."'
-;";
+  FROM '.USER_CONFIRM_MAIL_TABLE.'
+WHERE id = "'.$id.'"
+;';
     list($count) = pwg_db_fetch_row(pwg_query($query));
 
@@ -1627,30 +1767,30 @@
   if (isset($Confirm_Mail_ID))
   {
-    $query = "
+    $query = '
 SELECT status
-  FROM ".USER_INFOS_TABLE."
-WHERE user_id = '".$user_id."'
-;";
+  FROM '.USER_INFOS_TABLE.'
+WHERE user_id = '.$user_id.'
+;';
     list($status) = pwg_db_fetch_row(pwg_query($query));
     
-    $query = "
-INSERT INTO ".USER_CONFIRM_MAIL_TABLE."
+    $query = '
+INSERT INTO '.USER_CONFIRM_MAIL_TABLE.'
   (id, user_id, mail_address, status, date_check)
 VALUES
-  ('".$Confirm_Mail_ID."', '".$user_id."', '".$email."', '".$status."', null)
-;";
+  ("'.$Confirm_Mail_ID.'", '.$user_id.', "'.$email.'", "'.$status.'", null)
+;';
     pwg_query($query);
 
     // Delete user from all groups
     // ---------------------------
-    $query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$user_id."'
+    $query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$user_id.'
   AND (
-    group_id = '".$conf_UAM[2]."'
+    group_id = '.$conf_UAM[2].'
   OR 
-    group_id = '".$conf_UAM[3]."'
+    group_id = '.$conf_UAM[3].'
   )
-;";
+;';
     pwg_query($query);
 
@@ -1659,9 +1799,9 @@
     if (!is_admin() and $conf_UAM[7] <> -1)
     {
-      $query = "
-UPDATE ".USER_INFOS_TABLE."
-SET status = '".$conf_UAM[7]."'
-WHERE user_id = '".$user_id."'
-;";
+      $query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET status = "'.$conf_UAM[7].'"
+WHERE user_id = '.$user_id.'
+;';
       pwg_query($query);
     }
@@ -1671,10 +1811,10 @@
     if (!is_admin() and $conf_UAM[2] <> -1)
     {
-      $query = "
-INSERT INTO ".USER_GROUP_TABLE."
+      $query = '
+INSERT INTO '.USER_GROUP_TABLE.'
   (user_id, group_id)
 VALUES
-  ('".$user_id."', '".$conf_UAM[2]."')
-;";
+  ('.$user_id.', '.$conf_UAM[2].')
+;';
       pwg_query($query);
     }
@@ -1684,11 +1824,15 @@
     if (!is_admin() and $conf_UAM[35] <> -1)
     {
-      $query = "
-UPDATE ".USER_INFOS_TABLE."
-SET level = '".$conf_UAM[35]."'
-WHERE user_id = '".$user_id."'
-;";
+      $query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET level = "'.$conf_UAM[35].'"
+WHERE user_id = '.$user_id.'
+;';
       pwg_query($query);
     }
+    
+    // Set UAM_validated field to false in #_users table
+    // -------------------------------------------------
+    SetUnvalidated($user_id);
     
     return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
@@ -1712,22 +1856,22 @@
 // Groups cleanup
 // --------------
-  $query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$user_id."'
+  $query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$user_id.'
   AND (
-    group_id = '".$conf_UAM[2]."'
+    group_id = '.$conf_UAM[2].'
   OR 
-    group_id = '".$conf_UAM[3]."'
+    group_id = '.$conf_UAM[3].'
   )
-;";
+;';
   pwg_query($query);
 
   if (!is_admin() and $conf_UAM[7] <> -1) // Set status
   {
-    $query = "
-UPDATE ".USER_INFOS_TABLE."
-SET status = '".$conf_UAM[7]."'
-WHERE user_id = '".$user_id."'
-;";
+    $query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET status = "'.$conf_UAM[7].'"
+WHERE user_id = '.$user_id.'
+;';
     pwg_query($query);
   }
@@ -1735,10 +1879,10 @@
   if (!is_admin() and $conf_UAM[2] <> -1) // Set group
   {
-    $query = "
-INSERT INTO ".USER_GROUP_TABLE."
+    $query = '
+INSERT INTO '.USER_GROUP_TABLE.'
   (user_id, group_id)
 VALUES
-  ('".$user_id."', '".$conf_UAM[2]."')
-;";
+  ('.$user_id.', '.$conf_UAM[2].')
+;';
     pwg_query($query);
   }
@@ -1746,10 +1890,10 @@
   if (!is_admin() and $conf_UAM[35] <> -1) // Set privacy level
   {
-    $query = "
-INSERT INTO ".USER_INFOS_TABLE."
+    $query = '
+INSERT INTO '.USER_INFOS_TABLE.'
   (user_id, level)
 VALUES
-  ('".$user_id."', '".$conf_UAM[35]."')
-;";
+  ('.$user_id.', "'.$conf_UAM[35].'")
+;';
     pwg_query($query);
   }
@@ -1775,17 +1919,17 @@
   if (isset($Confirm_Mail_ID))
   { 
-    $query = "
-UPDATE ".USER_CONFIRM_MAIL_TABLE."
-SET id = '".$Confirm_Mail_ID."'
-WHERE user_id = '".$user_id."'
-;";
+    $query = '
+UPDATE '.USER_CONFIRM_MAIL_TABLE.'
+SET id = "'.$Confirm_Mail_ID.'"
+WHERE user_id = '.$user_id.'
+;';
     pwg_query($query);
 
-		$query = "
-UPDATE ".USER_INFOS_TABLE."
-SET registration_date = '".$dbnow."'
-WHERE user_id = '".$user_id."'
-;";
-		pwg_query($query);
+				$query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET registration_date = "'.$dbnow.'"
+WHERE user_id = '.$user_id.'
+;';
+				pwg_query($query);
     
     return get_absolute_root_url().UAM_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID.'&userid='.$user_id;
@@ -1806,9 +1950,9 @@
   list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
 
-  $query = "
-UPDATE ".USER_LASTVISIT_TABLE."
-SET lastvisit = '".$dbnow."', reminder = 'true'
-WHERE user_id = '".$user_id."'
-;";
+  $query = '
+UPDATE '.USER_LASTVISIT_TABLE.'
+SET lastvisit = "'.$dbnow.'", reminder = "true"
+WHERE user_id = '.$user_id.'
+;';
   pwg_query($query);
 }
@@ -1821,8 +1965,8 @@
 function DeleteConfirmMail($user_id)
 {
-  $query = "
-DELETE FROM ".USER_CONFIRM_MAIL_TABLE."
-WHERE user_id = '".$user_id."'
-;";
+  $query = '
+DELETE FROM '.USER_CONFIRM_MAIL_TABLE.'
+WHERE user_id = '.$user_id.'
+;';
   pwg_query($query);
 }
@@ -1834,8 +1978,8 @@
 function DeleteLastVisit($user_id)
 {
-  $query = "
-DELETE FROM ".USER_LASTVISIT_TABLE."
-WHERE user_id = '".$user_id."'
-;";
+  $query = '
+DELETE FROM '.USER_LASTVISIT_TABLE.'
+WHERE user_id = '.$user_id.'
+;';
   pwg_query($query);
 }
@@ -1864,8 +2008,8 @@
   unset($values[array_search($user_id, $values)]);
      
-  $query = "
-UPDATE ".CONFIG_TABLE."
-SET value = \"".implode(',', $values)."\"
-WHERE param = 'UserAdvManager_Redir';";
+  $query = '
+UPDATE '.CONFIG_TABLE.'
+SET value = "'.implode(',', $values).'"
+WHERE param = "UserAdvManager_Redir";';
 
   pwg_query($query);
@@ -1892,27 +2036,27 @@
   $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
 
-  $query = "
+  $query = '
 SELECT COUNT(*)
-FROM ".USER_CONFIRM_MAIL_TABLE."
-WHERE id = '".$id."'
-;";
+FROM '.USER_CONFIRM_MAIL_TABLE.'
+WHERE id = "'.$id.'"
+;';
   list($count) = pwg_db_fetch_row(pwg_query($query));
 
   if ($count == 1)
   {
-    $query = "
+    $query = '
 SELECT user_id, status, date_check
-FROM ".USER_CONFIRM_MAIL_TABLE."
-WHERE id = '".$id."'
-;";
+FROM '.USER_CONFIRM_MAIL_TABLE.'
+WHERE id = "'.$id.'"
+;';
     $data = pwg_db_fetch_assoc(pwg_query($query));
 
     if (!empty($data) and isset($data['user_id']) and is_null($data['date_check']))
     {
-      $query = "
+      $query = '
 SELECT registration_date
-FROM ".USER_INFOS_TABLE."
-WHERE user_id = '".$data['user_id']."'
-;";
+FROM '.USER_INFOS_TABLE.'
+WHERE user_id = '.$data['user_id'].'
+;';
       list($registration_date) = pwg_db_fetch_row(pwg_query($query));
 
@@ -1921,184 +2065,200 @@
       if (!empty($registration_date))
       {
-				// Verify Confirmmail with time limit ON
-        // -------------------------------------
-				if (isset ($conf_UAM_ConfirmMail[1]))
-				{
-					// Dates formating and compare
-          // ---------------------------
-					$today = date("d-m-Y"); // Get today's date
-					list($day, $month, $year) = explode('-', $today); // explode date of today						 
- 					$daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
-	  		
-	  			list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
-					list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
-					$regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
-			
-					$deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps	
-					$deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
-
-					// Condition with the value set for time limit
-          // -------------------------------------------
-					if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
-					{
-						list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
-
-            // Update ConfirmMail table
-            // ------------------------
-						$query = '
+								// Verify Confirmmail with time limit ON
+    				// -------------------------------------
+								if (isset ($conf_UAM_ConfirmMail[1]))
+								{
+										// Dates formating and compare
+     					// ---------------------------
+										$today = date("d-m-Y"); // Get today's date
+										list($day, $month, $year) = explode('-', $today); // explode date of today						 
+ 									$daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
+
+	  							list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
+										list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
+										$regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
+
+										$deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps	
+										$deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
+
+										// Condition with the value set for time limit
+     					// -------------------------------------------
+										if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
+										{
+												list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
+
+        				// Update ConfirmMail table
+        				// ------------------------
+												$query = '
 UPDATE '.USER_CONFIRM_MAIL_TABLE.'
 SET date_check="'.$dbnow.'", reminder="false"
 WHERE id = "'.$id.'"
 ;';
-						pwg_query($query);
-
-            // Update LastVisit table - Force reminder field to false
-            // Usefull when a user has been automatically downgraded and revalidate its registration
-            // -------------------------------------------------------------------------------------
-						$query = '
+												pwg_query($query);
+
+        				// Update LastVisit table - Force reminder field to false
+        				// Usefull when a user has been automatically downgraded and revalidate its registration
+        				// -------------------------------------------------------------------------------------
+												$query = '
 UPDATE '.USER_LASTVISIT_TABLE.'
 SET reminder="false"
 WHERE user_id = "'.$data['user_id'].'"
 ;';
-						pwg_query($query);
+												pwg_query($query);
       
-						if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
-						{
-							$query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$data['user_id']."'
-  AND group_id = '".$conf_UAM[2]."'
-;";
-							pwg_query($query);
-						}
-	    
-						if ($conf_UAM[3] <> -1) // Add user to validated users group 
-						{
-							$query = "
-INSERT INTO ".USER_GROUP_TABLE."
+												if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
+												{
+														$query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$data['user_id'].'
+  AND group_id = '.$conf_UAM[2].'
+;';
+														pwg_query($query);
+												}
+
+												if ($conf_UAM[3] <> -1) // Add user to validated users group 
+												{
+														$query = '
+INSERT INTO '.USER_GROUP_TABLE.'
   (user_id, group_id)
 VALUES
-  ('".$data['user_id']."', '".$conf_UAM[3]."')
-;";
-							pwg_query($query);
-						}
-
-						if (($conf_UAM[4] <> -1 or isset($data['status']))) // Change user's status
-						{
-							$query = "
-UPDATE ".USER_INFOS_TABLE."
-SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
-WHERE user_id = '".$data['user_id']."'
-;";
-							pwg_query($query);
-						}
-
-						if (($conf_UAM[36] <> -1 or isset($data['level']))) // Change user's privacy level
-						{
-							$query = "
-UPDATE ".USER_INFOS_TABLE."
-SET level = '".(isset($data['level']) ? $data['level'] : $conf_UAM[36])."'
-WHERE user_id = '".$data['user_id']."'
-;";
-							pwg_query($query);
-						}
-
-						// Refresh user's category cache
-            // -----------------------------
-						invalidate_user_cache();
-  
-						return true;
-					}
-					elseif ($deltadays > $conf_UAM_ConfirmMail[1]) // If timelimit exeeds
-					{
-						return false;
-					}
-				}
-				// Verify Confirmmail with time limit OFF
-        // --------------------------------------
-				else
-				{
-					list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
-
-          // Update ConfirmMail table
-          // ------------------------
-					$query = '
+  ('.$data['user_id'].', '.$conf_UAM[3].')
+;';
+														pwg_query($query);
+												}
+
+												if ($conf_UAM[4] <> -1) // Change user's status
+												{
+														$query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET status = "'.$conf_UAM[4].'"
+WHERE user_id = '.$data['user_id'].'
+;';
+														pwg_query($query);
+												}
+
+												if ($conf_UAM[36] <> -1) // Change user's privacy level
+												{
+														$query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET level = "'.$conf_UAM[36].'"
+WHERE user_id = '.$data['user_id'].'
+;';
+														pwg_query($query);
+												}
+
+												// Set UAM_validated field to True in #_users table
+												$query = '
+UPDATE '.USERS_TABLE.'
+SET UAM_validated = "true"
+WHERE id = '.$data['user_id'].'
+;';
+												pwg_query($query);
+
+												// Refresh user's category cache
+      						// -----------------------------
+												invalidate_user_cache();
+
+												return true;
+										}
+										elseif ($deltadays > $conf_UAM_ConfirmMail[1]) // If timelimit exeeds
+										{
+												return false;
+										}
+								}
+								// Verify Confirmmail with time limit OFF
+    				// --------------------------------------
+								else
+								{
+										list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
+
+      				// Update ConfirmMail table
+      				// ------------------------
+										$query = '
 UPDATE '.USER_CONFIRM_MAIL_TABLE.'
 SET date_check="'.$dbnow.'"
 WHERE id = "'.$id.'"
 ;';
-					pwg_query($query);
-
-          // Update LastVisit table - Force reminder field to false
-          // Usefull when a user has been automatically downgraded and revalidate its registration
-          // -------------------------------------------------------------------------------------
-					$query = '
+										pwg_query($query);
+
+      				// Update LastVisit table - Force reminder field to false
+      				// Usefull when a user has been automatically downgraded and revalidate its registration
+      				// -------------------------------------------------------------------------------------
+										$query = '
 UPDATE '.USER_LASTVISIT_TABLE.'
 SET reminder="false"
 WHERE user_id = "'.$data['user_id'].'"
 ;';
-          pwg_query($query);
-      
-					if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
-					{
-						$query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$data['user_id']."'
-AND group_id = '".$conf_UAM[2]."'
-;";
-						pwg_query($query);
-					}
-    
-					if ($conf_UAM[3] <> -1)
-					{
-						$query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$data['user_id']."'
-AND group_id = '".$conf_UAM[3]."'
-;";
-						pwg_query($query);
-
-						$query = "
-INSERT INTO ".USER_GROUP_TABLE."
+      				pwg_query($query);
+
+										if ($conf_UAM[2] <> -1) // Delete user from unvalidated users group
+										{
+												$query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$data['user_id'].'
+AND group_id = '.$conf_UAM[2].'
+;';
+												pwg_query($query);
+										}
+
+										if ($conf_UAM[3] <> -1)
+										{
+												$query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$data['user_id'].'
+AND group_id = '.$conf_UAM[3].'
+;';
+												pwg_query($query);
+
+												$query = '
+INSERT INTO '.USER_GROUP_TABLE.'
   (user_id, group_id)
 VALUES
-  ('".$data['user_id']."', '".$conf_UAM[3]."')
-;";
-						pwg_query($query);
-					}
-
-					if (($conf_UAM[4] <> -1 or isset($data['status']))) // Change user's status
-					{
-						$query = "
-UPDATE ".USER_INFOS_TABLE."
-SET status = '".(isset($data['status']) ? $data['status'] : $conf_UAM[4])."'
-WHERE user_id = '".$data['user_id']."'
-;";
-						pwg_query($query);
-					}
-
-					if (($conf_UAM[36] <> -1 or isset($data['level']))) // Change user's privacy level
-					{
-						$query = "
-UPDATE ".USER_INFOS_TABLE."
-SET level = '".(isset($data['level']) ? $data['level'] : $conf_UAM[36])."'
-WHERE user_id = '".$data['user_id']."'
-;";
-						pwg_query($query);
-					}
-
-					// Refresh user's category cache
-          // -----------------------------
-					invalidate_user_cache();
-  
-					return true;
+  ('.$data['user_id'].', '.$conf_UAM[3].')
+;';
+												pwg_query($query);
+										}
+
+										if ($conf_UAM[4] <> -1) // Change user's status
+										{
+												$query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET status = "'.$conf_UAM[4].'"
+WHERE user_id = '.$data['user_id'].'
+;';
+												pwg_query($query);
+										}
+
+										if ($conf_UAM[36] <> -1) // Change user's privacy level
+										{
+												$query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET level = "'.$conf_UAM[36].'"
+WHERE user_id = '.$data['user_id'].'
+;';
+												pwg_query($query);
+										}
+
+										// Set UAM_validated field to True in #_users table
+										$query = '
+UPDATE '.USERS_TABLE.'
+SET UAM_validated = "true"
+WHERE id = '.$data['user_id'].'
+;';
+										pwg_query($query);
+
+										// Refresh user's category cache
+      				// -----------------------------
+										invalidate_user_cache();
+
+										return true;
+								}
+						}
 				}
-			}
+    else if (!empty($data) and !is_null($data['date_check']))
+    {
+      return false;
+    }
 		}
-    else if (!empty($data) and !is_null($data['date_check']))
-    {
-      return false;
-    }
-	}
   else
     return false;
@@ -2107,131 +2267,87 @@
 
 /**
- * Function called from UAM_admin.php to force users validation by admin
+ * Function called from UAM_admin.php for manual validation by admin
  *
  * @param : User id
  * 
  */
-function ForceValidation($id)
-{
-  global $conf;
-
-  $conf_UAM = unserialize($conf['UserAdvManager']);
-
-  if (isset($conf_UAM[1]) and $conf_UAM[1] == 'true')
-  {
-    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
-
-		$query = "
-UPDATE ".USER_CONFIRM_MAIL_TABLE."
-SET date_check='".$dbnow."'
-WHERE user_id = '".$id."'
-;";
-    pwg_query($query);
-	     
-		if ($conf_UAM[2] <> -1)
+function ManualValidation($id)
+{
+		global $conf;
+
+		$conf_UAM = unserialize($conf['UserAdvManager']);
+
+		if (isset($conf_UAM[1]) and $conf_UAM[1] == 'true') // Set date of validation
 		{
-			$query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$id."'
-  AND group_id = '".$conf_UAM[2]."'
-;";
-			pwg_query($query);
+				list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
+
+				$query = '
+UPDATE '.USER_CONFIRM_MAIL_TABLE.'
+SET date_check="'.$dbnow.'"
+WHERE user_id = '.$id.'
+;';
+				pwg_query($query);
 		}
-  
-		if ($conf_UAM[3] <> -1) // Change user's group
+
+		if ($conf_UAM[2] <> -1) // Delete user from waiting group
 		{
-			$query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$id."'
-  AND group_id = '".$conf_UAM[3]."'
-;";
-      pwg_query($query);
+				$query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$id.'
+		AND group_id = '.$conf_UAM[2].'
+;';
+				pwg_query($query);
+		}
+  
+		if ($conf_UAM[3] <> -1) // Set user's valid group
+		{
+				$query = '
+DELETE FROM '.USER_GROUP_TABLE.'
+WHERE user_id = '.$id.'
+		AND group_id = '.$conf_UAM[3].'
+;';
+				pwg_query($query);
 	
-			$query = "
-INSERT INTO ".USER_GROUP_TABLE."
-  (user_id, group_id)
+				$query = '
+INSERT INTO '.USER_GROUP_TABLE.'
+		(user_id, group_id)
 VALUES
-  ('".$id."', '".$conf_UAM[3]."')
-;";
-			pwg_query($query);
-    }
-
-		if ($conf_UAM[4] <> -1) // Change user's status
+		('.$id.', '.$conf_UAM[3].')
+;';
+				pwg_query($query);
+		}
+
+		if ($conf_UAM[4] <> -1) // Set user's valid status
 		{
-			$query = "
-UPDATE ".USER_INFOS_TABLE."
-SET status = '".$conf_UAM[4]."'
-WHERE user_id = '".$id."'
-;";
-			pwg_query($query);
+				$query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET status = "'.$conf_UAM[4].'"
+WHERE user_id = '.$id.'
+;';
+				pwg_query($query);
 		}
 
-		if ($conf_UAM[36] <> -1) // Change user's privacy level
+		if ($conf_UAM[36] <> -1) // Set user's valid privacy level
 		{
-			$query = "
-UPDATE ".USER_INFOS_TABLE."
-SET level = '".$conf_UAM[36]."'
-WHERE user_id = '".$id."'
-;";
-			pwg_query($query);
+				$query = '
+UPDATE '.USER_INFOS_TABLE.'
+SET level = "'.$conf_UAM[36].'"
+WHERE user_id = '.$id.'
+;';
+				pwg_query($query);
 		}
-  }
-  elseif (isset($conf_UAM[1]) and $conf_UAM[1] == 'local')
-  {
-    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
-
-    if ($conf_UAM[2] <> -1) // Delete user's from waiting group
-    {
-		  $query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$id."'
-  AND group_id = '".$conf_UAM[2]."'
-;";
-		  pwg_query($query);
-    }
-
-    if ($conf_UAM[3] <> -1) // Change user's group
-    {
-      $query = "
-DELETE FROM ".USER_GROUP_TABLE."
-WHERE user_id = '".$id."'
-  AND group_id = '".$conf_UAM[3]."'
-;";
-      pwg_query($query);
-	
-		  $query = "
-INSERT INTO ".USER_GROUP_TABLE."
-  (user_id, group_id)
-VALUES
-  ('".$id."', '".$conf_UAM[3]."')
-;";
-		  pwg_query($query);
-    }
-
-    if ($conf_UAM[4] <> -1) // Change user's status
-    {
-		  $query = "
-UPDATE ".USER_INFOS_TABLE."
-SET status = '".$conf_UAM[4]."'
-WHERE user_id = '".$id."'
-;";
-      pwg_query($query);
-    }
-
-    if ($conf_UAM[36] <> -1) // Change user's privacy level
-    {
-		  $query = "
-UPDATE ".USER_INFOS_TABLE."
-SET level = '".$conf_UAM[36]."'
-WHERE user_id = '".$id."'
-;";
-      pwg_query($query);
-    }
-  }
-}
-
-
-/**
- * Function called from functions.inc.php - Check if username matches forbidden caracters
+
+		// Set UAM_validated field to True in #_users table
+		$query = '
+UPDATE '.USERS_TABLE.'
+SET UAM_validated = "true"
+WHERE id = '.$id.'
+;';
+		pwg_query($query);
+}
+
+
+/**
+ * Function called from functions.inc.php - Check if username matches forbidden characters
  *
  * @param : User login
@@ -2251,5 +2367,30 @@
     for ($i = 0 ; $i < count($conf_CharExclusion) ; $i++)
     {
-      $pattern = '/'.$conf_CharExclusion[$i].'/i';
+      //Detect meta-characters (# ! ^ $ ( ) [ ] { } ? + * . \ -) for special pattern
+      if ($conf_CharExclusion[$i] == "#"
+       or $conf_CharExclusion[$i] == "$"
+       or $conf_CharExclusion[$i] == "!"
+       or $conf_CharExclusion[$i] == "^"
+       or $conf_CharExclusion[$i] == "*"
+       or $conf_CharExclusion[$i] == "?"
+       or $conf_CharExclusion[$i] == "+"
+       or $conf_CharExclusion[$i] == "."
+       or $conf_CharExclusion[$i] == "\\"
+       or $conf_CharExclusion[$i] == "|"
+       or $conf_CharExclusion[$i] == "["
+       or $conf_CharExclusion[$i] == "]"
+       or $conf_CharExclusion[$i] == "("
+       or $conf_CharExclusion[$i] == ")"
+       or $conf_CharExclusion[$i] == "{"
+       or $conf_CharExclusion[$i] == "}"
+       or $conf_CharExclusion[$i] == "-")
+      {
+        $pattern = '/[][^$.\*+?(){}#|-]/i';
+      }
+      else
+      {
+        $pattern = '/'.$conf_CharExclusion[$i].'/i';
+      }
+
       if (preg_match($pattern, $login))
       {
@@ -2280,16 +2421,16 @@
   $conf_UAM = unserialize($conf['UserAdvManager']);
   
-	if (isset($email) and isset($conf_UAM[11]) and $conf_UAM[11] <> '')
-	{
-		$conf_MailExclusion = preg_split("/[\s,]+/",$conf_UAM[11]);
-		for ($i = 0 ; $i < count($conf_MailExclusion) ; $i++)
+		if (isset($email) and isset($conf_UAM[11]) and $conf_UAM[11] <> '')
 		{
-			$pattern = '/'.$conf_MailExclusion[$i].'/i';
-			if (preg_match($pattern, $email))
-      {
-        		return true;
-      }
+				$conf_MailExclusion = preg_split("/[\s,]+/",$conf_UAM[11]);
+				for ($i = 0 ; $i < count($conf_MailExclusion) ; $i++)
+				{
+						$pattern = '/'.$conf_MailExclusion[$i].'/i';
+						if (preg_match($pattern, $email))
+      {
+      		return true;
+      }
+				}
 		}
-	}
   else
   {
@@ -2307,7 +2448,7 @@
 function get_unvalid_user_list()
 {
-	global $conf, $page;
+		global $conf, $page;
           
-	// Get ConfirmMail configuration
+		// Get ConfirmMail configuration
   // -----------------------------
   $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
@@ -2318,5 +2459,5 @@
   $users = array();
 
-	// Search users depending expiration date
+		// Search users depending expiration date
   // --------------------------------------
   $query = '
@@ -2335,48 +2476,32 @@
 WHERE u.'.$conf['user_fields']['id'].' >= 3
   AND (TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) >= "'.$conf_UAM_ConfirmMail[1].'"
-  OR TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) < "'.$conf_UAM_ConfirmMail[1].'")';
-
-	if ($conf_UAM[2] <> '-1' and $conf_UAM[7] == '-1')
-  {
-    $query.= '
-  AND ug.group_id = '.$conf_UAM[2];
-  }
-  if ($conf_UAM[2] == '-1' and $conf_UAM[7] <> '-1')
-  {
-    $query.= '
-  AND ui.status = \''.$conf_UAM[7]."'";
-  }
-  if ($conf_UAM[2] <> '-1' and $conf_UAM[7] <> '-1')
-  {
-    $query.= '
-  AND ug.group_id = \''.$conf_UAM[2]."'";
-  }
-  $query.= '
+  OR TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) < "'.$conf_UAM_ConfirmMail[1].'")
+		AND u.UAM_validated = "false"
 ORDER BY ui.registration_date ASC
 ;';
 
-	$result = pwg_query($query);
+		$result = pwg_query($query);
       
   while ($row = pwg_db_fetch_assoc($result))
   {
-  	$user = $row;
+  		$user = $row;
     $user['groups'] = array();
 
     array_push($users, $user);
-	}
-
-	// Add groups list
+		}
+
+		// Add groups list
   // ---------------
   $user_ids = array();
   foreach ($users as $i => $user)
   {
-  	$user_ids[$i] = $user['id'];
-	}
-	
-	$user_nums = array_flip($user_ids);
+  		$user_ids[$i] = $user['id'];
+		}
+
+		$user_nums = array_flip($user_ids);
 
   if (count($user_ids) > 0)
   {
-  	$query = '
+  		$query = '
 SELECT user_id, group_id
   FROM '.USER_GROUP_TABLE.'
@@ -2384,16 +2509,16 @@
 ;';
         
-		$result = pwg_query($query);
+				$result = pwg_query($query);
         
     while ($row = pwg_db_fetch_assoc($result))
     {
-    	array_push(
-      	$users[$user_nums[$row['user_id']]]['groups'],
+    		array_push(
+      		$users[$user_nums[$row['user_id']]]['groups'],
         $row['group_id']
-			);
+						);
+				}
 		}
-	}
-
-	return $users;
+
+		return $users;
 }
 
@@ -2408,7 +2533,7 @@
 function get_unvalid_user_autotasks()
 {
-	global $conf, $page;
+		global $conf, $page;
           
-	// Get ConfirmMail configuration
+		// Get ConfirmMail configuration
   // -----------------------------
   $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
@@ -2416,5 +2541,5 @@
   $users = array();
 
-	// search users depending expiration date
+		// search users depending expiration date
   // --------------------------------------
   $query = '
@@ -2428,12 +2553,12 @@
 ORDER BY ui.registration_date ASC;';
 
-	$result = pwg_query($query);
-      
+		$result = pwg_query($query);
+
   while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($users, $row);
-	}
-
-	return $users;
+		}
+
+		return $users;
 }
 
@@ -2447,11 +2572,11 @@
 function get_ghost_user_list()
 {
-	global $conf, $page;
+		global $conf, $page;
 
   $conf_UAM = unserialize($conf['UserAdvManager']);
-  
+
   $users = array();
 
-	// Search users depending expiration date
+		// Search users depending expiration date
   // --------------------------------------
   $query = '
@@ -2467,15 +2592,15 @@
 ORDER BY lv.lastvisit ASC;';
 
-	$result = pwg_query($query);
+		$result = pwg_query($query);
       
   while ($row = pwg_db_fetch_assoc($result))
   {
-  	$user = $row;
+  		$user = $row;
     $user['groups'] = array();
 
     array_push($users, $user);
-	}
-
-	// Add groups list
+		}
+
+		// Add groups list
   // ---------------
   $user_ids = array();
@@ -2483,7 +2608,7 @@
   {
   	$user_ids[$i] = $user['id'];
-	}
-
-	return $users;
+		}
+
+		return $users;
 }
 
@@ -2497,5 +2622,5 @@
 function get_ghosts_autotasks()
 {
-	global $conf, $page;
+		global $conf, $page;
 
   $conf_UAM = unserialize($conf['UserAdvManager']);
@@ -2503,5 +2628,5 @@
   $users = array();
   
-	// Search users depending expiration date and reminder sent
+		// Search users depending expiration date and reminder sent
   // --------------------------------------------------------
   $query = '
@@ -2514,12 +2639,12 @@
 ORDER BY lv.lastvisit ASC;';
 
-	$result = pwg_query($query);
+		$result = pwg_query($query);
       
-  while ($row = pwg_db_fetch_assoc($result))
+		while ($row = pwg_db_fetch_assoc($result))
   {
     array_push($users, $row);
-	}
-  
-	return $users;
+		}
+  
+		return $users;
 }
 
@@ -2533,9 +2658,9 @@
 function get_user_list()
 {
-	global $conf, $page;
+		global $conf, $page;
   
   $users = array();
 
-	// Search users depending expiration date with exclusion of Adult_Content generic users
+		// Search users depending expiration date with exclusion of Adult_Content generic users
   // ------------------------------------------------------------------------------------
   $query = '
@@ -2553,23 +2678,23 @@
 ;';
 
-	$result = pwg_query($query);
+		$result = pwg_query($query);
       
   while ($row = pwg_db_fetch_assoc($result))
   {
-  	$user = $row;
+  		$user = $row;
     $user['groups'] = array();
 
     array_push($users, $user);
-	}
-
-	// Add groups list
+		}
+
+		// Add groups list
   // ---------------
   $user_ids = array();
   foreach ($users as $i => $user)
   {
-  	$user_ids[$i] = $user['id'];
-	}
-
-	return $users;
+ 			$user_ids[$i] = $user['id'];
+		}
+
+		return $users;
 }
 
@@ -2587,47 +2712,47 @@
 	global $conf, $page;
           
-	// Get ConfirmMail configuration
+		// Get ConfirmMail configuration
   // -----------------------------
   $conf_UAM_ConfirmMail = unserialize($conf['UserAdvManager_ConfirmMail']);
           
-	// Get UAM configuration
+		// Get UAM configuration
   // ---------------------
   $conf_UAM = unserialize($conf['UserAdvManager']);
 	
-	$query = "
+		$query = '
 SELECT registration_date
-  FROM ".USER_INFOS_TABLE."
-WHERE user_id = '".$id."'
-;";
-	list($registration_date) = pwg_db_fetch_row(pwg_query($query));
+  FROM '.USER_INFOS_TABLE.'
+WHERE user_id = '.$id.'
+;';
+		list($registration_date) = pwg_db_fetch_row(pwg_query($query));
 
 //              Time limit process             
 // ********************************************  
-	if (!empty($registration_date))
-  {
-		// Dates formating and compare
-    // ---------------------------
-		$today = date("d-m-Y"); // Get today's date
-		list($day, $month, $year) = explode('-', $today); // explode date of today						 
- 		$daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
+		if (!empty($registration_date))
+  {
+				// Dates formating and compare
+  		// ---------------------------
+				$today = date("d-m-Y"); // Get today's date
+				list($day, $month, $year) = explode('-', $today); // explode date of today						 
+ 			$daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
 	  	
-	  list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
-		list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
-		$regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
+	  	list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
+				list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
+				$regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
 			
-		$deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps	
-		$deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
-
-		// Condition with the value set for time limit
+				$deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps	
+				$deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
+
+				// Condition with the value set for time limit
     // -------------------------------------------
-		if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
-		{
-			return false;
+				if ($deltadays <= $conf_UAM_ConfirmMail[1]) // If Nb of days is less than the limit set
+				{
+						return false;
+				}
+				else
+				{
+						return true;
+				}
 		}
-		else
-		{
-			return true;
-		}
-	}
 }
 
@@ -2642,9 +2767,9 @@
  * Thanx to MathieuGut from http://m-gut.developpez.com
  */
-function testpassword($password) // Le mot de passe passé en paramètre - $password given by user
-{
-
-  // Initialisation des variables - Variables initiation
-  // ---------------------------------------------------
+function testpassword($password) // $password given by user
+{
+
+  // Variables initiation
+  // --------------------
   $points = 0;
   $point_lowercase = 0;
@@ -2653,72 +2778,72 @@
   $point_characters = 0;
 
-  // On récupère la longueur du mot de passe - Getting password lengh
-  // ----------------------------------------------------------------
+  // Getting password lengh
+  // ----------------------
   $length = strlen($password);
-  
-  // On fait une boucle pour lire chaque lettre - Loop to read password characters
+
+  // Loop to read password characters
   for($i = 0; $i < $length; $i++)
   {
-    // On sélectionne une à une chaque lettre - Select each letters
-    // $i étant à 0 lors du premier passage de la boucle - $i is 0 at first turn
-    // -------------------------------------------------------------------------
+    // Select each letters
+    // $i is 0 at first turn
+    // ---------------------
     $letters = $password[$i];
 
     if ($letters>='a' && $letters<='z')
     {
-      // On ajoute 1 point pour une minuscule - Adding 1 point to score for a lowercase
-      // ------------------------------------------------------------------------------
-		  $points = $points + 1;
-
-		  // On rajoute le bonus pour une minuscule - Adding bonus points for lowercase
-      // --------------------------------------------------------------------------
+      // Adding 1 point to score for a lowercase
+      // ---------------------------------------
+		  		$points = $points + 1;
+
+		  		// Adding bonus points for lowercase
+      // ---------------------------------
 		  $point_lowercase = 1;
     }
     else if ($letters>='A' && $letters <='Z')
     {
-      // On ajoute 2 points pour une majuscule - Adding 2 points to score for uppercase
-      // ------------------------------------------------------------------------------
+      // Adding 2 points to score for uppercase
+      // --------------------------------------
       $points = $points + 2;
 		
-      // On rajoute le bonus pour une majuscule - Adding bonus points for uppercase
-      // --------------------------------------------------------------------------
+      // Adding bonus points for uppercase
+      // ---------------------------------
       $point_uppercase = 2;
     }
     else if ($letters>='0' && $letters<='9')
     {
-      // On ajoute 3 points pour un chiffre - Adding 3 points to score for numbers
-      // -------------------------------------------------------------------------
+      // Adding 3 points to score for numbers
+      // ------------------------------------
       $points = $points + 3;
 		
-      // On rajoute le bonus pour un chiffre - Adding bonus points for numbers
-      // ---------------------------------------------------------------------
+      // Adding bonus points for numbers
+      // -------------------------------
       $point_numbers = 3;
     }
     else
     {
-      // On ajoute 5 points pour un caractère autre - Adding 5 points to score for special characters
-      // --------------------------------------------------------------------------------------------
+      // Adding 5 points to score for special characters
+      // -----------------------------------------------
       $points = $points + 5;
 		
-      // On rajoute le bonus pour un caractère autre - Adding bonus points for special characters
-      // ----------------------------------------------------------------------------------------
+      // Adding bonus points for special characters
+      // ------------------------------------------
       $point_characters = 5;
     }
   }
 
-  // Calcul du coefficient points/longueur - calculating the coefficient points/length
-  // ---------------------------------------------------------------------------------
+  // Calculating the coefficient points/length
+  // -----------------------------------------
   $step1 = $points / $length;
 
-  // Calcul du coefficient de la diversité des types de caractères... - Calculation of the diversity of character types...
-  // ---------------------------------------------------------------------------------------------------------------------
+  // Calculation of the diversity of character types...
+  // --------------------------------------------------
   $step2 = $point_lowercase + $point_uppercase + $point_numbers + $point_characters;
 
-  // Multiplication du coefficient de diversité avec celui de la longueur - Multiplying the coefficient of diversity with that of the length
-  // --------------------------------------------------------------------------------------------------------------------------------------------
+  // Multiplying the coefficient of diversity with that of the length
+  // ----------------------------------------------------------------
   $score = $step1 * $step2;
 
-  // Multiplication du resultat par la longueur de la chaine - Multiplying the result by the length of the string
-  // ------------------------------------------------------------------------------------------------------------
+  // Multiplying the result by the length of the string
+  // --------------------------------------------------
   $finalscore = $score * $length;
 
@@ -2799,25 +2924,35 @@
   global $conf;
 
-	// Get UAM configuration
-  // ---------------------
-  $conf_UAM = unserialize($conf['UserAdvManager']);
-
   $query = '
-SELECT group_id
-  FROM '.USER_GROUP_TABLE.'
-WHERE user_id = '.$user_id.'
-  AND group_id = '.$conf_UAM[2].'
-;';
-
-  $count = pwg_db_num_rows(pwg_query($query));
-  
-  if ($count == 0)
-  {
-    return true; // User is not in a "Waiting" group
-  }
-  else
-  {
-    return false; // User is still in a "Waiting" group
-  }
+SELECT UAM_validated
+FROM '.USERS_TABLE.'
+WHERE id='.$user_id.'
+;';
+
+  $result = pwg_db_fetch_assoc(pwg_query($query));
+
+  if($result['UAM_validated'] == 'true')
+  {
+    return true;
+  }
+  else return false;
+}
+
+
+/**
+ * SetUnvalidated
+ * Set UAM_validated field to false in #_users table
+ * 
+ **/
+function SetUnvalidated($user_id)
+{
+  $query ='
+UPDATE '.USERS_TABLE.'
+SET UAM_validated = "false"
+WHERE id = '.$user_id.'
+LIMIT 1
+;';
+
+  pwg_query($query);
 }
 
@@ -2921,5 +3056,5 @@
   $ListTables = array(USER_CONFIRM_MAIL_TABLE, USER_LASTVISIT_TABLE);
   $j=0;
-  
+
   while($j < count($ListTables))
   {
@@ -3230,3 +3365,4 @@
    fclose($fo) ;
 }
+
 ?>
