1<?php
2/**
3 * Copyright 2014-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category  Horde
9 * @copyright 2014-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package   Core
12 */
13
14/**
15 * Manage the logout tasks registered with Horde_Registry.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2014-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
21 * @package   Core
22 * @since     2.13.0
23 */
24class Horde_Registry_Logout
25{
26    /** Session storage key. */
27    const SESSION_KEY = 'registry_logout';
28
29    /**
30     * Add a class to the logout queue.
31     *
32     * @param mixed $classname  The classname to add (or an object of that
33     *                          class). The class must be able to be
34     *                          instantiated via Horde_Injector and must
35     *                          implement the Horde_Registry_Logout_Task
36     *                          interface.
37     */
38    public function add($classname)
39    {
40        $classname = is_object($classname)
41            ? get_class($classname)
42            : strval($classname);
43
44        $queue = $this->_getTasks();
45
46        if (!in_array($classname, $queue)) {
47            $queue[] = $classname;
48            $this->_setTasks($queue);
49        }
50    }
51
52    /**
53     * Runs the list of logout tasks and clears the queue.
54     */
55    public function run()
56    {
57        global $injector;
58
59        foreach ($this->_getTasks() as $val) {
60            try {
61                $ob = $injector->getInstance($val);
62                if ($ob instanceof Horde_Registry_Logout_Task) {
63                    $ob->logoutTask();
64                }
65            } catch (Exception $e) {}
66        }
67
68        $this->_setTasks(array());
69    }
70
71    /**
72     * Return the list of logout tasks.
73     */
74    private function _getTasks()
75    {
76        global $session;
77
78        return $session->get(
79            'horde',
80            self::SESSION_KEY,
81            $session::TYPE_ARRAY
82        );
83    }
84
85    /**
86     * Set the list of logout tasks.
87     *
88     * @param array $queue  List of classnames.
89     */
90    private function _setTasks($queue)
91    {
92        $GLOBALS['session']->set(
93            'horde',
94            self::SESSION_KEY,
95            $queue
96        );
97    }
98
99}
100