source: extensions/oAuth/include/public_events.inc.php @ 23815

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

compatibility fix with Crypto Captcha

File size: 7.5 KB
Line 
1<?php
2defined('OAUTH_PATH') or die('Hacking attempt!');
3
4/**
5 * identification page
6 */
7function oauth_begin_identification()
8{
9  global $template, $conf;
10 
11  oauth_assign_template_vars();
12  $template->assign('REDIRECT_TO', !empty($_GET['redirect']) ? urldecode($_GET['redirect']) : get_gallery_home_url());
13 
14  $template->set_prefilter('identification', 'oauth_add_buttons_prefilter');
15}
16
17/**
18 * interrupt normal login if corresponding to an oauth user
19 */
20function oauth_try_log_user($success, $username)
21{
22  global $conf, $redirect_to;
23 
24  $query = '
25SELECT oauth_id FROM '.USERS_TABLE.'
26  WHERE '.$conf['user_fields']['username'].' = "'.pwg_db_real_escape_string($username).'"
27  AND oauth_id != ""
28;';
29  $result = pwg_query($query);
30 
31  if (pwg_db_num_rows($result))
32  {
33    list($oauth_id) = pwg_db_fetch_row($result);
34    list($provider) = explode('---', $oauth_id);
35    $_SESSION['page_errors'][] = sprintf(l10n('You registered with a %s account, please sign in with the same account.'), $provider);
36   
37    $redirect_to = get_root_url().'identification.php'; // variable used by identification.php
38    return true;
39  }
40 
41  return false;
42}
43
44
45/**
46 * register page
47 */
48function oauth_begin_register()
49{
50  global $conf, $template, $hybridauth_conf, $page;
51 
52  // coming from identification page
53  if (pwg_get_session_var('oauth_new_user') != null)
54  {
55    list($provider, $user_identifier) = pwg_get_session_var('oauth_new_user');
56   
57    require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
58   
59    try {
60      $hybridauth = new Hybrid_Auth($hybridauth_conf);
61      $adapter = $hybridauth->authenticate($provider);
62      $remote_user = $adapter->getUserProfile();
63     
64      // security, check remote identifier
65      if ($remote_user->identifier != $user_identifier)
66      {
67        pwg_unset_session_var('oauth_new_user');
68        throw new Exception('Hacking attempt!', 403);
69      }
70   
71      $template->assign(array(
72        'OAUTH_PROVIDER' => $provider,
73        'OAUTH_USERNAME' => $remote_user->displayName,
74        'OAUTH_PROFILE_URL' => $remote_user->profileURL,
75        'OAUTH_AVATAR' => $remote_user->photoURL,
76        'OAUTH_PATH' => OAUTH_PATH,
77        ));
78       
79      array_push($page['infos'], l10n('Your registration is almost done, please complete the registration form.'));
80     
81      $oauth_id = $provider.'---'.$remote_user->identifier;
82     
83      // form submited
84      if (isset($_POST['submit']))
85      {
86        $page['errors'] =
87          register_user($_POST['login'],
88                        hash('sha1', $oauth_id.$conf['secret_key']),
89                        $_POST['mail_address']
90                        );
91                       
92        if (count($page['errors']) == 0)
93        {
94          pwg_unset_session_var('oauth_new_user');
95          $user_id = get_userid($_POST['login']);
96         
97          // update oauth field
98          $query = '
99UPDATE '.USERS_TABLE.'
100  SET oauth_id = "'.$oauth_id.'"
101  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
102;';
103          pwg_query($query);
104         
105          // log_user and redirect
106          log_user($user_id, false);
107          redirect('profile.php');
108        }
109     
110        unset($_POST['submit']);
111      }
112      else
113      {
114        // overwrite fields with remote datas
115        $_POST['login'] = $remote_user->displayName;
116        $_POST['mail_address'] = $remote_user->email;
117      }
118     
119      // template
120      $template->set_prefilter('register', 'oauth_add_profile_prefilter');
121      $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
122    }
123    catch (Exception $e) {
124      array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
125    }
126  }
127  // display login buttons
128  else if ($conf['oauth']['display_register'])
129  {
130    oauth_assign_template_vars();
131    $template->assign('REDIRECT_TO', get_gallery_home_url());
132   
133    $template->set_prefilter('register', 'oauth_add_buttons_prefilter');
134  }
135}
136
137
138/**
139 * profile page
140 */
141function oauth_begin_profile()
142{
143  global $template, $user, $conf, $hybridauth_conf, $page;
144 
145  $query = '
146SELECT oauth_id FROM '.USERS_TABLE.'
147  WHERE '.$conf['user_fields']['id'].' = '.$user['id'].'
148  AND oauth_id != ""
149;';
150  $result = pwg_query($query);
151 
152  if (!pwg_db_num_rows($result))
153  {
154    return;
155  }
156 
157  list($oauth_id) = pwg_db_fetch_row($result);
158  list($provider) = explode('---', $oauth_id);
159 
160  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
161 
162  try {
163    $hybridauth = new Hybrid_Auth($hybridauth_conf);
164    $adapter = $hybridauth->getAdapter($provider);
165    $remote_user = $adapter->getUserProfile();
166   
167    $template->assign(array(
168      'OAUTH_PROVIDER' => $provider,
169      'OAUTH_USERNAME' => $remote_user->displayName,
170      'OAUTH_PROFILE_URL' => $remote_user->profileURL,
171      'OAUTH_AVATAR' => $remote_user->photoURL,
172      'OAUTH_PATH' => OAUTH_PATH,
173      ));
174   
175    $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter');
176    $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter');
177  }
178  catch (Exception $e) {
179    array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
180  }
181}
182
183
184/**
185 * logout
186 */
187function oauth_logout($user_id)
188{
189  global $conf, $hybridauth_conf;
190 
191  $query = '
192SELECT oauth_id FROM '.USERS_TABLE.'
193  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
194  AND oauth_id != ""
195;';
196  $result = pwg_query($query);
197 
198  if (!pwg_db_num_rows($result))
199  {
200    return;
201  }
202 
203  list($oauth_id) = pwg_db_fetch_row($result);
204  list($provider) = explode('---', $oauth_id);
205 
206  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
207 
208  try {
209    $hybridauth = new Hybrid_Auth($hybridauth_conf);
210    $adapter = $hybridauth->getAdapter($provider);
211    $adapter->logout();
212  }
213  catch (Exception $e) {
214    $_SESSION['page_errors'][] = sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode());
215  }
216}
217
218
219/**
220 * identification menu block
221 */
222function oauth_blockmanager($menu_ref_arr)
223{
224  global $template, $conf;
225 
226  $menu = &$menu_ref_arr[0]; 
227 
228  if ( !$conf['oauth']['display_menubar'] or $menu->get_block('mbIdentification') == null )
229  {
230    return;
231  }
232 
233  oauth_assign_template_vars();
234  $template->assign('REDIRECT_TO', get_gallery_home_url());
235 
236  $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter');
237}
238
239
240/**
241 * prefilters
242 */
243function oauth_add_buttons_prefilter($content)
244{
245  $search = '</form>';
246  $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl');
247  return str_replace($search, $search.$add, $content);
248}
249
250function oauth_remove_password_fields_prefilter($content)
251{
252  $search = 'type="password" ';
253  $add = 'disabled="disabled" ';
254  $script = '
255{footer_script require="jquery"}
256jQuery("input[type=\'password\'], input[name=\'send_password_by_mail\']").parent().css("display", "none");
257{/footer_script}';
258
259  $content = str_replace($search, $search.$add, $content);
260  return $content.$script;
261}
262
263function oauth_add_profile_prefilter($content)
264{
265  $search = '#</legend>#';
266  $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl');
267  return preg_replace($search, '</legend> '.$add, $content, 1);
268}
269
270function oauth_add_menubar_buttons_prefilter($content)
271{
272  $search = '{include file=$block->template|@get_extent:$id }';
273  $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl');
274  return str_replace($search, $search.$add, $content);
275}
276
277?>
Note: See TracBrowser for help on using the repository browser.