X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FWorker%2FOnePoll.php;h=8d2e6f2b0b1b5b43ca2c880bbb7b78ada342aa0e;hb=5c63de1692c5be4ca457e03fc037683909a44348;hp=9cb33c471b0c68cacf4d8ae3d150dfff258161e7;hpb=86922c4821d3e9ab2bb5738e82518e981637c1b1;p=friendica.git diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 9cb33c471b..8d2e6f2b0b 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -4,25 +4,29 @@ */ namespace Friendica\Worker; +use Friendica\BaseObject; +use Friendica\Content\Text\BBCode; use Friendica\Core\Config; use Friendica\Core\PConfig; +use Friendica\Database\dba; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Item; use Friendica\Protocol\Email; use Friendica\Protocol\PortableContact; -use dba; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\Network; +use Friendica\Util\XML; require_once 'include/dba.php'; -require_once 'include/follow.php'; -Class OnePoll +class OnePoll { - public static function execute($contact_id = 0, $command = '') { - global $a; + public static function execute($contact_id = 0, $command = '') + { + $a = BaseObject::getApp(); - require_once 'include/datetime.php'; require_once 'include/items.php'; - require_once 'include/queue_fn.php'; logger('start'); @@ -41,9 +45,9 @@ Class OnePoll return; } - $d = datetime_convert(); + $d = DateTimeFormat::utcNow(); - $contact = dba::select('contact', [], ['id' => $contact_id], ['limit' => 1]); + $contact = dba::selectFirst('contact', [], ['id' => $contact_id]); if (!DBM::is_result($contact)) { logger('Contact not found or cannot be used.'); return; @@ -67,7 +71,7 @@ Class OnePoll // 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(); + $updated = DateTimeFormat::utcNow(); if ($last_updated) { logger('Contact '.$contact['id'].' had last update on '.$last_updated, LOGGER_DEBUG); @@ -76,11 +80,11 @@ Class OnePoll $last_updated = $contact['last-item']; } - $fields = array('last-item' => $last_updated, 'last-update' => $updated, 'success_update' => $updated); + $fields = ['last-item' => DateTimeFormat::utc($last_updated), 'last-update' => $updated, 'success_update' => $updated]; self::updateContact($contact, $fields); Contact::unmarkForArchival($contact); } else { - self::updateContact($contact, array('last-update' => $updated, 'failure_update' => $updated)); + self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]); Contact::markForArchival($contact); logger('Contact '.$contact['id'].' is marked for archival', LOGGER_DEBUG); } @@ -96,7 +100,7 @@ Class OnePoll $contact['priority'] = intval($poll_interval); $hub_update = false; - if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) { + if (DateTimeFormat::utcNow() > DateTimeFormat::utc($t . " + 1 day")) { $hub_update = true; } } else { @@ -104,8 +108,8 @@ Class OnePoll } $last_update = (($contact['last-update'] <= NULL_DATE) - ? datetime_convert('UTC', 'UTC', 'now - 7 days', ATOM_TIME) - : datetime_convert('UTC', 'UTC', $contact['last-update'], ATOM_TIME) + ? DateTimeFormat::utc('now - 7 days', DateTimeFormat::ATOM) + : DateTimeFormat::utc($contact['last-update'], DateTimeFormat::ATOM) ); // Update the contact entry @@ -114,16 +118,16 @@ Class OnePoll 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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); return; } - if (!update_contact($contact["id"])) { + 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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); return; } else { Contact::unmarkForArchival($contact); @@ -134,7 +138,7 @@ Class OnePoll logger('Ignore public contacts'); // set the last-update so we don't keep polling - dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); return; } @@ -146,11 +150,12 @@ Class OnePoll 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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); return; } $importer = $r[0]; + $url = ''; logger("poll: ({$contact['network']}-{$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}"); @@ -169,8 +174,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 @@ -178,11 +183,11 @@ Class OnePoll . '&type=data&last_update=' . $last_update . '&perm=' . $perm ; - $ret = z_fetch_url($url); + $ret = Network::curl($url); - if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { + if (!$ret["success"] && ($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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); Contact::markForArchival($contact); return; } @@ -204,7 +209,7 @@ Class OnePoll Contact::markForArchival($contact); // set the last-update so we don't keep polling - $fields = array('last-update' => datetime_convert(), 'failure_update' => datetime_convert()); + $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()]; self::updateContact($contact, $fields); return; } @@ -214,20 +219,20 @@ Class OnePoll Contact::markForArchival($contact); - $fields = array('last-update' => datetime_convert(), 'failure_update' => datetime_convert()); + $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()]; self::updateContact($contact, $fields); return; } - $res = parse_xml_string($handshake_xml); + $res = XML::parseString($handshake_xml); if (intval($res->status) == 1) { 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()); + $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()]; self::updateContact($contact, $fields); Contact::markForArchival($contact); @@ -238,16 +243,16 @@ Class OnePoll 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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['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); @@ -273,7 +278,7 @@ Class OnePoll 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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); Contact::markForArchival($contact); return; } @@ -282,7 +287,7 @@ Class OnePoll $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION; $postvars['perm'] = 'rw'; - $xml = post_url($contact['poll'], $postvars); + $xml = Network::post($contact['poll'], $postvars); } elseif (($contact['network'] === NETWORK_OSTATUS) || ($contact['network'] === NETWORK_DIASPORA) @@ -300,25 +305,25 @@ 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']) { + if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked']) { // set the last-update so we don't keep polling - dba::update('contact', ['last-update' => datetime_convert()], ['id' => $contact['id']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); return; } $cookiejar = tempnam(get_temppath(), 'cookiejar-onepoll-'); - $ret = z_fetch_url($contact['poll'], false, $redirects, array('cookiejar' => $cookiejar)); + $ret = Network::curl($contact['poll'], false, $redirects, ['cookiejar' => $cookiejar]); unlink($cookiejar); - if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { + if (!$ret["success"] && ($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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); Contact::markForArchival($contact); return; } @@ -332,7 +337,7 @@ Class OnePoll $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']]); + dba::update('contact', ['last-update' => DateTimeFormat::utcNow()], ['id' => $contact['id']]); Contact::markForArchival($contact); return; } @@ -340,20 +345,20 @@ Class OnePoll 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)) { + $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']); + 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' => DateTimeFormat::utcNow()]; + 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)); @@ -375,26 +380,26 @@ Class OnePoll 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; + $datarray['network'] = NETWORK_MAIL; // $meta = Email::messageMeta($mbox, $msg_uid); $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 = Item::selectFirst($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' => DateTimeFormat::utcNow()]; + Item::update($fields, ['id' => $item['id']]); } switch ($mailconf['action']) { @@ -432,15 +437,13 @@ Class OnePoll $refs_arr = explode(' ', $raw_refs); if (count($refs_arr)) { for ($x = 0; $x < count($refs_arr); $x ++) { - $refs_arr[$x] = "'" . Email::msgid2iri(str_replace(array('<', '>', ' '),array('', '', ''),dbesc($refs_arr[$x]))) . "'"; + $refs_arr[$x] = Email::msgid2iri(str_replace(['<', '>', ' '],['', '', ''], $refs_arr[$x])); } } - $qstr = implode(',', $refs_arr); - $r = q("SELECT `parent-uri` FROM `item` USE INDEX (`uid_uri`) WHERE `uri` IN ($qstr) AND `uid` = %d LIMIT 1", - intval($importer_uid) - ); - if (DBM::is_result($r)) { - $datarray['parent-uri'] = $r[0]['parent-uri']; // Set the parent as the top-level item + $condition = ['uri' => $refs_arr, 'uid' => $importer_uid]; + $parent = Item::selectFirst(['parent-uri'], $condition); + if (DBM::is_result($parent)) { + $datarray['parent-uri'] = $parent['parent-uri']; // Set the parent as the top-level item } } @@ -457,7 +460,7 @@ Class OnePoll $datarray['title'] = notags(trim($datarray['title'])); //$datarray['title'] = notags(trim($meta->subject)); - $datarray['created'] = datetime_convert('UTC', 'UTC', $meta->date); + $datarray['created'] = DateTimeFormat::utc($meta->date); // Is it a reply? $reply = ((substr(strtolower($datarray['title']), 0, 3) == "re:") || @@ -469,12 +472,11 @@ Class OnePoll // If it seems to be a reply but a header couldn't be found take the last message with matching subject if (empty($datarray['parent-uri']) && $reply) { - $r = q("SELECT `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d AND `network` = '%s' ORDER BY `created` DESC LIMIT 1", - dbesc(protect_sprintf($datarray['title'])), - intval($importer_uid), - dbesc(NETWORK_MAIL)); - if (DBM::is_result($r)) { - $datarray['parent-uri'] = $r[0]['parent-uri']; + $condition = ['title' => $datarray['title'], 'uid' => importer_uid, 'network' => NETWORK_MAIL]; + $params = ['order' => ['created' => true]]; + $parent = Item::selectFirst(['parent-uri'], $condition, $params); + if (DBM::is_result($parent)) { + $datarray['parent-uri'] = $parent['parent-uri']; } } @@ -488,7 +490,7 @@ Class OnePoll continue; } $datarray['body'] = escape_tags($r['body']); - $datarray['body'] = limit_body_size($datarray['body']); + $datarray['body'] = BBCode::limitBodySize($datarray['body']); logger("Mail: Importing ".$msg_uid." for ".$mailconf['user']); @@ -532,12 +534,7 @@ Class OnePoll $datarray['allow_cid'] = '<' . $contact['id'] . '>'; } - $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)); + $stored_item = Item::insert($datarray); switch ($mailconf['action']) { case 0: @@ -575,7 +572,7 @@ Class OnePoll if (!strstr($xml, '<')) { logger('post_handshake: response from ' . $url . ' did not contain XML.'); - $fields = array('last-update' => datetime_convert(), 'failure_update' => datetime_convert()); + $fields = ['last-update' => DateTimeFormat::utcNow(), 'failure_update' => DateTimeFormat::utcNow()]; self::updateContact($contact, $fields); Contact::markForArchival($contact); return; @@ -584,14 +581,15 @@ Class OnePoll logger("Consume feed of contact ".$contact['id']); - consume_feed($xml, $importer, $contact, $hub, 1, 1); + consume_feed($xml, $importer, $contact, $hub); - // do it twice. Ensures that children of parents which may be later in the stream aren't tossed - - consume_feed($xml, $importer, $contact, $hub, 1, 2); + // do it a second time for DFRN so that any children find their parents. + if ($contact['network'] === NETWORK_DFRN) { + consume_feed($xml, $importer, $contact, $hub); + } $hubmode = 'subscribe'; - if ($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly']) { + if ($contact['network'] === NETWORK_DFRN || $contact['blocked']) { $hubmode = 'unsubscribe'; } @@ -619,27 +617,28 @@ Class OnePoll } } - $updated = datetime_convert(); + $updated = DateTimeFormat::utcNow(); - self::updateContact($contact, array('last-update' => $updated, 'success_update' => $updated)); - dba::update('gcontact', array('last_contact' => $updated), array('nurl' => $contact['nurl'])); + 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"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_FEED))) { - $updated = datetime_convert(); + } elseif (in_array($contact["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, NETWORK_FEED])) { + $updated = DateTimeFormat::utcNow(); - self::updateContact($contact, array('last-update' => $updated, 'failure_update' => $updated)); - 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 { - $updated = datetime_convert(); - dba::update('contact', array('last-update' => $updated), array('id' => $contact['id'])); + $updated = DateTimeFormat::utcNow(); + 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:"))) { + private static function RemoveReply($subject) + { + while (in_array(strtolower(substr($subject, 0, 3)), ["re:", "aw:"])) { $subject = trim(substr($subject, 4)); } @@ -652,8 +651,9 @@ Class OnePoll * @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, array('id' => $contact['id'])); - dba::update('contact', $fields, array('uid' => 0, 'nurl' => $contact['nurl'])); + private static function updateContact($contact, $fields) + { + dba::update('contact', $fields, ['id' => $contact['id']]); + dba::update('contact', $fields, ['uid' => 0, 'nurl' => $contact['nurl']]); } }