Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | 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.         public function unsetcookie($name) {
  29.                 $this->setcookie($name, '', time()-9999, true);
  30.         }
  31.  
  32.         private function getCookieDomain() {
  33.                 $default_domain = ''; // ini_get('session.cookie_domain');
  34.                 $domain = OIDplus::baseConfig()->getValue('COOKIE_DOMAIN', $default_domain);
  35.                 if ($domain === '(auto)') {
  36.                         $tmp = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE/*_CANONICAL*/);
  37.                         if ($tmp === false) return $default_domain;
  38.                         $tmp = parse_url($tmp);
  39.                         if ($tmp === false) return $default_domain;
  40.                         if (!isset($tmp['host'])) return $default_domain;
  41.                         $domain = $tmp['host'];
  42.                 }
  43.                 return $domain;
  44.         }
  45.  
  46.         private function getCookiePath() {
  47.                 $default_path = '/'; // ini_get('session.cookie_path');
  48.                 $path = OIDplus::baseConfig()->getValue('COOKIE_PATH', $default_path);
  49.                 if ($path === '(auto)') {
  50.                         $tmp = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE/*_CANONICAL*/);
  51.                         if ($tmp === false) return $default_path;
  52.                         $tmp = parse_url($tmp);
  53.                         if ($tmp === false) return $default_path;
  54.                         if (!isset($tmp['path'])) return $default_path;
  55.                         $path = $tmp['path'];
  56.  
  57.                         // Alternatively:
  58.                         //$path = OIDplus::webpath(null,OIDplus::PATH_RELATIVE_TO_ROOT_CANONICAL);
  59.                         //if ($path === false) return $default_path;
  60.                 }
  61.                 return $path;
  62.         }
  63.  
  64.         // 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.
  65.         public function setcookie($name, $value, $expires=0, $allowJS=false, $samesite=null, $forceInsecure=false) {
  66.                 $domain = $this->getCookieDomain();
  67.                 $path = $this->getCookiePath();
  68.                 $secure = $forceInsecure ? false : OIDplus::isSSL();
  69.                 $httponly = !$allowJS;
  70.                 if (is_null($samesite)) {
  71.                         $samesite = OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY', 'Strict');
  72.                 }
  73.  
  74.                 if (strnatcmp(phpversion(),'7.3.0') >= 0) {
  75.                         $options = array(
  76.                                 "expires" => $expires,
  77.                                 "path" => $path,
  78.                                 "domain" => $domain,
  79.                                 "secure" => $secure,
  80.                                 "httponly" => $httponly,
  81.                                 "samesite" => $samesite
  82.                         );
  83.                         setcookie($name, $value, $options);
  84.                 } else {
  85.                         setcookie($name, $value, $expires, $path.'; samesite='.$samesite, $domain, $secure, $httponly);
  86.                 }
  87.         }
  88.  
  89. }
  90.