Completed
Pull Request — master (#567)
by Michael
06:13
created

Cookie::exists()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
/**
3
 * Cookie class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   XOOPS Project (http://xoops.org)
13
 * @license     GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @author      Andricq Nicolas (AKA MusS)
15
 * @package     system
16
 */
17
18
class Cookie
19
{
20
    // Reserved session keys
21
    private static $_reserved = array('XOLOGGERVIEW', 'xoops_user');
22
23
    // Static class cannot be initialized
24
    private function __construct()
25
    {
26
    }
27
28
    // Alias for delete() function
29
    public static function del($key)
30
    {
31
        self::delete($key);
32
    }
33
34
    // Delete a cookie
35
    public static function delete($key)
36
    {
37
        // Change string representation array to key/value array
38
        $key = self::_scrubKey($key);
39
40
        // Make sure the cookie exists
41
        if (self::exists($key)) {
42
            // Check for key array
43
            if (is_array($key)) {
44
                // Grab key/value pair
45
                list($k, $v) = each($key);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

45
                list($k, $v) = /** @scrutinizer ignore-deprecated */ each($key);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
46
47
                // Set string representation
48
                $key = $k . '[' . $v . ']';
49
50
                // Set expiration time to -1hr (will cause browser deletion)
51
                setcookie($key, false, time() - 3600);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $value of setcookie(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
                setcookie($key, /** @scrutinizer ignore-type */ false, time() - 3600);
Loading history...
52
53
                // Unset the cookie
54
                unset($_COOKIE[$k][$v]);
55
            }
56
57
            // Check for cookie array
58
            elseif (is_array($_COOKIE[$key])) {
59
                foreach ($_COOKIE[$key] as $k => $v) {
60
                    // Set string representation
61
                    $cookie = $key . '[' . $k . ']';
62
63
                    // Set expiration time to -1hr (will cause browser deletion)
64
                    setcookie($cookie, false, time() - 3600);
65
66
                    // Unset the cookie
67
                    unset($_COOKIE[$key][$k]);
68
                }
69
            }
70
71
            // Unset single cookie
72
            else {
73
                // Set expiration time to -1hr (will cause browser deletion)
74
                setcookie($key, false, time() - 3600);
75
76
                // Unset key
77
                unset($_COOKIE[$key]);
78
            }
79
        }
80
    }
81
82
    // See if a cookie key exists
83
    public static function exists($key)
84
    {
85
        // Change string representation array to key/value array
86
        $key = self::_scrubKey($key);
87
88
        // Check for array
89
        if (is_array($key)) {
90
            // Grab key/value pair
91
            list($k, $v) = each($key);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

91
            list($k, $v) = /** @scrutinizer ignore-deprecated */ each($key);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
92
93
            // Check for key/value pair and return
94
            if (isset($_COOKIE[$k][$v])) {
95
                return true;
96
            }
97
        }
98
99
        // If key exists, return true
100
        elseif (isset($_COOKIE[$key])) {
101
            return true;
102
        }
103
104
        // Key does not exist
105
        return false;
106
    }
107
108
    // Get cookie information
109
    public static function get($key)
110
    {
111
        // Change string representation array to key/value array
112
        $key = self::_scrubKey($key);
113
114
        // Check for array
115
        if (is_array($key)) {
116
            // Grab key/value pair
117
            list($k, $v) = each($key);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

117
            list($k, $v) = /** @scrutinizer ignore-deprecated */ each($key);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
118
119
            // Check for key/value pair and return
120
            if (isset($_COOKIE[$k][$v])) {
121
                return $_COOKIE[$k][$v];
122
            }
123
        }
124
125
        // Return single key if it's set
126
        elseif (isset($_COOKIE[$key])) {
127
            return $_COOKIE[$key];
128
        }
129
130
        // Otherwise return null
131
        else {
132
            return null;
133
        }
134
    }
135
136
    // Return the cookie array
137
    public static function contents()
138
    {
139
        return $_COOKIE;
140
    }
141
142
    // Set cookie information
143
    public static function set(
144
        $key,
145
        $value,
146
        $expire = 0,            /* Default expire time (session, 1 week = 604800) */
147
        $path = '',             /* Default path */
148
        $domain = '',           /* Default domain */
149
        $secure = false,        /* Does this cookie need a secure HTTPS connection? */
150
        $httponly = true        /* Can non-HTTP services access this cookie (IE: javascript)? */
151
    ) {
152
        // Make sure they aren't trying to set a reserved word
153
        if (!in_array($key, self::$_reserved)) {
154
            // If $key is in array format, change it to string representation
155
            $key = self::_scrubKey($key, true);
156
157
            // Store the cookie
158
            setcookie($key, $value, $expire, $path, $domain, $secure, $httponly);
159
        }
160
161
        // Otherwise, throw an error
162
        else {
163
            Error::warning('Could not set key -- it is reserved.', __CLASS__);
0 ignored issues
show
Bug introduced by
The method warning() does not exist on Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
            Error::/** @scrutinizer ignore-call */ 
164
                   warning('Could not set key -- it is reserved.', __CLASS__);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
        }
165
    }
166
167
    // Converts strings to arrays (or vice versa if toString = true)
168
    private static function _scrubKey($key, $toString = false)
169
    {
170
        // Converting from array to string
171
        if ($toString) {
172
            // If $key is in array format, change it to string representation
173
            if (is_array($key)) {
174
                // Grab key/value pair
175
                list($k, $v) = each($key);
0 ignored issues
show
Deprecated Code introduced by
The function each() has been deprecated: 7.2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

175
                list($k, $v) = /** @scrutinizer ignore-deprecated */ each($key);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
176
177
                // Set string representation
178
                $key = $k . '[' . $v . ']';
179
            }
180
        }
181
182
        // Converting from string to array
183
        elseif (!is_array($key)) {
184
            // is this a string representation of an array?
185
            if (preg_match('/([\w\d]+)\[([\w\d]+)\]$/i', $key, $matches)) {
186
                // Store as key/value pair
187
                $key = array($matches[1] => $matches[2]);
188
            }
189
        }
190
191
        // Return key
192
        return $key;
193
    }
194
}
195