PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.84
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.84
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / Drive / WPDA_Ftp.php

WPDA_Ftp.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.84, at WPDataAccess/Drive/WPDA_Ftp.php

156 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\Drive {
4
5 use WPDataAccess\WPDA;
6
7 class WPDA_Ftp extends WPDA_Drive {
8
9 private $connection;
10 private $login = false;
11
12 public function __construct( $drive_name, $drive = array(), $enabled = false ) {
13
14 $this->drive_type = 'ftp';
15
16 parent::__construct( $drive_name, $drive, $enabled );
17
18 }
19
20 public function authorize( $authorization, $enabled = true ) {
21
22 if (
23 isset(
24 $authorization['host'],
25 $authorization['username'],
26 $authorization['password'],
27 $authorization['port'],
28 $authorization['ssl'],
29 $authorization['passive'],
30 $authorization['timeout'],
31 $authorization['directory']
32 )
33 ) {
34 $this->drive = array(
35 'host' => $authorization['host'],
36 'username' => $authorization['username'],
37 'password' => $authorization['password'],
38 'port' => $authorization['port'],
39 'ssl' => $authorization['ssl'],
40 'passive' => $authorization['passive'],
41 'timeout' => $authorization['timeout'],
42 'directory' => $authorization['directory'],
43 );
44 $this->enabled = $enabled;
45 $this->save();
46
47 return true;
48 }
49
50 return false;
51
52 }
53
54 private function prepare_connection() {
55
56 return $this->enabled;
57
58 }
59
60 public function refresh_token() {
61
62 return true; // N.A.
63
64 }
65
66 public function connect() {
67
68 if ( ! $this-> prepare_connection() ) { return false; }
69
70 if (
71 ! isset(
72 $this->drive['host'],
73 $this->drive['username'],
74 $this->drive['password'],
75 $this->drive['port'],
76 $this->drive['ssl'],
77 $this->drive['passive'],
78 $this->drive['timeout'],
79 $this->drive['directory']
80 )
81 ) {
82 return false;
83 }
84
85 $this->connection = $this->drive['ssl']
86 ? ftp_ssl_connect( $this->drive['host'], $this->drive['port'], $this->drive['timeout'] )
87 : ftp_connect( $this->drive['host'], $this->drive['port'], $this->drive['timeout'] );
88
89 if ( ! $this->connection ) {
90 WPDA::wpda_log_wp_error("ERROR: Could not connect to FTP server - {$this->drive_name}" );
91 return false;
92 }
93
94 $this->login = ftp_login( $this->connection, $this->drive['username'], $this->drive['password'] );
95
96 if ( ! $this->login ) {
97 WPDA::wpda_log_wp_error( "ERROR: FTP login failed - {$this->drive_name}" );
98 return false;
99 }
100
101 return ftp_pasv( $this->connection, $this->drive['passive'] );
102
103 }
104
105 public function close() {
106
107 if ( $this->connection ) {
108 ftp_close( $this->connection );
109 }
110
111 }
112
113 public function upload_file( $local_file, $remote_file ) {
114
115 if ( null !== $this->connection && ! $this->connection || ! $this->login ) { return false; }
116
117 $path = '/' === substr( $this->drive['directory'], -1 ) ? $this->drive['directory'] : $this->drive['directory'] . '/';
118
119 return ftp_put( $this->connection, $path . $remote_file, $local_file, FTP_BINARY );
120
121 }
122
123 public function cleanup( $query, $keep ) {
124
125 if ( 'ALL' === $keep ) return true;
126
127 if ( null !== $this->connection && ! $this->connection || ! $this->login ) { return false; }
128
129 $path = '/' === substr( $this->drive['directory'], -1 ) ? $this->drive['directory'] : $this->drive['directory'] . '/';
130
131 $files = ftp_nlist( $this->connection, $path );
132 if ( ! $files || ! is_array( $files ) ) return false;
133
134 $matched_files = array_filter( $files, function ( $file ) use ( $query ) {
135 return fnmatch( $query, basename( $file ) ) !== false && $file !== '.' && $file !== '..';
136 });
137
138 usort( $matched_files, function ( $a, $b ) {
139 return strcmp( $b, $a );
140 });
141
142 $count = 0;
143 foreach ( $matched_files as $file ) {
144 $count++;
145 if ( $count > (int) $keep ) {
146 ftp_delete( $this->connection, $path . $file );
147 }
148 }
149
150 return true;
151
152 }
153
154 }
155
156 }