/**
  * Validates a $_FILES array against the upload configuration
  * 
  * @param array $file_array  The $_FILES array for a single file
  * @return string  The validation error message
  */
 private function validateField($file_array)
 {
     if (empty($file_array['name'])) {
         if ($this->required) {
             return self::compose('Please upload a file');
         }
         return NULL;
     }
     if ($file_array['error'] == UPLOAD_ERR_FORM_SIZE || $file_array['error'] == UPLOAD_ERR_INI_SIZE) {
         $max_size = !empty($_POST['MAX_FILE_SIZE']) ? $_POST['MAX_FILE_SIZE'] : ini_get('upload_max_filesize');
         $max_size = !is_numeric($max_size) ? fFilesystem::convertToBytes($max_size) : $max_size;
         return self::compose('The file uploaded is over the limit of %s', fFilesystem::formatFilesize($max_size));
     }
     if ($this->max_size && $file_array['size'] > $this->max_size) {
         return self::compose('The file uploaded is over the limit of %s', fFilesystem::formatFilesize($this->max_size));
     }
     if (empty($file_array['tmp_name']) || empty($file_array['size'])) {
         if ($this->required) {
             return self::compose('Please upload a file');
         }
         return NULL;
     }
     if (!empty($this->mime_types) && file_exists($file_array['tmp_name'])) {
         $contents = file_get_contents($file_array['tmp_name'], FALSE, NULL, 0, 4096);
         if (!in_array(fFile::determineMimeType($file_array['name'], $contents), $this->mime_types)) {
             return self::compose($this->mime_type_message);
         }
     }
     if (!$this->allow_php) {
         $file_info = fFilesystem::getPathInfo($file_array['name']);
         if (in_array(strtolower($file_info['extension']), array('php', 'php4', 'php5'))) {
             return self::compose('The file uploaded is a PHP file, but those are not permitted');
         }
     }
     if (!$this->allow_dot_files) {
         if (substr($file_array['name'], 0, 1) == '.') {
             return self::compose('The name of the uploaded file may not being with a .');
         }
     }
     if ($this->image_dimensions && file_exists($file_array['tmp_name'])) {
         if (fImage::isImageCompatible($file_array['tmp_name'])) {
             list($width, $height, $other) = getimagesize($file_array['tmp_name']);
             if ($this->image_dimensions['min_width'] && $width < $this->image_dimensions['min_width']) {
                 return self::compose('The uploaded image is narrower than the minimum width of %spx', $this->image_dimensions['min_width']);
             }
             if ($this->image_dimensions['min_height'] && $height < $this->image_dimensions['min_height']) {
                 return self::compose('The uploaded image is shorter than the minimum height of %spx', $this->image_dimensions['min_height']);
             }
             if ($this->image_dimensions['max_width'] && $width > $this->image_dimensions['max_width']) {
                 return self::compose('The uploaded image is wider than the maximum width of %spx', $this->image_dimensions['max_width']);
             }
             if ($this->image_dimensions['max_height'] && $height > $this->image_dimensions['max_height']) {
                 return self::compose('The uploaded image is taller than the maximum height of %spx', $this->image_dimensions['max_height']);
             }
         }
     }
     if ($this->image_ratio && file_exists($file_array['tmp_name'])) {
         if (fImage::isImageCompatible($file_array['tmp_name'])) {
             list($width, $height, $other) = getimagesize($file_array['tmp_name']);
             if ($this->image_ratio['allow_excess_dimension'] == 'width' && $width / $height < $this->image_ratio['width'] / $this->image_ratio['height']) {
                 return self::compose('The uploaded image is too narrow for its height. The required ratio is %1$sx%2$s or wider.', $this->image_ratio['width'], $this->image_ratio['height']);
             }
             if ($this->image_ratio['allow_excess_dimension'] == 'height' && $width / $height > $this->image_ratio['width'] / $this->image_ratio['height']) {
                 return self::compose('The uploaded image is too short for its width. The required ratio is %1$sx%2$s or taller.', $this->image_ratio['width'], $this->image_ratio['height']);
             }
         }
     }
 }
 /**
  * Gets the disk usage of the directory and all files and folders contained within
  * 
  * This method may return incorrect results if files over 2GB exist and the
  * server uses a 32 bit operating system
  * 
  * @param  boolean $format          If the filesize should be formatted for human readability
  * @param  integer $decimal_places  The number of decimal places to format to (if enabled)
  * @return integer|string  If formatted, a string with filesize in b/kb/mb/gb/tb, otherwise an integer
  */
 public function getSize($format = FALSE, $decimal_places = 1)
 {
     $this->tossIfDeleted();
     $size = 0;
     $children = $this->scan();
     foreach ($children as $child) {
         $size += $child->getSize();
     }
     if (!$format) {
         return $size;
     }
     return fFilesystem::formatFilesize($size, $decimal_places);
 }
Exemple #3
0
 /**
  * Gets the size of the file
  * 
  * The return value may be incorrect for files over 2GB on 32-bit OSes.
  * 
  * @param  boolean $format          If the filesize should be formatted for human readability
  * @param  integer $decimal_places  The number of decimal places to format to (if enabled)
  * @return integer|string  If formatted a string with filesize in b/kb/mb/gb/tb, otherwise an integer
  */
 public function getSize($format = FALSE, $decimal_places = 1)
 {
     $this->tossIfDeleted();
     // This technique can overcome signed integer limit
     $size = sprintf("%u", filesize($this->file));
     if (!$format) {
         return $size;
     }
     return fFilesystem::formatFilesize($size, $decimal_places);
 }
Exemple #4
0
 /**
  * Validates the uploaded file, ensuring a file was actually uploaded and that is matched the restrictions put in place
  * 
  * @throws fValidationException  When no file is uploaded or the uploaded file violates the options set for this object
  * 
  * @param  string  $field  The field the file was uploaded through
  * @param  integer $index  If the field was an array of file uploads, this specifies which one to validate
  * @return void
  */
 public function validate($field, $index = NULL)
 {
     if (!self::check($field)) {
         throw new fProgrammerException('The field specified, %s, does not appear to be a file upload field', $field);
     }
     $file_array = $this->extractFileUploadArray($field, $index);
     // Do some validation of the file provided
     if (empty($file_array['name'])) {
         throw new fValidationException('Please upload a file');
     }
     if ($file_array['error'] == UPLOAD_ERR_FORM_SIZE || $file_array['error'] == UPLOAD_ERR_INI_SIZE) {
         $max_size = !empty($_POST['MAX_FILE_SIZE']) ? $_POST['MAX_FILE_SIZE'] : ini_get('upload_max_filesize');
         $max_size = !is_numeric($max_size) ? fFilesystem::convertToBytes($max_size) : $max_size;
         $msg = $this->max_message != "" ? $this->max_message : 'The file uploaded is over the limit of %s';
         throw new fValidationException($msg, fFilesystem::formatFilesize($max_size));
     }
     if ($this->max_file_size && $file_array['size'] > $this->max_file_size) {
         $msg = $this->max_message != "" ? $this->max_message : 'The file uploaded is over the limit of %s';
         throw new fValidationException($msg, fFilesystem::formatFilesize($this->max_file_size));
     }
     if (empty($file_array['tmp_name']) || empty($file_array['size'])) {
         throw new fValidationException('Please upload a file');
     }
     if (!empty($this->mime_types) && file_exists($file_array['tmp_name']) && !in_array(fFile::determineMimeType($file_array['tmp_name']), $this->mime_types)) {
         throw new fValidationException($this->mime_type_message);
     }
     if (!$this->allow_php) {
         $file_info = fFilesystem::getPathInfo($file_array['name']);
         if (in_array(strtolower($file_info['extension']), array('php', 'php4', 'php5'))) {
             throw new fValidationException('The file uploaded is a PHP file, but those are not permitted');
         }
     }
     return $file_array;
 }
Exemple #5
0
 /**
  * Sends a message via the SMTP server
  * 
  * @internal
  * 
  * @throws fValidationException  When the message is too large for the server
  * 
  * @param string $from     The email address being sent from - this will be used as the `Return-Path` header
  * @param array  $to       All of the To, Cc and Bcc email addresses to send the message to - this does not affect the message headers in any way
  * @param string $headers  The message headers - the Bcc header will be removed if present
  * @param string $body     The mail body
  * @return void
  */
 public function send($from, $to, $headers, $body)
 {
     $this->connect();
     // Lines starting with . need to start with two .s because the leading
     // . will be stripped
     $body = preg_replace('#^\\.#m', '..', $body);
     // Removed the Bcc header incase the SMTP server doesn't
     $headers = preg_replace('#^Bcc:(.*?)\\r\\n([^ ])#mi', '\\2', $headers);
     // Add the Date header
     $headers = "Date: " . date('D, j M Y H:i:s O') . "\r\n" . $headers;
     $data = $headers . "\r\n\r\n" . $body;
     if ($this->max_size && strlen($data) > $this->max_size) {
         throw new fValidationException('The email provided is %1$s, which is larger than the maximum size of %2$s that the server supports', fFilesystem::formatFilesize(strlen($data)), fFilesystem::formatFilesize($this->max_size));
     }
     $mail_from = "MAIL FROM:<" . $from . ">";
     if ($this->pipelining) {
         $expect = 2;
         $rcpt_to = '';
         foreach ($to as $email) {
             $rcpt_to .= "RCPT TO:<" . $email . ">\r\n";
             $expect++;
         }
         $rcpt_to = trim($rcpt_to);
         $this->write($mail_from . "\r\n" . $rcpt_to . "\r\nDATA\r\n", $expect);
     } else {
         $this->write($mail_from, 1);
         foreach ($to as $email) {
             $this->write("RCPT TO:<" . $email . ">", 1);
         }
         $this->write('DATA', 1);
     }
     $this->write($data . "\r\n.\r\n", 1);
     $this->write('RSET', 1);
 }
Exemple #6
0
 /**
  * Validates a $_FILES array against the upload configuration
  * 
  * @param array $file_array  The $_FILES array for a single file
  * @return string  The validation error message
  */
 private function validateField($file_array)
 {
     if (empty($file_array['name'])) {
         if ($this->required) {
             return self::compose('Please upload a file');
         }
         return NULL;
     }
     if ($file_array['error'] == UPLOAD_ERR_FORM_SIZE || $file_array['error'] == UPLOAD_ERR_INI_SIZE) {
         $max_size = !empty($_POST['MAX_FILE_SIZE']) ? $_POST['MAX_FILE_SIZE'] : ini_get('upload_max_filesize');
         $max_size = !is_numeric($max_size) ? fFilesystem::convertToBytes($max_size) : $max_size;
         return self::compose('The file uploaded is over the limit of %s', fFilesystem::formatFilesize($max_size));
     }
     if ($this->max_size && $file_array['size'] > $this->max_size) {
         return self::compose('The file uploaded is over the limit of %s', fFilesystem::formatFilesize($this->max_size));
     }
     if (empty($file_array['tmp_name']) || empty($file_array['size'])) {
         if ($this->required) {
             return self::compose('Please upload a file');
         }
         return NULL;
     }
     if (!empty($this->mime_types) && file_exists($file_array['tmp_name'])) {
         $contents = file_get_contents($file_array['tmp_name'], FALSE, NULL, 0, 4096);
         if (!in_array(fFile::determineMimeType($file_array['name'], $contents), $this->mime_types)) {
             return self::compose($this->mime_type_message);
         }
     }
     if (!$this->allow_php) {
         $file_info = fFilesystem::getPathInfo($file_array['name']);
         if (in_array(strtolower($file_info['extension']), array('php', 'php4', 'php5'))) {
             return self::compose('The file uploaded is a PHP file, but those are not permitted');
         }
     }
     if (!$this->allow_dot_files) {
         if (substr($file_array['name'], 0, 1) == '.') {
             return self::compose('The name of the uploaded file may not being with a .');
         }
     }
 }