[Scrutinizer] Fix undeclared variables in src/Protocol/

- Use dba::selectFirst to remove intermediate variables `$r`
- Remove unused variable `$sender` in Protocol\Email
- Simplify Protocol\OStatus:fetchAuthor cascading queries
This commit is contained in:
Hypolite Petovan 2018-02-13 23:58:46 -05:00
parent 2234bb92ae
commit d8e9ed5ff8
6 changed files with 78 additions and 98 deletions

View file

@ -930,6 +930,7 @@ class DFRN
// Remove the abstract element. It is only locally important.
$body = BBCode::stripAbstract($body);
$htmlbody = '';
if ($type == 'html') {
$htmlbody = $body;
@ -1432,19 +1433,17 @@ class DFRN
$author["name"] = $xpath->evaluate($element."/atom:name/text()", $context)->item(0)->nodeValue;
$author["link"] = $xpath->evaluate($element."/atom:uri/text()", $context)->item(0)->nodeValue;
$r = q(
"SELECT `id`, `uid`, `url`, `network`, `avatar-date`, `name-date`, `uri-date`, `addr`,
$contact_old = dba::fetch_first("SELECT `id`, `uid`, `url`, `network`, `avatar-date`, `name-date`, `uri-date`, `addr`,
`name`, `nick`, `about`, `location`, `keywords`, `xmpp`, `bdyear`, `bd`, `hidden`, `contact-type`
FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
intval($importer["uid"]),
dbesc(normalise_link($author["link"])),
dbesc(NETWORK_STATUSNET)
FROM `contact` WHERE `uid` = ? AND `nurl` = ? AND `network` != ?",
$importer["uid"],
normalise_link($author["link"]),
NETWORK_STATUSNET
);
if (DBM::is_result($r)) {
$contact = $r[0];
$author["contact-id"] = $r[0]["id"];
$author["network"] = $r[0]["network"];
if (DBM::is_result($contact_old)) {
$author["contact-id"] = $contact_old["id"];
$author["network"] = $contact_old["network"];
} else {
if (!$onlyfetch) {
logger("Contact ".$author["link"]." wasn't found for user ".$importer["uid"]." XML: ".$xml, LOGGER_DEBUG);
@ -1471,7 +1470,7 @@ class DFRN
$width = $attributes->textContent;
}
if ($attributes->name == "updated") {
$contact["avatar-date"] = $attributes->textContent;
$contact_old["avatar-date"] = $attributes->textContent;
}
}
if (($width > 0) && ($href != "")) {
@ -1483,10 +1482,10 @@ class DFRN
$author["avatar"] = current($avatarlist);
}
if (DBM::is_result($r) && !$onlyfetch) {
logger("Check if contact details for contact " . $r[0]["id"] . " (" . $r[0]["nick"] . ") have to be updated.", LOGGER_DEBUG);
if (DBM::is_result($contact_old) && !$onlyfetch) {
logger("Check if contact details for contact " . $contact_old["id"] . " (" . $contact_old["nick"] . ") have to be updated.", LOGGER_DEBUG);
$poco = ["url" => $contact["url"]];
$poco = ["url" => $contact_old["url"]];
// When was the last change to name or uri?
$name_element = $xpath->query($element . "/atom:name", $context)->item(0);
@ -1545,12 +1544,12 @@ class DFRN
// If the "hide" element is present then the profile isn't searchable.
$hide = intval($xpath->evaluate($element . "/dfrn:hide/text()", $context)->item(0)->nodeValue == "true");
logger("Hidden status for contact " . $contact["url"] . ": " . $hide, LOGGER_DEBUG);
logger("Hidden status for contact " . $contact_old["url"] . ": " . $hide, LOGGER_DEBUG);
// If the contact isn't searchable then set the contact to "hidden".
// Problem: This can be manually overridden by the user.
if ($hide) {
$contact["hidden"] = true;
$contact_old["hidden"] = true;
}
// Save the keywords into the contact table
@ -1565,8 +1564,6 @@ class DFRN
}
// "dfrn:birthday" contains the birthday converted to UTC
$old_bdyear = $contact["bdyear"];
$birthday = $xpath->evaluate($element . "/dfrn:birthday/text()", $context)->item(0)->nodeValue;
if (strtotime($birthday) > time()) {
@ -1590,15 +1587,15 @@ class DFRN
$poco["bd"] = $value;
}
$contact = array_merge($contact, $poco);
$contact = array_merge($contact_old, $poco);
if ($old_bdyear != $contact["bdyear"]) {
if ($contact_old["bdyear"] != $contact["bdyear"]) {
self::birthdayEvent($contact, $birthday);
}
// Get all field names
$fields = [];
foreach ($r[0] as $field => $data) {
foreach ($contact_old as $field => $data) {
$fields[$field] = $data;
}
@ -1609,18 +1606,19 @@ class DFRN
unset($fields["name-date"]);
unset($fields["uri-date"]);
$update = false;
// Update check for this field has to be done differently
$datefields = ["name-date", "uri-date"];
foreach ($datefields as $field) {
if (strtotime($contact[$field]) > strtotime($r[0][$field])) {
logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $r[0][$field] . "'", LOGGER_DEBUG);
if (strtotime($contact[$field]) > strtotime($contact_old[$field])) {
logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $contact_old[$field] . "'", LOGGER_DEBUG);
$update = true;
}
}
foreach ($fields as $field => $data) {
if ($contact[$field] != $r[0][$field]) {
logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $r[0][$field] . "'", LOGGER_DEBUG);
if ($contact[$field] != $contact_old[$field]) {
logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $contact_old[$field] . "'", LOGGER_DEBUG);
$update = true;
}
}
@ -1645,7 +1643,7 @@ class DFRN
$author["avatar"],
$importer["uid"],
$contact["id"],
(strtotime($contact["avatar-date"]) > strtotime($r[0]["avatar-date"]))
(strtotime($contact["avatar-date"]) > strtotime($contact_old["avatar-date"]))
);
/*
@ -2404,14 +2402,12 @@ class DFRN
$item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue;
$current = q(
"SELECT `id`, `uid`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
dbesc($item["uri"]),
intval($importer["importer_uid"])
$current = dba::selectFirst('item',
['id', 'uid', 'edited', 'body'],
['uri' => $item["uri"], 'uid' => $importer["importer_uid"]]
);
// Is there an existing item?
if (DBM::is_result($current) && !self::isEditedTimestampNewer($current[0], $item)) {
if (DBM::is_result($current) && !self::isEditedTimestampNewer($current, $item)) {
logger("Item ".$item["uri"]." (".$item['edited'].") already existed.", LOGGER_DEBUG);
return;
}
@ -2652,7 +2648,7 @@ class DFRN
// Update content if 'updated' changes
if (DBM::is_result($current)) {
if (self::updateContent($r[0], $item, $importer, $entrytype)) {
if (self::updateContent($current, $item, $importer, $entrytype)) {
logger("Item ".$item["uri"]." was updated.", LOGGER_DEBUG);
} else {
logger("Item ".$item["uri"]." already existed.", LOGGER_DEBUG);
@ -2731,7 +2727,7 @@ class DFRN
private static function processDeletion($xpath, $deletion, $importer)
{
logger("Processing deletions");
$uri = null;
foreach ($deletion->attributes as $attributes) {
if ($attributes->name == "ref") {
$uri = $attributes->textContent;

View file

@ -355,6 +355,9 @@ class Diaspora
}
$children = $basedom->children('https://joindiaspora.com/protocol');
$inner_aes_key = null;
$inner_iv = null;
if ($children->header) {
$public = true;
$author_link = str_replace('acct:', '', $children->header->author_id);
@ -393,6 +396,7 @@ class Diaspora
// figure out where in the DOM tree our data is hiding
$base = null;
if ($dom->provenance->data) {
$base = $dom->provenance;
} elseif ($dom->env->data) {
@ -638,8 +642,6 @@ class Diaspora
return false;
}
$first_child = $data->getName();
// Is this the new or the old version?
if ($data->getName() == "XML") {
$oldXML = true;
@ -668,6 +670,8 @@ class Diaspora
$fields = new SimpleXMLElement("<".$type."/>");
$signed_data = "";
$author_signature = null;
$parent_author_signature = null;
foreach ($element->children() as $fieldname => $entry) {
if ($oldXML) {
@ -796,14 +800,11 @@ class Diaspora
*/
public static function personByHandle($handle)
{
$r = q(
"SELECT * FROM `fcontact` WHERE `network` = '%s' AND `addr` = '%s' LIMIT 1",
dbesc(NETWORK_DIASPORA),
dbesc($handle)
);
if ($r) {
$person = $r[0];
logger("In cache " . print_r($r, true), LOGGER_DEBUG);
$update = false;
$person = dba::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]);
if (DBM::is_result($person)) {
logger("In cache " . print_r($person, true), LOGGER_DEBUG);
// update record occasionally so it doesn't get stale
$d = strtotime($person["updated"]." +00:00");
@ -816,7 +817,7 @@ class Diaspora
}
}
if (!$person || $update) {
if (!DBM::is_result($person) || $update) {
logger("create or refresh", LOGGER_DEBUG);
$r = Probe::uri($handle, NETWORK_DIASPORA);
@ -827,6 +828,7 @@ class Diaspora
$person = $r;
}
}
return $person;
}
@ -3741,6 +3743,7 @@ class Diaspora
$parent = $p[0];
$target_type = ($parent["uri"] === $parent["parent-uri"] ? "Post" : "Comment");
$positive = null;
if ($item['verb'] === ACTIVITY_LIKE) {
$positive = "true";
} elseif ($item['verb'] === ACTIVITY_DISLIKE) {
@ -4181,6 +4184,10 @@ class Diaspora
$small = System::baseUrl().'/photo/custom/50/' .$profile['uid'].'.jpg';
$searchable = (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false');
$dob = null;
$about = null;
$location = null;
$tags = null;
if ($searchable === 'true') {
$dob = '';

View file

@ -617,12 +617,7 @@ class Email
$currline = $arrbody[$i];
while ($previousquote < $quotelevel) {
if ($sender != '') {
$quote = "[quote title=$sender]";
$sender = '';
} else
$quote = "[quote]";
$arrbody[$i] = $quote.$arrbody[$i];
$previousquote++;
}

View file

@ -68,6 +68,7 @@ class Feed {
$xpath->registerNamespace('poco', NAMESPACE_POCO);
$author = [];
$entries = null;
// Is it RDF?
if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
@ -369,9 +370,8 @@ class Feed {
$item["title"] = '';
}
$preview = '';
if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
$preview = "";
// Handle enclosures and treat them as preview picture
foreach ($attachments AS $attachment) {
if ($attachment["type"] == "image/jpeg") {
@ -410,6 +410,7 @@ class Feed {
if (!empty($tags)) {
$item["tag"] = $tags;
} else {
// @todo $preview is never set in this case, is it intended? - @MrPetovan 2018-02-13
$item["tag"] = add_page_keywords($item["plink"], $preview, true, $contact["ffi_keyword_blacklist"]);
}
$item["body"] .= "\n".$item['tag'];

View file

@ -71,55 +71,34 @@ class OStatus
}
$author["contact-id"] = $contact["id"];
$found = false;
$contact = null;
if ($aliaslink != '') {
$condition = ["`uid` = ? AND `alias` = ? AND `network` != ?",
$importer["uid"], $aliaslink, NETWORK_STATUSNET];
$r = dba::selectFirst('contact', [], $condition);
if (DBM::is_result($r)) {
$found = true;
if ($r['blocked']) {
$r['id'] = -1;
}
$contact = $r;
$author["contact-id"] = $r["id"];
}
$contact = dba::selectFirst('contact', [], $condition);
}
if (!$found && ($author["author-link"] != "")) {
if (!DBM::is_result($contact) && $author["author-link"] != '') {
if ($aliaslink == "") {
$aliaslink = $author["author-link"];
}
$condition = ["`uid` = ? AND `nurl` IN (?, ?) AND `network` != ?", $importer["uid"],
normalise_link($author["author-link"]), normalise_link($aliaslink), NETWORK_STATUSNET];
$r = dba::selectFirst('contact', [], $condition);
if (DBM::is_result($r)) {
$found = true;
if ($r['blocked']) {
$r['id'] = -1;
}
$contact = $r;
$author["contact-id"] = $r["id"];
}
$contact = dba::selectFirst('contact', [], $condition);
}
if (!$found && ($addr != "")) {
if (!DBM::is_result($contact) && ($addr != '')) {
$condition = ["`uid` = ? AND `addr` = ? AND `network` != ?",
$importer["uid"], $addr, NETWORK_STATUSNET];
$r = dba::selectFirst('contact', [], $condition);
$contact = dba::selectFirst('contact', [], $condition);
}
if (DBM::is_result($r)) {
$found = true;
if ($r['blocked']) {
$r['id'] = -1;
}
$contact = $r;
$author["contact-id"] = $r["id"];
if (DBM::is_result($contact)) {
if ($contact['blocked']) {
$contact['id'] = -1;
}
$author["contact-id"] = $contact["id"];
}
$avatarlist = [];
@ -154,7 +133,7 @@ class OStatus
$author["owner-avatar"] = $author["author-avatar"];
// Only update the contacts if it is an OStatus contact
if ($r && ($r['id'] > 0) && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
if (DBM::is_result($contact) && ($contact['id'] > 0) && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
// Update contact data
$current = $contact;
@ -738,6 +717,7 @@ class OStatus
$links = $xpath->query('//link');
if ($links) {
$file = '';
foreach ($links as $link) {
$attribute = self::readAttributes($link);
if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
@ -1251,6 +1231,7 @@ class OStatus
$root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
$root->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON);
$title = '';
switch ($filter) {
case 'activity': $title = L10n::t('%s\'s timeline', $owner['name']); break;
case 'posts' : $title = L10n::t('%s\'s posts' , $owner['name']); break;
@ -1385,10 +1366,7 @@ class OStatus
*/
private static function addAuthor($doc, $owner)
{
$r = q("SELECT `homepage`, `publish` FROM `profile` WHERE `uid` = %d AND `is-default` LIMIT 1", intval($owner["uid"]));
if (DBM::is_result($r)) {
$profile = $r[0];
}
$profile = dba::selectFirst('profile', ['homepage', 'publish'], ['uid' => $owner['uid'], 'is-default' => true]);
$author = $doc->createElement("author");
XML::addElement($doc, $author, "id", $owner["url"]);
XML::addElement($doc, $author, "activity:object-type", ACTIVITY_OBJ_PERSON);
@ -1428,6 +1406,7 @@ class OStatus
$author->appendChild($element);
}
if (DBM::is_result($profile)) {
if (trim($profile["homepage"]) != "") {
$urls = $doc->createElement("poco:urls");
XML::addElement($doc, $urls, "poco:type", "homepage");
@ -1436,7 +1415,6 @@ class OStatus
$author->appendChild($urls);
}
if (count($profile)) {
XML::addElement($doc, $author, "followers", "", ["url" => System::baseUrl()."/viewcontacts/".$owner["nick"]]);
XML::addElement($doc, $author, "statusnet:profile_info", "", ["local_id" => $owner["uid"]]);
}
@ -1495,6 +1473,8 @@ class OStatus
*/
private static function entry($doc, $item, $owner, $toplevel = false)
{
$xml = null;
$repeated_guid = self::getResharedGuid($item);
if ($repeated_guid != "") {
$xml = self::reshareEntry($doc, $item, $owner, $repeated_guid, $toplevel);

View file

@ -317,6 +317,7 @@ class PortableContact
$contact['created'] = DateTimeFormat::utcNow();
}
$server_url = '';
if ($force) {
$server_url = normalise_link(self::detectServer($profile));
}