Share.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 $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 $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 an array of FreshRSS_Share objects.
  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 $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 $type is a unique string defining the kind of share option.
  80. * @param $url_transform defines the url format to use in order to share.
  81. * @param $transform is an array of transformations to apply on link and title.
  82. * @param $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 $help_url is an optional url to give help on this option.
  86. * @param $method defines the sharing method (GET or POST)
  87. */
  88. private function __construct($type, $url_transform, $transform,
  89. $form_type, $help_url, $method, $field) {
  90. $this->type = $type;
  91. $this->name = _t('gen.share.' . $type);
  92. $this->url_transform = $url_transform;
  93. $this->help_url = $help_url;
  94. if (!is_array($transform)) {
  95. $transform = array();
  96. }
  97. $this->transform = $transform;
  98. if (!in_array($form_type, array('simple', 'advanced'))) {
  99. $form_type = 'simple';
  100. }
  101. $this->form_type = $form_type;
  102. if (!in_array($method, array('GET', 'POST'))) {
  103. $method = 'GET';
  104. }
  105. $this->method = $method;
  106. $this->field = $field;
  107. }
  108. /**
  109. * Update a FreshRSS_Share object with information from an array.
  110. * @param $options is a list of informations to update where keys should be
  111. * in this list: name, url, id, title, link.
  112. */
  113. public function update($options) {
  114. $available_options = array(
  115. 'name' => 'custom_name',
  116. 'url' => 'base_url',
  117. 'id' => 'id',
  118. 'title' => 'title',
  119. 'link' => 'link',
  120. 'method' => 'method',
  121. 'field' => 'field',
  122. );
  123. foreach ($options as $key => $value) {
  124. if (isset($available_options[$key])) {
  125. $this->{$available_options[$key]} = $value;
  126. }
  127. }
  128. }
  129. /**
  130. * Return the current type of the share option.
  131. */
  132. public function type() {
  133. return $this->type;
  134. }
  135. /**
  136. * Return the current method of the share option.
  137. */
  138. public function method() {
  139. return $this->method;
  140. }
  141. /**
  142. * Return the current field of the share option. It's null for shares
  143. * using the GET method.
  144. */
  145. public function field() {
  146. return $this->field;
  147. }
  148. /**
  149. * Return the current form type of the share option.
  150. */
  151. public function formType() {
  152. return $this->form_type;
  153. }
  154. /**
  155. * Return the current help url of the share option.
  156. */
  157. public function help() {
  158. return $this->help_url;
  159. }
  160. /**
  161. * Return the current name of the share option.
  162. */
  163. public function name($real = false) {
  164. if ($real || is_null($this->custom_name) || empty($this->custom_name)) {
  165. return $this->name;
  166. } else {
  167. return $this->custom_name;
  168. }
  169. }
  170. /**
  171. * Return the current base url of the share option.
  172. */
  173. public function baseUrl() {
  174. return $this->base_url;
  175. }
  176. /**
  177. * Return the current url by merging url_transform and base_url.
  178. */
  179. public function url() {
  180. $matches = array(
  181. '~ID~',
  182. '~URL~',
  183. '~TITLE~',
  184. '~LINK~',
  185. );
  186. $replaces = array(
  187. $this->id(),
  188. $this->base_url,
  189. $this->title(),
  190. $this->link(),
  191. );
  192. return str_replace($matches, $replaces, $this->url_transform);
  193. }
  194. /**
  195. * Return the id.
  196. * @param $raw true if we should get the id without transformations.
  197. */
  198. public function id($raw = false) {
  199. if ($raw) {
  200. return $this->id;
  201. }
  202. return $this->transform($this->id, $this->getTransform('id'));
  203. }
  204. /**
  205. * Return the title.
  206. * @param $raw true if we should get the title without transformations.
  207. */
  208. public function title($raw = false) {
  209. if ($raw) {
  210. return $this->title;
  211. }
  212. return $this->transform($this->title, $this->getTransform('title'));
  213. }
  214. /**
  215. * Return the link.
  216. * @param $raw true if we should get the link without transformations.
  217. */
  218. public function link($raw = false) {
  219. if ($raw) {
  220. return $this->link;
  221. }
  222. return $this->transform($this->link, $this->getTransform('link'));
  223. }
  224. /**
  225. * Transform a data with the given functions.
  226. * @param $data the data to transform.
  227. * @param $tranform an array containing a list of functions to apply.
  228. * @return the transformed data.
  229. */
  230. private static function transform($data, $transform) {
  231. if (!is_array($transform) || empty($transform)) {
  232. return $data;
  233. }
  234. foreach ($transform as $action) {
  235. if (is_string($action) && $action != '') {
  236. $data = call_user_func($action, $data);
  237. }
  238. }
  239. return $data;
  240. }
  241. /**
  242. * Get the list of transformations for the given attribute.
  243. * @param $attr the attribute of which we want the transformations.
  244. * @return an array containing a list of transformations to apply.
  245. */
  246. private function getTransform($attr) {
  247. if (array_key_exists($attr, $this->transform)) {
  248. return $this->transform[$attr];
  249. }
  250. return $this->transform;
  251. }
  252. }