Share.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. continue;
  109. }
  110. $this->$available_options[$key] = $value;
  111. }
  112. }
  113. /**
  114. * Return the current type of the share option.
  115. */
  116. public function type() {
  117. return $this->type;
  118. }
  119. /**
  120. * Return the current form type of the share option.
  121. */
  122. public function formType() {
  123. return $this->form_type;
  124. }
  125. /**
  126. * Return the current help url of the share option.
  127. */
  128. public function help() {
  129. return $this->help_url;
  130. }
  131. /**
  132. * Return the current name of the share option.
  133. */
  134. public function name($real = false) {
  135. if ($real || is_null($this->custom_name) || empty($this->custom_name)) {
  136. return $this->name;
  137. } else {
  138. return $this->custom_name;
  139. }
  140. }
  141. /**
  142. * Return the current base url of the share option.
  143. */
  144. public function baseUrl() {
  145. return $this->base_url;
  146. }
  147. /**
  148. * Return the current url by merging url_transform and base_url.
  149. */
  150. public function url() {
  151. $matches = array(
  152. '~URL~',
  153. '~TITLE~',
  154. '~LINK~',
  155. );
  156. $replaces = array(
  157. $this->base_url,
  158. $this->title(),
  159. $this->link(),
  160. );
  161. return str_replace($matches, $replaces, $this->url_transform);
  162. }
  163. /**
  164. * Return the title.
  165. * @param $raw true if we should get the title without transformations.
  166. */
  167. public function title($raw = false) {
  168. if ($raw) {
  169. return $this->title;
  170. }
  171. return $this->transform($this->title, $this->getTransform('title'));
  172. }
  173. /**
  174. * Return the link.
  175. * @param $raw true if we should get the link without transformations.
  176. */
  177. public function link($raw = false) {
  178. if ($raw) {
  179. return $this->link;
  180. }
  181. return $this->transform($this->link, $this->getTransform('link'));
  182. }
  183. /**
  184. * Transform a data with the given functions.
  185. * @param $data the data to transform.
  186. * @param $tranform an array containing a list of functions to apply.
  187. * @return the transformed data.
  188. */
  189. private static function transform($data, $transform) {
  190. if (!is_array($transform) || empty($transform)) {
  191. return $data;
  192. }
  193. foreach ($transform as $action) {
  194. $data = call_user_func($action, $data);
  195. }
  196. return $data;
  197. }
  198. /**
  199. * Get the list of transformations for the given attribute.
  200. * @param $attr the attribute of which we want the transformations.
  201. * @return an array containing a list of transformations to apply.
  202. */
  203. private function getTransform($attr) {
  204. if (array_key_exists($attr, $this->transform)) {
  205. return $this->transform[$attr];
  206. }
  207. return $this->transform;
  208. }
  209. }