Thrash.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // Todas variáveis vieram pra ca
  3. require("../static.php");
  4. $db = new PDO($dbdsn, $dbuser, $dbpass);
  5. class Thrash {
  6. public $id;
  7. public $code;
  8. public $title;
  9. public $original_url;
  10. public $creator_host;
  11. public $date_created;
  12. public $image_owner_id;
  13. public $blocked_domain;
  14. public $slices;
  15. private $image_path;
  16. public function __construct($title = null, $original_url = null) {
  17. if ($title) $this->title = $title;
  18. if ($original_url) $this->original_url = $original_url;
  19. }
  20. public function get_url() {
  21. return Thrash::$url . $this->code;
  22. }
  23. public function get_image_path() {
  24. if ($this->image_owner_id) {
  25. $owner = Thrash::get($this->image_owner_id);
  26. if ($owner->slices > 0) {
  27. $res = array();
  28. for ($i = 0; $i < $owner->slices; $i ++) {
  29. array_push($res, "{$owner->image_storage_base}{$owner->code}_$i.png");
  30. }
  31. return $res;
  32. } else return array("{$owner->image_storage_base}{$owner->code}.png");
  33. } else {
  34. if ($this->slices > 0) {
  35. $res = array();
  36. for ($i = 0; $i < $this->slices; $i ++) {
  37. array_push($res, "{$this->image_storage_base}{$this->code}_$i.png");
  38. }
  39. return $res;
  40. } else return array("{$this->image_storage_base}{$this->code}.png");
  41. }
  42. }
  43. public function save() {
  44. global $db;
  45. $query = $db->prepare("insert into thrash (date_created, title, original_url, image_storage_base, creator_host, blocked_domain) values (:created,:title,:url,:image_storage_base,:creator_host,:blocked_domain)");
  46. $query->bindParam(':created', date("Y-m-d H:i:s"), PDO::PARAM_STR);
  47. $query->bindParam(':title', $this->title, PDO::PARAM_STR);
  48. $query->bindParam(':url', $this->original_url, PDO::PARAM_STR);
  49. $query->bindParam(':image_storage_base', Thrash::$image_storage_base, PDO::PARAM_STR);
  50. $query->bindParam(':creator_host', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
  51. $query->bindParam(':blocked_domain', $this->blocked_domain, PDO::PARAM_BOOL);
  52. $query->execute();
  53. $this->id = $db->lastInsertId();
  54. $this->code = base_convert($this->id, 10, 32);
  55. $db->prepare("update thrash set code='{$this->code}' where id={$this->id}")->execute();
  56. }
  57. static function get($id) {
  58. global $db;
  59. $query = $db->prepare("select * from thrash where id=? limit 1");
  60. $query->execute(array($id));
  61. return array_shift($query->fetchAll(PDO::FETCH_CLASS, "Thrash"));
  62. }
  63. static function get_by_code($code) {
  64. global $db;
  65. $query = $db->prepare("select * from thrash where code=? limit 1");
  66. $query->execute(array($code));
  67. return array_shift($query->fetchAll(PDO::FETCH_CLASS, "Thrash"));
  68. }
  69. static function get_by_url($url) {
  70. global $db;
  71. $query = $db->prepare("select * from thrash where original_url=? and image_owner_id is null and blocked_domain <> 1 order by date_created desc limit 1");
  72. $query->execute(array($url));
  73. return array_shift($query->fetchAll(PDO::FETCH_CLASS, "Thrash"));
  74. }
  75. static function create($url, $title) {
  76. global $db;
  77. $db->beginTransaction();
  78. $new = new Thrash($title, $url);
  79. $new->blocked_domain = false; //Temporariamente desabilitado pois voltaram a funcionar os dominios - preg_match(Thrash::$blocked_domains_regex, $url) ? 1 : 0;
  80. $old = $new->blocked_domain ? null : Thrash::get_by_url($url);
  81. try {
  82. $new->save();
  83. } catch(PDOExecption $e) {
  84. print "Error!: " . $e->getMessage() . "</br>";
  85. }
  86. if ($old) {
  87. $date = DateTime::createFromFormat('Y-m-d H:i:s', $old->date_created);
  88. $today = new DateTime;
  89. }
  90. if ($old && $today->diff($date)->days <= 1) {
  91. $db->prepare("update thrash set image_owner_id={$old->id} where id={$new->id}")->execute();
  92. } else if (!$new->blocked_domain) {
  93. $basePath = $_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF']).'/';
  94. $path = $basePath.Thrash::$image_storage_base;
  95. $return = shell_exec($basePath."capture.sh \"$url\" $path{$new->code}.png");
  96. $sliceHeight = 768;
  97. $info = getimagesize("$path{$new->code}.png");
  98. if (!$info) {
  99. $db->rollBack();
  100. shell_exec("rm $path{$new->code}.png");
  101. header("Location: ./?error=load&url=$url");
  102. die();
  103. } else if ($info[1] > $sliceHeight) {
  104. $slices = ceil($info[1]/$sliceHeight);
  105. shell_exec($basePath."slice.sh $path{$new->code} $slices");
  106. $db->prepare("update thrash set slices=$slices where id={$new->id}")->execute();
  107. }
  108. shell_exec($basePath."optimize.sh $path{$new->code}.png");
  109. }
  110. $db->commit();
  111. return $new;
  112. }
  113. }