A QCodo powered CMS
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.

172 lines
4.9 KiB

  1. <?php
  2. /**
  3. * This class provides an HTML input field for file uploading.
  4. *
  5. *@author Erik Winn <sidewalksoftware@gmail.com>
  6. *
  7. *@version 0.1
  8. *
  9. * @package QuintaContrib
  10. * @subpackage Classes
  11. */
  12. class QFileInput extends QControl{
  13. /**
  14. *@var string - the full path to the temporary uploaded file.
  15. */
  16. private $strTempUri = null;
  17. /**
  18. *@var string - the original name of the file on the client machine.
  19. */
  20. private $strFileName = null;
  21. /**
  22. *@var string - the file extension (if any).
  23. */
  24. private $strExtension = null;
  25. /**
  26. *@var string - the mime type uploaded file (NOTE: according to the browser).
  27. */
  28. private $strMimeType = null;
  29. /**
  30. *@var integer - the size of the uploaded file in bytes.
  31. */
  32. private $intSize = null;
  33. /**
  34. *@var string - the error code associated with this file upload.
  35. */
  36. private $intErrorCode = null;
  37. private $aryErrorMessages = array(
  38. UPLOAD_ERR_OK => 'Upload Successful',
  39. UPLOAD_ERR_INI_SIZE => 'File exceeds the upload_max_filesize php directive',
  40. UPLOAD_ERR_FORM_SIZE => 'File exceeds the MAX_FILE_SIZE HTML directive',
  41. UPLOAD_ERR_PARTIAL => 'File was only partially uploaded',
  42. UPLOAD_ERR_NO_FILE => 'No file was uploaded',
  43. UPLOAD_ERR_NO_TMP_DIR => 'The temporary upload directory does not exist',
  44. UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
  45. UPLOAD_ERR_EXTENSION => 'File upload stopped by extension',
  46. );
  47. /**
  48. *@var array - attributes for the form to indicate upload
  49. */
  50. protected $strFormAttributes = array('enctype'=>'multipart/form-data');
  51. /**
  52. *@var integer - the maximum size of the uploaded file in bytes.
  53. */
  54. private $intMaxFileSize = 1000000;
  55. public function ParsePostData(){
  56. if ( array_key_exists($this->strControlId, $_FILES)
  57. && isset($_FILES[$this->strControlId]['tmp_name']) )
  58. {
  59. $this->strTempUri = QType::Cast($_FILES[$this->strControlId]['tmp_name'], QType::String);
  60. $this->strFileName = QType::Cast($_FILES[$this->strControlId]['name'], QType::String);
  61. $this->strMimeType = QType::Cast($_FILES[$this->strControlId]['type'], QType::String);
  62. $this->intSize = QType::Cast($_FILES[$this->strControlId]['size'], QType::Integer);
  63. $this->intErrorCode = QType::Cast($_FILES[$this->strControlId]['error'], QType::Integer);
  64. $pos = strrpos($this->FileName,'.');
  65. if( false !== $pos )
  66. $this->strExtension = substr($this->FileName, $pos +1);
  67. }
  68. }
  69. public function GetJavaScriptAction() {
  70. return "onchange";
  71. }
  72. protected function GetControlHtml(){
  73. $strToReturn = '';
  74. $this->strTempUri = null;
  75. $this->strFileName = null;
  76. $this->strExtension = null;
  77. $this->strMimeType = null;
  78. $this->intSize = null;
  79. $this->intErrorCode = null;
  80. $strStyleAttributes = $this->GetStyleAttributes();
  81. $strStyle = '';
  82. if (! empty($strStyleAttributes) )
  83. $strStyle = sprintf('style="%s"', $strStyleAttributes);
  84. $strToReturn = sprintf('<input type="hidden" name="MAX_FILE_SIZE" value="%d" />',
  85. $this->intMaxFileSize);
  86. $strToReturn .= sprintf('<input type="file" name="%s" id="%s" %s%s />',
  87. $this->strControlId,
  88. $this->strControlId,
  89. $this->GetAttributes(),
  90. $strStyle);
  91. return $strToReturn;
  92. }
  93. public function Validate(){
  94. $this->strValidationError = "";
  95. if ($this->ErrorCode != 0){
  96. $this->strValidationError = $this->ErrorMessage;
  97. return false;
  98. }
  99. if (! $this->blnRequired )
  100. return true;
  101. if (strlen($this->strFileName) > 0)
  102. return true;
  103. if(strlen( $this->strName ))
  104. $this->strValidationError = $this->strName . ' ' . QApplication::Translate('is required');
  105. else
  106. $this->strValidationError = "Filename is required";
  107. return false;
  108. }
  109. public function __get($strName){
  110. switch ($strName){
  111. case "TempUri":
  112. return $this->strTempUri;
  113. case "FileName":
  114. return $this->strFileName;
  115. case "MimeType":
  116. return $this->strMimeType;
  117. case "Size":
  118. return $this->intSize;
  119. case "Extension":
  120. return $this->strExtension;
  121. case "ErrorCode":
  122. return $this->intErrorCode;
  123. case "ErrorMessage":
  124. if($this->intErrorCode !== null)
  125. return $this->aryErrorMessages[$this->intErrorCode];
  126. return "Internal Error - Error code unset.";
  127. case "MaxFileSize":
  128. return $this->intMaxFileSize;
  129. default:
  130. try {
  131. return parent::__get($strName);
  132. } catch (QCallerException $objExc) {
  133. $objExc->IncrementOffset();
  134. throw $objExc;
  135. }
  136. }
  137. }
  138. public function __set($strName, $mixValue){
  139. $this->blnModified = true;
  140. switch ($strName){
  141. case 'MaxFileSize':
  142. try {
  143. return ($this->intMaxFileSize = QType::Cast($mixValue, QType::Integer ));
  144. } catch (QInvalidCastException $objExc) {
  145. $objExc->IncrementOffset();
  146. throw $objExc;
  147. }
  148. default:
  149. try {
  150. parent::__set($strName, $mixValue);
  151. } catch (QCallerException $objExc) {
  152. $objExc->IncrementOffset();
  153. throw $objExc;
  154. }
  155. break;
  156. }
  157. }
  158. }
  159. ?>