aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/Connect.php
blob: 978a4ce391a0c1d3e8241c37b2b19f2082dac57c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php /** @file */

namespace Zotlabs\Lib;

use App;
use Zotlabs\Access\Permissions;
use Zotlabs\Daemon\Master;



class Connect {

	/**
	 * Takes a $channel and a $url/handle and adds a new connection
	 *
	 * Returns array
	 *  $return['success'] boolean true if successful
	 *  $return['abook'] Address book entry joined with xchan if successful
	 *  $return['message'] error text if success is false.
	 *
	 * This function does NOT send sync packets to clones. The caller is responsible for doing this
	 */

	static function connect($channel, $url, $sub_channel = false) {

		$uid = $channel['channel_id'];

		if (strpos($url,'@') === false && strpos($url,'/') === false) {
			$url = $url . '@' . App::get_hostname();
		}

		$result = [ 'success' => false, 'message' => '' ];

		$my_perms = false;
		$protocol = '';

		if (substr($url,0,1) === '[') {
			$x = strpos($url,']');
			if ($x) {
				$protocol = substr($url,1,$x-1);
				$url = substr($url,$x+1);
			}
		}

		if (! check_siteallowed($url)) {
			$result['message'] = t('Channel is blocked on this site.');
			return $result;
		}

		if (! $url) {
			$result['message'] = t('Channel location missing.');
			return $result;
		}

		// check service class limits

		$r = q("select count(*) as total from abook where abook_channel = %d and abook_self = 0 ",
			intval($uid)
		);
		if ($r) {
			$total_channels = $r[0]['total'];
		}

		if (! service_class_allows($uid,'total_channels',$total_channels)) {
			$result['message'] = upgrade_message();
			return $result;
		}

		$xchan_hash = '';
		$sql_options = (($protocol) ? " and xchan_network = '" . dbesc($protocol) . "' " : '');

		$r = q("select * from xchan where ( xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s') $sql_options ",
			dbesc($url),
			dbesc($url),
			dbesc($url)
		);

		if ($r) {

			// reset results to the best record or the first if we don't have the best
			// note: this is a single record and not an array of results

			$r = Libzot::zot_record_preferred($r,'xchan_network');

		}

		$singleton = false;
		$d = false;

		if (! $r) {

			// not in cache - try discovery

			$wf = discover_by_webbie($url,$protocol);

			if (! $wf) {
				$feeds = get_config('system','feed_contacts');

				if (($feeds) && (in_array($protocol, [ '', 'feed', 'rss' ]))) {
					$d = discover_feed($url);
				}
				else {
					$result['message'] = t('Remote channel or protocol unavailable.');
					return $result;
				}
			}
		}

		if ($wf || $d) {

			// something was discovered - find the record which was just created.

			$r = q("select * from xchan where ( xchan_hash = '%s' or xchan_url = '%s' or xchan_addr = '%s' ) $sql_options",
				dbesc(($wf) ? $wf : $url),
				dbesc($url),
				dbesc($url)
			);

			// convert to a single record (once again preferring a zot solution in the case of multiples)

			if ($r) {
				$r = Libzot::zot_record_preferred($r,'xchan_network');
			}
		}

		// if discovery was a success or the channel was already cached we should have an xchan record in $r

		if ($r) {
			$xchan = $r;
			$xchan_hash = $r['xchan_hash'];
			$their_perms = EMPTY_STR;
		}

		// failure case

		if (! $xchan_hash) {
			$result['message'] = t('Channel discovery failed.');
			logger('follow: ' . $result['message']);
			return $result;
		}

		if (! check_channelallowed($xchan_hash)) {
			$result['message'] = t('Channel is blocked on this site.');
			logger('follow: ' . $result['message']);
			return $result;

		}

		$allowed = ((in_array($xchan['xchan_network'],['rss','zot','zot6'])) ? 1 : 0);

		$hookdata = ['channel_id' => $uid, 'follow_address' => $url, 'xchan' => $xchan, 'allowed' => $allowed, 'singleton' => 0];
		call_hooks('follow_allow',$hookdata);

		if(! $hookdata['allowed']) {
			hz_syslog('zesz');
			$result['message'] = t('Protocol disabled.');
			return $result;
		}

		$singleton = intval($hookdata['singleton']);

		// Now start processing the new connection

		$aid = $channel['channel_account_id'];
		$default_group = $channel['channel_default_group'];

		if (in_array($xchan_hash, [$channel['channel_hash'], $channel['channel_portable_id']])) {
			$result['message'] = t('Cannot connect to yourself.');
			return $result;
		}

		if ($xchan['xchan_network'] === 'rss') {

			// check service class feed limits

			$t = q("select count(*) as total from abook where abook_account = %d and abook_feed = 1 ",
				intval($aid)
			);
			if ($t) {
				$total_feeds = $t[0]['total'];
			}

			if (! service_class_allows($uid,'total_feeds',$total_feeds)) {
				$result['message'] = upgrade_message();
				return $result;
			}

			// Always set these "remote" permissions for feeds since we cannot interact with them
			// to negotiate a suitable permission response

			$p = get_abconfig($uid,$xchan_hash,'system','their_perms',EMPTY_STR);
			if ($p) {
				$p .= ',';
			}
			$p .= 'view_stream,republish';
			set_abconfig($uid,$xchan_hash,'system','their_perms',$p);

		}


		$p = Permissions::connect_perms($uid);

		// parent channels have unencumbered write permission

		if ($sub_channel) {
			$p['perms']['post_wall']     = 1;
			$p['perms']['post_comments'] = 1;
			$p['perms']['write_storage'] = 1;
			$p['perms']['post_like']     = 1;
			$p['perms']['delegate']      = 0;
			$p['perms']['moderated']     = 0;
		}

		$my_perms = Permissions::serialise($p['perms']);
		
		$profile_assign = get_pconfig($uid,'system','profile_assign','');


		// See if we are already connected by virtue of having an abook record

		$r = q("select abook_id, abook_xchan, abook_pending, abook_instance from abook 
			where abook_xchan = '%s' and abook_channel = %d limit 1",
			dbesc($xchan_hash),
			intval($uid)
		);

		if ($r) {

			$abook_instance = $r[0]['abook_instance'];

			// If they are on a non-nomadic network, add them to this location

			if (($singleton) && strpos($abook_instance,z_root()) === false) {
				if ($abook_instance) {
					$abook_instance .= ',';
				}
				$abook_instance .= z_root();

				$x = q("update abook set abook_instance = '%s', abook_not_here = 0 where abook_id = %d",
					dbesc($abook_instance),
					intval($r[0]['abook_id'])
				);
			}

			// if they have a pending connection, we just followed them so approve the connection request

			if (intval($r[0]['abook_pending'])) {
				$x = q("update abook set abook_pending = 0 where abook_id = %d",
					intval($r[0]['abook_id'])
				);
			}
		}
		else {

			// create a new abook record

			$closeness = get_pconfig($uid,'system','new_abook_closeness',80);

			$r = abook_store_lowlevel(
				[
					'abook_account'   => intval($aid),
					'abook_channel'   => intval($uid),
					'abook_closeness' => intval($closeness),
					'abook_xchan'     => $xchan_hash,
					'abook_profile'   => $profile_assign,
					'abook_feed'      => intval(($xchan['xchan_network'] === 'rss') ? 1 : 0),
					'abook_created'   => datetime_convert(),
					'abook_updated'   => datetime_convert(),
					'abook_instance'  => (($singleton) ? z_root() : '')
				]
			);
		}

		if (! $r) {
			logger('abook creation failed');
			$result['message'] = t('error saving data');
			return $result;
		}

		// Set suitable permissions to the connection

		if ($my_perms) {
			set_abconfig($uid,$xchan_hash,'system','my_perms',$my_perms);
		}

		// fetch the entire record

		$r = q("select abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash 
			where abook_xchan = '%s' and abook_channel = %d limit 1",
			dbesc($xchan_hash),
			intval($uid)
		);

		if ($r) {
			$result['abook'] = array_shift($r);
			Master::Summon([ 'Notifier', 'permission_create', $result['abook']['abook_id'] ]);
		}

		$arr = [ 'channel_id' => $uid, 'channel' => $channel, 'abook' => $result['abook'] ];

		call_hooks('follow', $arr);

		/** If there is a default group for this channel, add this connection to it */

		if ($default_group) {
			$g = AccessList::rec_byhash($uid,$default_group);
			if ($g) {
				AccessList::member_add($uid,'',$xchan_hash,$g['id']);
			}
		}

		$result['success'] = true;
		return $result;
	}
}