Share.php 8.7 KB

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