0cc72a3f0b5ef8ab9e055edd6a82e67bb1bfac5a
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Configuration / Image / AbstractImagePreset.php
1 <?php
2 namespace TYPO3\CMS\Install\Configuration\Image;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Install\Configuration;
28
29 /**
30 * Abstract class implements common image preset code
31 */
32 abstract class AbstractImagePreset extends Configuration\AbstractPreset {
33
34 /**
35 * @var array Default paths to search for executable, with trailing slash
36 */
37 protected $defaultExecutableSearchPaths = array(
38 '/usr/local/bin/',
39 '/opt/local/bin/',
40 '/usr/bin/',
41 '/usr/X11R6/bin/',
42 '/opt/bin/',
43 'C:/php/ImageMagick/',
44 'C:/php/GraphicsMagick/',
45 'C:/apache/ImageMagick/',
46 'C:/apache/GraphicsMagick/',
47 );
48
49 /**
50 * @var string Absolute path with found executable
51 */
52 protected $foundPath = '';
53
54 /**
55 * Path where executable was found
56 *
57 * @return string Fonud path
58 */
59 public function getFoundPath() {
60 return $this->foundPath;
61 }
62
63 /**
64 * Check is preset is currently active on the system.
65 * Overwrites parent method to ignore im_path and im_path_lzw settings
66 *
67 * @return boolean TRUE if preset is active
68 */
69 public function isActive() {
70 $isActive = TRUE;
71 foreach ($this->configurationValues as $configurationKey => $configurationValue) {
72 if ($configurationKey !== 'GFX/im_path'
73 && $configurationKey !== 'GFX/im_path_lzw'
74 ) {
75 $currentValue = $this->configurationManager->getConfigurationValueByPath($configurationKey);
76 if ($currentValue !== $configurationValue) {
77 $isActive = FALSE;
78 break;
79 }
80 }
81 }
82 return $isActive;
83 }
84
85 /**
86 * Find out if GraphicsMagick is available
87 *
88 * @return boolean TRUE if GraphicsMagick executable is found in path
89 */
90 public function isAvailable() {
91 $searchPaths = $this->getSearchPaths();
92 return $this->findExecutableInPath($searchPaths);
93 }
94
95 /**
96 * Get configuration values to activate prefix
97 *
98 * @return array Configuration values needed to activate prefix
99 */
100 public function getConfigurationValues() {
101 $this->findExecutableInPath($this->getSearchPaths());
102 $configurationValues = $this->configurationValues;
103 $configurationValues['GFX/im_path'] = $this->getFoundPath();
104 $configurationValues['GFX/im_path_lzw'] = $this->getFoundPath();
105 return $configurationValues;
106 }
107
108 /**
109 * Find executable in path, wrapper for specific ImageMagick/GraphicsMagick find methods.
110 *
111 * @param array $searchPaths
112 * @return mixed
113 */
114 abstract protected function findExecutableInPath(array $searchPaths);
115
116 /**
117 * Get list of paths to search for image handling executables
118 *
119 * @return array List of pathes to search for
120 */
121 protected function getSearchPaths() {
122 $searchPaths = $this->defaultExecutableSearchPaths;
123
124 // Add configured im_path on top
125 $imPath = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'];
126 if (strlen($imPath) && !in_array($imPath, $searchPaths)) {
127 $path = $this->cleanUpPath($imPath);
128 array_unshift($searchPaths, $path);
129 }
130
131 // Add configured im_path_lzw on top
132 $imLzwSearchPath = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'];
133 if (strlen($imLzwSearchPath) && !in_array($imLzwSearchPath, $searchPaths)) {
134 $path = $this->cleanUpPath($imLzwSearchPath);
135 array_unshift($searchPaths, $path);
136 }
137
138 // Add additional search path from form if given
139 if (isset($this->postValues['additionalSearchPath'])
140 && strlen($this->postValues['additionalSearchPath'])
141 && !in_array($this->postValues['additionalSearchPath'], $searchPaths)
142 ) {
143 $path = $this->cleanUpPath($this->postValues['additionalSearchPath']);
144 array_unshift($searchPaths, $path);
145 }
146
147 return $searchPaths;
148 }
149
150 /**
151 * Consolidate between Windows and Unix and add trailing slash im missing
152 *
153 * @param string $path Given path
154 * @return string Cleaned up path
155 */
156 protected function cleanUpPath($path) {
157 $path = \TYPO3\CMS\Core\Utility\GeneralUtility::fixWindowsFilePath($path);
158 // Add trailing slash if missing
159 if (!preg_match('/[\\/]$/', $path)) {
160 $path .= '/';
161 }
162 return $path;
163 }
164 }