Merge pull request #488 from qligier/pr-extension

Rewrite of getExtension
This commit is contained in:
Tobias Reich 2016-03-17 09:17:37 +01:00
commit 144c9b7274
4 changed files with 15 additions and 10 deletions

View File

@ -278,7 +278,7 @@ final class Album {
if (!@is_readable($photo->url)) continue; if (!@is_readable($photo->url)) continue;
// Get extension of image // Get extension of image
$extension = getExtension($photo->url); $extension = getExtension($photo->url, true);
// Set title for photo // Set title for photo
$zipFileName = $zipTitle . '/' . $photo->title . $extension; $zipFileName = $zipTitle . '/' . $photo->title . $extension;

View File

@ -50,7 +50,7 @@ final class Import {
// This prevents us from downloading invalid photos. // This prevents us from downloading invalid photos.
// Verify extension // Verify extension
$extension = getExtension($url); $extension = getExtension($url, true);
if (!in_array(strtolower($extension), Photo::$validExtensions, true)) { if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
$error = true; $error = true;
Log::error(Database::get(), __METHOD__, __LINE__, 'Photo format not supported (' . $url . ')'); Log::error(Database::get(), __METHOD__, __LINE__, 'Photo format not supported (' . $url . ')');

View File

@ -124,7 +124,7 @@ final class Photo {
} }
// Verify extension // Verify extension
$extension = getExtension($file['name']); $extension = getExtension($file['name'], false);
if (!in_array(strtolower($extension), self::$validExtensions, true)) { if (!in_array(strtolower($extension), self::$validExtensions, true)) {
Log::error(Database::get(), __METHOD__, __LINE__, 'Photo format not supported'); Log::error(Database::get(), __METHOD__, __LINE__, 'Photo format not supported');
if ($returnOnError===true) return false; if ($returnOnError===true) return false;
@ -845,8 +845,8 @@ final class Photo {
} }
// Get extension // Get extension
$extension = getExtension($photo->url); $extension = getExtension($photo->url, true);
if ($extension===false) { if (empty($extension)) {
Log::error(Database::get(), __METHOD__, __LINE__, 'Invalid photo extension'); Log::error(Database::get(), __METHOD__, __LINE__, 'Invalid photo extension');
return false; return false;
} }

View File

@ -1,13 +1,18 @@
<?php <?php
function getExtension($filename) { # Returns the extension of the filename (path or URI) or an empty string
function getExtension($filename, $isURI = false) {
$extension = strpos($filename, '.') !== false # If $filename is an URI, get only the path component
? strrchr($filename, '.') if ($isURI)
: ''; $filename = parse_url($filename, PHP_URL_PATH);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if (!empty($extension))
$extension = '.' . $extension;
return $extension; return $extension;
} }
?> ?>