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