Completed
Push — master ( 8652ef...929a28 )
by Morris
11:10
created

Notifications::getTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
24
namespace OCA\FederatedFileSharing;
25
26
use OCP\BackgroundJob\IJobList;
27
use OCP\Http\Client\IClientService;
28
29
class Notifications {
30
	const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
31
32
	/** @var AddressHandler */
33
	private $addressHandler;
34
35
	/** @var IClientService */
36
	private $httpClientService;
37
38
	/** @var DiscoveryManager */
39
	private $discoveryManager;
40
41
	/** @var IJobList  */
42
	private $jobList;
43
44
	/**
45 1
	 * @param AddressHandler $addressHandler
46
	 * @param IClientService $httpClientService
47
	 * @param DiscoveryManager $discoveryManager
48
	 * @param IJobList $jobList
49 1
	 */
50 1
	public function __construct(
51 1
		AddressHandler $addressHandler,
52
		IClientService $httpClientService,
53
		DiscoveryManager $discoveryManager,
54
		IJobList $jobList
55
	) {
56
		$this->addressHandler = $addressHandler;
57
		$this->httpClientService = $httpClientService;
58
		$this->discoveryManager = $discoveryManager;
59
		$this->jobList = $jobList;
60
	}
61
62
	/**
63
	 * send server-to-server share to remote server
64
	 *
65
	 * @param string $token
66
	 * @param string $shareWith
67
	 * @param string $name
68
	 * @param int $remote_id
69
	 * @param string $owner
70
	 * @return bool
71
	 */
72
	public function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) {
73
74
		list($user, $remote) = $this->addressHandler->splitUserRemote($shareWith);
75
76
		if ($user && $remote) {
77
			$url = $remote;
78
			$local = $this->addressHandler->generateRemoteURL();
79
80
			$fields = array(
81
				'shareWith' => $user,
82
				'token' => $token,
83
				'name' => $name,
84
				'remoteId' => $remote_id,
85
				'owner' => $owner,
86
				'remote' => $local,
87
			);
88
89
			$url = $this->addressHandler->removeProtocolFromUrl($url);
90
			$result = $this->tryHttpPostToShareEndpoint($url, '', $fields);
91
			$status = json_decode($result['result'], true);
92
93
			if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) {
94
				\OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]);
95
				return true;
96
			}
97
98
		}
99
100
		return false;
101
	}
102
103
	/**
104
	 * send server-to-server unshare to remote server
105
	 *
106
	 * @param string $remote url
107
	 * @param int $id share id
108
	 * @param string $token
109
	 * @param int $try how often did we already tried to send the un-share request
110
	 * @return bool
111
	 */
112
	public function sendRemoteUnShare($remote, $id, $token, $try = 0) {
113
		$url = rtrim($remote, '/');
114
		$fields = array('token' => $token, 'format' => 'json');
115
		$url = $this->addressHandler->removeProtocolFromUrl($url);
116
		$result = $this->tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields);
117
		$status = json_decode($result['result'], true);
118
119
		if ($result['success'] && 
120
			($status['ocs']['meta']['statuscode'] === 100 || 
121
				$status['ocs']['meta']['statuscode'] === 200
122
			)
123
		) {
124
			return true;
125
		} elseif ($try === 0) {
126
			// only add new job on first try
127
			$this->jobList->add('OCA\FederatedFileSharing\BackgroundJob\UnShare',
128
				[
129
					'remote' => $remote,
130
					'id' => $id,
131
					'token' => $token,
132
					'try' => $try,
133
					'lastRun' => $this->getTimestamp()
134
				]
135
			);
136
		}
137
138
		return false;
139
	}
140
141
	/**
142
	 * return current timestamp
143
	 *
144
	 * @return int
145
	 */
146
	protected function getTimestamp() {
147
		return time();
148
	}
149
150
	/**
151
	 * try http post first with https and then with http as a fallback
152
	 *
153
	 * @param string $remoteDomain
154
	 * @param string $urlSuffix
155
	 * @param array $fields post parameters
156
	 * @return array
157
	 */
158
	protected function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) {
159
		$client = $this->httpClientService->newClient();
160
		$protocol = 'https://';
161
		$result = [
162
			'success' => false,
163
			'result' => '',
164
		];
165
		$try = 0;
166
167
		while ($result['success'] === false && $try < 2) {
168
			$endpoint = $this->discoveryManager->getShareEndpoint($protocol . $remoteDomain);
169
			try {
170
				$response = $client->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, [
171
					'body' => $fields
172
				]);
173
				$result['result'] = $response->getBody();
174
				$result['success'] = true;
175
				break;
176
			} catch (\Exception $e) {
177
				$try++;
178
				$protocol = 'http://';
179
			}
180
		}
181
182
		return $result;
183
	}
184
}
185