Skip to content

Commit

Permalink
allow bulletproof to capture browser limit exceeded error
Browse files Browse the repository at this point in the history
  • Loading branch information
samayo committed Sep 14, 2017
1 parent 9861c93 commit f5ee15e
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions src/bulletproof.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* BulletProof
* BULLETPROOF
*
* Bulletproof is a single-class, secure and user-friendly Image uploader in php.
* Bulletproof is a single-class library to upload images in PHP with security.
*
* PHP support 5.3+
*
* @package bulletproof
* @version 3.0.0
* @version 3.1.0
* @author https://twitter.com/_samayo
* @link https://github.com/samayo/bulletproof
* @license MIT
Expand Down Expand Up @@ -89,14 +89,14 @@ class Image implements \ArrayAccess
* @var array error messages strings
*/
protected $common_upload_errors = array(
UPLOAD_ERR_OK => '',
UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server',
UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser',
UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later',
UPLOAD_ERR_NO_FILE => 'Image is not found',
UPLOAD_ERR_OK => '',
UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server',
UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser',
UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later',
UPLOAD_ERR_NO_FILE => 'Image is not found',
UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )',
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions',
UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process'
UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process'
);

/**
Expand Down Expand Up @@ -167,6 +167,20 @@ public function offsetGet($offset)
return $this->error;
}

if(!isset($this->_files[$offset])){
return ;
}

/* check for common upload errors */
if ($this->_files[$offset]['error'] == 2) {
$this->error = "Image is larger than specified by the browser";
return ;
}

if ($this->error || $this->error = $this->commonUploadErrors($this->_files[$offset]['error'])) {
return false;
}

if (isset($this->_files[$offset]) && file_exists($this->_files[$offset]['tmp_name'])) {
$this->_files = $this->_files[$offset];
return true;
Expand Down Expand Up @@ -228,13 +242,20 @@ public function setSize($min, $max)
*/
public function setLocation($dir = 'bulletproof', $permission = 0666)
{

if (!file_exists($dir) && !is_dir($dir) && !$this->location) {
$createFolder = @mkdir('' . $dir, (int)$permission, true);
if (!$createFolder) {
$this->error = 'Error! Folder ' . $dir . ' could not be created';
return false;
}
}

/* check if we can create a file in the directory */
if (!is_writable($dir)) {
$this->error = "The images directory \"" . $dir . "\" is not writable!";
return false;
}

$this->location = $dir;
return $this;
Expand Down Expand Up @@ -262,7 +283,7 @@ public function setDimension($maxWidth, $maxHeight)
public function getName()
{
if (!$this->name) {
return uniqid(true) . '_' . str_shuffle(implode(range('e', 'q')));
return uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q')));
}

return $this->name;
Expand Down Expand Up @@ -388,11 +409,6 @@ public function upload()
$image = $this;
$files = $this->_files;

/* check for common upload errors */
if ($image->error || $image->error = $image->commonUploadErrors($files['error'])) {
return false;
}

/* check image for valid mime types and return mime */
$image->mime = $image->getImageMime($files['tmp_name']);

Expand All @@ -416,8 +432,9 @@ public function upload()
/* check image size based on the settings */
if ($files['size'] < $minSize || $files['size'] > $maxSize) {
$min = intval($minSize / 1000) ?: 1;
$image->error = 'Image size should be at least more than ' . $min . ' kb';
return false;
$max = intval($maxSize / 1000) ?: 1;
$image->error = "Image size should be at least " . $min . " KB, and no more than " . $max . " KB";
return null;
}

/* check image dimension */
Expand Down

0 comments on commit f5ee15e

Please sign in to comment.