Completed
Push — master ( 446472...7ef246 )
by Lukas
02:50
created

DocumentController::publicPage()   B

Complexity

Conditions 4
Paths 21

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 24
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 25
nc 21
nop 2
crap 20
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2014 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments\Controller;
13
14
use OCA\Richdocuments\TokenManager;
15
use OCA\Richdocuments\WOPI\Parser;
16
use \OCP\AppFramework\Controller;
17
use OCP\AppFramework\Http\JSONResponse;
18
use OCP\Files\Folder;
19
use OCP\Files\IRootFolder;
20
use OCP\Files\Node;
21
use \OCP\IRequest;
22
use \OCP\IConfig;
23
use \OCP\IL10N;
24
use \OCP\AppFramework\Http\ContentSecurityPolicy;
25
use \OCP\AppFramework\Http\TemplateResponse;
26
use \OCA\Richdocuments\AppConfig;
27
use \OCA\Richdocuments\Helper;
28
use \OC\Files\View;
29
use OCP\Share\IManager;
30
31
class DocumentController extends Controller {
32
	/** @var string */
33
	private $uid;
34
	/** @var IL10N */
35
	private $l10n;
36
	/** @var IConfig */
37
	private $settings;
38
	/** @var AppConfig */
39
	private $appConfig;
40
	/** @var Parser */
41
	private $wopiParser;
42
	/** @var IManager */
43
	private $shareManager;
44
	/** @var TokenManager */
45
	private $tokenManager;
46
	/** @var IRootFolder */
47
	private $rootFolder;
48
49
	const ODT_TEMPLATE_PATH = '/assets/odttemplate.odt';
50
51
	/**
52
	 * @param string $appName
53
	 * @param IRequest $request
54
	 * @param IConfig $settings
55
	 * @param AppConfig $appConfig
56
	 * @param IL10N $l10n
57
	 * @param Parser $wopiParser
58
	 * @param IManager $shareManager
59
	 * @param TokenManager $tokenManager
60
	 * @param IRootFolder $rootFolder
61
	 * @param string $UserId
62
	 */
63
	public function __construct($appName,
64
								IRequest $request,
65
								IConfig $settings,
66
								AppConfig $appConfig,
67
								IL10N $l10n,
68
								Parser $wopiParser,
69
								IManager $shareManager,
70
								TokenManager $tokenManager,
71
								IRootFolder $rootFolder,
72
								$UserId) {
73
		parent::__construct($appName, $request);
74
		$this->uid = $UserId;
75
		$this->l10n = $l10n;
76
		$this->settings = $settings;
77
		$this->appConfig = $appConfig;
78
		$this->wopiParser = $wopiParser;
79
		$this->shareManager = $shareManager;
80
		$this->tokenManager = $tokenManager;
81
		$this->rootFolder = $rootFolder;
82
	}
83
84
	/**
85
	 * @NoAdminRequired
86
	 *
87
	 * @param string $fileId
88
	 * @return TemplateResponse
89
	 */
90
	public function index($fileId) {
91
		try {
92
			$folder = $this->rootFolder->getUserFolder($this->uid);
93
			$item = $folder->getById($fileId)[0];
94
			if(!($item instanceof Node)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Node does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
95
				throw new \Exception();
96
			}
97
			list($urlSrc, $token) = $this->tokenManager->getToken($item->getId());
98
			$params = [
99
				'permissions' => $item->getPermissions(),
100
				'title' => $item->getName(),
101
				'fileId' => $item->getId(),
102
				'token' => $token,
103
				'urlsrc' => $urlSrc,
104
				'path' => '/',
105
			];
106
107
			$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty');
108
			$policy = new ContentSecurityPolicy();
109
			$policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url'));
110
			$policy->allowInlineScript(true);
111
			$response->setContentSecurityPolicy($policy);
112
			return $response;
113
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
114
		}
115
116
		return new TemplateResponse('core', '403', [], 'guest');
117
	}
118
119
	/**
120
	 * @PublicPage
121
	 *
122
	 * @param string $shareToken
123
	 * @param string $fileName
124
	 * @return TemplateResponse
125
	 * @throws \Exception
126
	 */
127
	public function publicPage($shareToken, $fileName) {
128
		try {
129
			$share = $this->shareManager->getShareByToken($shareToken);
130
			$node = $share->getNode();
131
			if($node instanceof Folder) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Folder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
132
				$item = $node->get($fileName);
133
			} else {
134
				$item = $node;
135
			}
136
			if ($item instanceof Node) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Node does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
137
				list($urlSrc, $token) = $this->tokenManager->getToken($item->getId(), $shareToken);
138
				$params = [
139
					'permissions' => $share->getPermissions(),
140
					'title' => $item->getName(),
141
					'fileId' => $item->getId(),
142
					'token' => $token,
143
					'urlsrc' => $urlSrc,
144
					'path' => '/',
145
				];
146
147
				$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty');
148
				$policy = new ContentSecurityPolicy();
149
				$policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url'));
150
				$policy->allowInlineScript(true);
151
				$response->setContentSecurityPolicy($policy);
152
				return $response;
153
			}
154
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
155
		}
156
157
		return new TemplateResponse('core', '403', [], 'guest');
158
	}
159
160
	/**
161
	 * @NoAdminRequired
162
	 *
163
	 * @param string $mimetype
164
	 * @param string $filename
165
	 * @param string $dir
166
	 * @return JSONResponse
167
	 */
168
	public function create($mimetype,
169
						   $filename,
170
						   $dir){
171
172
		$view = new View('/' . $this->uid . '/files');
173
		if (!$dir){
174
			$dir = '/';
175
		}
176
177
		$basename = $this->l10n->t('New Document.odt');
178
		switch ($mimetype) {
179
			case 'application/vnd.oasis.opendocument.spreadsheet':
180
				$basename = $this->l10n->t('New Spreadsheet.ods');
181
				break;
182
			case 'application/vnd.oasis.opendocument.presentation':
183
				$basename = $this->l10n->t('New Presentation.odp');
184
				break;
185
			case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
186
				$basename = $this->l10n->t('New Document.docx');
187
				break;
188
			case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
189
				$basename = $this->l10n->t('New Spreadsheet.xlsx');
190
				break;
191
			case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
192
				$basename = $this->l10n->t('New Presentation.pptx');
193
				break;
194
			default:
195
				// to be safe
196
				$mimetype = 'application/vnd.oasis.opendocument.text';
197
				break;
198
		}
199
200
		if (!$filename){
201
			$path = Helper::getNewFileName($view, $dir . '/' . $basename);
202
		} else {
203
			$path = $dir . '/' . $filename;
204
		}
205
206
		$content = '';
207
		if (class_exists('\OC\Files\Type\TemplateManager')){
208
			$manager = \OC_Helper::getFileTemplateManager();
209
			$content = $manager->getTemplate($mimetype);
210
		}
211
212
		if (!$content){
213
			$content = file_get_contents(dirname(__DIR__) . self::ODT_TEMPLATE_PATH);
214
		}
215
216
		if ($content && $view->file_put_contents($path, $content)) {
217
			$info = $view->getFileInfo($path);
218
			$ret = $this->wopiParser->getUrlSrc($mimetype);
219
			$response =  array(
220
				'status' => 'success',
221
				'fileid' => $info['fileid'],
222
				'urlsrc' => $ret['urlsrc'],
223
				'action' => $ret['action'],
224
				'lolang' => $this->settings->getUserValue($this->uid, 'core', 'lang', 'en'),
225
				'data' => \OCA\Files\Helper::formatFileInfo($info)
226
			);
227
		} else {
228
			$response =  array(
229
				'status' => 'error',
230
				'message' => (string) $this->l10n->t('Can\'t create document')
231
			);
232
		}
233
234
		return $response;
235
	}
236
}
237