[20293] | 1 | <?php |
---|
| 2 | defined('OAUTH_PATH') or die('Hacking attempt!'); |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | * identification page |
---|
| 6 | */ |
---|
| 7 | function oauth_begin_identification() |
---|
| 8 | { |
---|
[20323] | 9 | global $template, $conf; |
---|
[20293] | 10 | |
---|
[20323] | 11 | oauth_assign_template_vars(); |
---|
[20293] | 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 | */ |
---|
| 20 | function oauth_try_log_user($success, $username) |
---|
| 21 | { |
---|
| 22 | global $conf, $redirect_to; |
---|
| 23 | |
---|
| 24 | $query = ' |
---|
| 25 | SELECT 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); |
---|
[20301] | 35 | $_SESSION['page_errors'][] = sprintf(l10n('You registered with a %s account, please sign in with the same account.'), $provider); |
---|
[20293] | 36 | |
---|
[23808] | 37 | $redirect_to = get_root_url().'identification.php'; // variable used by identification.php |
---|
[20293] | 38 | return true; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | return false; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * register page |
---|
| 47 | */ |
---|
| 48 | function oauth_begin_register() |
---|
| 49 | { |
---|
[20323] | 50 | global $conf, $template, $hybridauth_conf, $page; |
---|
[20293] | 51 | |
---|
[23808] | 52 | // coming from identification page |
---|
[20293] | 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(); |
---|
[21584] | 63 | |
---|
| 64 | // security, check remote identifier |
---|
| 65 | if ($remote_user->identifier != $user_identifier) |
---|
| 66 | { |
---|
| 67 | pwg_unset_session_var('oauth_new_user'); |
---|
[23808] | 68 | throw new Exception('Hacking attempt!', 403); |
---|
[21584] | 69 | } |
---|
[20323] | 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.')); |
---|
[20293] | 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']), |
---|
[23815] | 89 | $_POST['mail_address'] |
---|
| 90 | ); |
---|
[20293] | 91 | |
---|
| 92 | if (count($page['errors']) == 0) |
---|
| 93 | { |
---|
| 94 | pwg_unset_session_var('oauth_new_user'); |
---|
| 95 | $user_id = get_userid($_POST['login']); |
---|
| 96 | |
---|
[21584] | 97 | // update oauth field |
---|
[20293] | 98 | $query = ' |
---|
| 99 | UPDATE '.USERS_TABLE.' |
---|
| 100 | SET oauth_id = "'.$oauth_id.'" |
---|
| 101 | WHERE '.$conf['user_fields']['id'].' = '.$user_id.' |
---|
| 102 | ;'; |
---|
| 103 | pwg_query($query); |
---|
| 104 | |
---|
| 105 | // log_user and redirect |
---|
| 106 | log_user($user_id, false); |
---|
| 107 | redirect('profile.php'); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | unset($_POST['submit']); |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | // overwrite fields with remote datas |
---|
| 115 | $_POST['login'] = $remote_user->displayName; |
---|
[23815] | 116 | $_POST['mail_address'] = $remote_user->email; |
---|
[20293] | 117 | } |
---|
| 118 | |
---|
| 119 | // template |
---|
[20323] | 120 | $template->set_prefilter('register', 'oauth_add_profile_prefilter'); |
---|
[20293] | 121 | $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter'); |
---|
| 122 | } |
---|
[20301] | 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())); |
---|
[20293] | 125 | } |
---|
| 126 | } |
---|
[20301] | 127 | // display login buttons |
---|
| 128 | else if ($conf['oauth']['display_register']) |
---|
[20293] | 129 | { |
---|
[20323] | 130 | oauth_assign_template_vars(); |
---|
[20293] | 131 | $template->assign('REDIRECT_TO', get_gallery_home_url()); |
---|
| 132 | |
---|
| 133 | $template->set_prefilter('register', 'oauth_add_buttons_prefilter'); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * profile page |
---|
| 140 | */ |
---|
| 141 | function oauth_begin_profile() |
---|
| 142 | { |
---|
[23808] | 143 | global $template, $user, $conf, $hybridauth_conf, $page; |
---|
[20293] | 144 | |
---|
| 145 | $query = ' |
---|
| 146 | SELECT oauth_id FROM '.USERS_TABLE.' |
---|
| 147 | WHERE '.$conf['user_fields']['id'].' = '.$user['id'].' |
---|
| 148 | AND oauth_id != "" |
---|
| 149 | ;'; |
---|
| 150 | $result = pwg_query($query); |
---|
| 151 | |
---|
| 152 | if (!pwg_db_num_rows($result)) |
---|
| 153 | { |
---|
| 154 | return; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | list($oauth_id) = pwg_db_fetch_row($result); |
---|
| 158 | list($provider) = explode('---', $oauth_id); |
---|
| 159 | |
---|
| 160 | require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'); |
---|
| 161 | |
---|
| 162 | try { |
---|
| 163 | $hybridauth = new Hybrid_Auth($hybridauth_conf); |
---|
| 164 | $adapter = $hybridauth->getAdapter($provider); |
---|
| 165 | $remote_user = $adapter->getUserProfile(); |
---|
| 166 | |
---|
| 167 | $template->assign(array( |
---|
[20301] | 168 | 'OAUTH_PROVIDER' => $provider, |
---|
[20293] | 169 | 'OAUTH_USERNAME' => $remote_user->displayName, |
---|
[20323] | 170 | 'OAUTH_PROFILE_URL' => $remote_user->profileURL, |
---|
[20293] | 171 | 'OAUTH_AVATAR' => $remote_user->photoURL, |
---|
| 172 | 'OAUTH_PATH' => OAUTH_PATH, |
---|
| 173 | )); |
---|
| 174 | |
---|
| 175 | $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter'); |
---|
[20301] | 176 | $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter'); |
---|
[20293] | 177 | } |
---|
[20301] | 178 | catch (Exception $e) { |
---|
| 179 | array_push($page['errors'], sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode())); |
---|
[20293] | 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * logout |
---|
| 186 | */ |
---|
| 187 | function oauth_logout($user_id) |
---|
| 188 | { |
---|
| 189 | global $conf, $hybridauth_conf; |
---|
| 190 | |
---|
| 191 | $query = ' |
---|
| 192 | SELECT oauth_id FROM '.USERS_TABLE.' |
---|
| 193 | WHERE '.$conf['user_fields']['id'].' = '.$user_id.' |
---|
| 194 | AND oauth_id != "" |
---|
| 195 | ;'; |
---|
| 196 | $result = pwg_query($query); |
---|
| 197 | |
---|
| 198 | if (!pwg_db_num_rows($result)) |
---|
| 199 | { |
---|
| 200 | return; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | list($oauth_id) = pwg_db_fetch_row($result); |
---|
| 204 | list($provider) = explode('---', $oauth_id); |
---|
| 205 | |
---|
| 206 | require_once(OAUTH_PATH . 'include/hybridauth/Hybrid/Auth.php'); |
---|
| 207 | |
---|
| 208 | try { |
---|
| 209 | $hybridauth = new Hybrid_Auth($hybridauth_conf); |
---|
| 210 | $adapter = $hybridauth->getAdapter($provider); |
---|
| 211 | $adapter->logout(); |
---|
| 212 | } |
---|
[20301] | 213 | catch (Exception $e) { |
---|
| 214 | $_SESSION['page_errors'][] = sprintf(l10n('An error occured, please contact the gallery owner. <i>Error code : %s</i>'), $e->getCode()); |
---|
[20293] | 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * identification menu block |
---|
| 221 | */ |
---|
| 222 | function oauth_blockmanager($menu_ref_arr) |
---|
| 223 | { |
---|
| 224 | global $template, $conf; |
---|
| 225 | |
---|
| 226 | $menu = &$menu_ref_arr[0]; |
---|
| 227 | |
---|
[20301] | 228 | if ( !$conf['oauth']['display_menubar'] or $menu->get_block('mbIdentification') == null ) |
---|
[20293] | 229 | { |
---|
| 230 | return; |
---|
| 231 | } |
---|
| 232 | |
---|
[20323] | 233 | oauth_assign_template_vars(); |
---|
[20293] | 234 | $template->assign('REDIRECT_TO', get_gallery_home_url()); |
---|
| 235 | |
---|
[21584] | 236 | $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter'); |
---|
[20293] | 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * prefilters |
---|
| 242 | */ |
---|
| 243 | function oauth_add_buttons_prefilter($content) |
---|
| 244 | { |
---|
| 245 | $search = '</form>'; |
---|
[20301] | 246 | $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl'); |
---|
[20293] | 247 | return str_replace($search, $search.$add, $content); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | function oauth_remove_password_fields_prefilter($content) |
---|
| 251 | { |
---|
| 252 | $search = 'type="password" '; |
---|
| 253 | $add = 'disabled="disabled" '; |
---|
| 254 | $script = ' |
---|
[23808] | 255 | {footer_script require="jquery"} |
---|
| 256 | jQuery("input[type=\'password\'], input[name=\'send_password_by_mail\']").parent().css("display", "none"); |
---|
[21584] | 257 | {/footer_script}'; |
---|
[20293] | 258 | |
---|
| 259 | $content = str_replace($search, $search.$add, $content); |
---|
| 260 | return $content.$script; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | function oauth_add_profile_prefilter($content) |
---|
| 264 | { |
---|
[20323] | 265 | $search = '#</legend>#'; |
---|
[20293] | 266 | $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl'); |
---|
[20323] | 267 | return preg_replace($search, '</legend> '.$add, $content, 1); |
---|
[20293] | 268 | } |
---|
| 269 | |
---|
[21584] | 270 | function oauth_add_menubar_buttons_prefilter($content) |
---|
[20293] | 271 | { |
---|
| 272 | $search = '{include file=$block->template|@get_extent:$id }'; |
---|
[20301] | 273 | $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl'); |
---|
[20293] | 274 | return str_replace($search, $search.$add, $content); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | ?> |
---|