Rev 1189 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
1131 | daniel-mar | 26 | class OIDplusPagePublicLoginGoogle extends OIDplusPagePluginPublic |
27 | implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_5, /* alternativeLoginMethods */ |
||
28 | INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8 /* getNotifications */ |
||
29 | { |
||
635 | daniel-mar | 30 | |
1116 | daniel-mar | 31 | /** |
32 | * @param string $actionID |
||
33 | * @param array $params |
||
34 | * @return array |
||
35 | * @throws OIDplusException |
||
36 | */ |
||
37 | public function action(string $actionID, array $params): array { |
||
38 | return parent::action($actionID, $params); |
||
635 | daniel-mar | 39 | } |
40 | |||
1116 | daniel-mar | 41 | /** |
42 | * @param bool $html |
||
43 | * @return void |
||
44 | */ |
||
45 | public function init(bool $html=true) { |
||
635 | daniel-mar | 46 | // Nothing |
47 | } |
||
48 | |||
1116 | daniel-mar | 49 | /** |
50 | * @param string $id |
||
51 | * @param array $out |
||
52 | * @param bool $handled |
||
53 | * @return void |
||
54 | * @throws OIDplusException |
||
55 | */ |
||
56 | public function gui(string $id, array &$out, bool &$handled) { |
||
635 | daniel-mar | 57 | if ($id === 'oidplus:login_google') { |
58 | $handled = true; |
||
59 | $out['title'] = _L('Login using Google'); |
||
801 | daniel-mar | 60 | $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png'; |
635 | daniel-mar | 61 | |
62 | if (!OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) { |
||
1206 | daniel-mar | 63 | throw new OIDplusException(_L('Google OAuth authentication is disabled on this system.'), $out['title']); |
635 | daniel-mar | 64 | } |
65 | |||
66 | $target = |
||
67 | "https://accounts.google.com/o/oauth2/v2/auth?". |
||
68 | "response_type=code&". |
||
69 | "client_id=".urlencode(OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_ID'))."&". |
||
70 | "scope=".implode('%20', array(/*'openid',*/ 'email', 'profile'))."&". |
||
801 | daniel-mar | 71 | "redirect_uri=".urlencode(OIDplus::webpath(__DIR__,OIDplus::PATH_ABSOLUTE_CANONICAL).'oauth.php')."&". |
635 | daniel-mar | 72 | "state=".urlencode($_COOKIE['csrf_token_weak']); |
73 | $out['text'] = '<p>'._L('Please wait...').'</p><script>window.location.href = '.js_escape($target).';</script>'; |
||
74 | } |
||
75 | } |
||
76 | |||
1116 | daniel-mar | 77 | /** |
78 | * @param array $out |
||
79 | * @return void |
||
80 | */ |
||
81 | public function publicSitemap(array &$out) { |
||
635 | daniel-mar | 82 | $out[] = 'oidplus:login_google'; |
83 | } |
||
84 | |||
1116 | daniel-mar | 85 | /** |
86 | * @param array $json |
||
87 | * @param string|null $ra_email |
||
88 | * @param bool $nonjs |
||
89 | * @param string $req_goto |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
||
635 | daniel-mar | 93 | return true; |
94 | } |
||
95 | |||
1116 | daniel-mar | 96 | /** |
97 | * @param string $request |
||
98 | * @return array|false |
||
99 | */ |
||
100 | public function tree_search(string $request) { |
||
635 | daniel-mar | 101 | return false; |
102 | } |
||
103 | |||
1116 | daniel-mar | 104 | /** |
1131 | daniel-mar | 105 | * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_5 |
1116 | daniel-mar | 106 | * @return array |
107 | * @throws OIDplusException |
||
108 | */ |
||
1130 | daniel-mar | 109 | public function alternativeLoginMethods(): array { |
635 | daniel-mar | 110 | $logins = array(); |
111 | if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) { |
||
112 | $logins[] = array( |
||
113 | 'oidplus:login_google', |
||
114 | _L('Login using Google'), |
||
801 | daniel-mar | 115 | OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png' |
635 | daniel-mar | 116 | ); |
117 | } |
||
118 | return $logins; |
||
119 | } |
||
1000 | daniel-mar | 120 | |
1116 | daniel-mar | 121 | /** |
1131 | daniel-mar | 122 | * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8 |
1130 | daniel-mar | 123 | * @param string|null $user |
1116 | daniel-mar | 124 | * @return array |
125 | * @throws OIDplusException |
||
126 | */ |
||
1130 | daniel-mar | 127 | public function getNotifications(string $user=null): array { |
1000 | daniel-mar | 128 | $notifications = array(); |
129 | if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) { |
||
130 | if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) { |
||
1181 | daniel-mar | 131 | if (!url_post_contents_available(true, $reason)) { |
1000 | daniel-mar | 132 | $title = _L('Google OAuth Login'); |
1189 | daniel-mar | 133 | $notifications[] = new OIDplusNotification('ERR', _L('OIDplus plugin "%1" is enabled, but OIDplus cannot connect to the Internet.', htmlentities($title)).' '.$reason); |
1000 | daniel-mar | 134 | } |
135 | } |
||
136 | } |
||
137 | return $notifications; |
||
138 | } |
||
139 | |||
635 | daniel-mar | 140 | } |