dotpathUtil.php 7.5 KB

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