Completed
Push — master ( e9ef07...05c89c )
by Sergey
02:57 queued 23s
created

Cookies::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 3
eloc 2
nc 3
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
class Cookies
6
{
7
    const TOKEN_NAME = 'csrftoken';
8
    const DEFAULT_TOKEN = '1234';
9
10
    /**
11
     * @var array
12
     */
13
    protected $cookies = [];
14
15
    /**
16
     * @param $name
17
     * @return mixed
18
     */
19
    public function get($name)
20
    {
21
        if(array_key_exists($name, $this->cookies)) {
22
            return $this->cookies[$name]['value'];
23
       }
24
25
        return null;
26
    }
27
28
    /**
29
     * @return string|null
30
     */
31
    public function getToken()
32
    {
33
        return $this->get('csrftoken');
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function all()
40
    {
41
        return $this->cookies;
42
    }
43
44
    /**
45
     * @param string $file
46
     */
47
    public function fill($file)
48
    {
49
        $this->cookies = [];
50
51
        foreach (file($file) as $line) {
52
            if($cookie = $this->parseCookie($line)) {
53
                $this->cookies[$cookie['name']] = $cookie;
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param $line
60
     * @return bool
61
     */
62
    protected function isHttp($line)
63
    {
64
        return substr($line, 0, 10) == '#HttpOnly_';
65
    }
66
67
    /**
68
     * @param $line
69
     * @return bool
70
     */
71
    protected function isValid($line)
72
    {
73
        return strlen($line) > 0 && $line[0] != '#' && substr_count($line, "\t") == 6;
74
    }
75
76
    /**
77
     * @param $line
78
     * @return array|bool
79
     */
80
    protected function parseCookie($line)
81
    {
82
        $cookie = [];
83
84
        // detect httponly cookies and remove #HttpOnly prefix
85
        $cookie['httponly'] = $this->isHttp($line);
86
87
        if ($cookie['httponly']) {
88
            $line = substr($line, 10);
89
        }
90
91
        if (!$this->isValid($line)) return false;
92
93
        // get tokens in an array
94
        $tokens = explode("\t", $line);
95
        // trim the tokens
96
        $tokens = array_map('trim', $tokens);
97
98
        // Extract the data
99
        $cookie['domain'] = $tokens[0]; // The domain that created AND can read the variable.
100
        $cookie['flag'] = $tokens[1];   // A TRUE/FALSE value indicating if all machines within a given domain can access the variable.
101
        $cookie['path'] = $tokens[2];   // The path within the domain that the variable is valid for.
102
        $cookie['secure'] = $tokens[3]; // A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
103
104
        $cookie['expiration-epoch'] = $tokens[4];  // The UNIX time that the variable will expire on.
105
        $cookie['name'] = urldecode($tokens[5]);   // The name of the variable.
106
        $cookie['value'] = urldecode($tokens[6]);  // The value of the variable.
107
108
        // Convert date to a readable format
109
        $cookie['expiration'] = date('Y-m-d h:i:s', $tokens[4]);
110
        return $cookie;
111
    }
112
}
113