Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1131 | 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
 
635 daniel-mar 26
class OIDplusPagePublicLoginGoogle extends OIDplusPagePluginPublic {
27
 
1116 daniel-mar 28
        /**
29
         * @param string $actionID
30
         * @param array $params
31
         * @return array
32
         * @throws OIDplusException
33
         */
34
        public function action(string $actionID, array $params): array {
35
                return parent::action($actionID, $params);
635 daniel-mar 36
        }
37
 
1116 daniel-mar 38
        /**
39
         * @param bool $html
40
         * @return void
41
         */
42
        public function init(bool $html=true) {
635 daniel-mar 43
                // Nothing
44
        }
45
 
1116 daniel-mar 46
        /**
47
         * @param string $id
48
         * @param array $out
49
         * @param bool $handled
50
         * @return void
51
         * @throws OIDplusException
52
         */
53
        public function gui(string $id, array &$out, bool &$handled) {
635 daniel-mar 54
                if ($id === 'oidplus:login_google') {
55
                        $handled = true;
56
                        $out['title'] = _L('Login using Google');
801 daniel-mar 57
                        $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
635 daniel-mar 58
 
59
                        if (!OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
800 daniel-mar 60
                                $out['icon'] = 'img/error.png';
635 daniel-mar 61
                                $out['text'] = _L('Google OAuth authentication is disabled on this system.');
62
                                return;
63
                        }
64
 
65
                        $target =
66
                                "https://accounts.google.com/o/oauth2/v2/auth?".
67
                                "response_type=code&".
68
                                "client_id=".urlencode(OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_ID'))."&".
69
                                "scope=".implode('%20', array(/*'openid',*/ 'email', 'profile'))."&".
801 daniel-mar 70
                                "redirect_uri=".urlencode(OIDplus::webpath(__DIR__,OIDplus::PATH_ABSOLUTE_CANONICAL).'oauth.php')."&".
635 daniel-mar 71
                                "state=".urlencode($_COOKIE['csrf_token_weak']);
72
                        $out['text'] = '<p>'._L('Please wait...').'</p><script>window.location.href = '.js_escape($target).';</script>';
73
                }
74
        }
75
 
1116 daniel-mar 76
        /**
77
         * @param array $out
78
         * @return void
79
         */
80
        public function publicSitemap(array &$out) {
635 daniel-mar 81
                $out[] = 'oidplus:login_google';
82
        }
83
 
1116 daniel-mar 84
        /**
85
         * @param array $json
86
         * @param string|null $ra_email
87
         * @param bool $nonjs
88
         * @param string $req_goto
89
         * @return bool
90
         */
91
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 92
                return true;
93
        }
94
 
1116 daniel-mar 95
        /**
96
         * @param string $request
97
         * @return array|false
98
         */
99
        public function tree_search(string $request) {
635 daniel-mar 100
                return false;
101
        }
102
 
1116 daniel-mar 103
        /**
104
         * @param string $id
105
         * @return bool
106
         */
107
        public function implementsFeature(string $id): bool {
1000 daniel-mar 108
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.5') return true; // alternativeLoginMethods()
109
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
635 daniel-mar 110
                return false;
111
        }
112
 
1116 daniel-mar 113
        /**
114
         * Implements interface 1.3.6.1.4.1.37476.2.5.2.3.5
115
         * @return array
116
         * @throws OIDplusException
117
         */
1130 daniel-mar 118
        public function alternativeLoginMethods(): array {
635 daniel-mar 119
                $logins = array();
120
                if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
121
                        $logins[] = array(
122
                                'oidplus:login_google',
123
                                _L('Login using Google'),
801 daniel-mar 124
                                OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'
635 daniel-mar 125
                        );
126
                }
127
                return $logins;
128
        }
1000 daniel-mar 129
 
1116 daniel-mar 130
        /**
131
         * Implements interface 1.3.6.1.4.1.37476.2.5.2.3.8
1130 daniel-mar 132
         * @param string|null $user
1116 daniel-mar 133
         * @return array
134
         * @throws OIDplusException
135
         */
1130 daniel-mar 136
        public function getNotifications(string $user=null): array {
1000 daniel-mar 137
                $notifications = array();
138
                if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
139
                        if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
140
                                if (!function_exists('curl_init')) {
141
                                        $title = _L('Google OAuth Login');
1008 daniel-mar 142
                                        $notifications[] = array('ERR', _L('OIDplus plugin "%1" is enabled, but the required PHP extension "%2" is not installed.', htmlentities($title), 'php_curl'));
1000 daniel-mar 143
                                }
144
                        }
145
                }
146
                return $notifications;
147
        }
148
 
635 daniel-mar 149
}