Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1340 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
557 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
557 daniel-mar 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
557 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
730 daniel-mar 26
class OIDplusCookieUtils extends OIDplusBaseClass {
557 daniel-mar 27
 
1116 daniel-mar 28
        /**
29
         * @param string $name
30
         * @return void
31
         * @throws OIDplusException
32
         */
33
        public function unsetcookie(string $name) {
557 daniel-mar 34
                $this->setcookie($name, '', time()-9999, true);
35
        }
36
 
1116 daniel-mar 37
        /**
38
         * @return string
39
         * @throws OIDplusException
40
         */
41
        private function getCookieDomain(): string {
812 daniel-mar 42
                $default_domain = ''; // ini_get('session.cookie_domain');
43
                $domain = OIDplus::baseConfig()->getValue('COOKIE_DOMAIN', $default_domain);
44
                if ($domain === '(auto)') {
816 daniel-mar 45
                        $tmp = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE/*_CANONICAL*/);
812 daniel-mar 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
        }
806 daniel-mar 54
 
1116 daniel-mar 55
        /**
56
         * @return string
57
         * @throws OIDplusException
58
         */
59
        private function getCookiePath(): string {
812 daniel-mar 60
                $default_path = '/'; // ini_get('session.cookie_path');
61
                $path = OIDplus::baseConfig()->getValue('COOKIE_PATH', $default_path);
62
                if ($path === '(auto)') {
816 daniel-mar 63
                        $tmp = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE/*_CANONICAL*/);
812 daniel-mar 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'];
808 daniel-mar 69
 
812 daniel-mar 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
        }
557 daniel-mar 76
 
847 daniel-mar 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.
1116 daniel-mar 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
         */
1130 daniel-mar 88
        public function setcookie(string $name, string $value, int $expires=0, bool $allowJS=false, string $samesite=null, bool $forceInsecure=false) {
812 daniel-mar 89
                $domain = $this->getCookieDomain();
90
                $path = $this->getCookiePath();
1130 daniel-mar 91
                $secure = !$forceInsecure && OIDplus::isSSL();
557 daniel-mar 92
                $httponly = !$allowJS;
564 daniel-mar 93
                if (is_null($samesite)) {
94
                        $samesite = OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY', 'Strict');
95
                }
557 daniel-mar 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
}