-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.php
322 lines (269 loc) · 11.3 KB
/
sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
//echo print_r(getenv(), true);
//phpinfo();
//die();
error_reporting(-1);
//ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
ini_set('max_input_time', PHP_INT_MAX);
set_time_limit(10);
date_default_timezone_set('America/Vancouver');
//@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++) {
@ob_end_flush();
}
ob_implicit_flush(1);
define('BRNL', "<br />\n");
define('HR', "<hr>");
$path = dirname(__FILE__) . '/lib/phpseclib';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
echo 'Incude path=' . get_include_path() . BRNL;
//include('Net/SSH2.php');
require_once('Net/SFTP.php');
define('NET_SFTP_LOGGING', NET_SFTP_LOG_SIMPLE);
require_once('config.php');
require_once('lib/logger/Logger.php');
require_once('lib/db/mysqldatabase.php');
require_once('lib/db/mysqlresultset.php');
//========
echo 'Libraries found and constants set' . BRNL;
class MarkZilla {
//Object holders
public $sftp = NULL;
protected $db = NULL;
protected $log = NULL;
//keeps track of directories we've already processed (loaded from the DB into this var)
protected $db_processed = '';
function __construct($config) {
$this->importProperties($config);
$this->log_path = dirname(__FILE__)."/temp/$this->log_path";
$this->log = new Logger($this->log_path);
$this->setupDatabase();
$this->db_processed = $this->getProcessed();
}
function importProperties($object) {
foreach (get_object_vars($object) as $key => $value) {
$this->$key = $value;
}
}
function setupDatabase() {
// get the MySqlDatabase instance
$this->db = MySqlDatabase::getInstance();
try {
$conn = $this->db->connect($this->db_host, $this->db_user, $this->db_pw, $this->db_databaseName);
} catch (Exception $e) {
die(__FUNCTION__.'::'.$e->getMessage());
}
}
function showPath() {
$this->output('Directory=' . $this->sftp->pwd());
}
function dbLog($msg) {
$msg = mysql_escape_string(filter_var($msg, FILTER_SANITIZE_STRING));
$query = "REPLACE INTO sync (location) VALUES ('$msg')";
$lastId = $this->db->insert($query);
return $lastId;
}
function getProcessed() {
$query = 'SELECT location from sync';
$result = array();
foreach ($this->db->iterate($query) as $row) {
$result[] = $row->location;
}
return $result;
}
function output($msg) {
//echo $msg . BRNL;
$this->log->logWrite($msg);
//fflush();
}
function output_error($msg) {
$this->output("<font color=\"red\">$msg</font>");
}
function pre($msg) {
echo HR . '<PRE>' . print_r($msg, true) . '</PRE>' . HR;
}
/**
*
* @param type $localDir absolute local pathname from the root localdir
* @return type
* @throws Exception
*/
function ensure_localdir_exists($localDir) {
$debugThis = false;
if ($debugThis)
$this->output(__FUNCTION__ . " looking for local folder $localDir");
$localDir = trim($localDir);
if (!is_dir($localDir)) {
$dirArr = explode(DIRECTORY_SEPARATOR, $localDir);
$dirname = array_pop($dirArr);
$basedir = implode(DIRECTORY_SEPARATOR, $dirArr);
if ($debugThis)
$this->output(__FUNCTION__ . " Current dir=" . getcwd());
if ($debugThis)
$this->output(__FUNCTION__ . " going to chdir into $basedir");
$success = chdir($basedir);
$old_umask = umask(0);
if ($debugThis)
$this->output(__FUNCTION__ . " about to mkdir $dirname");
$success = mkdir($dirname, 0777);
umask($old_umask);
clearstatcache();
// $success = mkdir($localDir, 0777, true);
// chmod($localDir, 0777);
// $var = "chmod 777 $localDir -R";
// shell_exec($var);
// clearstatcache();
if ($success) {
$this->output("Made the local folder $localDir");
} else {
throw new Exception("ERROR! Failed to make the local folder $localDir");
}
return $success;
} else {
if ($debugThis)
$this->output("The local subdir already exists=$localDir" . HR);
}
return true;
}
/**
*
* @param type $path remote absolute path to the current pwd
* @param type $localPath local absolute path to the target root dir
* @return boolean always true
* @throws Exception
*/
function getAll($path, $localPath = NULL) {
$debugThis = false;
if (is_null($localPath) || $localPath == '') {
$localPath = $this->localPath;
}
//$this->output('.');
//$this->output(__FUNCTION__ . " started with remote path=$path");
flush();
$path = trim($path);
if (empty($path))
return true;
$this->sftp->chdir($path);
$path = trim($this->sftp->pwd());
$this->output(__FUNCTION__ . "::Current remote path=" . $path);
if (empty($path))
throw new Exception('ERROR: I thought I was going into an actual folder but when I tried it came out empty');
//$this->output('Folder contents=' . $this->pre($this->sftp->nlist()));
$dirRawList = $this->sftp->rawlist();
//$this->output('Current directory contents='.$this->pre($dirRawList));
foreach ($dirRawList as $filename => &$remoteAttributes) {
if ($debugThis)
echo 'Looking at filename=' . print_r($filename, true) . BRNL;
if (strcasecmp(trim($filename), '.') == 0) {
if ($debugThis)
$this->output('Skipping dirItem=' . $filename);
continue;
}
if ($debugThis)
$this->output('Try getting dirItem=' . $path . "/$filename");
if ($remoteAttributes['type'] == NET_SFTP_TYPE_REGULAR) {
$remote_file = $filename;
$local_file = rtrim($localPath, '/') . "/$remote_file";
if ($debugThis)
$this->output("Remote attributes=" . print_r($remoteAttributes, true));
if (file_exists($local_file)) {
$localAttr = array('mtime' => filemtime($local_file), 'size' => filesize($local_file));
if ($debugThis)
$this->output("Local attributes=" . print_r($localAttr, true));
if ($localAttr['size'] == $remoteAttributes['size']) {
if ($debugThis)
$this->output("Local and remote files are the same size ({$remoteAttributes['size']}) - skipping");
continue;
} else {
unlink($local_file);
}
}
$this->output("About to transfer remote=$path$remote_file to local=$local_file");
$success = $this->sftp->get($remote_file, $local_file);
if ($success) {
$this->output(__FUNCTION__ . "::Successfully transferred remote=$remote_file to local=$local_file");
flush();
} else {
throw new Exception(__FUNCTION__ . "::ERROR! Failed to transfer file $filename");
}
} elseif ($remoteAttributes['type'] == NET_SFTP_TYPE_DIRECTORY) {
$remoteDir = rtrim($path, '/') . "/$filename";
//skip ones that have already been processed
if(in_array($remoteDir, $this->db_processed)) {
$this->output(__FUNCTION__ . "::Skipping already-processed subdir=$remoteDir" . HR);
continue;
}
//skip empty subdirs
$remoteDirList = $this->sftp->nlist($remoteDir);
unset($remoteDirList['.']);
if (empty($remoteDirList)) {
$this->output(__FUNCTION__ . "::Skipping empty subdir=$remoteDir" . HR);
continue;
}
$localDir = rtrim($localPath, '/') . "/$filename";
if(file_exists($localDir) && is_dir($localDir)) {
$localAttr = array('mtime' => filemtime($localDir));
if ($localAttr['mtime'] == $remoteAttributes['mtime']) {
$this->output(__FUNCTION__ . "::Skipping same modified time subdir=$remoteDir" . HR);
continue;
}
} else {
$this->ensure_localdir_exists($localDir);
}
//recurse into subdirectories
$this->output(__FUNCTION__ . "::Going into subdir=$remoteDir" . HR);
flush();
$this->getAll($remoteDir, $localDir);
$this->output(__FUNCTION__ . "::Done the folder $remoteDir" . HR);
$this->dbLog($remoteDir);
continue;
} else {
throw new Exception(__FUNCTION__ . '::ERROR! dirItem=' . $filename . ' is not a directory or a file:' . print_r($remoteAttributes, true));
}
}
unset($remoteAttributes);
return true;
}
function go($dir = '', $localDir = '') {
try {
echo 'in the go'.BRNL;
$this->output('Switching to the local working directory');
$absoluteLocalDir = $this->localPath . "/$localDir";
$this->ensure_localdir_exists($absoluteLocalDir);
$this->sftp = new Net_SFTP($this->host, $this->port, $this->timeout);
$this->output('Net_SFTP set up');
if (!$this->sftp->login($this->username, $this->password)) {
throw new Exception("Login failed with username=$this->username");
}
$this->output('Login success');
$this->showPath();
$this->output('Switching to the remote working directory');
$this->sftp->chdir($this->remoteRoot);
$this->showPath();
$path = $this->sftp->pwd();
$this->output("Start the root getAll with path=$path");
echo 'About to do the first getAll'.BRNL;
$this->getAll($path . "$dir/", $absoluteLocalDir);
//$this->output('Log:'.BRNL.'<PRE>'.print_r($this->sftp->getSFTPErrors(), true).'</PRE>'.HR);
//output('Complete Log:'.BRNL.print_r($sftp->getSFTPLog(), true).HR);
} catch (Exception $e) {
echo 'ERROR! Exception caught=' . print_r($e, true);
echo 'Complete Log:' . BRNL . $this->sftp->getSFTPLog() . HR;
echo 'Log:' . BRNL . $this->sftp->getLastSFTPError() . HR;
echo 'Backtrace:';
debug_print_backtrace();
die('died-o');
}
}
}
$config = new Config();
$markZilla = new MarkZilla($config);
$rootDirsToGet = array('filedir', 'lang', /* 'autobackups', */ 'mod_certificate', /* 'repository' */);
echo 'About to for loop'.BRNL;
foreach ($rootDirsToGet as $dir) {
$markZilla->go($dir, $dir);
$markZilla->output('Done all the folders');
}