Share.php 8.5 KB

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