Subversion Repositories oidplus

Rev

Rev 1372 | 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.         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_facebook') {
  58.                         $handled = true;
  59.                         $out['title'] = _L('Login using Facebook');
  60.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  61.  
  62.                         if (!OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_ENABLED', false)) {
  63.                                 throw new OIDplusException(_L('Facebook OAuth authentication is disabled on this system.'), $out['title']);
  64.                         }
  65.  
  66.                         $out['text']  = '<p>'._L('Please wait...').'</p>';
  67.                         $out['text'] .= '<noscript>';
  68.                         $out['text'] .= '<p><font color="red">'._L('You need to enable JavaScript to use this feature.').'</font></p>';
  69.                         $out['text'] .= '</noscript>';
  70.                         $out['text'] .= '<form action="https://www.facebook.com/v8.0/dialog/oauth" method="GET">';
  71.                         $out['text'] .= '<input type="hidden" name="client_id" value="'.htmlentities(OIDplus::baseConfig()->getValue('FACEBOOK_OAUTH2_CLIENT_ID')).'">'."\n";
  72.                         $out['text'] .= '<input type="hidden" name="redirect_uri" value="'.htmlentities('').'">'."\n"; // URL will be filled by JavaScript
  73.                         $out['text'] .= '<input type="hidden" name="state" value="'.htmlentities($_COOKIE['csrf_token_weak']).'">'."\n"; // URL will be added by JavaScript
  74.                         $out['text'] .= '<input type="hidden" name="scope" value="'.htmlentities(implode(' ', array('email'))).'">'."\n";
  75.                         $out['text'] .= '</form>';
  76.                         $out['text'] .= '<script>';
  77.                         $out['text'] .= 'redir_url = getSystemUrl() + "plugins/viathinksoft/publicPages/820_login_facebook/oauth.php";';
  78.                         $out['text'] .= 'document.forms[0].elements.redirect_uri.value = redir_url;';
  79.                         $out['text'] .= 'document.forms[0].elements.state.value = redir_url + "|" + document.forms[0].elements.state.value;';
  80.                         $out['text'] .= 'document.forms[0].submit();';
  81.                         $out['text'] .= '</script>';
  82.                 }
  83.         }
  84.  
  85.         /**
  86.          * @param array $out
  87.          * @return void
  88.          */
  89.         public function publicSitemap(array &$out) {
  90.                 $out[] = 'oidplus:login_facebook';
  91.         }
  92.  
  93.         /**
  94.          * @param array $json
  95.          * @param string|null $ra_email
  96.          * @param bool $nonjs
  97.          * @param string $req_goto
  98.          * @return bool
  99.          */
  100.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  101.                 return true;
  102.         }
  103.  
  104.         /**
  105.          * @param string $request
  106.          * @return array|false
  107.          */
  108.         public function tree_search(string $request) {
  109.                 return false;
  110.         }
  111.  
  112.         /**
  113.          * Implements interface INTF_OID_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 INTF_OID_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 (!url_post_contents_available(true, $reason)) {
  140.                                         $title = _L('Facebook OAuth Login');
  141.                                         $notifications[] = new OIDplusNotification('ERR', _L('OIDplus plugin "%1" is enabled, but OIDplus cannot connect to the Internet.', htmlentities($title)).' '.$reason);
  142.                                 }
  143.                         }
  144.                 }
  145.                 return $notifications;
  146.         }
  147.  
  148. }
  149.