FeedsFetcher.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * Implements FeedsPlugin::pluginType().
  109. */
  110. public function pluginType() {
  111. return 'fetcher';
  112. }
  113. /**
  114. * Fetch content from a source and return it.
  115. *
  116. * Every class that extends FeedsFetcher must implement this method.
  117. *
  118. * @param $source
  119. * Source value as entered by user through sourceForm().
  120. *
  121. * @return
  122. * A FeedsFetcherResult object.
  123. */
  124. public abstract function fetch(FeedsSource $source);
  125. /**
  126. * Clear all caches for results for given source.
  127. *
  128. * @param FeedsSource $source
  129. * Source information for this expiry. Implementers can choose to only clear
  130. * caches pertaining to this source.
  131. */
  132. public function clear(FeedsSource $source) {}
  133. /**
  134. * Request handler invoked if callback URL is requested. Locked down by
  135. * default. For a example usage see FeedsHTTPFetcher.
  136. *
  137. * Note: this method may exit the script.
  138. *
  139. * @return
  140. * A string to be returned to the client.
  141. */
  142. public function request($feed_nid = 0) {
  143. drupal_access_denied();
  144. }
  145. /**
  146. * Construct a path for a concrete fetcher/source combination. The result of
  147. * this method matches up with the general path definition in
  148. * FeedsFetcher::menuItem(). For example usage look at FeedsHTTPFetcher.
  149. *
  150. * @return
  151. * Path for this fetcher/source combination.
  152. */
  153. public function path($feed_nid = 0) {
  154. $id = urlencode($this->id);
  155. if ($feed_nid && is_numeric($feed_nid)) {
  156. return "feeds/importer/$id/$feed_nid";
  157. }
  158. return "feeds/importer/$id";
  159. }
  160. /**
  161. * Menu item definition for fetchers of this class. Note how the path
  162. * component in the item definition matches the return value of
  163. * FeedsFetcher::path();
  164. *
  165. * Requests to this menu item will be routed to FeedsFetcher::request().
  166. *
  167. * @return
  168. * An array where the key is the Drupal menu item path and the value is
  169. * a valid Drupal menu item definition.
  170. */
  171. public function menuItem() {
  172. return array(
  173. 'feeds/importer/%feeds_importer' => array(
  174. 'page callback' => 'feeds_fetcher_callback',
  175. 'page arguments' => array(2, 3),
  176. 'access callback' => TRUE,
  177. 'file' => 'feeds.pages.inc',
  178. 'type' => MENU_CALLBACK,
  179. ),
  180. );
  181. }
  182. /**
  183. * Subscribe to a source. Only implement if fetcher requires subscription.
  184. *
  185. * @param FeedsSource $source
  186. * Source information for this subscription.
  187. */
  188. public function subscribe(FeedsSource $source) {}
  189. /**
  190. * Unsubscribe from a source. Only implement if fetcher requires subscription.
  191. *
  192. * @param FeedsSource $source
  193. * Source information for unsubscribing.
  194. */
  195. public function unsubscribe(FeedsSource $source) {}
  196. /**
  197. * Override import period settings. This can be used to force a certain import
  198. * interval.
  199. *
  200. * @param $source
  201. * A FeedsSource object.
  202. *
  203. * @return
  204. * A time span in seconds if periodic import should be overridden for given
  205. * $source, NULL otherwise.
  206. */
  207. public function importPeriod(FeedsSource $source) {}
  208. }