init.php 1.7 KB

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