Completed
Pull Request — master (#1559)
by Christoph
09:17
created

PageController::index()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 8.9197
cc 4
eloc 13
nc 2
nop 0
crap 4
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 * @author Timo Witte <[email protected]>
8
 *
9
 * ownCloud - Mail
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Mail\Controller;
26
27
use OCA\Mail\Db\MailAccountMapper;
28
use OCA\Mail\Service\DefaultAccount\DefaultAccountManager;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\ContentSecurityPolicy;
31
use OCP\AppFramework\Http\RedirectResponse;
32
use OCP\AppFramework\Http\TemplateResponse;
33
use OCP\IConfig;
34
use OCP\IRequest;
35
use OCP\IURLGenerator;
36
37
class PageController extends Controller {
38
39
	/**
40
	 * @var DefaultAccountManager
41
	 */
42
	private $defaultAccountManager;
43
44
	/**
45
	 * @var MailAccountMapper
46
	 */
47
	private $mailAccountMapper;
48
49
	/**
50
	 * @var IURLGenerator
51
	 */
52
	private $urlGenerator;
53
54
	/**
55
	 * @var IConfig
56
	 */
57
	private $config;
58
59
	/**
60
	 * @var string
61
	 */
62
	private $currentUserId;
63
64
	/**
65
	 * @param string $appName
66
	 * @param IRequest $request
67
	 * @param $mailAccountMapper
68
	 * @param IConfig $config
69
	 * @param $UserId
70
	 */
71 6
	public function __construct($appName, IRequest $request,
72
		MailAccountMapper $mailAccountMapper, IURLGenerator $urlGenerator,
73
		IConfig $config, DefaultAccountManager $defaultAccountManager, $UserId) {
74 6
		parent::__construct($appName, $request);
75 6
		$this->mailAccountMapper = $mailAccountMapper;
76 6
		$this->urlGenerator = $urlGenerator;
77 6
		$this->config = $config;
78 6
		$this->defaultAccountManager = $defaultAccountManager;
79 6
		$this->currentUserId = $UserId;
80 6
	}
81
82
	/**
83
	 * @NoAdminRequired
84
	 * @NoCSRFRequired
85
	 *
86
	 * @return TemplateResponse renders the index page
87
	 */
88 1
	public function index() {
89 1
		$this->defaultAccountManager->createOrUpdateDefaultAccount();
90
91 1
		$coreVersion = $this->config->getSystemValue('version', '0.0.0');
92 1
		$hasDavSupport = (int) version_compare($coreVersion, '9.0.0', '>=');
93
		// TODO: remove DEBUG constant check once minimum oc
94
		// core version >= 8.2, see https://github.com/owncloud/core/pull/18510
95 1
		$response = new TemplateResponse($this->appName, 'index', [
96 1
			'debug' => (defined('DEBUG') && DEBUG) || $this->config->getSystemValue('debug', false),
97 1
			'app-version' => $this->config->getAppValue('mail', 'installed_version'),
98 1
			'has-dav-support' => $hasDavSupport,
99 1
		]);
100
101
		// set csp rules for ownCloud 8.1
102 1
		if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) {
103 1
			$csp = new ContentSecurityPolicy();
104 1
			$csp->addAllowedFrameDomain('\'self\'');
105 1
			$response->setContentSecurityPolicy($csp);
106 1
		}
107
108 1
		return $response;
109
	}
110
111
	/**
112
	 * @NoAdminRequired
113
	 * @NoCSRFRequired
114
	 *
115
	 * @param string $uri
116
	 * @return TemplateResponse renders the compose page
117
	 */
118 5
	public function compose($uri) {
119 5
		$parts = parse_url($uri);
120 5
		$params = ['to' => $parts['path']];
121 5
		if (isset($parts['query'])) {
122 4
			$parts = explode('&', $parts['query']);
123 4
			foreach ($parts as $part) {
124 4
				$pair = explode('=', $part, 2);
125 4
				$params[strtolower($pair[0])] = urldecode($pair[1]);
126 4
			}
127 4
		}
128
129 5
		array_walk($params, function (&$value, $key) {
130 5
			$value = "$key=" . urlencode($value);
131 5
		});
132
133 5
		$hashParams = '#mailto?' . implode('&', $params);
134
135 5
		$baseUrl = $this->urlGenerator->linkToRoute("mail.page.index");
136 5
		return new RedirectResponse($baseUrl . $hashParams);
137
	}
138
139
}
140