Index: /extensions/oAuth/admin/config.php
===================================================================
--- /extensions/oAuth/admin/config.php	(revision 26625)
+++ /extensions/oAuth/admin/config.php	(revision 26626)
@@ -8,4 +8,5 @@
     'display_register' => isset($_POST['display_register']),
     'display_menubar' => isset($_POST['display_menubar']),
+    'allow_merge_accounts' => isset($_POST['allow_merge_accounts']),
     'identification_icon' => $_POST['identification_icon'],
     'menubar_icon' => $_POST['menubar_icon'],
Index: /extensions/oAuth/admin/template/config.tpl
===================================================================
--- /extensions/oAuth/admin/template/config.tpl	(revision 26625)
+++ /extensions/oAuth/admin/template/config.tpl	(revision 26626)
@@ -20,4 +20,11 @@
         <input type="checkbox" name="display_register" {if $display_register}checked="checked"{/if}>
         <b>{'Display sign in buttons on the register page'|translate}</b>
+      </label>
+    </li>
+
+    <li>
+      <label>
+        <input type="checkbox" name="allow_merge_accounts" {if $allow_merge_accounts}checked="checked"{/if}>
+        <b>{'Allow users to merge existing account with new <i>Social Connect</i> identity'|translate}</b>
       </label>
     </li>
Index: /extensions/oAuth/auth.php
===================================================================
--- /extensions/oAuth/auth.php	(revision 26625)
+++ /extensions/oAuth/auth.php	(revision 26626)
@@ -33,4 +33,5 @@
     {
       header('HTTP/1.1 503 Service Unavailable');
+      echo json_encode($response);
       exit;
     }
Index: /extensions/oAuth/include/public_events.inc.php
===================================================================
--- /extensions/oAuth/include/public_events.inc.php	(revision 26625)
+++ /extensions/oAuth/include/public_events.inc.php	(revision 26626)
@@ -56,5 +56,5 @@
 function oauth_begin_register()
 {
-  global $conf, $template, $hybridauth_conf, $page;
+  global $conf, $template, $hybridauth_conf, $page, $user;
   
   if ($hybridauth_conf['enabled'] == 0)
@@ -110,5 +110,5 @@
       $page['infos'][] = l10n('Your registration is almost done, please complete the registration form.');
       
-      // form submited
+      // register form submited
       if (isset($_POST['submit']))
       {
@@ -141,4 +141,31 @@
         unset($_POST['submit']);
       }
+      // login form submited
+      else if (isset($_POST['login']) && $conf['oauth']['allow_merge_accounts'])
+      {
+        if ($conf['insensitive_case_logon'] == true)
+        {
+          $_POST['username'] = search_case_username($_POST['username']);
+        }
+        
+        if ( pwg_login(false, $_POST['username'], $_POST['password'], false) )
+        {
+          pwg_unset_session_var('oauth_new_user');
+          
+          // update oauth field
+          $query = '
+UPDATE ' . USER_INFOS_TABLE . '
+  SET oauth_id = "' . $oauth_id . '"
+  WHERE user_id = ' . $user['id'] . '
+;';
+          pwg_query($query);
+
+          redirect('profile.php');
+        }
+        else
+        {
+          $page['errors'][] = l10n('Invalid password!');
+        }
+      }
       else
       {
@@ -158,6 +185,14 @@
       // template
       $template->assign('OAUTH_PATH', OAUTH_PATH);
-      $template->set_prefilter('register', 'oauth_add_profile_prefilter');
-      $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
+      if ($conf['oauth']['allow_merge_accounts'])
+      {
+        $template->assign('OAUTH_LOGIN_IN_REGISTER', true);
+        $template->set_prefilter('register', 'oauth_add_login_in_register');
+      }
+      else
+      {
+        $template->set_prefilter('register', 'oauth_add_profile_prefilter');
+        $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
+      }
     }
     catch (Exception $e)
@@ -335,2 +370,13 @@
   return preg_replace($search, '$1 '.$add, $content);
 }
+
+function oauth_add_login_in_register($content)
+{
+  $search[0] = '<form method="post" action="{$F_ACTION}"';
+  $replace[0] = file_get_contents(OAUTH_PATH . 'template/register.tpl') . $search[0];
+  
+  $search[1] = '{\'Enter your personnal informations\'|@translate}';
+  $replace[1] = '{\'Create a new account\'|@translate}';
+  
+  return str_replace($search, $replace, $content);
+}
Index: /extensions/oAuth/language/en_UK/plugin.lang.php
===================================================================
--- /extensions/oAuth/language/en_UK/plugin.lang.php	(revision 26625)
+++ /extensions/oAuth/language/en_UK/plugin.lang.php	(revision 26626)
@@ -28,4 +28,6 @@
 $lang['Profile URL'] = 'Profile URL';
 $lang['Cancel'] = 'Cancel';
+$lang['Associate with an existing account'] = 'Associate with an existing account';
+$lang['Allow users to merge existing account with new <i>Social Connect</i> identity'] = 'Allow users to merge existing account with new <i>Social Connect</i> identity';
 
 ?>
Index: /extensions/oAuth/language/fr_FR/plugin.lang.php
===================================================================
--- /extensions/oAuth/language/fr_FR/plugin.lang.php	(revision 26625)
+++ /extensions/oAuth/language/fr_FR/plugin.lang.php	(revision 26626)
@@ -26,5 +26,6 @@
 $lang['Logged with'] = 'Connecté avec';
 $lang['Profile URL'] = 'URL du profil';
-
+$lang['Associate with an existing account'] = 'Associer à un compte existant';
+$lang['Allow users to merge existing account with new <i>Social Connect</i> identity'] = 'Autoriser les utilisateur à fusionner un compte existant avec leur identité <i>Social Connect</i>';
 $lang['Cancel'] = 'Annuler';
 $lang['Please enter your user ID'] = 'Veuillez entrer votre ID d\'utilisateur';
Index: /extensions/oAuth/main.inc.php
===================================================================
--- /extensions/oAuth/main.inc.php	(revision 26625)
+++ /extensions/oAuth/main.inc.php	(revision 26626)
@@ -66,5 +66,5 @@
 function oauth_init()
 {
-  global $conf, $page, $hybridauth_conf;
+  global $conf, $page, $hybridauth_conf, $template;
   
   include_once(OAUTH_PATH . 'maintain.inc.php');
@@ -90,18 +90,24 @@
   
   // in case of registration aborded
-  // DON'T WORK, because potentially executed by sub-scripts like Secureimage for Crypto Captcha
-  // if ( script_basename() != 'register' and ($data=pwg_get_session_var('oauth_new_user')) !== null )
-  // {
-    // pwg_unset_session_var('oauth_new_user');
+  if ( script_basename() == 'index' and ($oauth_id=pwg_get_session_var('oauth_new_user')) !== null )
+  {
+    pwg_unset_session_var('oauth_new_user');
     
-    // require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
-    
-    // try {
-      // $hybridauth = new Hybrid_Auth($hybridauth_conf);
-      // $adapter = $hybridauth->getAdapter($data[0]);
-      // $adapter->logout();
-    // }
-    // catch (Exception $e) {
-    // }
-  // }
+    if ($oauth_id[0] == 'Persona')
+    {
+      oauth_assign_template_vars(get_gallery_home_url());
+      $template->block_footer_script(null, 'navigator.id.logout();');
+    }
+    else
+    {
+      require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
+      
+      try {
+        $hybridauth = new Hybrid_Auth($hybridauth_conf);
+        $adapter = $hybridauth->getAdapter($oauth_id[0]);
+        $adapter->logout();
+      }
+      catch (Exception $e) {}
+    }
+  }
 }
Index: /extensions/oAuth/maintain.inc.php
===================================================================
--- /extensions/oAuth/maintain.inc.php	(revision 26625)
+++ /extensions/oAuth/maintain.inc.php	(revision 26626)
@@ -11,4 +11,5 @@
     'identification_icon' => '38px',
     'menubar_icon' => '26px',
+    'allow_merge_accounts' => true,
     );
     
@@ -30,4 +31,16 @@
       $conf['oauth'] = serialize($this->default_conf);
       conf_update_param('oauth', $conf['oauth']);
+    }
+    else
+    {
+      $old_conf = is_string($conf['oauth']) ? unserialize($conf['oauth']) : $conf['oauth'];
+      
+      if (!isset($old_conf['allow_merge_accounts']))
+      {
+        $old_conf['allow_merge_accounts'] = true;
+        
+        $conf['oauth'] = serialize($old_conf);
+        conf_update_param('oauth', $conf['oauth']);
+      }
     }
     
@@ -55,5 +68,5 @@
     }
     
-    // add fields in hybridauth conf file
+    // add 'total' and 'enabled' fields in hybridauth conf file
     if (file_exists($this->file))
     {
Index: /extensions/oAuth/template/register.tpl
===================================================================
--- /extensions/oAuth/template/register.tpl	(revision 26626)
+++ /extensions/oAuth/template/register.tpl	(revision 26626)
@@ -0,0 +1,75 @@
+{html_style}
+form[name=register_form], form[name=login_form] { 
+  width:49%;
+  display:inline-block;
+  vertical-align:top;
+}
+#oauth {
+  width:400px;
+  height:48px;
+  overflow:hidden;
+  margin:0 auto 15px auto;
+  padding:5px;
+  background:rgba(128,128,128,0.2);
+  border:1px solid #7e7e7e;
+  border-radius:5px;
+}
+#oauth .avatar {
+  width:48px;
+  border-radius:5px;
+  margin-right:5px;
+  float:left;
+}
+{/html_style}
+
+{footer_script require="jquery"}
+jQuery("form[name=register_form] input[type=password], form[name=register_form] input[name=send_password_by_mail]").parent().hide();
+{/footer_script}
+
+
+<div id="oauth">
+{if $OAUTH_USER.avatar}
+  <img src="{$OAUTH_USER.avatar}" class="avatar">
+{else}
+  <img src="{$ROOT_URL}{$OAUTH_PATH}template/images/avatar-default.png" class="avatar">
+{/if}
+
+  {'Logged with'|translate} : <b>{$OAUTH_USER.provider}</b><br>
+  <b>{'Username'|translate}</b> : {$OAUTH_USER.username}<br>
+  {if $OAUTH_USER.u_profile}<b>{'Profile URL'|translate}</b> : <a href="{$OAUTH_USER.u_profile}">{$OAUTH_USER.u_profile|truncate:40:' ... ':true:true}</a>{/if}
+</div>
+
+<form action="{$F_ACTION}" method="post" name="login_form" class="properties">
+  <fieldset>
+    <legend>{'Associate with an existing account'|@translate}</legend>
+
+    <ul>
+      <li>
+        <span class="property">
+          <label for="username">* {'Username'|@translate}</label>
+        </span>
+        <input tabindex="1" class="login" type="text" name="username" id="username" size="25" maxlength="40">
+      </li>
+
+      <li>
+        <span class="property">
+          <label for="password">* {'Password'|@translate}</label>
+        </span>
+        <input tabindex="2" class="login" type="password" name="password" id="password" size="25" maxlength="25">
+      </li>
+    </ul>
+  </fieldset>
+
+  <p>
+    <input tabindex="4" type="submit" name="login" value="{'Submit'|@translate}">
+  </p>
+
+{if isset($U_LOST_PASSWORD)}
+	<p>
+		<a href="{$U_LOST_PASSWORD}" title="{'Forgot your password?'|@translate}" class="pwg-state-default pwg-button">
+			<span class="pwg-icon pwg-icon-lost-password">&nbsp;</span><span>{'Forgot your password?'|@translate}</span>
+		</a>
+	</p>
+{/if}
+
+</form>
