Share.php 8.7 KB

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