summaryrefslogtreecommitdiffstats
path: root/inc/message.inc.php
blob: f21f708084753c2ad13d1abd3de11d40a275dd31 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

// TODO: Move to extra file
global $error_text;
$error_text = array(
	'loginfail'           => 'Benutzername oder Kennwort falsch',
	'token'               => 'Ungültiges Token. CSRF Angriff?',
	'adduser-disabled'    => 'Keine ausreichenden Rechte, um weitere Benutzer hinzuzufügen',
	'password-mismatch'   => 'Passwort und Passwortbestätigung stimmen nicht überein',
	'empty-field'         => 'Ein Feld wurde nicht ausgefüllt',
	'adduser-success'     => 'Benutzer erfolgreich hinzugefügt',
	'no-permission'       => 'Keine ausreichenden Rechte, um auf diese Seite zuzugreifen',
	'settings-updated'    => 'Einstellungen wurden aktualisiert',
	'debug-mode'          => 'Der Debug-Modus ist aktiv!',
	'value-invalid'       => 'Der Wert {{1}} ist ungültig für die Option {{0}} und wurde ignoriert',
	'invalid-action'      => 'Ungültige Aktion: {{0}}',
	'remote-timeout'      => 'Konnte Ressource {{0}} nicht herunterladen ({{1}})',
	'remote-parse-failed' => 'Parsen der empfangenen Daten fehlgeschlagen ({{0}})',
	'missing-file'        => 'Es wurde keine Datei ausgewählt!',
	'invalid-file'        => 'Die Datei {{0}} existiert nicht!',
	'upload-complete'     => 'Upload von {{0}} war erfolgreich',
	'upload-failed'       => 'Upload schlug fehl: {{0}}',
	'config-activated'    => 'Konfiguration {{0}} wurde aktiviert',
	'config-invalid'      => 'Konfiguration mit ID {{0}} existiert nicht',
	'error-write'         => 'Fehler beim Schreiben von {{0}}',
	'error-read'          => 'Fehler beim Lesen von {{0}}',
	'error-archive'       => 'Korruptes Archiv oder nicht unterstütztes Format',
	'error-rename'        => 'Konnte {{0}} nicht in {{1}} umbenennen',
	'error-nodir'         => 'Das Verzeichnis {{0}} existiert nicht.',
	'empty-archive'       => 'Das Archiv enthält keine Dateien oder Verzeichnisse',
	'error-extract'       => 'Konnte Archiv nicht nach {{0}} entpacken - {{1}}',
	'module-added'        => 'Modul erfolgreich hinzugefügt',
	'module-deleted'      => 'Modul {{0}} wurde gelöscht',
	'module-in-use'       => 'Modul {{0}} wird noch durch Konfiguration {{1}} verwendet',
	'taskmanager-error'   => 'Verbindung zum Taskmanager fehlgeschlagen',
	'taskmanager-format'  => 'Taskmanager hat ungültige Daten zurückgeliefert',
	'task-error'          => 'Ausführung fehlgeschlagen: {{0}}',
	'invalid-ip'          => 'Kein Interface ist auf die Adresse {{0}} konfiguriert',
	'news-success'		  => 'News erfolgreich aktualisiert.',
	'news-empty'		  => 'Es wurde keine News in der Datenbank gefunden.',
);

class Message
{
	private static $list = array();
	private static $alreadyDisplayed = array();
	private static $flushed = false;

	/**
	 * Add error message to page. If messages have not been flushed
	 * yet, it will be added to the queue, otherwise it will be added
	 * in place during rendering.
	 */
	public static function addError($id)
	{
		self::add('error', $id, func_get_args());
	}

	public static function addWarning($id)
	{
		self::add('warning', $id, func_get_args());
	}

	public static function addInfo($id)
	{
		self::add('info', $id, func_get_args());
	}

	public static function addSuccess($id)
	{
		self::add('success', $id, func_get_args());
	}

	/**
	 * Internal function that adds a message. Used by
	 * addError/Success/Info/... above.
	 */
	private static function add($type, $id, $params)
	{
		global $error_text;
		if (!isset($error_text[$id])) Util::traceError('Invalid message id: ' . $id);
		self::$list[] = array(
			'type'   => $type,
			'id'     => $id,
			'params' => array_slice($params, 1)
		);
		if (self::$flushed) self::renderList();
	}

	/**
	 * Render all currently queued messages, flushing the queue.
	 * After calling this, any further calls to add* will be rendered in
	 * place in the current page output.
	 */
	public static function renderList()
	{
		global $error_text;
		if (!self::$flushed) Render::openTag('div', array('class' => 'container'));
		foreach (self::$list as $item) {
			$message = $error_text[$item['id']];
			foreach ($item['params'] as $index => $text) {
				$message = str_replace('{{' . $index . '}}', '<b>' . htmlspecialchars($text) . '</b>', $message);
			}
			Render::addTemplate('messagebox-' . $item['type'], array('message' => $message));
			self::$alreadyDisplayed[] = $item;
		}
		if (!self::$flushed) Render::closeTag('div');
		self::$list = array();
		self::$flushed = true;
	}

	/**
	 * Get all queued messages, flushing the queue.
	 * Useful in api/ajax mode.
	 */
	public static function asString()
	{
		global $error_text;
		$return = '';
		foreach (self::$list as $item) {
			$message = $error_text[$item['id']];
			foreach ($item['params'] as $index => $text) {
				$message = str_replace('{{' . $index . '}}', $text, $message);
			}
			$return .= '[' . $item['type'] . ']: ' . $message . "\n";
			self::$alreadyDisplayed[] = $item;
		}
		self::$list = array();
		return $return;
	}

	/**
	 * Deserialize any messages from the current HTTP request and
	 * place them in the message queue.
	 */
	public static function fromRequest()
	{
		$messages = is_array($_REQUEST['message']) ? $_REQUEST['message'] : array($_REQUEST['message']);
		foreach ($messages as $message) {
			$data = explode('|', $message);
			if (count($data) < 2 || !preg_match('/^(error|warning|info|success)$/', $data[0])) continue;
			self::add($data[0], $data[1], array_slice($data, 1));
		}
	}

	/**
	 * Turn the current message queue into a serialized version,
	 * suitable for appending to a GET or POST request
	 */
	public static function toRequest()
	{
		$parts = array();
		foreach (array_merge(self::$list, self::$alreadyDisplayed) as $item) {
			$str = 'message[]=' . urlencode($item['type'] . '|' .$item['id']);
			if (!empty($item['params'])) {
				$str .= '|' . implode('|', $item['params']);
			}
			$parts[] = $str;
		}
		return implode('&', $parts);
	}

}