Completed
Push — master ( 34a52e...661e23 )
by Stephanie
04:47
created

FrmCreateFile::create_file()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 21
rs 9.0534
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( 'hostname' => '', 'username' => '' ) );
87
88
		$credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : $credentials['hostname'];
89
		$credentials['username'] = defined('FTP_USER') ? FTP_USER : $credentials['username'];
90
		$credentials['password'] = defined('FTP_PASS') ? FTP_PASS : '';
91
92
		// Check to see if we are setting the public/private keys for ssh
93
		$credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : '';
94
		$credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : '';
95
96
		// Sanitize the hostname, Some people might pass in odd-data:
97
		$credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); //Strip any schemes off
98
99
		if ( strpos( $credentials['hostname'], ':' ) ) {
100
			list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
101
			if ( ! is_numeric( $credentials['port'] ) ) {
102
				unset( $credentials['port'] );
103
			}
104
		} else {
105
			unset( $credentials['port'] );
106
		}
107
108
		if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) {
109
			$credentials['connection_type'] = 'ssh';
110
		} else if ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) {
111
			//Only the FTP Extension understands SSL
112
			$credentials['connection_type'] = 'ftps';
113
		} else if ( ! isset( $credentials['connection_type'] ) ) {
114
			//All else fails (And it's not defaulted to something else saved), Default to FTP
115
			$credentials['connection_type'] = 'ftp';
116
		}
117
118
		$has_creds = ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) );
119
		$can_ssh = ( 'ssh' == $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) );
120
		if ( $has_creds || $can_ssh ) {
121
			$stored_credentials = $credentials;
122
			if ( ! empty( $stored_credentials['port'] ) ) {
123
				//save port as part of hostname to simplify above code.
124
				$stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
125
			}
126
127
			unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] );
128
129
			return $credentials;
130
		}
131
132
		return false;
133
	}
134
135
	private function show_error_message() {
136
		if ( ! empty( $this->error_message ) ) {
137
			echo '<div class="message">' . $this->error_message . '</div>';
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
138
		}
139
	}
140
}
141