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

144 lines 4.9 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 = array();
13 public $chmod_dir = 0755;
14 public $chmod_file = 0644;
15
16 public function __construct( $atts ) {
17 $this->folder_name = $atts['folder_name'];
18 $this->file_name = $atts['file_name'];
19 $this->error_message = isset( $atts['error_message'] ) ? $atts['error_message'] : '';
20 $this->uploads = wp_upload_dir();
21 $this->chmod_dir = defined('FS_CHMOD_DIR') ? FS_CHMOD_DIR : ( fileperms( ABSPATH ) & 0777 | 0755 );
22 $this->chmod_file = defined('FS_CHMOD_FILE') ? FS_CHMOD_FILE : ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 );
23 }
24
25 public function create_file( $file_content ) {
26 $creds = $this->get_creds();
27
28 if ( empty( $creds ) || ! WP_Filesystem( $creds ) ) {
29 // initialize the API - any problems and we exit
30 $this->show_error_message();
31 } else {
32 $dirs_exist = true;
33
34 // Create the directories if need be
35 $this->create_directories( $dirs_exist );
36
37 // only write the file if the folders exist
38 if ( $dirs_exist ) {
39 global $wp_filesystem;
40
41 $new_file = $this->uploads['basedir'] . '/' . $this->folder_name . '/' . $this->file_name;
42 $wp_filesystem->put_contents( $new_file, $file_content, $this->chmod_file );
43 }
44 }
45 }
46
47 private function create_directories( &$dirs_exist ) {
48 global $wp_filesystem;
49
50 $needed_dirs = $this->get_needed_dirs();
51 foreach ( $needed_dirs as $_dir ) {
52 // Only check to see if the Dir exists upon creation failure. Less I/O this way.
53 if ( $wp_filesystem->mkdir( $_dir, $this->chmod_dir ) || $wp_filesystem->is_dir( $_dir ) ) {
54 $index_path = $_dir . '/index.php';
55 $wp_filesystem->put_contents( $index_path, "<?php\n// Silence is golden.\n?>", $this->chmod_file );
56 } else {
57 $dirs_exist = false;
58 }
59 }
60 }
61
62 private function get_needed_dirs() {
63 $dir_names = explode( '/', $this->folder_name );
64 $needed_dirs = array();
65
66 $next_dir = '';
67 foreach ( $dir_names as $dir ) {
68 $next_dir .= '/' . $dir;
69 $needed_dirs[] = $this->uploads['basedir'] . $next_dir;
70 }
71
72 return $needed_dirs;
73 }
74
75 private function get_creds() {
76 $access_type = get_filesystem_method();
77 if ( $access_type === 'direct' ) {
78 $creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() );
79 } else {
80 $creds = $this->get_ftp_creds( $access_type );
81 }
82 return $creds;
83 }
84
85 private function get_ftp_creds( $type ) {
86 $credentials = get_option( 'ftp_credentials', array(
87 'hostname' => '',
88 'username' => '',
89 ) );
90
91 $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : $credentials['hostname'];
92 $credentials['username'] = defined('FTP_USER') ? FTP_USER : $credentials['username'];
93 $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : '';
94
95 // Check to see if we are setting the public/private keys for ssh
96 $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : '';
97 $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : '';
98
99 // Sanitize the hostname, Some people might pass in odd-data:
100 $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); //Strip any schemes off
101
102 if ( strpos( $credentials['hostname'], ':' ) ) {
103 list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
104 if ( ! is_numeric( $credentials['port'] ) ) {
105 unset( $credentials['port'] );
106 }
107 } else {
108 unset( $credentials['port'] );
109 }
110
111 if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) {
112 $credentials['connection_type'] = 'ssh';
113 } else if ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) {
114 //Only the FTP Extension understands SSL
115 $credentials['connection_type'] = 'ftps';
116 } else if ( ! isset( $credentials['connection_type'] ) ) {
117 //All else fails (And it's not defaulted to something else saved), Default to FTP
118 $credentials['connection_type'] = 'ftp';
119 }
120
121 $has_creds = ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) );
122 $can_ssh = ( 'ssh' == $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) );
123 if ( $has_creds || $can_ssh ) {
124 $stored_credentials = $credentials;
125 if ( ! empty( $stored_credentials['port'] ) ) {
126 //save port as part of hostname to simplify above code.
127 $stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
128 }
129
130 unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] );
131
132 return $credentials;
133 }
134
135 return false;
136 }
137
138 private function show_error_message() {
139 if ( ! empty( $this->error_message ) ) {
140 echo '<div class="message">' . $this->error_message . '</div>';
141 }
142 }
143 }
144