[!!!][TASK] Rename GFX settings referring to IM in their name
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Configuration / Image / AbstractImagePreset.php
1 <?php
2 namespace TYPO3\CMS\Install\Configuration\Image;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Install\Configuration;
18
19 /**
20 * Abstract class implements common image preset code
21 */
22 abstract class AbstractImagePreset extends Configuration\AbstractPreset
23 {
24 /**
25 * @var array Default paths to search for executable, with trailing slash
26 */
27 protected $defaultExecutableSearchPaths = array(
28 '/usr/local/bin/',
29 '/opt/local/bin/',
30 '/usr/bin/',
31 '/usr/X11R6/bin/',
32 '/opt/bin/',
33 'C:/php/ImageMagick/',
34 'C:/php/GraphicsMagick/',
35 'C:/apache/ImageMagick/',
36 'C:/apache/GraphicsMagick/',
37 );
38
39 /**
40 * @var string Absolute path with found executable
41 */
42 protected $foundPath = '';
43
44 /**
45 * Path where executable was found
46 *
47 * @return string Fonud path
48 */
49 public function getFoundPath()
50 {
51 return $this->foundPath;
52 }
53
54 /**
55 * Check is preset is currently active on the system.
56 * Overwrites parent method to ignore processor_path and processor_path_lzw settings
57 *
58 * @return bool TRUE if preset is active
59 */
60 public function isActive()
61 {
62 $isActive = true;
63 foreach ($this->configurationValues as $configurationKey => $configurationValue) {
64 if ($configurationKey !== 'GFX/processor_path'
65 && $configurationKey !== 'GFX/processor_path_lzw'
66 ) {
67 $currentValue = $this->configurationManager->getConfigurationValueByPath($configurationKey);
68 if ($currentValue !== $configurationValue) {
69 $isActive = false;
70 break;
71 }
72 }
73 }
74 return $isActive;
75 }
76
77 /**
78 * Find out if GraphicsMagick is available
79 *
80 * @return bool TRUE if GraphicsMagick executable is found in path
81 */
82 public function isAvailable()
83 {
84 $searchPaths = $this->getSearchPaths();
85 return $this->findExecutableInPath($searchPaths);
86 }
87
88 /**
89 * Get configuration values to activate prefix
90 *
91 * @return array Configuration values needed to activate prefix
92 */
93 public function getConfigurationValues()
94 {
95 $this->findExecutableInPath($this->getSearchPaths());
96 $configurationValues = $this->configurationValues;
97 $configurationValues['GFX/processor_path'] = $this->getFoundPath();
98 $configurationValues['GFX/processor_path_lzw'] = $this->getFoundPath();
99 return $configurationValues;
100 }
101
102 /**
103 * Find executable in path, wrapper for specific ImageMagick/GraphicsMagick find methods.
104 *
105 * @param array $searchPaths
106 * @return mixed
107 */
108 abstract protected function findExecutableInPath(array $searchPaths);
109
110 /**
111 * Get list of paths to search for image handling executables
112 *
113 * @return array List of pathes to search for
114 */
115 protected function getSearchPaths()
116 {
117 $searchPaths = $this->defaultExecutableSearchPaths;
118
119 // Add configured processor_path on top
120 $imPath = $GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_path'];
121 if ((string)$imPath !== '' && !in_array($imPath, $searchPaths)) {
122 $path = $this->cleanUpPath($imPath);
123 array_unshift($searchPaths, $path);
124 }
125
126 // Add configured processor_path_lzw on top
127 $imLzwSearchPath = $GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_path_lzw'];
128 if ((string)$imLzwSearchPath !== '' && !in_array($imLzwSearchPath, $searchPaths)) {
129 $path = $this->cleanUpPath($imLzwSearchPath);
130 array_unshift($searchPaths, $path);
131 }
132
133 // Add additional search path from form if given
134 if (isset($this->postValues['additionalSearchPath'])
135 && (string)$this->postValues['additionalSearchPath'] !== ''
136 && !in_array($this->postValues['additionalSearchPath'], $searchPaths)
137 ) {
138 $path = $this->cleanUpPath($this->postValues['additionalSearchPath']);
139 array_unshift($searchPaths, $path);
140 }
141
142 return $searchPaths;
143 }
144
145 /**
146 * Consolidate between Windows and Unix and add trailing slash im missing
147 *
148 * @param string $path Given path
149 * @return string Cleaned up path
150 */
151 protected function cleanUpPath($path)
152 {
153 $path = \TYPO3\CMS\Core\Utility\GeneralUtility::fixWindowsFilePath($path);
154 // Add trailing slash if missing
155 if (!preg_match('/[\\/]$/', $path)) {
156 $path .= '/';
157 }
158 return $path;
159 }
160 }