A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
7.0 KiB

13 years ago
  1. <?php
  2. /**
  3. * This class provides an HTML input field for file uploading.
  4. *
  5. *
  6. *@author Erik Winn <erikwinnmail@yahoo.com>
  7. *
  8. *
  9. * $Id: QFileInput.class.php 93 2008-08-28 21:23:02Z erikwinn $
  10. *@version 0.1
  11. *
  12. *@copyright (C) 2008 by Erik Winn
  13. *@license GPL v.2
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  25. *
  26. * @package QuasiContrib
  27. * @subpackage Classes
  28. */
  29. class QFileInput extends QControl
  30. {
  31. /**
  32. *@var string - the full path to the temporary uploaded file.
  33. */
  34. private $strTempUri = null;
  35. /**
  36. *@var string - the original name of the file on the client machine.
  37. */
  38. private $strFileName = null;
  39. /**
  40. *@var string - the file extension (if any).
  41. */
  42. private $strExtension = null;
  43. /**
  44. *@var string - the mime type uploaded file (NOTE: according to the browser).
  45. */
  46. private $strMimeType = null;
  47. /**
  48. *@var integer - the size of the uploaded file in bytes.
  49. */
  50. private $intSize = null;
  51. /**
  52. *@var string - the error code associated with this file upload.
  53. */
  54. private $intErrorCode = null;
  55. private $aryErrorMessages = array(
  56. UPLOAD_ERR_OK => 'Upload Successful',
  57. UPLOAD_ERR_INI_SIZE => 'File exceeds the upload_max_filesize php directive',
  58. UPLOAD_ERR_FORM_SIZE => 'File exceeds the MAX_FILE_SIZE HTML directive',
  59. UPLOAD_ERR_PARTIAL => 'File was only partially uploaded',
  60. UPLOAD_ERR_NO_FILE => 'No file was uploaded',
  61. UPLOAD_ERR_NO_TMP_DIR => 'The temporary upload directory does not exist',
  62. UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
  63. UPLOAD_ERR_EXTENSION => 'File upload stopped by extension',
  64. );
  65. /**
  66. *@var array - attributes for the form to indicate upload
  67. */
  68. protected $strFormAttributes = array('enctype'=>'multipart/form-data');
  69. /**
  70. *@var integer - the maximum size of the uploaded file in bytes.
  71. */
  72. private $intMaxFileSize = 1000000;
  73. public function ParsePostData()
  74. {
  75. if ( array_key_exists($this->strControlId, $_FILES)
  76. && isset($_FILES[$this->strControlId]['tmp_name']) )
  77. {
  78. $this->strTempUri = QType::Cast($_FILES[$this->strControlId]['tmp_name'], QType::String);
  79. $this->strFileName = QType::Cast($_FILES[$this->strControlId]['name'], QType::String);
  80. $this->strMimeType = QType::Cast($_FILES[$this->strControlId]['type'], QType::String);
  81. $this->intSize = QType::Cast($_FILES[$this->strControlId]['size'], QType::Integer);
  82. $this->intErrorCode = QType::Cast($_FILES[$this->strControlId]['error'], QType::Integer);
  83. $pos = strrpos($this->FileName,'.');
  84. if( false !== $pos )
  85. $this->strExtension = substr($this->FileName, $pos +1);
  86. }
  87. }
  88. public function GetJavaScriptAction() {
  89. return "onchange";
  90. }
  91. protected function GetControlHtml()
  92. {
  93. $strToReturn = '';
  94. $this->strTempUri = null;
  95. $this->strFileName = null;
  96. $this->strExtension = null;
  97. $this->strMimeType = null;
  98. $this->intSize = null;
  99. $this->intErrorCode = null;
  100. $strStyleAttributes = $this->GetStyleAttributes();
  101. $strStyle = '';
  102. if (! empty($strStyleAttributes) )
  103. $strStyle = sprintf('style="%s"', $strStyleAttributes);
  104. $strToReturn = sprintf('<input type="hidden" name="MAX_FILE_SIZE" value="%d" />',
  105. $this->intMaxFileSize);
  106. $strToReturn .= sprintf('<input type="file" name="%s" id="%s" %s%s />',
  107. $this->strControlId,
  108. $this->strControlId,
  109. $this->GetAttributes(),
  110. $strStyle);
  111. return $strToReturn;
  112. }
  113. public function Validate()
  114. {
  115. $this->strValidationError = "";
  116. if ($this->ErrorCode != 0)
  117. {
  118. $this->strValidationError = $this->ErrorMessage;
  119. return false;
  120. }
  121. if (! $this->blnRequired )
  122. return true;
  123. if (strlen($this->strFileName) > 0)
  124. return true;
  125. if(strlen( $this->strName ))
  126. $this->strValidationError = $this->strName . ' ' . QApplication::Translate('is required');
  127. else
  128. $this->strValidationError = "Filename is required";
  129. return false;
  130. }
  131. public function __get($strName)
  132. {
  133. switch ($strName)
  134. {
  135. case "TempUri":
  136. return $this->strTempUri;
  137. case "FileName":
  138. return $this->strFileName;
  139. case "MimeType":
  140. return $this->strMimeType;
  141. case "Size":
  142. return $this->intSize;
  143. case "Extension":
  144. return $this->strExtension;
  145. case "ErrorCode":
  146. return $this->intErrorCode;
  147. case "ErrorMessage":
  148. if($this->intErrorCode !== null)
  149. return $this->aryErrorMessages[$this->intErrorCode];
  150. return "Internal Error - Error code unset.";
  151. case "MaxFileSize":
  152. return $this->intMaxFileSize;
  153. default:
  154. try {
  155. return parent::__get($strName);
  156. } catch (QCallerException $objExc) {
  157. $objExc->IncrementOffset();
  158. throw $objExc;
  159. }
  160. }
  161. }
  162. public function __set($strName, $mixValue)
  163. {
  164. $this->blnModified = true;
  165. switch ($strName)
  166. {
  167. case 'MaxFileSize':
  168. try {
  169. return ($this->intMaxFileSize = QType::Cast($mixValue, QType::Integer ));
  170. } catch (QInvalidCastException $objExc) {
  171. $objExc->IncrementOffset();
  172. throw $objExc;
  173. }
  174. default:
  175. try {
  176. parent::__set($strName, $mixValue);
  177. } catch (QCallerException $objExc) {
  178. $objExc->IncrementOffset();
  179. throw $objExc;
  180. }
  181. break;
  182. }
  183. }
  184. }
  185. ?>