Share.php 6.2 KB

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