summaryrefslogtreecommitdiff
blob: b6e1ac1dcf1bcbff89a1cc29e7382ce27aabdbf8 (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
#!/usr/bin/php
<?php
function update_cache($url, $type=null, $key=null) {
	if (strtolower(substr($url, 0, 6)) == 'ftp://') {
		$result=update_cache_ftp(substr($url, 6));
		if ($result) {
			list($command, $file, $mtime, $size)=$result;
		}
	}
	if (isset($command)) {
		if (!is_file(CACHE."/$file") || filemtime(CACHE."/$file") < $mtime) {
			echo "$command\n";
			passthru($command);
			if (filesize(TMPDIR."/$file") != $size) return false;
			touch(TMPDIR."/$file", $mtime);
			rename(TMPDIR."/$file", CACHE."/$file");
		}
		if (isset($type)) {
			if (!isset($key)) $key='';
			$r=query('SELECT * FROM `cache` WHERE `type`="'.$type.'" AND `key`="'.$key.'"');
			if ($r->rowCount()) {
				$obj=new sql_cache_entry($r->fetch(PDO::FETCH_ASSOC));
				if ($obj->file != $file)
					unlink(CACHE."/$obj->file");
			} else {
				$obj=new sql_cache_entry();
				$obj->type=$type;
				$obj->key=$key;
			}
			$obj->file=$file;
			$obj->write();
		}
		return true;
	}
	return false;
}
function update_cache_ftp($url) {
	$args=array('-c', '--timeout=20');
	$pattern=substr($url, strrpos($url, '/')+1);
	$url=substr($url, 0, strlen($url)-strlen($pattern)-1);
	if (strpos($url, '/') === false) {
		$server=$url;
		$sdir=null;
	} else {
		list($server, $sdir)=explode('/', $url, 2);
	}
	if (strpos($server, '@') === false) {
		$user='anonymous';
		$pass='ingenue@soc.dev.gentoo.org';
	} else {
		list($user, $server)=explode('@', $server);
		if (strpos($user, ':') === false) {
			$pass='ingenue@soc.dev.gentoo.org';
			$args[]=escapeshellarg("--password=$pass");
		} else
			list($user, $pass)=explode(':', $user, 2);
		$args[]=escapeshellarg("--user=$user");
	}
	if (strpos($server, ':') === false)
		$port=21;
	else
		list($server, $port)=explode(':', $server);
	echo "Connecting to $server:$port...";
	$ftp=ftp_connect($server, $port, 20); if (!$ftp) return false; // Timeout=20
	echo "done\n";
	echo "Logging in as $user...";
	$r=ftp_login($ftp, $user, $pass); if (!$r) return false;
	echo "done\n";
	echo "Changing dir to /$sdir...";
	$r=ftp_chdir($ftp, '/'.$sdir); if (!$r) return false;
	echo "done\n";
	echo "Getting list of files...";
	$list=ftp_nlist($ftp, '.');
	echo "done\n";
	$newest=null;
	$newest_id=null;
	foreach ($list as $file) {
		if (!preg_match("/^$pattern$/", $file, $match)) continue;
		if (isset($match[1])) {
			if ($match[1] > $newest_id) {
				$newest_id=$match[1];
				$newest=$file;
			}
		} else {
			$mtime=ftp_mdtm($ftp, $file);
			if ($mtime > $newest_id) {
				$newest=$file;
				$newest_id=$mtime;
			}
		}
	}
	if ($newest === null) {
		echo "No files matched pattern\n";
		return;
	}
	$mtime=ftp_mdtm($ftp, $newest);
	$size=ftp_size($ftp, $newest);
	ftp_close($ftp);
	$args[]=escapeshellarg('--directory-prefix='.TMPDIR);
	$args[]="ftp://$server/$sdir/$newest";
	$cmd='wget '.implode(' ', $args);
	return array($cmd, $newest, $mtime, $size);
}
require_once(dirname(__FILE__).'/shared/include/includes.php'); // __DIR__ 5.3.0
require_once(SHARED.'/include/dbinit.php');
$file=fopen(CACHE.'/conf', 'r');
for ($line=fgets($file, 10240); !feof($file); $line=fgets($file, 10240)) {
	$line=trim($line);
	if (!$line || substr($line, 0, 1) == '#') continue;
	$array=explode("\t", $line);
	foreach ($array as &$val) {
		if (strlen($val) == 0 || strtolower($val) == 'null') $val=null;
	}
	for ($i=0; $i < 5; $i++) {
		if (call_user_func_array('update_cache', $array)) break;
		echo "Failed... sleep 5\n";
		sleep(5);
	}
}
?>