[TASK] Re-work/simplify copyright header in PHP files - Part 1
[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 return $this->foundPath;
51 }
52
53 /**
54 * Check is preset is currently active on the system.
55 * Overwrites parent method to ignore im_path and im_path_lzw settings
56 *
57 * @return boolean TRUE if preset is active
58 */
59 public function isActive() {
60 $isActive = TRUE;
61 foreach ($this->configurationValues as $configurationKey => $configurationValue) {
62 if ($configurationKey !== 'GFX/im_path'
63 && $configurationKey !== 'GFX/im_path_lzw'
64 ) {
65 $currentValue = $this->configurationManager->getConfigurationValueByPath($configurationKey);
66 if ($currentValue !== $configurationValue) {
67 $isActive = FALSE;
68 break;
69 }
70 }
71 }
72 return $isActive;
73 }
74
75 /**
76 * Find out if GraphicsMagick is available
77 *
78 * @return boolean TRUE if GraphicsMagick executable is found in path
79 */
80 public function isAvailable() {
81 $searchPaths = $this->getSearchPaths();
82 return $this->findExecutableInPath($searchPaths);
83 }
84
85 /**
86 * Get configuration values to activate prefix
87 *
88 * @return array Configuration values needed to activate prefix
89 */
90 public function getConfigurationValues() {
91 $this->findExecutableInPath($this->getSearchPaths());
92 $configurationValues = $this->configurationValues;
93 $configurationValues['GFX/im_path'] = $this->getFoundPath();
94 $configurationValues['GFX/im_path_lzw'] = $this->getFoundPath();
95 return $configurationValues;
96 }
97
98 /**
99 * Find executable in path, wrapper for specific ImageMagick/GraphicsMagick find methods.
100 *
101 * @param array $searchPaths
102 * @return mixed
103 */
104 abstract protected function findExecutableInPath(array $searchPaths);
105
106 /**
107 * Get list of paths to search for image handling executables
108 *
109 * @return array List of pathes to search for
110 */
111 protected function getSearchPaths() {
112 $searchPaths = $this->defaultExecutableSearchPaths;
113
114 // Add configured im_path on top
115 $imPath = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'];
116 if (strlen($imPath) && !in_array($imPath, $searchPaths)) {
117 $path = $this->cleanUpPath($imPath);
118 array_unshift($searchPaths, $path);
119 }
120
121 // Add configured im_path_lzw on top
122 $imLzwSearchPath = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'];
123 if (strlen($imLzwSearchPath) && !in_array($imLzwSearchPath, $searchPaths)) {
124 $path = $this->cleanUpPath($imLzwSearchPath);
125 array_unshift($searchPaths, $path);
126 }
127
128 // Add additional search path from form if given
129 if (isset($this->postValues['additionalSearchPath'])
130 && strlen($this->postValues['additionalSearchPath'])
131 && !in_array($this->postValues['additionalSearchPath'], $searchPaths)
132 ) {
133 $path = $this->cleanUpPath($this->postValues['additionalSearchPath']);
134 array_unshift($searchPaths, $path);
135 }
136
137 return $searchPaths;
138 }
139
140 /**
141 * Consolidate between Windows and Unix and add trailing slash im missing
142 *
143 * @param string $path Given path
144 * @return string Cleaned up path
145 */
146 protected function cleanUpPath($path) {
147 $path = \TYPO3\CMS\Core\Utility\GeneralUtility::fixWindowsFilePath($path);
148 // Add trailing slash if missing
149 if (!preg_match('/[\\/]$/', $path)) {
150 $path .= '/';
151 }
152 return $path;
153 }
154 }