aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/cookies.php
blob: ec57d05acc0b406fd029307cf864d75975280126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php namespace HashOver;

// Copyright (C) 2010-2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class Cookies
{
	public $setup;
	public $secure = false;

	public function __construct (Setup $setup)
	{
		$this->setup = $setup;
		$this->domain = $setup->domain;

		// Remove port from domain
		if (mb_strpos ($this->domain, ':') !== false) {
			$this->domain = mb_substr ($this->domain, 0, strrpos ($this->domain, ':'));
		}

		// Transmit cookies over HTTPS if set so in Settings
		if ($setup->secureCookies === true) {
			$this->secure = !empty ($_SERVER['HTTPS']) ? true : false;
		}
	}

	// Set a cookie with expiration date
	public function set ($name, $value = '', $date = '')
	{
		$name = 'hashover-' . $name;

		// Use specific expiration date or the one in Settings
		$date = !empty ($date) ? $date : $this->setup->cookieExpiration;

		// Set the cookie if cookies are enabled
		if ($this->setup->setsCookies !== false) {
			setcookie ($name, $value, $date, '/', $this->domain, $this->secure, true);
		}
	}

	// Set cookies for remembering state login actions
	public function setFailedOn ($input, $reply_to, $replied = true)
	{
		// Set success status cookie
		$this->set ('failed-on', $input);

		// Set reply cookie
		if ($replied === true and !empty ($reply_to)) {
			$this->set ('replied', $reply_to);
		}

		// Set comment text cookie
		$comment = $this->setup->getRequest ('comment');

		// Check if comment is set
		if ($comment !== false) {
			$this->set ('comment', $comment);
		}
	}

	// Get cookie value
	public function getValue ($name, $trim = false)
	{
		$name = 'hashover-' . $name;

		// Check if it exists
		if (!empty ($_COOKIE[$name])) {
			$value = $_COOKIE[$name];

			// Strip escaping backslashes from cookie value
			if (get_magic_quotes_gpc ()) {
				$value = stripslashes ($value);
			}

			// Return trimmed value if told to
			if ($trim === true) {
				$value = trim ($value, " \r\n\t");
			}

			return $value;
		}

		// If not set return null
		return null;
	}

	// Expire a cookie
	public function expireCookie ($cookie)
	{
		// Set its expiration date to 1 if it exists
		if ($this->getValue ($cookie) !== null) {
			$this->set ($cookie, '', 1);
		}
	}

	// Expire HashOver's default cookies
	public function clear ()
	{
		// Expire message cookie
		$this->expireCookie ('message');

		// Expire error cookie
		$this->expireCookie ('error');

		// Expire comment failure cookie
		$this->expireCookie ('failed-on');

		// Expire reply failure cookie
		$this->expireCookie ('replied');

		// Expire comment text cookie
		$this->expireCookie ('comment');
	}
}