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

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

fix lightbow conflict

File size: 7.9 KB
RevLine 
[20293]1<?php
2defined('OAUTH_PATH') or die('Hacking attempt!');
3
[24142]4function oauth_anti_lightbox($tpl_thumbnails_var)
5{
6  global $template, $page;
7 
8  if ($page['section'] == 'collections' && !empty($template->css_by_priority[0]))
9  {
10    foreach ($template->css_by_priority[0] as $file)
11    {
12      if (strpos($file[0], 'colorbox.css') !== false)
13      {
14        $template->assign('OAUTH_NO_LIGHTBOX', true);
15        break;
16      }
17    }
18  }
19 
20  return $tpl_thumbnails_var;
21}
22
[20293]23/**
24 * identification page
25 */
26function oauth_begin_identification()
27{
[20323]28  global $template, $conf;
[20293]29 
[20323]30  oauth_assign_template_vars();
[20293]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);
[20301]54    $_SESSION['page_errors'][] = sprintf(l10n('You registered with a %s account, please sign in with the same account.'), $provider);
[20293]55   
[23808]56    $redirect_to = get_root_url().'identification.php'; // variable used by identification.php
[20293]57    return true;
58  }
59 
60  return false;
61}
62
63
64/**
65 * register page
66 */
67function oauth_begin_register()
68{
[20323]69  global $conf, $template, $hybridauth_conf, $page;
[20293]70 
[23808]71  // coming from identification page
[20293]72  if (pwg_get_session_var('oauth_new_user') != null)
73  {
74    list($provider, $user_identifier) = pwg_get_session_var('oauth_new_user');
75   
76    require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
77   
78    try {
79      $hybridauth = new Hybrid_Auth($hybridauth_conf);
80      $adapter = $hybridauth->authenticate($provider);
81      $remote_user = $adapter->getUserProfile();
[21584]82     
83      // security, check remote identifier
84      if ($remote_user->identifier != $user_identifier)
85      {
86        pwg_unset_session_var('oauth_new_user');
[23808]87        throw new Exception('Hacking attempt!', 403);
[21584]88      }
[20323]89   
90      $template->assign(array(
91        'OAUTH_PROVIDER' => $provider,
92        'OAUTH_USERNAME' => $remote_user->displayName,
93        'OAUTH_PROFILE_URL' => $remote_user->profileURL,
94        'OAUTH_AVATAR' => $remote_user->photoURL,
95        'OAUTH_PATH' => OAUTH_PATH,
96        ));
97       
98      array_push($page['infos'], l10n('Your registration is almost done, please complete the registration form.'));
[20293]99     
100      $oauth_id = $provider.'---'.$remote_user->identifier;
101     
102      // form submited
103      if (isset($_POST['submit']))
104      {
105        $page['errors'] =
106          register_user($_POST['login'],
107                        hash('sha1', $oauth_id.$conf['secret_key']),
[23815]108                        $_POST['mail_address']
109                        );
[20293]110                       
111        if (count($page['errors']) == 0)
112        {
113          pwg_unset_session_var('oauth_new_user');
114          $user_id = get_userid($_POST['login']);
115         
[21584]116          // update oauth field
[20293]117          $query = '
118UPDATE '.USERS_TABLE.'
119  SET oauth_id = "'.$oauth_id.'"
120  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
121;';
122          pwg_query($query);
123         
124          // log_user and redirect
125          log_user($user_id, false);
126          redirect('profile.php');
127        }
128     
129        unset($_POST['submit']);
130      }
131      else
132      {
133        // overwrite fields with remote datas
134        $_POST['login'] = $remote_user->displayName;
[23815]135        $_POST['mail_address'] = $remote_user->email;
[20293]136      }
137     
138      // template
[20323]139      $template->set_prefilter('register', 'oauth_add_profile_prefilter');
[20293]140      $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter');
141    }
[20301]142    catch (Exception $e) {
143      array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
[20293]144    }
145  }
[20301]146  // display login buttons
147  else if ($conf['oauth']['display_register'])
[20293]148  {
[20323]149    oauth_assign_template_vars();
[20293]150    $template->assign('REDIRECT_TO', get_gallery_home_url());
151   
152    $template->set_prefilter('register', 'oauth_add_buttons_prefilter');
153  }
154}
155
156
157/**
158 * profile page
159 */
160function oauth_begin_profile()
161{
[23808]162  global $template, $user, $conf, $hybridauth_conf, $page;
[20293]163 
164  $query = '
165SELECT oauth_id FROM '.USERS_TABLE.'
166  WHERE '.$conf['user_fields']['id'].' = '.$user['id'].'
167  AND oauth_id != ""
168;';
169  $result = pwg_query($query);
170 
171  if (!pwg_db_num_rows($result))
172  {
173    return;
174  }
175 
176  list($oauth_id) = pwg_db_fetch_row($result);
177  list($provider) = explode('---', $oauth_id);
178 
179  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
180 
181  try {
182    $hybridauth = new Hybrid_Auth($hybridauth_conf);
183    $adapter = $hybridauth->getAdapter($provider);
184    $remote_user = $adapter->getUserProfile();
185   
186    $template->assign(array(
[20301]187      'OAUTH_PROVIDER' => $provider,
[20293]188      'OAUTH_USERNAME' => $remote_user->displayName,
[20323]189      'OAUTH_PROFILE_URL' => $remote_user->profileURL,
[20293]190      'OAUTH_AVATAR' => $remote_user->photoURL,
191      'OAUTH_PATH' => OAUTH_PATH,
192      ));
193   
194    $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter');
[20301]195    $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter');
[20293]196  }
[20301]197  catch (Exception $e) {
198    array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()));
[20293]199  }
200}
201
202
203/**
204 * logout
205 */
206function oauth_logout($user_id)
207{
208  global $conf, $hybridauth_conf;
209 
210  $query = '
211SELECT oauth_id FROM '.USERS_TABLE.'
212  WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
213  AND oauth_id != ""
214;';
215  $result = pwg_query($query);
216 
217  if (!pwg_db_num_rows($result))
218  {
219    return;
220  }
221 
222  list($oauth_id) = pwg_db_fetch_row($result);
223  list($provider) = explode('---', $oauth_id);
224 
225  require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php');
226 
227  try {
228    $hybridauth = new Hybrid_Auth($hybridauth_conf);
229    $adapter = $hybridauth->getAdapter($provider);
230    $adapter->logout();
231  }
[20301]232  catch (Exception $e) {
233    $_SESSION['page_errors'][] = sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode());
[20293]234  }
235}
236
237
238/**
239 * identification menu block
240 */
241function oauth_blockmanager($menu_ref_arr)
242{
243  global $template, $conf;
244 
245  $menu = &$menu_ref_arr[0]; 
246 
[20301]247  if ( !$conf['oauth']['display_menubar'] or $menu->get_block('mbIdentification') == null )
[20293]248  {
249    return;
250  }
251 
[20323]252  oauth_assign_template_vars();
[20293]253  $template->assign('REDIRECT_TO', get_gallery_home_url());
254 
[21584]255  $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter');
[20293]256}
257
258
259/**
260 * prefilters
261 */
262function oauth_add_buttons_prefilter($content)
263{
264  $search = '</form>';
[20301]265  $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl');
[20293]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 = '
[23808]274{footer_script require="jquery"}
275jQuery("input[type=\'password\'], input[name=\'send_password_by_mail\']").parent().css("display", "none");
[21584]276{/footer_script}';
[20293]277
278  $content = str_replace($search, $search.$add, $content);
279  return $content.$script;
280}
281
282function oauth_add_profile_prefilter($content)
283{
[20323]284  $search = '#</legend>#';
[20293]285  $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl');
[20323]286  return preg_replace($search, '</legend> '.$add, $content, 1);
[20293]287}
288
[21584]289function oauth_add_menubar_buttons_prefilter($content)
[20293]290{
291  $search = '{include file=$block->template|@get_extent:$id }';
[20301]292  $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl');
[20293]293  return str_replace($search, $search.$add, $content);
294}
295
296?>
Note: See TracBrowser for help on using the repository browser.