Skip to content

Commit

Permalink
Add property calculate_hash_file to allow skip md5_file check.
Browse files Browse the repository at this point in the history
  • Loading branch information
ve3 committed Oct 18, 2021
1 parent 0e84319 commit 67a5bda
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
$Upload->security_scan = true;
// If you upload multiple files, do you want it to be stopped if error occur? (Set to false will skip the error files).
$Upload->stop_on_failed_upload_multiple = false;
// You may set calculate hash file to false if file size is too large to prevent execution timeout. (This is new since 2.0.13).
$Upload->calculate_hash_file = true;

// Begins upload
$upload_result = $Upload->upload();
Expand Down
26 changes: 24 additions & 2 deletions Rundiz/Upload/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class Upload
*/
public $allowed_file_extensions;

/**
* @since 2.0.13
* @var bool Calculate hash file content. Set to `true` to calculate file content such as `md5_file()`, `false` to not calculate. Default is `true`.
* Skip this calculation if file size is too large.
*/
public $calculate_hash_file = true;

/**
* @var array|null The array set of file extensions and its valid mime types for check when process the uploaded files.<br>
* Example:
Expand Down Expand Up @@ -191,7 +198,7 @@ class Upload

/**
* @since 2.0.12
* @var array External security scan result message.
* @var string External security scan result message.
*/
public $externalSecurityScanResultMessage = '';

Expand Down Expand Up @@ -254,7 +261,10 @@ protected static function __($string)
public function clear()
{
$this->allowed_file_extensions = null;
$this->calculate_hash_file = true;
$this->error_messages = array();
$this->externalSecurityScan = array();
$this->externalSecurityScanResultMessage = '';
$this->file_extensions_mime_types = null;
$this->files = array();
$this->input_file_name = null;
Expand Down Expand Up @@ -370,7 +380,7 @@ public function getUploadedData()
$output[$key]['new_name'] = $queue_item['new_name'];
$output[$key]['full_path_new_name'] = $queue_item['move_uploaded_to'];
$output[$key]['mime'] = $mime;
$output[$key]['md5_file'] = (is_file($queue_item['move_uploaded_to']) ? md5_file($queue_item['move_uploaded_to']) : null);
$output[$key]['md5_file'] = ($this->calculate_hash_file === true && is_file($queue_item['move_uploaded_to']) ? md5_file($queue_item['move_uploaded_to']) : null);

unset($file_extension, $mime);
}
Expand Down Expand Up @@ -1303,6 +1313,18 @@ protected function validateOptionsProperties()
$this->allowed_file_extensions = array($this->allowed_file_extensions);
}

if (!is_bool($this->calculate_hash_file)) {
$this->calculate_hash_file = true;
}

if (!is_array($this->externalSecurityScan)) {
$this->externalSecurityScan = array();
}

if (!is_scalar($this->externalSecurityScanResultMessage)) {
$this->externalSecurityScanResultMessage = '';
}

if (!is_array($this->file_extensions_mime_types) && $this->file_extensions_mime_types != null) {
$this->file_extensions_mime_types = null;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/via-http/test-full-customize-file-upload-no-ext-limited.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
if (isset($_POST['security_scan'])) {
$Upload->security_scan = ($_POST['security_scan'] === 'true' ? true : false);
}
if (isset($_POST['calculate_hash_file'])) {
$Upload->calculate_hash_file = ($_POST['calculate_hash_file'] === 'true' ? true : false);
}
if (isset($_POST['stop_on_failed_upload_multiple'])) {
$Upload->stop_on_failed_upload_multiple = ($_POST['stop_on_failed_upload_multiple'] === 'true' ? true : false);
}
Expand All @@ -52,6 +55,9 @@
if (!isset($security_scan)) {
$security_scan = 'false';
}
if (!isset($calculate_hash_file)) {
$calculate_hash_file = 'true';
}
if (!isset($stop_on_failed_upload_multiple)) {
$stop_on_failed_upload_multiple = 'true';
}
Expand Down Expand Up @@ -138,6 +144,14 @@
</select>
<p class="help-block">Scan for PHP and perl code in the file.</p>
</div>
<div class="form-group">
<label>Calculate hash file content</label>
<select name="calculate_hash_file" class="form-control">
<option value="true"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'true') { ?> selected<?php } ?>>Yes</option>
<option value="false"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'false') { ?> selected<?php } ?>>No</option>
</select>
<p class="help-block">Calculate hash file content. Skip this if file size is too large.</p>
</div>
<div>
<button type="submit" class="btn btn-primary">Upload</button>
</div>
Expand Down Expand Up @@ -191,6 +205,14 @@
</select>
<p class="help-block">Scan for PHP and perl code in the file.</p>
</div>
<div class="form-group">
<label>Calculate hash file content</label>
<select name="calculate_hash_file" class="form-control">
<option value="true"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'true') { ?> selected<?php } ?>>Yes</option>
<option value="false"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'false') { ?> selected<?php } ?>>No</option>
</select>
<p class="help-block">Calculate hash file content. Skip this if file size is too large.</p>
</div>
<div class="form-group">
<label>Stop on failed upload occur</label>
<select name="stop_on_failed_upload_multiple" class="form-control">
Expand Down
22 changes: 22 additions & 0 deletions tests/via-http/test-full-customize-file-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
if (isset($_POST['security_scan'])) {
$Upload->security_scan = ($_POST['security_scan'] === 'true' ? true : false);
}
if (isset($_POST['calculate_hash_file'])) {
$Upload->calculate_hash_file = ($_POST['calculate_hash_file'] === 'true' ? true : false);
}
if (isset($_POST['stop_on_failed_upload_multiple'])) {
$Upload->stop_on_failed_upload_multiple = ($_POST['stop_on_failed_upload_multiple'] === 'true' ? true : false);
}
Expand All @@ -53,6 +56,9 @@
if (!isset($security_scan)) {
$security_scan = 'false';
}
if (!isset($calculate_hash_file)) {
$calculate_hash_file = 'true';
}
if (!isset($stop_on_failed_upload_multiple)) {
$stop_on_failed_upload_multiple = 'true';
}
Expand Down Expand Up @@ -140,6 +146,14 @@
</select>
<p class="help-block">Scan for PHP and perl code in the file.</p>
</div>
<div class="form-group">
<label>Calculate hash file content</label>
<select name="calculate_hash_file" class="form-control">
<option value="true"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'true') { ?> selected<?php } ?>>Yes</option>
<option value="false"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'false') { ?> selected<?php } ?>>No</option>
</select>
<p class="help-block">Calculate hash file content. Skip this if file size is too large.</p>
</div>
<div>
<button type="submit" class="btn btn-primary">Upload</button>
</div>
Expand Down Expand Up @@ -194,6 +208,14 @@
</select>
<p class="help-block">Scan for PHP and perl code in the file.</p>
</div>
<div class="form-group">
<label>Calculate hash file content</label>
<select name="calculate_hash_file" class="form-control">
<option value="true"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'true') { ?> selected<?php } ?>>Yes</option>
<option value="false"<?php if (isset($calculate_hash_file) && $calculate_hash_file === 'false') { ?> selected<?php } ?>>No</option>
</select>
<p class="help-block">Calculate hash file content. Skip this if file size is too large.</p>
</div>
<div class="form-group">
<label>Stop on failed upload occur</label>
<select name="stop_on_failed_upload_multiple" class="form-control">
Expand Down

0 comments on commit 67a5bda

Please sign in to comment.