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

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

finish updating guides, fix flow issues, update language file

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