aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Daemon/Externals.php
blob: da05cad1898ccea6d87674b6afd916881e459924 (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
<?php /** @file */

namespace Zotlabs\Daemon;

use Zotlabs\Lib\Activity;
use Zotlabs\Lib\ActivityStreams;
use Zotlabs\Lib\ASCollection;

require_once('include/channel.php');


class Externals {

	static public function run($argc, $argv) {

		logger('externals: start');

		$importer = get_sys_channel();
		$total    = 0;
		$attempts = 0;

		logger('externals: startup', LOGGER_DEBUG);

		// pull in some public posts

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

			if ($arr['url']) {
				$url = $arr['url'];
			}
			else {
				$randfunc = db_getfunc('RAND');

				// fixme this query does not deal with directory realms.

				$r = q("select site_url, site_pull from site where site_url != '%s'
						and site_flags != %d and site_type = %d
                        and site_dead = 0 and site_project like '%s' and site_version > '5.3.1' order by $randfunc limit 1",
					dbesc(z_root()),
					intval(DIRECTORY_MODE_STANDALONE),
					intval(SITE_TYPE_ZOT),
					dbesc('hubzilla%')
				);
				if ($r)
					$url = $r[0]['site_url'];
			}

			$blacklisted = false;

			if (!check_siteallowed($url)) {
				logger('blacklisted site: ' . $url);
				$blacklisted = true;
			}

			$attempts++;

			// make sure we can eventually break out if somebody blacklists all known sites

			if ($blacklisted) {
				if ($attempts > 20)
					break;
				$attempts--;
				continue;
			}

			if ($url) {

				$max = intval(get_config('system', 'max_imported_posts', 30));
				if (intval($max)) {
					logger('externals: fetching outbox');

					$feed_url = $url . '/zotfeed';
					$obj      = new ASCollection($feed_url, $importer, 0, $max);
					$messages = $obj->get();

					if ($messages) {
						foreach ($messages as $message) {
							if (is_string($message)) {
								$message = Activity::fetch($message, $importer);
							}
							$AS = new ActivityStreams($message);
							if ($AS->is_valid() && is_array($AS->obj)) {
								$item = Activity::decode_note($AS);
								Activity::store($importer, $importer['xchan_hash'], $AS, $item, true);
								$total++;
							}
						}
					}
					logger('externals: import_public_posts: ' . $total . ' messages imported', LOGGER_DEBUG);
				}
			}
		}
		return;

		/*		$total    = 0;
				$attempts = 0;

				logger('externals: startup', LOGGER_DEBUG);

				// pull in some public posts

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

					if ($arr['url']) {
						$url = $arr['url'];
					}
					else {
						$randfunc = db_getfunc('RAND');

						// fixme this query does not deal with directory realms.

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

					$blacklisted = false;

					if (!check_siteallowed($url)) {
						logger('blacklisted site: ' . $url);
						$blacklisted = true;
					}

					$attempts++;

					// make sure we can eventually break out if somebody blacklists all known sites

					if ($blacklisted) {
						if ($attempts > 20)
							break;
						$attempts--;
						continue;
					}

					if ($url) {
						if ($r[0]['site_pull'] > NULL_DATE)
							$mindate = urlencode(datetime_convert('', '', $r[0]['site_pull'] . ' - 1 day'));
						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'",
								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) {
									// on these posts, clear any route info.
									$message['route'] = '';
									process_delivery(['hash' => 'undefined'], get_item_elements($message),
										[['hash' => $sys['xchan_hash']]], false, true);
									$total++;
								}
								logger('externals: import_public_posts: ' . $total . ' messages imported', LOGGER_DEBUG);
							}
						}
					}
				}*/
	}
}