Share.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Manage the sharing options in FreshRSS.
  5. */
  6. class FreshRSS_Share {
  7. /**
  8. * The list of available sharing options.
  9. * @var array<string,FreshRSS_Share>
  10. */
  11. private static array $list_sharing = [];
  12. /**
  13. * Register a new sharing option.
  14. * @param array{'type':string,'url':string,'transform'?:array<callable>|array<string,array<callable>>,'field'?:string,'help'?:string,'form'?:'simple'|'advanced',
  15. * 'method'?:'GET'|'POST','HTMLtag'?:'button','deprecated'?:bool} $share_options is an array defining the share option.
  16. */
  17. public static function register(array $share_options): void {
  18. $type = $share_options['type'];
  19. if (isset(self::$list_sharing[$type])) {
  20. return;
  21. }
  22. self::$list_sharing[$type] = new FreshRSS_Share(
  23. $type,
  24. $share_options['url'],
  25. $share_options['transform'] ?? [],
  26. $share_options['form'] ?? 'simple',
  27. $share_options['help'] ?? '',
  28. $share_options['method'] ?? 'GET',
  29. $share_options['field'] ?? null,
  30. $share_options['HTMLtag'] ?? null,
  31. $share_options['deprecated'] ?? false
  32. );
  33. }
  34. /**
  35. * Register sharing options in a file.
  36. * @param string $filename the name of the file to load.
  37. */
  38. public static function load(string $filename): void {
  39. $shares_from_file = @include($filename);
  40. if (!is_array($shares_from_file)) {
  41. $shares_from_file = [];
  42. }
  43. foreach ($shares_from_file as $share_type => $share_options) {
  44. $share_options['type'] = $share_type;
  45. self::register($share_options);
  46. }
  47. uasort(self::$list_sharing, static fn(FreshRSS_Share $a, FreshRSS_Share $b) => strcasecmp($a->name() ?? '', $b->name() ?? ''));
  48. }
  49. /**
  50. * Return the list of sharing options.
  51. * @return array<string,FreshRSS_Share>
  52. */
  53. public static function enum(): array {
  54. return self::$list_sharing;
  55. }
  56. /**
  57. * @param string $type the share type, null if $type is not registered.
  58. * @return FreshRSS_Share|null object related to the given type.
  59. */
  60. public static function get(string $type): ?FreshRSS_Share {
  61. return self::$list_sharing[$type] ?? null;
  62. }
  63. private readonly string $name;
  64. /**
  65. * @phpstan-var 'simple'|'advanced'
  66. */
  67. private readonly string $form_type;
  68. private ?string $custom_name = null;
  69. private ?string $base_url = null;
  70. private ?string $id = null;
  71. private ?string $title = null;
  72. private ?string $link = null;
  73. /**
  74. * @phpstan-var 'GET'|'POST'
  75. */
  76. private string $method;
  77. /**
  78. * Create a FreshRSS_Share object.
  79. * @param string $type is a unique string defining the kind of share option.
  80. * @param string $url_transform defines the url format to use in order to share.
  81. * @param array<callable>|array<string,array<callable>> $transforms is an array of transformations to apply on link and title.
  82. * @param 'simple'|'advanced' $form_type defines which form we have to use to complete. "simple"
  83. * is typically for a centralized service while "advanced" is for
  84. * decentralized ones.
  85. * @param string $help_url is an optional url to give help on this option.
  86. * @param 'GET'|'POST' $method defines the sharing method (GET or POST)
  87. * @param 'button'|null $HTMLtag
  88. */
  89. private function __construct(
  90. private readonly string $type,
  91. private readonly string $url_transform,
  92. private array $transforms,
  93. string $form_type,
  94. private readonly string $help_url,
  95. string $method,
  96. private ?string $field,
  97. private readonly ?string $HTMLtag,
  98. private readonly bool $isDeprecated = false
  99. ) {
  100. $this->name = _t('gen.share.' . $this->type);
  101. if (!in_array($form_type, ['simple', 'advanced'], true)) {
  102. $form_type = 'simple';
  103. }
  104. $this->form_type = $form_type;
  105. if (!in_array($method, ['GET', 'POST'], true)) {
  106. $method = 'GET';
  107. }
  108. $this->method = $method;
  109. }
  110. /**
  111. * Update a FreshRSS_Share object with information from an array.
  112. * @param array<string,string> $options is a list of information to update where keys should be
  113. * in this list: name, url, id, title, link.
  114. */
  115. public function update(array $options): void {
  116. foreach ($options as $key => $value) {
  117. switch ($key) {
  118. case 'name':
  119. $this->custom_name = $value;
  120. break;
  121. case 'url':
  122. $this->base_url = $value;
  123. break;
  124. case 'id':
  125. $this->id = $value;
  126. break;
  127. case 'title':
  128. $this->title = $value;
  129. break;
  130. case 'link':
  131. $this->link = $value;
  132. break;
  133. case 'method':
  134. $this->method = strcasecmp($value, 'POST') === 0 ? 'POST' : 'GET';
  135. break;
  136. case 'field':
  137. $this->field = $value;
  138. break;
  139. }
  140. }
  141. }
  142. /**
  143. * Return the current type of the share option.
  144. */
  145. public function type(): string {
  146. return $this->type;
  147. }
  148. /**
  149. * Return the current method of the share option.
  150. * @return 'GET'|'POST'
  151. */
  152. public function method(): string {
  153. return $this->method;
  154. }
  155. /**
  156. * Return the current field of the share option. It’s null for shares
  157. * using the GET method.
  158. */
  159. public function field(): ?string {
  160. return $this->field;
  161. }
  162. /**
  163. * Return the current form type of the share option.
  164. * @return 'simple'|'advanced'
  165. */
  166. public function formType(): string {
  167. return $this->form_type;
  168. }
  169. /**
  170. * Return the current help url of the share option.
  171. */
  172. public function help(): string {
  173. return $this->help_url;
  174. }
  175. /**
  176. * Return the custom type of HTML tag of the share option, null for default.
  177. * @return 'button'|null
  178. */
  179. public function HTMLtag(): ?string {
  180. return $this->HTMLtag;
  181. }
  182. /**
  183. * Return the current name of the share option.
  184. */
  185. public function name(bool $real = false): ?string {
  186. if ($real || empty($this->custom_name)) {
  187. return $this->name;
  188. } else {
  189. return $this->custom_name;
  190. }
  191. }
  192. /**
  193. * Return the current base url of the share option.
  194. */
  195. public function baseUrl(): string {
  196. return $this->base_url ?? '';
  197. }
  198. /**
  199. * Return the deprecated status of the share option.
  200. */
  201. public function isDeprecated(): bool {
  202. return $this->isDeprecated;
  203. }
  204. /**
  205. * Return the current url by merging url_transform and base_url.
  206. */
  207. public function url(): string {
  208. $matches = [
  209. '~ID~',
  210. '~URL~',
  211. '~TITLE~',
  212. '~LINK~',
  213. ];
  214. $replaces = [
  215. $this->id(),
  216. $this->base_url,
  217. $this->title(),
  218. $this->link(),
  219. ];
  220. return str_replace($matches, $replaces, $this->url_transform);
  221. }
  222. /**
  223. * Return the id.
  224. * @param bool $raw true if we should get the id without transformations.
  225. */
  226. public function id(bool $raw = false): ?string {
  227. if ($raw) {
  228. return $this->id;
  229. }
  230. if ($this->id === null) {
  231. return null;
  232. }
  233. return self::transform($this->id, $this->getTransform('id'));
  234. }
  235. /**
  236. * Return the title.
  237. * @param bool $raw true if we should get the title without transformations.
  238. */
  239. public function title(bool $raw = false): string {
  240. if ($raw) {
  241. return $this->title ?? '';
  242. }
  243. if ($this->title === null) {
  244. return '';
  245. }
  246. return self::transform($this->title, $this->getTransform('title'));
  247. }
  248. /**
  249. * Return the link.
  250. * @param bool $raw true if we should get the link without transformations.
  251. */
  252. public function link(bool $raw = false): string {
  253. if ($raw) {
  254. return $this->link ?? '';
  255. }
  256. if ($this->link === null) {
  257. return '';
  258. }
  259. return self::transform($this->link, $this->getTransform('link'));
  260. }
  261. /**
  262. * Transform a data with the given functions.
  263. * @param string $data the data to transform.
  264. * @param array<callable> $transform an array containing a list of functions to apply.
  265. * @return string the transformed data.
  266. */
  267. private static function transform(string $data, array $transform): string {
  268. if (empty($transform)) {
  269. return $data;
  270. }
  271. foreach ($transform as $action) {
  272. $data = call_user_func($action, $data);
  273. }
  274. return $data;
  275. }
  276. /**
  277. * Get the list of transformations for the given attribute.
  278. * @param string $attr the attribute of which we want the transformations.
  279. * @return array<callable> containing a list of transformations to apply.
  280. */
  281. private function getTransform(string $attr): array {
  282. if (array_key_exists($attr, $this->transforms)) {
  283. $candidates = is_array($this->transforms[$attr]) ? $this->transforms[$attr] : [];
  284. } else {
  285. $candidates = $this->transforms;
  286. }
  287. $transforms = [];
  288. foreach ($candidates as $transform) {
  289. if (is_callable($transform)) {
  290. $transforms[] = $transform;
  291. }
  292. }
  293. return $transforms;
  294. }
  295. }