Project

General

Profile

Revision 2c8c2b87

Added by Assos Assos about 9 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/feeds/plugins/FeedsParser.inc
264 264
 * Enclosure element, can be part of the result array.
265 265
 */
266 266
class FeedsEnclosure extends FeedsElement {
267
  protected $mime_type;
267

  
268
  /**
269
   * The mime type of the enclosure.
270
   *
271
   * @param string
272
   */
273
   protected $mime_type;
274

  
275
   /**
276
   * The default list of allowed extensions.
277
   *
278
   * @param string
279
   */
280
  protected $allowedExtensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
281

  
282
  /**
283
   * The sanitized local file name.
284
   *
285
   * @var string
286
   */
287
  protected $safeFilename;
268 288

  
269 289
  /**
270 290
   * Constructor, requires MIME type.
......
287 307
    return $this->mime_type;
288 308
  }
289 309

  
310
  /**
311
   * Sets the list of allowed extensions.
312
   *
313
   * @param string $extensions
314
   *   The list of allowed extensions separated by a space.
315
   */
316
  public function setAllowedExtensions($extensions) {
317
    // Normalize whitespace so that empty extensions are not allowed.
318
    $this->allowedExtensions = trim(preg_replace('/\s/', ' ', $extensions));
319
  }
320

  
290 321
  /**
291 322
   * Use this method instead of FeedsElement::getValue() when fetching the file
292 323
   * from the URL.
......
301 332
  }
302 333

  
303 334
  /**
304
   * Use this method instead of FeedsElement::getValue() to get the file name
305
   * transformed for better local saving (underscores instead of spaces)
335
   * Returns the full path to the file URI with a safe filen ame.
306 336
   *
307
   * @return
337
   * @return string
338
   *   The safe file URI.
339
   *
340
   * @throws RuntimeException
341
   *   Thrown if the file extension is invalid.
342
   */
343
  public function getSanitizedUri() {
344
    return drupal_dirname($this->getValue()) . '/' . $this->getSafeFilename();
345
  }
346

  
347
  /**
348
   * Returns the file name transformed for better local saving.
349
   *
350
   * @return string
308 351
   *   Value with space characters changed to underscores.
309 352
   *
353
   * @throws RuntimeException
354
   *   Thrown if the file extension is invalid.
355
   *
310 356
   * @see FeedsElement::getValue()
311 357
   */
312 358
  public function getLocalValue() {
313
    return str_replace(' ', '_', $this->getValue());
359
    return str_replace(' ', '_', $this->getSafeFilename());
314 360
  }
315 361

  
316 362
  /**
317
   * @return
363
   * Returns the safe file name.
364
   *
365
   * @return string
366
   *   A filename that is safe to save to the filesystem.
367
   *
368
   * @throws RuntimeException
369
   *   Thrown if the file extension is invalid.
370
   *
371
   * @see FeedsElement::getValue()
372
   */
373
  protected function getSafeFilename() {
374
    if (isset($this->safeFilename)) {
375
      return $this->safeFilename;
376
    }
377

  
378
    $filename = rawurldecode(drupal_basename($this->getValue()));
379

  
380
    if (module_exists('transliteration')) {
381
      require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
382
      $filename = transliteration_clean_filename($filename);
383
    }
384

  
385
    // Remove leading and trailing whitespace and periods.
386
    $filename = trim($filename, " \t\n\r\0\x0B.");
387

  
388
    if (strpos($filename, '.') === FALSE) {
389
      $extension = FALSE;
390
    }
391
    else {
392
      $extension = substr($filename, strrpos($filename, '.') + 1);
393
    }
394

  
395
    if (!$extension || !in_array($extension, explode(' ', $this->allowedExtensions), TRUE)) {
396
      throw new RuntimeException(t('The file @file has an invalid extension.', array('@file' => $filename)));
397
    }
398

  
399
    $this->safeFilename = file_munge_filename($filename, $this->allowedExtensions, FALSE);
400

  
401
    return $this->safeFilename;
402
  }
403

  
404
  /**
405
   * Downloads the content from the file URL.
406
   *
407
   * @return string
318 408
   *   The content of the referenced resource.
319 409
   */
320 410
  public function getContent() {
......
345 435
      // Prepare destination directory.
346 436
      file_prepare_directory($destination, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
347 437
      // Copy or save file depending on whether it is remote or local.
348
      if (drupal_realpath($this->getValue())) {
438
      if (drupal_realpath($this->getSanitizedUri())) {
349 439
        $file           = new stdClass();
350 440
        $file->uid      = 0;
351
        $file->uri      = $this->getValue();
441
        $file->uri      = $this->getSanitizedUri();
352 442
        $file->filemime = $this->mime_type;
353
        $file->filename = basename($file->uri);
354
        if (dirname($file->uri) != $destination) {
443
        $file->filename = $this->getSafeFilename();
444

  
445
        if (drupal_dirname($file->uri) !== $destination) {
355 446
          $file = file_copy($file, $destination);
356 447
        }
357 448
        else {
......
368 459
        }
369 460
      }
370 461
      else {
371
        $filename = basename($this->getLocalValue());
372
        if (module_exists('transliteration')) {
373
          require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc';
374
          $filename = transliteration_clean_filename($filename);
375
        }
376 462
        if (file_uri_target($destination)) {
377 463
          $destination = trim($destination, '/') . '/';
378 464
        }
379 465
        try {
380
          $file = file_save_data($this->getContent(), $destination . $filename);
466
          $file = file_save_data($this->getContent(), $destination . $this->getLocalValue());
381 467
        }
382 468
        catch (Exception $e) {
383 469
          watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
......
388 474
      if (!$file) {
389 475
        throw new Exception(t('Invalid enclosure %enclosure', array('%enclosure' => $this->getValue())));
390 476
      }
477

  
478
      return $file;
391 479
    }
392
    return $file;
393 480
  }
481

  
394 482
}
395 483

  
396 484
/**

Also available in: Unified diff