PluginProbe
Contact Forms by Cimatti / 1.9.2
Contact Forms by Cimatti v1.9.2
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
← All changes | classes/Element/File.php +178 -181 1.21.9.2 View file →
@@ -1,181 +1,178 @@
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 -}
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.", 'contact-forms');
12 + $this->limitsText = __("Maximum filesize: %size%. Allowed extensions: %extensions%.", 'contact-forms');
13 + $this->errorsText = array(
14 + 'upload' => __('Upload failed, please retry. If the problem persists please contact us', 'contact-forms'),
15 + 'size' => __('Uploaded file is too big', 'contact-forms'),
16 + 'empty' => __('Uploaded file is empty', 'contact-forms'),
17 + 'name' => __('File name is not valid. Please rename it avoiding unusual characters', 'contact-forms'),
18 + 'ext' => __('File extension not allowed. Allowed extensions are %extensions%', 'contact-forms'),
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($value) {
66 + $value = strtolower( trim( $value ) );
67 + $bytes = (float) $value;
68 +
69 + if ( false !== strpos( $value, 'g' ) ) {
70 + $bytes *= 1073741824; // 1024 * 1024 * 1024
71 + } elseif ( false !== strpos( $value, 'm' ) ) {
72 + $bytes *= 1048576; // 1024 * 1024
73 + } elseif ( false !== strpos( $value, 'k' ) ) {
74 + $bytes *= 1024;
75 + }
76 +
77 + return min( $bytes, PHP_INT_MAX );
78 + }
79 +
80 + public static function file_upload_max_size() {
81 + static $max_size = -1;
82 +
83 + if ($max_size < 0) {
84 + $max_size = wp_max_upload_size();
85 + }
86 + return $max_size;
87 + }
88 +
89 + public function setMaxSize($size) {
90 + $phpMaxSize = self::file_upload_max_size();
91 + $size = self::parse_size($size);
92 + return $this->maxSize = ($phpMaxSize < $size) ? $phpMaxSize : $size;
93 + }
94 +
95 + public function setErrorsText($errors){
96 + if(is_array($errors)) {
97 + $this->errorsText = $errors + $this->errorsText;
98 + }
99 + }
100 +
101 + public function setDestPath($path) {
102 + if (substr($path,0,1) !== '/') {
103 + $path = ABSPATH . '/' . $path;
104 + }
105 + if (!is_dir($path)){
106 + @ mkdir($path, 0777, true);
107 + }
108 + return $this->destPath = realpath($path) . '/';
109 + }
110 +
111 + public function handle_upload($filedata){
112 + $valid = true;
113 + $file = array(
114 + 'dest_path' => $this->destPath,
115 + );
116 +
117 + if (!empty($filedata['error'])){
118 + $valid = false;
119 + switch($filedata['error']){
120 + case UPLOAD_ERR_INI_SIZE:
121 + case UPLOAD_ERR_FORM_SIZE:
122 + $file['errors'][] = $this->errorsText['size'];
123 + break;
124 + default:
125 + $file['errors'][] = $this->errorsText['upload'];
126 + }
127 + } else if ($filedata['size'] <= 0) {
128 + $valid = false;
129 + $file['errors'][] = $this->errorsText['empty'];
130 + } else if ($this->maxSize > 0 && $filedata['size'] > $this->maxSize) {
131 + $valid = false;
132 + $file['errors'][] = $this->errorsText['size'];
133 + } else {
134 + $file['size'] = $filedata['size'];
135 + }
136 +
137 + if (isset($filedata['name']) && $filedata['name'] !== ''){
138 + $file['name'] = $filedata['name'];
139 + 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) ) {
140 + $valid = false;
141 + $file['errors'][] = $this->errorsText['name'];
142 + } else if ($this->validExtensions) {
143 + $ext = strrchr($file['name'], '.');
144 + $ext = ($ext === false) ? '' : strtolower(ltrim($ext, '.'));
145 + if (!in_array($ext, $this->validExtensions, true)) {
146 + $valid = false;
147 + $file['errors'][] = str_replace('%extensions%', implode(', ',$this->validExtensions), $this->errorsText['ext']);
148 + }
149 + }
150 + } else {
151 + $file['name'] = '';
152 + if ($valid) {
153 + $valid = false;
154 + $file['errors'][] = $this->errorsText['name'];
155 + }
156 + }
157 +
158 + if ($valid) {
159 + if (!is_dir($this->destPath)){
160 + @ mkdir($this->destPath, 0777, true);
161 + }
162 +
163 + do {
164 + $tmpname = 'tmp' . mt_rand() . '_' . $file['name'];
165 + } while (is_file($this->destPath.$tmpname));
166 +
167 + @ $valid = move_uploaded_file($filedata['tmp_name'], $this->destPath.$tmpname);
168 + if ($valid) {
169 + $file['tmp_name'] = $tmpname;
170 + } else {
171 + $file['errors'][] = $this->errorsText['upload'];
172 + }
173 +
174 + }
175 +
176 + return $file;
177 + }
178 +}