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

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

move "oauth_id" field to user + display 16px icon on users list

File size: 8.7 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;
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      // 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      else
144      {
145        // overwrite fields with remote datas
146        if ($provider == 'Persona')
147        {
148          $_POST['login'] = '';
149          $_POST['mail_address'] = $user_identifier;
150        }
151        else
152        {
153          $_POST['login'] = $remote_user->displayName;
154          $_POST['mail_address'] = $remote_user->email;
155        }
156      }
157     
158      // template
159      $template->assign('OAUTH_PATH', OAUTH_PATH);
160      $template->set_prefilter('register', 'oauth_add_profile_prefilter');
161      $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
162    }
163    catch (Exception $e)
164    {
165      $page['errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode());
166    }
167  }
168  // display login buttons
169  else if ($conf['oauth']['display_register'])
170  {
171    oauth_assign_template_vars(get_gallery_home_url());
172   
173    $template->set_prefilter('register', 'oauth_add_buttons_prefilter');
174  }
175}
176
177
178/**
179 * profile page
180 */
181function oauth_begin_profile()
182{
183  global $template, $user, $hybridauth_conf, $page, $user;
184 
185  if (empty($user['oauth_id']))
186  {
187    return;
188  }
189 
190  list($provider, $user_identifier) = explode('---', $user['oauth_id'], 2);
191 
192  try {
193    if ($provider == 'Persona')
194    {
195      $template->assign('OAUTH_USER', array(
196        'provider' => 'Persona',
197        'username' => $user_identifier,
198        'u_profile' => null,
199        'avatar' => null,
200        ));
201    }
202    else
203    {
204      require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
205     
206      $hybridauth = new Hybrid_Auth($hybridauth_conf);
207      $adapter = $hybridauth->getAdapter($provider);
208      $remote_user = $adapter->getUserProfile();
209     
210      $template->assign('OAUTH_USER', array(
211        'provider' => $hybridauth_conf['providers'][$provider]['name'],
212        'username' => $remote_user->displayName,
213        'u_profile' => $remote_user->profileURL,
214        'avatar' => $remote_user->photoURL,
215        ));
216    }
217   
218    $template->assign('OAUTH_PATH', OAUTH_PATH);
219    $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter');
220    $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter');
221  }
222  catch (Exception $e)
223  {
224    $page['errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode());
225  }
226}
227
228
229/**
230 * logout
231 */
232function oauth_logout($user_id)
233{
234  global $hybridauth_conf;
235 
236  $oauth_id = get_oauth_id($user_id);
237 
238  if (!isset($oauth_id))
239  {
240    return;
241  }
242
243  list($provider, $identifier) = explode('---', $oauth_id, 2);
244 
245 
246  if ($provider != 'Persona')
247  {
248    require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
249   
250    try {
251      $hybridauth = new Hybrid_Auth($hybridauth_conf);
252      $adapter = $hybridauth->getAdapter($provider);
253      $adapter->logout();
254    }
255    catch (Exception $e) {
256      $_SESSION['page_errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode());
257    }
258  }
259}
260
261
262/**
263 * identification menu block
264 */
265function oauth_blockmanager($menu_ref_arr)
266{
267  global $template, $conf, $hybridauth_conf;
268 
269  $menu = &$menu_ref_arr[0]; 
270 
271  if ($hybridauth_conf['enabled'] == 0 or
272      !$conf['oauth']['display_menubar'] or
273      $menu->get_block('mbIdentification') == null
274    )
275  {
276    return;
277  }
278 
279  $u_redirect = !empty($_GET['redirect']) ? urldecode($_GET['redirect']) : get_gallery_home_url();
280  oauth_assign_template_vars($u_redirect);
281 
282  $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter');
283}
284
285
286/**
287 * load common javascript
288 */
289function oauth_page_header()
290{
291  global $conf, $template;
292
293  if (isset($conf['oauth']['include_common_template']))
294  {
295    $template->set_filename('oauth', realpath(OAUTH_PATH . 'template/identification_common.tpl'));
296    $template->parse('oauth');
297  }
298}
299
300
301/**
302 * prefilters
303 */
304function oauth_add_buttons_prefilter($content)
305{
306  $search = '</form>';
307  $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl');
308  return str_replace($search, $search.$add, $content);
309}
310
311function oauth_remove_password_fields_prefilter($content)
312{
313  $search = 'type="password" ';
314  $add = 'disabled="disabled" ';
315  $script = '
316{footer_script require="jquery"}
317jQuery("input[type=password], input[name=send_password_by_mail]").parent().hide();
318{/footer_script}';
319
320  $content = str_replace($search, $search.$add, $content);
321  return $content.$script;
322}
323
324function oauth_add_profile_prefilter($content)
325{
326  $search = '#(</legend>)#';
327  $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl');
328  return preg_replace($search, '$1 '.$add, $content, 1);
329}
330
331function oauth_add_menubar_buttons_prefilter($content)
332{
333  $search = '#({include file=\$block->template\|@?get_extent:\$id ?})#';
334  $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl');
335  return preg_replace($search, '$1 '.$add, $content);
336}
Note: See TracBrowser for help on using the repository browser.