init.php 15 KB

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