Share.php 7.4 KB

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