Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/units/helpers/upload_helper.php
Show All 10 Lines | |||||
* or other free or open source software licenses. | * or other free or open source software licenses. | ||||
* See http://www.in-portal.org/license for copyright notices and details. | * See http://www.in-portal.org/license for copyright notices and details. | ||||
*/ | */ | ||||
class kUploadHelper extends kHelper | class kUploadHelper extends kHelper | ||||
{ | { | ||||
/** | /** | ||||
* File helper reference | |||||
* | |||||
* @var FileHelper | |||||
*/ | |||||
protected $fileHelper = null; | |||||
/** | |||||
* Creates kUploadHelper instance. | * Creates kUploadHelper instance. | ||||
*/ | */ | ||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
parent::__construct(); | parent::__construct(); | ||||
$this->fileHelper = $this->Application->recallObject('FileHelper'); | |||||
// 5 minutes execution time | // 5 minutes execution time | ||||
@set_time_limit(5 * 60); | @set_time_limit(5 * 60); | ||||
} | } | ||||
/** | /** | ||||
* Handles the upload. | * Handles the upload. | ||||
* | * | ||||
* @param kEvent $event Event. | * @param kEvent $event Event. | ||||
Show All 39 Lines | |||||
} | } | ||||
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); | ||||
} | } | ||||
/** @var FileHelper $file_helper */ | $filename = $this->fileHelper->ensureUniqueFilename($tmp_path, $filename); | ||||
$file_helper = $this->Application->recallObject('FileHelper'); | |||||
$filename = $file_helper->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 ) { | if ( $storage_format ) { | ||||
/** @var ImageHelper $image_helper */ | $this->resizeUploadedFile($tmp_path . $filename, $storage_format); | ||||
$image_helper = $this->Application->recallObject('ImageHelper'); | |||||
$this->moveUploadedFile($value['tmp_name'] . '.jpg'); // add extension, so ResizeImage can work | |||||
$url = $image_helper->ResizeImage($value['tmp_name'] . '.jpg', $storage_format); | |||||
$tmp_name = preg_replace('/^' . preg_quote($this->Application->BaseURL(), '/') . '/', '/', $url); | |||||
rename($tmp_name, $tmp_path . $filename); | |||||
} | |||||
else { | |||||
$this->moveUploadedFile($tmp_path . $filename); | |||||
} | } | ||||
$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) ) { | ||||
$this->deleteTempFiles($thumbs_path); | $this->deleteTempFiles($thumbs_path); | ||||
} | } | ||||
return preg_replace('/^' . preg_quote($id, '/') . '_/', '', $filename); | return preg_replace('/^' . preg_quote($id, '/') . '_/', '', $filename); | ||||
} | } | ||||
/** | /** | ||||
* Resizes uploaded file. | |||||
* | |||||
* @param string $file_path File path. | |||||
* @param string $format Format. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function resizeUploadedFile($file_path, $format) | |||||
{ | |||||
/** @var ImageHelper $image_helper */ | |||||
$image_helper = $this->Application->recallObject('ImageHelper'); | |||||
// Add extension, so that "ImageHelper::ResizeImage" can work. | |||||
$resize_file_path = tempnam(sys_get_temp_dir(), 'uploaded_') . '.jpg'; | |||||
if ( rename($file_path, $resize_file_path) === false ) { | |||||
return false; | |||||
} | |||||
$resized_file_path = $this->fileHelper->urlToPath( | |||||
$image_helper->ResizeImage($resize_file_path, $format) | |||||
); | |||||
return rename($resized_file_path, $file_path); | |||||
} | |||||
/** | |||||
* Sends headers to ensure, that response is never cached. | * Sends headers to ensure, that response is never cached. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
protected function disableBrowserCache() | protected function disableBrowserCache() | ||||
{ | { | ||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); | header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); | ||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | ||||
▲ Show 20 Lines • Show All 214 Lines • Show Last 20 Lines |