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

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