Subversion Repositories oidplus

Rev

Rev 1130 | Rev 1149 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  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. {
  30.  
  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);
  39.         }
  40.  
  41.         /**
  42.          * @param bool $html
  43.          * @return void
  44.          */
  45.         public function init(bool $html=true) {
  46.                 // Nothing
  47.         }
  48.  
  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) {
  57.                 if ($id === 'oidplus:login_google') {
  58.                         $handled = true;
  59.                         $out['title'] = _L('Login using Google');
  60.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  61.  
  62.                         if (!OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
  63.                                 $out['icon'] = 'img/error.png';
  64.                                 $out['text'] = _L('Google OAuth authentication is disabled on this system.');
  65.                                 return;
  66.                         }
  67.  
  68.                         $target =
  69.                                 "https://accounts.google.com/o/oauth2/v2/auth?".
  70.                                 "response_type=code&".
  71.                                 "client_id=".urlencode(OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_ID'))."&".
  72.                                 "scope=".implode('%20', array(/*'openid',*/ 'email', 'profile'))."&".
  73.                                 "redirect_uri=".urlencode(OIDplus::webpath(__DIR__,OIDplus::PATH_ABSOLUTE_CANONICAL).'oauth.php')."&".
  74.                                 "state=".urlencode($_COOKIE['csrf_token_weak']);
  75.                         $out['text'] = '<p>'._L('Please wait...').'</p><script>window.location.href = '.js_escape($target).';</script>';
  76.                 }
  77.         }
  78.  
  79.         /**
  80.          * @param array $out
  81.          * @return void
  82.          */
  83.         public function publicSitemap(array &$out) {
  84.                 $out[] = 'oidplus:login_google';
  85.         }
  86.  
  87.         /**
  88.          * @param array $json
  89.          * @param string|null $ra_email
  90.          * @param bool $nonjs
  91.          * @param string $req_goto
  92.          * @return bool
  93.          */
  94.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  95.                 return true;
  96.         }
  97.  
  98.         /**
  99.          * @param string $request
  100.          * @return array|false
  101.          */
  102.         public function tree_search(string $request) {
  103.                 return false;
  104.         }
  105.  
  106.         /**
  107.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_5
  108.          * @return array
  109.          * @throws OIDplusException
  110.          */
  111.         public function alternativeLoginMethods(): array {
  112.                 $logins = array();
  113.                 if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
  114.                         $logins[] = array(
  115.                                 'oidplus:login_google',
  116.                                 _L('Login using Google'),
  117.                                 OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'
  118.                         );
  119.                 }
  120.                 return $logins;
  121.         }
  122.  
  123.         /**
  124.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8
  125.          * @param string|null $user
  126.          * @return array
  127.          * @throws OIDplusException
  128.          */
  129.         public function getNotifications(string $user=null): array {
  130.                 $notifications = array();
  131.                 if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
  132.                         if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
  133.                                 if (!function_exists('curl_init')) {
  134.                                         $title = _L('Google OAuth Login');
  135.                                         $notifications[] = array('ERR', _L('OIDplus plugin "%1" is enabled, but the required PHP extension "%2" is not installed.', htmlentities($title), 'php_curl'));
  136.                                 }
  137.                         }
  138.                 }
  139.                 return $notifications;
  140.         }
  141.  
  142. }
  143.