1 | <?php |
---|
2 | defined('OAUTH_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | function 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 | |
---|
23 | /** |
---|
24 | * identification page |
---|
25 | */ |
---|
26 | function oauth_begin_identification() |
---|
27 | { |
---|
28 | global $template, $conf; |
---|
29 | |
---|
30 | oauth_assign_template_vars(); |
---|
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 | */ |
---|
39 | function oauth_try_log_user($success, $username) |
---|
40 | { |
---|
41 | global $conf, $redirect_to; |
---|
42 | |
---|
43 | $query = ' |
---|
44 | SELECT 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'][] = sprintf(l10n('You registered with a %s account, please sign in with the same account.'), $provider); |
---|
55 | |
---|
56 | $redirect_to = get_root_url().'identification.php'; // variable used by identification.php |
---|
57 | return true; |
---|
58 | } |
---|
59 | |
---|
60 | return false; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * register page |
---|
66 | */ |
---|
67 | function oauth_begin_register() |
---|
68 | { |
---|
69 | global $conf, $template, $hybridauth_conf, $page; |
---|
70 | |
---|
71 | // coming from identification page |
---|
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(); |
---|
82 | |
---|
83 | // security, check remote identifier |
---|
84 | if ($remote_user->identifier != $user_identifier) |
---|
85 | { |
---|
86 | pwg_unset_session_var('oauth_new_user'); |
---|
87 | throw new Exception('Hacking attempt!', 403); |
---|
88 | } |
---|
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.')); |
---|
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']), |
---|
108 | $_POST['mail_address'] |
---|
109 | ); |
---|
110 | |
---|
111 | if (count($page['errors']) == 0) |
---|
112 | { |
---|
113 | pwg_unset_session_var('oauth_new_user'); |
---|
114 | $user_id = get_userid($_POST['login']); |
---|
115 | |
---|
116 | // update oauth field |
---|
117 | $query = ' |
---|
118 | UPDATE '.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; |
---|
135 | $_POST['mail_address'] = $remote_user->email; |
---|
136 | } |
---|
137 | |
---|
138 | // template |
---|
139 | $template->set_prefilter('register', 'oauth_add_profile_prefilter'); |
---|
140 | $template->set_prefilter('register', 'oauth_remove_password_fields_prefilter'); |
---|
141 | } |
---|
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())); |
---|
144 | } |
---|
145 | } |
---|
146 | // display login buttons |
---|
147 | else if ($conf['oauth']['display_register']) |
---|
148 | { |
---|
149 | oauth_assign_template_vars(); |
---|
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 | */ |
---|
160 | function oauth_begin_profile() |
---|
161 | { |
---|
162 | global $template, $user, $conf, $hybridauth_conf, $page; |
---|
163 | |
---|
164 | $query = ' |
---|
165 | SELECT 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( |
---|
187 | 'OAUTH_PROVIDER' => $provider, |
---|
188 | 'OAUTH_USERNAME' => $remote_user->displayName, |
---|
189 | 'OAUTH_PROFILE_URL' => $remote_user->profileURL, |
---|
190 | 'OAUTH_AVATAR' => $remote_user->photoURL, |
---|
191 | 'OAUTH_PATH' => OAUTH_PATH, |
---|
192 | )); |
---|
193 | |
---|
194 | $template->set_prefilter('profile_content', 'oauth_add_profile_prefilter'); |
---|
195 | $template->set_prefilter('profile_content', 'oauth_remove_password_fields_prefilter'); |
---|
196 | } |
---|
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())); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | /** |
---|
204 | * logout |
---|
205 | */ |
---|
206 | function oauth_logout($user_id) |
---|
207 | { |
---|
208 | global $conf, $hybridauth_conf; |
---|
209 | |
---|
210 | $query = ' |
---|
211 | SELECT 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 | } |
---|
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()); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | /** |
---|
239 | * identification menu block |
---|
240 | */ |
---|
241 | function oauth_blockmanager($menu_ref_arr) |
---|
242 | { |
---|
243 | global $template, $conf; |
---|
244 | |
---|
245 | $menu = &$menu_ref_arr[0]; |
---|
246 | |
---|
247 | if ( !$conf['oauth']['display_menubar'] or $menu->get_block('mbIdentification') == null ) |
---|
248 | { |
---|
249 | return; |
---|
250 | } |
---|
251 | |
---|
252 | oauth_assign_template_vars(); |
---|
253 | $template->assign('REDIRECT_TO', get_gallery_home_url()); |
---|
254 | |
---|
255 | $template->set_prefilter('menubar', 'oauth_add_menubar_buttons_prefilter'); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | /** |
---|
260 | * prefilters |
---|
261 | */ |
---|
262 | function oauth_add_buttons_prefilter($content) |
---|
263 | { |
---|
264 | $search = '</form>'; |
---|
265 | $add = file_get_contents(OAUTH_PATH . 'template/identification_page.tpl'); |
---|
266 | return str_replace($search, $search.$add, $content); |
---|
267 | } |
---|
268 | |
---|
269 | function oauth_remove_password_fields_prefilter($content) |
---|
270 | { |
---|
271 | $search = 'type="password" '; |
---|
272 | $add = 'disabled="disabled" '; |
---|
273 | $script = ' |
---|
274 | {footer_script require="jquery"} |
---|
275 | jQuery("input[type=\'password\'], input[name=\'send_password_by_mail\']").parent().css("display", "none"); |
---|
276 | {/footer_script}'; |
---|
277 | |
---|
278 | $content = str_replace($search, $search.$add, $content); |
---|
279 | return $content.$script; |
---|
280 | } |
---|
281 | |
---|
282 | function oauth_add_profile_prefilter($content) |
---|
283 | { |
---|
284 | $search = '#</legend>#'; |
---|
285 | $add = file_get_contents(OAUTH_PATH . 'template/profile.tpl'); |
---|
286 | return preg_replace($search, '</legend> '.$add, $content, 1); |
---|
287 | } |
---|
288 | |
---|
289 | function oauth_add_menubar_buttons_prefilter($content) |
---|
290 | { |
---|
291 | $search = '{include file=$block->template|@get_extent:$id }'; |
---|
292 | $add = file_get_contents(OAUTH_PATH . 'template/identification_menubar.tpl'); |
---|
293 | return str_replace($search, $search.$add, $content); |
---|
294 | } |
---|
295 | |
---|
296 | ?> |
---|