Rev 511 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
436 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
436 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
20 | # More information about the OAuth2 implementation: |
||
21 | # - https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow |
||
22 | # - https://developers.facebook.com/tools/explorer/ |
||
23 | |||
24 | require_once __DIR__ . '/../../../includes/oidplus.inc.php'; |
||
25 | |||
26 | OIDplus::init(true); |
||
440 | daniel-mar | 27 | set_exception_handler(array('OIDplusGui', 'html_exception_handler')); |
436 | daniel-mar | 28 | |
532 | daniel-mar | 29 | if (OIDplus::baseConfig()->getValue('DISABLE_PLUGIN_OIDplusPagePublicLoginFacebook', false)) { |
30 | throw new OIDplusException(_L('This plugin was disabled by the system administrator!')); |
||
31 | } |
||
32 | |||
436 | daniel-mar | 33 | if (!OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_ENABLED', false)) { |
34 | throw new OIDplusException(_L('Facebook OAuth authentication is disabled on this system.')); |
||
35 | } |
||
36 | |||
37 | if (!isset($_GET['code'])) die(); |
||
38 | if (!isset($_GET['state'])) die(); |
||
39 | |||
40 | if ($_GET['state'] != $_COOKIE['csrf_token']) { |
||
41 | die('Invalid CSRF token'); |
||
42 | } |
||
43 | |||
484 | daniel-mar | 44 | if (!function_exists('curl_init')) { |
464 | daniel-mar | 45 | die(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl')); |
463 | daniel-mar | 46 | } |
47 | |||
436 | daniel-mar | 48 | // Get access token |
49 | |||
50 | $ch = curl_init(); |
||
496 | daniel-mar | 51 | if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem'); |
436 | daniel-mar | 52 | curl_setopt($ch, CURLOPT_URL,"https://graph.facebook.com/v8.0/oauth/access_token?". |
53 | "client_id=".urlencode(OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_CLIENT_ID'))."&". |
||
496 | daniel-mar | 54 | "redirect_uri=".urlencode(OIDplus::webpath(__DIR__,false).'oauth.php')."&". |
436 | daniel-mar | 55 | "client_secret=".urlencode(OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_CLIENT_SECRET'))."&". |
56 | "code=".$_GET['code'] |
||
57 | ); |
||
58 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
59 | $cont = curl_exec($ch); |
||
60 | curl_close($ch); |
||
61 | $data = json_decode($cont,true); |
||
62 | if (isset($data['error'])) { |
||
63 | echo '<h2>Error at step 2</h2>'; |
||
64 | echo '<p>'.$data['error']['message'].'</p>'; |
||
65 | die(); |
||
66 | } |
||
67 | $access_token = $data['access_token']; |
||
68 | |||
69 | // Get user infos |
||
70 | |||
71 | $ch = curl_init(); |
||
496 | daniel-mar | 72 | if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem'); |
436 | daniel-mar | 73 | curl_setopt($ch, CURLOPT_URL,"https://graph.facebook.com/v8.0/me?". |
74 | "fields=id,email,name&". |
||
75 | "access_token=".urlencode($access_token) |
||
76 | ); |
||
77 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
78 | $cont = curl_exec($ch); |
||
79 | curl_close($ch); |
||
80 | $data = json_decode($cont,true); |
||
81 | if (isset($data['error'])) { |
||
440 | daniel-mar | 82 | throw new OIDplusException(_L('Error receiving the authentication token from %1: %2','Facebook',$data['error']['message'])); |
436 | daniel-mar | 83 | } |
84 | $personal_name = $data['name']; |
||
85 | $email = !isset($data['email']) ? '' : $data['email']; |
||
86 | if (empty($email)) { |
||
440 | daniel-mar | 87 | throw new OIDplusException(_L('Your Facebook account does not have an email address.')); |
436 | daniel-mar | 88 | } |
89 | |||
90 | // Everything's done! Now login and/or create account |
||
91 | |||
92 | if (!empty($email)) { |
||
93 | $ra = new OIDplusRA($email); |
||
94 | if (!$ra->existing()) { |
||
95 | $ra->register_ra(null); // create a user account without password |
||
96 | |||
97 | OIDplus::db()->query("update ###ra set ra_name = ?, personal_name = ? where email = ?", array($personal_name, $personal_name, $email)); |
||
98 | |||
99 | OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' was created because of successful Facebook OAuth2 login"); |
||
100 | } |
||
101 | |||
102 | OIDplus::logger()->log("[OK]RA($email)!", "RA '$email' logged in via Facebook OAuth2"); |
||
103 | OIDplus::authUtils()::raLogin($email); |
||
104 | |||
105 | OIDplus::db()->query("UPDATE ###ra set last_login = ".OIDplus::db()->sqlDate()." where email = ?", array($email)); |
||
106 | |||
107 | // Go back to OIDplus |
||
108 | |||
496 | daniel-mar | 109 | header('Location:'.OIDplus::webpath(null,false)); |
436 | daniel-mar | 110 | } |