Пример #1
0
 /**
  * 	method:	getTempFilename
  *
  * 	todo: write documentation
  */
 public static function getTempFilename($name)
 {
     $file = Amslib_FILES::get($name);
     return $file && isset($file["tmp_name"]) ? $file["tmp_name"] : false;
 }
Пример #2
0
 /**
  * method:	validate_file
  *
  * Validates the presence of a file in the uploaded data
  * (this only works in PHP by extracting the info from the $_FILES superglobal)
  *
  * parameters:
  * 	name		-	The name of the field
  * 	value		-	The value of the field
  * 	required	-	Boolean true or false, whether the field is mandatory or not
  * 	options		-	Validation restrictions
  *
  * returns:
  * 	If failed because file was too large for the PHP configuration, return "FILE_EXCEED_INI_SIZE"
  * 	If failed because file exceeded the HTML designated form size, return "FILE_EXCEED_FORM_SIZE"
  * 	If failed because file was only partially uploaded, return "FILE_PARTIAL_FILE"
  * 	If failed because file was missing or could not be located, return "FILE_MISSING_FILE"
  * 	If failed because there was no tmp directory to upload (perhaps it was invalid) return "FILE_NO_TMP_DIRECTORY"
  * 	If failed because tmp directory cannot be written to, return "FILE_CANNOT_WRITE"
  * 	If failed because file extension was banned (disallowed, such as .exe), return "FILE_BANNED_EXTENSION"
  * 	If failed because file could not be found in the $_FILES array, return "FILE_NOT_FOUND"
  *
  * operations:
  * 	-	Test the value was NULL or not (NULL means file didnt upload, or not uploaded)
  * 	-	If the file was NOT NULL, test further
  * 	-	Test the file exists on disk
  * 	-	If the file exists, set the validData and return true
  * 	-	If the file doesnt exist, but the file is not required, just return true anyway
  * 	-	If the file was required, but not found, interrogate the error to find the correct error message to return
  * 	-	if none of those errors matched the situation, then look to see whether the value was required
  * 	-	If an error could not be found, but the file cannot be found either BUT the file is not required,
  * 		just put a REQUEST_FILE_NOT_FOUND message and return true (validated ok, but couldnt find anything
  * 		when it was expected, it's not an error, it's just a warning)
  * 	-	If none of these match, then return "FILE_NOT_FOUND"
  */
 protected function validate_file($name, $value, $required, $options)
 {
     $value = Amslib_FILES::get($name);
     $valid = true;
     if ($value !== NULL) {
         //	Check the file is in the allowed types
         if (isset($options["approved_types"])) {
             if (is_string($options["approved_types"])) {
                 $options["approved_types"] = array($options["approved_types"]);
             }
             if (count($options["approved_types"]) && !in_array($value["type"], $options["approved_types"])) {
                 $valid = "NOT_APPROVED_TYPE";
             }
         }
         //	Check the file is not in the disallowed types
         if (isset($options["rejected_types"])) {
             if (is_string($options["rejected_types"])) {
                 $options["rejected_types"] = array($options["rejected_types"]);
             }
             if (count($options["rejected_types"]) && in_array($value["type"], $options["rejected_types"])) {
                 $valid = "WAS_REJECTED_TYPE";
             }
         }
         if ($valid === true && strlen($value["tmp_name"]) && is_file($value["tmp_name"])) {
             $this->setValid($name, $value);
             return true;
         }
     }
     if ($required == false) {
         return true;
     }
     if ($value) {
         //	Return some alternative errors to FILE_NOT_FOUND
         if ($value["error"] == UPLOAD_ERR_INI_SIZE) {
             return "FILE_EXCEED_INI_SIZE";
         }
         if ($value["error"] == UPLOAD_ERR_FORM_SIZE) {
             return "FILE_EXCEED_FORM_SIZE";
         }
         if ($value["error"] == UPLOAD_ERR_PARTIAL) {
             return "FILE_PARTIAL_FILE";
         }
         if ($value["error"] == UPLOAD_ERR_NO_FILE) {
             return "FILE_MISSING_FILE";
         }
         if ($value["error"] == UPLOAD_ERR_NO_TMP_DIR) {
             return "FILE_NO_TMP_DIRECTORY";
         }
         if ($value["error"] == UPLOAD_ERR_CANT_WRITE) {
             return "FILE_CANNOT_WRITE";
         }
         if ($value["error"] == UPLOAD_ERR_EXTENSION) {
             return "FILE_BANNED_EXTENSION";
         }
         if ($valid !== true) {
             return "FILE_{$valid}";
         }
         //	Unknown error, just comment it here so I can't lose the info
         return "FILE_UNKNOWN_ERROR[{$value["error"]}]";
     }
     return "FILE_NOT_FOUND";
 }