Changeset View
Changeset View
Standalone View
Standalone View
core/units/helpers/upload_helper.php
Show First 20 Lines • Show All 82 Lines • ▼ Show 20 Line(s) | |||||
if ( !is_writable($tmp_path) ) { | if ( !is_writable($tmp_path) ) { | ||||
// 500 Internal Server Error | // 500 Internal Server Error | ||||
// check both temp and live upload directory | // check both temp and live upload directory | ||||
throw new kUploaderException('Write permissions not set on the server, please contact server administrator.', 500); | throw new kUploaderException('Write permissions not set on the server, please contact server administrator.', 500); | ||||
} | } | ||||
$filename = $this->fileHelper->ensureUniqueFilename($tmp_path, $filename); | $filename = $this->fileHelper->ensureUniqueFilename($tmp_path, $filename); | ||||
$storage_format = $this->getStorageFormat($this->Application->GetVar('field'), $event); | $storage_format = $this->getStorageFormat($this->Application->GetVar('field'), $event); | ||||
$this->moveUploadedFile($tmp_path . $filename); | |||||
if ( $storage_format ) { | $file_path = $tmp_path . $filename; | ||||
$this->resizeUploadedFile($tmp_path . $filename, $storage_format); | $actual_file_path = $this->moveUploadedFile($file_path); | ||||
if ( $storage_format && $file_path == $actual_file_path ) { | |||||
$this->resizeUploadedFile($file_path, $storage_format); | |||||
alex: Later this would be replaced by `kUploadHelper::resizeUploadedFile` call from D320. | |||||
} | } | ||||
$this->deleteTempFiles($tmp_path); | $this->deleteTempFiles($tmp_path); | ||||
$thumbs_path = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $tmp_path, 1); | $thumbs_path = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $tmp_path, 1); | ||||
$thumbs_path = FULL_PATH . THUMBS_PATH . $thumbs_path; | $thumbs_path = FULL_PATH . THUMBS_PATH . $thumbs_path; | ||||
if ( file_exists($thumbs_path) ) { | if ( file_exists($thumbs_path) ) { | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Line(s) | |||||
return isset($field_options['storage_format']) ? $field_options['storage_format'] : false; | return isset($field_options['storage_format']) ? $field_options['storage_format'] : false; | ||||
} | } | ||||
/** | /** | ||||
* Moves uploaded file to given location. | * Moves uploaded file to given location. | ||||
* | * | ||||
* @param string $file_path File path. | * @param string $file_path File path. | ||||
* | * | ||||
* @return void | * @return string | ||||
* @throws kUploaderException When upload could not be handled properly. | * @throws kUploaderException When upload could not be handled properly. | ||||
*/ | */ | ||||
protected function moveUploadedFile($file_path) | protected function moveUploadedFile($file_path) | ||||
{ | { | ||||
// Chunking might be enabled | // Chunking might be enabled. | ||||
$chunk = (int)$this->Application->GetVar('chunk', 0); | $chunk = (int)$this->Application->GetVar('chunk', 0); | ||||
$chunks = (int)$this->Application->GetVar('chunks', 0); | $chunks = (int)$this->Application->GetVar('chunks', 0); | ||||
$actual_file_path = $file_path . '.part'; | |||||
// Open temp file | // Open temp file. | ||||
if ( !$out = @fopen("{$file_path}.part", $chunks ? 'ab' : 'wb') ) { | if ( !$out = @fopen($actual_file_path, $chunks ? 'ab' : 'wb') ) { | ||||
Lint: CodingStandard.PHP.NoSilencedErrors.Discouraged: Silencing errors is discouraged | |||||
Assignment in "if" control structure is forbidden Lint: CodingStandard.ControlStructures.AssignmentInCondition.Forbidden: Assignment in "if" control structure is forbidden | |||||
throw new kUploaderException('Failed to open output stream.', 102); | throw new kUploaderException('Failed to open output stream.', 102); | ||||
} | } | ||||
if ( !empty($_FILES) ) { | if ( !empty($_FILES) ) { | ||||
if ( $_FILES['file']['error'] || !is_uploaded_file($_FILES['file']['tmp_name']) ) { | if ( $_FILES['file']['error'] || !is_uploaded_file($_FILES['file']['tmp_name']) ) { | ||||
throw new kUploaderException('Failed to move uploaded file.', 103); | throw new kUploaderException('Failed to move uploaded file.', 103); | ||||
} | } | ||||
// Read binary input stream and append it to temp file | // Read binary input stream and append it to temp file. | ||||
if ( !$in = @fopen($_FILES['file']['tmp_name'], 'rb') ) { | if ( !$in = @fopen($_FILES['file']['tmp_name'], 'rb') ) { | ||||
Lint: CodingStandard.PHP.NoSilencedErrors.Discouraged: Silencing errors is discouraged | |||||
Assignment in "if" control structure is forbidden Lint: CodingStandard.ControlStructures.AssignmentInCondition.Forbidden: Assignment in "if" control structure is forbidden | |||||
throw new kUploaderException('Failed to open input stream.', 101); | throw new kUploaderException('Failed to open input stream.', 101); | ||||
} | } | ||||
} | } | ||||
else { | else { | ||||
if ( !$in = @fopen('php://input', 'rb') ) { | if ( !$in = @fopen('php://input', 'rb') ) { | ||||
throw new kUploaderException('Failed to open input stream.', 101); | throw new kUploaderException('Failed to open input stream.', 101); | ||||
} | } | ||||
} | } | ||||
while ( $buff = fread($in, 4096) ) { | while ( $buff = fread($in, 4096) ) { | ||||
fwrite($out, $buff); | fwrite($out, $buff); | ||||
} | } | ||||
@fclose($out); | @fclose($out); | ||||
@fclose($in); | @fclose($in); | ||||
// Check if file has been uploaded | // Check if file has been uploaded. | ||||
if ( !$chunks || $chunk == $chunks - 1 ) { | if ( !$chunks || $chunk == $chunks - 1 ) { | ||||
// Strip the temp .part suffix off | // Strip the temp .part suffix off. | ||||
rename("{$file_path}.part", $file_path); | rename($actual_file_path, $file_path); | ||||
$actual_file_path = $file_path; | |||||
} | } | ||||
return $actual_file_path; | |||||
} | } | ||||
/** | /** | ||||
* Delete temporary files, that won't be used for sure | * Delete temporary files, that won't be used for sure | ||||
* | * | ||||
* @param string $path | * @param string $path | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |
Later this would be replaced by kUploadHelper::resizeUploadedFile call from D320.