- $feed = new SimplePie();
- $feed->set_raw_data($xml);
- $feed->enable_order_by_date(false);
- $feed->init();
-
- $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
- if($photo_rawupdate) {
- $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
- $photo_url = $feed->get_image_url();
- if(strlen($photo_url) && $photo_timestamp > $contact['avatar-date']) {
-
- require_once("Photo.php");
-
- $photo_failure = false;
-
- $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
- intval($contact['id']),
- intval($contact['uid'])
- );
- if(count($r)) {
- $resource_id = $r[0]['resource-id'];
- $img_str = fetch_url($photo_url,true);
- $img = new Photo($img_str);
- if($img) {
- q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
- dbesc($resource_id),
- intval($contact['id']),
- intval($contact['uid'])
- );
-
- $img->scaleImageSquare(175);
-
- $hash = $resource_id;
-
- $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
-
- $img->scaleImage(80);
- $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
- if($r)
- q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
- dbesc(datetime_convert()),
- intval($contact['uid']),
- intval($contact['id'])
- );
+ $importer = $r[0];
+
+ logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
+
+ $last_update = (($contact['last-update'] === '0000-00-00 00:00:00')
+ ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
+ : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
+ );
+
+ if($contact['network'] === NETWORK_DFRN) {
+
+ $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
+
+ if(intval($contact['duplex']) && $contact['dfrn-id'])
+ $idtosend = '0:' . $orig_id;
+ if(intval($contact['duplex']) && $contact['issued-id'])
+ $idtosend = '1:' . $orig_id;
+
+ // they have permission to write to us. We already filtered this in the contact query.
+ $perm = 'rw';
+
+ $url = $contact['poll'] . '?dfrn_id=' . $idtosend
+ . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
+ . '&type=data&last_update=' . $last_update
+ . '&perm=' . $perm ;
+
+ $handshake_xml = fetch_url($url);
+
+ logger('poller: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
+
+
+ if(! $handshake_xml) {
+ logger("poller: $url appears to be dead - marking for death ");
+ // dead connection - might be a transient event, or this might
+ // mean the software was uninstalled or the domain expired.
+ // Will keep trying for one month.
+ mark_for_death($contact);
+
+ // set the last-update so we don't keep polling
+
+ $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
+ dbesc(datetime_convert()),
+ intval($contact['id'])
+ );
+
+ continue;
+ }
+
+ if(! strstr($handshake_xml,'<?xml')) {
+ logger('poller: response from ' . $url . ' did not contain XML.');
+ $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
+ dbesc(datetime_convert()),
+ intval($contact['id'])
+ );
+ continue;
+ }
+
+
+ $res = parse_xml_string($handshake_xml);
+
+ if(intval($res->status) == 1) {
+ logger("poller: $url replied status 1 - marking for death ");
+
+ // we may not be friends anymore. Will keep trying for one month.
+ // set the last-update so we don't keep polling
+
+ $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
+ dbesc(datetime_convert()),
+ intval($contact['id'])
+ );
+
+ mark_for_death($contact);
+ }
+ else {
+ if($contact['term-date'] != '0000-00-00 00:00:00') {
+ logger("poller: $url back from the dead - removing mark for death");
+ unmark_for_death($contact);