source: extensions/oAuth/auth.php @ 20368

Last change on this file since 20368 was 20368, checked in by mistic100, 11 years ago

complete Instagram help

File size: 3.3 KB
Line 
1<?php
2define('PHPWG_ROOT_PATH', '../../');
3include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
4
5global $hybridauth_conf;
6require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
7
8$provider = @$_GET['provider'];
9
10try {
11  // inputs
12  if ( $provider == 'OpenID' and !isset($_GET['openid_identifier']) )
13  {
14    throw new Exception('Invalid OpenID!', 1003);
15  }
16 
17  if (
18    !array_key_exists($provider, $hybridauth_conf['providers'])
19    or !$hybridauth_conf['providers'][$provider]['enabled']
20  ) {
21    throw new Exception('Hacking attempt!', 1002);
22  }
23 
24  $hybridauth = new Hybrid_Auth($hybridauth_conf);
25 
26  // connected
27  if ($hybridauth->isConnectedWith($provider))
28  {
29    $adapter = $hybridauth->getAdapter($provider);
30    $remote_user = $adapter->getUserProfile();
31   
32    $oauth_id = $provider.'---'.$remote_user->identifier;
33   
34    // check is already registered
35    $query = '
36SELECT id FROM '.USERS_TABLE.'
37  WHERE oauth_id = "'.$oauth_id.'"
38;';
39    $result = pwg_query($query);
40    // registered : log_user and redirect
41    if (pwg_db_num_rows($result))
42    {
43      list($user_id) = pwg_db_fetch_row($result);
44      log_user($user_id, false);
45     
46      $template->assign('REDIRECT_TO', 'default');
47    }
48    // not registered : redirect to register page
49    else
50    {
51      if ($conf['allow_user_registration'])
52      {
53        pwg_set_session_var('oauth_new_user', array($provider,$remote_user->identifier));
54        $template->assign('REDIRECT_TO', 'register');
55      }
56      else
57      {
58        $_SESSION['page_errors'][] = l10n('Sorry, new registrations are blocked on this gallery.');
59        $adapter->logout();
60        $template->assign('REDIRECT_TO', 'identification');
61      }
62    }
63  }
64  // init connect
65  else if (isset($_GET['init_auth']))
66  {
67    if ($provider == 'OpenID')
68    {
69      $params['openid_identifier'] = $_GET['openid_identifier'];
70    }
71     
72    // try to authenticate
73    $adapter = $hybridauth->authenticate($provider, $params);
74  }
75  // display loader
76  else
77  {
78    $template->assign('LOADING', '&openid_identifier='.@$_GET['openid_identifier'].'&init_auth=1');
79  }
80}
81/*
82 library errors :
83     0 : Unspecified error
84     1 : Hybriauth configuration error
85     2 : Provider not properly configured
86     3 : Unknown or disabled provider
87     4 : Missing provider application credentials
88     5 : Authentication aborded
89     6 : User profile request failed
90 other errors :
91  1002 : Invalid provider
92  1003 : Missing openid_identifier
93*/
94catch (Exception $e) {
95  switch ($e->getCode()) {
96    case 5:
97      $template->assign('ERROR', l10n('Authentication canceled')); break;
98    case 404:
99      $template->assign('ERROR', l10n('User not found')); break;
100    default:
101      $template->assign('ERROR', sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
102  }
103}
104
105
106$template->assign(array(
107  'GALLERY_TITLE' => $conf['gallery_title'],
108  'CONTENT_ENCODING' => get_pwg_charset(),
109  'U_HOME' => get_gallery_home_url(),
110 
111  'OAUTH_PATH' => OAUTH_PATH,
112  'PROVIDER' => $provider,
113  'SELF_URL' => OAUTH_PATH . 'auth.php?provider='.$provider,
114  ));
115
116$template->set_filename('index', realpath(OAUTH_PATH . 'template/auth.tpl'));
117$template->pparse('index');
118?>
Note: See TracBrowser for help on using the repository browser.