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_Sftp.php

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

163 lines 4.8 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_Sftp extends WPDA_Drive {
8
9 private $connection = null;
10 private $login = false;
11
12 public function __construct( $drive_name, $drive = array(), $enabled = false ) {
13
14 $this->drive_type = 'sftp';
15 $this->register_phpseclib();
16
17 parent::__construct( $drive_name, $drive, $enabled );
18
19 }
20
21 private function register_phpseclib() {
22
23 $baseDir = __DIR__ . '/../../vendor/phpseclib';
24
25 spl_autoload_register(function ($class) use ($baseDir) {
26 if (strpos($class, 'phpseclib3\\') === 0) {
27 $path = str_replace('\\', '/', substr($class, strlen('phpseclib3\\')));
28 $file = $baseDir . '/' . $path . '.php';
29 if (file_exists($file)) {
30 require_once $file;
31 }
32 }
33 });
34
35 }
36
37 public function authorize( $authorization, $enabled = true ) {
38
39 if (
40 isset(
41 $authorization['host'],
42 $authorization['username'],
43 $authorization['password'],
44 $authorization['port'],
45 $authorization['timeout'],
46 $authorization['directory']
47 )
48 ) {
49 $this->drive = array(
50 'host' => $authorization['host'],
51 'username' => $authorization['username'],
52 'password' => $authorization['password'],
53 'port' => $authorization['port'],
54 'timeout' => $authorization['timeout'],
55 'directory' => $authorization['directory'],
56 );
57 $this->enabled = $enabled;
58 $this->save();
59
60 return true;
61 }
62
63 return false;
64
65 }
66
67 private function prepare_connection() {
68
69 return $this->enabled;
70
71 }
72
73 public function refresh_token() {
74
75 return true; // N.A.
76
77 }
78
79 public function connect() {
80
81 if ( ! $this->enabled ) { return false; }
82
83 if (
84 ! isset(
85 $this->drive['host'],
86 $this->drive['port'],
87 $this->drive['timeout'],
88 $this->drive['username'],
89 $this->drive['password'],
90 $this->drive['directory']
91 )
92 ) {
93 return false;
94 }
95
96 $this->connection = new \phpseclib3\Net\SFTP( $this->drive['host'], $this->drive['port'], $this->drive['timeout'] );
97
98 $this->login = $this->connection->login( $this->drive['username'], $this->drive['password'] );
99
100 if ( ! $this->login ) {
101 WPDA::wpda_log_wp_error( "ERROR: SFTP login failed - {$this->drive_name}" );
102 return false;
103 }
104
105 return true;
106
107 }
108
109 public function close() {
110
111 if (
112 null !== $this->connection &&
113 $this->connection->isConnected()
114 ) {
115 $this->connection->disconnect();
116 }
117
118 }
119
120 public function upload_file( $local_file, $remote_file ) {
121
122 if ( null !== $this->connection && ! $this->connection->isConnected() || ! $this->login ) { return false; }
123
124 $path = '/' === substr( $this->drive['directory'], -1 ) ? $this->drive['directory'] : $this->drive['directory'] . '/';
125
126 return $this->connection->put( $path . $remote_file, $local_file, \phpseclib3\Net\SFTP::SOURCE_LOCAL_FILE );
127
128 }
129
130 public function cleanup( $query, $keep ) {
131
132 if ( 'ALL' === $keep ) return true;
133
134 if ( null !== $this->connection && ! $this->connection->isConnected() || ! $this->login ) { return false; }
135
136 $path = '/' === substr( $this->drive['directory'], -1 ) ? $this->drive['directory'] : $this->drive['directory'] . '/';
137
138 $files = $this->connection->nlist( $path );
139 if ( ! $files || ! is_array( $files ) ) return false;
140
141 $matched_files = array_filter( $files, function ( $file ) use ( $query ) {
142 return fnmatch( $query, basename( $file ) ) !== false && $file !== '.' && $file !== '..';
143 });
144
145 usort( $matched_files, function ( $a, $b ) {
146 return strcmp( $b, $a );
147 });
148
149 $count = 0;
150 foreach ( $matched_files as $file ) {
151 $count++;
152 if ( $count > (int) $keep ) {
153 $this->connection->delete( $path . $file );
154 }
155 }
156
157 return true;
158
159 }
160
161 }
162
163 }