PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26
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
formidable / classes / models / FrmCreateFile.php

FrmCreateFile.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26, at classes/models/FrmCreateFile.php

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