]> git.mxchange.org Git - friendica.git/blob - src/Protocol/Feed.php
Merge pull request #8879 from MrPetovan/task/8847-httpsig-quotes
[friendica.git] / src / Protocol / Feed.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Protocol;
23
24 use DOMDocument;
25 use DOMXPath;
26 use Friendica\Content\PageInfo;
27 use Friendica\Content\Text\HTML;
28 use Friendica\Core\Logger;
29 use Friendica\Core\Protocol;
30 use Friendica\Database\DBA;
31 use Friendica\DI;
32 use Friendica\Model\Item;
33 use Friendica\Model\Tag;
34 use Friendica\Util\Network;
35 use Friendica\Util\ParseUrl;
36 use Friendica\Util\XML;
37
38 /**
39  * This class contain functions to import feeds (RSS/RDF/Atom)
40  */
41 class Feed
42 {
43         /**
44          * consume - process atom feed and update anything/everything we might need to update
45          *
46          * $xml = the (atom) feed to consume - RSS isn't as fully supported but may work for simple feeds.
47          *
48          * $importer = the contact_record (joined to user_record) of the local user who owns this relationship.
49          *             It is this person's stuff that is going to be updated.
50          * $contact =  the person who is sending us stuff. If not set, we MAY be processing a "follow" activity
51          *             from an external network and MAY create an appropriate contact record. Otherwise, we MUST
52          *             have a contact record.
53          * $hub = should we find a hub declation in the feed, pass it back to our calling process, who might (or
54          *        might not) try and subscribe to it.
55          * $datedir sorts in reverse order
56          * $pass - by default ($pass = 0) we cannot guarantee that a parent item has been
57          *      imported prior to its children being seen in the stream unless we are certain
58          *      of how the feed is arranged/ordered.
59          * With $pass = 1, we only pull parent items out of the stream.
60          * With $pass = 2, we only pull children (comments/likes).
61          *
62          * So running this twice, first with pass 1 and then with pass 2 will do the right
63          * thing regardless of feed ordering. This won't be adequate in a fully-threaded
64          * model where comments can have sub-threads. That would require some massive sorting
65          * to get all the feed items into a mostly linear ordering, and might still require
66          * recursion.
67          *
68          * @param       $xml
69          * @param array $importer
70          * @param array $contact
71          * @param       $hub
72          * @throws ImagickException
73          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
74          */
75         public static function consume($xml, array $importer, array $contact, &$hub)
76         {
77                 if ($contact['network'] === Protocol::OSTATUS) {
78                         Logger::info('Consume OStatus messages');
79                         OStatus::import($xml, $importer, $contact, $hub);
80
81                         return;
82                 }
83
84                 if ($contact['network'] === Protocol::FEED) {
85                         Logger::info('Consume feeds');
86                         self::import($xml, $importer, $contact);
87
88                         return;
89                 }
90
91                 if ($contact['network'] === Protocol::DFRN) {
92                         Logger::info('Consume DFRN messages');
93                         $dfrn_importer = DFRN::getImporter($contact['id'], $importer['uid']);
94                         if (!empty($dfrn_importer)) {
95                                 Logger::info('Now import the DFRN feed');
96                                 DFRN::import($xml, $dfrn_importer, true);
97                                 return;
98                         }
99                 }
100         }
101
102         /**
103          * Read a RSS/RDF/Atom feed and create an item entry for it
104          *
105          * @param string $xml      The feed data
106          * @param array  $importer The user record of the importer
107          * @param array  $contact  The contact record of the feed
108          *
109          * @return array Returns the header and the first item in dry run mode
110          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
111          */
112         public static function import($xml, array $importer = [], array $contact = [])
113         {
114                 $dryRun = empty($importer) && empty($contact);
115
116                 if ($dryRun) {
117                         Logger::info("Test Atom/RSS feed");
118                 } else {
119                         Logger::info("Import Atom/RSS feed '" . $contact["name"] . "' (Contact " . $contact["id"] . ") for user " . $importer["uid"]);
120                 }
121
122                 if (empty($xml)) {
123                         Logger::info('XML is empty.');
124                         return [];
125                 }
126
127                 if (!empty($contact['poll'])) {
128                         $basepath = $contact['poll'];
129                 } elseif (!empty($contact['url'])) {
130                         $basepath = $contact['url'];
131                 } else {
132                         $basepath = '';
133                 }
134
135                 $doc = new DOMDocument();
136                 @$doc->loadXML(trim($xml));
137                 $xpath = new DOMXPath($doc);
138                 $xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
139                 $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
140                 $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
141                 $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
142                 $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
143                 $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
144                 $xpath->registerNamespace('poco', ActivityNamespace::POCO);
145
146                 $author = [];
147                 $entries = null;
148
149                 // Is it RDF?
150                 if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
151                         $author["author-link"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:link/text()');
152                         $author["author-name"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:title/text()');
153
154                         if (empty($author["author-name"])) {
155                                 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rdf:RDF/rss:channel/rss:description/text()');
156                         }
157                         $entries = $xpath->query('/rdf:RDF/rss:item');
158                 }
159
160                 // Is it Atom?
161                 if ($xpath->query('/atom:feed')->length > 0) {
162                         $alternate = XML::getFirstAttributes($xpath, "atom:link[@rel='alternate']");
163                         if (is_object($alternate)) {
164                                 foreach ($alternate AS $attribute) {
165                                         if ($attribute->name == "href") {
166                                                 $author["author-link"] = $attribute->textContent;
167                                         }
168                                 }
169                         }
170
171                         if (empty($author["author-link"])) {
172                                 $self = XML::getFirstAttributes($xpath, "atom:link[@rel='self']");
173                                 if (is_object($self)) {
174                                         foreach ($self AS $attribute) {
175                                                 if ($attribute->name == "href") {
176                                                         $author["author-link"] = $attribute->textContent;
177                                                 }
178                                         }
179                                 }
180                         }
181
182                         if (empty($author["author-link"])) {
183                                 $author["author-link"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:id/text()');
184                         }
185                         $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:logo/text()');
186
187                         $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:title/text()');
188
189                         if (empty($author["author-name"])) {
190                                 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:subtitle/text()');
191                         }
192
193                         if (empty($author["author-name"])) {
194                                 $author["author-name"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:name/text()');
195                         }
196
197                         $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:displayName/text()');
198                         if ($value != "") {
199                                 $author["author-name"] = $value;
200                         }
201
202                         if ($dryRun) {
203                                 $author["author-id"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:id/text()');
204
205                                 // See https://tools.ietf.org/html/rfc4287#section-3.2.2
206                                 $value = XML::getFirstNodeValue($xpath, 'atom:author/atom:uri/text()');
207                                 if ($value != "") {
208                                         $author["author-link"] = $value;
209                                 }
210
211                                 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:preferredUsername/text()');
212                                 if ($value != "") {
213                                         $author["author-nick"] = $value;
214                                 }
215
216                                 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:address/poco:formatted/text()');
217                                 if ($value != "") {
218                                         $author["author-location"] = $value;
219                                 }
220
221                                 $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:note/text()');
222                                 if ($value != "") {
223                                         $author["author-about"] = $value;
224                                 }
225
226                                 $avatar = XML::getFirstAttributes($xpath, "atom:author/atom:link[@rel='avatar']");
227                                 if (is_object($avatar)) {
228                                         foreach ($avatar AS $attribute) {
229                                                 if ($attribute->name == "href") {
230                                                         $author["author-avatar"] = $attribute->textContent;
231                                                 }
232                                         }
233                                 }
234                         }
235
236                         $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:updated/text()');
237
238                         $author["app"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:generator/text()');
239
240                         $entries = $xpath->query('/atom:feed/atom:entry');
241                 }
242
243                 // Is it RSS?
244                 if ($xpath->query('/rss/channel')->length > 0) {
245                         $author["author-link"] = XML::getFirstNodeValue($xpath, '/rss/channel/link/text()');
246
247                         $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/title/text()');
248                         $author["author-avatar"] = XML::getFirstNodeValue($xpath, '/rss/channel/image/url/text()');
249
250                         if (empty($author["author-name"])) {
251                                 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/copyright/text()');
252                         }
253
254                         if (empty($author["author-name"])) {
255                                 $author["author-name"] = XML::getFirstNodeValue($xpath, '/rss/channel/description/text()');
256                         }
257
258                         $author["edited"] = $author["created"] = XML::getFirstNodeValue($xpath, '/rss/channel/pubDate/text()');
259
260                         $author["app"] = XML::getFirstNodeValue($xpath, '/rss/channel/generator/text()');
261
262                         $entries = $xpath->query('/rss/channel/item');
263                 }
264
265                 if (!$dryRun) {
266                         $author["author-link"] = $contact["url"];
267
268                         if (empty($author["author-name"])) {
269                                 $author["author-name"] = $contact["name"];
270                         }
271
272                         $author["author-avatar"] = $contact["thumb"];
273
274                         $author["owner-link"] = $contact["url"];
275                         $author["owner-name"] = $contact["name"];
276                         $author["owner-avatar"] = $contact["thumb"];
277                 }
278
279                 $header = [];
280                 $header["uid"] = $importer["uid"] ?? 0;
281                 $header["network"] = Protocol::FEED;
282                 $header["wall"] = 0;
283                 $header["origin"] = 0;
284                 $header["gravity"] = GRAVITY_PARENT;
285                 $header["private"] = Item::PUBLIC;
286                 $header["verb"] = Activity::POST;
287                 $header["object-type"] = Activity\ObjectType::NOTE;
288
289                 $header["contact-id"] = $contact["id"] ?? 0;
290
291                 if (!is_object($entries)) {
292                         Logger::info("There are no entries in this feed.");
293                         return [];
294                 }
295
296                 $items = [];
297
298                 // Limit the number of items that are about to be fetched
299                 $total_items = ($entries->length - 1);
300                 $max_items = DI::config()->get('system', 'max_feed_items');
301                 if (($max_items > 0) && ($total_items > $max_items)) {
302                         $total_items = $max_items;
303                 }
304
305                 // Importing older entries first
306                 for ($i = $total_items; $i >= 0; --$i) {
307                         $entry = $entries->item($i);
308
309                         $item = array_merge($header, $author);
310
311                         $alternate = XML::getFirstAttributes($xpath, "atom:link[@rel='alternate']", $entry);
312                         if (!is_object($alternate)) {
313                                 $alternate = XML::getFirstAttributes($xpath, "atom:link", $entry);
314                         }
315                         if (is_object($alternate)) {
316                                 foreach ($alternate AS $attribute) {
317                                         if ($attribute->name == "href") {
318                                                 $item["plink"] = $attribute->textContent;
319                                         }
320                                 }
321                         }
322
323                         if (empty($item["plink"])) {
324                                 $item["plink"] = XML::getFirstNodeValue($xpath, 'link/text()', $entry);
325                         }
326
327                         if (empty($item["plink"])) {
328                                 $item["plink"] = XML::getFirstNodeValue($xpath, 'rss:link/text()', $entry);
329                         }
330
331                         $item["uri"] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
332
333                         if (empty($item["uri"])) {
334                                 $item["uri"] = XML::getFirstNodeValue($xpath, 'guid/text()', $entry);
335                         }
336
337                         if (empty($item["uri"])) {
338                                 $item["uri"] = $item["plink"];
339                         }
340
341                         // Add the base path if missing
342                         $item["uri"] = Network::addBasePath($item["uri"], $basepath);
343                         $item["plink"] = Network::addBasePath($item["plink"], $basepath);
344
345                         $orig_plink = $item["plink"];
346
347                         $item["plink"] = Network::finalUrl($item["plink"]);
348
349                         $item["parent-uri"] = $item["uri"];
350
351                         if (!$dryRun) {
352                                 $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
353                                         $importer["uid"], $item["uri"], Protocol::FEED, Protocol::DFRN];
354                                 $previous = Item::selectFirst(['id'], $condition);
355                                 if (DBA::isResult($previous)) {
356                                         Logger::info("Item with uri " . $item["uri"] . " for user " . $importer["uid"] . " already existed under id " . $previous["id"]);
357                                         continue;
358                                 }
359                         }
360
361                         $item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
362
363                         if (empty($item["title"])) {
364                                 $item["title"] = XML::getFirstNodeValue($xpath, 'title/text()', $entry);
365                         }
366                         if (empty($item["title"])) {
367                                 $item["title"] = XML::getFirstNodeValue($xpath, 'rss:title/text()', $entry);
368                         }
369
370                         $item["title"] = html_entity_decode($item["title"], ENT_QUOTES, 'UTF-8');
371
372                         $published = XML::getFirstNodeValue($xpath, 'atom:published/text()', $entry);
373
374                         if (empty($published)) {
375                                 $published = XML::getFirstNodeValue($xpath, 'pubDate/text()', $entry);
376                         }
377
378                         if (empty($published)) {
379                                 $published = XML::getFirstNodeValue($xpath, 'dc:date/text()', $entry);
380                         }
381
382                         $updated = XML::getFirstNodeValue($xpath, 'atom:updated/text()', $entry);
383
384                         if (empty($updated) && !empty($published)) {
385                                 $updated = $published;
386                         }
387
388                         if (empty($published) && !empty($updated)) {
389                                 $published = $updated;
390                         }
391
392                         if ($published != "") {
393                                 $item["created"] = $published;
394                         }
395
396                         if ($updated != "") {
397                                 $item["edited"] = $updated;
398                         }
399
400                         $creator = XML::getFirstNodeValue($xpath, 'author/text()', $entry);
401
402                         if (empty($creator)) {
403                                 $creator = XML::getFirstNodeValue($xpath, 'atom:author/atom:name/text()', $entry);
404                         }
405
406                         if (empty($creator)) {
407                                 $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
408                         }
409
410                         if ($creator != "") {
411                                 $item["author-name"] = $creator;
412                         }
413
414                         $creator = XML::getFirstNodeValue($xpath, 'dc:creator/text()', $entry);
415
416                         if ($creator != "") {
417                                 $item["author-name"] = $creator;
418                         }
419
420                         /// @TODO ?
421                         // <category>Ausland</category>
422                         // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
423
424                         $attachments = [];
425
426                         $enclosures = $xpath->query("enclosure|atom:link[@rel='enclosure']", $entry);
427                         foreach ($enclosures AS $enclosure) {
428                                 $href = "";
429                                 $length = "";
430                                 $type = "";
431
432                                 foreach ($enclosure->attributes AS $attribute) {
433                                         if (in_array($attribute->name, ["url", "href"])) {
434                                                 $href = $attribute->textContent;
435                                         } elseif ($attribute->name == "length") {
436                                                 $length = $attribute->textContent;
437                                         } elseif ($attribute->name == "type") {
438                                                 $type = $attribute->textContent;
439                                         }
440                                 }
441
442                                 if (!empty($item["attach"])) {
443                                         $item["attach"] .= ',';
444                                 } else {
445                                         $item["attach"] = '';
446                                 }
447
448                                 $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
449
450                                 $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '"[/attach]';
451                         }
452
453                         $taglist = [];
454                         $categories = $xpath->query("category", $entry);
455                         foreach ($categories AS $category) {
456                                 $taglist[] = $category->nodeValue;
457                         }
458
459                         $body = trim(XML::getFirstNodeValue($xpath, 'atom:content/text()', $entry));
460
461                         if (empty($body)) {
462                                 $body = trim(XML::getFirstNodeValue($xpath, 'content:encoded/text()', $entry));
463                         }
464
465                         $summary = trim(XML::getFirstNodeValue($xpath, 'atom:summary/text()', $entry));
466
467                         if (empty($summary)) {
468                                 $summary = trim(XML::getFirstNodeValue($xpath, 'description/text()', $entry));
469                         }
470
471                         if (empty($body)) {
472                                 $body = $summary;
473                                 $summary = '';
474                         }
475
476                         if ($body == $summary) {
477                                 $summary = '';
478                         }
479
480                         // remove the content of the title if it is identically to the body
481                         // This helps with auto generated titles e.g. from tumblr
482                         if (self::titleIsBody($item["title"], $body)) {
483                                 $item["title"] = "";
484                         }
485                         $item["body"] = HTML::toBBCode($body, $basepath);
486
487                         if (($item["body"] == '') && ($item["title"] != '')) {
488                                 $item["body"] = $item["title"];
489                                 $item["title"] = '';
490                         }
491
492                         $preview = '';
493                         if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
494                                 // Handle enclosures and treat them as preview picture
495                                 foreach ($attachments AS $attachment) {
496                                         if ($attachment["type"] == "image/jpeg") {
497                                                 $preview = $attachment["link"];
498                                         }
499                                 }
500
501                                 // Remove a possible link to the item itself
502                                 $item["body"] = str_replace($item["plink"], '', $item["body"]);
503                                 $item["body"] = trim(preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]));
504
505                                 // Replace the content when the title is longer than the body
506                                 $replace = (strlen($item["title"]) > strlen($item["body"]));
507
508                                 // Replace it, when there is an image in the body
509                                 if (strstr($item["body"], '[/img]')) {
510                                         $replace = true;
511                                 }
512
513                                 // Replace it, when there is a link in the body
514                                 if (strstr($item["body"], '[/url]')) {
515                                         $replace = true;
516                                 }
517
518                                 if ($replace) {
519                                         $item["body"] = trim($item["title"]);
520                                 }
521
522                                 $data = ParseUrl::getSiteinfoCached($item['plink'], true);
523                                 if (!empty($data['text']) && !empty($data['title']) && (mb_strlen($item['body']) < mb_strlen($data['text']))) {
524                                         // When the fetched page info text is longer than the body, we do try to enhance the body
525                                         if (!empty($item['body']) && (strpos($data['title'], $item['body']) === false) && (strpos($data['text'], $item['body']) === false)) {
526                                                 // The body is not part of the fetched page info title or page info text. So we add the text to the body
527                                                 $item['body'] .= "\n\n" . $data['text'];
528                                         } else {
529                                                 // Else we replace the body with the page info text
530                                                 $item['body'] = $data['text'];
531                                         }
532                                 }
533
534                                 // We always strip the title since it will be added in the page information
535                                 $item["title"] = "";
536                                 $item["body"] = $item["body"] . "\n" . PageInfo::getFooterFromUrl($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
537                                 $taglist = $contact["fetch_further_information"] == 2 ? PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"] ?? '') : [];
538                                 $item["object-type"] = Activity\ObjectType::BOOKMARK;
539                                 unset($item["attach"]);
540                         } else {
541                                 if (!empty($summary)) {
542                                         $item["body"] = '[abstract]' . HTML::toBBCode($summary, $basepath) . "[/abstract]\n" . $item["body"];
543                                 }
544
545                                 if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] == 3)) {
546                                         if (empty($taglist)) {
547                                                 $taglist = PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"]);
548                                         }
549                                         $item["body"] .= "\n" . self::tagToString($taglist);
550                                 } else {
551                                         $taglist = [];
552                                 }
553
554                                 // Add the link to the original feed entry if not present in feed
555                                 if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
556                                         $item["body"] .= "[hr][url]" . $item['plink'] . "[/url]";
557                                 }
558                         }
559
560                         if ($dryRun) {
561                                 $items[] = $item;
562                                 break;
563                         } else {
564                                 Logger::info('Stored feed', ['item' => $item]);
565
566                                 $notify = Item::isRemoteSelf($contact, $item);
567
568                                 // Distributed items should have a well formatted URI.
569                                 // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
570                                 if ($notify) {
571                                         $item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname());
572                                         unset($item['uri']);
573                                         unset($item['parent-uri']);
574
575                                         // Set the delivery priority for "remote self" to "medium"
576                                         $notify = PRIORITY_MEDIUM;
577                                 }
578
579                                 $id = Item::insert($item, $notify);
580
581                                 Logger::info("Feed for contact " . $contact["url"] . " stored under id " . $id);
582
583                                 if (!empty($id) && !empty($taglist)) {
584                                         $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
585                                         foreach ($taglist as $tag) {
586                                                 Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
587                                         }                                       
588                                 }
589                         }
590                 }
591
592                 return ["header" => $author, "items" => $items];
593         }
594
595         /**
596          * Convert a tag array to a tag string
597          *
598          * @param array $tags
599          * @return string tag string
600          */
601         private static function tagToString(array $tags)
602         {
603                 $tagstr = '';
604
605                 foreach ($tags as $tag) {
606                         if ($tagstr != "") {
607                                 $tagstr .= ", ";
608                         }
609         
610                         $tagstr .= "#[url=" . DI::baseUrl() . "/search?tag=" . urlencode($tag) . "]" . $tag . "[/url]";
611                 }
612
613                 return $tagstr;
614         }
615
616         private static function titleIsBody($title, $body)
617         {
618                 $title = strip_tags($title);
619                 $title = trim($title);
620                 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
621                 $title = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $title);
622
623                 $body = strip_tags($body);
624                 $body = trim($body);
625                 $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
626                 $body = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $body);
627
628                 if (strlen($title) < strlen($body)) {
629                         $body = substr($body, 0, strlen($title));
630                 }
631
632                 if (($title != $body) && (substr($title, -3) == "...")) {
633                         $pos = strrpos($title, "...");
634                         if ($pos > 0) {
635                                 $title = substr($title, 0, $pos);
636                                 $body = substr($body, 0, $pos);
637                         }
638                 }
639                 return ($title == $body);
640         }
641 }