Attachment.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. class Attachment {
  3. private $conn;
  4. private $table_name = "attachments";
  5. public $id;
  6. public $item_id;
  7. public $filename;
  8. public $original_name;
  9. public $file_type;
  10. public $file_path;
  11. public $file_size;
  12. public $mime_type;
  13. public $created_at;
  14. public function __construct($db) {
  15. $this->conn = $db;
  16. }
  17. public function create() {
  18. $query = "INSERT INTO " . $this->table_name . " SET item_id=:item_id, filename=:filename, original_name=:original_name, file_type=:file_type, file_path=:file_path, file_size=:file_size, mime_type=:mime_type, created_at=:created_at";
  19. $stmt = $this->conn->prepare($query);
  20. $this->item_id = htmlspecialchars(strip_tags($this->item_id));
  21. $this->filename = htmlspecialchars(strip_tags($this->filename));
  22. $this->original_name = htmlspecialchars(strip_tags($this->original_name));
  23. $this->file_type = htmlspecialchars(strip_tags($this->file_type));
  24. $this->file_path = htmlspecialchars(strip_tags($this->file_path));
  25. $this->file_size = htmlspecialchars(strip_tags($this->file_size));
  26. $this->mime_type = htmlspecialchars(strip_tags($this->mime_type));
  27. $this->created_at = date('Y-m-d H:i:s');
  28. $stmt->bindParam(":item_id", $this->item_id);
  29. $stmt->bindParam(":filename", $this->filename);
  30. $stmt->bindParam(":original_name", $this->original_name);
  31. $stmt->bindParam(":file_type", $this->file_type);
  32. $stmt->bindParam(":file_path", $this->file_path);
  33. $stmt->bindParam(":file_size", $this->file_size);
  34. $stmt->bindParam(":mime_type", $this->mime_type);
  35. $stmt->bindParam(":created_at", $this->created_at);
  36. if($stmt->execute()) {
  37. return true;
  38. }
  39. return false;
  40. }
  41. public function read() {
  42. $query = "SELECT * FROM " . $this->table_name . " WHERE item_id = ? ORDER BY created_at DESC";
  43. $stmt = $this->conn->prepare($query);
  44. $stmt->bindParam(1, $this->item_id);
  45. $stmt->execute();
  46. return $stmt;
  47. }
  48. public function readOne() {
  49. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  50. $stmt = $this->conn->prepare($query);
  51. $stmt->bindParam(1, $this->id);
  52. $stmt->execute();
  53. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  54. $this->item_id = $row['item_id'];
  55. $this->filename = $row['filename'];
  56. $this->original_name = $row['original_name'];
  57. $this->file_type = $row['file_type'];
  58. $this->file_path = $row['file_path'];
  59. $this->file_size = $row['file_size'];
  60. $this->mime_type = $row['mime_type'];
  61. $this->created_at = $row['created_at'];
  62. }
  63. public function delete() {
  64. $query = "SELECT file_path FROM " . $this->table_name . " WHERE id = ?";
  65. $stmt = $this->conn->prepare($query);
  66. $stmt->bindParam(1, $this->id);
  67. $stmt->execute();
  68. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  69. if($row && file_exists($row['file_path'])) {
  70. unlink($row['file_path']);
  71. }
  72. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  73. $stmt = $this->conn->prepare($query);
  74. $stmt->bindParam(1, $this->id);
  75. if($stmt->execute()) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. public function uploadFile($file, $item_id, $file_type) {
  81. $uploadDir = '/var/www/html/attachments/';
  82. $allowedTypes = [
  83. 'application/pdf',
  84. 'image/jpeg',
  85. 'image/png',
  86. 'image/gif',
  87. 'text/plain',
  88. 'application/msword',
  89. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  90. ];
  91. $maxFileSize = 10 * 1024 * 1024; // 10MB
  92. if (!file_exists($uploadDir)) {
  93. mkdir($uploadDir, 0755, true);
  94. }
  95. if (!in_array($file['type'], $allowedTypes)) {
  96. return ['success' => false, 'message' => 'Invalid file type.'];
  97. }
  98. if ($file['size'] > $maxFileSize) {
  99. return ['success' => false, 'message' => 'File too large. Maximum size is 10MB.'];
  100. }
  101. $fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
  102. $uniqueFileName = uniqid() . '.' . $fileExtension;
  103. $uploadPath = $uploadDir . $uniqueFileName;
  104. if (move_uploaded_file($file['tmp_name'], $uploadPath)) {
  105. $this->item_id = $item_id;
  106. $this->filename = $uniqueFileName;
  107. $this->original_name = $file['name'];
  108. $this->file_type = $file_type;
  109. $this->file_path = $uploadPath;
  110. $this->file_size = $file['size'];
  111. $this->mime_type = $file['type'];
  112. if ($this->create()) {
  113. $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
  114. $apiPath = dirname($_SERVER['PHP_SELF']);
  115. $fullUrl = $baseUrl . '/uploads/' . $uploadPath;
  116. return ['success' => true, 'url' => $fullUrl, 'id' => $this->conn->lastInsertId()];
  117. } else {
  118. unlink($uploadPath);
  119. return ['success' => false, 'message' => 'Failed to save attachment record.'];
  120. }
  121. } else {
  122. return ['success' => false, 'message' => 'Failed to upload file.'];
  123. }
  124. }
  125. }
  126. ?>