PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmCreateFile.php +34 -109 6.275.0 View file →
@@ -4,55 +4,21 @@
4 4 }
5 5
6 6 class FrmCreateFile {
7 7
8 - /**
9 - * @var string
10 - */
11 8 public $folder_name;
12 -
13 - /**
14 - * @var string
15 - */
16 9 public $file_name;
17 -
18 - /**
19 - * @var string
20 - */
21 10 public $error_message;
22 -
23 - /**
24 - * @var array
25 - */
26 11 public $uploads;
27 -
28 - /**
29 - * @var string
30 - */
31 12 private $new_file_path;
32 -
33 - /**
34 - * @var int
35 - */
36 13 public $chmod_dir = 0755;
37 -
38 - /**
39 - * @var int
40 - */
41 14 public $chmod_file = 0644;
42 -
43 - /**
44 - * @var bool
45 - */
46 15 private $has_permission = false;
47 16
48 - /**
49 - * @param array $atts
50 - */
51 17 public function __construct( $atts ) {
52 - $this->folder_name = $atts['folder_name'] ?? '';
18 + $this->folder_name = isset( $atts['folder_name'] ) ? $atts['folder_name'] : '';
53 19 $this->file_name = $atts['file_name'];
54 - $this->error_message = $atts['error_message'] ?? '';
20 + $this->error_message = isset( $atts['error_message'] ) ? $atts['error_message'] : '';
55 21 $this->uploads = wp_upload_dir();
56 22 $this->set_new_file_path( $atts );
57 23 $this->chmod_dir = defined( 'FS_CHMOD_DIR' ) ? FS_CHMOD_DIR : ( fileperms( ABSPATH ) & 0777 | 0755 );
58 24 $this->chmod_file = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 );
@@ -61,12 +27,8 @@
61 27 }
62 28
63 29 /**
64 30 * @since 3.0
65 - *
66 - * @param array $atts Attributes.
67 - *
68 - * @return void
69 31 */
70 32 private function set_new_file_path( $atts ) {
71 33 if ( isset( $atts['new_file_path'] ) ) {
72 34 $this->new_file_path = $atts['new_file_path'] . '/' . $this->file_name;
@@ -74,21 +36,16 @@
74 36 $this->new_file_path = $this->uploads['basedir'] . '/' . $this->folder_name . '/' . $this->file_name;
75 37 }
76 38 }
77 39
78 - /**
79 - * @param string $file_content
80 - *
81 - * @return void
82 - */
83 40 public function create_file( $file_content ) {
84 41 if ( $this->has_permission ) {
85 42 $dirs_exist = true;
86 43
87 - // Create the directories if need be.
44 + // Create the directories if need be
88 45 $this->create_directories( $dirs_exist );
89 46
90 - // Only write the file if the folders exist.
47 + // only write the file if the folders exist
91 48 if ( $dirs_exist ) {
92 49 global $wp_filesystem;
93 50 $wp_filesystem->put_contents( $this->new_file_path, $file_content, $this->chmod_file );
94 51 }
@@ -96,16 +53,14 @@
96 53 }
97 54
98 55 /**
99 56 * @since 3.0
100 - *
101 - * @param string $file_content File content.
102 - *
103 - * @return void
104 57 */
105 58 public function append_file( $file_content ) {
106 59 if ( $this->has_permission ) {
60 +
107 61 if ( file_exists( $this->new_file_path ) ) {
62 +
108 63 $existing_content = $this->get_contents();
109 64 $file_content = $existing_content . $file_content;
110 65 }
111 66
@@ -117,16 +72,13 @@
117 72 * Combine an array of files into one
118 73 *
119 74 * @since 3.0
120 75 *
121 - * @param array $file_names And array of file paths.
122 - *
123 - * @return void
76 + * @param array $file_names And array of file paths
124 77 */
125 78 public function combine_files( $file_names ) {
126 79 if ( $this->has_permission ) {
127 80 $content = '';
128 -
129 81 foreach ( $file_names as $file_name ) {
130 82 $content .= $this->get_contents( $file_name ) . "\n";
131 83 }
132 84 $this->create_file( $content );
@@ -134,26 +86,25 @@
134 86 }
135 87
136 88 /**
137 89 * @since 3.0
138 - *
139 - * @return string
140 90 */
141 91 public function get_file_contents() {
142 - return $this->has_permission ? $this->get_contents() : '';
92 + $content = '';
93 +
94 + if ( $this->has_permission ) {
95 + $content = $this->get_contents();
96 + }
97 +
98 + return $content;
143 99 }
144 100
145 101 /**
146 102 * @since 3.0
147 - *
148 - * @param string $file File.
149 - *
150 - * @return string
151 103 */
152 104 private function get_contents( $file = '' ) {
153 105 global $wp_filesystem;
154 -
155 - if ( ! $file ) {
106 + if ( empty( $file ) ) {
156 107 $file = $this->new_file_path;
157 108 }
158 109
159 110 return $wp_filesystem->get_contents( $file );
@@ -160,17 +111,14 @@
160 111 }
161 112
162 113 /**
163 114 * @since 3.0
164 - *
165 - * @return void
166 115 */
167 116 private function check_permission() {
168 117 $creds = $this->get_creds();
169 118
170 119 $this->has_permission = true;
171 -
172 - if ( ! $creds || ! WP_Filesystem( $creds ) ) {
120 + if ( empty( $creds ) || ! WP_Filesystem( $creds ) ) {
173 121 // initialize the API - any problems and we exit
174 122 $this->show_error_message();
175 123 $this->has_permission = false;
176 124 }
@@ -175,18 +123,12 @@
175 123 $this->has_permission = false;
176 124 }
177 125 }
178 126
179 - /**
180 - * @param true $dirs_exist
181 - *
182 - * @return void
183 - */
184 127 private function create_directories( &$dirs_exist ) {
185 128 global $wp_filesystem;
186 129
187 130 $needed_dirs = $this->get_needed_dirs();
188 -
189 131 foreach ( $needed_dirs as $_dir ) {
190 132 // Only check to see if the Dir exists upon creation failure. Less I/O this way.
191 133 if ( $wp_filesystem->mkdir( $_dir, $this->chmod_dir ) ) {
192 134 $index_path = $_dir . '/index.php';
@@ -196,18 +138,15 @@
196 138 }
197 139 }
198 140 }
199 141
200 - /**
201 - * @return string[]
202 - */
203 142 private function get_needed_dirs() {
204 143 $dir_names = explode( '/', $this->folder_name );
205 144 $needed_dirs = array();
206 - $next_dir = '';
207 145
146 + $next_dir = '';
208 147 foreach ( $dir_names as $dir ) {
209 - $next_dir .= '/' . $dir;
148 + $next_dir .= '/' . $dir;
210 149 $needed_dirs[] = $this->uploads['basedir'] . $next_dir;
211 150 }
212 151
213 152 return $needed_dirs;
@@ -212,30 +151,23 @@
212 151
213 152 return $needed_dirs;
214 153 }
215 154
216 - /**
217 - * @return array|bool
218 - */
219 155 private function get_creds() {
220 156 if ( ! function_exists( 'get_filesystem_method' ) ) {
221 - include_once ABSPATH . 'wp-admin/includes/file.php';
157 + include_once( ABSPATH . 'wp-admin/includes/file.php' );
222 158 }
223 159
224 160 $access_type = get_filesystem_method();
225 -
226 161 if ( $access_type === 'direct' ) {
227 - return request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() );
162 + $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() );
163 + } else {
164 + $creds = $this->get_ftp_creds( $access_type );
228 165 }
229 166
230 - return $this->get_ftp_creds( $access_type );
167 + return $creds;
231 168 }
232 169
233 - /**
234 - * @param string $type
235 - *
236 - * @return array|false
237 - */
238 170 private function get_ftp_creds( $type ) {
239 171 $credentials = get_option(
240 172 'ftp_credentials',
241 173 array(
@@ -247,19 +179,17 @@
247 179 $credentials['hostname'] = defined( 'FTP_HOST' ) ? FTP_HOST : $credentials['hostname'];
248 180 $credentials['username'] = defined( 'FTP_USER' ) ? FTP_USER : $credentials['username'];
249 181 $credentials['password'] = defined( 'FTP_PASS' ) ? FTP_PASS : '';
250 182
251 - // Check to see if we are setting the public/private keys for ssh.
183 + // Check to see if we are setting the public/private keys for ssh
252 184 $credentials['public_key'] = defined( 'FTP_PUBKEY' ) ? FTP_PUBKEY : '';
253 185 $credentials['private_key'] = defined( 'FTP_PRIKEY' ) ? FTP_PRIKEY : '';
254 186
255 - // Sanitize the hostname, Some people might pass in odd-data.
256 - // Strip any schemes off.
257 - $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] );
187 + // Sanitize the hostname, Some people might pass in odd-data:
188 + $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); //Strip any schemes off
258 189
259 - if ( str_contains( $credentials['hostname'], ':' ) ) {
190 + if ( strpos( $credentials['hostname'], ':' ) ) {
260 191 list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
261 -
262 192 if ( ! is_numeric( $credentials['port'] ) ) {
263 193 unset( $credentials['port'] );
264 194 }
265 195 } else {
@@ -265,26 +195,24 @@
265 195 } else {
266 196 unset( $credentials['port'] );
267 197 }
268 198
269 - if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) {
199 + if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) {
270 200 $credentials['connection_type'] = 'ssh';
271 - } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) {
272 - // Only the FTP Extension understands SSL.
201 + } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) {
202 + //Only the FTP Extension understands SSL
273 203 $credentials['connection_type'] = 'ftps';
274 204 } elseif ( ! isset( $credentials['connection_type'] ) ) {
275 - // All else fails (And it's not defaulted to something else saved), Default to FTP.
205 + //All else fails (And it's not defaulted to something else saved), Default to FTP
276 206 $credentials['connection_type'] = 'ftp';
277 207 }
278 208
279 - $has_creds = ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] );
280 - $can_ssh = 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] );
281 -
209 + $has_creds = ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) );
210 + $can_ssh = ( 'ssh' == $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) );
282 211 if ( $has_creds || $can_ssh ) {
283 212 $stored_credentials = $credentials;
284 -
285 213 if ( ! empty( $stored_credentials['port'] ) ) {
286 - // Save port as part of hostname to simplify above code.
214 + //save port as part of hostname to simplify above code.
287 215 $stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
288 216 }
289 217
290 218 unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] );
@@ -294,11 +222,8 @@
294 222
295 223 return false;
296 224 }
297 225
298 - /**
299 - * @return void
300 - */
301 226 private function show_error_message() {
302 227 if ( ! empty( $this->error_message ) ) {
303 228 echo '<div class="message">' . esc_html( $this->error_message ) . '</div>';
304 229 }