PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/V/lib/DBFile.class.php

https://github.com/Krassmus/tsc
PHP | 241 lines | 188 code | 24 blank | 29 comment | 25 complexity | 36601d776b626a85739e5d14262c4f28 MD5 | raw file
  1. <?php
  2. require_once dirname(__file__)."/vendor/qqUpload.php";
  3. require_once dirname(__file__)."/Cache.class.php";
  4. class DBFile {
  5. //Die folgenden müssen public sein für die Rechtechecks
  6. public $table;
  7. public $mime_type_field = "mime_type";
  8. public $filename_field = "filename";
  9. public $content_field = "content";
  10. public $date_field = "date";
  11. public $mime_type_part1 = "text";
  12. public $width_field = NULL;
  13. public $height_field = NULL;
  14. protected $id;
  15. public function __construct($id = null) {
  16. $this->id = $id;
  17. if (!$this->table) {
  18. throw new Exception("Datei-Typ hat keinen zugewiesenen Tabellennamen in der Datenbank.");
  19. $this->__destruct();
  20. return;
  21. }
  22. if (!$this->id_field) {
  23. $db = DBManager::get();
  24. }
  25. }
  26. public function getId() {
  27. return $this->id;
  28. }
  29. /**
  30. * darf der aktuelle Nutzer die Datei sehen?
  31. */
  32. public function readable() {
  33. return true;
  34. }
  35. /**
  36. * darf der aktuelle Nutzer die Datei überschreiben/bearbeiten?
  37. */
  38. public function writable() {
  39. return false;
  40. }
  41. /**
  42. * Liefert die aktuelle Datei aus, wenn der Nutzer das Recht zum Sehen hat.
  43. * Ignoriert alle anderen Ausgaben von PHP und beendet das Programm, sodass nur die Datei
  44. * zurückgegeben werden kann!
  45. */
  46. public function deliver() {
  47. if ($this->readable()) {
  48. $db = DBManager::get();
  49. $file_data = Cache::getCachedFetch(
  50. "SELECT * FROM `".$this->table."` WHERE ".$this->id_field." = :id ",
  51. array('id' => $this->id),
  52. 5
  53. );
  54. if ($this->mime_type_field === null) {
  55. header("Content-type: ".$this->mime_type_part1);
  56. } else {
  57. header("Content-type: ".$file_data[$this->mime_type_field]);
  58. }
  59. if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] && $file_data[$this->date_field] < strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  60. //cache-control:
  61. header("HTTP/1.1 304 Not Modified");
  62. exit;
  63. }
  64. header('Content-Disposition: inline; filename="'.$file_data[$this->filename_field].'"');
  65. header("Cache-Control: no-cache");
  66. header("Pragma: no-cache");
  67. print $file_data[$this->content_field];
  68. exit;
  69. }
  70. }
  71. public function save($file_path) {
  72. if ($this->readable()) {
  73. $db = DBManager::get();
  74. $file_data = $db->query("SELECT * " .
  75. "FROM `".$this->table."` " .
  76. "WHERE ".$this->id_field." = ".$db->quote($this->id))->fetch();
  77. file_put_contents($file_path, $file_data['content']);
  78. }
  79. }
  80. /**
  81. * deprecated
  82. */
  83. public function create($db_entries = array()) {
  84. global $force;
  85. $allowedExtensions = array();
  86. $sizeLimit = 1 * 1024 * 1024;
  87. $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
  88. $result = $uploader->handleUpload(dirname(__file__)."/../tmp/");
  89. // to pass data through iframe you will need to encode all html tags
  90. echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
  91. if ($result['success'] === true && $force->has($_REQUEST['force_id'])) {
  92. //packe das Bild in die Datenbank:
  93. $db = DBManager::get();
  94. $filename = $result['filename'];
  95. $mime_type = $this->mime_type_part1."/".$result['ext'];
  96. if ($this->width_field OR $this->height_field) {
  97. $size = getimagesize($result['path']);
  98. }
  99. $additional_db_entries = "";
  100. foreach ($db_entries as $key => $value) {
  101. $additional_db_entries .= "`".addslashes($key) . "` = " . $db->quote($value) . ", ";
  102. }
  103. $time = time();
  104. $db->exec(
  105. "INSERT INTO `".addslashes($this->table)."` " .
  106. "SET ".addslashes($this->filename_field)." = ".$db->quote($filename).", " .
  107. $additional_db_entries .
  108. ($this->width_field ?
  109. "`".addslashes($this->width_field)."` = ".($size[0] ? $db->quote($size[0]) : "NULL").", " : "") .
  110. ($this->height_field ?
  111. "`".addslashes($this->height_field)."` = ".($size[1] ? $db->quote($size[1]) : "NULL").", " : "") .
  112. "`".addslashes($this->date_field)."` = ".$db->quote($time).", " .
  113. ($this->mime_type_field ?
  114. "`".addslashes($this->mime_type_field)."` = ".$db->quote($mime_type).", " : "") .
  115. "`".addslashes($this->content_field)."` = ".$db->quote(file_get_contents($result['path']))." " .
  116. "");
  117. //der folgende Aufruf sollte genau genug sein, um ein Table-Lock unnötig werden zu lassen:
  118. $this->id = $db->lastInsertId();
  119. };
  120. //Datei löschen und fertig:
  121. if (file_exists($result['path'])) {
  122. unlink($result['path']);
  123. }
  124. }
  125. /**
  126. * deprecated
  127. */
  128. public function update($db_entries = array()) {
  129. global $force;
  130. if (!$this->id) {
  131. return false;
  132. }
  133. $allowedExtensions = array();
  134. $sizeLimit = 1 * 1024 * 1024;
  135. $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
  136. $result = $uploader->handleUpload(dirname(__file__)."/../tmp/");
  137. // to pass data through iframe you will need to encode all html tags
  138. if ($result['success'] === true && $force->has($_REQUEST['force_id']) && $this->writable()) {
  139. //packe das Bild in die Datenbank:
  140. $db = DBManager::get();
  141. $filename = $result['filename'];
  142. $mime_type = $this->mime_type_part1."/".$result['ext'];
  143. if ($this->width_field OR $this->height_field) {
  144. $size = getimagesize($result['path']);
  145. }
  146. $additional_db_entries = "";
  147. foreach ($db_entries as $key => $value) {
  148. $additional_db_entries .= $key . " = " . $db->quote($value) . ", ";
  149. }
  150. $db->exec(
  151. "UPDATE `".addslashes($this->table)."` " .
  152. "SET ".addslashes($this->filename_field)." = ".$db->quote($filename).", " .
  153. $additional_db_entries .
  154. "`".addslashes($this->width_field)."` = ".($size[0] ? $db->quote($size[0]) : "NULL").", " .
  155. "`".addslashes($this->height_field)."` = ".($size[1] ? $db->quote($size[1]) : "NULL").", " .
  156. "`".addslashes($this->date_field)."` = ".$db->quote($time).", " .
  157. "`".addslashes($this->mime_type_field)."` = ".$db->quote($mime_type).", " .
  158. "`".addslashes($this->content_field)."` = ".$db->quote(file_get_contents($result['path']))." " .
  159. "WHERE ".$this->id_field." = ".$db->quote($this->id)." " .
  160. "");
  161. };
  162. $result['id'] = $this->id;
  163. echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
  164. //Datei löschen und fertig:
  165. if (file_exists($result['path'])) {
  166. unlink($result['path']);
  167. }
  168. }
  169. public function upload($db_entries = array()) {
  170. global $force;
  171. $allowedExtensions = array();
  172. $sizeLimit = 1 * 1024 * 1024;
  173. $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
  174. $result = $uploader->handleUpload(dirname(__file__)."/../tmp/");
  175. // to pass data through iframe you will need to encode all html tags
  176. if ($result['success'] === true && $force->has($_REQUEST['force_id']) && $this->writable()) {
  177. //packe die Datei in die Datenbank:
  178. $db = DBManager::get();
  179. $filename = $result['filename'];
  180. $mime_type = $this->mime_type_part1."/".$result['ext'];
  181. if ($this->width_field OR $this->height_field) {
  182. $size = getimagesize($result['path']);
  183. }
  184. $additional_db_entries = "";
  185. foreach ($db_entries as $key => $value) {
  186. $additional_db_entries .= $key . " = " . $db->quote($value) . ", ";
  187. }
  188. $set = "SET `".addslashes($this->filename_field)."` = ".$db->quote($filename).", " .
  189. $additional_db_entries .
  190. ($this->width_field ? "`".addslashes($this->width_field)."` = ".($size[0] ? $db->quote($size[0]) : "NULL").", " : "") .
  191. ($this->height_field ? "`".addslashes($this->height_field)."` = ".($size[1] ? $db->quote($size[1]) : "NULL").", " : "") .
  192. ($this->date_field ? "`".addslashes($this->date_field)."` = ".$db->quote($time).", " : "") .
  193. ($this->mime_type_field ? "`".addslashes($this->mime_type_field)."` = ".$db->quote($mime_type).", " : "") .
  194. "`".addslashes($this->content_field)."` = ".$db->quote(file_get_contents($result['path']))." ";
  195. if ($this->id) {
  196. print "hi ".$this->id;
  197. $db->exec(
  198. "UPDATE `".addslashes($this->table)."` " .
  199. $set .
  200. "WHERE `".$this->id_field."` = ".$db->quote($this->id)." " .
  201. "");
  202. } else {
  203. $db->exec(
  204. "INSERT INTO `".addslashes($this->table)."` " .
  205. $set .
  206. "");
  207. $this->id = $db->lastInsertId();
  208. }
  209. }
  210. $result['id'] = $this->id;
  211. echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
  212. //Datei löschen und fertig:
  213. if (file_exists($result['path'])) {
  214. unlink($result['path']);
  215. }
  216. }
  217. }