Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Thu, Jul 24, 7:10 PM

in-portal

Index: branches/RC/core/kernel/utility/formatters/upload_formatter.php
===================================================================
--- branches/RC/core/kernel/utility/formatters/upload_formatter.php (revision 9252)
+++ branches/RC/core/kernel/utility/formatters/upload_formatter.php (revision 9253)
@@ -1,300 +1,322 @@
<?php
class kUploadFormatter extends kFormatter
{
var $DestinationPath;
var $FullPath;
function kUploadFormatter()
{
parent::kBase();
if ($this->DestinationPath) {
$this->FullPath = FULL_PATH.$this->DestinationPath;
}
}
/**
* Processes file uploads from form
*
* @param mixed $value
* @param string $field_name
* @param kDBItem $object
* @return string
*/
function Parse($value, $field_name, &$object)
{
$ret = '';
$options = $object->GetFieldOptions($field_name);
if(getArrayValue($options, 'upload_dir'))
{
$this->DestinationPath = $options['upload_dir'];
$this->FullPath = FULL_PATH.$this->DestinationPath;
}
// SWF Uploader
if (is_array($value) && isset($value['tmp_ids'])) {
if ($value['tmp_deleted']) {
$deleted = explode('|', $value['tmp_deleted']);
$upload = explode('|', $value['upload']);
$n_upload = array();
// $n_ids = array();
foreach ($upload as $name) {
if (in_array($name, $deleted)) continue;
$n_upload[] = $name;
// $n_ids[] = $name;
}
$value['upload'] = implode('|', $n_upload);
// $value['tmp_ids'] = implode('|', $n_ids);
}
if (!$value['tmp_ids']) {
return getArrayValue($value, 'upload');
}
$swf_uploaded_ids = explode('|', $value['tmp_ids']);
$swf_uploaded_names = explode('|', $value['tmp_names']);
$existing = $value['upload'] ? explode('|', $value['upload']) : array();
if (isset($options['multiple'])) {
$max_files = $options['multiple'] == false ? 1 : $options['multiple'];
}
else {
$max_files = 1;
}
$fret = array();
for ($i=0; $i<min($max_files, count($swf_uploaded_ids)); $i++) {
$real_name = $swf_uploaded_names[$i];
$real_name = $this->ValidateFileName($this->FullPath, $real_name);
$file_name = $this->FullPath.$real_name;
$tmp_file = WRITEABLE.'/tmp/'.$swf_uploaded_ids[$i].'_'.$swf_uploaded_names[$i];
rename($tmp_file, $file_name);
@chmod($file_name, 0666);
$fret[] = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath.$real_name;
}
$fret = array_merge($existing, $fret);
return implode('|', $fret);
}
// SWF Uploader END
if (getArrayValue($value, 'upload') && getArrayValue($value, 'error') == UPLOAD_ERR_NO_FILE) {
// file was not uploaded this time, but was uploaded before, then use previously uploaded file (from db)
return getArrayValue($value, 'upload');
}
if (is_array($value) && count($value) > 1 && $value['size']) {
if (is_array($value) && $value['error'] === UPLOAD_ERR_OK) {
$max_filesize = isset($options['max_size']) ? $options['max_size'] : MAX_UPLOAD_SIZE;
if (getArrayValue($options, 'allowed_types') && !in_array($value['type'], $options['allowed_types'])) {
$object->SetError($field_name, 'bad_file_format', 'la_error_InvalidFileFormat');
}
elseif ($value['size'] > $max_filesize) {
$object->SetError($field_name, 'bad_file_size', 'la_error_FileTooLarge');
}
elseif (!is_writable($this->FullPath)) {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
else {
$real_name = $this->ValidateFileName($this->FullPath, $value['name']);
$file_name = $this->FullPath.$real_name;
if (!move_uploaded_file($value['tmp_name'], $file_name)) {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
else {
@chmod($file_name, 0666);
if (getArrayValue($options, 'size_field')) {
$object->SetDBField($options['size_field'], $value['size']);
}
if (getArrayValue($options, 'orig_name_field')) {
$object->SetDBField($options['orig_name_field'], $value['name']);
}
if (getArrayValue($options, 'content_type_field')) {
$object->SetDBField($options['content_type_field'], $value['type']);
}
$ret = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath.$real_name;
// delete previous file, when new file is uploaded under same field
/*$previous_file = isset($value['upload']) ? $value['upload'] : false;
if ($previous_file && file_exists($this->FullPath.$previous_file)) {
unlink($this->FullPath.$previous_file);
}*/
}
}
}
else {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
}
else {
if (getArrayValue($options, 'required')) {
$object->SetError($field_name, 'required');
}
}
// && !$object->FieldErrors[$field_name]['pseudo'] - already implemented in kDBItem::SetError method
if ((count($value) > 1) && $value['error'] && ($value['error'] != UPLOAD_ERR_NO_FILE)) {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
return $ret;
}
- function Format($value, $field_name, &$object, $format=null)
+ function getSingleFormat($format)
{
- if ( is_null($value) ) return '';
+ $single_mapping = Array (
+ 'file_urls' => 'full_url',
+ 'file_sizes' => 'file_size',
+ 'files_resized' => 'resize',
+ );
- $options = $object->GetFieldOptions($field_name);
- if ( isset($format) ) $options['format'] = $format;
+ return $single_mapping[$format];
+ }
- if ($format == 'file_urls' || $format == 'file_names' || $format == 'file_sizes' || substr($format, 0, 13) == 'files_resized') {
- $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath;
- $files = explode('|', $value);
- $urls = array();
- $names = array();
- if ($value) { // if value string was not empty
- foreach ($files as $a_file) {
- if (isset($options['direct_links']) && $options['direct_links']) {
- $urls[] = rtrim($this->Application->BaseURL(), '/').$upload_dir.$a_file;
- }
- else {
- $urls[] = $this->Application->HREF('', '', array('no_amp'=>1, 'pass'=>'m,'.$object->Prefix, $object->Prefix.'_event'=>'OnViewFile', 'file'=>$a_file, 'field'=>$field_name));
- }
- $names[] = $a_file;
- $sizes[] = filesize(FULL_PATH.$upload_dir.$a_file);
- }
- }
+ /**
+ * Return formatted file url,path or size (or same for multiple files)
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem $object
+ * @param string $format
+ * @return string
+ */
+ function Format($value, $field_name, &$object, $format = null)
+ {
+ if (is_null($value)) {
+ return '';
+ }
- if (preg_match('/files_resized:([\d]*)x([\d]*)/', $format, $regs)) {
- $resized = Array();
- foreach ($files as $a_file) {
- $resized[] = $this->resizeImage(FULL_PATH.$upload_dir.$a_file, $regs[1], $regs[2]);
- }
- return implode('|', $resized);
- }
+ $options = $object->GetFieldOptions($field_name);
+ if (!isset($format)) {
+ $format = isset($options['format']) ? $options['format'] : false;
+ }
- switch ($format) {
- case 'file_urls':
- return implode('|', $urls);
- break;
+ if ($format && preg_match('/(file_urls|file_names|file_sizes|files_resized)(.*)/', $format, $regs)) {
+ if (!$value || $format == 'file_names') {
+ // storage format matches display format OR no value
+ return $value;
+ }
- case 'file_names':
- return implode('|', $names);
- break;
+ $ret = Array ();
+ $files = explode('|', $value);
+ $format = $this->getSingleFormat($regs[1]).$regs[2];
- case 'file_sizes':
- return implode('|', $sizes);
- break;
+ foreach ($files as $a_file) {
+ $ret[] = $this->GetFormatted($a_file, $field_name, $object, $format);
}
- return implode('|', $format == 'file_urls' ? $urls : $names);
+
+ return implode('|', $ret);
}
$tc_value = $this->TypeCast($value, $options);
if( ($tc_value === false) || ($tc_value != $value) ) return $value; // for leaving badly formatted date on the form
- return $this->GetFormatted($tc_value, $options);
+ $object->Fields[$field_name]['direct_links'] = true; // for case, when non-swf uploader is used
+ return $this->GetFormatted($tc_value, $field_name, $object, $format);
}
- function GetFormatted($tc_value, &$options)
+ /**
+ * Return formatted file url,path or size
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem $object
+ * @param string $format
+ * @return string
+ */
+ function GetFormatted($value, $field_name, &$object, $format = null)
{
- if (isset($options['format'])) {
- $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath;
+ if (!$format) {
+ return $value;
+ }
- if (preg_match('/resize:([\d]*)x([\d]*)/', $options['format'], $regs)) {
- return $this->resizeImage(FULL_PATH.$upload_dir.$tc_value, $regs[1], $regs[2]);
- }
+ $options = $object->GetFieldOptions($field_name);
+ $upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath;
+
+ if (preg_match('/resize:([\d]*)x([\d]*)/', $format, $regs)) {
+ return $this->resizeImage(FULL_PATH.$upload_dir.$value, $regs[1], $regs[2]);
+ }
- switch ($options['format']) {
- case 'full_url':
- return rtrim($this->Application->BaseURL(), '/').$upload_dir.$tc_value;
- break;
+ switch ($format) {
+ case 'full_url':
+ if (isset($options['direct_links']) && $options['direct_links']) {
+ return rtrim($this->Application->BaseURL(), '/').$upload_dir.$value;
+ }
+ else {
+ $url_params = Array (
+ 'no_amp' => 1, 'pass' => 'm,'.$object->Prefix,
+ $object->Prefix.'_event' => 'OnViewFile',
+ 'file' => $value, 'field' => $field_name
+ );
+ return $this->Application->HREF('', '', $url_params);
+ }
+ break;
- case 'full_path':
- return FULL_PATH.$upload_dir.$tc_value;
- break;
+ case 'full_path':
+ return FULL_PATH.$upload_dir.$value;
+ break;
- default:
- return sprintf($options['format'], $tc_value);
- break;
- }
+ case 'file_size':
+ return filesize(FULL_PATH.$upload_dir.$value);
+ break;
}
- return $tc_value;
+ return sprintf($format, $value);
}
function resizeImage($src_image, $max_width, $max_height)
{
if ($max_width > 0 || $max_height > 0) {
$image_helper =& $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
list ($max_width, $max_height, $resized) = $image_helper->GetImageDimensions($src_image, $max_width, $max_height);
if ($resized) {
$src_path = dirname($src_image);
$dst_image = preg_replace('/^'.preg_quote($src_path, '/').'(.*)\.(.*)$/', $src_path.'/resized\\1_'.$max_width.'x'.$max_height.'.\\2', $src_image);
if (!file_exists($dst_image) || filemtime($src_image) > filemtime($dst_image)) {
// resized image not available OR should be recreated due source image change
$image_resized = $image_helper->ScaleImage($src_image, $dst_image, $max_width, $max_height);
if (!$image_resized) {
// resize failed, because of server error
$dst_image = $src_image;
}
}
// resize ok
$src_image = $dst_image;
}
}
$base_url = rtrim($this->Application->BaseURL(), '/');
return preg_replace('/^'.preg_quote(FULL_PATH, '/').'(.*)/', $base_url.'\\1', $src_image);
}
function ValidateFileName($path, $name)
{
$parts = pathinfo($name);
$ext = '.'.$parts['extension'];
$filename = substr($parts['basename'], 0, -strlen($ext));
$new_name = $filename.$ext;
while ( file_exists($path.'/'.$new_name) )
{
if ( preg_match('/('.preg_quote($filename, '/').'_)([0-9]*)('.preg_quote($ext, '/').')/', $new_name, $regs) ) {
$new_name = $regs[1].($regs[2]+1).$regs[3];
}
else {
$new_name = $filename.'_1'.$ext;
}
}
return $new_name;
}
}
class kPictureFormatter extends kUploadFormatter
{
function kPictureFormatter()
{
$this->NakeLookupPath = IMAGES_PATH; // used ?
$this->DestinationPath = constOn('ADMIN') ? IMAGES_PENDING_PATH : IMAGES_PATH;
parent::kUploadFormatter();
}
- function GetFormatted($tc_value, &$options)
+ function GetFormatted($value, $field_name, &$object, $format = null)
{
- if (isset($options['format']) && ($options['format'] == 'img_size')) {
+ if ($format == 'img_size') {
$upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath;
- $img_path = FULL_PATH.'/'.$upload_dir.$tc_value;
+ $img_path = FULL_PATH.'/'.$upload_dir.$value;
$image_info = @getimagesize($img_path);
return ' width="'.$image_info[0].'" height="'.$image_info[1].'"';
}
- return parent::GetFormatted($tc_value, $options);
+ return parent::GetFormatted($value, $field_name, $object, $format);
}
}
\ No newline at end of file
Property changes on: branches/RC/core/kernel/utility/formatters/upload_formatter.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.10.2.4
\ No newline at end of property
+1.10.2.5
\ No newline at end of property

Event Timeline