PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/libraries/MultiUpload/singleupload.php

http://punchcms.googlecode.com/
PHP | 358 lines | 264 code | 54 blank | 40 comment | 57 complexity | 13cf3d7f5c71b5f1d94351092e1a4d58 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Single File Upload - version 1.2.5
  4. * Easy and reliable upload class for single file upload.
  5. *
  6. * Copyright (c)2006, Phixel.org
  7. *
  8. * CHANGELOG
  9. * version 1.2.5, 11 Oct 2009
  10. * CHG: Changed access of getFileName from protected to public.
  11. * version 1.2.4, 20 Apr 2009
  12. * BUG: Fixed an error regarding files with multiple dots in the name.
  13. * version 1.2.3, 18 Jun 2008
  14. * BUG: Fixed an error retrival bug in getErrorMessage.
  15. * version 1.2.2, 05 May 2008
  16. * BUG: Fixed an unlink bug after uploading.
  17. * version 1.2.1, 13 Nov 2007
  18. * BUG: Fixed a bug involving uppercase extensions.
  19. * version 1.2.0, 07 Nov 2007
  20. * CHG: Temp. file will always be unlinked.
  21. * version 1.1.0, 05 Mar 2007
  22. * ADD: Added the moveToFTP method.
  23. * version 1.0.0, 04 Apr 2006
  24. * NEW: Created class.
  25. */
  26. class SingleUpload {
  27. protected $strOriginalName;
  28. protected $strTempName;
  29. protected $strUploadFolder;
  30. protected $blnReplace = FALSE;
  31. protected $blnRename = FALSE;
  32. protected $blnCheckFilename;
  33. protected $intMaxNameLength = 100;
  34. protected $arrExtensions = array();
  35. protected $strExtensions = "";
  36. protected $intHttpError;
  37. protected $strLocalName;
  38. protected $arrMessages = array();
  39. protected $blnCreateFolder = TRUE;
  40. //*** Constructor.
  41. public function SingleUpload() {
  42. }
  43. //*** Public Properties.
  44. public function setUploadFolder($value) {
  45. $this->strUploadFolder = $value;
  46. }
  47. public function getUploadFolder() {
  48. return $this->strUploadFolder;
  49. }
  50. public function getOriginalName() {
  51. return $this->strOriginalName;
  52. }
  53. public function setOriginalName($value) {
  54. $this->strOriginalName = $value;
  55. }
  56. public function getTempName() {
  57. return $this->strTempName;
  58. }
  59. public function getLocalName() {
  60. return $this->strLocalName;
  61. }
  62. public function setReplace($value) {
  63. $this->blnReplace = $value;
  64. }
  65. public function getReplace() {
  66. return $this->blnReplace;
  67. }
  68. public function setRename($value) {
  69. $this->blnRename = $value;
  70. }
  71. public function getRename() {
  72. return $this->blnRename;
  73. }
  74. public function setCheckFilename($value) {
  75. $this->blnCheckFilename = $value;
  76. }
  77. public function getCheckFilename() {
  78. return $this->blnCheckFilename;
  79. }
  80. public function setCreateFolder($value) {
  81. $this->blnCreateFolder = $value;
  82. }
  83. public function getCreateFolder() {
  84. return $this->blnCreateFolder;
  85. }
  86. public function setExtensions($value) {
  87. $this->arrExtensions = $value;
  88. }
  89. public function getExtensions() {
  90. return $this->arrExtensions;
  91. }
  92. //*** Public Methods.
  93. public function errorMessage() {
  94. $strReturn = "";
  95. foreach ($this->arrMessages as $value) {
  96. $strReturn .= $value . "<br>\n";
  97. }
  98. return $strReturn;
  99. }
  100. public function upload($strTargetName = "") {
  101. $strTempName = $this->getFileName($strTargetName);
  102. if ($this->checkFilename($strTempName)) {
  103. if ($this->validateExtension()) {
  104. if (is_uploaded_file($this->strTempName)) {
  105. $this->strLocalName = $strTempName;
  106. if ($this->moveUpload($this->strTempName, $this->strLocalName)) {
  107. $this->arrMessages[] = $this->getErrorMessage($this->intHttpError);
  108. if ($this->blnRename) $this->arrMessages[] = $this->getErrorMessage(16);
  109. return true;
  110. }
  111. } else {
  112. $this->arrMessages[] = $this->getErrorMessage($this->intHttpError);
  113. return false;
  114. }
  115. } else {
  116. $this->prepareExtensions();
  117. $this->arrMessages[] = $this->getErrorMessage(11);
  118. return false;
  119. }
  120. } else {
  121. return false;
  122. }
  123. }
  124. public function moveToFTP($strLocalName, $strUploadFolder, $strServer, $strUsername, $strPassword, $strRemoteFolder) {
  125. $blnReturn = true;
  126. if (!empty($strLocalName)) {
  127. //*** Connect to the server.
  128. $objFtp = new FTP($strServer, NULL, NULL, TRUE);
  129. $objRet = $objFtp->login($strUsername, $strPassword);
  130. if (!$objRet) {
  131. $this->arrMessages[] = "Login failed. Check credentials.";
  132. $blnReturn = false;
  133. }
  134. //*** Passive mode.
  135. $objFtp->pasv(TRUE);
  136. //*** Transfer file.
  137. $objRet = $objFtp->nb_put($strRemoteFolder . $strLocalName, $strUploadFolder . $strLocalName, FTP_BINARY);
  138. while ($objRet == FTP_MOREDATA) {
  139. // Continue uploading...
  140. $objRet = $objFtp->nb_continue();
  141. }
  142. if ($objRet != FTP_FINISHED) {
  143. //*** Something went wrong.
  144. $this->arrMessages[] = $this->getErrorMessage($this->intHttpError);
  145. $blnReturn = false;
  146. }
  147. //*** Remove local file.
  148. @unlink($strUploadFolder . $strLocalName);
  149. }
  150. return $blnReturn;
  151. }
  152. //*** Private Methods.
  153. protected function moveUpload($strTempFile, $strTargetFile) {
  154. umask(0);
  155. if (!$this->fileExists($strTargetFile)) {
  156. $strTargetPath = $this->strUploadFolder . $strTargetFile;
  157. if ($this->prepareFolder($this->strUploadFolder)) {
  158. if (move_uploaded_file($strTempFile, $strTargetPath)) {
  159. if ($this->blnReplace == TRUE) {
  160. //system("chmod 0777 $strTargetPath"); // maybe you need to use the system command in some cases...
  161. chmod($strTargetPath , 0777);
  162. } else {
  163. // system("chmod 0755 $strTargetPath");
  164. chmod($strTargetPath , 0755);
  165. }
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. } else {
  171. $this->arrMessages[] = $this->getErrorMessage(14);
  172. return false;
  173. }
  174. } else {
  175. $this->arrMessages[] = $this->getErrorMessage(15);
  176. return false;
  177. }
  178. }
  179. protected function prepareExtensions() {
  180. //*** This method is only used for detailed error reporting.
  181. $this->strExtensions = implode(" ", $this->arrExtensions);
  182. }
  183. protected function fileExists($strFile) {
  184. if ($this->blnReplace == TRUE) {
  185. return false;
  186. } else {
  187. if (file_exists($this->strUploadFolder . $strFile)) {
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. }
  193. }
  194. protected function prepareFolder($strFolder) {
  195. if (!is_dir($strFolder)) {
  196. if ($this->blnCreateFolder) {
  197. umask(0);
  198. mkdir($strFolder, 0777);
  199. return true;
  200. } else {
  201. return false;
  202. }
  203. } else {
  204. return true;
  205. }
  206. }
  207. public function getFileName($strName = "") {
  208. //*** This "conversion" is used for unique/new filenames.
  209. $strReturn = "";
  210. if ($this->blnRename) {
  211. if ($this->strOriginalName == "") return;
  212. $strExtension = $this->getExtension($this->strOriginalName);
  213. $strName = $this->fixFilename(basename(strtolower($strName), $strExtension));
  214. $strReturn = (empty($strName)) ? strtotime("now") : $strName . "__" . strtotime("now");
  215. $strReturn = $strReturn . $this->getExtension($this->strOriginalName);
  216. } else {
  217. $strReturn = $this->strOriginalName;
  218. }
  219. return $strReturn;
  220. }
  221. protected function checkFilename($strName) {
  222. if (!empty($strName)) {
  223. if (strlen($strName) > $this->intMaxNameLength) {
  224. $this->arrMessages[] = $this->getErrorMessage(13);
  225. return false;
  226. } else {
  227. if ($this->blnCheckFilename == TRUE) {
  228. if (preg_match("/^[a-z0-9_\.]*\.(.){1,5}$/i", strtolower($strName))) {
  229. return true;
  230. } else {
  231. $this->arrMessages[] = $this->getErrorMessage(12);
  232. return false;
  233. }
  234. } else {
  235. return true;
  236. }
  237. }
  238. } else {
  239. $this->arrMessages[] = $this->getErrorMessage(10);
  240. return false;
  241. }
  242. }
  243. protected function fixFilename($strName) {
  244. $strReturn = $strName;
  245. if (!empty($strReturn)) {
  246. if (strlen($strName) > $this->intMaxNameLength) {
  247. $strReturn = substr($strReturn, 0, $this->intMaxNameLength);
  248. }
  249. $strReturn = mb_strtolower($strReturn);
  250. $arrPatterns = array(
  251. "/\s/", # Whitespace
  252. "/\&/", # Ampersand
  253. "/\+/" # Plus
  254. );
  255. $arrReplacements = array(
  256. "_", # Whitespace
  257. "and", # Ampersand
  258. "plus" # Plus
  259. );
  260. $strReturn = preg_replace($arrPatterns, $arrReplacements, $strReturn);
  261. $strFiltered = "";
  262. for ($i = 0; $i < strlen($strReturn); $i++) {
  263. $strCurrentChar = substr($strReturn, $i, 1);
  264. if (ctype_alnum($strCurrentChar) == TRUE || $strCurrentChar == "_" || $strCurrentChar == ".") {
  265. $strFiltered .= $strCurrentChar;
  266. }
  267. }
  268. $strReturn = $strFiltered;
  269. }
  270. return $strReturn;
  271. }
  272. protected function validateExtension() {
  273. $strExtension = $this->getExtension($this->strOriginalName);
  274. if (in_array($strExtension, $this->arrExtensions)) {
  275. //*** Check mime type against allowed/restricted mime types (boolean check mimetype).
  276. return true;
  277. } else {
  278. return false;
  279. }
  280. }
  281. protected function getExtension($strFile) {
  282. $strReturn = strtolower(strrchr($strFile, "."));
  283. return $strReturn;
  284. }
  285. protected function getErrorMessage($intErrNumber) {
  286. //*** Some error (HTTP)reporting, change the messages or remove options if you like.
  287. $arrError[0] = "File: <b>".$this->strOriginalName."</b> successfully uploaded!";
  288. $arrError[1] = "The uploaded file exceeds the max. upload filesize directive in the server configuration.";
  289. $arrError[2] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form.";
  290. $arrError[3] = "The uploaded file was only partially uploaded";
  291. $arrError[4] = "No file was uploaded";
  292. $arrError[10] = "Please select a file for upload.";
  293. $arrError[11] = "Only files with the following extensions are allowed: <b>".$this->strExtensions."</b>";
  294. $arrError[12] = "Sorry, the filename contains invalid characters. Use only alphanumerical chars and separate parts of the name (if needed) with an underscore. <br>A valid filename ends with one dot followed by the extension.";
  295. $arrError[13] = "The filename exceeds the maximum length of ".$this->intMaxNameLength." characters.";
  296. $arrError[14] = "Sorry, the upload directory doesn't exist!";
  297. $arrError[15] = "Uploading <b>".$this->strOriginalName."...Error!</b> Sorry, a file with this name already exitst.";
  298. $arrError[16] = "The uploaded file is renamed to <b>".$this->strLocalName."</b>.";
  299. return (array_key_exists($intErrNumber, $arrError)) ? $arrError[$intErrNumber] : "";
  300. }
  301. }
  302. ?>