dotNotationUtil.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. declare(strict_types=1);
  3. final class FreshRSS_dotNotation_Util
  4. {
  5. /**
  6. * Get an item from an array using "dot" notation.
  7. * Functions adapted from https://stackoverflow.com/a/39118759
  8. * https://github.com/illuminate/support/blob/52e8f314b8043860b1c09e5c2c7e8cca94aafc7d/Arr.php#L270-L305
  9. * Newer version in
  10. * https://github.com/laravel/framework/blob/10.x/src/Illuminate/Collections/Arr.php#L302-L337
  11. *
  12. * @param \ArrayAccess<string,mixed>|array<string,mixed>|mixed $array
  13. * @param string|null $key
  14. * @param mixed $default
  15. * @return mixed
  16. */
  17. public static function get($array, ?string $key, mixed $default = null) {
  18. if (!static::accessible($array)) {
  19. return static::value($default);
  20. }
  21. /** @var \ArrayAccess<string,mixed>|array<string,mixed> $array */
  22. if (in_array($key, [null, '', '.', '$'], true)) {
  23. return $array;
  24. }
  25. // Compatibility with brackets path such as `items[0].value`
  26. $key = preg_replace('/\[(\d+)\]/', '.$1', $key);
  27. if ($key === null) {
  28. return null;
  29. }
  30. if (static::exists($array, $key)) {
  31. return $array[$key];
  32. }
  33. if (strpos($key, '.') === false) {
  34. return $array[$key] ?? static::value($default);
  35. }
  36. foreach (explode('.', $key) as $segment) {
  37. if (static::accessible($array) && static::exists($array, $segment)) {
  38. $array = $array[$segment];
  39. } else {
  40. return static::value($default);
  41. }
  42. }
  43. return $array;
  44. }
  45. /**
  46. * Get a string from an array using "dot" notation.
  47. *
  48. * @param \ArrayAccess<string,mixed>|array<string,mixed>|mixed $array
  49. * @param string|null $key
  50. */
  51. public static function getString($array, ?string $key): ?string {
  52. $result = self::get($array, $key, null);
  53. return is_string($result) || is_bool($result) || is_float($result) || is_int($result) ? (string)$result : null;
  54. }
  55. /**
  56. * Determine whether the given value is array accessible.
  57. *
  58. * @param mixed $value
  59. * @return bool
  60. */
  61. private static function accessible($value): bool {
  62. return is_array($value) || $value instanceof \ArrayAccess;
  63. }
  64. /**
  65. * Determine if the given key exists in the provided array.
  66. *
  67. * @param \ArrayAccess<string,mixed>|array<string,mixed>|mixed $array
  68. * @param string $key
  69. * @return bool
  70. */
  71. private static function exists($array, string $key): bool {
  72. if ($array instanceof \ArrayAccess) {
  73. return $array->offsetExists($key);
  74. }
  75. if (is_array($array)) {
  76. return array_key_exists($key, $array);
  77. }
  78. return false;
  79. }
  80. /** @param mixed $value */
  81. private static function value($value): mixed {
  82. return $value instanceof Closure ? $value() : $value;
  83. }
  84. /**
  85. * Convert a JSON object to a RSS document
  86. * mapping fields from the JSON object into RSS equivalents
  87. * according to the dot-separated paths
  88. *
  89. * @param array<string> $jf json feed
  90. * @param string $feedSourceUrl the source URL for the feed
  91. * @param array<string,string> $dotNotation dot notation to map JSON into RSS
  92. * @param string $defaultRssTitle Default title of the RSS feed, if not already provided in dotNotation `feedTitle`
  93. */
  94. public static function convertJsonToRss(array $jf, string $feedSourceUrl, array $dotNotation, string $defaultRssTitle = ''): ?string {
  95. if (!isset($dotNotation['item']) || $dotNotation['item'] === '') {
  96. return null; //no definition of item path, but we can't scrape anything without knowing this
  97. }
  98. $view = new FreshRSS_View();
  99. $view->_path('index/rss.phtml');
  100. $view->internal_rendering = true;
  101. $view->rss_url = htmlspecialchars($feedSourceUrl, ENT_COMPAT, 'UTF-8');
  102. $view->html_url = $view->rss_url;
  103. $view->entries = [];
  104. $view->rss_title = isset($dotNotation['feedTitle'])
  105. ? (htmlspecialchars(FreshRSS_dotNotation_Util::getString($jf, $dotNotation['feedTitle']) ?? '', ENT_COMPAT, 'UTF-8') ?: $defaultRssTitle)
  106. : $defaultRssTitle;
  107. $jsonItems = FreshRSS_dotNotation_Util::get($jf, $dotNotation['item']);
  108. if (!is_array($jsonItems) || count($jsonItems) === 0) {
  109. return null;
  110. }
  111. foreach ($jsonItems as $jsonItem) {
  112. $rssItem = [];
  113. $rssItem['link'] = isset($dotNotation['itemUri']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemUri']) ?? '' : '';
  114. if (empty($rssItem['link'])) {
  115. continue;
  116. }
  117. $rssItem['title'] = isset($dotNotation['itemTitle']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemTitle']) ?? '' : '';
  118. $rssItem['author'] = isset($dotNotation['itemAuthor']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemAuthor']) ?? '' : '';
  119. $rssItem['timestamp'] = isset($dotNotation['itemTimestamp']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemTimestamp']) ?? '' : '';
  120. //get simple content, but if a path for HTML content has been provided, replace the simple content with HTML content
  121. $rssItem['content'] = isset($dotNotation['itemContent']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemContent']) ?? '' : '';
  122. $rssItem['content'] = isset($dotNotation['itemContentHTML'])
  123. ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemContentHTML']) ?? ''
  124. : $rssItem['content'];
  125. if (isset($dotNotation['itemTimeFormat']) && is_string($dotNotation['itemTimeFormat'])) {
  126. $dateTime = DateTime::createFromFormat($dotNotation['itemTimeFormat'], $rssItem['timestamp']);
  127. if ($dateTime != false) {
  128. $rssItem['timestamp'] = $dateTime->format(DateTime::ATOM);
  129. }
  130. }
  131. if (isset($dotNotation['itemCategories'])) {
  132. $jsonItemCategories = FreshRSS_dotNotation_Util::get($jsonItem, $dotNotation['itemCategories']);
  133. if (is_string($jsonItemCategories) && $jsonItemCategories !== '') {
  134. $rssItem['tags'] = [$jsonItemCategories];
  135. } elseif (is_array($jsonItemCategories) && count($jsonItemCategories) > 0) {
  136. $rssItem['tags'] = [];
  137. foreach ($jsonItemCategories as $jsonItemCategory) {
  138. if (is_string($jsonItemCategory)) {
  139. $rssItem['tags'][] = $jsonItemCategory;
  140. }
  141. }
  142. }
  143. }
  144. $rssItem['thumbnail'] = isset($dotNotation['itemThumbnail']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemThumbnail']) ?? '' : '';
  145. //Enclosures?
  146. if (isset($dotNotation['itemAttachment'])) {
  147. $jsonItemAttachments = FreshRSS_dotNotation_Util::get($jsonItem, $dotNotation['itemAttachment']);
  148. if (is_array($jsonItemAttachments) && count($jsonItemAttachments) > 0) {
  149. $rssItem['attachments'] = [];
  150. foreach ($jsonItemAttachments as $attachment) {
  151. $rssAttachment = [];
  152. $rssAttachment['url'] = isset($dotNotation['itemAttachmentUrl'])
  153. ? FreshRSS_dotNotation_Util::getString($attachment, $dotNotation['itemAttachmentUrl'])
  154. : '';
  155. $rssAttachment['type'] = isset($dotNotation['itemAttachmentType'])
  156. ? FreshRSS_dotNotation_Util::getString($attachment, $dotNotation['itemAttachmentType'])
  157. : '';
  158. $rssAttachment['length'] = isset($dotNotation['itemAttachmentLength'])
  159. ? FreshRSS_dotNotation_Util::get($attachment, $dotNotation['itemAttachmentLength'])
  160. : '';
  161. $rssItem['attachments'][] = $rssAttachment;
  162. }
  163. }
  164. }
  165. if (isset($dotNotation['itemUid'])) {
  166. $rssItem['guid'] = FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemUid']);
  167. }
  168. if (empty($rssItem['guid'])) {
  169. $rssItem['guid'] = 'urn:sha1:' . sha1($rssItem['title'] . $rssItem['content'] . $rssItem['link']);
  170. }
  171. if ($rssItem['title'] != '' || $rssItem['content'] != '' || $rssItem['link'] != '') {
  172. // HTML-encoding/escaping of the relevant fields (all except 'content')
  173. foreach (['author', 'guid', 'link', 'thumbnail', 'timestamp', 'tags', 'title'] as $key) {
  174. if (!empty($rssItem[$key]) && is_string($rssItem[$key])) {
  175. $rssItem[$key] = Minz_Helper::htmlspecialchars_utf8($rssItem[$key]);
  176. }
  177. }
  178. $view->entries[] = FreshRSS_Entry::fromArray($rssItem);
  179. }
  180. }
  181. return $view->renderToString();
  182. }
  183. }