PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/application/models/Files.php

https://github.com/manubamba/site
PHP | 270 lines | 157 code | 32 blank | 81 comment | 22 complexity | 387e39f5bd90da68c2ed8ae484d59cb7 MD5 | raw file
  1. <?php
  2. /**
  3. * Files -> Files database model for content files table.
  4. *
  5. * Copyright (c) <2009>, Markus Riihel�
  6. * Copyright (c) <2009>, Mikko Sallinen
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
  16. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * License text found in /license/
  19. */
  20. /**
  21. * Files - class
  22. *
  23. * @package models
  24. * @author Markus Riihel� & Mikko Sallinen
  25. * @copyright 2009 Markus Riihel� & Mikko Sallinen
  26. * @license GPL v2
  27. * @version 1.0
  28. */
  29. class Default_Model_Files extends Zend_Db_Table_Abstract
  30. {
  31. // Table name
  32. protected $_name = 'files_fil';
  33. // Table primary key
  34. protected $_primary = 'id_fil';
  35. // Table reference map
  36. protected $_referenceMap = array(
  37. 'FileContent' => array(
  38. 'columns' => array('id_cnt_fil'),
  39. 'refTableClass' => 'Default_Model_Content',
  40. 'refColumns' => array('id_cnt')
  41. ),
  42. 'FileUser' => array(
  43. 'columns' => array('id_usr_fil'),
  44. 'refTableClass' => 'Default_Model_User',
  45. 'refColumns' => array('id_usr')
  46. )
  47. );
  48. /**
  49. * newFile
  50. *
  51. * handle uploading of new file, file goes to files/[userid]/[hash of filecontent+filename]
  52. *
  53. * @param id_cnt int content id with which the file is linked
  54. * @param id_usr int user id for the user whose pic to modify
  55. * @param uploadedFile array has info of file, if left empty will use $_FILES
  56. * @return success boolean was the procedure succesful?
  57. */
  58. public function newFile($id_cnt, $id_usr, $uploadedFile = "")
  59. {
  60. if ($uploadedFile == "") {
  61. $uploadedFile = $_FILES['content_file_upload'];
  62. }
  63. $hash = hash_hmac_file('sha1', $uploadedFile['tmp_name'], $id_cnt.$uploadedFile['name']);
  64. $dir = "files/".$id_usr."/";
  65. if (! file_exists($dir)) {
  66. mkdir($dir, 0777, true);
  67. }
  68. if ( !file_exists($dir) || file_exists($dir.$hash)) {
  69. return false;
  70. }
  71. move_uploaded_file($uploadedFile['tmp_name'], $dir.$hash);
  72. $file = $this->createRow();
  73. $file->id_cnt_fil = $id_cnt;
  74. $file->id_usr_fil = $id_usr;
  75. $file->filetype_fil = $uploadedFile['type'];
  76. $file->filename_fil = $uploadedFile['name'];
  77. $file->hash_fil = $hash;
  78. $file->created_fil = new Zend_Db_Expr('NOW()');
  79. $file->modified_fil = new Zend_Db_Expr('NOW()');
  80. $file->save();
  81. }
  82. public function getFilenamesByCntId($id_cnt){
  83. $select = $this->select()
  84. ->from($this, array('id_fil', 'filename_fil'))
  85. ->where('id_cnt_fil = ?', $id_cnt);
  86. $result = $this->fetchAll($select);
  87. $rows = array();
  88. foreach ($result as $row) {
  89. $rows[$row->id_fil] = $row->filename_fil;
  90. }
  91. return $rows;
  92. }
  93. public function getFile($id_fil = 0)
  94. {
  95. if ($id_fil != 0) {
  96. return $this->find($id_fil);
  97. }
  98. }
  99. public function getFileData($id_fil = 0)
  100. {
  101. // Get file data
  102. if ($id_fil != 0) {
  103. // Create query
  104. /*$select = $this->_db->select()
  105. ->from('files_fil',
  106. array('data_fil', 'filetype_fil'))
  107. ->where('id_fil = ?', $id_fil);
  108. // Fetch data from database
  109. $result = $this->_db->fetchAll($select);*/
  110. $rs = $this->find($id_fil);
  111. $cur = $rs->current();
  112. $dir = "files/".$cur->id_usr_fil."/".$cur->hash_fil;
  113. $result = file_get_contents($dir);
  114. return $result;
  115. }
  116. }
  117. public function getContentFiles($id_cnt = 0) {
  118. if ($id_cnt != 0) {
  119. $select = $this->select()->from($this, array("id_fil", "filename_fil"))
  120. ->where('id_cnt_fil = ?', $id_cnt);
  121. $result = $this->fetchAll($select);
  122. return $result;
  123. }
  124. }
  125. public function deleteFiles($files) {
  126. if (isset($files))
  127. {
  128. foreach ($files as $file) {
  129. // Delete from filesystem
  130. $this->deleteFromFilesystem($file);
  131. // Delete link from database
  132. $this->delete('id_fil = ' . $file);
  133. }
  134. }
  135. }
  136. /* deleleteFromFilesystem
  137. *
  138. * delete a file from filesystem according to database link
  139. *
  140. * @param id_fil files_fil tables id
  141. * @return success whether removing file from filesystem was successfull or not
  142. */
  143. private function deleteFromFilesystem($id_fil)
  144. {
  145. // Delete from filesystem
  146. $rs = $this->find($id_fil);
  147. $cur = $rs->current();
  148. $dir = "files/".$cur->id_usr_fil."/".$cur->hash_fil;
  149. $success = @unlink($dir);
  150. return $success;
  151. }
  152. /* deleteFromFilsystemByContentId
  153. *
  154. * Deletes specified contents files from filesystem
  155. *
  156. * $param $id_cnt_fil content id
  157. * $return bool if fileremoval was successfull
  158. */
  159. private function deleteFromFilesystemByContentId($id_cnt_fil){
  160. $select = $this->select()//->from($this, array('id_fil'))
  161. ->where('id_cnt_fil = ?', $id_cnt_fil);
  162. $result = $this->fetchAll($select);
  163. $results = array();
  164. foreach ($result as $row) {
  165. array_push($results, $this->deleteFromFilesystem($row->id_fil));
  166. }
  167. return !in_array(false, $results);
  168. }
  169. /**
  170. * fileExists
  171. *
  172. * Check if file exists in database.
  173. *
  174. * @param $id_fil integer Id of file
  175. * @return boolean
  176. */
  177. public function fileExists($id_fil = 0)
  178. {
  179. $exists = false;
  180. if ($id_fil != 0) {
  181. $select = $this->select()
  182. ->from($this, array('filename_fil'))
  183. ->where('id_fil = ?', $id_fil);
  184. $result = $this->fetchAll($select)->toArray();
  185. if(isset($result[0]) && !empty($result[0])) {
  186. $exists = true;
  187. }
  188. }
  189. return $exists;
  190. }
  191. /**
  192. * removeContentFiles
  193. * Removes specified file
  194. *
  195. * @param int id_cnt_fil Id of the content
  196. * @author Mikko Korpinen
  197. */
  198. public function removeContentFiles($id_cnt_fil)
  199. {
  200. $where = $this->getAdapter()->quoteInto('id_cnt_fil = ?', $id_cnt_fil);
  201. $filesystemDeleteResult = $this->deleteFromFilesystemByContentId($id_cnt_fil);
  202. $databaseDeleteResult = $this->delete($where);
  203. if ($databaseDeleteResult && $filesystemDeleteResult) {
  204. return true;
  205. } else {
  206. return false;
  207. }
  208. }
  209. public function convertFiles() {
  210. $select = $this->_db->select()
  211. ->from("files_fil_old");
  212. $rs = $this->_db->fetchAll($select);
  213. foreach ($rs as $row) {
  214. $hash = hash_hmac('sha1', $row['data_fil'], $row['id_cnt_fil'].$row['filename_fil'] );
  215. $dir = "files/".$row["id_usr_fil"]."/";
  216. if (! file_exists($dir)) {
  217. mkdir($dir, 0777, true);
  218. }
  219. if (! file_exists($dir.$hash)) {
  220. if (($fh = fopen($dir.$hash, "w"))) {
  221. fwrite($fh, $row['data_fil']);
  222. fclose($fh);
  223. $file = $this->createRow();
  224. $file->id_cnt_fil = $row['id_cnt_fil'];
  225. $file->id_usr_fil = $row['id_usr_fil'];
  226. $file->filetype_fil = $row['filetype_fil'];
  227. $file->filename_fil = $row['filename_fil'];
  228. $file->hash_fil = $hash;
  229. $file->created_fil = new Zend_Db_Expr('NOW()');
  230. $file->modified_fil = new Zend_Db_Expr('NOW()');
  231. $file->save();
  232. }
  233. }
  234. }
  235. }
  236. } // end of class
  237. ?>