summaryrefslogtreecommitdiff
path: root/html/includes/ftp.php
blob: 1a6e7eea589d7da410221fff9953c474ee7392ee (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php 

//require_once('config.php');

DeBugMessage("Execute: includes/ftp.php\nUse ftp_type: $ftp_type");

if ($ftp_type == 'php') {
	require(dirname(__FILE__) . '/ftp_class_native.php');

} else {

	require(dirname(__FILE__) . '/ftp_class.php');

}



$g_ftp_error = false;



function ftp_error($message) {

	global $html, $g_ftp_error, $ftp;

	

	$g_ftp_error = true;

	tablerow('ERROR:', $message, true);

	while(($err = $ftp->PopError()) !== false) {

		$fctname = $err['fctname'];

		$msg = $err['msg'];

		$desc = $err['desc'];

		if($desc) $tmp=' ('.$desc.')'; else $tmp='';

		if (strpos($msg, 'socket set') === 0) {

			$tmp .= "\nTry disabling the usage of sockets (set \$ftp_type = 'pure'; in config.php)";

		}

		tablerow('Error details:', $fctname.': '.$msg.$tmp, true);

	}



}



function tablerow($left, $right, $error=false) {

	global $html, $ftp_debug;

	if ($ftp_debug) return;

	$space = ($html) ? '&nbsp;' : ' ';

	$left = (empty($left)) ? $space : (($html) ? htmlentities($left) : $left);

	$right = (empty($right)) ? $space : (($html) ? nl2br(htmlentities($right)) : $right);

	$style = ($error) ? 'style="background-color: red;"' : '';

	if ($html) {

		echo '<tr>';

		echo '<td class="smheading" '. $style .' align="left" width="170">'. $left .'</td>';

		echo '<td class="grey" '. $style .' align="left" width="380">'. $right .'</td>';

		echo '</tr>';

	} else {

		if (strlen($left) < 30) $left .= str_repeat(" ", 30 - strlen($left));

		echo "$left $right\n";

	}

	flush();

}



function ftpupdate()

{

	global  $html, $ftp, $ftp_uname, $ftp_upass, $ftp_hostname, $ftp_port, $g_ftp_error, $ftp_debug,

		$ftp_delete, $ftp_movedir, $ftp_dir, $ftp_passive, $import_log_start, $import_log_extension,

		$import_utdc_download_enable, $import_utdc_log_start, $import_utdc_log_extension, $import_utdc_screenshot_start, $import_utdc_screenshot_extension,
		$import_ac_download_enable, $import_ac_log_start, $import_ac_log_extension,
		$import_ace_download_enable, $import_ace_log_start, $import_ace_log_extension, $import_ace_screenshot_start, $import_ace_screenshot_extension;



	if (!$ftp_debug) {

		if ($html) echo'<table class="zebra box" border="0" cellpadding="0" cellspacing="0" style="table-layout:fixed"><tr><td class="smheading" align="center" height="25" width="550" colspan="2">';

		echo "FTP Transferring Log Files...\n";

		if ($html) echo '</td></tr>';

	}



	// Update, from here on were going to be doing multiple FTP sessions.

	for ($i = 0; $i < count($ftp_hostname); $i++) {

		if ($i != 0) {

			if ($html and !$ftp_debug) echo '<tr><td class="" align="center" height="25" width="550" colspan="2"></td></tr>';

			echo "\n";

		}



		tablerow('Connecting to server:', $ftp_hostname[$i] .':'. $ftp_port[$i]);

		if (!$ftp->SetServer($ftp_hostname[$i], $ftp_port[$i])) {

			ftp_error("Unable to set server: ". $ftp->lastmsg); $ftp->quit(true); continue;

		}

		if (!$ftp->connect()) {

			ftp_error("Unable to connect to server: ". $ftp->lastmsg); $ftp->quit(true); continue;

		}



		tablerow('', "Connected, now logging in...");

		if (!$ftp->login($ftp_uname[$i], $ftp_upass[$i])) {

			ftp_error("Login failed!\nBad username/password?"); $ftp->quit(true); continue;

		}

		tablerow('', "Logged in!");



		if (!$ftp->SetType(FTP_BINARY)) {

			ftp_error("Could not set type: ". $ftp->lastmsg); $ftp->quit(true); continue;

		}

		if (!isset($ftp_passive[$i]) or $ftp_passive[$i]) {

			tablerow("", "Setting passive mode");

			if(!$ftp->Passive(true)) {

				ftp_error("Could not set passive mode: ". $ftp->lastmsg); $ftp->quit(true); continue;

			}

		} else {

			tablerow("", "Setting active mode");

			if(!$ftp->Passive(false)) {

				ftp_error("Could not set active mode: ". $ftp->lastmsg); $ftp->quit(true); continue;

			}

		}

		if (($pwd = $ftp->pwd()) === false) {

			ftp_error("Unable to retrieve current working directory"); $ftp->quit(true); continue;

		}

		tablerow("Current directory is:", $pwd);



		$dl_start = time();

		$dl_files = 0;

		$dl_bytes = 0;

		$error = false;

		foreach($ftp_dir[$i] as $dir) {

			if (!empty($dir)) {

				if (!$ftp->chdir($dir)) {

					ftp_error("Unable to change directory to: $dir"); $ftp->quit(true); continue;

				}

				tablerow('', "Changing directory to: $dir");

				if (($pwd = $ftp->pwd()) === false) {

					ftp_error("Unable to retrieve current working directory"); $ftp->quit(true); continue;

				}

				tablerow("New directory is:", $pwd);

			}

			

			if (($filelist = $ftp->nlist()) === false) {

				ftp_error("Unable to retrieve file list"); continue;

			}

			

			tablerow("Directory contains:", count($filelist) ." ". ((count($filelist) == 1) ? 'file' : 'files'));

			if (count($filelist) == 0) {

				continue;

			}

			foreach ($filelist as $filename) {

				if (((substr($filename, 0, strlen($import_log_start)) == $import_log_start) and (substr($filename, strlen($filename) - strlen($import_log_extension)) == $import_log_extension))

				or ($import_utdc_download_enable and (substr($filename, 0, strlen($import_utdc_log_start)) == $import_utdc_log_start) and (substr($filename, strlen($filename) - strlen($import_utdc_log_extension)) == $import_utdc_log_extension))

				or ($import_utdc_download_enable and (substr($filename, 0, strlen($import_utdc_screenshot_start)) == $import_utdc_screenshot_start) and (substr($filename, strlen($filename) - strlen($import_utdc_screenshot_extension)) == $import_utdc_screenshot_extension))
	
				or ($import_ac_download_enable and (substr($filename, 0, strlen($import_ac_log_start)) == $import_ac_log_start) and (substr($filename, strlen($filename) - strlen($import_ac_log_extension)) == $import_ac_log_extension)) 

				or ($import_ace_download_enable and (substr($filename, 0, strlen($import_ace_log_start)) == $import_ace_log_start) and (substr($filename, strlen($filename) - strlen($import_ace_log_extension)) == $import_ace_log_extension)) 

				or ($import_ace_download_enable and (substr($filename, 0, strlen($import_ace_screenshot_start)) == $import_ace_screenshot_start) and (substr($filename, strlen($filename) - strlen($import_ace_screenshot_extension)) == $import_ace_screenshot_extension))) {
				} else {
					continue;
				}

				$size = $ftp->get($filename, 'logs/' . $filename);

				if ($size === FALSE) {

					$result = 'ERROR!';

					$error = true;

					if (file_exists('logs/' . $filename)) {
						unlink('logs/' . $filename);
					}

				} else {

					$result = "OK (". number_format(round(($size / 1024), 0)) ." KB)";

					$dl_files++;

					$dl_bytes += $size;

				}

				tablerow(($dl_files == 1) ? 'Downloading...' : '', "$filename -> $result");

				if ((!isset($ftp_delete[$i]) or $ftp_delete[$i]) and !$error) {
					$ftp->delete($filename);
				}
				else {
					// rename the file to prevent reimporting
					$ftp->rename($filename, "~" . $filename);
				}

			}

		}	

		$dl_kb = number_format(round(($dl_bytes / 1024), 0));

		$dl_time = time() - $dl_start;

		tablerow("Downloaded:", "$dl_files ". ((count($filelist) == 1) ? 'file' : 'files') ." ($dl_kb KB) in $dl_time seconds");



		if ($error) {

			ftp_error('There were errors when downoading (some) files!');

		}



		tablerow("Disconnecting...", "done!");

		$ftp->quit(true);

	}

	if (!$ftp_debug and $html) echo '</table><br />';

	echo "\n\n";



	//update timestamp

	

	if (!$g_ftp_error) {

		$file = fopen('includes/ftptimestamp.php', 'wb+', 1);

		fwrite($file, time());

		fclose($file);

	}

}





$fname = 'includes/ftptimestamp.php';

$timestamp = 0;



if(file_exists($fname))

{

	$file = fopen($fname, 'rb');

	$timestamp = trim(my_fgets($file));

	fclose($file);

}



if(!$timestamp || (time() - $timestamp) > $ftp_interval*60) {

	if ($timestamp) {

		if ($html) echo '<p class="pages">';

		echo "Last FTP update more than $ftp_interval minutes ago, starting update ($ftp_type): \n";

		if ($html) echo '</p>';

	}

	if ($ftp_debug) {

		if ($html) echo '<table class="box" border="0"><tr><td class="smheading" width="550">';

		echo "FTP Debugging Output:\n";

		if ($html) echo '</td></tr><tr><td width="550" align="left"><pre>';

	}

	$ftp = new ftp($ftp_debug, $ftp_debug);

	ftpupdate();

	if ($ftp_debug and $html) echo '</pre></td></tr></table><br />';

} else {

	if ($html) echo '<p class="pages">';

	echo "Last FTP update was ". round(((time() - $timestamp) / 60), 0) ." minutes ago, no update necessary\n";

	if ($html) echo '</p>';

}



?>