Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.67% covered (success)
96.67%
29 / 30
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LocalFile
96.67% covered (success)
96.67%
29 / 30
75.00% covered (warning)
75.00%
3 / 4
15
0.00% covered (danger)
0.00%
0 / 1
 supports
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUrl
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 replaceEnvironmentSizeAndIdTokens
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 replaceImageTypeTokens
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * LocalFile cover content loader.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Content
25 * @author   Leila Gonzales <lmg@agiweb.org>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\Content\Covers;
31
32use function in_array;
33use function is_string;
34
35/**
36 * Local file cover content loader.
37 *
38 * @category VuFind
39 * @package  Content
40 * @author   Leila Gonzales <lmg@agiweb.org>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development Wiki
43 */
44class LocalFile extends \VuFind\Content\AbstractCover
45{
46    /**
47     * Image file extensions to look for when using %anyimage% token.
48     *
49     * @var array
50     */
51    protected $imageExtensions = ['gif', 'jpg', 'jpeg', 'png', 'tif', 'tiff'];
52
53    /**
54     * Image sizes to look for when using %size% token.
55     *
56     * @var array
57     */
58    protected $imageSizes = ['small', 'medium', 'large'];
59
60    /**
61     * MIME types allowed to be loaded from disk.
62     *
63     * @var array
64     */
65    protected $allowedMimeTypes = [
66        'image/gif', 'image/jpeg', 'image/png', 'image/tiff',
67    ];
68
69    /**
70     * Does this plugin support the provided ID array?
71     *
72     * @param array $ids IDs that will later be sent to load() -- see below.
73     *
74     * @return bool
75     */
76    public function supports($ids)
77    {
78        // We won't know what we need until we parse the path string; accept
79        // everything at this stage:
80        return true;
81    }
82
83    /**
84     * Get image location from local file storage.
85     *
86     * @param string $key  local file directory path
87     * @param string $size Size of image to load (small/medium/large)
88     * @param array  $ids  Associative array of identifiers (keys may include 'isbn'
89     * pointing to an ISBN object and 'issn' pointing to a string)
90     *
91     * @return string|bool
92     */
93    public function getUrl($key, $size, $ids)
94    {
95        // convert all of the tokens:
96        $fileName = $this->replaceImageTypeTokens(
97            $this->replaceEnvironmentSizeAndIdTokens($key, $ids, $size)
98        );
99        // Validate MIME type if we have a valid file path.
100        if ($fileName && file_exists($fileName)) {
101            if (in_array(mime_content_type($fileName), $this->allowedMimeTypes)) {
102                return 'file://' . $fileName;
103            }
104        }
105        // If we got this far, we couldn't find a match.
106        return false;
107    }
108
109    /**
110     * Convert tokens to appropriate values from environment, size parameter and ID array values.
111     *
112     * @param string $filePath file path of image file
113     * @param array  $ids      Associative array of identifiers
114     * (keys may include 'isbn' pointing to an ISBN object and
115     * 'issn' pointing to a string)
116     * @param string $size     size of image (small/medium/large)
117     *
118     * @return string
119     */
120    protected function replaceEnvironmentSizeAndIdTokens(string $filePath, array $ids, string $size): string
121    {
122        // Seed the token array with standard environment variables:
123        $tokens = ['%vufind-home%', '%vufind-local-dir%'];
124        $replacements = [APPLICATION_PATH, LOCAL_OVERRIDE_DIR];
125
126        // Only create tokens for strings, not objects:
127        foreach ($ids as $key => $val) {
128            if (is_string($val)) {
129                $tokens[] = '%' . $key . '%';
130                $replacements[] = $val;
131            }
132        }
133
134        // Special-case handling for ISBN object:
135        if (isset($ids['isbn'])) {
136            $tokens[] = '%isbn10%';
137            $replacements[] = $ids['isbn']->get10();
138
139            $tokens[] = '%isbn13%';
140            $replacements[] = $ids['isbn']->get13();
141        }
142
143        // Size handling:
144        if (in_array($size, $this->imageSizes)) {
145            $tokens[] = '%size%';
146            $replacements[] = $size;
147        }
148
149        return str_replace($tokens, $replacements, $filePath);
150    }
151
152    /**
153     * Convert tokens to image type file extension.
154     *
155     * @param string $fileName file path of image file
156     *
157     * @return string
158     */
159    protected function replaceImageTypeTokens(string $fileName): string
160    {
161        // If anyimage is specified, then we loop through all
162        // image extensions to find the right filename
163        if (str_contains($fileName, '%anyimage%')) {
164            foreach ($this->imageExtensions as $val) {
165                foreach ([$val, strtoupper($val), ucwords($val)] as $finalVal) {
166                    $checkFile = str_replace('%anyimage%', $finalVal, $fileName);
167                    if (file_exists($checkFile)) {
168                        return $checkFile;
169                    }
170                }
171            }
172        }
173        // Default behavior: do not modify filename:
174        return $fileName;
175    }
176}