init.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. class Af_RedditImgur extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Inline images (and other content) in Reddit RSS feeds",
  7. "fox");
  8. }
  9. function flags() {
  10. return array("needs_curl" => true);
  11. }
  12. function init($host) {
  13. $this->host = $host;
  14. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  15. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  16. }
  17. function hook_prefs_tab($args) {
  18. if ($args != "prefFeeds") return;
  19. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Reddit content settings (af_redditimgur)')."\">";
  20. $enable_readability = $this->host->get($this, "enable_readability");
  21. $enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
  22. print "<form dojoType=\"dijit.form.Form\">";
  23. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  24. evt.preventDefault();
  25. if (this.validate()) {
  26. console.log(dojo.objectToQuery(this.getValues()));
  27. new Ajax.Request('backend.php', {
  28. parameters: dojo.objectToQuery(this.getValues()),
  29. onComplete: function(transport) {
  30. notify_info(transport.responseText);
  31. }
  32. });
  33. //this.reset();
  34. }
  35. </script>";
  36. print_hidden("op", "pluginhandler");
  37. print_hidden("method", "save");
  38. print_hidden("plugin", "af_redditimgur");
  39. print "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
  40. print "<p/>";
  41. print_checkbox("enable_readability", $enable_readability);
  42. print "&nbsp;<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
  43. print "<br/>";
  44. print_checkbox("enable_content_dupcheck", $enable_content_dupcheck);
  45. print "&nbsp;<label for=\"enable_content_dupcheck\">" . __("Enable additional duplicate checking") . "</label>";
  46. print "<p>"; print_button("submit", __("Save"));
  47. print "</form>";
  48. print "</div>";
  49. }
  50. function save() {
  51. $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
  52. $enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]) == "true";
  53. $this->host->set($this, "enable_readability", $enable_readability, false);
  54. $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck);
  55. echo __("Configuration saved");
  56. }
  57. /**
  58. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  59. */
  60. private function inline_stuff($article, &$doc, $xpath, $debug = false) {
  61. $entries = $xpath->query('(//a[@href]|//img[@src])');
  62. $found = false;
  63. foreach ($entries as $entry) {
  64. if ($entry->hasAttribute("href")) {
  65. _debug("processing href: " . $entry->getAttribute("href"), $debug);
  66. $matches = array();
  67. if (preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) {
  68. _debug("handling as twitter: " . $matches[1] . " " . $matches[2], $debug);
  69. $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=" . urlencode($entry->getAttribute("href")));
  70. if ($oembed_result) {
  71. $oembed_result = json_decode($oembed_result, true);
  72. if ($oembed_result && isset($oembed_result["html"])) {
  73. $tmp = new DOMDocument();
  74. if ($tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
  75. $p = $doc->createElement("p");
  76. $p->appendChild($doc->importNode(
  77. $tmp->getElementsByTagName("blockquote")->item(0), TRUE));
  78. $br = $doc->createElement('br');
  79. $entry->parentNode->insertBefore($p, $entry);
  80. $entry->parentNode->insertBefore($br, $entry);
  81. $found = 1;
  82. }
  83. }
  84. }
  85. }
  86. if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
  87. $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
  88. }
  89. if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
  90. _debug("Handling as Gfycat", $debug);
  91. $tmp = fetch_file_contents($entry->getAttribute("href"));
  92. if ($tmp) {
  93. $tmpdoc = new DOMDocument();
  94. if (@$tmpdoc->loadHTML($tmp)) {
  95. $tmpxpath = new DOMXPath($tmpdoc);
  96. $source_node = $tmpxpath->query("//video[contains(@class,'share-video')]//source[contains(@src, '.mp4')]")->item(0);
  97. $poster_node = $tmpxpath->query("//video[contains(@class,'share-video') and @poster]")->item(0);
  98. if ($source_node && $poster_node) {
  99. $source_stream = $source_node->getAttribute("src");
  100. $poster_url = $poster_node->getAttribute("poster");
  101. $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
  102. $found = 1;
  103. }
  104. }
  105. }
  106. }
  107. if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) {
  108. _debug("Handling as Streamable", $debug);
  109. $tmp = fetch_file_contents($entry->getAttribute("href"));
  110. if ($tmp) {
  111. $tmpdoc = new DOMDocument();
  112. if (@$tmpdoc->loadHTML($tmp)) {
  113. $tmpxpath = new DOMXPath($tmpdoc);
  114. $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0);
  115. $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0);
  116. if ($source_node && $poster_node) {
  117. $source_stream = $source_node->getAttribute("src");
  118. $poster_url = $poster_node->getAttribute("poster");
  119. $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
  120. $found = 1;
  121. }
  122. }
  123. }
  124. }
  125. // imgur .gif -> .gifv
  126. if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
  127. _debug("Handling as imgur gif (->gifv)", $debug);
  128. $entry->setAttribute("href",
  129. str_replace(".gif", ".gifv", $entry->getAttribute("href")));
  130. }
  131. if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) {
  132. _debug("Handling as imgur gifv", $debug);
  133. $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
  134. if (strpos($source_stream, "imgur.com") !== FALSE)
  135. $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
  136. $this->handle_as_video($doc, $entry, $source_stream, $poster_url, $debug);
  137. $found = true;
  138. }
  139. $matches = array();
  140. if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
  141. preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
  142. preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
  143. preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
  144. $vid_id = $matches[1];
  145. _debug("Handling as youtube: $vid_id", $debug);
  146. $iframe = $doc->createElement("iframe");
  147. $iframe->setAttribute("class", "youtube-player");
  148. $iframe->setAttribute("type", "text/html");
  149. $iframe->setAttribute("width", "640");
  150. $iframe->setAttribute("height", "385");
  151. $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
  152. $iframe->setAttribute("allowfullscreen", "1");
  153. $iframe->setAttribute("frameborder", "0");
  154. $br = $doc->createElement('br');
  155. $entry->parentNode->insertBefore($iframe, $entry);
  156. $entry->parentNode->insertBefore($br, $entry);
  157. $found = true;
  158. }
  159. if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) ||
  160. mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== FALSE ||
  161. mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== FALSE) {
  162. _debug("Handling as a picture", $debug);
  163. $img = $doc->createElement('img');
  164. $img->setAttribute("src", $entry->getAttribute("href"));
  165. $br = $doc->createElement('br');
  166. $entry->parentNode->insertBefore($img, $entry);
  167. $entry->parentNode->insertBefore($br, $entry);
  168. $found = true;
  169. }
  170. // linked albums & pages
  171. if (!$found && preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
  172. preg_match("/^https?:\/\/(m\.)?imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
  173. _debug("Handling as an imgur page/album/gallery", $debug);
  174. $album_content = fetch_file_contents($entry->getAttribute("href"),
  175. false, false, false, false, 10);
  176. if ($album_content) {
  177. $adoc = new DOMDocument();
  178. if (@$adoc->loadHTML($album_content)) {
  179. $axpath = new DOMXPath($adoc);
  180. $aentries = $axpath->query("(//div[@class='post-image']/img[@src] | //a[@class='zoom']/img[@src] | //div[@class='video-elements']/source)");
  181. $urls = [];
  182. foreach ($aentries as $aentry) {
  183. $url = $aentry->getAttribute("src");
  184. if (!in_array($url, $urls)) {
  185. if ($aentry->tagName == "img") {
  186. $img = $doc->createElement('img');
  187. $img->setAttribute("src", $url);
  188. $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
  189. $br = $doc->createElement('br');
  190. $entry->parentNode->insertBefore($img, $entry);
  191. $entry->parentNode->insertBefore($br, $entry);
  192. } else if ($aentry->tagName == "source") {
  193. if (strpos($url, "imgur.com") !== FALSE)
  194. $poster_url = str_replace(".mp4", "h.jpg", $url);
  195. else
  196. $poster_url = "";
  197. $this->handle_as_video($doc, $entry, $url, $poster_url);
  198. }
  199. array_push($urls, $url);
  200. $found = true;
  201. }
  202. }
  203. if ($debug) print_r($urls);
  204. }
  205. }
  206. }
  207. // wtf is this even
  208. if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
  209. $img_id = $matches[1];
  210. _debug("handling as gyazo: $img_id", $debug);
  211. $img = $doc->createElement('img');
  212. $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
  213. $br = $doc->createElement('br');
  214. $entry->parentNode->insertBefore($img, $entry);
  215. $entry->parentNode->insertBefore($br, $entry);
  216. $found = true;
  217. }
  218. }
  219. // remove tiny thumbnails
  220. if ($entry->hasAttribute("src")) {
  221. if ($entry->parentNode && $entry->parentNode->parentNode) {
  222. $entry->parentNode->parentNode->removeChild($entry->parentNode);
  223. }
  224. }
  225. }
  226. return $found;
  227. }
  228. function hook_article_filter($article) {
  229. if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
  230. $doc = new DOMDocument();
  231. @$doc->loadHTML($article["content"]);
  232. $xpath = new DOMXPath($doc);
  233. $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
  234. if ($this->host->get($this, "enable_content_dupcheck")) {
  235. if ($content_link) {
  236. $content_href = db_escape_string($content_link->getAttribute("href"));
  237. $entry_guid = db_escape_string($article["guid_hashed"]);
  238. $owner_uid = $article["owner_uid"];
  239. if (DB_TYPE == "pgsql") {
  240. $interval_qpart = "date_entered < NOW() - INTERVAL '1 day'";
  241. } else {
  242. $interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)";
  243. }
  244. $result = db_query("SELECT COUNT(id) AS cid
  245. FROM ttrss_entries, ttrss_user_entries WHERE
  246. ref_id = id AND
  247. $interval_qpart AND
  248. guid != '$entry_guid' AND
  249. owner_uid = '$owner_uid' AND
  250. content LIKE '%href=\"$content_href\">[link]%'");
  251. if ($result) {
  252. $num_found = db_fetch_result($result, 0, "cid");
  253. if ($num_found > 0) $article["force_catchup"] = true;
  254. }
  255. }
  256. }
  257. $found = $this->inline_stuff($article, $doc, $xpath);
  258. $node = $doc->getElementsByTagName('body')->item(0);
  259. if ($node && $found) {
  260. $article["content"] = $doc->saveHTML($node);
  261. } else if ($content_link) {
  262. $article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath);
  263. }
  264. }
  265. return $article;
  266. }
  267. function api_version() {
  268. return 2;
  269. }
  270. private function handle_as_video($doc, $entry, $source_stream, $poster_url = false, $debug = false) {
  271. _debug("handle_as_video: $source_stream", $debug);
  272. $video = $doc->createElement('video');
  273. $video->setAttribute("autoplay", "1");
  274. $video->setAttribute("controls", "1");
  275. $video->setAttribute("loop", "1");
  276. if ($poster_url) $video->setAttribute("poster", $poster_url);
  277. $source = $doc->createElement('source');
  278. $source->setAttribute("src", $source_stream);
  279. $source->setAttribute("type", "video/mp4");
  280. $video->appendChild($source);
  281. $br = $doc->createElement('br');
  282. $entry->parentNode->insertBefore($video, $entry);
  283. $entry->parentNode->insertBefore($br, $entry);
  284. $img = $doc->createElement('img');
  285. $img->setAttribute("src",
  286. "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
  287. $entry->parentNode->insertBefore($img, $entry);
  288. }
  289. function testurl() {
  290. $url = htmlspecialchars($_REQUEST["url"]);
  291. header("Content-type: text/plain");
  292. print "URL: $url\n";
  293. $doc = new DOMDocument();
  294. @$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
  295. $xpath = new DOMXPath($doc);
  296. $found = $this->inline_stuff([], $doc, $xpath, true);
  297. print "Inline result: $found\n";
  298. if (!$found) {
  299. print "\nReadability result:\n";
  300. $article = $this->readability([], $url, $doc, $xpath, true);
  301. print_r($article);
  302. } else {
  303. print "\nResulting HTML:\n";
  304. print $doc->saveHTML();
  305. }
  306. }
  307. private function get_content_type($url, $useragent = SELF_USER_AGENT) {
  308. $content_type = false;
  309. if (function_exists("curl_init") && !defined("NO_CURL")) {
  310. $ch = curl_init($url);
  311. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  312. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  313. curl_setopt($ch, CURLOPT_HEADER, true);
  314. curl_setopt($ch, CURLOPT_NOBODY, true);
  315. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
  316. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  317. @curl_exec($ch);
  318. $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  319. }
  320. return $content_type;
  321. }
  322. /**
  323. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  324. */
  325. private function readability($article, $url, $doc, $xpath, $debug = false) {
  326. if (!defined('NO_CURL') && function_exists("curl_init") && $this->host->get($this, "enable_readability") &&
  327. mb_strlen(strip_tags($article["content"])) <= 150) {
  328. if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php");
  329. if ($url &&
  330. strpos($url, "twitter.com") === FALSE &&
  331. strpos($url, "youtube.com") === FALSE &&
  332. strpos($url, "reddit.com") === FALSE) {
  333. /* link may lead to a huge video file or whatever, we need to check content type before trying to
  334. parse it which p much requires curl */
  335. $useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
  336. $content_type = $this->get_content_type($url, $useragent_compat);
  337. if ($content_type && strpos($content_type, "text/html") !== FALSE) {
  338. $tmp = fetch_file_contents(array("url" => $url,
  339. "useragent" => $useragent_compat));
  340. if ($debug) _debug("tmplen: " . mb_strlen($tmp));
  341. if ($tmp && mb_strlen($tmp) < 1024 * 500) {
  342. $r = new Readability($tmp, $url);
  343. if ($r->init()) {
  344. $tmpxpath = new DOMXPath($r->dom);
  345. $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
  346. foreach ($entries as $entry) {
  347. if ($entry->hasAttribute("href")) {
  348. $entry->setAttribute("href",
  349. rewrite_relative_url($url, $entry->getAttribute("href")));
  350. }
  351. if ($entry->hasAttribute("src")) {
  352. $entry->setAttribute("src",
  353. rewrite_relative_url($url, $entry->getAttribute("src")));
  354. }
  355. }
  356. $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"];
  357. }
  358. }
  359. }
  360. }
  361. }
  362. return $article;
  363. }
  364. }