Share.php 8.6 KB

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