summaryrefslogtreecommitdiffstats
path: root/inc/download.inc.php
blob: 8358eaa306d6f67e98c145c75cb468c615c0ff00 (plain) (blame)
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
<?php

declare(strict_types=1);

class Download
{

	/**
	 * @var false|resource
	 */
	private static $curlHandle = false;

	/**
	 * Common initialization for download and downloadToFile
	 * Return file handle to header file
	 * @return false|resource
	 */
	private static function initCurl(string $url, int $timeout)
	{
		if (self::$curlHandle === false) {
			self::$curlHandle = curl_init();
			if (self::$curlHandle === false) {
				ErrorHandler::traceError('Could not initialize cURL');
			}
			curl_setopt(self::$curlHandle, CURLOPT_CONNECTTIMEOUT, ceil($timeout / 2));
			curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $timeout);
			curl_setopt(self::$curlHandle, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt(self::$curlHandle, CURLOPT_AUTOREFERER, true);
			curl_setopt(self::$curlHandle, CURLOPT_MAXREDIRS, 6);
			curl_setopt(self::$curlHandle, CURLOPT_ACCEPT_ENCODING, '');
			curl_setopt(self::$curlHandle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) SLX-Admin/1.0');
			curl_setopt(self::$curlHandle, CURLOPT_PROTOCOLS, CURLPROTO_FTP | CURLPROTO_FTPS | CURLPROTO_HTTP | CURLPROTO_HTTPS);
		}

		curl_setopt(self::$curlHandle, CURLOPT_URL, $url);

		return self::$curlHandle;
	}

	/**
	 * Download file, obey given timeout in seconds
	 * Return data on success, false on failure
	 */
	public static function asString(string $url, int $timeout, ?int &$code)
	{
		$ch = self::initCurl($url, $timeout);
		curl_setopt($ch, CURLOPT_FILE, null);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$data = curl_exec($ch);
		$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
		return $data;
	}

	/**
	 * POST-Download file, obey given timeout in seconds
	 * Return data on success, false on failure
	 * @param string $url URL to fetch
	 * @param array|false $params POST params to set in body, list of key-value-pairs
	 * @param int $timeout timeout in seconds
	 * @param ?int $code HTTP response code, or 999 on error
	 * @return string|false
	 */
	public static function asStringPost(string $url, $params, int $timeout, ?int &$code)
	{
		$string = '';
		if (is_array($params)) {
			foreach ($params as $k => $v) {
				if (!empty($string)) {
					$string .= '&';
				}
				$string .= $k . '=' . urlencode($v);
			}
		}
		$ch = self::initCurl($url, $timeout);
		curl_setopt($ch, CURLOPT_FILE, null);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
		$data = curl_exec($ch);
		$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
		return $data;
	}

	/**
	 * Download a file from a URL to file.
	 *
	 * @param string $target destination path to download file to
	 * @param string $url URL of file to download
	 * @param int $timeout timeout in seconds
	 * @param ?int $code HTTP status code passed out by reference
	 */
	public static function toFile(string $target, string $url, int $timeout, ?int &$code): bool
	{
		$fh = fopen($target, 'wb');
		if ($fh === false)
			ErrorHandler::traceError("Could not open $target for writing.");
		$ch = self::initCurl($url, $timeout);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($ch, CURLOPT_FILE, $fh);
		$res = curl_exec($ch);
		$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
		fclose($fh);
		if ($res === false) {
			@unlink($target);
			return false;
		}
		return true;
	}

}