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