Subversion Repositories oidplus

Rev

Rev 1116 | Go to most recent revision | Blame | 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 OIDplusAuthContentStoreSession extends OIDplusAuthContentStore {
  27.  
  28.         /**
  29.          * @return OIDplusSessionHandler
  30.          */
  31.         protected static function getSessionHandler(): OIDplusSessionHandler {
  32.                 static $sesHandler = null;
  33.                 if (is_null($sesHandler)) {
  34.                         $sesHandler = new OIDplusSessionHandler();
  35.                 }
  36.                 return $sesHandler;
  37.         }
  38.  
  39.         // Override abstract functions
  40.         # TODO: shouldn't we just include OIDplusSessionHandler in this class?
  41.  
  42.         /**
  43.          * @param string $name
  44.          * @param mixed|null $default
  45.          * @return mixed|null
  46.          * @throws OIDplusException
  47.          */
  48.         public function getValue(string $name, $default = NULL) {
  49.                 try {
  50.                         return self::getSessionHandler()->getValue($name, $default);
  51.                 } catch (\Exception $e) {
  52.                         self::getSessionHandler()->destroySession();
  53.                         // TODO: For some reason If destroySession() is called, we won't get this Exception?!
  54.                         throw new OIDplusException(_L('Internal error with session. Please reload the page and log-in again. %1', $e->getMessage()));
  55.                 }
  56.         }
  57.  
  58.         /**
  59.          * @param string $name
  60.          * @param mixed $value
  61.          * @return void
  62.          * @throws OIDplusException
  63.          */
  64.         public function setValue(string $name, $value) {
  65.                 self::getSessionHandler()->setValue($name, $value);
  66.         }
  67.  
  68.         /**
  69.          * @param string $name
  70.          * @return bool
  71.          * @throws OIDplusException
  72.          */
  73.         public function exists(string $name): bool {
  74.                 return self::getSessionHandler()->exists($name);
  75.         }
  76.  
  77.         /**
  78.          * @param string $name
  79.          * @return void
  80.          * @throws OIDplusException
  81.          */
  82.         public function delete(string $name) {
  83.                 self::getSessionHandler()->delete($name);
  84.         }
  85.  
  86.         /**
  87.          * @return void
  88.          * @throws OIDplusException
  89.          */
  90.         public function destroySession() {
  91.                 self::getSessionHandler()->destroySession();
  92.         }
  93.  
  94.         /**
  95.          * @return OIDplusAuthContentStoreSession|null
  96.          */
  97.         public static function getActiveProvider()/*: ?OIDplusAuthContentStore*/ {
  98.                 static $contentProvider = null;
  99.  
  100.                 $rel_url = substr($_SERVER['REQUEST_URI'], strlen(OIDplus::webpath(null, OIDplus::PATH_RELATIVE_TO_ROOT)));
  101.                 if (str_starts_with($rel_url, 'rest/')) { // <== TODO: Find a way how to move this into the plugin, since REST does not belong to the core. (Maybe some kind of "stateless mode" that is enabled by the REST plugin)
  102.                         // For REST, we must only allow JWT from Bearer and nothing else! So disable cookies if we are accessing the REST plugin
  103.                         return null;
  104.                 }
  105.  
  106.                 if (!$contentProvider) {
  107.                         if (self::getSessionHandler()->isActive()) {
  108.                                 $contentProvider = new OIDplusAuthContentStoreSession();
  109.                         }
  110.                 }
  111.  
  112.                 return $contentProvider;
  113.         }
  114.  
  115.         /**
  116.          * @param string $email
  117.          * @param string $loginfo
  118.          * @return void
  119.          */
  120.         public function raLoginEx(string $email, string &$loginfo) {
  121.                 $this->raLogin($email);
  122.                 if (is_null(self::getActiveProvider())) {
  123.                         $loginfo = 'into new PHP session';
  124.                 } else {
  125.                         $loginfo = 'into existing PHP session';
  126.                 }
  127.         }
  128.  
  129.         /**
  130.          * @param string $loginfo
  131.          * @return void
  132.          */
  133.         public function adminLoginEx(string &$loginfo) {
  134.                 $this->adminLogin();
  135.                 if (is_null(self::getActiveProvider())) {
  136.                         $loginfo = 'into new PHP session';
  137.                 } else {
  138.                         $loginfo = 'into existing PHP session';
  139.                 }
  140.         }
  141.  
  142.         /**
  143.          * @param string $email
  144.          * @param string $loginfo
  145.          * @return void
  146.          */
  147.         public function raLogoutEx(string $email, string &$loginfo) {
  148.                 $this->raLogout($email);
  149.                 $loginfo = 'from PHP session';
  150.         }
  151.  
  152.         /**
  153.          * @param string $loginfo
  154.          * @return void
  155.          */
  156.         public function adminLogoutEx(string &$loginfo) {
  157.                 $this->adminLogout();
  158.                 $loginfo = 'from PHP session';
  159.         }
  160.  
  161.         /**
  162.          * @return void
  163.          */
  164.         public function activate() {
  165.                 # Sessions automatically activate during setValue()
  166.         }
  167.  
  168. }
  169.