Share.php 7.1 KB

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