aboutsummaryrefslogtreecommitdiff
path: root/app/Models/Share.php
blob: 7bd6de9cfa3cfb7a625c9ab779daeffca0a03634 (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
<?php

/**
 * Manage the sharing options in FreshRSS.
 */
class FreshRSS_Share {
	/**
	 * The list of available sharing options.
	 * @var array<string,FreshRSS_Share>
	 */
	private static $list_sharing = [];

	/**
	 * Register a new sharing option.
	 * @param array{'type':string,'url':string,'transform'?:array<string>|array<string,string>,'field'?:string,'help'?:string,'form'?:'simple'|'advanced',
	 *	'method'?:'GET'|'POST','HTMLtag'?:'button','deprecated'?:bool} $share_options is an array defining the share option.
	 */
	private static function register(array $share_options): void {
		$type = $share_options['type'];
		if (isset(self::$list_sharing[$type])) {
			return;
		}

		self::$list_sharing[$type] = new FreshRSS_Share(
			$type,
			$share_options['url'],
			$share_options['transform'] ?? [],
			$share_options['form'] ?? 'simple',
			$share_options['help'] ?? '',
			$share_options['method'] ?? 'GET',
			$share_options['field'] ?? null,
			$share_options['HTMLtag'] ?? null,
			$share_options['deprecated'] ?? false
		);
	}

	/**
	 * Register sharing options in a file.
	 * @param string $filename the name of the file to load.
	 */
	public static function load(string $filename): void {
		$shares_from_file = @include($filename);
		if (!is_array($shares_from_file)) {
			$shares_from_file = array();
		}

		foreach ($shares_from_file as $share_type => $share_options) {
			$share_options['type'] = $share_type;
			self::register($share_options);
		}

		uasort(self::$list_sharing, function ($a, $b) {
			return strcasecmp($a->name(), $b->name());
		});
	}

	/**
	 * Return the list of sharing options.
	 * @return array<string,FreshRSS_Share>
	 */
	public static function enum(): array {
		return self::$list_sharing;
	}

	/**
	 * @param string $type the share type, null if $type is not registered.
	 * @return FreshRSS_Share|null object related to the given type.
	 */
	public static function get(string $type): ?FreshRSS_Share {
		if (!isset(self::$list_sharing[$type])) {
			return null;
		}

		return self::$list_sharing[$type];
	}


	/** @var string */
	private $type = '';
	/** @var string */
	private $name = '';
	/** @var string */
	private $url_transform = '';
	/** @var array<string>|array<string,array<string>> */
	private $transforms = [];
	/**
	 * @phpstan-var 'simple'|'advanced'
	 * @var string
	 */
	private $form_type = 'simple';
	/** @var string */
	private $help_url = '';
	/** @var string|null */
	private $custom_name = null;
	/** @var string|null */
	private $base_url = null;
	/** @var string|null */
	private $id = null;
	/** @var string|null */
	private $title = null;
	/** @var string|null */
	private $link = null;
	/** @var bool */
	private $isDeprecated = false;
	/**
	 * @phpstan-var 'GET'|'POST'
	 * @var string
	 */
	private $method = 'GET';
	/** @var string|null */
	private $field;
	/**
	 * @phpstan-var 'button'|null
	 * @var string
	 */
	private $HTMLtag;

	/**
	 * Create a FreshRSS_Share object.
	 * @param string $type is a unique string defining the kind of share option.
	 * @param string $url_transform defines the url format to use in order to share.
	 * @param array<string>|array<string,array<string>> $transforms is an array of transformations to apply on link and title.
	 * @param 'simple'|'advanced' $form_type defines which form we have to use to complete. "simple"
	 *        is typically for a centralized service while "advanced" is for
	 *        decentralized ones.
	 * @param string $help_url is an optional url to give help on this option.
	 * @param 'GET'|'POST' $method defines the sharing method (GET or POST)
	 * @param 'button'|null $HTMLtag
	 */
	private function __construct(string $type, string $url_transform, array $transforms, string $form_type,
		string $help_url, string $method, ?string $field, ?string $HTMLtag, bool $isDeprecated = false) {
		$this->type = $type;
		$this->name = _t('gen.share.' . $type);
		$this->url_transform = $url_transform;
		$this->help_url = $help_url;
		$this->HTMLtag = $HTMLtag;
		$this->isDeprecated = $isDeprecated;
		$this->transforms = $transforms;

		if (!in_array($form_type, array('simple', 'advanced'))) {
			$form_type = 'simple';
		}
		$this->form_type = $form_type;
		if (!in_array($method, array('GET', 'POST'))) {
			$method = 'GET';
		}
		$this->method = $method;
		$this->field = $field;
	}

	/**
	 * Update a FreshRSS_Share object with information from an array.
	 * @param array<string,string> $options is a list of information to update where keys should be
	 *        in this list: name, url, id, title, link.
	 */
	public function update(array $options): void {
		$available_options = array(
			'name' => 'custom_name',
			'url' => 'base_url',
			'id' => 'id',
			'title' => 'title',
			'link' => 'link',
			'method' => 'method',
			'field' => 'field',
		);

		foreach ($options as $key => $value) {
			if (isset($available_options[$key])) {
				$this->{$available_options[$key]} = $value;
			}
		}
	}

	/**
	 * Return the current type of the share option.
	 */
	public function type(): string {
		return $this->type;
	}

	/**
	 * Return the current method of the share option.
	 * @return 'GET'|'POST'
	 */
	public function method(): string {
		return $this->method;
	}

	/**
	 * Return the current field of the share option. It’s null for shares
	 * using the GET method.
	 */
	public function field(): ?string {
		return $this->field;
	}

	/**
	 * Return the current form type of the share option.
	 * @return 'simple'|'advanced'
	 */
	public function formType(): string {
		return $this->form_type;
	}

	/**
	 * Return the current help url of the share option.
	 */
	public function help(): string {
		return $this->help_url;
	}

	/**
	 * Return the custom type of HTML tag of the share option, null for default.
	 * @return 'button'|null
	 */
	public function HTMLtag(): ?string {
		return $this->HTMLtag;
	}

	/**
	 * Return the current name of the share option.
	 */
	public function name(bool $real = false): ?string {
		if ($real || is_null($this->custom_name) || empty($this->custom_name)) {
			return $this->name;
		} else {
			return $this->custom_name;
		}
	}

	/**
	 * Return the current base url of the share option.
	 */
	public function baseUrl(): string {
		return $this->base_url;
	}

	/**
	 * Return the deprecated status of the share option.
	 */
	public function isDeprecated(): bool {
		return $this->isDeprecated;
	}

	/**
	 * Return the current url by merging url_transform and base_url.
	 */
	public function url(): string {
		$matches = array(
			'~ID~',
			'~URL~',
			'~TITLE~',
			'~LINK~',
		);
		$replaces = array(
			$this->id(),
			$this->base_url,
			$this->title(),
			$this->link(),
		);
		return str_replace($matches, $replaces, $this->url_transform);
	}

	/**
	 * Return the id.
	 * @param bool $raw true if we should get the id without transformations.
	 */
	public function id(bool $raw = false): ?string {
		if ($raw) {
			return $this->id;
		}

		return self::transform($this->id, $this->getTransform('id'));
	}

	/**
	 * Return the title.
	 * @param bool $raw true if we should get the title without transformations.
	 */
	public function title(bool $raw = false): string {
		if ($raw) {
			return $this->title;
		}

		return self::transform($this->title, $this->getTransform('title'));
	}

	/**
	 * Return the link.
	 * @param bool $raw true if we should get the link without transformations.
	 */
	public function link(bool $raw = false): string {
		if ($raw) {
			return $this->link;
		}

		return self::transform($this->link, $this->getTransform('link'));
	}

	/**
	 * Transform a data with the given functions.
	 * @param string $data the data to transform.
	 * @param array<string> $transform an array containing a list of functions to apply.
	 * @return string the transformed data.
	 */
	private static function transform(string $data, $transform): string {
		if (!is_array($transform) || empty($transform)) {
			return $data;
		}

		foreach ($transform as $action) {
			if (is_string($action) && $action != '') {
				$data = call_user_func($action, $data);
			}
		}

		return $data;
	}

	/**
	 * Get the list of transformations for the given attribute.
	 * @param string $attr the attribute of which we want the transformations.
	 * @return array<string> containing a list of transformations to apply.
	 */
	private function getTransform(string $attr): array {
		if (array_key_exists($attr, $this->transforms)) {
			return $this->transforms[$attr];
		}

		return $this->transforms;
	}
}