aboutsummaryrefslogtreecommitdiffstats
path: root/include/socgraph.php
blob: 45ab9a366594f631cb9264653ee41ec5e83398cc (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<?php /** @file */

require_once('include/dir_fns.php');
require_once('include/zot.php');

/**
 * poco_load
 *
 * xchan is your connection
 * We will load their friend list, and store in xlink_xchan your connection hash and xlink_link the hash for each connection
 * If xchan isn't provided we will load the list of people from url who have indicated they are willing to be friends with
 * new folks and add them to xlink with no xlink_xchan.
 *
 * Old behaviour: (documentation only):
 * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
 * and add the entries to the gcontact (Global Contact) table, or update existing entries
 * if anything (name or photo) has changed.
 * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
 *
 * Once the global contact is stored add (if necessary) the contact linkage which associates
 * the given uid, cid to the global contact entry. There can be many uid/cid combinations
 * pointing to the same global contact id. 
 *
 * @param string $xchan
 * @param string $url
 */
function poco_load($xchan = '', $url = null) {

	if($xchan && ! $url) {
		$r = q("select xchan_connurl from xchan where xchan_hash = '%s' limit 1",
			dbesc($xchan)
		);
		if($r) {
			$url = $r[0]['xchan_connurl'];
		}
	}

	if(! $url) {
		logger('poco_load: no url');
		return;
	}

	$url = $url . '?f=&fields=displayName,hash,urls,photos' ;

	logger('poco_load: ' . $url, LOGGER_DEBUG);

	$s = z_fetch_url($url);

	if(! $s['success']) {
		if($s['return_code'] == 401)
			logger('poco_load: protected');
		elseif($s['return_code'] == 404)
			logger('poco_load: nothing found');
		else
			logger('poco_load: returns ' . print_r($s,true));
		return;
	}

	$j = json_decode($s['body'],true);

	if(! $j) {
		logger('poco_load: unable to json_decode returned data.');
		return;
	}

	logger('poco_load: ' . print_r($j,true),LOGGER_DATA);

	if($xchan) {
		if(array_key_exists('chatrooms',$j) && is_array($j['chatrooms'])) {
			foreach($j['chatrooms'] as $room) {
				if((! $room['url']) || (! $room['desc']))
					continue;

				$r = q("select * from xchat where xchat_url = '%s' and xchat_xchan = '%s' limit 1",
					dbesc($room['url']),
					dbesc($xchan)
				);
				if($r) {
					q("update xchat set xchat_edited = '%s' where xchat_id = %d",
						dbesc(datetime_convert()),
						intval($r[0]['xchat_id'])
					);
				}
				else {
					$x = q("insert into xchat ( xchat_url, xchat_desc, xchat_xchan, xchat_edited )
						values ( '%s', '%s', '%s', '%s' ) ",
						dbesc(escape_tags($room['url'])),
						dbesc(escape_tags($room['desc'])),
						dbesc($xchan),
						dbesc(datetime_convert())
					);
				}
			}
		}
		q("delete from xchat where xchat_edited < %s - INTERVAL %s and xchat_xchan = '%s' ",
			db_utcnow(), db_quoteinterval('7 DAY'),
			dbesc($xchan)
		);
	}

	if(! ((x($j,'entry')) && (is_array($j['entry'])))) {
		logger('poco_load: no entries');
		return;
	}

	$total = 0;
	foreach($j['entry'] as $entry) {

		$profile_url = '';
		$profile_photo = '';
		$address = '';
		$name = '';
		$hash = '';
		$rating = 0;

		$name   = $entry['displayName'];
		$hash   = $entry['hash'];

		if(x($entry,'urls') && is_array($entry['urls'])) {
			foreach($entry['urls'] as $url) {
				if($url['type'] == 'profile') {
					$profile_url = $url['value'];
					continue;
				}
				if($url['type'] == 'zot' || $url['type'] == 'diaspora' || $url['type'] == 'friendica') {
					$network = $url['type'];
					$address = str_replace('acct:' , '', $url['value']);
					continue;
				}
			}
		}
		if(x($entry,'photos') && is_array($entry['photos'])) { 
			foreach($entry['photos'] as $photo) {
				if($photo['type'] == 'profile') {
					$profile_photo = $photo['value'];
					continue;
				}
			}
		}

		if((! $name) || (! $profile_url) || (! $profile_photo) || (! $hash) || (! $address)) {
			logger('poco_load: missing data');
			continue; 
		}

		$x = q("select xchan_hash from xchan where xchan_hash = '%s' limit 1",
			dbesc($hash)
		);

		// We've never seen this person before. Import them.

		if(($x !== false) && (! count($x))) {
			if($address) {
				if($network === 'zot') {
					$z = zot_finger($address,null);
					if($z['success']) {
						$j = json_decode($z['body'],true);
						if($j)
							import_xchan($j);
					}
					$x = q("select xchan_hash from xchan where xchan_hash = '%s' limit 1",
						dbesc($hash)
					);
					if(! $x) {
						continue;
					}
				}
				else {
					$x = import_author_diaspora(array('address' => $address));
					if(! $x) {
						continue;
					}
				}
			}
			else {
				continue;
			}
		}

		$total ++;


		$r = q("select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 0 limit 1",
			dbesc($xchan),
			dbesc($hash)
		);

		if(! $r) {
			q("insert into xlink ( xlink_xchan, xlink_link, xlink_updated, xlink_static ) values ( '%s', '%s', '%s', 0 ) ",
				dbesc($xchan),
				dbesc($hash),
				dbesc(datetime_convert())
			);
		}
		else {
			q("update xlink set xlink_updated = '%s' where xlink_id = %d",
				dbesc(datetime_convert()),
				intval($r[0]['xlink_id'])
			);
		}
	}
	logger("poco_load: loaded $total entries",LOGGER_DEBUG);

	q("delete from xlink where xlink_xchan = '%s' and xlink_updated < %s - INTERVAL %s and xlink_static = 0",
		dbesc($xchan),
		db_utcnow(), db_quoteinterval('2 DAY')
	);

}


function count_common_friends($uid,$xchan) {

	$r = q("SELECT count(xlink_id) as total from xlink where xlink_xchan = '%s' and xlink_static = 0 and xlink_link in
		(select abook_xchan from abook where abook_xchan != '%s' and abook_channel = %d and abook_flags = 0 )",
		dbesc($xchan),
		dbesc($xchan),
		intval($uid)
	);

	if($r)
		return $r[0]['total'];
	return 0;
}


function common_friends($uid,$xchan,$start = 0,$limit=100000000,$shuffle = false) {

	$rand = db_getfunc('rand');
	if($shuffle)
		$sql_extra = " order by $rand ";
	else
		$sql_extra = " order by xchan_name asc "; 

	$r = q("SELECT * from xchan left join xlink on xlink_link = xchan_hash where xlink_xchan = '%s' and xlink_static = 0 and xlink_link in
		(select abook_xchan from abook where abook_xchan != '%s' and abook_channel = %d and abook_flags = 0 ) $sql_extra limit %d offset %d",
		dbesc($xchan),
		dbesc($xchan),
		intval($uid),
		intval($limit),
		intval($start)
	);

	return $r;
}


function count_common_friends_zcid($uid,$zcid) {

	$r = q("SELECT count(*) as `total` 
		FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
		where `glink`.`zcid` = %d
		and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
		intval($zcid),
		intval($uid)
	);

	if(count($r))
		return $r[0]['total'];

	return 0;
}

function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {

	if($shuffle)
		$sql_extra = " order by rand() ";
	else
		$sql_extra = " order by `gcontact`.`name` asc "; 

	$r = q("SELECT `gcontact`.* 
		FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
		where `glink`.`zcid` = %d
		and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) 
		$sql_extra limit %d offset %d",
		intval($zcid),
		intval($uid),
		intval($limit),
		intval($start)
	);

	return $r;
}


function count_all_friends($uid,$cid) {

	$r = q("SELECT count(*) as `total`
		FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
		where `glink`.`cid` = %d and `glink`.`uid` = %d ",
		intval($cid),
		intval($uid)
	);

	if(count($r))
		return $r[0]['total'];

	return 0;
}


function all_friends($uid,$cid,$start = 0, $limit = 80) {

	$r = q("SELECT `gcontact`.* 
		FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
		where `glink`.`cid` = %d and `glink`.`uid` = %d 
		order by `gcontact`.`name` asc LIMIT %d OFFSET %d ",
		intval($cid),
		intval($uid),
		intval($limit),
		intval($start)
	);

	return $r;
}



function suggestion_query($uid, $myxchan, $start = 0, $limit = 80) {

	if((! $uid) || (! $myxchan))
		return array();

	$r = q("SELECT count(xlink_xchan) as `total`, xchan.* from xchan
		left join xlink on xlink_link = xchan_hash
		where xlink_xchan in ( select abook_xchan from abook where abook_channel = %d )
		and not xlink_link in ( select abook_xchan from abook where abook_channel = %d )
		and not xlink_link in ( select xchan from xign where uid = %d )
		and xlink_xchan != ''
		and xchan_hidden = 0
		and xchan_deleted = 0
		and xlink_static = 0
		group by xchan_hash order by total desc limit %d offset %d ",
		intval($uid),
		intval($uid),
		intval($uid),
		intval($limit),
		intval($start)
	);

	if($r && count($r) >= ($limit -1))
		return $r;

	$r2 = q("SELECT count(xlink_link) as `total`, xchan.* from xchan
		left join xlink on xlink_link = xchan_hash
		where xlink_xchan = ''
		and not xlink_link in ( select abook_xchan from abook where abook_channel = %d )
		and not xlink_link in ( select xchan from xign where uid = %d )
		and xchan_hidden = 0
		and xchan_deleted = 0
		and xlink_static = 0
		group by xchan_hash order by total desc limit %d offset %d ",
		intval($uid),
		intval($uid),
		intval($limit),
		intval($start)
	);

	if(is_array($r) && is_array($r2))
		return array_merge($r,$r2);

	return array();
}

function update_suggestions() {

	$dirmode = get_config('system', 'directory_mode');
	if($dirmode === false)
		$dirmode = DIRECTORY_MODE_NORMAL;

	if(($dirmode == DIRECTORY_MODE_PRIMARY) || ($dirmode == DIRECTORY_MODE_STANDALONE)) {
		$url = z_root() . '/sitelist';
	}
	else {
		$directory = find_upstream_directory($dirmode);
		$url = $directory['url'] . '/sitelist';
	}
	if(! $url)
		return;

	$ret = z_fetch_url($url);

	if($ret['success']) {

		// We will grab fresh data once a day via the poller. Remove anything over a week old because
		// the targets may have changed their preferences and don't want to be suggested - and they 
		// may have simply gone away. 

		$r = q("delete from xlink where xlink_xchan = '' and xlink_updated < %s - INTERVAL %s and xlink_static = 0",
			db_utcnow(), db_quoteinterval('7 DAY')
		);

		$j = json_decode($ret['body'],true);
		if($j && $j['success']) {
			foreach($j['entries'] as $host) {
				poco_load('',$host['url'] . '/poco');
			}
		}
	}
}


function poco($a,$extended = false) {

	$system_mode = false;

	if(intval(get_config('system','block_public')) && (! local_channel()) && (! remote_channel())) {
		logger('mod_poco: block_public');
		http_status_exit(401);
	}

	$observer = $a->get_observer();

	if(argc() > 1) {
		$user = notags(trim(argv(1)));
	}
	if(! x($user)) {
		$c = q("select * from pconfig where cat = 'system' and k = 'suggestme' and v = '1'");
		if(! $c) {
			logger('mod_poco: system mode. No candidates.', LOGGER_DEBUG);
			http_status_exit(404);
		}
		$system_mode = true;
	}

	$format = (($_REQUEST['format']) ? $_REQUEST['format'] : 'json');

	$justme = false;

	if(argc() > 2 && argv(2) === '@me')
		$justme = true;
	if(argc() > 3) {
		if(argv(3) === '@all')
			$justme = false;
		elseif(argv(3) === '@self')
			$justme = true;
	}
	if(argc() > 4 && intval(argv(4)) && $justme == false)
		$cid = intval(argv(4));

	if(! $system_mode) {

		$r = q("SELECT channel_id from channel where channel_address = '%s' limit 1",
			dbesc($user)
		);
		if(! $r) {
			logger('mod_poco: user mode. Account not found. ' . $user);
			http_status_exit(404);
		}

		$channel_id = $r[0]['channel_id'];
		$ohash = (($observer) ? $observer['xchan_hash'] : '');

		if(! perm_is_allowed($channel_id,$ohash,'view_contacts')) {
			logger('mod_poco: user mode. Permission denied for ' . $ohash . ' user: ' . $user);
			http_status_exit(401);
		}

	}

	if($justme)
		$sql_extra = " and ( abook_flags & " . ABOOK_FLAG_SELF . " )>0 ";
	else
		$sql_extra = " and abook_flags = 0 ";

	if($cid)
		$sql_extra = sprintf(" and abook_id = %d  and ( abook_flags & " . ABOOK_FLAG_HIDDEN . " ) = 0 ",intval($cid));

	if($system_mode) {
		$r = q("SELECT count(*) as `total` from abook where ( abook_flags & " . ABOOK_FLAG_SELF . 
			" )>0 and abook_channel in (select uid from pconfig where cat = 'system' and k = 'suggestme' and v = '1') ");
	}
	else {
		$r = q("SELECT count(*) as `total` from abook where abook_channel = %d 
			$sql_extra ",
			intval($channel_id)
		);
		$rooms = q("select * from menu_item where ( mitem_flags & " . intval(MENU_ITEM_CHATROOM) . " )>0 and allow_cid = '' and allow_gid = '' and deny_cid = '' and deny_gid = '' and mitem_channel_id = %d",
			intval($channel_id)
		);
	}
	if($r)
		$totalResults = intval($r[0]['total']);
	else
		$totalResults = 0;

	$startIndex = intval($_GET['startIndex']);
	if(! $startIndex)
		$startIndex = 0;

	$itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);

	if($system_mode) {
		$r = q("SELECT abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash where ( abook_flags & " . ABOOK_FLAG_SELF . 
			" )>0 and abook_channel in (select uid from pconfig where cat = 'system' and k = 'suggestme' and v = '1') limit %d offset %d ",
			intval($itemsPerPage),
			intval($startIndex)
		);
	} else {
		$r = q("SELECT abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash where abook_channel = %d 
			$sql_extra LIMIT %d OFFSET %d",
			intval($channel_id),
			intval($itemsPerPage),
			intval($startIndex)
		);
	}

	$ret = array();
	if(x($_GET,'sorted'))
		$ret['sorted'] = 'false';
	if(x($_GET,'filtered'))
		$ret['filtered'] = 'false';
	if(x($_GET,'updatedSince'))
		$ret['updateSince'] = 'false';

	$ret['startIndex']   = (string) $startIndex;
	$ret['itemsPerPage'] = (string) $itemsPerPage;
	$ret['totalResults'] = (string) $totalResults;

	if($rooms) {
		$ret['chatrooms'] = array();
		foreach($rooms as $room) {
			$ret['chatrooms'][] = array('url' => $room['mitem_link'], 'desc' => $room['mitem_desc']);
		}
	}

	$ret['entry'] = array();

	$fields_ret = array(
		'id' => false,
		'guid' => false,
		'guid_sig' => false,
		'hash' => false,
		'displayName' => false,
		'urls' => false,
		'preferredUsername' => false,
		'photos' => false,
		'rating' => false
	);

	if((! x($_GET,'fields')) || ($_GET['fields'] === '@all')) {
		foreach($fields_ret as $k => $v)
			$fields_ret[$k] = true;
	} else {
		$fields_req = explode(',',$_GET['fields']);
		foreach($fields_req as $f)
			$fields_ret[trim($f)] = true;
	}

	if(is_array($r)) {
		if(count($r)) {
			foreach($r as $rr) {
				$entry = array();
				if($fields_ret['id'])
					$entry['id'] = $rr['abook_id'];
				if($fields_ret['guid'])
					$entry['guid'] = $rr['xchan_guid'];
				if($fields_ret['guid_sig'])
					$entry['guid_sig'] = $rr['xchan_guid_sig'];
				if($fields_ret['hash'])
					$entry['hash'] = $rr['xchan_hash'];

				if($fields_ret['displayName'])
					$entry['displayName'] = $rr['xchan_name'];
				if($fields_ret['urls']) {
					$entry['urls'] = array(array('value' => $rr['xchan_url'], 'type' => 'profile'));
					$network = $rr['xchan_network'];
					if(strpos($network,'friendica') !== false)
						$network = 'friendica';
					if($rr['xchan_addr'])
						$entry['urls'][] = array('value' => 'acct:' . $rr['xchan_addr'], 'type' => $network);  
				}
				if($fields_ret['preferredUsername'])
					$entry['preferredUsername'] = substr($rr['xchan_addr'],0,strpos($rr['xchan_addr'],'@'));
				if($fields_ret['photos'])
					$entry['photos'] = array(array('value' => $rr['xchan_photo_l'], 'mimetype' => $rr['xchan_photo_mimetype'], 'type' => 'profile'));
				$ret['entry'][] = $entry;
			}
		}
		else
			$ret['entry'][] = array();
	}
	else
		http_status_exit(500);

	if($format === 'xml') {
		header('Content-type: text/xml');
		echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
		http_status_exit(500);
	}
	if($format === 'json') {
		header('Content-type: application/json');
		echo json_encode($ret);
		killme();	
	}
	else
		http_status_exit(500);

}