init.php 15 KB

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