Passed
Push — master ( 0208b2...1155ca )
by Robbie
09:03
created

InstallEnvironmentAware::isIIS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Dev\Install;
4
5
use BadMethodCallException;
6
use SilverStripe\Core\Path;
7
8
/**
9
 * For classes which are aware of install, project, and environment state.
10
 *
11
 * These should be basic getters / setters that infer from current state.
12
 */
13
trait InstallEnvironmentAware
14
{
15
    /**
16
     * Base path
17
     * @var
18
     */
19
    protected $baseDir;
20
21
    /**
22
     * Init base path, or guess if able
23
     *
24
     * @param string|null $basePath
25
     */
26
    protected function initBaseDir($basePath)
27
    {
28
        if ($basePath) {
29
            $this->setBaseDir($basePath);
30
        } elseif (defined('BASE_PATH')) {
31
            $this->setBaseDir(BASE_PATH);
32
        } else {
33
            throw new BadMethodCallException("No BASE_PATH defined");
34
        }
35
    }
36
37
    /**
38
     * @param string $base
39
     * @return $this
40
     */
41
    protected function setBaseDir($base)
42
    {
43
        $this->baseDir = $base;
44
        return $this;
45
    }
46
47
    /**
48
     * Get base path for this installation
49
     *
50
     * @return string
51
     */
52
    public function getBaseDir()
53
    {
54
        return Path::normalise($this->baseDir) . DIRECTORY_SEPARATOR;
55
    }
56
57
    /**
58
     * Get path to public directory
59
     *
60
     * @return string
61
     */
62
    public function getPublicDir()
63
    {
64
        $base = $this->getBaseDir();
65
        $public = Path::join($base, 'public') . DIRECTORY_SEPARATOR;
66
        if (file_exists($public)) {
67
            return $public;
68
        }
69
        return $base;
70
    }
71
72
    /**
73
     * Check that a module exists
74
     *
75
     * @param string $dirname
76
     * @return bool
77
     */
78
    public function checkModuleExists($dirname)
79
    {
80
        // Mysite is base-only and doesn't need _config.php to be counted
81
        if (in_array($dirname, ['mysite', 'app'])) {
82
            return file_exists($this->getBaseDir() . $dirname);
83
        }
84
85
        $paths = [
86
            "vendor/silverstripe/{$dirname}/",
87
            "{$dirname}/",
88
        ];
89
        foreach ($paths as $path) {
90
            $checks = ['_config', '_config.php'];
91
            foreach ($checks as $check) {
92
                if (file_exists($this->getBaseDir() . $path . $check)) {
93
                    return true;
94
                }
95
            }
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * Get project dir name.
103
     *
104
     * @return string 'app', or 'mysite' (deprecated)
105
     */
106
    protected function getProjectDir()
107
    {
108
        $base = $this->getBaseDir();
109
        if (is_dir($base . 'mysite')) {
110
            /** @deprecated 4.2..5.0 */
111
            return 'mysite';
112
        }
113
114
        // Default
115
        return 'app';
116
    }
117
118
    /**
119
     * Get src dir name for project
120
     *
121
     * @return string
122
     */
123
    protected function getProjectSrcDir()
124
    {
125
        $projectDir = $this->getProjectDir();
126
        if ($projectDir === 'mysite') {
127
            /** @deprecated 4.2..5.0 */
128
            return $projectDir . DIRECTORY_SEPARATOR . 'code';
129
        }
130
131
        // Default
132
        return $projectDir . DIRECTORY_SEPARATOR . 'src';
133
    }
134
135
    /**
136
     * Check if the web server is IIS and version greater than the given version.
137
     *
138
     * @param int $fromVersion
139
     * @return bool
140
     */
141
    public function isIIS($fromVersion = 7)
142
    {
143
        $webserver = $this->findWebserver();
144
        if (preg_match('#.*IIS/(?<version>[.\\d]+)$#', $webserver, $matches)) {
0 ignored issues
show
Bug introduced by
It seems like $webserver can also be of type false; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
        if (preg_match('#.*IIS/(?<version>[.\\d]+)$#', /** @scrutinizer ignore-type */ $webserver, $matches)) {
Loading history...
145
            return version_compare($matches['version'], $fromVersion, '>=');
146
        }
147
        return false;
148
    }
149
150
    /**
151
     * @return bool
152
     */
153
    public function isApache()
154
    {
155
        return strpos($this->findWebserver(), 'Apache') !== false;
0 ignored issues
show
Bug introduced by
It seems like $this->findWebserver() can also be of type false; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

155
        return strpos(/** @scrutinizer ignore-type */ $this->findWebserver(), 'Apache') !== false;
Loading history...
156
    }
157
158
    /**
159
     * Find the webserver software running on the PHP host.
160
     *
161
     * @return string|false Server software or boolean FALSE
162
     */
163
    public function findWebserver()
164
    {
165
        // Try finding from SERVER_SIGNATURE or SERVER_SOFTWARE
166
        if (!empty($_SERVER['SERVER_SIGNATURE'])) {
167
            $webserver = $_SERVER['SERVER_SIGNATURE'];
168
        } elseif (!empty($_SERVER['SERVER_SOFTWARE'])) {
169
            $webserver = $_SERVER['SERVER_SOFTWARE'];
170
        } else {
171
            return false;
172
        }
173
174
        return strip_tags(trim($webserver));
175
    }
176
177
    public function testApacheRewriteExists($moduleName = 'mod_rewrite')
178
    {
179
        if (function_exists('apache_get_modules') && in_array($moduleName, apache_get_modules())) {
180
            return true;
181
        }
182
        if (isset($_SERVER['HTTP_MOD_REWRITE']) && $_SERVER['HTTP_MOD_REWRITE'] == 'On') {
183
            return true;
184
        }
185
        if (isset($_SERVER['REDIRECT_HTTP_MOD_REWRITE']) && $_SERVER['REDIRECT_HTTP_MOD_REWRITE'] == 'On') {
186
            return true;
187
        }
188
        return false;
189
    }
190
191
    public function testIISRewriteModuleExists($moduleName = 'IIS_UrlRewriteModule')
192
    {
193
        if (isset($_SERVER[$moduleName]) && $_SERVER[$moduleName]) {
194
            return true;
195
        } else {
196
            return false;
197
        }
198
    }
199
200
    /**
201
     * Determines if the web server has any rewriting capability.
202
     *
203
     * @return bool
204
     */
205
    public function hasRewritingCapability()
206
    {
207
        return ($this->testApacheRewriteExists() || $this->testIISRewriteModuleExists());
208
    }
209
210
    /**
211
     * Get "nice" database name without "Database" suffix
212
     *
213
     * @param string $databaseClass
214
     * @return string
215
     */
216
    public function getDatabaseTypeNice($databaseClass)
217
    {
218
        return substr($databaseClass, 0, -8);
219
    }
220
221
    /**
222
     * Get an instance of a helper class for the specific database.
223
     *
224
     * @param string $databaseClass e.g. MySQLDatabase or MSSQLDatabase
225
     * @return DatabaseConfigurationHelper
226
     */
227
    public function getDatabaseConfigurationHelper($databaseClass)
228
    {
229
        return DatabaseAdapterRegistry::getDatabaseConfigurationHelper($databaseClass);
230
    }
231
}
232