AbstractImagePreset.php 4.46 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Install\Configuration\Image;

18
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Configuration\AbstractPreset;
20
21
22

/**
 * Abstract class implements common image preset code
23
 * @internal only to be used within EXT:install
24
 */
25
abstract class AbstractImagePreset extends AbstractPreset
26
27
28
29
{
    /**
     * @var array Default paths to search for executable, with trailing slash
     */
30
    protected $defaultExecutableSearchPaths = [
31
32
33
34
35
36
37
38
39
        '/usr/local/bin/',
        '/opt/local/bin/',
        '/usr/bin/',
        '/usr/X11R6/bin/',
        '/opt/bin/',
        'C:/php/ImageMagick/',
        'C:/php/GraphicsMagick/',
        'C:/apache/ImageMagick/',
        'C:/apache/GraphicsMagick/',
40
    ];
41
42
43
44
45
46
47
48
49

    /**
     * @var string Absolute path with found executable
     */
    protected $foundPath = '';

    /**
     * Path where executable was found
     *
50
     * @return string Found path
51
52
53
54
55
56
57
58
     */
    public function getFoundPath()
    {
        return $this->foundPath;
    }

    /**
     * Check is preset is currently active on the system.
59
     * Overwrites parent method to ignore processor_path setting
60
61
62
63
64
65
66
     *
     * @return bool TRUE if preset is active
     */
    public function isActive()
    {
        $isActive = true;
        foreach ($this->configurationValues as $configurationKey => $configurationValue) {
67
            if ($configurationKey !== 'GFX/processor_path') {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
                $currentValue = $this->configurationManager->getConfigurationValueByPath($configurationKey);
                if ($currentValue !== $configurationValue) {
                    $isActive = false;
                    break;
                }
            }
        }
        return $isActive;
    }

    /**
     * Find out if GraphicsMagick is available
     *
     * @return bool TRUE if GraphicsMagick executable is found in path
     */
    public function isAvailable()
    {
        $searchPaths = $this->getSearchPaths();
        return $this->findExecutableInPath($searchPaths);
    }

    /**
     * Get configuration values to activate prefix
     *
     * @return array Configuration values needed to activate prefix
     */
    public function getConfigurationValues()
    {
        $this->findExecutableInPath($this->getSearchPaths());
        $configurationValues = $this->configurationValues;
98
        $configurationValues['GFX/processor_path'] = $this->getFoundPath();
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        return $configurationValues;
    }

    /**
     * Find executable in path, wrapper for specific ImageMagick/GraphicsMagick find methods.
     *
     * @param array $searchPaths
     * @return mixed
     */
    abstract protected function findExecutableInPath(array $searchPaths);

    /**
     * Get list of paths to search for image handling executables
     *
113
     * @return array List of paths to search for
114
115
116
117
118
     */
    protected function getSearchPaths()
    {
        $searchPaths = $this->defaultExecutableSearchPaths;

119
120
        // Add configured processor_path on top
        $imPath = $GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_path'];
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        if ((string)$imPath !== '' && !in_array($imPath, $searchPaths)) {
            $path = $this->cleanUpPath($imPath);
            array_unshift($searchPaths, $path);
        }

        // Add additional search path from form if given
        if (isset($this->postValues['additionalSearchPath'])
            && (string)$this->postValues['additionalSearchPath'] !== ''
            && !in_array($this->postValues['additionalSearchPath'], $searchPaths)
        ) {
            $path = $this->cleanUpPath($this->postValues['additionalSearchPath']);
            array_unshift($searchPaths, $path);
        }

        return $searchPaths;
    }

    /**
     * Consolidate between Windows and Unix and add trailing slash im missing
     *
     * @param string $path Given path
     * @return string Cleaned up path
     */
    protected function cleanUpPath($path)
    {
146
        $path = GeneralUtility::fixWindowsFilePath($path);
147
148
149
150
151
152
        // Add trailing slash if missing
        if (!preg_match('/[\\/]$/', $path)) {
            $path .= '/';
        }
        return $path;
    }
153
}