X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FWorker%2FOnePoll.php;h=293cd265e26038be91332ee0896694f3580ce1fc;hb=610f017b28edc37212df6385126993f2dfc41f59;hp=98d1303115044180fb4bdb6c8b8400d8bc8f7158;hpb=b92fc24ff06681f445edff0d45f8f81a7e25ebe6;p=friendica.git diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 98d1303115..293cd265e2 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -7,24 +7,22 @@ namespace Friendica\Worker; use Friendica\Core\Config; use Friendica\Core\PConfig; use Friendica\Database\DBM; -use Friendica\Object\Contact; +use Friendica\Model\Contact; +use Friendica\Protocol\Email; use Friendica\Protocol\PortableContact; use dba; -require_once 'include/follow.php'; +require_once 'include/dba.php'; -Class OnePoll +class OnePoll { public static function execute($contact_id = 0, $command = '') { global $a; require_once 'include/datetime.php'; require_once 'include/items.php'; - require_once 'include/Contact.php'; - require_once 'include/email.php'; - require_once 'include/queue_fn.php'; - logger('onepoll: start'); + logger('start'); $manual_id = 0; $generation = 0; @@ -37,36 +35,18 @@ Class OnePoll } if (!$contact_id) { - logger('onepoll: no contact'); + logger('no contact'); return; } $d = datetime_convert(); - // Only poll from those with suitable relationships, - // and which have a polling address and ignore Diaspora since - // we are unable to match those posts with a Diaspora GUID and prevent duplicates. - - $contacts = q("SELECT `contact`.* FROM `contact` - WHERE (`rel` = %d OR `rel` = %d) AND `poll` != '' - AND NOT `network` IN ('%s', '%s') - AND `contact`.`id` = %d - AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0 - AND `contact`.`archive` = 0 LIMIT 1", - intval(CONTACT_IS_SHARING), - intval(CONTACT_IS_FRIEND), - dbesc(NETWORK_FACEBOOK), - dbesc(NETWORK_PUMPIO), - intval($contact_id) - ); - - if (!count($contacts)) { + $contact = dba::selectFirst('contact', [], ['id' => $contact_id]); + if (!DBM::is_result($contact)) { logger('Contact not found or cannot be used.'); return; } - $contact = $contacts[0]; - $importer_uid = $contact['uid']; // load current friends if possible. @@ -82,18 +62,25 @@ Class OnePoll } } - /// @TODO Check why we don't poll the Diaspora feed at the moment (some guid problem in the items?) - /// @TODO Check whether this is possible with Redmatrix - if ($contact["network"] == NETWORK_DIASPORA) { - if (PortableContact::updateNeeded($contact["created"], $contact["last-item"], $contact["failure_update"], $contact["success_update"])) { - $last_updated = PortableContact::lastUpdated($contact["url"]); - $updated = datetime_convert(); - if ($last_updated) { - $fields = array('last-item' => $last_updated, 'last-update' => $updated, 'success_update' => $updated); - dba::update('contact', $fields, array('id' => $contact['id'])); - } else { - dba::update('contact', array('last-update' => $updated, 'failure_update' => $updated), array('id' => $contact['id'])); + // Diaspora users, archived users and followers are only checked if they still exist. + if ($contact['archive'] || ($contact["network"] == NETWORK_DIASPORA) || ($contact["rel"] == CONTACT_IS_FOLLOWER)) { + $last_updated = PortableContact::lastUpdated($contact["url"], true); + $updated = datetime_convert(); + if ($last_updated) { + logger('Contact '.$contact['id'].' had last update on '.$last_updated, LOGGER_DEBUG); + + // The last public item can be older than the last item we got + if ($last_updated < $contact['last-item']) { + $last_updated = $contact['last-item']; } + + $fields = ['last-item' => $last_updated, 'last-update' => $updated, 'success_update' => $updated]; + self::updateContact($contact, $fields); + Contact::unmarkForArchival($contact); + } else { + self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]); + Contact::markForArchival($contact); + logger('Contact '.$contact['id'].' is marked for archival', LOGGER_DEBUG); } return; } @@ -103,8 +90,8 @@ Class OnePoll $t = $contact['last-update']; if ($contact['subhub']) { - $poll_interval = Config::get('system', 'pushpoll_frequency'); - $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3); + $poll_interval = Config::get('system', 'pushpoll_frequency', 3); + $contact['priority'] = intval($poll_interval); $hub_update = false; if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) { @@ -123,20 +110,29 @@ Class OnePoll if (($contact['network'] === NETWORK_OSTATUS) || ($contact['network'] === NETWORK_DIASPORA) || ($contact['network'] === NETWORK_DFRN)) { if (!PortableContact::reachable($contact['url'])) { logger("Skipping probably dead contact ".$contact['url']); + + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); return; } - if (!update_contact($contact["id"])) { - mark_for_death($contact); + if (!Contact::updateFromProbe($contact["id"])) { + Contact::markForArchival($contact); logger('Contact is marked dead'); + + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); return; } else { - unmark_for_death($contact); + Contact::unmarkForArchival($contact); } } if ($importer_uid == 0) { logger('Ignore public contacts'); + + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); return; } @@ -146,12 +142,15 @@ Class OnePoll if (!DBM::is_result($r)) { logger('No self contact for user '.$importer_uid); + + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); return; } $importer = $r[0]; - logger("onepoll: poll: ({$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}"); + logger("poll: ({$contact['network']}-{$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}"); if ($contact['network'] === NETWORK_DFRN) { $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']); @@ -168,8 +167,8 @@ Class OnePoll // But this may be our first communication, so set the writable flag if it isn't set already. if (!intval($contact['writable'])) { - $fields = array('writable' => true); - dba::update('contact', $fields, array('id' => $contact['id'])); + $fields = ['writable' => true]; + dba::update('contact', $fields, ['id' => $contact['id']]); } $url = $contact['poll'] . '?dfrn_id=' . $idtosend @@ -180,6 +179,9 @@ Class OnePoll $ret = z_fetch_url($url); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); + Contact::markForArchival($contact); return; } @@ -187,33 +189,31 @@ Class OnePoll $html_code = $a->get_curl_code(); - logger('onepoll: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA); + logger('handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA); if (!strlen($handshake_xml) || ($html_code >= 400) || !$html_code) { - logger("poller: $url appears to be dead - marking for death "); + logger("$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); + Contact::markForArchival($contact); // set the last-update so we don't keep polling - $fields = array('last-update' => datetime_convert(), 'failure_update' => datetime_convert()); - dba::update('contact', $fields, array('id' => $contact['id'])); - + $fields = ['last-update' => datetime_convert(), 'failure_update' => datetime_convert()]; + self::updateContact($contact, $fields); return; } if (!strstr($handshake_xml, '<')) { - logger('poller: response from ' . $url . ' did not contain XML.'); + logger('response from ' . $url . ' did not contain XML.'); - mark_for_death($contact); - - $fields = array('last-update' => datetime_convert(), 'failure_update' => datetime_convert()); - dba::update('contact', $fields, array('id' => $contact['id'])); + Contact::markForArchival($contact); + $fields = ['last-update' => datetime_convert(), 'failure_update' => datetime_convert()]; + self::updateContact($contact, $fields); return; } @@ -221,29 +221,31 @@ Class OnePoll $res = parse_xml_string($handshake_xml); if (intval($res->status) == 1) { - logger("poller: $url replied status 1 - marking for death "); + logger("$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 - $fields = array('last-update' => datetime_convert(), 'failure_update' => datetime_convert()); - dba::update('contact', $fields, array('id' => $contact['id'])); + $fields = ['last-update' => datetime_convert(), 'failure_update' => datetime_convert()]; + self::updateContact($contact, $fields); - mark_for_death($contact); + Contact::markForArchival($contact); } elseif ($contact['term-date'] > NULL_DATE) { - logger("poller: $url back from the dead - removing mark for death"); - unmark_for_death($contact); + logger("$url back from the dead - removing mark for death"); + Contact::unmarkForArchival($contact); } if ((intval($res->status) != 0) || !strlen($res->challenge) || !strlen($res->dfrn_id)) { + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); return; } if (((float)$res->dfrn_version > 2.21) && ($contact['poco'] == '')) { - $fields = array('poco' => str_replace('/profile/', '/poco/', $contact['url'])); - dba::update('contact', $fields, array('id' => $contact['id'])); + $fields = ['poco' => str_replace('/profile/', '/poco/', $contact['url'])]; + dba::update('contact', $fields, ['id' => $contact['id']]); } - $postvars = array(); + $postvars = []; $sent_dfrn_id = hex2bin((string) $res->dfrn_id); $challenge = hex2bin((string) $res->challenge); @@ -265,8 +267,12 @@ Class OnePoll } if ($final_dfrn_id != $orig_id) { - logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id); // did not decode properly - cannot trust this site + logger('ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id); + + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); + Contact::markForArchival($contact); return; } @@ -292,52 +298,60 @@ Class OnePoll } if ($stat_writeable != $contact['writable']) { - $fields = array('writable' => $stat_writeable); - dba::update('contact', $fields, array('id' => $contact['id'])); + $fields = ['writable' => $stat_writeable]; + dba::update('contact', $fields, ['id' => $contact['id']]); } // Are we allowed to import from this person? if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly']) { + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); return; } $cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-'); - $ret = z_fetch_url($contact['poll'], false, $redirects, array('cookiejar' => $cookiejar)); + $ret = z_fetch_url($contact['poll'], false, $redirects, ['cookiejar' => $cookiejar]); + unlink($cookiejar); if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); + Contact::markForArchival($contact); return; } $xml = $ret['body']; - unlink($cookiejar); - } elseif ($contact['network'] === NETWORK_MAIL || $contact['network'] === NETWORK_MAIL2) { + } elseif ($contact['network'] === NETWORK_MAIL) { logger("Mail: Fetching for ".$contact['addr'], LOGGER_DEBUG); $mail_disabled = ((function_exists('imap_open') && (! Config::get('system', 'imap_disabled'))) ? 0 : 1); if ($mail_disabled) { + // set the last-update so we don't keep polling + dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); + Contact::markForArchival($contact); return; } logger("Mail: Enabled", LOGGER_DEBUG); $mbox = null; - $x = dba::select('user', array('prvkey'), array('uid' => $importer_uid), array('limit' => 1)); + $user = dba::selectFirst('user', ['prvkey'], ['uid' => $importer_uid]); - $condition = array("`server` != '' AND `uid` = ?", $importer_uid); - $mailconf = dba::select('mailacct', array(), $condition, array('limit' => 1)); - if (DBM::is_result($x) && DBM::is_result($mailconf)) { - $mailbox = construct_mailbox_name($mailconf); + $condition = ["`server` != '' AND `uid` = ?", $importer_uid]; + $mailconf = dba::selectFirst('mailacct', [], $condition); + if (DBM::is_result($user) && DBM::is_result($mailconf)) { + $mailbox = Email::constructMailboxName($mailconf); $password = ''; - openssl_private_decrypt(hex2bin($mailconf['pass']), $password, $x['prvkey']); - $mbox = email_connect($mailbox, $mailconf['user'], $password); + openssl_private_decrypt(hex2bin($mailconf['pass']), $password, $user['prvkey']); + $mbox = Email::connect($mailbox, $mailconf['user'], $password); unset($password); logger("Mail: Connect to " . $mailconf['user']); if ($mbox) { - $fields = array('last_check' => datetime_convert()); - dba::update('mailacct', $fields, array('id' => $mailconf['id'])); + $fields = ['last_check' => datetime_convert()]; + dba::update('mailacct', $fields, ['id' => $mailconf['id']]); logger("Mail: Connected to " . $mailconf['user']); } else { logger("Mail: Connection error ".$mailconf['user']." ".print_r(imap_errors(), true)); @@ -345,41 +359,39 @@ Class OnePoll } if ($mbox) { - $msgs = email_poll($mbox, $contact['addr']); + $msgs = Email::poll($mbox, $contact['addr']); if (count($msgs)) { logger("Mail: Parsing ".count($msgs)." mails from ".$contact['addr']." for ".$mailconf['user'], LOGGER_DEBUG); - $metas = email_msg_meta($mbox,implode(',', $msgs)); + $metas = Email::messageMeta($mbox, implode(',', $msgs)); if (count($metas) != count($msgs)) { - logger("onepoll: for " . $mailconf['user'] . " there are ". count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG); + logger("for " . $mailconf['user'] . " there are ". count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG); } else { $msgs = array_combine($msgs, $metas); foreach ($msgs as $msg_uid => $meta) { logger("Mail: Parsing mail ".$msg_uid, LOGGER_DATA); - $datarray = array(); + $datarray = []; $datarray['verb'] = ACTIVITY_POST; $datarray['object-type'] = ACTIVITY_OBJ_NOTE; - // $meta = email_msg_meta($mbox, $msg_uid); - // $headers = email_msg_headers($mbox, $msg_uid); + // $meta = Email::messageMeta($mbox, $msg_uid); - $datarray['uri'] = msgid2iri(trim($meta->message_id, '<>')); + $datarray['uri'] = Email::msgid2iri(trim($meta->message_id, '<>')); // Have we seen it before? - $fields = array('deleted', 'id'); - $condition = array('uid' => $importer_uid, 'uri' => $datarray['uri']); - $r = dba::select('item', $fields, $condition, array('limit' => 1)); - - if (DBM::is_result($r)) { + $fields = ['deleted', 'id']; + $condition = ['uid' => $importer_uid, 'uri' => $datarray['uri']]; + $item = dba::selectFirst('item', $fields, $condition); + if (DBM::is_result($item)) { logger("Mail: Seen before ".$msg_uid." for ".$mailconf['user']." UID: ".$importer_uid." URI: ".$datarray['uri'],LOGGER_DEBUG); // Only delete when mails aren't automatically moved or deleted if (($mailconf['action'] != 1) && ($mailconf['action'] != 3)) - if ($meta->deleted && ! $r['deleted']) { - $fields = array('deleted' => true, 'changed' => datetime_convert()); - dba::update('item', $fields, array('id' => $r['id'])); + if ($meta->deleted && ! $item['deleted']) { + $fields = ['deleted' => true, 'changed' => datetime_convert()]; + dba::update('item', $fields, ['id' => $item['id']]); } switch ($mailconf['action']) { @@ -417,7 +429,7 @@ Class OnePoll $refs_arr = explode(' ', $raw_refs); if (count($refs_arr)) { for ($x = 0; $x < count($refs_arr); $x ++) { - $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<', '>', ' '),array('', '', ''),dbesc($refs_arr[$x]))) . "'"; + $refs_arr[$x] = "'" . Email::msgid2iri(str_replace(['<', '>', ' '],['', '', ''],dbesc($refs_arr[$x]))) . "'"; } } $qstr = implode(',', $refs_arr); @@ -467,7 +479,7 @@ Class OnePoll $datarray['parent-uri'] = $datarray['uri']; } - $r = email_get_msg($mbox, $msg_uid, $reply); + $r = Email::getMessage($mbox, $msg_uid, $reply); if (!$r) { logger("Mail: can't fetch msg ".$msg_uid." for ".$mailconf['user']); continue; @@ -519,11 +531,6 @@ Class OnePoll $stored_item = item_store($datarray); - $condition = array('parent-uri' => $datarray['parent-uri'], 'uid' => $importer_uid); - dba::update('item', array('last-child' => false), $condition); - - dba::update('item', array('last-child' => true), array('id' => $stored_item)); - switch ($mailconf['action']) { case 0: logger("Mail: Seen before ".$msg_uid." for ".$mailconf['user'].". Doing nothing.", LOGGER_DEBUG); @@ -556,13 +563,13 @@ Class OnePoll } if ($xml) { - logger('poller: received xml : ' . $xml, LOGGER_DATA); + logger('received xml : ' . $xml, LOGGER_DATA); if (!strstr($xml, '<')) { - logger('poller: post_handshake: response from ' . $url . ' did not contain XML.'); - - $fields = array('last-update' => datetime_convert(), 'failure_update' => datetime_convert()); - dba::update('contact', $fields, array('id' => $contact['id'])); + logger('post_handshake: response from ' . $url . ' did not contain XML.'); + $fields = ['last-update' => datetime_convert(), 'failure_update' => datetime_convert()]; + self::updateContact($contact, $fields); + Contact::markForArchival($contact); return; } @@ -591,7 +598,7 @@ Class OnePoll logger("Contact ".$contact['id']." returned hub: ".$hub." Network: ".$contact['network']." Relation: ".$contact['rel']." Update: ".$hub_update); if (strlen($hub) && $hub_update && (($contact['rel'] != CONTACT_IS_FOLLOWER) || $contact['network'] == NETWORK_FEED)) { - logger('poller: hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']); + logger('hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']); $hubs = explode(',', $hub); if (count($hubs)) { foreach ($hubs as $h) { @@ -606,25 +613,39 @@ Class OnePoll $updated = datetime_convert(); - dba::update('contact', array('last-update' => $updated, 'success_update' => $updated), array('id' => $contact['id'])); - dba::update('gcontact', array('last_contact' => $updated), array('nurl' => $contact['nurl'])); - } elseif (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_FEED))) { + self::updateContact($contact, ['last-update' => $updated, 'success_update' => $updated]); + dba::update('gcontact', ['last_contact' => $updated], ['nurl' => $contact['nurl']]); + Contact::unmarkForArchival($contact); + } elseif (in_array($contact["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_FEED])) { $updated = datetime_convert(); - dba::update('contact', array('last-update' => $updated, 'failure_update' => $updated), array('id' => $contact['id'])); - dba::update('gcontact', array('last_failure' => $updated), array('nurl' => $contact['nurl'])); + self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]); + dba::update('gcontact', ['last_failure' => $updated], ['nurl' => $contact['nurl']]); + Contact::markForArchival($contact); } else { - dba::update('contact', array('last-update' => $updated), array('id' => $contact['id'])); + $updated = datetime_convert(); + dba::update('contact', ['last-update' => $updated], ['id' => $contact['id']]); } return; } private static function RemoveReply($subject) { - while (in_array(strtolower(substr($subject, 0, 3)), array("re:", "aw:"))) { + while (in_array(strtolower(substr($subject, 0, 3)), ["re:", "aw:"])) { $subject = trim(substr($subject, 4)); } return $subject; } + + /** + * @brief Updates a personal contact entry and the public contact entry + * + * @param array $contact The personal contact entry + * @param array $fields The fields that are updated + */ + private static function updateContact($contact, $fields) { + dba::update('contact', $fields, ['id' => $contact['id']]); + dba::update('contact', $fields, ['uid' => 0, 'nurl' => $contact['nurl']]); + } }