summaryrefslogtreecommitdiff
path: root/init.php
blob: d8b42abfb7ae97fe0ec8ed46d4f859375a57a949 (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
<?php
require_once __DIR__ . "/vendor/autoload.php";

use \Aws\S3\S3Client;
use \Aws\Exception\AwsException;

class Cache_S3 extends Plugin implements Cache_Adapter {

	/** @phpstan-ignore-next-line */
	private S3Client $s3;
	private string $dir;

	const CACHE_S3_ENDPOINT = "CACHE_S3_ENDPOINT";
	const CACHE_S3_BUCKET = "CACHE_S3_BUCKET";
	const CACHE_S3_REGION = "CACHE_S3_REGION";
	const CACHE_S3_ACCESS_KEY = "CACHE_S3_ACCESS_KEY";
	const CACHE_S3_SECRET_KEY = "CACHE_S3_SECRET_KEY";

	public function remove(string $filename): bool {
		return unlink($this->get_full_path($filename));
	}

	function about() {
		return array(null,
			"File cache backend using S3 protocol (experimental)",
			"fox",
			true);
	}

	function init($host) {
		Config::add(self::CACHE_S3_ENDPOINT, "", Config::T_STRING);
		Config::add(self::CACHE_S3_BUCKET, "", Config::T_STRING);
		Config::add(self::CACHE_S3_REGION, "", Config::T_STRING);
		Config::add(self::CACHE_S3_ACCESS_KEY, "", Config::T_STRING);
		Config::add(self::CACHE_S3_SECRET_KEY, "", Config::T_STRING);

		$s3_client_props = [
			'version' => 'latest',
			'region'  => Config::get(self::CACHE_S3_REGION),
			'endpoint' => Config::get(self::CACHE_S3_ENDPOINT),
			'use_path_style_endpoint' => true,
		];

		if (Config::get(self::CACHE_S3_ACCESS_KEY) && Config::get(self::CACHE_S3_SECRET_KEY)) {
			$s3_client_props['credentials'] = [
			  'key'    => Config::get(self::CACHE_S3_ACCESS_KEY),
			  'secret' => Config::get(self::CACHE_S3_SECRET_KEY),
			];
		}

		/** @phpstan-ignore-next-line */
		$this->s3 = new Aws\S3\S3Client($s3_client_props);

		/** @phpstan-ignore-next-line */
		$this->s3->registerStreamWrapper();
	}

	public function get_mtime(string $filename) {
		return filemtime($this->get_full_path($filename));
	}

	public function set_dir(string $dir): void {
		$this->dir = $dir;
	}

	public function get_dir(): string {
		return $this->dir;
	}

	public function make_dir(): bool {
		return true;
	}

	public function is_writable(?string $filename = null): bool {
		return true;
	}

	public function exists(string $filename): bool {
		return file_exists($this->get_full_path($filename));
	}

	public function get_size(string $filename) {
		return filesize($this->get_full_path($filename));
	}

	public function put(string $filename, $data) {
		return file_put_contents($this->get_full_path($filename), $data);
	}

	public function get(string $filename): ?string {
		return file_get_contents($this->get_full_path($filename));
	}

	public function get_full_path(string $filename): string {
		return 's3://' . Config::get(self::CACHE_S3_BUCKET) . '/' . ($this->dir ? $this->dir . '/' : '') . basename(clean($filename));
	}

	public function get_mime_type(string $filename) {
		$fh = fopen($this->get_full_path($filename), 'r');

		if ($fh) {
			$mimetype =  mime_content_type($fh);

			fclose($fh);

			return $mimetype;
		}

		return false;
	}

	public function send(string $filename) {
		return readfile($this->get_full_path($filename));
	}

	public function expire_all(): void {
		$dh = opendir('s3://' . Config::get(self::CACHE_S3_BUCKET));
		$rules = [];

		if ($dh) {
			while (($file = readdir($dh)) !== false) {
				$full_path = $this->get_full_path($file);

				if (is_dir($full_path) && !file_exists("$full_path/.no-auto-expiry")) {
					array_push($rules,
						[
							'ID' => $file,
							'Expiration' => [
								'Days' => (int) Config::get(Config::CACHE_MAX_DAYS),
							],
							'Filter' => [
								'Prefix' => $file
							],
							'Status' => 'Enabled'
						]
					);
				}
			}
			closedir($dh);
		}

		try {
			/** @phpstan-ignore-next-line */
			$this->s3->putBucketLifecycleConfiguration([
				'Bucket' => Config::get(self::CACHE_S3_BUCKET),
				'LifecycleConfiguration' => [
					'Rules' => $rules
				]
			]);

			Debug::log("Set lifecycle policy on " . count($rules) . " prefixe(s).");
		} catch (Exception $e) {
			user_error($e, E_USER_WARNING);
		}
	}

	function api_version() {
		return 2;
	}

}