Subversion Repositories oidplus

Rev

Rev 1086 | 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.                 if (!$contentProvider) {
  101.                         if (self::getSessionHandler()->isActive()) {
  102.                                 $contentProvider = new OIDplusAuthContentStoreSession();
  103.                         }
  104.                 }
  105.  
  106.                 return $contentProvider;
  107.         }
  108.  
  109.         /**
  110.          * @param string $email
  111.          * @param string $loginfo
  112.          * @return void
  113.          */
  114.         public function raLoginEx(string $email, string &$loginfo) {
  115.                 $this->raLogin($email);
  116.                 if (is_null(self::getActiveProvider())) {
  117.                         $loginfo = 'into new PHP session';
  118.                 } else {
  119.                         $loginfo = 'into existing PHP session';
  120.                 }
  121.         }
  122.  
  123.         /**
  124.          * @param string $loginfo
  125.          * @return void
  126.          */
  127.         public function adminLoginEx(string &$loginfo) {
  128.                 $this->adminLogin();
  129.                 if (is_null(self::getActiveProvider())) {
  130.                         $loginfo = 'into new PHP session';
  131.                 } else {
  132.                         $loginfo = 'into existing PHP session';
  133.                 }
  134.         }
  135.  
  136.         /**
  137.          * @param string $email
  138.          * @param string $loginfo
  139.          * @return void
  140.          */
  141.         public function raLogoutEx(string $email, string &$loginfo) {
  142.                 $this->raLogout($email);
  143.                 $loginfo = 'from PHP session';
  144.         }
  145.  
  146.         /**
  147.          * @param string $loginfo
  148.          * @return void
  149.          */
  150.         public function adminLogoutEx(string &$loginfo) {
  151.                 $this->adminLogout();
  152.                 $loginfo = 'from PHP session';
  153.         }
  154.  
  155.         /**
  156.          * @return void
  157.          */
  158.         public function activate() {
  159.                 # Sessions automatically activate during setValue()
  160.         }
  161.  
  162. }
  163.