]> git.mxchange.org Git - friendica.git/commitdiff
Avoid "Trying to get property of non-object"
authorMichael <heluecht@pirati.ca>
Sun, 8 Jul 2018 11:46:05 +0000 (11:46 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 8 Jul 2018 11:46:05 +0000 (11:46 +0000)
src/Model/Item.php
src/Protocol/DFRN.php
src/Protocol/Feed.php
src/Util/XML.php

index 05254b8eb12427d12142eda71f1237ecdd69a8b7..f10c20936a4d04c779233567021e25a9ae870cdd 100644 (file)
@@ -51,7 +51,7 @@ class Item extends BaseObject
                        'attach', 'tag', 'bookmark', 'deleted', 'extid',
                        'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
                        'author-id', 'author-link', 'owner-link', 'contact-uid',
-                       'signed_text', 'signature', 'signer'];
+                       'signed_text', 'signature', 'signer', 'network'];
 
        // Field list for "item-content" table that is mixed with the item table
        const MIXED_CONTENT_FIELDLIST = ['title', 'content-warning', 'body', 'location',
index 6525601feb2590d13e09d9e7760c7d4e26e6c8f5..ddbda750b919e21c1a84368a0d099c6ec566f040 100644 (file)
@@ -1534,8 +1534,8 @@ class DFRN
        private static function fetchauthor($xpath, $context, $importer, $element, $onlyfetch, $xml = "")
        {
                $author = [];
-               $author["name"] = $xpath->evaluate($element."/atom:name/text()", $context)->item(0)->nodeValue;
-               $author["link"] = $xpath->evaluate($element."/atom:uri/text()", $context)->item(0)->nodeValue;
+               $author["name"] = XML::getFirstNodeValue($xpath, $element."/atom:name/text()", $context);
+               $author["link"] = XML::getFirstNodeValue($xpath, $element."/atom:uri/text()", $context);
 
                $fields = ['id', 'uid', 'url', 'network', 'avatar-date', 'avatar', 'name-date', 'uri-date', 'addr',
                        'name', 'nick', 'about', 'location', 'keywords', 'xmpp', 'bdyear', 'bd', 'hidden', 'contact-type'];
@@ -1606,33 +1606,33 @@ class DFRN
                        }
 
                        // Update contact data
-                       $value = $xpath->evaluate($element . "/dfrn:handle/text()", $context)->item(0)->nodeValue;
+                       $value = XML::getFirstNodeValue($xpath, $element . "/dfrn:handle/text()", $context);
                        if ($value != "") {
                                $poco["addr"] = $value;
                        }
 
-                       $value = $xpath->evaluate($element . "/poco:displayName/text()", $context)->item(0)->nodeValue;
+                       $value = XML::getFirstNodeValue($xpath, $element . "/poco:displayName/text()", $context);
                        if ($value != "") {
                                $poco["name"] = $value;
                        }
 
-                       $value = $xpath->evaluate($element . "/poco:preferredUsername/text()", $context)->item(0)->nodeValue;
+                       $value = XML::getFirstNodeValue($xpath, $element . "/poco:preferredUsername/text()", $context);
                        if ($value != "") {
                                $poco["nick"] = $value;
                        }
 
-                       $value = $xpath->evaluate($element . "/poco:note/text()", $context)->item(0)->nodeValue;
+                       $value = XML::getFirstNodeValue($xpath, $element . "/poco:note/text()", $context);
                        if ($value != "") {
                                $poco["about"] = $value;
                        }
 
-                       $value = $xpath->evaluate($element . "/poco:address/poco:formatted/text()", $context)->item(0)->nodeValue;
+                       $value = XML::getFirstNodeValue($xpath, $element . "/poco:address/poco:formatted/text()", $context);
                        if ($value != "") {
                                $poco["location"] = $value;
                        }
 
                        /// @todo Only search for elements with "poco:type" = "xmpp"
-                       $value = $xpath->evaluate($element . "/poco:ims/poco:value/text()", $context)->item(0)->nodeValue;
+                       $value = XML::getFirstNodeValue($xpath, $element . "/poco:ims/poco:value/text()", $context);
                        if ($value != "") {
                                $poco["xmpp"] = $value;
                        }
@@ -1645,7 +1645,7 @@ class DFRN
                        /// - poco:country
 
                        // 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");
+                       $hide = intval(XML::getFirstNodeValue($xpath, $element . "/dfrn:hide/text()", $context) == "true");
 
                        logger("Hidden status for contact " . $contact_old["url"] . ": " . $hide, LOGGER_DEBUG);
 
@@ -1667,7 +1667,7 @@ class DFRN
                        }
 
                        // "dfrn:birthday" contains the birthday converted to UTC
-                       $birthday = $xpath->evaluate($element . "/dfrn:birthday/text()", $context)->item(0)->nodeValue;
+                       $birthday = XML::getFirstNodeValue($xpath, $element . "/poco:birthday/text()", $context);
 
                        if (strtotime($birthday) > time()) {
                                $bd_timestamp = strtotime($birthday);
@@ -1676,7 +1676,7 @@ class DFRN
                        }
 
                        // "poco:birthday" is the birthday in the format "yyyy-mm-dd"
-                       $value = $xpath->evaluate($element . "/poco:birthday/text()", $context)->item(0)->nodeValue;
+                       $value = XML::getFirstNodeValue($xpath, $element . "/poco:birthday/text()", $context);
 
                        if (!in_array($value, ["", "0000-00-00", "0001-01-01"])) {
                                $bdyear = date("Y");
@@ -2410,9 +2410,9 @@ class DFRN
                $item["source"] = $xml;
 
                // Get the uri
-               $item["uri"] = $xpath->query("atom:id/text()", $entry)->item(0)->nodeValue;
+               $item["uri"] = XML::getFirstNodeValue($xpath, "atom:id/text()", $entry);
 
-               $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue;
+               $item["edited"] = XML::getFirstNodeValue($xpath, "atom:updated/text()", $entry);
 
                $current = Item::selectFirst(['id', 'uid', 'edited', 'body'],
                        ['uri' => $item["uri"], 'uid' => $importer["importer_uid"]]
@@ -2435,11 +2435,11 @@ class DFRN
                $item["author-link"] = $author["link"];
                $item["author-id"] = Contact::getIdForURL($author["link"], 0);
 
-               $item["title"] = $xpath->query("atom:title/text()", $entry)->item(0)->nodeValue;
+               $item["title"] = XML::getFirstNodeValue($xpath, "atom:title/text()", $entry);
 
-               $item["created"] = $xpath->query("atom:published/text()", $entry)->item(0)->nodeValue;
+               $item["created"] = XML::getFirstNodeValue($xpath, "atom:published/text()", $entry);
 
-               $item["body"] = $xpath->query("dfrn:env/text()", $entry)->item(0)->nodeValue;
+               $item["body"] = XML::getFirstNodeValue($xpath, "dfrn:env/text()", $entry);
                $item["body"] = str_replace([' ',"\t","\r","\n"], ['','','',''], $item["body"]);
                // make sure nobody is trying to sneak some html tags by us
                $item["body"] = notags(base64url_decode($item["body"]));
@@ -2472,18 +2472,15 @@ class DFRN
                // We don't need the content element since "dfrn:env" is always present
                //$item["body"] = $xpath->query("atom:content/text()", $entry)->item(0)->nodeValue;
 
-               $item["location"] = $xpath->query("dfrn:location/text()", $entry)->item(0)->nodeValue;
+               $item["location"] = XML::getFirstNodeValue($xpath, "dfrn:location/text()", $entry);
 
-               $georsspoint = $xpath->query("georss:point", $entry);
-               if ($georsspoint) {
-                       $item["coord"] = $georsspoint->item(0)->nodeValue;
-               }
+               $item["coord"] = XML::getFirstNodeValue($xpath, "georss:point", $entry);
 
-               $item["private"] = $xpath->query("dfrn:private/text()", $entry)->item(0)->nodeValue;
+               $item["private"] = XML::getFirstNodeValue($xpath, "dfrn:private/text()", $entry);
 
-               $item["extid"] = $xpath->query("dfrn:extid/text()", $entry)->item(0)->nodeValue;
+               $item["extid"] = XML::getFirstNodeValue($xpath, "dfrn:extid/text()", $entry);
 
-               if ($xpath->query("dfrn:bookmark/text()", $entry)->item(0)->nodeValue == "true") {
+               if (XML::getFirstNodeValue($xpath, "dfrn:bookmark/text()", $entry) == "true") {
                        $item["bookmark"] = true;
                }
 
@@ -2496,18 +2493,18 @@ class DFRN
                        }
                }
 
-               $item["guid"] = $xpath->query("dfrn:diaspora_guid/text()", $entry)->item(0)->nodeValue;
+               $item["guid"] = XML::getFirstNodeValue($xpath, "dfrn:diaspora_guid/text()", $entry);
 
                // We store the data from "dfrn:diaspora_signature" in a different table, this is done in "Item::insert"
-               $dsprsig = unxmlify($xpath->query("dfrn:diaspora_signature/text()", $entry)->item(0)->nodeValue);
+               $dsprsig = unxmlify(XML::getFirstNodeValue($xpath, "dfrn:diaspora_signature/text()", $entry));
                if ($dsprsig != "") {
                        $item["dsprsig"] = $dsprsig;
                }
 
-               $item["verb"] = $xpath->query("activity:verb/text()", $entry)->item(0)->nodeValue;
+               $item["verb"] = XML::getFirstNodeValue($xpath, "activity:verb/text()", $entry);
 
-               if ($xpath->query("activity:object-type/text()", $entry)->item(0)->nodeValue != "") {
-                       $item["object-type"] = $xpath->query("activity:object-type/text()", $entry)->item(0)->nodeValue;
+               if (XML::getFirstNodeValue($xpath, "activity:object-type/text()", $entry) != "") {
+                       $item["object-type"] = XML::getFirstNodeValue($xpath, "activity:object-type/text()", $entry);
                }
 
                $object = $xpath->query("activity:object", $entry)->item(0);
@@ -2544,8 +2541,10 @@ class DFRN
                                                $termhash = array_shift($parts);
                                                $termurl = implode(":", $parts);
 
-                                               if (strlen($item["tag"])) {
+                                               if (!empty($item["tag"])) {
                                                        $item["tag"] .= ",";
+                                               } else {
+                                                       $item["tag"] = "";
                                                }
 
                                                $item["tag"] .= $termhash . "[url=" . $termurl . "]" . $term . "[/url]";
@@ -2561,7 +2560,7 @@ class DFRN
                        self::parseLinks($links, $item);
                }
 
-               $item['conversation-uri'] = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
+               $item['conversation-uri'] = XML::getFirstNodeValue($xpath, 'ostatus:conversation/text()', $entry);
 
                $conv = $xpath->query('ostatus:conversation', $entry);
                if (is_object($conv->item(0))) {
@@ -2835,11 +2834,11 @@ class DFRN
                logger("Import DFRN message for user " . $importer["importer_uid"] . " from contact " . $importer["id"], LOGGER_DEBUG);
 
                // is it a public forum? Private forums aren't exposed with this method
-               $forum = intval($xpath->evaluate("/atom:feed/dfrn:community/text()")->item(0)->nodeValue);
+               $forum = intval(XML::getFirstNodeValue($xpath, "/atom:feed/dfrn:community/text()"));
 
                // The account type is new since 3.5.1
                if ($xpath->query("/atom:feed/dfrn:account_type")->length > 0) {
-                       $accounttype = intval($xpath->evaluate("/atom:feed/dfrn:account_type/text()")->item(0)->nodeValue);
+                       $accounttype = intval(XML::getFirstNodeValue($xpath, "/atom:feed/dfrn:account_type/text()"));
 
                        if ($accounttype != $importer["contact-type"]) {
                                dba::update('contact', ['contact-type' => $accounttype], ['id' => $importer["id"]]);
@@ -2887,7 +2886,7 @@ class DFRN
                        $newentries = [];
                        $entries = $xpath->query("/atom:feed/atom:entry");
                        foreach ($entries as $entry) {
-                               $created = $xpath->query("atom:published/text()", $entry)->item(0)->nodeValue;
+                               $created = XML::getFirstNodeValue($xpath, "atom:published/text()", $entry);
                                $newentries[strtotime($created)] = $entry;
                        }
 
index 29ab21d1f1ec10c5559fb7ccb8014b99c137b7c0..c0b094caefc870350990c300d3d71d3c93d7da87 100644 (file)
@@ -131,11 +131,11 @@ class Feed {
                                if ($value != "") {
                                        $author["author-nick"] = $value;
                                }
-                               $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()')->item(0)->nodeValue;
+                               $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:address/poco:formatted/text()');
                                if ($value != "") {
                                        $author["author-location"] = $value;
                                }
-                               $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
+                               $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:note/text()');
                                if ($value != "") {
                                        $author["author-about"] = $value;
                                }
@@ -149,9 +149,9 @@ class Feed {
                                }
                        }
 
-                       $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
+                       $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:updated/text()');
 
-                       $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
+                       $author["app"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:generator/text()');
 
                        $entries = $xpath->query('/atom:feed/atom:entry');
                }
@@ -226,16 +226,16 @@ class Feed {
                                }
                        }
                        if ($item["plink"] == "") {
-                               $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
+                               $item["plink"] = XML::getFirstNodeValue($xpath, 'link/text()', $entry);
                        }
                        if ($item["plink"] == "") {
-                               $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
+                               $item["plink"] = XML::getFirstNodeValue($xpath, 'rss:link/text()', $entry);
                        }
 
-                       $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
+                       $item["uri"] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
 
                        if ($item["uri"] == "") {
-                               $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
+                               $item["uri"] = XML::getFirstNodeValue($xpath, 'guid/text()', $entry);
                        }
                        if ($item["uri"] == "") {
                                $item["uri"] = $item["plink"];
@@ -257,23 +257,23 @@ class Feed {
                                }
                        }
 
-                       $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
+                       $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
 
                        if ($item["title"] == "") {
-                               $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
+                               $item["title"] = XML::getFirstNodeValue($xpath, 'title/text()', $entry);
                        }
                        if ($item["title"] == "") {
-                               $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
+                               $item["title"] = XML::getFirstNodeValue($xpath, 'rss:title/text()', $entry);
                        }
-                       $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
+                       $published = XML::getFirstNodeValue($xpath, 'atom:published/text()', $entry);
 
                        if ($published == "") {
-                               $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
+                               $published = XML::getFirstNodeValue($xpath, 'pubDate/text()', $entry);
                        }
                        if ($published == "") {
-                               $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
+                               $published = XML::getFirstNodeValue($xpath, 'dc:date/text()', $entry);
                        }
-                       $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
+                       $updated = XML::getFirstNodeValue($xpath, 'atom:updated/text()', $entry);
 
                        if ($updated == "") {
                                $updated = $published;
@@ -284,18 +284,18 @@ class Feed {
                        if ($updated != "") {
                                $item["edited"] = $updated;
                        }
-                       $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
+                       $creator = XML::getFirstNodeValue($xpath, 'author/text()', $entry);
 
                        if ($creator == "") {
-                               $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
+                               $creator = XML::getFirstNodeValue($xpath, 'atom:author/atom:name/text()', $entry);
                        }
                        if ($creator == "") {
-                               $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
+                               $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
                        }
                        if ($creator != "") {
                                $item["author-name"] = $creator;
                        }
-                       $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
+                       $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
 
                        if ($creator != "") {
                                $item["author-name"] = $creator;
index 8dc540324368b23af4d917c9e05956de333ade1e..03e17a55404bba4a4ec99a63219595ab6346d74e 100644 (file)
@@ -431,4 +431,19 @@ class XML
                }
                return $x;
        }
+
+       public static function getFirstNodeValue($xpath, $element, $context = null)
+       {
+               $result = $xpath->evaluate($element, $context);
+               if (!is_object($result)) {
+                       return '';
+               }
+
+               $first_item = $result->item(0);
+               if (!is_object($first_item)) {
+                       return '';
+               }
+
+               return $first_item->nodeValue;
+       }
 }