1 | <?php |
---|
2 | defined('OAUTH_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | function load_hybridauth_conf() |
---|
5 | { |
---|
6 | global $hybridauth_conf, $conf; |
---|
7 | |
---|
8 | if (file_exists(PHPWG_ROOT_PATH.OAUTH_CONFIG)) |
---|
9 | { |
---|
10 | $hybridauth_conf = include(PHPWG_ROOT_PATH.OAUTH_CONFIG); |
---|
11 | $hybridauth_conf['base_url'] = OAUTH_PUBLIC; |
---|
12 | if (!empty($conf['oauth_debug_file'])) |
---|
13 | { |
---|
14 | $hybridauth_conf['debug_mode'] = true; |
---|
15 | $hybridauth_conf['debug_file'] = $conf['oauth_debug_file']; |
---|
16 | } |
---|
17 | return true; |
---|
18 | } |
---|
19 | else |
---|
20 | { |
---|
21 | return false; |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | function oauth_assign_template_vars($u_redirect=null) |
---|
26 | { |
---|
27 | global $template, $conf, $hybridauth_conf, $user; |
---|
28 | |
---|
29 | $conf['oauth']['include_common_template'] = true; |
---|
30 | |
---|
31 | if ($template->get_template_vars('OAUTH') == null) |
---|
32 | { |
---|
33 | if (!empty($user['oauth_id'])) |
---|
34 | { |
---|
35 | list($provider, $identifier) = explode('---', $user['oauth_id'], 2); |
---|
36 | if ($provider == 'Persona') |
---|
37 | { |
---|
38 | $persona_email = $identifier; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | $template->assign('OAUTH', array( |
---|
43 | 'conf' => $conf['oauth'], |
---|
44 | 'u_login' => get_root_url() . OAUTH_PATH . 'auth.php?provider=', |
---|
45 | 'providers' => $hybridauth_conf['providers'], |
---|
46 | 'persona_email' => @$persona_email, |
---|
47 | 'key' => get_ephemeral_key(0), |
---|
48 | )); |
---|
49 | $template->assign(array( |
---|
50 | 'OAUTH_PATH' => OAUTH_PATH, |
---|
51 | 'OAUTH_ABS_PATH' => realpath(OAUTH_PATH) . '/', |
---|
52 | 'ABS_ROOT_URL' => rtrim(get_gallery_home_url(), '/') . '/', |
---|
53 | )); |
---|
54 | } |
---|
55 | |
---|
56 | if (isset($u_redirect)) |
---|
57 | { |
---|
58 | $template->append('OAUTH', compact('u_redirect'), true); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | function get_oauth_id($user_id) |
---|
63 | { |
---|
64 | $query = ' |
---|
65 | SELECT oauth_id FROM ' . USER_INFOS_TABLE . ' |
---|
66 | WHERE user_id = ' . $user_id . ' |
---|
67 | AND oauth_id != "" |
---|
68 | ;'; |
---|
69 | $result = pwg_query($query); |
---|
70 | |
---|
71 | if (!pwg_db_num_rows($result)) |
---|
72 | { |
---|
73 | return null; |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | list($oauth_id) = pwg_db_fetch_row($result); |
---|
78 | return $oauth_id; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | function get_servername($with_port=false) |
---|
83 | { |
---|
84 | $scheme = 'http'; |
---|
85 | if ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ) |
---|
86 | { |
---|
87 | $scheme = 'https'; |
---|
88 | } |
---|
89 | |
---|
90 | $servername = $scheme . '://' . $_SERVER['HTTP_HOST']; |
---|
91 | if ($with_port) |
---|
92 | { |
---|
93 | $servername.= ':' . $_SERVER['SERVER_PORT']; |
---|
94 | } |
---|
95 | |
---|
96 | return $servername; |
---|
97 | } |
---|
98 | |
---|
99 | // http://www.sitepoint.com/authenticate-users-with-mozilla-persona/ |
---|
100 | function persona_verify() |
---|
101 | { |
---|
102 | $url = 'https://verifier.login.persona.org/verify'; |
---|
103 | |
---|
104 | $assert = filter_input( |
---|
105 | INPUT_POST, |
---|
106 | 'assertion', |
---|
107 | FILTER_UNSAFE_RAW, |
---|
108 | FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH |
---|
109 | ); |
---|
110 | |
---|
111 | $params = 'assertion=' . urlencode($assert) . '&audience=' . urlencode(get_servername(true)); |
---|
112 | |
---|
113 | $options = array( |
---|
114 | CURLOPT_URL => $url, |
---|
115 | CURLOPT_RETURNTRANSFER => true, |
---|
116 | CURLOPT_POST => true, |
---|
117 | CURLOPT_POSTFIELDS => $params, |
---|
118 | CURLOPT_SSL_VERIFYPEER => true, |
---|
119 | CURLOPT_SSL_VERIFYHOST => 2, |
---|
120 | ); |
---|
121 | |
---|
122 | $ch = curl_init(); |
---|
123 | curl_setopt_array($ch, $options); |
---|
124 | $result = curl_exec($ch); |
---|
125 | curl_close($ch); |
---|
126 | |
---|
127 | if ($result === false) |
---|
128 | { |
---|
129 | return false; |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | return json_decode($result, true); |
---|
134 | } |
---|
135 | } |
---|