PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_tuiyo/libraries/tuiyo/user/uploads.php

https://github.com/iduknow/ignite
PHP | 302 lines | 128 code | 43 blank | 131 comment | 29 complexity | ea41569cc6bdcd23c5f28d384559f21b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * ******************************************************************
  4. * Uploaded File manager object for the Tuiyo platform *
  5. * ******************************************************************
  6. * @copyright : 2008 tuiyo Platform *
  7. * @license : http://platform.tuiyo.com/license BSD License *
  8. * @version : Release: $Id$ *
  9. * @link : http://platform.tuiyo.com/ *
  10. * @author : livingstone[at]drstonyhills[dot]com *
  11. * @access : Public *
  12. * @since : 1.0.0 alpha *
  13. * @package : tuiyo *
  14. * ******************************************************************
  15. */
  16. /**
  17. * No direct access to these files
  18. */
  19. defined('TUIYO_EXECUTE') || die;
  20. /**
  21. * joomla File management libraries
  22. */
  23. jimport('joomla.filesystem.file');
  24. jimport('joomla.filesystem.folder');
  25. jimport('joomla.filesystem.path');
  26. jimport('joomla.filesystem.archive');
  27. /**
  28. * TuiyoUploads
  29. *
  30. * @package tuiyo
  31. * @author Livingstone Fultang
  32. * @copyright 2009
  33. * @version $Id$
  34. * @access public
  35. */
  36. class TuiyoUploads
  37. {
  38. /**
  39. * The Maximum file size per type in bytes
  40. * Do not change this
  41. */
  42. private $_maxFileSize = array(
  43. "avatar" => 6553600,
  44. "audio" => 10000000,
  45. "photos" => 1048576,
  46. "wallpaper" => 1048576,
  47. "gavatar" => 6553600,
  48. );
  49. /**
  50. * The ultimate max file size in bytes
  51. * Do not change this (set at 25MB)
  52. */
  53. private $_postMaxSize = 26214400;
  54. /**
  55. * The allowed file extensions
  56. * jpg, gif, png, jpeg, mp3, doc,
  57. **/
  58. private $_extWhitelist = array("mp3","jpg", "gif", "png", "jpeg", "zip");
  59. /**
  60. * Characters allowed in the file name
  61. * (in a Regular Expression format)
  62. */
  63. private $_validChars = '.A-Z0-9_ !@#$%^&()+={}\[\]\',~`-';
  64. /**
  65. * The upload file type
  66. */
  67. private $_fileType = null;
  68. /**
  69. * The Last uploaded File
  70. */
  71. private $_lastUploadedItem = null;
  72. /**
  73. * Max File Length
  74. */
  75. private $_maxNameLength = 100;
  76. /**
  77. * TuiyoUploads::__contstruct()
  78. *
  79. * @return void
  80. */
  81. public function __construct( $type ){
  82. //Set File Type
  83. $this->_fileType = $type;
  84. }
  85. /**
  86. * TuiyoUploads::checkItemPermission()
  87. *
  88. * @return
  89. */
  90. public function getPermission()
  91. {}
  92. /**
  93. * TuiyoUploads::getItemUrl()
  94. *
  95. * @return
  96. */
  97. public function getItemUrl()
  98. {}
  99. /**
  100. * TuiyoUploads::saveItem()
  101. *
  102. * @return
  103. */
  104. public function saveItem( $fData , $sData )
  105. {
  106. //Check Upload Method
  107. if ($_SERVER['REQUEST_METHOD'] !== "POST"){
  108. trigger_error( _("Method Accepts only POST"), E_USER_ERROR);
  109. return false;
  110. }
  111. //Check the file
  112. if(!isset($fData) || empty($this->_fileType )) {
  113. trigger_error(_("No uploaded files detected"), E_USER_ERROR);
  114. return false;
  115. } elseif (isset($fData["error"]) && $fData["error"] != 0) {
  116. trigger_error($fData["error"], E_USER_ERROR );
  117. return false;
  118. } elseif (!isset($fData["tmp_name"]) || !@is_uploaded_file($fData["tmp_name"])) {
  119. trigger_error(_("Invalid uploaded resource."), E_USER_ERROR );
  120. return false;
  121. } else if (!isset($fData['name'])){
  122. trigger_error(_("File has no name"), E_USER_ERROR);
  123. return false;
  124. }
  125. //Check User Upload Limit;
  126. //Check File upload limit;
  127. //Check the passed Data
  128. //Validate the file
  129. $fName = preg_replace('/[^'.$this->_validChars.']|\.+$/i', "", basename($fData['name']));
  130. if (strlen($fName) == 0 || strlen($fName) > $this->_maxNameLength ) {
  131. trigger_error(_("Invalid file Name."), E_USER_ERROR );
  132. return false;
  133. }
  134. //move the file to the cache
  135. $targetCache = JPATH_CACHE.DS.basename( $fData['name'] );
  136. if(!move_uploaded_file($fData['tmp_name'], $targetCache )) {
  137. trigger_error(_("Upload Failed"), E_USER_ERROR);
  138. return false;
  139. }
  140. //Load the resources table
  141. $resourceTable =& TuiyoLoader::table("resources", true);
  142. $this->_lastUploadedItem = $resourceTable->saveFile($fData, $this->_fileType );
  143. return true;
  144. }
  145. /**
  146. * TuiyoUploads::getLastUploaded()
  147. *
  148. * @return void
  149. */
  150. public function getLastUploaded(){
  151. return $this->_lastUploadedItem->url ;
  152. }
  153. /**
  154. * TuiyoUploads::checkUploadLimit()
  155. *
  156. * @return
  157. */
  158. public function checkUploadLimit()
  159. {
  160. //Check Overall Limit
  161. $multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1)));
  162. if ((int)$_SERVER['CONTENT_LENGTH'] > $multiplier*(int)$this->postMaxSize && $this->postMaxSize) {
  163. trigger_error(_("Exceeded Maximum file size"), E_USER_ERROR );
  164. }
  165. //Check Type limit
  166. $fileSize = @filesize($_FILES["Filedata"]["tmp_name"]);
  167. if (!$fileSize || $fileSize > $this->_maxFileSize[$this->_fileType]) {
  168. trigger_error(_("File exceeds the maximum allowed size"), E_USER_ERROR);
  169. return false;
  170. }
  171. if ($fileSize <= 0) {
  172. trigger_error(_("File size outside allowed lower bound"), E_USER_ERROR);
  173. return false;
  174. }
  175. }
  176. /**
  177. * Produces an archive of files
  178. *
  179. * TuiyoUploads::archiveFiles()
  180. *
  181. * @param mixed $userID
  182. * @param mixed $files
  183. * @return link on success, raises error on failure
  184. */
  185. public function archiveFiles($userID, $files ){
  186. //move the file to the cache
  187. $targetCache = JPATH_CACHE.DS;
  188. $targetFolder = $targetCache.DS.$this->_randomCode(4);
  189. $targetName = $targetFolder.DS.$this->_randomCode(4).".archive.zip";
  190. $archiveFiles = array();
  191. $archiveZip = new ZipArchive();
  192. //Create Folder;
  193. if( JFolder::create( $targetFolder) ){
  194. //create the file and throw the error if unsuccessful
  195. if ($archiveZip->open($targetName, ZIPARCHIVE::CREATE )!== true ) {
  196. trigger_error(_("Could not open $targetName archive"), E_USER_ERROR);
  197. return false;
  198. }
  199. //Archive the files
  200. foreach($files as $file){
  201. $name = JFile::getName( $file );
  202. $dest = $targetFolder.DS.$name;
  203. if(!JFile::copy( $file , $dest ) ){
  204. trigger_error(_("Could not archive files"), E_USER_ERROR);
  205. return false;
  206. }
  207. //add the File
  208. $archiveZip->addFile($dest, $name);
  209. }
  210. }
  211. $archiveZip->close();
  212. //If the achive exists! copy it to user resource!
  213. if(JFile::exists( $targetName )){
  214. if(JFile::move($targetName, $targetCache.DS.basename( $targetName ) )){
  215. JFolder::delete( $targetFolder );
  216. $fData = array(
  217. "name" => basename( $targetName )
  218. );
  219. //Save to the resources table
  220. $resourceTable =& TuiyoLoader::table("resources", true);
  221. $resourceLink =& $resourceTable->saveFile($fData, $this->_fileType );
  222. return $resourceLink;
  223. }
  224. }
  225. return false;
  226. }
  227. /**
  228. * TuiyoUploads::downloadItem()
  229. *
  230. * @return
  231. */
  232. public function downloadItem()
  233. {}
  234. /**
  235. * TuiyoUploads::downloadItems()
  236. *
  237. * @return
  238. */
  239. public function downloadItems()
  240. {}
  241. /**
  242. * TuiyoUploads::getErrors()
  243. *
  244. * @return void
  245. */
  246. public function getErrors(){}
  247. /**
  248. * TuiyoUploads::_randomCode()
  249. *
  250. * @return void
  251. */
  252. private function _randomCode($length){
  253. $code = md5(uniqid(rand(), true));
  254. if ($length != "")
  255. return substr($code, 0, $length);
  256. else
  257. return $code;
  258. }
  259. }