init.php 14 KB

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