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

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

don't delete keys when the provider is removed, hide fieldset when no provider active, use jQuery instead of $

File size: 7.9 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';
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  // comming 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!');
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                        true,
91                        $page['errors']);
92                       
93        if (count($page['errors']) == 0)
94        {
95          pwg_unset_session_var('oauth_new_user');
96          $user_id = get_userid($_POST['login']);
97         
98          // update oauth field
99          $query = '
100UPDATE '.USERS_TABLE.'
101  SET oauth_id = "'.$oauth_id.'"
102  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
103;';
104          pwg_query($query);
105         
106          // log_user and redirect
107          log_user($user_id, false);
108          redirect('profile.php');
109        }
110     
111        unset($_POST['submit']);
112      }
113      else
114      {
115        // overwrite fields with remote datas
116        $_POST['login'] = $remote_user->displayName;
117        $_POST['mail_adress'] = $remote_user->email;
118      }
119     
120      // template
121      $template->set_prefilter('register', 'oauth_add_profile_prefilter');
122      $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
123    }
124    catch (Exception $e) {
125      array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
126    }
127  }
128  // display login buttons
129  else if ($conf['oauth']['display_register'])
130  {
131    oauth_assign_template_vars();
132    $template->assign('REDIRECT_TO', get_gallery_home_url());
133   
134    $template->set_prefilter('register', 'oauth_add_buttons_prefilter');
135  }
136}
137
138
139/**
140 * profile page
141 */
142function oauth_begin_profile()
143{
144  global $template, $user, $conf, $hybridauth_conf;
145 
146  $query = '
147SELECT oauth_id FROM '.USERS_TABLE.'
148  WHERE '.$conf['user_fields']['id'].' = '.$user['id'].'
149  AND oauth_id != ""
150;';
151  $result = pwg_query($query);
152 
153  if (!pwg_db_num_rows($result))
154  {
155    return;
156  }
157 
158  list($oauth_id) = pwg_db_fetch_row($result);
159  list($provider) = explode('---', $oauth_id);
160 
161  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
162 
163  try {
164    $hybridauth = new Hybrid_Auth($hybridauth_conf);
165    $adapter = $hybridauth->getAdapter($provider);
166    $remote_user = $adapter->getUserProfile();
167   
168    $template->assign(array(
169      'OAUTH_PROVIDER' => $provider,
170      'OAUTH_USERNAME' => $remote_user->displayName,
171      'OAUTH_PROFILE_URL' => $remote_user->profileURL,
172      'OAUTH_AVATAR' => $remote_user->photoURL,
173      'OAUTH_PATH' => OAUTH_PATH,
174      ));
175   
176    $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter');
177    $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter');
178  }
179  catch (Exception $e) {
180    array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
181  }
182}
183
184
185/**
186 * logout
187 */
188function oauth_logout($user_id)
189{
190  global $conf, $hybridauth_conf;
191 
192  $query = '
193SELECT oauth_id FROM '.USERS_TABLE.'
194  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
195  AND oauth_id != ""
196;';
197  $result = pwg_query($query);
198 
199  if (!pwg_db_num_rows($result))
200  {
201    return;
202  }
203 
204  list($oauth_id) = pwg_db_fetch_row($result);
205  list($provider) = explode('---', $oauth_id);
206 
207  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
208 
209  try {
210    $hybridauth = new Hybrid_Auth($hybridauth_conf);
211    $adapter = $hybridauth->getAdapter($provider);
212    $adapter->logout();
213  }
214  catch (Exception $e) {
215    $_SESSION['page_errors'][] = sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode());
216  }
217}
218
219
220/**
221 * identification menu block
222 */
223function oauth_blockmanager($menu_ref_arr)
224{
225  global $template, $conf;
226 
227  $menu = &$menu_ref_arr[0]; 
228 
229  if ( !$conf['oauth']['display_menubar'] or $menu->get_block('mbIdentification') == null )
230  {
231    return;
232  }
233 
234  oauth_assign_template_vars();
235  $template->assign('REDIRECT_TO', get_gallery_home_url());
236 
237  $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter');
238}
239
240
241/**
242 * prefilters
243 */
244function oauth_add_buttons_prefilter($content)
245{
246  $search = '</form>';
247  $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl');
248  return str_replace($search, $search.$add, $content);
249}
250
251function oauth_remove_password_fields_prefilter($content)
252{
253  $search = 'type="password" ';
254  $add = 'disabled="disabled" ';
255  $script = '
256{footer_script}
257jQuery("input[type=\'password\'], input[name=\'send_password_by_mail\']").parent("li").css("display", "none");
258{/footer_script}';
259
260  $content = str_replace($search, $search.$add, $content);
261  return $content.$script;
262}
263
264function oauth_add_profile_prefilter($content)
265{
266  $search = '#</legend>#';
267  $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl');
268  return preg_replace($search, '</legend> '.$add, $content, 1);
269}
270
271function oauth_add_menubar_buttons_prefilter($content)
272{
273  $search = '{include file=$block->template|@get_extent:$id }';
274  $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl');
275  return str_replace($search, $search.$add, $content);
276}
277
278
279function oauth_assign_template_vars()
280{
281  global $template, $conf;
282 
283  if ($template->get_template_vars('OAUTH_URL') == null)
284  {
285    $template->assign(array(
286      'oauth' => $conf['oauth'],
287      'OAUTH_URL' => get_root_url() . OAUTH_PATH . 'auth.php?provider=',
288      'OAUTH_PATH' => OAUTH_PATH,
289      'OAUTH_ABS_PATH' => realpath(OAUTH_PATH) . '/',
290      'PROVIDERS' => get_activated_providers(),
291      'ABS_ROOT_URL' => rtrim(get_gallery_home_url(), '/') . '/',
292      ));
293  }
294}
295?>
Note: See TracBrowser for help on using the repository browser.