dotNotationUtil.php 7.5 KB

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