Subversion Repositories oidplus

Rev

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