[20293] | 1 | <?php |
---|
| 2 | defined('OAUTH_PATH') or die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | * identification page |
---|
| 6 | */ |
---|
| 7 | function oauth_begin_identification() |
---|
| 8 | { |
---|
[26604] | 9 | global $template, $conf, $hybridauth_conf; |
---|
[20293] | 10 | |
---|
[26605] | 11 | if ($hybridauth_conf['enabled'] == 0) |
---|
[26604] | 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 | |
---|
[20293] | 19 | $template->set_prefilter('identification', 'oauth_add_buttons_prefilter'); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * interrupt normal login if corresponding to an oauth user |
---|
| 24 | */ |
---|
| 25 | function oauth_try_log_user($success, $username) |
---|
| 26 | { |
---|
| 27 | global $conf, $redirect_to; |
---|
| 28 | |
---|
| 29 | $query = ' |
---|
[26604] | 30 | SELECT oauth_id FROM ' . USERS_TABLE . ' |
---|
| 31 | WHERE ' . $conf['user_fields']['username'] . ' = "' . pwg_db_real_escape_string($username) . '" |
---|
[20293] | 32 | AND oauth_id != "" |
---|
| 33 | ;'; |
---|
| 34 | $result = pwg_query($query); |
---|
| 35 | |
---|
| 36 | if (pwg_db_num_rows($result)) |
---|
| 37 | { |
---|
| 38 | list($oauth_id) = pwg_db_fetch_row($result); |
---|
[26605] | 39 | list($provider) = explode('---', $oauth_id, 2); |
---|
[26556] | 40 | $_SESSION['page_errors'][] = l10n('You registered with a %s account, please sign in with the same account.', $provider); |
---|
[20293] | 41 | |
---|
[23808] | 42 | $redirect_to = get_root_url().'identification.php'; // variable used by identification.php |
---|
[20293] | 43 | return true; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | return false; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * register page |
---|
| 52 | */ |
---|
| 53 | function oauth_begin_register() |
---|
| 54 | { |
---|
[20323] | 55 | global $conf, $template, $hybridauth_conf, $page; |
---|
[20293] | 56 | |
---|
[26604] | 57 | if ($hybridauth_conf['enabled'] == 0) |
---|
| 58 | { |
---|
| 59 | return; |
---|
| 60 | } |
---|
| 61 | |
---|
[23808] | 62 | // coming from identification page |
---|
[20293] | 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 | try { |
---|
[26605] | 68 | if ($provider == 'Persona') |
---|
| 69 | { |
---|
| 70 | $template->assign('OAUTH_USER', array( |
---|
| 71 | 'provider' => $provider, |
---|
| 72 | 'username' => $user_identifier, |
---|
| 73 | 'u_profile' => null, |
---|
| 74 | 'avatar' => null, |
---|
| 75 | )); |
---|
| 76 | |
---|
| 77 | oauth_assign_template_vars(); |
---|
| 78 | $template->append('OAUTH', array('persona_email'=>$user_identifier), true); |
---|
[21584] | 79 | |
---|
[26605] | 80 | $conf['oauth']['include_common_template'] = true; |
---|
| 81 | } |
---|
| 82 | else |
---|
[21584] | 83 | { |
---|
[26605] | 84 | require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'); |
---|
| 85 | |
---|
| 86 | $hybridauth = new Hybrid_Auth($hybridauth_conf); |
---|
| 87 | $adapter = $hybridauth->authenticate($provider); |
---|
| 88 | $remote_user = $adapter->getUserProfile(); |
---|
| 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!', 403); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | $template->assign('OAUTH_USER', array( |
---|
| 98 | 'provider' => $provider, |
---|
| 99 | 'username' => $remote_user->displayName, |
---|
| 100 | 'u_profile' => $remote_user->profileURL, |
---|
| 101 | 'avatar' => $remote_user->photoURL, |
---|
| 102 | )); |
---|
[21584] | 103 | } |
---|
[26605] | 104 | |
---|
| 105 | $oauth_id = $provider.'---'.$user_identifier; |
---|
| 106 | |
---|
[26556] | 107 | $page['infos'][] = l10n('Your registration is almost done, please complete the registration form.'); |
---|
[20293] | 108 | |
---|
| 109 | // form submited |
---|
| 110 | if (isset($_POST['submit'])) |
---|
| 111 | { |
---|
[26604] | 112 | $user_id = register_user( |
---|
| 113 | $_POST['login'], |
---|
| 114 | hash('sha1', $oauth_id.$conf['secret_key']), |
---|
| 115 | $_POST['mail_address'], |
---|
| 116 | true, |
---|
| 117 | $page['errors'], |
---|
| 118 | false |
---|
| 119 | ); |
---|
| 120 | |
---|
| 121 | if ($user_id !== false) |
---|
[20293] | 122 | { |
---|
| 123 | pwg_unset_session_var('oauth_new_user'); |
---|
| 124 | |
---|
[21584] | 125 | // update oauth field |
---|
[20293] | 126 | $query = ' |
---|
[26604] | 127 | UPDATE ' . USERS_TABLE . ' |
---|
| 128 | SET oauth_id = "' . $oauth_id . '" |
---|
| 129 | WHERE ' . $conf['user_fields']['id'] . ' = ' . $user_id . ' |
---|
[20293] | 130 | ;'; |
---|
| 131 | pwg_query($query); |
---|
| 132 | |
---|
| 133 | // log_user and redirect |
---|
| 134 | log_user($user_id, false); |
---|
| 135 | redirect('profile.php'); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | unset($_POST['submit']); |
---|
| 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
| 142 | // overwrite fields with remote datas |
---|
[26605] | 143 | if ($provider == 'Persona') |
---|
| 144 | { |
---|
| 145 | $_POST['login'] = ''; |
---|
| 146 | $_POST['mail_address'] = $user_identifier; |
---|
| 147 | } |
---|
| 148 | else |
---|
| 149 | { |
---|
| 150 | $_POST['login'] = $remote_user->displayName; |
---|
| 151 | $_POST['mail_address'] = $remote_user->email; |
---|
| 152 | } |
---|
[20293] | 153 | } |
---|
| 154 | |
---|
| 155 | // template |
---|
[26604] | 156 | $template->assign('OAUTH_PATH', OAUTH_PATH); |
---|
[20323] | 157 | $template->set_prefilter('register', 'oauth_add_profile_prefilter'); |
---|
[20293] | 158 | $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter'); |
---|
| 159 | } |
---|
[26605] | 160 | catch (Exception $e) |
---|
| 161 | { |
---|
[26556] | 162 | $page['errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode()); |
---|
[20293] | 163 | } |
---|
| 164 | } |
---|
[20301] | 165 | // display login buttons |
---|
| 166 | else if ($conf['oauth']['display_register']) |
---|
[20293] | 167 | { |
---|
[26604] | 168 | oauth_assign_template_vars(get_gallery_home_url()); |
---|
[20293] | 169 | |
---|
| 170 | $template->set_prefilter('register', 'oauth_add_buttons_prefilter'); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * profile page |
---|
| 177 | */ |
---|
| 178 | function oauth_begin_profile() |
---|
| 179 | { |
---|
[26605] | 180 | global $template, $user, $hybridauth_conf, $page, $user; |
---|
[20293] | 181 | |
---|
[26605] | 182 | if (empty($user['oauth_id'])) |
---|
[20293] | 183 | { |
---|
| 184 | return; |
---|
| 185 | } |
---|
| 186 | |
---|
[26605] | 187 | list($provider, $user_identifier) = explode('---', $user['oauth_id'], 2); |
---|
[20293] | 188 | |
---|
| 189 | try { |
---|
[26605] | 190 | if ($provider == 'Persona') |
---|
| 191 | { |
---|
| 192 | $template->assign('OAUTH_USER', array( |
---|
| 193 | 'provider' => $provider, |
---|
| 194 | 'username' => $user_identifier, |
---|
| 195 | 'u_profile' => null, |
---|
| 196 | 'avatar' => null, |
---|
| 197 | )); |
---|
| 198 | } |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'); |
---|
| 202 | |
---|
| 203 | $hybridauth = new Hybrid_Auth($hybridauth_conf); |
---|
| 204 | $adapter = $hybridauth->getAdapter($provider); |
---|
| 205 | $remote_user = $adapter->getUserProfile(); |
---|
| 206 | |
---|
| 207 | $template->assign('OAUTH_USER', array( |
---|
| 208 | 'provider' => $provider, |
---|
| 209 | 'username' => $remote_user->displayName, |
---|
| 210 | 'u_profile' => $remote_user->profileURL, |
---|
| 211 | 'avatar' => $remote_user->photoURL, |
---|
| 212 | )); |
---|
| 213 | } |
---|
[20293] | 214 | |
---|
[26604] | 215 | $template->assign('OAUTH_PATH', OAUTH_PATH); |
---|
[20293] | 216 | $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter'); |
---|
[20301] | 217 | $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter'); |
---|
[20293] | 218 | } |
---|
[26605] | 219 | catch (Exception $e) |
---|
| 220 | { |
---|
[26556] | 221 | $page['errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode()); |
---|
[20293] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | /** |
---|
| 227 | * logout |
---|
| 228 | */ |
---|
| 229 | function oauth_logout($user_id) |
---|
| 230 | { |
---|
[26604] | 231 | global $hybridauth_conf; |
---|
[20293] | 232 | |
---|
[26604] | 233 | $oauth_id = get_oauth_id($user_id); |
---|
[20293] | 234 | |
---|
[26604] | 235 | if (!isset($oauth_id)) |
---|
[20293] | 236 | { |
---|
| 237 | return; |
---|
| 238 | } |
---|
[26604] | 239 | |
---|
[26605] | 240 | list($provider, $identifier) = explode('---', $oauth_id, 2); |
---|
[20293] | 241 | |
---|
| 242 | |
---|
[26605] | 243 | if ($provider != 'Persona') |
---|
| 244 | { |
---|
| 245 | require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'); |
---|
| 246 | |
---|
| 247 | try { |
---|
| 248 | $hybridauth = new Hybrid_Auth($hybridauth_conf); |
---|
| 249 | $adapter = $hybridauth->getAdapter($provider); |
---|
| 250 | $adapter->logout(); |
---|
| 251 | } |
---|
| 252 | catch (Exception $e) { |
---|
| 253 | $_SESSION['page_errors'][] = l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>', $e->getCode()); |
---|
| 254 | } |
---|
[20293] | 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | * identification menu block |
---|
| 261 | */ |
---|
| 262 | function oauth_blockmanager($menu_ref_arr) |
---|
| 263 | { |
---|
[26604] | 264 | global $template, $conf, $hybridauth_conf; |
---|
[20293] | 265 | |
---|
| 266 | $menu = &$menu_ref_arr[0]; |
---|
| 267 | |
---|
[26604] | 268 | if ($hybridauth_conf['enabled'] == 0 or |
---|
| 269 | !$conf['oauth']['display_menubar'] or |
---|
| 270 | $menu->get_block('mbIdentification') == null |
---|
| 271 | ) |
---|
[20293] | 272 | { |
---|
| 273 | return; |
---|
| 274 | } |
---|
| 275 | |
---|
[26605] | 276 | $u_redirect = !empty($_GET['redirect']) ? urldecode($_GET['redirect']) : get_gallery_home_url(); |
---|
| 277 | oauth_assign_template_vars($u_redirect); |
---|
[20293] | 278 | |
---|
[21584] | 279 | $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter'); |
---|
[20293] | 280 | } |
---|
| 281 | |
---|
[26605] | 282 | |
---|
| 283 | /** |
---|
| 284 | * load common javascript |
---|
| 285 | */ |
---|
| 286 | function oauth_page_header() |
---|
[26604] | 287 | { |
---|
| 288 | global $conf, $template; |
---|
[26605] | 289 | |
---|
[26604] | 290 | if (isset($conf['oauth']['include_common_template'])) |
---|
| 291 | { |
---|
| 292 | $template->set_filename('oauth', realpath(OAUTH_PATH . 'template/identification_common.tpl')); |
---|
| 293 | $template->parse('oauth'); |
---|
| 294 | } |
---|
| 295 | } |
---|
[20293] | 296 | |
---|
[26604] | 297 | |
---|
[20293] | 298 | /** |
---|
| 299 | * prefilters |
---|
| 300 | */ |
---|
| 301 | function oauth_add_buttons_prefilter($content) |
---|
| 302 | { |
---|
| 303 | $search = '</form>'; |
---|
[20301] | 304 | $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl'); |
---|
[20293] | 305 | return str_replace($search, $search.$add, $content); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | function oauth_remove_password_fields_prefilter($content) |
---|
| 309 | { |
---|
| 310 | $search = 'type="password" '; |
---|
| 311 | $add = 'disabled="disabled" '; |
---|
| 312 | $script = ' |
---|
[23808] | 313 | {footer_script require="jquery"} |
---|
[26604] | 314 | jQuery("input[type=password], input[name=send_password_by_mail]").parent().hide(); |
---|
[21584] | 315 | {/footer_script}'; |
---|
[20293] | 316 | |
---|
| 317 | $content = str_replace($search, $search.$add, $content); |
---|
| 318 | return $content.$script; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | function oauth_add_profile_prefilter($content) |
---|
| 322 | { |
---|
[26604] | 323 | $search = '#(</legend>)#'; |
---|
[20293] | 324 | $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl'); |
---|
[26604] | 325 | return preg_replace($search, '$1 '.$add, $content, 1); |
---|
[20293] | 326 | } |
---|
| 327 | |
---|
[21584] | 328 | function oauth_add_menubar_buttons_prefilter($content) |
---|
[20293] | 329 | { |
---|
[26604] | 330 | $search = '#({include file=\$block->template\|@?get_extent:\$id ?})#'; |
---|
[20301] | 331 | $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl'); |
---|
[26604] | 332 | return preg_replace($search, '$1 '.$add, $content); |
---|
[20293] | 333 | } |
---|