Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

369 linhas
12KB

  1. <?php
  2. require_once("include/html2bbcode.php");
  3. require_once("include/items.php");
  4. /**
  5. * @brief Read a RSS/RDF/Atom feed and create an item entry for it
  6. *
  7. * @param string $xml The feed data
  8. * @param array $importer The user record of the importer
  9. * @param array $contact The contact record of the feed
  10. * @param string $hub Unused dummy value for compatibility reasons
  11. * @param bool $simulate If enabled, no data is imported
  12. *
  13. * @return array In simulation mode it returns the header and the first item
  14. */
  15. function feed_import($xml,$importer,&$contact, &$hub, $simulate = false) {
  16. $a = get_app();
  17. if (!$simulate) {
  18. logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG);
  19. } else {
  20. logger("Test Atom/RSS feed", LOGGER_DEBUG);
  21. }
  22. if ($xml == "") {
  23. logger('XML is empty.', LOGGER_DEBUG);
  24. return;
  25. }
  26. $doc = new DOMDocument();
  27. @$doc->loadXML($xml);
  28. $xpath = new DomXPath($doc);
  29. $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
  30. $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
  31. $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
  32. $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
  33. $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/");
  34. $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
  35. $xpath->registerNamespace('poco', NAMESPACE_POCO);
  36. $author = array();
  37. // Is it RDF?
  38. if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
  39. $author["author-link"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:link/text()')->item(0)->nodeValue;
  40. $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:title/text()')->item(0)->nodeValue;
  41. if ($author["author-name"] == "") {
  42. $author["author-name"] = $xpath->evaluate('/rdf:RDF/rss:channel/rss:description/text()')->item(0)->nodeValue;
  43. }
  44. $entries = $xpath->query('/rdf:RDF/rss:item');
  45. }
  46. // Is it Atom?
  47. if ($xpath->query('/atom:feed')->length > 0) {
  48. $alternate = $xpath->query("atom:link[@rel='alternate']")->item(0)->attributes;
  49. if (is_object($alternate)) {
  50. foreach ($alternate AS $attributes) {
  51. if ($attributes->name == "href") {
  52. $author["author-link"] = $attributes->textContent;
  53. }
  54. }
  55. }
  56. if ($author["author-link"] == "") {
  57. $author["author-link"] = $author["author-id"];
  58. }
  59. if ($author["author-link"] == "") {
  60. $self = $xpath->query("atom:link[@rel='self']")->item(0)->attributes;
  61. if (is_object($self)) {
  62. foreach ($self AS $attributes) {
  63. if ($attributes->name == "href") {
  64. $author["author-link"] = $attributes->textContent;
  65. }
  66. }
  67. }
  68. }
  69. if ($author["author-link"] == "") {
  70. $author["author-link"] = $xpath->evaluate('/atom:feed/atom:id/text()')->item(0)->nodeValue;
  71. }
  72. $author["author-avatar"] = $xpath->evaluate('/atom:feed/atom:logo/text()')->item(0)->nodeValue;
  73. $author["author-name"] = $xpath->evaluate('/atom:feed/atom:title/text()')->item(0)->nodeValue;
  74. if ($author["author-name"] == "") {
  75. $author["author-name"] = $xpath->evaluate('/atom:feed/atom:subtitle/text()')->item(0)->nodeValue;
  76. }
  77. if ($author["author-name"] == "") {
  78. $author["author-name"] = $xpath->evaluate('/atom:feed/atom:author/atom:name/text()')->item(0)->nodeValue;
  79. }
  80. $value = $xpath->evaluate('atom:author/poco:displayName/text()')->item(0)->nodeValue;
  81. if ($value != "") {
  82. $author["author-name"] = $value;
  83. }
  84. if ($simulate) {
  85. $author["author-id"] = $xpath->evaluate('/atom:feed/atom:author/atom:uri/text()')->item(0)->nodeValue;
  86. $value = $xpath->evaluate('atom:author/poco:preferredUsername/text()')->item(0)->nodeValue;
  87. if ($value != "") {
  88. $author["author-nick"] = $value;
  89. }
  90. $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
  91. if ($value != "") {
  92. $author["author-location"] = $value;
  93. }
  94. $value = $xpath->evaluate('atom:author/poco:note/text()')->item(0)->nodeValue;
  95. if ($value != "") {
  96. $author["author-about"] = $value;
  97. }
  98. }
  99. $author["edited"] = $author["created"] = $xpath->query('/atom:feed/atom:updated/text()')->item(0)->nodeValue;
  100. $author["app"] = $xpath->evaluate('/atom:feed/atom:generator/text()')->item(0)->nodeValue;
  101. $entries = $xpath->query('/atom:feed/atom:entry');
  102. }
  103. // Is it RSS?
  104. if ($xpath->query('/rss/channel')->length > 0) {
  105. $author["author-link"] = $xpath->evaluate('/rss/channel/link/text()')->item(0)->nodeValue;
  106. $author["author-name"] = $xpath->evaluate('/rss/channel/title/text()')->item(0)->nodeValue;
  107. $author["author-avatar"] = $xpath->evaluate('/rss/channel/image/url/text()')->item(0)->nodeValue;
  108. if ($author["author-name"] == "") {
  109. $author["author-name"] = $xpath->evaluate('/rss/channel/copyright/text()')->item(0)->nodeValue;
  110. }
  111. if ($author["author-name"] == "") {
  112. $author["author-name"] = $xpath->evaluate('/rss/channel/description/text()')->item(0)->nodeValue;
  113. }
  114. $author["edited"] = $author["created"] = $xpath->query('/rss/channel/pubDate/text()')->item(0)->nodeValue;
  115. $author["app"] = $xpath->evaluate('/rss/channel/generator/text()')->item(0)->nodeValue;
  116. $entries = $xpath->query('/rss/channel/item');
  117. }
  118. if (!$simulate) {
  119. $author["author-link"] = $contact["url"];
  120. if ($author["author-name"] == "") {
  121. $author["author-name"] = $contact["name"];
  122. }
  123. $author["author-avatar"] = $contact["thumb"];
  124. $author["owner-link"] = $contact["url"];
  125. $author["owner-name"] = $contact["name"];
  126. $author["owner-avatar"] = $contact["thumb"];
  127. }
  128. $header = array();
  129. $header["uid"] = $importer["uid"];
  130. $header["network"] = NETWORK_FEED;
  131. $header["type"] = "remote";
  132. $header["wall"] = 0;
  133. $header["origin"] = 0;
  134. $header["gravity"] = GRAVITY_PARENT;
  135. $header["private"] = 2;
  136. $header["verb"] = ACTIVITY_POST;
  137. $header["object-type"] = ACTIVITY_OBJ_NOTE;
  138. $header["contact-id"] = $contact["id"];
  139. if (!strlen($contact["notify"])) {
  140. // one way feed - no remote comment ability
  141. $header["last-child"] = 0;
  142. }
  143. if (!is_object($entries)) {
  144. logger("There are no entries in this feed.", LOGGER_DEBUG);
  145. return;
  146. }
  147. $items = array();
  148. $entrylist = array();
  149. foreach ($entries AS $entry) {
  150. $entrylist[] = $entry;
  151. }
  152. foreach (array_reverse($entrylist) AS $entry) {
  153. $item = array_merge($header, $author);
  154. $alternate = $xpath->query("atom:link[@rel='alternate']", $entry)->item(0)->attributes;
  155. if (!is_object($alternate)) {
  156. $alternate = $xpath->query("atom:link", $entry)->item(0)->attributes;
  157. }
  158. if (is_object($alternate)) {
  159. foreach ($alternate AS $attributes) {
  160. if ($attributes->name == "href") {
  161. $item["plink"] = $attributes->textContent;
  162. }
  163. }
  164. }
  165. if ($item["plink"] == "") {
  166. $item["plink"] = $xpath->evaluate('link/text()', $entry)->item(0)->nodeValue;
  167. }
  168. if ($item["plink"] == "") {
  169. $item["plink"] = $xpath->evaluate('rss:link/text()', $entry)->item(0)->nodeValue;
  170. }
  171. $item["uri"] = $xpath->evaluate('atom:id/text()', $entry)->item(0)->nodeValue;
  172. if ($item["uri"] == "") {
  173. $item["uri"] = $xpath->evaluate('guid/text()', $entry)->item(0)->nodeValue;
  174. }
  175. if ($item["uri"] == "") {
  176. $item["uri"] = $item["plink"];
  177. }
  178. $orig_plink = $item["plink"];
  179. $item["plink"] = original_url($item["plink"]);
  180. $item["parent-uri"] = $item["uri"];
  181. if (!$simulate) {
  182. $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s', '%s')",
  183. intval($importer["uid"]), dbesc($item["uri"]), dbesc(NETWORK_FEED), dbesc(NETWORK_DFRN));
  184. if (dbm::is_result($r)) {
  185. logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$r[0]["id"], LOGGER_DEBUG);
  186. continue;
  187. }
  188. }
  189. $item["title"] = $xpath->evaluate('atom:title/text()', $entry)->item(0)->nodeValue;
  190. if ($item["title"] == "") {
  191. $item["title"] = $xpath->evaluate('title/text()', $entry)->item(0)->nodeValue;
  192. }
  193. if ($item["title"] == "") {
  194. $item["title"] = $xpath->evaluate('rss:title/text()', $entry)->item(0)->nodeValue;
  195. }
  196. $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
  197. if ($published == "") {
  198. $published = $xpath->query('pubDate/text()', $entry)->item(0)->nodeValue;
  199. }
  200. if ($published == "") {
  201. $published = $xpath->query('dc:date/text()', $entry)->item(0)->nodeValue;
  202. }
  203. $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
  204. if ($updated == "") {
  205. $updated = $published;
  206. }
  207. if ($published != "") {
  208. $item["created"] = $published;
  209. }
  210. if ($updated != "") {
  211. $item["edited"] = $updated;
  212. }
  213. $creator = $xpath->query('author/text()', $entry)->item(0)->nodeValue;
  214. if ($creator == "") {
  215. $creator = $xpath->query('atom:author/atom:name/text()', $entry)->item(0)->nodeValue;
  216. }
  217. if ($creator == "") {
  218. $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
  219. }
  220. if ($creator != "") {
  221. $item["author-name"] = $creator;
  222. }
  223. if ($pubDate != "") {
  224. $item["edited"] = $item["created"] = $pubDate;
  225. }
  226. $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
  227. if ($creator != "") {
  228. $item["author-name"] = $creator;
  229. }
  230. /// @TODO ?
  231. // <category>Ausland</category>
  232. // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
  233. $attachments = array();
  234. $enclosures = $xpath->query("enclosure", $entry);
  235. foreach ($enclosures AS $enclosure) {
  236. $href = "";
  237. $length = "";
  238. $type = "";
  239. $title = "";
  240. foreach ($enclosure->attributes AS $attributes) {
  241. if ($attributes->name == "url") {
  242. $href = $attributes->textContent;
  243. } elseif ($attributes->name == "length") {
  244. $length = $attributes->textContent;
  245. } elseif ($attributes->name == "type") {
  246. $type = $attributes->textContent;
  247. }
  248. }
  249. if (strlen($item["attach"])) {
  250. $item["attach"] .= ',';
  251. }
  252. $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
  253. $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
  254. }
  255. if ($contact["fetch_further_information"]) {
  256. $preview = "";
  257. // Handle enclosures and treat them as preview picture
  258. foreach ($attachments AS $attachment) {
  259. if ($attachment["type"] == "image/jpeg") {
  260. $preview = $attachment["link"];
  261. }
  262. }
  263. $item["body"] = $item["title"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
  264. $item["tag"] = add_page_keywords($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]);
  265. $item["title"] = "";
  266. $item["object-type"] = ACTIVITY_OBJ_BOOKMARK;
  267. unset($item["attach"]);
  268. } else {
  269. $body = trim($xpath->evaluate('atom:content/text()', $entry)->item(0)->nodeValue);
  270. if ($body == "") {
  271. $body = trim($xpath->evaluate('content:encoded/text()', $entry)->item(0)->nodeValue);
  272. }
  273. if ($body == "") {
  274. $body = trim($xpath->evaluate('description/text()', $entry)->item(0)->nodeValue);
  275. }
  276. if ($body == "") {
  277. $body = trim($xpath->evaluate('atom:summary/text()', $entry)->item(0)->nodeValue);
  278. }
  279. // remove the content of the title if it is identically to the body
  280. // This helps with auto generated titles e.g. from tumblr
  281. if (title_is_body($item["title"], $body)) {
  282. $item["title"] = "";
  283. }
  284. $item["body"] = html2bbcode($body);
  285. }
  286. if (!$simulate) {
  287. logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
  288. $notify = item_is_remote_self($contact, $item);
  289. // Distributed items should have a well formatted URI.
  290. // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
  291. if ($notify) {
  292. $item['guid'] = uri_to_guid($orig_plink, $a->get_hostname());
  293. unset($item['uri']);
  294. unset($item['parent-uri']);
  295. }
  296. $id = item_store($item, false, $notify);
  297. logger("Feed for contact ".$contact["url"]." stored under id ".$id);
  298. } else {
  299. $items[] = $item;
  300. }
  301. if ($simulate) {
  302. break;
  303. }
  304. }
  305. if ($simulate) {
  306. return array("header" => $author, "items" => $items);
  307. }
  308. }