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

Last change on this file since 26667 was 26626, checked in by mistic100, 10 years ago

add 'allow_merge_accounts' parameter

File size: 10.0 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, $hybridauth_conf;
10 
11  if ($hybridauth_conf['enabled'] == 0)
12  {
13    return;
14  }
15
16  $u_redirect = !empty($_GET['redirect']) ? urldecode($_GET['redirect']) : get_gallery_home_url();
17  oauth_assign_template_vars($u_redirect);
18
19  $template->set_prefilter('identification', 'oauth_add_buttons_prefilter');
20}
21
22/**
23 * interrupt normal login if corresponding to an oauth user
24 */
25function oauth_try_log_user($success, $username)
26{
27  global $conf, $redirect_to;
28 
29  $query = '
30SELECT oauth_id
31  FROM ' . USER_INFOS_TABLE . ' AS i
32    INNER JOIN ' . USERS_TABLE . ' AS u
33    ON i.user_id = u.' . $conf['user_fields']['id'] . '
34  WHERE ' . $conf['user_fields']['username'] . ' = "' . pwg_db_real_escape_string($username) . '"
35  AND oauth_id != ""
36;';
37  $result = pwg_query($query);
38 
39  if (pwg_db_num_rows($result))
40  {
41    list($oauth_id) = pwg_db_fetch_row($result);
42    list($provider) = explode('---', $oauth_id, 2);
43    $_SESSION['page_errors'][] = l10n('You registered with a %s account, please sign in with the same account.', $provider);
44   
45    $redirect_to = get_root_url().'identification.php'; // variable used by identification.php
46    return true;
47  }
48 
49  return false;
50}
51
52
53/**
54 * register page
55 */
56function oauth_begin_register()
57{
58  global $conf, $template, $hybridauth_conf, $page, $user;
59 
60  if ($hybridauth_conf['enabled'] == 0)
61  {
62    return;
63  }
64 
65  // coming from identification page
66  if (pwg_get_session_var('oauth_new_user') != null)
67  {
68    list($provider, $user_identifier) = pwg_get_session_var('oauth_new_user');
69   
70    try {
71      if ($provider == 'Persona')
72      {
73        $template->assign('OAUTH_USER', array(
74          'provider' => 'Persona',
75          'username' => $user_identifier,
76          'u_profile' => null,
77          'avatar' => null,
78          ));
79       
80        oauth_assign_template_vars();
81        $template->append('OAUTH', array('persona_email'=>$user_identifier), true);
82     
83        $conf['oauth']['include_common_template'] = true;
84      }
85      else
86      {
87        require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
88       
89        $hybridauth = new Hybrid_Auth($hybridauth_conf);
90        $adapter = $hybridauth->authenticate($provider);
91        $remote_user = $adapter->getUserProfile();
92       
93        // security, check remote identifier
94        if ($remote_user->identifier != $user_identifier)
95        {
96          pwg_unset_session_var('oauth_new_user');
97          throw new Exception('Hacking attempt!', 403);
98        }
99     
100        $template->assign('OAUTH_USER', array(
101          'provider' => $hybridauth_conf['providers'][$provider]['name'],
102          'username' => $remote_user->displayName,
103          'u_profile' => $remote_user->profileURL,
104          'avatar' => $remote_user->photoURL,
105          ));
106      }
107     
108      $oauth_id = $provider.'---'.$user_identifier;
109     
110      $page['infos'][] = l10n('Your registration is almost done, please complete the registration form.');
111     
112      // register form submited
113      if (isset($_POST['submit']))
114      {
115        $user_id = register_user(
116          $_POST['login'],
117          hash('sha1', $oauth_id.$conf['secret_key']),
118          $_POST['mail_address'],
119          true,
120          $page['errors'],
121          false
122          );
123
124        if ($user_id !== false)
125        {
126          pwg_unset_session_var('oauth_new_user');
127         
128          // update oauth field
129          $query = '
130UPDATE ' . USER_INFOS_TABLE . '
131  SET oauth_id = "' . $oauth_id . '"
132  WHERE user_id = ' . $user_id . '
133;';
134          pwg_query($query);
135         
136          // log_user and redirect
137          log_user($user_id, false);
138          redirect('profile.php');
139        }
140     
141        unset($_POST['submit']);
142      }
143      // login form submited
144      else if (isset($_POST['login']) && $conf['oauth']['allow_merge_accounts'])
145      {
146        if ($conf['insensitive_case_logon'] == true)
147        {
148          $_POST['username'] = search_case_username($_POST['username']);
149        }
150       
151        if ( pwg_login(false, $_POST['username'], $_POST['password'], false) )
152        {
153          pwg_unset_session_var('oauth_new_user');
154         
155          // update oauth field
156          $query = '
157UPDATE ' . USER_INFOS_TABLE . '
158  SET oauth_id = "' . $oauth_id . '"
159  WHERE user_id = ' . $user['id'] . '
160;';
161          pwg_query($query);
162
163          redirect('profile.php');
164        }
165        else
166        {
167          $page['errors'][] = l10n('Invalid password!');
168        }
169      }
170      else
171      {
172        // overwrite fields with remote datas
173        if ($provider == 'Persona')
174        {
175          $_POST['login'] = '';
176          $_POST['mail_address'] = $user_identifier;
177        }
178        else
179        {
180          $_POST['login'] = $remote_user->displayName;
181          $_POST['mail_address'] = $remote_user->email;
182        }
183      }
184     
185      // template
186      $template->assign('OAUTH_PATH', OAUTH_PATH);
187      if ($conf['oauth']['allow_merge_accounts'])
188      {
189        $template->assign('OAUTH_LOGIN_IN_REGISTER', true);
190        $template->set_prefilter('register', 'oauth_add_login_in_register');
191      }
192      else
193      {
194        $template->set_prefilter('register', 'oauth_add_profile_prefilter');
195        $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
196      }
197    }
198    catch (Exception $e)
199    {
200      $page['errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode());
201    }
202  }
203  // display login buttons
204  else if ($conf['oauth']['display_register'])
205  {
206    oauth_assign_template_vars(get_gallery_home_url());
207   
208    $template->set_prefilter('register', 'oauth_add_buttons_prefilter');
209  }
210}
211
212
213/**
214 * profile page
215 */
216function oauth_begin_profile()
217{
218  global $template, $user, $hybridauth_conf, $page, $user;
219 
220  if (empty($user['oauth_id']))
221  {
222    return;
223  }
224 
225  list($provider, $user_identifier) = explode('---', $user['oauth_id'], 2);
226 
227  try {
228    if ($provider == 'Persona')
229    {
230      $template->assign('OAUTH_USER', array(
231        'provider' => 'Persona',
232        'username' => $user_identifier,
233        'u_profile' => null,
234        'avatar' => null,
235        ));
236    }
237    else
238    {
239      require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
240     
241      $hybridauth = new Hybrid_Auth($hybridauth_conf);
242      $adapter = $hybridauth->getAdapter($provider);
243      $remote_user = $adapter->getUserProfile();
244     
245      $template->assign('OAUTH_USER', array(
246        'provider' => $hybridauth_conf['providers'][$provider]['name'],
247        'username' => $remote_user->displayName,
248        'u_profile' => $remote_user->profileURL,
249        'avatar' => $remote_user->photoURL,
250        ));
251    }
252   
253    $template->assign('OAUTH_PATH', OAUTH_PATH);
254    $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter');
255    $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter');
256  }
257  catch (Exception $e)
258  {
259    $page['errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode());
260  }
261}
262
263
264/**
265 * logout
266 */
267function oauth_logout($user_id)
268{
269  global $hybridauth_conf;
270 
271  $oauth_id = get_oauth_id($user_id);
272 
273  if (!isset($oauth_id))
274  {
275    return;
276  }
277
278  list($provider, $identifier) = explode('---', $oauth_id, 2);
279 
280 
281  if ($provider != 'Persona')
282  {
283    require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
284   
285    try {
286      $hybridauth = new Hybrid_Auth($hybridauth_conf);
287      $adapter = $hybridauth->getAdapter($provider);
288      $adapter->logout();
289    }
290    catch (Exception $e) {
291      $_SESSION['page_errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode());
292    }
293  }
294}
295
296
297/**
298 * identification menu block
299 */
300function oauth_blockmanager($menu_ref_arr)
301{
302  global $template, $conf, $hybridauth_conf;
303 
304  $menu = &$menu_ref_arr[0]; 
305 
306  if ($hybridauth_conf['enabled'] == 0 or
307      !$conf['oauth']['display_menubar'] or
308      $menu->get_block('mbIdentification') == null
309    )
310  {
311    return;
312  }
313 
314  $u_redirect = !empty($_GET['redirect']) ? urldecode($_GET['redirect']) : get_gallery_home_url();
315  oauth_assign_template_vars($u_redirect);
316 
317  $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter');
318}
319
320
321/**
322 * load common javascript
323 */
324function oauth_page_header()
325{
326  global $conf, $template;
327
328  if (isset($conf['oauth']['include_common_template']))
329  {
330    $template->set_filename('oauth', realpath(OAUTH_PATH . 'template/identification_common.tpl'));
331    $template->parse('oauth');
332  }
333}
334
335
336/**
337 * prefilters
338 */
339function oauth_add_buttons_prefilter($content)
340{
341  $search = '</form>';
342  $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl');
343  return str_replace($search, $search.$add, $content);
344}
345
346function oauth_remove_password_fields_prefilter($content)
347{
348  $search = 'type="password" ';
349  $add = 'disabled="disabled" ';
350  $script = '
351{footer_script require="jquery"}
352jQuery("input[type=password], input[name=send_password_by_mail]").parent().hide();
353{/footer_script}';
354
355  $content = str_replace($search, $search.$add, $content);
356  return $content.$script;
357}
358
359function oauth_add_profile_prefilter($content)
360{
361  $search = '#(</legend>)#';
362  $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl');
363  return preg_replace($search, '$1 '.$add, $content, 1);
364}
365
366function oauth_add_menubar_buttons_prefilter($content)
367{
368  $search = '#({include file=\$block->template\|@?get_extent:\$id ?})#';
369  $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl');
370  return preg_replace($search, '$1 '.$add, $content);
371}
372
373function oauth_add_login_in_register($content)
374{
375  $search[0] = '<form method="post" action="{$F_ACTION}"';
376  $replace[0] = file_get_contents(OAUTH_PATH . 'template/register.tpl') . $search[0];
377 
378  $search[1] = '{\'Enter your personnal informations\'|@translate}';
379  $replace[1] = '{\'Create a new account\'|@translate}';
380 
381  return str_replace($search, $replace, $content);
382}
Note: See TracBrowser for help on using the repository browser.