summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/ApplicationStateCookie.php
blob: e40c17bb5f39b411b0cb1c8647fa2c28f6602e31 (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
<?php
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */

namespace Icinga\Web;

use Icinga\Application\Logger;
use Icinga\Authentication\Auth;
use Icinga\Exception\Json\JsonDecodeException;
use Icinga\Util\Json;

/**
 * Handle acknowledged application state messages via cookie
 */
class ApplicationStateCookie extends Cookie
{
    /** @var array */
    protected $acknowledgedMessages = [];

    public function __construct()
    {
        parent::__construct('icingaweb2-application-state');

        $this->setExpire(2147483648);

        if (isset($_COOKIE['icingaweb2-application-state'])) {
            try {
                $cookie = Json::decode($_COOKIE['icingaweb2-application-state'], true);
            } catch (JsonDecodeException $e) {
                Logger::error(
                    "Can't decode the application state cookie of user '%s'. An error occurred: %s",
                    Auth::getInstance()->getUser()->getUsername(),
                    $e
                );

                return;
            }

            if (isset($cookie['acknowledged-messages'])) {
                $this->setAcknowledgedMessages($cookie['acknowledged-messages']);
            }
        }
    }

    /**
     * Get the acknowledged messages
     *
     * @return  array
     */
    public function getAcknowledgedMessages()
    {
        return $this->acknowledgedMessages;
    }

    /**
     * Set the acknowledged messages
     *
     * @param   array $acknowledged
     *
     * @return  $this
     */
    public function setAcknowledgedMessages(array $acknowledged)
    {
        $this->acknowledgedMessages = $acknowledged;

        return $this;
    }

    public function getValue()
    {
        return Json::encode([
            'acknowledged-messages' => $this->getAcknowledgedMessages()
        ]);
    }
}