| 1 |
<?php |
| 2 |
class AccuaForm_Element_File extends Element_File { |
| 3 |
protected $destPath; |
| 4 |
protected $alreadySubmittedText; |
| 5 |
protected $validExtensions = array('txt','doc','rtf','pdf','jpg','jpeg','png','zip','gz','bz','bz2'); |
| 6 |
protected $maxSize; |
| 7 |
protected $limitsText; |
| 8 |
protected $errorsText; |
| 9 |
|
| 10 |
public function __construct($label, $name, array $properties = null) { |
| 11 |
$this->alreadySubmittedText = __("File already submitted.", 'accua-form-api'); |
| 12 |
$this->limitsText = __("Maximum filesize: %size%. Allowed extensions: %extensions%.", 'accua-form-api'); |
| 13 |
$this->errorsText = array( |
| 14 |
'upload' => __('Upload failed, please retry. If the problem persists please contact us', 'accua-form-api'), |
| 15 |
'size' => __('Uploaded file is too big', 'accua-form-api'), |
| 16 |
'empty' => __('Uploaded file is empty', 'accua-form-api'), |
| 17 |
'name' => __('File name is not valid. Please rename it avoiding unusual characters', 'accua-form-api'), |
| 18 |
'ext' => __('File extension not allowed. Allowed extensions are %extensions%', 'accua-form-api'), |
| 19 |
); |
| 20 |
$this->destPath = realpath(ABSPATH . '/wp-content/uploads/accua-forms').'/'; |
| 21 |
$this->maxSize = self::file_upload_max_size(); |
| 22 |
parent::__construct($label, $name, $properties); |
| 23 |
} |
| 24 |
|
| 25 |
public function __sleep() { |
| 26 |
return array("attributes", "label", "validation", 'alreadySubmittedText', 'validExtensions', 'maxSize', 'limitsText', 'errorsText', 'destPath'); |
| 27 |
} |
| 28 |
|
| 29 |
public function render() { |
| 30 |
if(empty($this->attributes["value"])){ |
| 31 |
parent::render(); |
| 32 |
if ($this->limitsText !== ''){ |
| 33 |
if ($this->validExtensions) { |
| 34 |
$validExtensions = implode(', ', $this->validExtensions); |
| 35 |
} else { |
| 36 |
$validExtensions = '*'; |
| 37 |
} |
| 38 |
$search = array('%size%', '%extensions%'); |
| 39 |
$replace = array(self::format_size($this->maxSize),$validExtensions); |
| 40 |
echo '<br /><small>', str_replace($search, $replace, $this->limitsText), '</small>'; |
| 41 |
} |
| 42 |
} else { |
| 43 |
echo $this->alreadySubmittedText; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function getAlreadySubmittedText() { |
| 48 |
return $this->alreadySubmittedText; |
| 49 |
} |
| 50 |
|
| 51 |
public static function format_size($size) { |
| 52 |
if ($size >= 1073741824) { |
| 53 |
return round($size/1073741824, 2).' GB'; |
| 54 |
} else if ($size >= 1048576) { |
| 55 |
return round($size/1048576, 2).' MB'; |
| 56 |
} else if ($size >= 1024) { |
| 57 |
return round($size/1024, 2).' KB'; |
| 58 |
} else if ($size > 0) { |
| 59 |
return round($size).' B'; |
| 60 |
} else { |
| 61 |
return '-'; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public static function parse_size($size) { |
| 66 |
if (is_numeric($size)) { |
| 67 |
return (int) $size; |
| 68 |
} |
| 69 |
|
| 70 |
$suffixes = array( |
| 71 |
'' => 1, |
| 72 |
'k' => 1024, |
| 73 |
'm' => 1048576, // 1024 * 1024 |
| 74 |
'g' => 1073741824, // 1024 * 1024 * 1024 |
| 75 |
); |
| 76 |
if (preg_match('/([0-9.,]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match)) { |
| 77 |
return $match[1] * $suffixes[strtolower($match[2])]; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public static function file_upload_max_size() { |
| 82 |
static $max_size = -1; |
| 83 |
|
| 84 |
if ($max_size < 0) { |
| 85 |
$upload_max = self::parse_size(ini_get('upload_max_filesize')); |
| 86 |
$post_max = self::parse_size(ini_get('post_max_size')); |
| 87 |
$max_size = ($upload_max < $post_max) ? $upload_max : $post_max; |
| 88 |
} |
| 89 |
return $max_size; |
| 90 |
} |
| 91 |
|
| 92 |
public function setMaxSize($size) { |
| 93 |
$phpMaxSize = self::file_upload_max_size(); |
| 94 |
$size = self::parse_size($size); |
| 95 |
return $this->maxSize = ($phpMaxSize < $size) ? $phpMaxSize : $size; |
| 96 |
} |
| 97 |
|
| 98 |
public function setErrorsText($errors){ |
| 99 |
if(is_array($errors)) { |
| 100 |
$this->errorsText = $errors + $this->errorsText; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function setDestPath($path) { |
| 105 |
if (substr($path,0,1) !== '/') { |
| 106 |
$path = ABSPATH . '/' . $path; |
| 107 |
} |
| 108 |
if (!is_dir($path)){ |
| 109 |
@ mkdir($path, 0777, true); |
| 110 |
} |
| 111 |
return $this->destPath = realpath($path) . '/'; |
| 112 |
} |
| 113 |
|
| 114 |
public function handle_upload($filedata){ |
| 115 |
$valid = true; |
| 116 |
$file = array( |
| 117 |
'dest_path' => $this->destPath, |
| 118 |
); |
| 119 |
|
| 120 |
if (!empty($filedata['error'])){ |
| 121 |
$valid = false; |
| 122 |
switch($filedata['error']){ |
| 123 |
case UPLOAD_ERR_INI_SIZE: |
| 124 |
case UPLOAD_ERR_FORM_SIZE: |
| 125 |
$file['errors'][] = $this->errorsText['size']; |
| 126 |
break; |
| 127 |
default: |
| 128 |
$file['errors'][] = $this->errorsText['upload']; |
| 129 |
} |
| 130 |
} else if ($filedata['size'] <= 0) { |
| 131 |
$valid = false; |
| 132 |
$file['errors'][] = $this->errorsText['empty']; |
| 133 |
} else if ($this->maxSize > 0 && $filedata['size'] > $this->maxSize) { |
| 134 |
$valid = false; |
| 135 |
$file['errors'][] = $this->errorsText['size']; |
| 136 |
} else { |
| 137 |
$file['size'] = $filedata['size']; |
| 138 |
} |
| 139 |
|
| 140 |
if (isset($filedata['name']) && $filedata['name'] !== ''){ |
| 141 |
$file['name'] = $filedata['name']; |
| 142 |
if ((strpos($file['name'], "\0") !== false) || (strpbrk($file['name'], "\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177\\/:*?\"<>|") !== false) ) { |
| 143 |
$valid = false; |
| 144 |
$file['errors'][] = $this->errorsText['name']; |
| 145 |
} else if ($this->validExtensions) { |
| 146 |
$ext = strrchr($file['name'], '.'); |
| 147 |
$ext = ($ext === false) ? '' : strtolower(ltrim($ext, '.')); |
| 148 |
if (!in_array($ext, $this->validExtensions, true)) { |
| 149 |
$valid = false; |
| 150 |
$file['errors'][] = str_replace('%extensions%', implode(', ',$this->validExtensions), $this->errorsText['ext']); |
| 151 |
} |
| 152 |
} |
| 153 |
} else { |
| 154 |
$file['name'] = ''; |
| 155 |
if ($valid) { |
| 156 |
$valid = false; |
| 157 |
$file['errors'][] = $this->errorsText['name']; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if ($valid) { |
| 162 |
if (!is_dir($this->destPath)){ |
| 163 |
@ mkdir($this->destPath, 0777, true); |
| 164 |
} |
| 165 |
|
| 166 |
do { |
| 167 |
$tmpname = 'tmp' . mt_rand() . '_' . $file['name']; |
| 168 |
} while (is_file($this->destPath.$tmpname)); |
| 169 |
|
| 170 |
@ $valid = move_uploaded_file($filedata['tmp_name'], $this->destPath.$tmpname); |
| 171 |
if ($valid) { |
| 172 |
$file['tmp_name'] = $tmpname; |
| 173 |
} else { |
| 174 |
$file['errors'][] = $this->errorsText['upload']; |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
return $file; |
| 180 |
} |
| 181 |
} |
| 182 |
|