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 +35 -109 6.274.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,21 +37,16 @@
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 42 if ( $this->has_permission ) {
85 43 $dirs_exist = true;
86 44
87 - // Create the directories if need be.
45 + // Create the directories if need be
88 46 $this->create_directories( $dirs_exist );
89 47
90 - // Only write the file if the folders exist.
48 + // only write the file if the folders exist
91 49 if ( $dirs_exist ) {
92 50 global $wp_filesystem;
93 51 $wp_filesystem->put_contents( $this->new_file_path, $file_content, $this->chmod_file );
94 52 }
@@ -96,16 +54,14 @@
96 54 }
97 55
98 56 /**
99 57 * @since 3.0
100 - *
101 - * @param string $file_content File content.
102 - *
103 - * @return void
104 58 */
105 59 public function append_file( $file_content ) {
106 60 if ( $this->has_permission ) {
61 +
107 62 if ( file_exists( $this->new_file_path ) ) {
63 +
108 64 $existing_content = $this->get_contents();
109 65 $file_content = $existing_content . $file_content;
110 66 }
111 67
@@ -117,16 +73,13 @@
117 73 * Combine an array of files into one
118 74 *
119 75 * @since 3.0
120 76 *
121 - * @param array $file_names And array of file paths.
122 - *
123 - * @return void
77 + * @param array $file_names And array of file paths
124 78 */
125 79 public function combine_files( $file_names ) {
126 80 if ( $this->has_permission ) {
127 81 $content = '';
128 -
129 82 foreach ( $file_names as $file_name ) {
130 83 $content .= $this->get_contents( $file_name ) . "\n";
131 84 }
132 85 $this->create_file( $content );
@@ -134,26 +87,25 @@
134 87 }
135 88
136 89 /**
137 90 * @since 3.0
138 - *
139 - * @return string
140 91 */
141 92 public function get_file_contents() {
142 - 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;
143 100 }
144 101
145 102 /**
146 103 * @since 3.0
147 - *
148 - * @param string $file File.
149 - *
150 - * @return string
151 104 */
152 105 private function get_contents( $file = '' ) {
153 106 global $wp_filesystem;
154 -
155 - if ( ! $file ) {
107 + if ( empty( $file ) ) {
156 108 $file = $this->new_file_path;
157 109 }
158 110
159 111 return $wp_filesystem->get_contents( $file );
@@ -160,17 +112,14 @@
160 112 }
161 113
162 114 /**
163 115 * @since 3.0
164 - *
165 - * @return void
166 116 */
167 117 private function check_permission() {
168 118 $creds = $this->get_creds();
169 119
170 120 $this->has_permission = true;
171 -
172 - if ( ! $creds || ! WP_Filesystem( $creds ) ) {
121 + if ( empty( $creds ) || ! WP_Filesystem( $creds ) ) {
173 122 // initialize the API - any problems and we exit
174 123 $this->show_error_message();
175 124 $this->has_permission = false;
176 125 }
@@ -175,18 +124,12 @@
175 124 $this->has_permission = false;
176 125 }
177 126 }
178 127
179 - /**
180 - * @param true $dirs_exist
181 - *
182 - * @return void
183 - */
184 128 private function create_directories( &$dirs_exist ) {
185 129 global $wp_filesystem;
186 130
187 131 $needed_dirs = $this->get_needed_dirs();
188 -
189 132 foreach ( $needed_dirs as $_dir ) {
190 133 // Only check to see if the Dir exists upon creation failure. Less I/O this way.
191 134 if ( $wp_filesystem->mkdir( $_dir, $this->chmod_dir ) ) {
192 135 $index_path = $_dir . '/index.php';
@@ -196,18 +139,15 @@
196 139 }
197 140 }
198 141 }
199 142
200 - /**
201 - * @return string[]
202 - */
203 143 private function get_needed_dirs() {
204 144 $dir_names = explode( '/', $this->folder_name );
205 145 $needed_dirs = array();
206 - $next_dir = '';
207 146
147 + $next_dir = '';
208 148 foreach ( $dir_names as $dir ) {
209 - $next_dir .= '/' . $dir;
149 + $next_dir .= '/' . $dir;
210 150 $needed_dirs[] = $this->uploads['basedir'] . $next_dir;
211 151 }
212 152
213 153 return $needed_dirs;
@@ -212,30 +152,23 @@
212 152
213 153 return $needed_dirs;
214 154 }
215 155
216 - /**
217 - * @return array|bool
218 - */
219 156 private function get_creds() {
220 157 if ( ! function_exists( 'get_filesystem_method' ) ) {
221 - include_once ABSPATH . 'wp-admin/includes/file.php';
158 + include_once( ABSPATH . 'wp-admin/includes/file.php' );
222 159 }
223 160
224 161 $access_type = get_filesystem_method();
225 -
226 162 if ( $access_type === 'direct' ) {
227 - 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 );
228 166 }
229 167
230 - return $this->get_ftp_creds( $access_type );
168 + return $creds;
231 169 }
232 170
233 - /**
234 - * @param string $type
235 - *
236 - * @return array|false
237 - */
238 171 private function get_ftp_creds( $type ) {
239 172 $credentials = get_option(
240 173 'ftp_credentials',
241 174 array(
@@ -247,19 +180,17 @@
247 180 $credentials['hostname'] = defined( 'FTP_HOST' ) ? FTP_HOST : $credentials['hostname'];
248 181 $credentials['username'] = defined( 'FTP_USER' ) ? FTP_USER : $credentials['username'];
249 182 $credentials['password'] = defined( 'FTP_PASS' ) ? FTP_PASS : '';
250 183
251 - // 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
252 185 $credentials['public_key'] = defined( 'FTP_PUBKEY' ) ? FTP_PUBKEY : '';
253 186 $credentials['private_key'] = defined( 'FTP_PRIKEY' ) ? FTP_PRIKEY : '';
254 187
255 - // Sanitize the hostname, Some people might pass in odd-data.
256 - // Strip any schemes off.
257 - $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
258 190
259 - if ( str_contains( $credentials['hostname'], ':' ) ) {
191 + if ( strpos( $credentials['hostname'], ':' ) ) {
260 192 list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
261 -
262 193 if ( ! is_numeric( $credentials['port'] ) ) {
263 194 unset( $credentials['port'] );
264 195 }
265 196 } else {
@@ -265,26 +196,24 @@
265 196 } else {
266 197 unset( $credentials['port'] );
267 198 }
268 199
269 - 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 ) ) {
270 201 $credentials['connection_type'] = 'ssh';
271 - } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) {
272 - // Only the FTP Extension understands SSL.
202 + } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) {
203 + //Only the FTP Extension understands SSL
273 204 $credentials['connection_type'] = 'ftps';
274 205 } elseif ( ! isset( $credentials['connection_type'] ) ) {
275 - // 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
276 207 $credentials['connection_type'] = 'ftp';
277 208 }
278 209
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 -
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'] ) );
282 212 if ( $has_creds || $can_ssh ) {
283 213 $stored_credentials = $credentials;
284 -
285 214 if ( ! empty( $stored_credentials['port'] ) ) {
286 - // Save port as part of hostname to simplify above code.
215 + //save port as part of hostname to simplify above code.
287 216 $stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
288 217 }
289 218
290 219 unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] );
@@ -294,11 +223,8 @@
294 223
295 224 return false;
296 225 }
297 226
298 - /**
299 - * @return void
300 - */
301 227 private function show_error_message() {
302 228 if ( ! empty( $this->error_message ) ) {
303 229 echo '<div class="message">' . esc_html( $this->error_message ) . '</div>';
304 230 }