Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1344 | 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 OIDplusCookieUtils extends OIDplusBaseClass {
  27.  
  28.         /**
  29.          * @param string $name
  30.          * @return void
  31.          * @throws OIDplusException
  32.          */
  33.         public function unsetcookie(string $name) {
  34.                 $this->setcookie($name, '', time()-9999, true);
  35.         }
  36.  
  37.         /**
  38.          * @return string
  39.          * @throws OIDplusException
  40.          */
  41.         private function getCookieDomain(): string {
  42.                 $default_domain = ''; // ini_get('session.cookie_domain');
  43.                 $domain = OIDplus::baseConfig()->getValue('COOKIE_DOMAIN', $default_domain);
  44.                 if ($domain === '(auto)') {
  45.                         $tmp = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE/*_CANONICAL*/);
  46.                         if ($tmp === false) return $default_domain;
  47.                         $tmp = parse_url($tmp);
  48.                         if ($tmp === false) return $default_domain;
  49.                         if (!isset($tmp['host'])) return $default_domain;
  50.                         $domain = $tmp['host'];
  51.                 }
  52.                 return $domain;
  53.         }
  54.  
  55.         /**
  56.          * @return string
  57.          * @throws OIDplusException
  58.          */
  59.         private function getCookiePath(): string {
  60.                 $default_path = '/'; // ini_get('session.cookie_path');
  61.                 $path = OIDplus::baseConfig()->getValue('COOKIE_PATH', $default_path);
  62.                 if ($path === '(auto)') {
  63.                         $tmp = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE/*_CANONICAL*/);
  64.                         if ($tmp === false) return $default_path;
  65.                         $tmp = parse_url($tmp);
  66.                         if ($tmp === false) return $default_path;
  67.                         if (!isset($tmp['path'])) return $default_path;
  68.                         $path = $tmp['path'];
  69.  
  70.                         // Alternatively:
  71.                         //$path = OIDplus::webpath(null,OIDplus::PATH_RELATIVE_TO_ROOT_CANONICAL);
  72.                         //if ($path === false) return $default_path;
  73.                 }
  74.                 return $path;
  75.         }
  76.  
  77.         // TODO: There are several PHPSESSID cookies set. That's not very nice. We should collect the cookies and then at script ending only send the last definition one time.
  78.         /**
  79.          * @param string $name
  80.          * @param string $value
  81.          * @param int $expires
  82.          * @param bool $allowJS
  83.          * @param string|null $samesite
  84.          * @param bool $forceInsecure
  85.          * @return void
  86.          * @throws OIDplusException
  87.          */
  88.         public function setcookie(string $name, string $value, int $expires=0, bool $allowJS=false, string $samesite=null, bool $forceInsecure=false) {
  89.                 $domain = $this->getCookieDomain();
  90.                 $path = $this->getCookiePath();
  91.                 $secure = !$forceInsecure && OIDplus::isSSL();
  92.                 $httponly = !$allowJS;
  93.                 if (is_null($samesite)) {
  94.                         $samesite = OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY', 'Strict');
  95.                 }
  96.  
  97.                 if (strnatcmp(phpversion(),'7.3.0') >= 0) {
  98.                         $options = array(
  99.                                 "expires" => $expires,
  100.                                 "path" => $path,
  101.                                 "domain" => $domain,
  102.                                 "secure" => $secure,
  103.                                 "httponly" => $httponly,
  104.                                 "samesite" => $samesite
  105.                         );
  106.                         setcookie($name, $value, $options);
  107.                 } else {
  108.                         setcookie($name, $value, $expires, $path.'; samesite='.$samesite, $domain, $secure, $httponly);
  109.                 }
  110.         }
  111.  
  112. }
  113.