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

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

first commit of oAuth plugin, still in developpement

File size: 7.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, $page, $hybridauth_conf;
10 
11  if (isset($_GET['redirect']))
12  {
13    pwg_set_session_var('oauth_redirect', $_GET['redirect']);
14  }
15  else
16  {
17    pwg_unset_session_var('oauth_redirect');
18  }
19 
20  // template icons
21  if ($template->get_template_vars('OAUTH_URL') == null)
22  {
23    $template->assign(array(
24      'OAUTH_URL' => get_root_url().OAUTH_PATH.'auth.php?provider=',
25      'OAUTH_PATH' => OAUTH_PATH,
26      'OAUTH_ABS_PATH' => realpath(OAUTH_PATH).'/',
27      'PROVIDERS' => get_activated_providers(),
28    ));
29  }
30 
31  $template->assign('REDIRECT_TO', !empty($_GET['redirect']) ? urldecode($_GET['redirect']) : get_gallery_home_url());
32 
33  $template->set_prefilter('identification', 'oauth_add_buttons_prefilter');
34}
35
36/**
37 * interrupt normal login if corresponding to an oauth user
38 */
39function oauth_try_log_user($success, $username)
40{
41  global $conf, $redirect_to;
42 
43  $query = '
44SELECT oauth_id FROM '.USERS_TABLE.'
45  WHERE '.$conf['user_fields']['username'].' = "'.pwg_db_real_escape_string($username).'"
46  AND oauth_id != ""
47;';
48  $result = pwg_query($query);
49 
50  if (pwg_db_num_rows($result))
51  {
52    list($oauth_id) = pwg_db_fetch_row($result);
53    list($provider) = explode('---', $oauth_id);
54    $_SESSION['page_errors'][] = 'You registered with a '.ucfirst($provider).' account, please login with the same account.';
55   
56    $redirect_to = get_root_url().'identification.php';
57    return true;
58  }
59 
60  return false;
61}
62
63
64/**
65 * register page
66 */
67function oauth_begin_register()
68{
69  global $conf, $template, $hybridauth_conf;
70 
71  if (!$conf['oauth']['display_register'])
72  {
73    return;
74  }
75 
76  // comming from identification page
77  if (pwg_get_session_var('oauth_new_user') != null)
78  {
79    list($provider, $user_identifier) = pwg_get_session_var('oauth_new_user');
80   
81    require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
82   
83    try {
84      $hybridauth = new Hybrid_Auth($hybridauth_conf);
85      $adapter = $hybridauth->authenticate($provider);
86      $remote_user = $adapter->getUserProfile();
87     
88      $oauth_id = $provider.'---'.$remote_user->identifier;
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!');
95      }
96     
97      // form submited
98      if (isset($_POST['submit']))
99      {
100        $page['errors'] =
101          register_user($_POST['login'],
102                        hash('sha1', $oauth_id.$conf['secret_key']),
103                        $_POST['mail_address'],
104                        true,
105                        $page['errors']);
106                       
107        if (count($page['errors']) == 0)
108        {
109          pwg_unset_session_var('oauth_new_user');
110          $user_id = get_userid($_POST['login']);
111         
112          // udpdate oauth field
113          $query = '
114UPDATE '.USERS_TABLE.'
115  SET oauth_id = "'.$oauth_id.'"
116  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
117;';
118          pwg_query($query);
119         
120          // log_user and redirect
121          log_user($user_id, false);
122          redirect('profile.php');
123        }
124     
125        unset($_POST['submit']);
126      }
127      else
128      {
129        // overwrite fields with remote datas
130        $_POST['login'] = $remote_user->displayName;
131        $_POST['mail_adress'] = $remote_user->email;
132      }
133     
134      // template
135      $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
136    }
137    catch( Exception $e ){
138      die($e->getMessage());
139    }
140  }
141  else
142  {
143    // template icons
144    if ($template->get_template_vars('OAUTH_URL') == null)
145    {
146      $template->assign(array(
147        'OAUTH_URL' => get_root_url().OAUTH_PATH.'auth.php?provider=',
148        'OAUTH_PATH' => OAUTH_PATH,
149        'OAUTH_ABS_PATH' => realpath(OAUTH_PATH).'/',
150        'PROVIDERS' => get_activated_providers(),
151        ));
152    }
153   
154    $template->assign('REDIRECT_TO', get_gallery_home_url());
155   
156    $template->set_prefilter('register', 'oauth_add_buttons_prefilter');
157  }
158}
159
160
161/**
162 * profile page
163 */
164function oauth_begin_profile()
165{
166  global $template, $user, $conf, $hybridauth_conf;
167 
168  $query = '
169SELECT oauth_id FROM '.USERS_TABLE.'
170  WHERE '.$conf['user_fields']['id'].' = '.$user['id'].'
171  AND oauth_id != ""
172;';
173  $result = pwg_query($query);
174 
175  if (!pwg_db_num_rows($result))
176  {
177    return;
178  }
179 
180  list($oauth_id) = pwg_db_fetch_row($result);
181  list($provider) = explode('---', $oauth_id);
182 
183  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
184 
185  try {
186    $hybridauth = new Hybrid_Auth($hybridauth_conf);
187    $adapter = $hybridauth->getAdapter($provider);
188    $remote_user = $adapter->getUserProfile();
189   
190    $template->assign(array(
191      'OAUTH_PROVIDER' => ucfirst($provider),
192      'OAUTH_USERNAME' => $remote_user->displayName,
193      'OAUTH_URL' => $remote_user->profileURL,
194      'OAUTH_AVATAR' => $remote_user->photoURL,
195      'OAUTH_PATH' => OAUTH_PATH,
196      ));
197   
198    $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter');
199  }
200  catch( Exception $e ){
201    die($e->getMessage());
202  }
203 
204 
205  $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter');
206}
207
208
209/**
210 * logout
211 */
212function oauth_logout($user_id)
213{
214  global $conf, $hybridauth_conf;
215 
216  $query = '
217SELECT oauth_id FROM '.USERS_TABLE.'
218  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
219  AND oauth_id != ""
220;';
221  $result = pwg_query($query);
222 
223  if (!pwg_db_num_rows($result))
224  {
225    return;
226  }
227 
228  list($oauth_id) = pwg_db_fetch_row($result);
229  list($provider) = explode('---', $oauth_id);
230 
231  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
232 
233  try {
234    $hybridauth = new Hybrid_Auth($hybridauth_conf);
235    $adapter = $hybridauth->getAdapter($provider);
236    $adapter->logout();
237  }
238  catch( Exception $e ){
239    die($e->getMessage());
240  }
241}
242
243
244/**
245 * identification menu block
246 */
247function oauth_blockmanager($menu_ref_arr)
248{
249  global $template, $conf;
250 
251  if (!$conf['oauth']['display_menubar'])
252  {
253    return;
254  }
255 
256  $menu = &$menu_ref_arr[0]; 
257 
258  if ($menu->get_block('mbIdentification') == null)
259  {
260    return;
261  }
262 
263  if ($template->get_template_vars('OAUTH_URL') == null)
264  {
265    $template->assign(array(
266      'OAUTH_URL' => get_root_url().OAUTH_PATH.'auth.php?provider=',
267      'OAUTH_PATH' => OAUTH_PATH,
268      'OAUTH_ABS_PATH' => realpath(OAUTH_PATH).'/',
269      'PROVIDERS' => get_activated_providers(),
270      ));
271  }
272 
273  $template->assign('REDIRECT_TO', get_gallery_home_url());
274 
275  $template->set_prefilter('menubar', 'oauth_add_menubar_buttons');
276}
277
278
279/**
280 * prefilters
281 */
282function oauth_add_buttons_prefilter($content)
283{
284  $search = '</form>';
285  $add = file_get_contents(OAUTH_PATH.'template/identification_page.tpl');
286  return str_replace($search, $search.$add, $content);
287}
288
289function oauth_remove_password_fields_prefilter($content)
290{
291  $search = 'type="password" ';
292  $add = 'disabled="disabled" ';
293  $script = '
294{footer_script}{literal}
295jQuery("input[type=\'password\'], input[name=\'send_password_by_mail\']").parent("li").css("display", "none");
296{/literal}{/footer_script}';
297
298  $content = str_replace($search, $search.$add, $content);
299  return $content.$script;
300}
301
302function oauth_add_profile_prefilter($content)
303{
304  $search = '<legend>{\'Registration\'|@translate}</legend>';
305  $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl');
306  return str_replace($search, $search.$add, $content);
307}
308
309function oauth_add_menubar_buttons($content)
310{
311  $search = '{include file=$block->template|@get_extent:$id }';
312  $add = file_get_contents(OAUTH_PATH.'template/identification_menubar.tpl');
313  return str_replace($search, $search.$add, $content);
314}
315
316?>
Note: See TracBrowser for help on using the repository browser.