Share.php 5.6 KB

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