init.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. class Af_Tumblr_1280 extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Replace Tumblr pictures with largest size if available (requires CURL)",
  7. "fox");
  8. }
  9. function flags() {
  10. return array("needs_curl" => true);
  11. }
  12. function init($host) {
  13. $this->host = $host;
  14. if (function_exists("curl_init")) {
  15. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  16. }
  17. }
  18. function hook_article_filter($article) {
  19. if (!function_exists("curl_init") || ini_get("open_basedir"))
  20. return $article;
  21. $charset_hack = '<head>
  22. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  23. </head>';
  24. $doc = new DOMDocument();
  25. $doc->loadHTML($charset_hack . $article["content"]);
  26. $found = false;
  27. if ($doc) {
  28. $xpath = new DOMXpath($doc);
  29. $images = $xpath->query('(//img[contains(@src, \'media.tumblr.com\')])');
  30. foreach ($images as $img) {
  31. $src = $img->getAttribute("src");
  32. $test_src = preg_replace("/_\d{3}.(jpg|gif|png)/", "_1280.$1", $src);
  33. if ($src != $test_src) {
  34. $ch = curl_init($test_src);
  35. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  36. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  37. curl_setopt($ch, CURLOPT_HEADER, true);
  38. curl_setopt($ch, CURLOPT_NOBODY, true);
  39. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  40. curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT);
  41. @$result = curl_exec($ch);
  42. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  43. if ($result && $http_code == 200) {
  44. $img->setAttribute("src", $test_src);
  45. $found = true;
  46. }
  47. }
  48. }
  49. if ($found) {
  50. $doc->removeChild($doc->firstChild); //remove doctype
  51. $article["content"] = $doc->saveHTML();
  52. }
  53. }
  54. return $article;
  55. }
  56. function api_version() {
  57. return 2;
  58. }
  59. }