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
contact-forms / classes / Element / File.php

File.php in Contact Forms by Cimatti 1.9.2, at classes/Element/File.php

179 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
179