PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/documents/Documents.php

https://bitbucket.org/brainbox/shared
PHP | 353 lines | 141 code | 93 blank | 119 comment | 13 complexity | 07875d2df3589666d58b875ca1d7dc43 MD5 | raw file
  1. <?php
  2. Class Documents{
  3. function __construct($dbPDO){
  4. $this->dbPDO=$dbPDO;
  5. $sql="SELECT * from documents ORDER BY upload_date DESC";
  6. //echo $sql;
  7. $stmt = $this->dbPDO->prepare($sql);
  8. $stmt->execute();
  9. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  10. //print_r($row);
  11. $this->items[]=$row;
  12. }
  13. //print_r($this->items);
  14. //exit;
  15. }
  16. /**
  17. * Factory class
  18. */
  19. public function fetch(){
  20. if(isset($this->items)){
  21. $row=each($this->items);
  22. if ( $row ) {
  23. return new Document($this->dbPDO,$row['value']);
  24. }
  25. else {
  26. reset ( $this->items);
  27. return false;
  28. }
  29. }
  30. else{
  31. return false;
  32. }
  33. }
  34. /**
  35. * factory Class
  36. */
  37. public function getByID($id){
  38. foreach($this->items as $name=>$value){
  39. if($value['id']==$id){
  40. //echo 'asdfdasdasd';
  41. return new Document($this->dbPDO,$value);
  42. }
  43. }
  44. //only gets this far if article not found
  45. reset($this->items); //Reset the pointer
  46. return false;
  47. }
  48. /**
  49. * Filename must be unique.
  50. *
  51. *
  52. */
  53. public function addDocument($fileData)
  54. {
  55. //echo '<hr>';
  56. //print_r($fileData);
  57. //Filename must NOT be in the database;
  58. $sql="SELECT count(*) FROM documents WHERE filename='" . $fileData['name'] ."'";
  59. //echo $sql;
  60. $stmt = $this->dbPDO->prepare($sql);
  61. $stmt->execute();
  62. $result = $stmt->fetch();
  63. //print_r($result);
  64. //exit;
  65. //echo 'The result is ' . $result[0];
  66. //exit;
  67. if ($result[0]>0)
  68. {
  69. //echo 'File exisits!!!!!!!!';
  70. return false;
  71. die();
  72. }
  73. else{
  74. //process the document
  75. $sql="INSERT INTO documents (filename, mimetype, filesize, create_date, upload_date, user_id)
  76. VALUES (
  77. '" . $fileData['name'] ."',
  78. '" . $fileData['type'] ."',
  79. '" . $fileData['size'] ."',
  80. " . time() . ",
  81. " . time() . ",
  82. 1)";
  83. //echo $sql;
  84. $stmt = $this->dbPDO->prepare($sql);
  85. $stmt->execute();
  86. //Need the insert ID
  87. $documentID=$this->dbPDO->lastInsertId();
  88. //Stick the file in the right place.
  89. require ('config/shackology_config.php');
  90. //print_r($config);
  91. //exit;
  92. $baseFilePath=$config['baseFilePath'] ;
  93. //echo $baseFilePath;
  94. move_uploaded_file($fileData['tmp_name'],$baseFilePath . 'documents/' . $documentID);
  95. return true;
  96. }
  97. }
  98. }
  99. /**
  100. * Created by factory
  101. *
  102. */
  103. Class Document{
  104. protected $id;
  105. protected $filename; // Don't need the stored filename - that can always be "invented". Might be handy, though, if I use get/set.
  106. protected $create_date;
  107. protected $upload_date;
  108. protected $filesize;
  109. protected $mime;
  110. protected $userID;
  111. function __construct($dbPDO,$data)
  112. {
  113. //echo 'HERE@';
  114. $this->dbPDO=$dbPDO;
  115. $this->id=$data['id'];
  116. $this->filename=$data['filename'];
  117. $this->filesize=$data['filesize'];
  118. $this->mime=$data['mimetype'];
  119. $this->create_date=$data['create_date'];
  120. $this->upload_date=$data['upload_date'];
  121. }
  122. /**
  123. * Magic Method
  124. */
  125. public function __get($nm){
  126. if (isset($this->$nm)) {
  127. return ($this->$nm);
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * Magic Method
  134. */
  135. public function __set($nm, $val){
  136. if($this->$nm !=$val){
  137. $this->$nm = $val;
  138. }
  139. }
  140. /**
  141. * Tricy rule: the filename can be the same as current filename... but NOT the same
  142. * as another filename!!!
  143. */
  144. public function addVersion($fileData, $folder)
  145. {
  146. //Filename must NOT be in the database;
  147. $sql="SELECT id FROM documents WHERE filename='" . $fileData['name'] ."'";
  148. //echo $sql;
  149. $stmt = $this->dbPDO->prepare($sql);
  150. $stmt->execute();
  151. $result = $stmt->fetch();
  152. //May be no result - that's fine. If it's the current doc, that's fine
  153. if (!$result || $result['id']==$this->id)
  154. {
  155. //???The follwojng needs to be sorted
  156. //exit;
  157. //process the document
  158. $sql="UPDATE documents SET
  159. filename='" . $fileData['name'] ."',
  160. mimetype='" . $fileData['type'] ."',
  161. filesize='" . $fileData['size'] ."',
  162. upload_date= " . time() . ",
  163. user_id=1
  164. WHERE id=" . $this->id;
  165. //echo $sql;
  166. $stmt = $this->dbPDO->prepare($sql);
  167. $stmt->execute();
  168. //Stick the file in the right place.
  169. //require ('config/shackology_config.php');
  170. //print_r($config);
  171. //exit;
  172. //$baseFilePath=$config['baseFilePath'] ;
  173. //echo $baseFilePath;
  174. move_uploaded_file($fileData['tmp_name'],BASE_FILE_PATH . '/documents/' . $this->id);
  175. return true;
  176. }
  177. else
  178. {
  179. //echo '<p>Filename in use by another Document';
  180. return false;
  181. }
  182. }
  183. function mimeTypeHuman(){
  184. }
  185. function fileSizeHuman(){
  186. $size=$this->filesize;
  187. $decimals=1; //decimal places
  188. $suffix = array('Bytes','KB','MB','GB','TB','PB','EB','ZB','YB','NB','DB');
  189. $i = 0;
  190. while ($size >= 1024 && ($i < count($suffix) - 1)){
  191. $size /= 1024;
  192. $i++;
  193. }
  194. return round($size, $decimals).'&nbsp;'.$suffix[$i];
  195. }
  196. public function sendFile(){
  197. //$path =
  198. //$fname = $this->filename;
  199. require_once('files/SendFile.php');
  200. $filePath = BASE_FILE_PATH . '/documents/' . $this->id;
  201. //echo $filePath;
  202. //exit;
  203. $sendFile=new SendFile($filePath, $this->filename);//path, filename
  204. $sendFile->serveFile();
  205. /*
  206. exit;
  207. session_write_close();
  208. ob_end_clean();
  209. //if(!is_file($path) || connection_status() != 0){
  210. // echo 'oops';
  211. // return false;
  212. //}
  213. //to prevent long file from getting cut off
  214. set_time_limit(0);
  215. //$name = basename($fname);
  216. //THIS IS MY ADDITION (FROM HARRY@S VERSION)
  217. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
  218. $mimeType = 'application/x-download';
  219. }
  220. else{
  221. $mimeType = 'application/octet-stream';
  222. }
  223. //echo $this->filename;
  224. //echo $this->mime;
  225. //exit;
  226. //echo '<p>the name is '.$name;
  227. //exit;
  228. header("Cache-Control: ");
  229. header("Pragma: ");
  230. header("Content-Type: " . $mimeType ); /// NOTE: This is NOT the mimetype ofd the FILE
  231. header("Content-Length: " . $this->fileSize );
  232. header('Content-Disposition: attachment; filename="' . $this->filename . '"');
  233. header("Content-Transfer-Encoding: binary\n");
  234. $path=BASE_FILE_PATH . '/documents/' . $this->id;
  235. if($file = fopen($path, 'rb'))
  236. {
  237. while((!feof($file)) && (connection_status()==0))
  238. {
  239. print(fread($file, 1024*8));
  240. flush();
  241. }
  242. fclose($file);
  243. }
  244. return connection_status() == 0 && !connection_aborted();
  245. */
  246. }
  247. }
  248. ?>