WopiMapper::getWopiForToken()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 21
cp 0
rs 9.392
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * @copyright 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Richdocuments\Db;
24
25
use OCP\AppFramework\Db\DoesNotExistException;
26
use OCP\AppFramework\Db\Mapper;
27
use OCP\AppFramework\Utility\ITimeFactory;
28
use OCP\IDBConnection;
29
use OCP\ILogger;
30
use OCP\Security\ISecureRandom;
31
32
class WopiMapper extends Mapper {
0 ignored issues
show
Deprecated Code introduced by
The class OCP\AppFramework\Db\Mapper has been deprecated with message: 14.0.0 Move over to QBMapper

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
33
	// Tokens expire after this many seconds (not defined by WOPI specs).
34
	const TOKEN_LIFETIME_SECONDS = 1800;
35
36
	/** @var ISecureRandom */
37
	private $random;
38
39
	/** @var ILogger */
40
	private $logger;
41
42
	/** @var ITimeFactory */
43
	private $timeFactory;
44
45
	public function __construct(IDBConnection $db,
46
								ISecureRandom $random,
47
								ILogger $logger,
48
								ITimeFactory $timeFactory) {
49
		parent::__construct($db, 'richdocuments_wopi', Wopi::class);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::__construct() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
50
51
		$this->random = $random;
52
		$this->logger = $logger;
53
		$this->timeFactory = $timeFactory;
54
	}
55
56
	/**
57
	 * @param int $fileId
58
	 * @param string $owner
59
	 * @param string $editor
60
	 * @param int $version
61
	 * @param bool $updatable
62
	 * @param string $serverHost
63
	 * @param string $guestDisplayname
64
	 * @param int $templateDestination
65
	 * @return Wopi
66
	 */
67
	public function generateFileToken($fileId, $owner, $editor, $version, $updatable, $serverHost, $guestDisplayname, $templateDestination = 0, $hideDownload = false, $direct = false, $isRemoteToken = false, $templateId = 0, $share = null) {
68
		$token = $this->random->generate(32, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
69
70
		$wopi = Wopi::fromParams([
71
			'fileid' => $fileId,
72
			'ownerUid' => $owner,
73
			'editorUid' => $editor,
74
			'version' => $version,
75
			'canwrite' => $updatable,
76
			'serverHost' => $serverHost,
77
			'token' => $token,
78
			'expiry' => $this->timeFactory->getTime() + self::TOKEN_LIFETIME_SECONDS,
79
			'guestDisplayname' => $guestDisplayname,
80
			'templateDestination' => $templateDestination,
81
			'hideDownload' => $hideDownload,
82
			'direct' => $direct,
83
			'isRemoteToken' => $isRemoteToken,
84
			'templateId' => $templateId,
85
			'remoteServer' => '',
86
			'remoteServerToken' => '',
87
			'share' => $share
88
		]);
89
90
		/** @var Wopi $wopi */
91
		$wopi = $this->insert($wopi);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
92
93
		return $wopi;
94
	}
95
96
	/**
97
	 * @deprecated
98
	 * @param $token
99
	 */
100
	public function getPathForToken($token) {
101
		return $this->getWopiForToken($token);
102
	}
103
	/**
104
	 * Given a token, validates it and
105
	 * constructs and validates the path.
106
	 * Returns the path, if valid, else false.
107
	 *
108
	 * @param string $token
109
	 * @throws DoesNotExistException
110
	 * @return Wopi
111
	 */
112
	public function getWopiForToken($token) {
113
114
		$qb = $this->db->getQueryBuilder();
115
		$qb->select('*')
116
			->from('richdocuments_wopi')
117
			->where(
118
				$qb->expr()->eq('token', $qb->createNamedParameter($token))
119
			);
120
		$result = $qb->execute();
121
		$row = $result->fetch();
122
		$result->closeCursor();
123
124
		$this->logger->debug('Loaded WOPI Token record: {row}.', [
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::debug() has been deprecated with message: 20.0.0 use \Psr\Log\LoggerInterface::debug

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
125
			'row' => $row,
126
			'app' => 'richdocuments'
127
		]);
128
		if ($row === false) {
129
			throw new DoesNotExistException('Could not find token.');
130
		}
131
132
		/** @var Wopi $wopi */
133
		$wopi = Wopi::fromRow($row);
134
135
		//TODO: validate.
136
		if ($wopi->getExpiry() > $this->timeFactory->getTime()){
137
			// Expired token!
138
			//http_response_code(404);
139
			//$wopi->deleteBy('id', $row['id']);
140
			//return false;
141
		}
142
143
		return $wopi;
144
	}
145
}
146