aboutsummaryrefslogtreecommitdiffstats
path: root/include/externals.php
blob: 95ced24bf9b51740843391c2db6132283493ba45 (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
<?php /** @file */

require_once('boot.php');
require_once('include/cli_startup.php');
require_once('include/zot.php');
require_once('include/identity.php');

function externals_run($argv, $argc){

	cli_startup();
	$a = get_app();
	

	$total = 0;
	$attempts = 0;

	// pull in some public posts


	while($total == 0 && $attempts < 3) {
		$arr = array('url' => '');
		call_hooks('externals_url_select',$arr);

		if($arr['url']) {
			$url = $arr['url'];
		} 
		else {
			$r = q("select site_url, site_pull from site where site_url != '%s' and site_flags != %d order by rand() limit 1",
				dbesc(z_root()),
				intval(DIRECTORY_MODE_STANDALONE)
			);
			if($r)
				$url = $r[0]['site_url'];
		}

		$attempts ++;

		if($url) {
			if($r[0]['site_pull'] !== '0000-00-00 00:00:00')
				$mindate = urlencode($r[0]['site_pull']);
			else {
				$days = get_config('externals','since_days');
				if($days === false)
					$days = 15;
				$mindate = urlencode(datetime_convert('','','now - ' . intval($days) . ' days'));
			}

			$feedurl = $url . '/zotfeed?f=&mindate=' . $mindate;

			logger('externals: pulling public content from ' . $feedurl, LOGGER_DEBUG);

			$x = z_fetch_url($feedurl);
			if(($x) && ($x['success'])) {

				q("update site set site_pull = '%s' where site_url = '%s limit 1",
					dbesc(datetime_convert()),
					dbesc($url)
				);

				$j = json_decode($x['body'],true);
				if($j['success'] && $j['messages']) {
					$sys = get_sys_channel();
					foreach($j['messages'] as $message) {
						$results = process_delivery(array('hash' => 'undefined'), get_item_elements($message),
							array(array('hash' => $sys['xchan_hash'])), false, true);
						$total ++;
					}
					logger('externals: import_public_posts: ' . $total . ' messages imported', LOGGER_DEBUG);
				}
			}
		}
	}
}

if (array_search(__file__,get_included_files())===0){
  externals_run($argv,$argc);
  killme();
}