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

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

clean code

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;
10 
11  // template icons
12  if ($template->get_template_vars('OAUTH_URL') == null)
13  {
14    $template->assign(array(
15      'OAUTH_URL' => get_root_url() . OAUTH_PATH . 'auth.php?provider=',
16      'OAUTH_PATH' => OAUTH_PATH,
17      'OAUTH_ABS_PATH' => realpath(OAUTH_PATH) . '/',
18      'PROVIDERS' => get_activated_providers(),
19    ));
20  }
21 
22  $template->assign('REDIRECT_TO', !empty($_GET['redirect']) ? urldecode($_GET['redirect']) : get_gallery_home_url());
23 
24  $template->set_prefilter('identification', 'oauth_add_buttons_prefilter');
25}
26
27/**
28 * interrupt normal login if corresponding to an oauth user
29 */
30function oauth_try_log_user($success, $username)
31{
32  global $conf, $redirect_to;
33 
34  $query = '
35SELECT oauth_id FROM '.USERS_TABLE.'
36  WHERE '.$conf['user_fields']['username'].' = "'.pwg_db_real_escape_string($username).'"
37  AND oauth_id != ""
38;';
39  $result = pwg_query($query);
40 
41  if (pwg_db_num_rows($result))
42  {
43    list($oauth_id) = pwg_db_fetch_row($result);
44    list($provider) = explode('---', $oauth_id);
45    $_SESSION['page_errors'][] = sprintf(l10n('You registered with a %s account, please sign in with the same account.'), $provider);
46   
47    $redirect_to = get_root_url().'identification.php';
48    return true;
49  }
50 
51  return false;
52}
53
54
55/**
56 * register page
57 */
58function oauth_begin_register()
59{
60  global $conf, $template, $hybridauth_conf;
61 
62  // comming 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    require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
68   
69    try {
70      $hybridauth = new Hybrid_Auth($hybridauth_conf);
71      $adapter = $hybridauth->authenticate($provider);
72      $remote_user = $adapter->getUserProfile();
73     
74      $oauth_id = $provider.'---'.$remote_user->identifier;
75     
76      // security, check remote identifier
77      if ($remote_user->identifier != $user_identifier)
78      {
79        pwg_unset_session_var('oauth_new_user');
80        throw new Exception('Hacking attempt!');
81      }
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          // udpdate 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_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    // template icons
131    if ($template->get_template_vars('OAUTH_URL') == null)
132    {
133      $template->assign(array(
134        'OAUTH_URL' => get_root_url() . OAUTH_PATH . 'auth.php?provider=',
135        'OAUTH_PATH' => OAUTH_PATH,
136        'OAUTH_ABS_PATH' => realpath(OAUTH_PATH) . '/',
137        'PROVIDERS' => get_activated_providers(),
138        ));
139    }
140   
141    $template->assign('REDIRECT_TO', get_gallery_home_url());
142   
143    $template->set_prefilter('register', 'oauth_add_buttons_prefilter');
144  }
145}
146
147
148/**
149 * profile page
150 */
151function oauth_begin_profile()
152{
153  global $template, $user, $conf, $hybridauth_conf;
154 
155  $query = '
156SELECT oauth_id FROM '.USERS_TABLE.'
157  WHERE '.$conf['user_fields']['id'].' = '.$user['id'].'
158  AND oauth_id != ""
159;';
160  $result = pwg_query($query);
161 
162  if (!pwg_db_num_rows($result))
163  {
164    return;
165  }
166 
167  list($oauth_id) = pwg_db_fetch_row($result);
168  list($provider) = explode('---', $oauth_id);
169 
170  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
171 
172  try {
173    $hybridauth = new Hybrid_Auth($hybridauth_conf);
174    $adapter = $hybridauth->getAdapter($provider);
175    $remote_user = $adapter->getUserProfile();
176   
177    $template->assign(array(
178      'OAUTH_PROVIDER' => $provider,
179      'OAUTH_USERNAME' => $remote_user->displayName,
180      'OAUTH_URL' => $remote_user->profileURL,
181      'OAUTH_AVATAR' => $remote_user->photoURL,
182      'OAUTH_PATH' => OAUTH_PATH,
183      ));
184   
185    $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter');
186    $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter');
187  }
188  catch (Exception $e) {
189    array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
190  }
191}
192
193
194/**
195 * logout
196 */
197function oauth_logout($user_id)
198{
199  global $conf, $hybridauth_conf;
200 
201  $query = '
202SELECT oauth_id FROM '.USERS_TABLE.'
203  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
204  AND oauth_id != ""
205;';
206  $result = pwg_query($query);
207 
208  if (!pwg_db_num_rows($result))
209  {
210    return;
211  }
212 
213  list($oauth_id) = pwg_db_fetch_row($result);
214  list($provider) = explode('---', $oauth_id);
215 
216  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
217 
218  try {
219    $hybridauth = new Hybrid_Auth($hybridauth_conf);
220    $adapter = $hybridauth->getAdapter($provider);
221    $adapter->logout();
222  }
223  catch (Exception $e) {
224    $_SESSION['page_errors'][] = sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode());
225  }
226}
227
228
229/**
230 * identification menu block
231 */
232function oauth_blockmanager($menu_ref_arr)
233{
234  global $template, $conf;
235 
236  $menu = &$menu_ref_arr[0]; 
237 
238  if ( !$conf['oauth']['display_menubar'] or $menu->get_block('mbIdentification') == null )
239  {
240    return;
241  }
242 
243  if ($template->get_template_vars('OAUTH_URL') == null)
244  {
245    $template->assign(array(
246      'OAUTH_URL' => get_root_url() . OAUTH_PATH . 'auth.php?provider=',
247      'OAUTH_PATH' => OAUTH_PATH,
248      'OAUTH_ABS_PATH' => realpath(OAUTH_PATH) . '/',
249      'PROVIDERS' => get_activated_providers(),
250      ));
251  }
252 
253  $template->assign('REDIRECT_TO', get_gallery_home_url());
254 
255  $template->set_prefilter('menubar', 'oauth_add_menubar_buttons');
256}
257
258
259/**
260 * prefilters
261 */
262function oauth_add_buttons_prefilter($content)
263{
264  $search = '</form>';
265  $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl');
266  return str_replace($search, $search.$add, $content);
267}
268
269function oauth_remove_password_fields_prefilter($content)
270{
271  $search = 'type="password" ';
272  $add = 'disabled="disabled" ';
273  $script = '
274{footer_script}{literal}
275jQuery("input[type=\'password\'], input[name=\'send_password_by_mail\']").parent("li").css("display", "none");
276{/literal}{/footer_script}';
277
278  $content = str_replace($search, $search.$add, $content);
279  return $content.$script;
280}
281
282function oauth_add_profile_prefilter($content)
283{
284  $search = '<legend>{\'Registration\'|@translate}</legend>';
285  $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl');
286  return str_replace($search, $search.$add, $content);
287}
288
289function oauth_add_menubar_buttons($content)
290{
291  $search = '{include file=$block->template|@get_extent:$id }';
292  $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl');
293  return str_replace($search, $search.$add, $content);
294}
295
296?>
Note: See TracBrowser for help on using the repository browser.