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

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