Subversion Repositories oidplus

Rev

Rev 801 | Rev 1008 | 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 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusPagePublicLoginGoogle extends OIDplusPagePluginPublic {
  23.  
  24.         public function action($actionID, $params) {
  25.                 throw new OIDplusException(_L('Unknown action ID'));
  26.         }
  27.  
  28.         public function init($html=true) {
  29.                 // Nothing
  30.         }
  31.  
  32.         public function gui($id, &$out, &$handled) {
  33.                 if ($id === 'oidplus:login_google') {
  34.                         $handled = true;
  35.                         $out['title'] = _L('Login using Google');
  36.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  37.  
  38.                         if (!OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
  39.                                 $out['icon'] = 'img/error.png';
  40.                                 $out['text'] = _L('Google OAuth authentication is disabled on this system.');
  41.                                 return;
  42.                         }
  43.  
  44.                         $target =
  45.                                 "https://accounts.google.com/o/oauth2/v2/auth?".
  46.                                 "response_type=code&".
  47.                                 "client_id=".urlencode(OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_CLIENT_ID'))."&".
  48.                                 "scope=".implode('%20', array(/*'openid',*/ 'email', 'profile'))."&".
  49.                                 "redirect_uri=".urlencode(OIDplus::webpath(__DIR__,OIDplus::PATH_ABSOLUTE_CANONICAL).'oauth.php')."&".
  50.                                 "state=".urlencode($_COOKIE['csrf_token_weak']);
  51.                         $out['text'] = '<p>'._L('Please wait...').'</p><script>window.location.href = '.js_escape($target).';</script>';
  52.                 }
  53.         }
  54.  
  55.         public function publicSitemap(&$out) {
  56.                 $out[] = 'oidplus:login_google';
  57.         }
  58.  
  59.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  60.                 return true;
  61.         }
  62.  
  63.         public function tree_search($request) {
  64.                 return false;
  65.         }
  66.  
  67.         public function implementsFeature($id) {
  68.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.5') return true; // alternativeLoginMethods()
  69.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
  70.                 return false;
  71.         }
  72.  
  73.         public function alternativeLoginMethods() {
  74.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.5
  75.                 $logins = array();
  76.                 if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
  77.                         $logins[] = array(
  78.                                 'oidplus:login_google',
  79.                                 _L('Login using Google'),
  80.                                 OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'
  81.                         );
  82.                 }
  83.                 return $logins;
  84.         }
  85.  
  86.         public function getNotifications($user=null): array {
  87.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.8
  88.                 $notifications = array();
  89.                 if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
  90.                         if (OIDplus::baseConfig()->getValue('GOOGLE_OAUTH2_ENABLED', false)) {
  91.                                 if (!function_exists('curl_init')) {
  92.                                         $title = _L('Google OAuth Login');
  93.                                         $notifications[] = array('ERR', _L('OIDplus plugin "%1" is enabled, but required PHP extension "%2" is not installed.', $title, 'php_curl'));
  94.                                 }
  95.                         }
  96.                 }
  97.                 return $notifications;
  98.         }
  99.  
  100. }
  101.