You are here

class FeedsEnclosure in Feeds 6

Same name and namespace in other branches
  1. 7.2 plugins/FeedsParser.inc \FeedsEnclosure
  2. 7 plugins/FeedsParser.inc \FeedsEnclosure

Enclosure element, can be part of the result array.

Hierarchy

Expanded class hierarchy of FeedsEnclosure

File

plugins/FeedsParser.inc, line 206

View source
class FeedsEnclosure extends FeedsElement {
  protected $mime_type;
  protected $file;

  /**
   * Delete flag, denoting whether a file should be deleted when this object
   * is destroyed.
   *
   * @see __destruct()
   * @see getFile()
   */
  protected $delete_file;

  /**
   * Constructor, requires MIME type.
   */
  public function __construct($value, $mime_type) {
    parent::__construct($value);
    $this->mime_type = $mime_type;
    $this->delete_file = FALSE;
  }

  /**
   * Destructor, clean up any temporary files.
   */
  public function __destruct() {
    if (!empty($this->file) && $this->delete_file) {
      file_delete($this->file);
    }
  }

  /**
   * @return
   *   MIME type of return value of getValue().
   */
  public function getMIMEType() {
    return $this->mime_type;
  }

  /**
   * @return
   *   The content of the referenced resource.
   */
  public function getContent() {
    feeds_include_library('http_request.inc', 'http_request');
    $result = http_request_get($this
      ->getValue());
    if ($result->code != 200) {
      throw new Exception(t('Download of @url failed with code !code.', array(
        '@url' => $this
          ->getValue(),
        '!code' => $result->code,
      )));
    }
    return $result->data;
  }

  /**
   * @return
   *   A path pointing to a file containing the resource referenced by the
   *   enclosure. This method downloads the resource if it is not local. The
   *   file path must be considered temporary and is only valid for the current
   *   page load.
   *
   * @todo Get file extension from mime_type.
   * @todo This is not concurrency safe.
   */
  public function getFile() {
    if (empty($this->file) && $this
      ->getValue()) {

      // Check if this enclosure contains a local file.
      if (!parse_url($this
        ->getValue(), PHP_URL_SCHEME)) {
        if (file_check_location($this
          ->getValue(), file_directory_path()) || file_check_location($this
          ->getValue(), file_directory_temp())) {
          if (file_exists($this
            ->getValue())) {
            $this->file = $this
              ->getValue();
            return $this->file;
          }
        }
        throw new Exception(t('Invalid enclosure %enclosure', array(
          '%enclosure' => $this
            ->getValue(),
        )));
      }
      $filename = basename($this
        ->getValue());
      if (module_exists('transliteration')) {
        require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
        $filename = transliteration_clean_filename($filename);
      }
      $dest = file_destination(file_directory_temp() . '/' . $filename, FILE_EXISTS_RENAME);
      if (ini_get('allow_url_fopen')) {
        $this->file = copy($this
          ->getValue(), $dest) ? $dest : 0;
      }
      else {
        $this->file = file_save_data($this
          ->getContent(), $dest);
      }
      if ($this->file === 0) {
        throw new Exception(t('Cannot write content to %dest', array(
          '%dest' => $dest,
        )));
      }
      $this->delete_file = TRUE;
    }
    return $this->file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsElement::$value protected property
FeedsElement::getValue public function @todo Make value public and deprecate use of getValue(). 3
FeedsElement::__toString public function 1
FeedsEnclosure::$delete_file protected property Delete flag, denoting whether a file should be deleted when this object is destroyed.
FeedsEnclosure::$file protected property
FeedsEnclosure::$mime_type protected property
FeedsEnclosure::getContent public function
FeedsEnclosure::getFile public function @todo Get file extension from mime_type. @todo This is not concurrency safe.
FeedsEnclosure::getMIMEType public function 1
FeedsEnclosure::__construct public function Constructor, requires MIME type. Overrides FeedsElement::__construct 1
FeedsEnclosure::__destruct public function Destructor, clean up any temporary files.