Completed
Push — federated-circles ( 47921f...25ee14 )
by Maxence
04:23
created

FederatedController::broadcastItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Controller;
28
29
use OC\AppFramework\Http;
30
use OCA\Circles\Model\FederatedLink;
31
use OCA\Circles\Service\FederatedService;
32
use OCA\Circles\Service\CirclesService;
33
use OCA\Circles\Service\ConfigService;
34
use OCA\Circles\Service\MembersService;
35
use OCA\Circles\Service\MiscService;
36
use OCA\Circles\Service\SharesService;
37
use OCP\AppFramework\Http\DataResponse;
38
use OCP\IL10N;
39
40
class FederatedController extends BaseController {
41
42
	/** @var string */
43
	protected $userId;
44
45
	/** @var IL10N */
46
	protected $l10n;
47
48
	/** @var ConfigService */
49
	protected $configService;
50
51
	/** @var CirclesService */
52
	protected $circlesService;
53
54
	/** @var MembersService */
55
	protected $membersService;
56
57
	/** @var SharesService */
58
	protected $sharesService;
59
60
	/** @var FederatedService */
61
	protected $federatedService;
62
63
	/** @var MiscService */
64
	protected $miscService;
65
66
67
	/**
68
	 * @PublicPage
69
	 * @NoCSRFRequired
70
	 *
71
	 * @param $token
72
	 * @param $uniqueId
73
	 * @param $sourceName
74
	 * @param $linkTo
75
	 * @param $address
76
	 *
77
	 * @return DataResponse
78
	 */
79
	public function requestedLink($token, $uniqueId, $sourceName, $linkTo, $address) {
80
81
		if (!$this->configService->isFederatedAllowed()) {
82
			return $this->federatedFail('federated_not_allowed');
83
		}
84
85
		$circle = $this->circlesService->infoCircleByName($linkTo);
86
		if ($circle === null) {
87
			return $this->federatedFail('circle_does_not_exist');
88
		}
89
90
		if ($circle->getUniqueId() === $uniqueId) {
91
			return $this->federatedFail('duplicate_unique_id');
92
		}
93
94
		if ($this->federatedService->getLink($circle->getId(), $uniqueId) !== null) {
95
			return $this->federatedFail('duplicate_link');
96
		}
97
98
		$link = new FederatedLink();
99
		$link->setToken($token)
100
			 ->setUniqueId($uniqueId)
101
			 ->setRemoteCircleName($sourceName)
102
			 ->setAddress($address);
103
104
		if ($this->federatedService->initiateLink($circle, $link)) {
105
			return $this->federatedSuccess(
106
				['status' => $link->getStatus(), 'uniqueId' => $circle->getUniqueId()], $link
107
			);
108
		} else {
109
			return $this->federatedFail('link_failed');
110
		}
111
	}
112
113
114
	/**
115
	 * @PublicPage
116
	 * @NoCSRFRequired
117
	 */
118
	public function broadcastItem() {
119
120
		$this->miscService->log("BroadItem start");
121
122
		// We don't want to keep the connection with the client up and running
123
		// as he might have others things to do
124
		$this->asyncAndLeaveClientOutOfThis('done');
125
126
		sleep(15);
127
		$this->miscService->log("BroadItem end");
128
		exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method broadcastItem() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
129
	}
130
131
	/**
132
	 * Hacky way to async the rest of the process without keeping client on hold.
133
	 *
134
	 * @param string $result
135
	 */
136
	private function asyncAndLeaveClientOutOfThis($result = '') {
137
		if (ob_get_contents() !== false) {
138
			ob_end_clean();
139
		}
140
141
		header("Connection: close");
142
		ignore_user_abort();
143
		ob_start();
144
		echo($result);
145
		$size = ob_get_length();
146
		header("Content-Length: $size");
147
		ob_end_flush();
148
		flush();
149
	}
150
151
	/**
152
	 * @param array $data
153
	 * @param FederatedLink $link
154
	 *
155
	 * @return DataResponse
156
	 */
157
	private function federatedSuccess($data, $link) {
158
		return new DataResponse(
159
			array_merge($data, ['token' => $link->getToken()]), Http::STATUS_OK
160
		);
161
162
	}
163
164
	/**
165
	 * @param $reason
166
	 *
167
	 * @return DataResponse
168
	 */
169
	private function federatedFail($reason) {
170
		return new DataResponse(
171
			[
172
				'status' => FederatedLink::STATUS_ERROR,
173
				'reason' => $reason
174
			],
175
			Http::STATUS_OK
176
		);
177
	}
178
}