Passed
Push — 2.2 ( ff3975...6cb6c9 )
by Myles
01:47
created

ScwCookie::getChoices()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ScwCookie;
4
5
class ScwCookie
6
{
7
    public $config        = [];
8
    private $decisionMade = false;
9
    private $choices      = [];
10
    
11
    public function __construct()
12
    {
13
        $this->config = parse_ini_file("config.ini", true);
14
15
        $this->decisionMade = self::getCookie('scwCookieHidden') == 'true';
16
        $this->choices      = $this->getChoices();
17
    }
18
19
    public function getChoices()
20
    {
21
        if (self::getCookie('scwCookie') !== false) {
22
            $cookie = self::getCookie('scwCookie');
23
            $cookie = self::decrypt($cookie);
24
            return $cookie;
25
        }
26
27
        $return = [];
28
        foreach ($this->enabledCookies() as $name => $label) {
29
            $return[$name] = $this->config['unsetDefault'];
30
        }
31
        return $return;
32
    }
33
34
    public static function encrypt($value)
35
    {
36
        $return = json_encode($value);
37
        return $return;
38
    }
39
40
    public static function decrypt($value)
41
    {
42
        $value = str_replace('\"', '"', $value);
43
        $return = json_decode($value, true);
44
        return $return;
45
    }
46
47
    public function isAllowed($name)
48
    {
49
        $choices = $this->getChoices();
50
        return isset($choices[$name]) && $choices[$name] == 'allowed';
51
    }
52
53
    public function isEnabled($name)
54
    {
55
        $check = $this->config[$name];
56
        return is_array($check) && isset($check['enabled']) && $check['enabled'];
57
    }
58
59
    public function getCode($name)
60
    {
61
        return isset($this->config[$name]) && isset($this->config[$name]['code'])
62
            ? $this->config[$name]['code']
63
            : false;
64
    }
65
66
    public function output()
67
    {
68
        echo $this->getOutput();
69
    }
70
71
    public function getOutput()
72
    {
73
        $return = [];
74
75
        // Get decision window output
76
        $return[] = $this->getOutputHTML('decision');
77
78
        // Get embed codes
79
        $embedCodes = [
80
            'Google_Analytics' => 'googleAnalytics',
81
            'Smartsupp'        => 'smartsupp',
82
            'Hotjar'           => 'hotjar',
83
            'Tawk.to'          => 'tawkto',
84
        ];
85
        foreach ($embedCodes as $configKey => $embedFile) {
86
            if ($this->config[$configKey]['enabled'] && $this->isAllowed($configKey)) {
87
                $return[] = $this->getOutputHTML($embedFile);
88
            }
89
        }
90
91
        return implode("\n", $return);
92
    }
93
94
    public function getOutputHTML($filename)
95
    {
96
        if (!file_exists(__DIR__.'/output/cookies/'.$filename.'.php')) {
97
            return false;
98
        }
99
        
100
        ob_start();
101
        include __DIR__.'/output/cookies/'.$filename.'.php';
102
        return trim(ob_get_clean());
103
    }
104
105
    public function enabledCookies()
106
    {
107
        $return = [];
108
        foreach ($this->config as $name => $value) {
109
            if (!$this->isEnabled($name)) {
110
                continue;
111
            }
112
            $return[$name] = $value['label'];
113
        }
114
        return $return;
115
    }
116
117
    public static function setCookie($name, $value, $lifetime = 30, $lifetimePeriod = 'days', $domain = '/', $secure = false)
118
    {
119
        // Validate parameters
120
        self::validateSetCookieParams($name, $value, $lifetime, $lifetimePeriod, $domain, $secure);
121
122
        // Calculate expiry
123
        $expiry = strtotime('+'.$lifetime.' '.$lifetimePeriod);
124
125
        // Set cookie
126
        return setcookie($name, $value, $expiry, $domain, $secure);
127
    }
128
129
    public static function validateSetCookieParams($name, $value, $lifetime, $lifetimePeriod, $domain, $secure)
130
    {
131
        // Set allowed time periods
132
        $lifetimePeriods = array('minutes', 'hours', 'days', 'weeks');
133
134
        // Check values passed
135
        $validParams = is_string($name);
136
        $validParams = is_string($value) ? $validParams : false;
137
        $validParams = is_int($lifetime) ? $validParams : false;
138
        $validParams = in_array($lifetimePeriod, $lifetimePeriods) ? $validParams : false;
139
        $validParams = is_string($domain) ? $validParams : false;
140
        $validParams = is_bool($secure) ? $validParams : false;
141
142
        if (!$validParams) {
143
            // Failed parameter check
144
            header('HTTP/1.0 403 Forbidden');
145
            throw new \Exception("Incorrect parameter passed to Cookie::set");
146
        }
147
148
        return true;
149
    }
150
151
    public static function getCookie($name)
152
    {
153
        // If cookie exists - return it, otherwise return false
154
        return isset($_COOKIE[$name]) ? $_COOKIE[$name] : false;
155
    }
156
157
    public static function destroyCookie($name)
158
    {
159
        // Set cookie expiration to 1 hour ago
160
        return setcookie($name, '', time() - 3600);
161
    }
162
}
163