dotNotationUtil.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * @phpstan-assert-if-true \ArrayAccess<string,mixed>|array<string,mixed> $array
  62. */
  63. private static function exists($array, string $key): bool {
  64. if ($array instanceof \ArrayAccess) {
  65. return $array->offsetExists($key);
  66. }
  67. if (is_array($array)) {
  68. return array_key_exists($key, $array);
  69. }
  70. return false;
  71. }
  72. private static function value(mixed $value): mixed {
  73. return $value instanceof Closure ? $value() : $value;
  74. }
  75. /**
  76. * Convert a JSON object to a RSS document
  77. * mapping fields from the JSON object into RSS equivalents
  78. * according to the dot-separated paths
  79. *
  80. * @param array<int|string,mixed> $jf json feed
  81. * @param string $feedSourceUrl the source URL for the feed
  82. * @param array<string,string> $dotNotation dot notation to map JSON into RSS
  83. * @param string $defaultRssTitle Default title of the RSS feed, if not already provided in dotNotation `feedTitle`
  84. */
  85. public static function convertJsonToRss(array $jf, string $feedSourceUrl, array $dotNotation, string $defaultRssTitle = ''): ?string {
  86. if (!isset($dotNotation['item']) || $dotNotation['item'] === '') {
  87. return null; //no definition of item path, but we can't scrape anything without knowing this
  88. }
  89. $view = new FreshRSS_View();
  90. $view->_path('index/rss.phtml');
  91. $view->internal_rendering = true;
  92. $view->rss_url = htmlspecialchars($feedSourceUrl, ENT_COMPAT, 'UTF-8');
  93. $view->html_url = $view->rss_url;
  94. $view->entries = [];
  95. $view->rss_title = isset($dotNotation['feedTitle'])
  96. ? (htmlspecialchars(FreshRSS_dotNotation_Util::getString($jf, $dotNotation['feedTitle']) ?? '', ENT_COMPAT, 'UTF-8') ?: $defaultRssTitle)
  97. : $defaultRssTitle;
  98. $jsonItems = FreshRSS_dotNotation_Util::get($jf, $dotNotation['item']);
  99. if (!is_array($jsonItems) || count($jsonItems) === 0) {
  100. return null;
  101. }
  102. foreach ($jsonItems as $jsonItem) {
  103. $rssItem = [];
  104. $rssItem['link'] = isset($dotNotation['itemUri']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemUri']) ?? '' : '';
  105. if (empty($rssItem['link'])) {
  106. continue;
  107. }
  108. $rssItem['title'] = isset($dotNotation['itemTitle']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemTitle']) ?? '' : '';
  109. $rssItem['author'] = isset($dotNotation['itemAuthor']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemAuthor']) ?? '' : '';
  110. $rssItem['timestamp'] = isset($dotNotation['itemTimestamp']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemTimestamp']) ?? '' : '';
  111. //get simple content, but if a path for HTML content has been provided, replace the simple content with HTML content
  112. $rssItem['content'] = isset($dotNotation['itemContent']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemContent']) ?? '' : '';
  113. $rssItem['content'] = isset($dotNotation['itemContentHTML'])
  114. ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemContentHTML']) ?? ''
  115. : $rssItem['content'];
  116. if (isset($dotNotation['itemTimeFormat']) && is_string($dotNotation['itemTimeFormat'])) {
  117. $dateTime = DateTime::createFromFormat($dotNotation['itemTimeFormat'], $rssItem['timestamp']);
  118. if ($dateTime != false) {
  119. $rssItem['timestamp'] = $dateTime->format(DateTime::ATOM);
  120. }
  121. }
  122. if (isset($dotNotation['itemCategories'])) {
  123. $jsonItemCategories = FreshRSS_dotNotation_Util::get($jsonItem, $dotNotation['itemCategories']);
  124. if (is_string($jsonItemCategories) && $jsonItemCategories !== '') {
  125. $rssItem['tags'] = [$jsonItemCategories];
  126. } elseif (is_array($jsonItemCategories) && count($jsonItemCategories) > 0) {
  127. $rssItem['tags'] = [];
  128. foreach ($jsonItemCategories as $jsonItemCategory) {
  129. if (is_string($jsonItemCategory)) {
  130. $rssItem['tags'][] = $jsonItemCategory;
  131. }
  132. }
  133. }
  134. }
  135. $rssItem['thumbnail'] = isset($dotNotation['itemThumbnail']) ? FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemThumbnail']) ?? '' : '';
  136. //Enclosures?
  137. if (isset($dotNotation['itemAttachment'])) {
  138. $jsonItemAttachments = FreshRSS_dotNotation_Util::get($jsonItem, $dotNotation['itemAttachment']);
  139. if (is_array($jsonItemAttachments) && count($jsonItemAttachments) > 0) {
  140. $rssItem['attachments'] = [];
  141. foreach ($jsonItemAttachments as $attachment) {
  142. $rssAttachment = [];
  143. $rssAttachment['url'] = isset($dotNotation['itemAttachmentUrl'])
  144. ? FreshRSS_dotNotation_Util::getString($attachment, $dotNotation['itemAttachmentUrl'])
  145. : '';
  146. $rssAttachment['type'] = isset($dotNotation['itemAttachmentType'])
  147. ? FreshRSS_dotNotation_Util::getString($attachment, $dotNotation['itemAttachmentType'])
  148. : '';
  149. $rssAttachment['length'] = isset($dotNotation['itemAttachmentLength'])
  150. ? FreshRSS_dotNotation_Util::get($attachment, $dotNotation['itemAttachmentLength'])
  151. : '';
  152. $rssItem['attachments'][] = $rssAttachment;
  153. }
  154. }
  155. }
  156. if (isset($dotNotation['itemUid'])) {
  157. $rssItem['guid'] = FreshRSS_dotNotation_Util::getString($jsonItem, $dotNotation['itemUid']);
  158. }
  159. if (empty($rssItem['guid'])) {
  160. $rssItem['guid'] = 'urn:sha1:' . sha1($rssItem['title'] . $rssItem['content'] . $rssItem['link']);
  161. }
  162. if ($rssItem['title'] != '' || $rssItem['content'] != '' || $rssItem['link'] != '') {
  163. // HTML-encoding/escaping of the relevant fields (all except 'content')
  164. foreach (['author', 'guid', 'link', 'thumbnail', 'timestamp', 'tags', 'title'] as $key) {
  165. if (!empty($rssItem[$key]) && is_string($rssItem[$key])) {
  166. $rssItem[$key] = Minz_Helper::htmlspecialchars_utf8($rssItem[$key]);
  167. }
  168. }
  169. $view->entries[] = FreshRSS_Entry::fromArray($rssItem);
  170. }
  171. }
  172. return $view->renderToString();
  173. }
  174. }