4 require_once('boot.php');
8 @include('.htconfig.php');
9 require_once('dba.php');
10 $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
11 unset($db_host, $db_user, $db_pass, $db_data);
13 require_once('session.php');
14 require_once('datetime.php');
15 require_once('simplepie/simplepie.inc');
16 require_once('include/items.php');
21 $a->set_baseurl($argv[1]);
23 $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
25 if(! count($contacts))
28 foreach($contacts as $contact) {
30 if($contact['priority']) {
33 $t = $contact['last-update'];
35 switch ($contact['priority']) {
37 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month"))
41 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week"))
45 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day"))
49 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour"))
54 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour"))
63 $importer_uid = $contact['uid'];
65 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
73 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
74 ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
75 : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
77 $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
79 $xml = fetch_url($url);
85 $res = simplexml_load_string($xml);
87 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
92 $sent_dfrn_id = hex2bin($res->dfrn_id);
95 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
96 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
97 if($final_dfrn_id != $contact['dfrn-id']) {
98 // did not decode properly - cannot trust this site
102 $postvars['dfrn_id'] = $contact['dfrn-id'];
103 $challenge = hex2bin($res->challenge);
105 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
107 $xml = post_url($contact['poll'],$postvars);
109 echo "XML response:" . $xml . "\r\n";
110 echo "Length:" . strlen($xml) . "\r\n";
113 // an empty response may mean there's nothing new - record the fact that we checked
114 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
115 dbesc(datetime_convert()),
116 intval($contact['id'])
121 $feed = new SimplePie();
122 $feed->set_raw_data($xml);
123 $feed->enable_order_by_date(false);
126 $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
127 if($photo_rawupdate) {
128 $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
129 $photo_url = $feed->get_image_url();
130 if(strlen($photo_url) && $photo_timestamp > $contact['avatar-date']) {
132 require_once("Photo.php");
134 $photo_failure = false;
136 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
137 intval($contact['id']),
138 intval($contact['uid'])
141 $resource_id = $r[0]['resource-id'];
142 $img_str = fetch_url($photo_url,true);
143 $img = new Photo($img_str);
145 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
147 intval($contact['id']),
148 intval($contact['uid'])
151 $img->scaleImageSquare(175);
153 $hash = $resource_id;
155 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
157 $img->scaleImage(80);
158 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
160 q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
161 dbesc(datetime_convert()),
162 intval($contact['uid']),
163 intval($contact['id'])
171 foreach($feed->get_items() as $item) {
175 $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
176 if(isset($rawdelete[0]['attribs']['']['ref'])) {
177 $uri = $rawthread[0]['attribs']['']['ref'];
179 if(isset($rawdelete[0]['attribs']['']['when'])) {
180 $when = $rawthread[0]['attribs']['']['when'];
181 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
184 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
187 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
189 intval($importer['uid'])
192 if($r[0]['uri'] == $r[0]['parent-uri']) {
193 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
194 `body` = '', `title` = ''
195 WHERE `parent-uri` = '%s'",
201 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s',
202 `body` = '', `title` = ''
203 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
206 intval($importer['uid'])
215 $item_id = $item->get_id();
216 $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
217 if(isset($rawthread[0]['attribs']['']['ref'])) {
219 $parent_uri = $rawthread[0]['attribs']['']['ref'];
225 // Have we seen it? If not, import it.
227 $item_id = $item->get_id();
229 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
231 intval($importer['uid'])
233 // FIXME update content if 'updated' changes
235 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
236 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
237 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
238 intval($allow[0]['data']),
240 intval($importer['uid'])
245 $datarray = get_atom_elements($item);
246 $datarray['parent-uri'] = $parent_uri;
247 $datarray['uid'] = $importer['uid'];
248 $datarray['contact-id'] = $contact['id'];
249 $r = post_remote($a,$datarray);
254 // Head post of a conversation. Have we seen it? If not, import it.
256 $item_id = $item->get_id();
257 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
259 intval($importer['uid'])
262 $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
263 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
264 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
265 intval($allow[0]['data']),
267 intval($importer['uid'])
273 $datarray = get_atom_elements($item);
274 $datarray['parent-uri'] = $item_id;
275 $datarray['uid'] = $importer['uid'];
276 $datarray['contact-id'] = $contact['id'];
277 $r = post_remote($a,$datarray);
284 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
285 dbesc(datetime_convert()),
286 intval($contact['id'])