Skip to content
Snippets Groups Projects
feeds.module 35.9 KiB
Newer Older
 *   The node id of a feed node if the source is attached to a feed node.
 *
 * @return
 *   A FeedsSource object or an object of a class defiend by the Drupal
 *   variable 'source_class'.
 */
function feeds_source($importer_id, $feed_nid = 0) {
  return FeedsSource::instance($importer_id, $feed_nid);
 * Gets an instance of a class for a given plugin and id.
  if ($class = ctools_plugin_load_class('feeds', 'plugins', $plugin, 'handler')) {
    return FeedsPlugin::instance($class, $id, ctools_get_plugins('feeds', 'plugins', $plugin));
  $args = array('%plugin' => $plugin, '@id' => $id);
  if (user_access('administer feeds')) {
    $args['@link'] = url('admin/structure/feeds/' . $id);
    drupal_set_message(t('Missing Feeds plugin %plugin. See <a href="@link">@id</a>. Check whether all required libraries and modules are installed properly.', $args), 'warning', FALSE);
    drupal_set_message(t('Missing Feeds plugin %plugin. Please contact your site administrator.', $args), 'warning', FALSE);
  $class = ctools_plugin_load_class('feeds', 'plugins', 'FeedsMissingPlugin', 'handler');

  return FeedsPlugin::instance($class, $id);
 * Includes a library file.
 *
 * @param $file
 *   The filename to load from.
 * @param $library
 *   The name of the library. If libraries module is installed,
 *   feeds_include_library() will look for libraries with this name managed by
 *   libraries module.
 */
function feeds_include_library($file, $library) {
  static $included = array();
    $library_dir = variable_get('feeds_library_dir', FALSE);
    $feeds_library_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file";
    // Try first whether libraries module is present and load the file from
    // there. If this fails, require the library from the local path.
Alex Barth's avatar
Alex Barth committed
    if (module_exists('libraries') && file_exists(libraries_get_path($library) . "/$file")) {
      require libraries_get_path($library) . "/$file";
      $included[$file] = TRUE;
    elseif ($library_dir && file_exists("$library_dir/$library/$file")) {
      require "$library_dir/$library/$file";
      $included[$file] = TRUE;
    elseif (file_exists($feeds_library_path)) {
      // @todo: Throws "Deprecated function: Assigning the return value of new
      // by reference is deprecated."
      require $feeds_library_path;
      $included[$file] = TRUE;

/**
 * Checks whether a library is present.
 *
 * @param $file
 *   The filename to load from.
 * @param $library
 *   The name of the library. If libraries module is installed,
 *   feeds_library_exists() will look for libraries with this name managed by
 *   libraries module.
 */
function feeds_library_exists($file, $library) {
Alex Barth's avatar
Alex Barth committed
  if (module_exists('libraries') && file_exists(libraries_get_path($library) . "/$file")) {
Alex Barth's avatar
Alex Barth committed
  elseif (file_exists(DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds') . "/libraries/$file")) {

  elseif ($library_dir = variable_get('feeds_library_dir', FALSE)) {
    if (file_exists("$library_dir/$library/$file")) {
      return TRUE;
    }
  }

 /**
 * Checks whether simplepie exists.
 */
function feeds_simplepie_exists() {
  return (
    feeds_library_exists('autoloader.php', 'simplepie') ||
    feeds_library_exists('simplepie.compiled.php', 'simplepie') ||
    feeds_library_exists('simplepie.mini.php', 'simplepie') ||
    feeds_library_exists('simplepie.inc', 'simplepie')
  );
}

/**
 * Includes the simplepie library.
 */
function feeds_include_simplepie() {
  $files = array(
    'autoloader.php',
    'simplepie.mini.php',
    'simplepie.compiled.php',
    'simplepie.inc',
  );

  foreach ($files as $file) {
    if (feeds_include_library($file, 'simplepie')) {
      return TRUE;
    }
  }
/**
 * @deprecated
 *
 * Simplified drupal_alter().
 *
 * - None of that 'multiple parameters by ref' crazyness.
 * - Don't use module_implements() to allow hot including on behalf
 *   implementations (see mappers/).
 *
 * @todo This needs to be removed and drupal_alter() used. This is crazy dumb.
 */
function feeds_alter($type, &$data) {
  $args = array(&$data);
  $additional_args = func_get_args();
  array_shift($additional_args);
  array_shift($additional_args);
  $args = array_merge($args, $additional_args);

  $hook = $type . '_alter';
  foreach (module_list() as $module) {
    if (module_hook($module, $hook)) {
      call_user_func_array($module . '_' . $hook, $args);
    }
  }
}


/**
 * Copy of valid_url() that supports the webcal scheme.
 *
 * @see valid_url().
 *
 * @todo Replace with valid_url() when http://drupal.org/node/295021 is fixed.
 */
function feeds_valid_url($url, $absolute = FALSE) {
  if ($absolute) {
    return (bool) preg_match("
      /^                                                      # Start at the beginning of the text
      (?:ftp|https?|feed|webcal):\/\/                         # Look for ftp, http, https, feed or webcal schemes
      (?:                                                     # Userinfo (optional) which is typically
        (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*      # a username or a username and password
        (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@          # combination
      )?
      (?:
        (?:[a-z0-9\-\.]|%[0-9a-f]{2})+                        # A domain name or a IPv4 address
        |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\])         # or a well formed IPv6 address
      )
      (?::[0-9]+)?                                            # Server port number (optional)
      (?:[\/|\?]
        (?:[|\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})   # The path and query (optional)
      *)?
    $/xi", $url);
  }
  else {
    return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);
  }
}

/**
 * Registers a feed subscription job for execution on feeds_exit().
 *
 * @param array $job
 *   Information about a new job to queue; or if set to NULL (default), leaves
 *   the current queued jobs unchanged.
 *
 * @return
 *   An array of subscribe jobs to process.
 *
 * @see feeds_exit()
 * @see feeds_get_subscription_jobs()
 */
function feeds_set_subscription_job(array $job = NULL) {
  $jobs = &drupal_static(__FUNCTION__, array());
  if (isset($job)) {
    $jobs[] = $job;
  }
  return $jobs;
}

/**
 * Returns the list of queued jobs to be run.
 *
 * @return
 *   An array of subscribe jobs to process.
 *
 * @see feeds_set_subscription_job()
 */
function feeds_get_subscription_jobs() {
  return feeds_set_subscription_job();
}

/**
 * Implements hook_entity_property_info_alter().
 */
function feeds_entity_property_info_alter(&$info) {

  foreach ($info as $entity_type => $entity_info) {
    $info[$entity_type]['properties']['feed_nid'] = array(
      'label' => 'Feed NID',
      'type' => 'integer',
      'description' => t('Nid of the Feed Node that imported this entity.'),
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
    $info[$entity_type]['properties']['feed_node'] = array(
      'label' => 'Feed node',
      'type' => 'node',
      'description' => t('Feed Node that imported this entity.'),
      'getter callback' => 'feeds_get_feed_nid_entity_callback',
    );

/**
 * Gets the feed_nid for an entity for use in entity metadata.
 */
function feeds_get_feed_nid_entity_callback($entity, array $options, $name, $entity_type) {
  list($entity_id, , ) = entity_extract_ids($entity_type, $entity);

  $feed_nid = NULL;
  if ($entity_id) {
    $feed_nid = feeds_get_feed_nid($entity_id, $entity_type);
    if ($feed_nid === FALSE) {
      return NULL;
    }
  }
  // If the entity has no ID (yet) try read the feed nid from the object
  // directly.
  elseif (isset($entity->feeds_item->feed_nid)) {
    $feed_nid = $entity->feeds_item->feed_nid;

/**
 * Implements hook_file_download().
 */
function feeds_file_download($uri) {
  $id = db_query("SELECT id FROM {feeds_source} WHERE source = :uri", array(':uri' => $uri))->fetchField();

  if (!$id) {
    // File is not associated with a feed.
    return;
  }

   // Get the file record based on the URI. If not in the database just return.
  $files = file_load_multiple(array(), array('uri' => $uri));
  foreach ($files as $item) {
    // Since some database servers sometimes use a case-insensitive comparison
    // by default, double check that the filename is an exact match.
    if ($item->uri === $uri) {
      $file = $item;
      break;
    }
  }
  if (!isset($file)) {
    return;
  }

  // Check if this file belongs to Feeds.
  $usage_list = file_usage_list($file);
  if (!isset($usage_list['feeds'])) {
    return;
  }

  if (!feeds_access('import', $id)) {
    // User does not have permission to import this feed.
    return -1;
  }

  // Return file headers.
  return file_get_content_headers($file);
}

/**
 * Feeds API version.
 */
function feeds_api_version() {
  $version = feeds_ctools_plugin_api('feeds', 'plugins');
  return $version['version'];
}