Share.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 string $type;
  64. private string $name;
  65. private string $url_transform;
  66. /** @var array<callable>|array<string,array<callable>> */
  67. private array $transforms;
  68. /**
  69. * @phpstan-var 'simple'|'advanced'
  70. */
  71. private string $form_type;
  72. private string $help_url;
  73. private ?string $custom_name = null;
  74. private ?string $base_url = null;
  75. private ?string $id = null;
  76. private ?string $title = null;
  77. private ?string $link = null;
  78. private bool $isDeprecated;
  79. /**
  80. * @phpstan-var 'GET'|'POST'
  81. */
  82. private string $method;
  83. private ?string $field;
  84. /**
  85. * @phpstan-var 'button'|null
  86. */
  87. private ?string $HTMLtag;
  88. /**
  89. * Create a FreshRSS_Share object.
  90. * @param string $type is a unique string defining the kind of share option.
  91. * @param string $url_transform defines the url format to use in order to share.
  92. * @param array<callable>|array<string,array<callable>> $transforms is an array of transformations to apply on link and title.
  93. * @param 'simple'|'advanced' $form_type defines which form we have to use to complete. "simple"
  94. * is typically for a centralized service while "advanced" is for
  95. * decentralized ones.
  96. * @param string $help_url is an optional url to give help on this option.
  97. * @param 'GET'|'POST' $method defines the sharing method (GET or POST)
  98. * @param string|null $field
  99. * @param 'button'|null $HTMLtag
  100. * @param bool $isDeprecated
  101. */
  102. private function __construct(string $type, string $url_transform, array $transforms, string $form_type,
  103. string $help_url, string $method, ?string $field, ?string $HTMLtag, bool $isDeprecated = false) {
  104. $this->type = $type;
  105. $this->name = _t('gen.share.' . $type);
  106. $this->url_transform = $url_transform;
  107. $this->help_url = $help_url;
  108. $this->HTMLtag = $HTMLtag;
  109. $this->isDeprecated = $isDeprecated;
  110. $this->transforms = $transforms;
  111. if (!in_array($form_type, ['simple', 'advanced'], true)) {
  112. $form_type = 'simple';
  113. }
  114. $this->form_type = $form_type;
  115. if (!in_array($method, ['GET', 'POST'], true)) {
  116. $method = 'GET';
  117. }
  118. $this->method = $method;
  119. $this->field = $field;
  120. }
  121. /**
  122. * Update a FreshRSS_Share object with information from an array.
  123. * @param array<string,string> $options is a list of information to update where keys should be
  124. * in this list: name, url, id, title, link.
  125. */
  126. public function update(array $options): void {
  127. foreach ($options as $key => $value) {
  128. switch ($key) {
  129. case 'name':
  130. $this->custom_name = $value;
  131. break;
  132. case 'url':
  133. $this->base_url = $value;
  134. break;
  135. case 'id':
  136. $this->id = $value;
  137. break;
  138. case 'title':
  139. $this->title = $value;
  140. break;
  141. case 'link':
  142. $this->link = $value;
  143. break;
  144. case 'method':
  145. $this->method = strcasecmp($value, 'POST') === 0 ? 'POST' : 'GET';
  146. break;
  147. case 'field':
  148. $this->field = $value;
  149. break;
  150. }
  151. }
  152. }
  153. /**
  154. * Return the current type of the share option.
  155. */
  156. public function type(): string {
  157. return $this->type;
  158. }
  159. /**
  160. * Return the current method of the share option.
  161. * @return 'GET'|'POST'
  162. */
  163. public function method(): string {
  164. return $this->method;
  165. }
  166. /**
  167. * Return the current field of the share option. It’s null for shares
  168. * using the GET method.
  169. */
  170. public function field(): ?string {
  171. return $this->field;
  172. }
  173. /**
  174. * Return the current form type of the share option.
  175. * @return 'simple'|'advanced'
  176. */
  177. public function formType(): string {
  178. return $this->form_type;
  179. }
  180. /**
  181. * Return the current help url of the share option.
  182. */
  183. public function help(): string {
  184. return $this->help_url;
  185. }
  186. /**
  187. * Return the custom type of HTML tag of the share option, null for default.
  188. * @return 'button'|null
  189. */
  190. public function HTMLtag(): ?string {
  191. return $this->HTMLtag;
  192. }
  193. /**
  194. * Return the current name of the share option.
  195. */
  196. public function name(bool $real = false): ?string {
  197. if ($real || empty($this->custom_name)) {
  198. return $this->name;
  199. } else {
  200. return $this->custom_name;
  201. }
  202. }
  203. /**
  204. * Return the current base url of the share option.
  205. */
  206. public function baseUrl(): string {
  207. return $this->base_url ?? '';
  208. }
  209. /**
  210. * Return the deprecated status of the share option.
  211. */
  212. public function isDeprecated(): bool {
  213. return $this->isDeprecated;
  214. }
  215. /**
  216. * Return the current url by merging url_transform and base_url.
  217. */
  218. public function url(): string {
  219. $matches = [
  220. '~ID~',
  221. '~URL~',
  222. '~TITLE~',
  223. '~LINK~',
  224. ];
  225. $replaces = [
  226. $this->id(),
  227. $this->base_url,
  228. $this->title(),
  229. $this->link(),
  230. ];
  231. return str_replace($matches, $replaces, $this->url_transform);
  232. }
  233. /**
  234. * Return the id.
  235. * @param bool $raw true if we should get the id without transformations.
  236. */
  237. public function id(bool $raw = false): ?string {
  238. if ($raw) {
  239. return $this->id;
  240. }
  241. if ($this->id === null) {
  242. return null;
  243. }
  244. return self::transform($this->id, $this->getTransform('id'));
  245. }
  246. /**
  247. * Return the title.
  248. * @param bool $raw true if we should get the title without transformations.
  249. */
  250. public function title(bool $raw = false): string {
  251. if ($raw) {
  252. return $this->title ?? '';
  253. }
  254. if ($this->title === null) {
  255. return '';
  256. }
  257. return self::transform($this->title, $this->getTransform('title'));
  258. }
  259. /**
  260. * Return the link.
  261. * @param bool $raw true if we should get the link without transformations.
  262. */
  263. public function link(bool $raw = false): string {
  264. if ($raw) {
  265. return $this->link ?? '';
  266. }
  267. if ($this->link === null) {
  268. return '';
  269. }
  270. return self::transform($this->link, $this->getTransform('link'));
  271. }
  272. /**
  273. * Transform a data with the given functions.
  274. * @param string $data the data to transform.
  275. * @param array<callable> $transform an array containing a list of functions to apply.
  276. * @return string the transformed data.
  277. */
  278. private static function transform(string $data, array $transform): string {
  279. if (empty($transform)) {
  280. return $data;
  281. }
  282. foreach ($transform as $action) {
  283. $data = call_user_func($action, $data);
  284. }
  285. return $data;
  286. }
  287. /**
  288. * Get the list of transformations for the given attribute.
  289. * @param string $attr the attribute of which we want the transformations.
  290. * @return array<callable> containing a list of transformations to apply.
  291. */
  292. private function getTransform(string $attr): array {
  293. if (array_key_exists($attr, $this->transforms)) {
  294. $candidates = is_array($this->transforms[$attr]) ? $this->transforms[$attr] : [];
  295. } else {
  296. $candidates = $this->transforms;
  297. }
  298. $transforms = [];
  299. foreach ($candidates as $transform) {
  300. if (is_callable($transform)) {
  301. $transforms[] = $transform;
  302. }
  303. }
  304. return $transforms;
  305. }
  306. }