Issues (14)

scwCookie/ajax.php (1 issue)

Labels
Severity
1
<?php
2
require_once('scwCookie.class.php');
3
4
if (!isset($_POST['action'])) {
5
    header('HTTP/1.0 403 Forbidden');
6
    throw new Exception("Action not specified");
7
}
8
9
switch ($_POST['action']) {
10
    case 'hide':
11
        // Set cookie
12
        ScwCookie\ScwCookie::setCookie('scwCookieHidden', 'true', 52, 'weeks');
13
        header('Content-Type: application/json');
14
        die(json_encode(['success' => true]));
15
        break;
16
17
    case 'toggle':
18
        $scwCookie = new ScwCookie\ScwCookie();
19
        $return    = [];
20
21
        // Update if cookie allowed or not
22
        $choices = $scwCookie->getCookie('scwCookie');
23
        if ($choices == false) {
24
            $choices = [];
25
            $enabledCookies = $scwCookie->enabledCookies();
26
            foreach ($enabledCookies as $name => $label) {
27
                $choices[$name] = $scwCookie->config['unsetDefault'];
28
            }
29
            $scwCookie->setCookie('scwCookie', $scwCookie->encrypt($choices), 52, 'weeks');
30
        } else {
31
            $choices = $scwCookie->decrypt($choices);
32
        }
33
        $choices[$_POST['name']] = $_POST['value'] == 'true' ? 'allowed' : 'blocked';
34
35
        // Remove cookies if now disabled
36
        if ($choices[$_POST['name']] == 'blocked') {
37
            $removeCookies = $scwCookie->clearCookieGroup($_POST['name']);
38
            $return['removeCookies'] = $removeCookies;
39
        }
40
41
        $choices = $scwCookie->encrypt($choices);
42
        $scwCookie->setCookie('scwCookie', $choices, 52, 'weeks');
43
44
        header('Content-Type: application/json');
45
        die(json_encode($return));
46
        break;
47
48
    case 'load':
49
        $scwCookie = new ScwCookie\ScwCookie();
50
        $return    = [];
51
52
        $removeCookies = [];
53
54
        foreach ($scwCookie->disabledCookies() as $cookie => $label) {
55
            $removeCookies = array_merge($removeCookies, $scwCookie->clearCookieGroup($cookie));
0 ignored issues
show
It seems like $scwCookie->clearCookieGroup($cookie) can also be of type false; however, parameter $array2 of array_merge() does only seem to accept null|array, maybe add an additional type check? ( Ignorable by Annotation )

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

55
            $removeCookies = array_merge($removeCookies, /** @scrutinizer ignore-type */ $scwCookie->clearCookieGroup($cookie));
Loading history...
56
        }
57
        $return['removeCookies'] = $removeCookies;
58
59
        header('Content-Type: application/json');
60
        die(json_encode($return));
61
        break;
62
63
    default:
64
        header('HTTP/1.0 403 Forbidden');
65
        throw new Exception("Action not recognised");
66
        break;
67
}
68