MailPanel.php 6.01 KB
Newer Older
Roman Mátyus's avatar
Roman Mátyus committed
1
2
3
<?php
namespace RM;

Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
4
use Nette\DateTime,
Roman Mátyus's avatar
Roman Mátyus committed
5
	Nette\Application\Application,
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
6
7
	Nette\Application\UI\Control,
	Nette\Diagnostics\IBarPanel,
Roman Mátyus's avatar
Roman Mátyus committed
8
	Nette\Caching\Cache,
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
9
10
11
12
	Nette\Caching\IStorage,
	Nette\Http\Response,
	Nette\Http\Request,
	Nette\Utils\Finder;
Roman Mátyus's avatar
Roman Mátyus committed
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

/**
 * Bar panel showing stored mails.
 *
 * Configuring Simple:
 * 	nette:
 * 		debugger:
 * 			bar:
 *    			- RM\MailPanel
 * 	services:
 * 		nette.mailer:
 * 			class: RM\FileMailer
 * 			setup:
 * 				- $tempDir(%appDir%/../temp/mails)
 * 
 * Configuring Advanced:
 * 	nette:
 * 		debugger:
 * 			bar:
 *    			- @mailPanel
 * 	services:
 * 		nette.mailer:
 * 			class: RM\FileMailer
 * 			setup:
 * 				- $tempDir(%appDir%/../temp/mails)
 * 		mailPanel:
 * 			class: RM\MailPanel
 * 			setup:
 * 				- $newMessageTime(-1 minute)
 * 				- $autoremove(-2 minutes)
 * 				- $show( [from,to] )
 * 				- $hideEmpty(TRUE)
 * 
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
46
47
 * @author Jan Drábek, Roman Mátyus
 * @copyright (c) Jan Drábek 2013
Roman Mátyus's avatar
Roman Mátyus committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 * @copyright (c) Roman Mátyus 2013
 * @license MIT
 * @package FileMailer
 */
class MailPanel extends Control implements IBarPanel {

	/** @var Request */
	private $request;

	/** @var Application */
	private $application;

	/** @var FileMailer */
	private $fileMailer;

	/** @var Cache */
	private $cache;

Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
66
67
68
	/** @var Response */
	private $response;

Roman Mátyus's avatar
Roman Mátyus committed
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
	/** @var integer */
	private $countAll = 0;

	/** @var integer */
	private $countNew = 0;

	/** @var array */
	private $messages = array();

	/** @var bool */
	private $processed = FALSE;

	/** @var string */
	public $newMessageTime = "-10 seconds";

	/** @var array */
	public $show = array(
					"subject",
					"from",
					"to",
				);

	/** @var mixed */
	public $autoremove = FALSE;

	/** @var bool */
	public $hideEmpty = TRUE;

Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
97
	public function __construct(Application $application, Request $request, FileMailer $fileMailer, IStorage $cacheStorage, Response $response)
Roman Mátyus's avatar
Roman Mátyus committed
98
99
100
101
102
	{
		$this->application = $application;
		$this->request = $request;
		$this->fileMailer = $fileMailer;
		$this->cache = new Cache($cacheStorage, "MailPanel");
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
103
		$this->response = $response;
Roman Mátyus's avatar
Roman Mátyus committed
104
105
106
107
		switch($request->getQuery("mail-panel")) {
			case 'delete-all':
				$this->handleDeleteAll();
				break;
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
108
109
110
			case 'download':
				$this->handleDownload($request->getQuery("mail-panel-mail"),$request->getQuery("mail-panel-file"));
				break;
Roman Mátyus's avatar
Roman Mátyus committed
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
			default:
				break;
		}
	}

	/**
	 * Renders HTML code for custom tab.
	 * @return mixed
	 */
	public function getTab() {
		$this->processMessage();
		if ($this->countAll===0&&$this->hideEmpty)
			return;
		$count = ($this->countNew)?'<span id="mailpanel-count"><span>'.$this->countNew.'</span></span>&nbsp;&nbsp;':NULL;
		return '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAITSURBVBgZpcHLThNhGIDh9/vn7/RApwc5VCmFWBPi1mvwAlx7BW69Afeu3bozcSE7E02ILjCRhRrds8AEbKVS2gIdSjvTmf+TYqLu+zyiqszDMCf75PnnnVwhuNcLpwsXk8Q4BYeSOsWpkqrinJI6JXVK6lSRdDq9PO+19vb37XK13Hj0YLMUTVVyWY//Cf8IVwQEGEeJN47S1YdPo4npDpNmnDh5udOh1YsZRcph39EaONpnjs65oxsqvZEyTaHdj3n2psPpKDLBcuOOGUWpZDOG+q0S7751ObuYUisJGQ98T/Ct4Fuo5IX+MGZr95jKjRKLlSxXxFxOEmaaN4us1Upsf+1yGk5ZKhp8C74H5ZwwCGO2drssLZZo1ouIcs2MJikz1oPmapHlaoFXH1oMwphyTghyQj+MefG+RblcoLlaJG/5y4zGCTMikEwTctaxXq/w9kuXdm9Cuzfh9acujXqFwE8xmuBb/hCwl1GKAnGccDwIadQCfD9DZ5Dj494QA2w2qtQW84wmMZ1eyFI1QBVQwV5GiaZOpdsPaSwH5HMZULi9UmB9pYAAouBQbMHHrgQcnQwZV/KgTu1o8PMgipONu2t5KeaNiEkxgAiICDMCCFeEK5aNauAOfoXx8KR9ZOOLk8P7j7er2WBhwWY9sdbDeIJnwBjBWBBAhGsCmiZxPD4/7Z98b/0QVWUehjkZ5vQb/Un5e/DIsVsAAAAASUVORK5CYII=" id="mailpanel-icon"/>'.$count;
	}

	/**
	 * Show content of panel.
	 * @return mixed
	 */
	public function getPanel() {
		if ($this->countAll===0&&$this->hideEmpty)
			return;
		$this->processMessage();
		ob_start();
		$template = clone $this->application->getPresenter()->template;
		$template->setFile(__DIR__.'/MailPanel.latte');
		$template->messages = $this->messages;
		$template->countNew = $this->countNew;
		$template->countAll = $this->countAll;
		$template->show = $this->show;
		$template->render();
		return ob_get_clean();
	}

	/**
	 * Process all messages.
	 */
	private function processMessage()
	{
		if ($this->processed)
			return;
		$this->processed = TRUE;
		$this->autoremove();

		foreach (Finder::findFiles('*')->in($this->fileMailer->tempDir) as $file) {
			$message = $this->cache->load($file->getFilename());
			if ($message === NULL) {
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
160
				$message = FileMailer::mailParser(file_get_contents($file),$file->getFilename());
Roman Mátyus's avatar
Roman Mátyus committed
161
162
163
164
165
166
167
168
				$this->cache->save($file->getFilename(),$message);
			}
			$time = new DateTime;
			if ($message->date>$time->modify($this->newMessageTime))
				$this->countNew++;
			$this->countAll++;
			$this->messages[] = $message;
		}
Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
169
170
171
172
		usort($this->messages, function($a1, $a2) {
			return $a2->date->getTimestamp() - $a1->date->getTimestamp();
		});

Roman Mátyus's avatar
Roman Mátyus committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	}

	/**
	 * Autoremove mails from filesystem an cache by argument 'autoremove'.
	 */
	private function autoremove()
	{
		if ($this->autoremove) {
			foreach (Finder::findFiles('*')->in($this->fileMailer->tempDir) as $file) {
				$now = new DateTime;
				$file_date = new DateTime("@".filemtime($file));
				$file_date->setTimezone($now->getTimezone());
				$remove_date = $now->modify($this->autoremove);
				if ($file_date<$remove_date) {
					$this->cache->remove($file->getFilename());
					unlink($file);
				}
			}
		}
	}

Roman Mátyus's avatar
v0.2    
Roman Mátyus committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	/**
	 * Delete all stored mails from filesystem an cache.
	 */
	public function handleDeleteAll()
	{
		foreach (Finder::findFiles('*')->in($this->fileMailer->tempDir) as $file)
			unlink($file);
		$this->cache->clean(
			array(
				Cache::ALL => TRUE,
			));
		header("Location: ".$this->request->getReferer());
		exit;
	}

	/**
	 * Download attachment from file.
	 */
	public function handleDownload($filename, $filehash)
	{
		$message = $this->cache->load($filename);
		$file = $message->attachments[$filehash];
		$this->response->setContentType($file->type, 'UTF-8');
		$this->response->setHeader('Content-Disposition', 'attachment; filename="' . $file->filename . '"');
		$this->response->setHeader('Content-Length', strlen($file->data));
		print base64_decode($file->data);
		exit;
	}
222
}