Share.php 6.6 KB

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