Share.php 6.8 KB

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