FeedsFetcher.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @file
  4. * Contains the FeedsFetcher and related classes.
  5. */
  6. /**
  7. * Base class for all fetcher results.
  8. */
  9. class FeedsFetcherResult extends FeedsResult {
  10. protected $raw;
  11. protected $file_path;
  12. /**
  13. * Constructor.
  14. */
  15. public function __construct($raw) {
  16. $this->raw = $raw;
  17. }
  18. /**
  19. * @return
  20. * The raw content from the source as a string.
  21. *
  22. * @throws Exception
  23. * Extending classes MAY throw an exception if a problem occurred.
  24. */
  25. public function getRaw() {
  26. return $this->sanitizeRaw($this->raw);
  27. }
  28. /**
  29. * Get a path to a temporary file containing the resource provided by the
  30. * fetcher.
  31. *
  32. * File will be deleted after DRUPAL_MAXIMUM_TEMP_FILE_AGE.
  33. *
  34. * @return
  35. * A path to a file containing the raw content as a source.
  36. *
  37. * @throws Exception
  38. * If an unexpected problem occurred.
  39. */
  40. public function getFilePath() {
  41. if (!isset($this->file_path)) {
  42. $destination = 'public://feeds';
  43. if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  44. throw new Exception(t('Feeds directory either cannot be created or is not writable.'));
  45. }
  46. $this->file_path = FALSE;
  47. if ($file = file_save_data($this->getRaw(), $destination . '/' . get_class($this) . REQUEST_TIME)) {
  48. $file->status = 0;
  49. file_save($file);
  50. $this->file_path = $file->uri;
  51. }
  52. else {
  53. throw new Exception(t('Cannot write content to %dest', array('%dest' => $destination)));
  54. }
  55. }
  56. return $this->sanitizeFile($this->file_path);
  57. }
  58. /**
  59. * Sanitize the raw content string. Currently supported sanitizations:
  60. *
  61. * - Remove BOM header from UTF-8 files.
  62. *
  63. * @param string $raw
  64. * The raw content string to be sanitized.
  65. * @return
  66. * The sanitized content as a string.
  67. */
  68. public function sanitizeRaw($raw) {
  69. if (substr($raw, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
  70. $raw = substr($raw, 3);
  71. }
  72. return $raw;
  73. }
  74. /**
  75. * Sanitize the file in place. Currently supported sanitizations:
  76. *
  77. * - Remove BOM header from UTF-8 files.
  78. *
  79. * @param string $filepath
  80. * The file path of the file to be sanitized.
  81. * @return
  82. * The file path of the sanitized file.
  83. */
  84. public function sanitizeFile($filepath) {
  85. $handle = fopen($filepath, 'r');
  86. $line = fgets($handle);
  87. fclose($handle);
  88. // If BOM header is present, read entire contents of file and overwrite
  89. // the file with corrected contents.
  90. if (substr($line, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
  91. $contents = file_get_contents($filepath);
  92. $contents = substr($contents, 3);
  93. $status = file_put_contents($filepath, $contents);
  94. if ($status === FALSE) {
  95. throw new Exception(t('File @filepath is not writeable.', array('@filepath' => $filepath)));
  96. }
  97. }
  98. return $filepath;
  99. }
  100. }
  101. /**
  102. * Abstract class, defines shared functionality between fetchers.
  103. *
  104. * Implements FeedsSourceInfoInterface to expose source forms to Feeds.
  105. */
  106. abstract class FeedsFetcher extends FeedsPlugin {
  107. /**
  108. * Fetch content from a source and return it.
  109. *
  110. * Every class that extends FeedsFetcher must implement this method.
  111. *
  112. * @param $source
  113. * Source value as entered by user through sourceForm().
  114. *
  115. * @return
  116. * A FeedsFetcherResult object.
  117. */
  118. public abstract function fetch(FeedsSource $source);
  119. /**
  120. * Clear all caches for results for given source.
  121. *
  122. * @param FeedsSource $source
  123. * Source information for this expiry. Implementers can choose to only clear
  124. * caches pertaining to this source.
  125. */
  126. public function clear(FeedsSource $source) {}
  127. /**
  128. * Request handler invoked if callback URL is requested. Locked down by
  129. * default. For a example usage see FeedsHTTPFetcher.
  130. *
  131. * Note: this method may exit the script.
  132. *
  133. * @return
  134. * A string to be returned to the client.
  135. */
  136. public function request($feed_nid = 0) {
  137. drupal_access_denied();
  138. }
  139. /**
  140. * Construct a path for a concrete fetcher/source combination. The result of
  141. * this method matches up with the general path definition in
  142. * FeedsFetcher::menuItem(). For example usage look at FeedsHTTPFetcher.
  143. *
  144. * @return
  145. * Path for this fetcher/source combination.
  146. */
  147. public function path($feed_nid = 0) {
  148. $id = urlencode($this->id);
  149. if ($feed_nid && is_numeric($feed_nid)) {
  150. return "feeds/importer/$id/$feed_nid";
  151. }
  152. return "feeds/importer/$id";
  153. }
  154. /**
  155. * Menu item definition for fetchers of this class. Note how the path
  156. * component in the item definition matches the return value of
  157. * FeedsFetcher::path();
  158. *
  159. * Requests to this menu item will be routed to FeedsFetcher::request().
  160. *
  161. * @return
  162. * An array where the key is the Drupal menu item path and the value is
  163. * a valid Drupal menu item definition.
  164. */
  165. public function menuItem() {
  166. return array(
  167. 'feeds/importer/%feeds_importer' => array(
  168. 'page callback' => 'feeds_fetcher_callback',
  169. 'page arguments' => array(2, 3),
  170. 'access callback' => TRUE,
  171. 'file' => 'feeds.pages.inc',
  172. 'type' => MENU_CALLBACK,
  173. ),
  174. );
  175. }
  176. /**
  177. * Subscribe to a source. Only implement if fetcher requires subscription.
  178. *
  179. * @param FeedsSource $source
  180. * Source information for this subscription.
  181. */
  182. public function subscribe(FeedsSource $source) {}
  183. /**
  184. * Unsubscribe from a source. Only implement if fetcher requires subscription.
  185. *
  186. * @param FeedsSource $source
  187. * Source information for unsubscribing.
  188. */
  189. public function unsubscribe(FeedsSource $source) {}
  190. /**
  191. * Override import period settings. This can be used to force a certain import
  192. * interval.
  193. *
  194. * @param $source
  195. * A FeedsSource object.
  196. *
  197. * @return
  198. * A time span in seconds if periodic import should be overridden for given
  199. * $source, NULL otherwise.
  200. */
  201. public function importPeriod(FeedsSource $source) {}
  202. }