[BUGFIX] Properly escape data in PermissionAjaxController
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / FlashMessageQueue.php
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
18
19 /**
20 * A class which collects and renders flash messages.
21 *
22 * @author Rupert Germann <rupi@gmx.li>
23 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
24 */
25 class FlashMessageQueue extends \SplQueue {
26
27 /**
28 * A unique identifier for this queue
29 *
30 * @var string
31 */
32 protected $identifier;
33
34 /**
35 * @param string $identifier The unique identifier for this queue
36 */
37 public function __construct($identifier) {
38 $this->identifier = $identifier;
39 }
40
41 /**
42 * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
43 * or it enqueues the message.
44 *
45 * @param FlashMessage $message Instance of \TYPO3\CMS\Core\Messaging\FlashMessage, representing a message
46 * @throws \TYPO3\CMS\Core\Exception
47 * @return void
48 */
49 public function enqueue($message) {
50 if (!($message instanceof FlashMessage)) {
51 throw new \TYPO3\CMS\Core\Exception(
52 'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . (is_object($message) ? get_class($message) : gettype($message)) . '"',
53 1376833554
54 );
55 }
56 if ($message->isSessionMessage()) {
57 $this->addFlashMessageToSession($message);
58 } else {
59 parent::enqueue($message);
60 }
61 }
62
63 /**
64 * @param FlashMessage $message
65 * @return void
66 */
67 public function addMessage(FlashMessage $message) {
68 $this->enqueue($message);
69 }
70
71 /**
72 * @return void
73 */
74 public function dequeue() {
75 // deliberately empty
76 }
77
78 /**
79 * Adds the given flash message to the array of
80 * flash messages that will be stored in the session.
81 *
82 * @param FlashMessage $message
83 * @return void
84 */
85 protected function addFlashMessageToSession(FlashMessage $message) {
86 $queuedFlashMessages = $this->getFlashMessagesFromSession();
87 $queuedFlashMessages[] = $message;
88 $this->storeFlashMessagesInSession($queuedFlashMessages);
89 }
90
91 /**
92 * Returns all messages from the current PHP session and from the current request.
93 *
94 * @return FlashMessage[]
95 */
96 public function getAllMessages() {
97 // Get messages from user session
98 $queuedFlashMessagesFromSession = $this->getFlashMessagesFromSession();
99 $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->toArray());
100 return $queuedFlashMessages;
101 }
102
103 /**
104 * Returns all messages from the current PHP session and from the current request.
105 * After fetching the messages the internal queue and the message queue in the session
106 * will be emptied.
107 *
108 * @return FlashMessage[]
109 */
110 public function getAllMessagesAndFlush() {
111 $queuedFlashMessages = $this->getAllMessages();
112 // Reset messages in user session
113 $this->removeAllFlashMessagesFromSession();
114 // Reset internal messages
115 $this->clear();
116 return $queuedFlashMessages;
117 }
118
119 /**
120 * Stores given flash messages in the session
121 *
122 * @param FlashMessage[] $flashMessages
123 * @return void
124 */
125 protected function storeFlashMessagesInSession(array $flashMessages) {
126 $this->getUserByContext()->setAndSaveSessionData($this->identifier, $flashMessages);
127 }
128
129 /**
130 * Removes all flash messages from the session
131 *
132 * @return void
133 */
134 protected function removeAllFlashMessagesFromSession() {
135 $this->getUserByContext()->setAndSaveSessionData($this->identifier, NULL);
136 }
137
138 /**
139 * Returns current flash messages from the session, making sure to always
140 * return an array.
141 *
142 * @return FlashMessage[]
143 */
144 protected function getFlashMessagesFromSession() {
145 $flashMessages = $this->getUserByContext()->getSessionData($this->identifier);
146 return is_array($flashMessages) ? $flashMessages : array();
147 }
148
149 /**
150 * Gets user object by context
151 *
152 * @return AbstractUserAuthentication
153 */
154 protected function getUserByContext() {
155 return TYPO3_MODE === 'BE' ? $GLOBALS['BE_USER'] : $GLOBALS['TSFE']->fe_user;
156 }
157
158 /**
159 * Fetches and renders all available flash messages from the queue.
160 *
161 * @return string All flash messages in the queue rendered as HTML.
162 */
163 public function renderFlashMessages() {
164 $content = '';
165 $flashMessages = $this->getAllMessagesAndFlush();
166 if (!empty($flashMessages)) {
167 foreach ($flashMessages as $flashMessage) {
168 $content .= $flashMessage->render();
169 }
170 }
171 return $content;
172 }
173
174 /**
175 * Returns all items of the queue as array
176 *
177 * @return FlashMessage[]
178 */
179 public function toArray() {
180 $array = array();
181 $this->rewind();
182 while ($this->valid()) {
183 $array[] = $this->current();
184 $this->next();
185 }
186 return $array;
187 }
188
189 /**
190 * Removes all items from the queue
191 *
192 * @return void
193 */
194 public function clear() {
195 $this->rewind();
196 while (!$this->isEmpty()) {
197 parent::dequeue();
198 }
199 }
200
201 }