Rev 496 | 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 | |
29 | if (!OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_ENABLED', false)) { |
||
30 | throw new OIDplusException(_L('Facebook OAuth authentication is disabled on this system.')); |
||
31 | } |
||
32 | |||
33 | if (!isset($_GET['code'])) die(); |
||
34 | if (!isset($_GET['state'])) die(); |
||
35 | |||
36 | if ($_GET['state'] != $_COOKIE['csrf_token']) { |
||
37 | die('Invalid CSRF token'); |
||
38 | } |
||
39 | |||
484 | daniel-mar | 40 | if (!function_exists('curl_init')) { |
464 | daniel-mar | 41 | 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 | 42 | } |
43 | |||
436 | daniel-mar | 44 | // Get access token |
45 | |||
46 | $ch = curl_init(); |
||
496 | daniel-mar | 47 | if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem'); |
436 | daniel-mar | 48 | curl_setopt($ch, CURLOPT_URL,"https://graph.facebook.com/v8.0/oauth/access_token?". |
49 | "client_id=".urlencode(OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_CLIENT_ID'))."&". |
||
496 | daniel-mar | 50 | "redirect_uri=".urlencode(OIDplus::webpath(__DIR__,false).'oauth.php')."&". |
436 | daniel-mar | 51 | "client_secret=".urlencode(OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_CLIENT_SECRET'))."&". |
52 | "code=".$_GET['code'] |
||
53 | ); |
||
54 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
55 | $cont = curl_exec($ch); |
||
56 | curl_close($ch); |
||
57 | $data = json_decode($cont,true); |
||
58 | if (isset($data['error'])) { |
||
59 | echo '<h2>Error at step 2</h2>'; |
||
60 | echo '<p>'.$data['error']['message'].'</p>'; |
||
61 | die(); |
||
62 | } |
||
63 | $access_token = $data['access_token']; |
||
64 | |||
65 | // Get user infos |
||
66 | |||
67 | $ch = curl_init(); |
||
496 | daniel-mar | 68 | if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem'); |
436 | daniel-mar | 69 | curl_setopt($ch, CURLOPT_URL,"https://graph.facebook.com/v8.0/me?". |
70 | "fields=id,email,name&". |
||
71 | "access_token=".urlencode($access_token) |
||
72 | ); |
||
73 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
74 | $cont = curl_exec($ch); |
||
75 | curl_close($ch); |
||
76 | $data = json_decode($cont,true); |
||
77 | if (isset($data['error'])) { |
||
440 | daniel-mar | 78 | throw new OIDplusException(_L('Error receiving the authentication token from %1: %2','Facebook',$data['error']['message'])); |
436 | daniel-mar | 79 | } |
80 | $personal_name = $data['name']; |
||
81 | $email = !isset($data['email']) ? '' : $data['email']; |
||
82 | if (empty($email)) { |
||
440 | daniel-mar | 83 | throw new OIDplusException(_L('Your Facebook account does not have an email address.')); |
436 | daniel-mar | 84 | } |
85 | |||
86 | // Everything's done! Now login and/or create account |
||
87 | |||
88 | if (!empty($email)) { |
||
89 | $ra = new OIDplusRA($email); |
||
90 | if (!$ra->existing()) { |
||
91 | $ra->register_ra(null); // create a user account without password |
||
92 | |||
93 | OIDplus::db()->query("update ###ra set ra_name = ?, personal_name = ? where email = ?", array($personal_name, $personal_name, $email)); |
||
94 | |||
95 | OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' was created because of successful Facebook OAuth2 login"); |
||
96 | } |
||
97 | |||
98 | OIDplus::logger()->log("[OK]RA($email)!", "RA '$email' logged in via Facebook OAuth2"); |
||
99 | OIDplus::authUtils()::raLogin($email); |
||
100 | |||
101 | OIDplus::db()->query("UPDATE ###ra set last_login = ".OIDplus::db()->sqlDate()." where email = ?", array($email)); |
||
102 | |||
103 | // Go back to OIDplus |
||
104 | |||
496 | daniel-mar | 105 | header('Location:'.OIDplus::webpath(null,false)); |
436 | daniel-mar | 106 | } |