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

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