1 <?php
2
3 namespace Setup;
4
5 use Format;
6 use Service;
7 use Settings as SET;
8
9 10 11 12 13
14 class Apache extends AbstractBinSetup {
15
16 17 18 19 20
21 protected $binchecker;
22
23 24 25 26 27
28 function __construct() {
29 $this->dest = DIR_TMP . 'httpd.zip';
30 $this->dest_unzip = DIR_TMP . 'httpd' . DIRECTORY_SEPARATOR;
31
32 $this->binchecker = new \BinChecker\Apache();
33 $this->links = $this->binchecker->getLinks();
34
35 $this
36 ->promptDownload()
37 ->unzip()
38 ->copy()
39 ->editHTTPD()
40 ->installService();
41 }
42
43 44 45 46 47 48
49 protected function installService() {
50 if(Service::exists('aloapache')) {
51 _echo('Removing previous AloWAMP apache service');
52 _echo(Service::delete('aloapache'));
53 }
54
55 _echo('Installing apache service');
56 _echo(Service::installExe('aloapache',
57 DIR_APACHE .
58 $this->version .
59 DIRECTORY_SEPARATOR .
60 'bin' .
61 DIRECTORY_SEPARATOR .
62 'httpd.exe -k runservice',
63 'AloWAMP Apache ' . $this->version));
64
65 return $this;
66 }
67
68 69 70 71 72
73 protected function checkWebDir() {
74 if(!SET::$s->web_dir) {
75 $new_dir =
76 \IO::readline('Your default website name was not found - please enter a name (defaults to my-default-website if left empty)');
77
78 if(!$new_dir) {
79 $new_dir = 'my-default-website';
80 }
81
82 SET::$s->web_dir = $new_dir;
83 SET::$s->save();
84 }
85
86 if(!file_exists(DIR_WWW . SET::$s->web_dir . DIRECTORY_SEPARATOR)) {
87 mkdir(DIR_WWW . SET::$s->web_dir . DIRECTORY_SEPARATOR, 777, true);
88 }
89 }
90
91 92 93 94 95 96
97 protected function editHTTPD() {
98 $this->checkWebDir();
99 $apache_dir = DIR_APACHE . $this->version . DIRECTORY_SEPARATOR;
100 $httpd_conf = $apache_dir . 'conf' . DIRECTORY_SEPARATOR . 'httpd.conf';
101
102 if(file_exists($httpd_conf)) {
103 $contents = file_get_contents($httpd_conf);
104
105 if(!$contents) {
106 die('Failed to open httpd.conf. Aborting setup.');
107 } else {
108 _echo('Editing httpd.conf');
109
110 $php_dir = DIR_PHP . SET::$s->php_version;
111
112 $php5_module_loc = '"' . $php_dir . DIRECTORY_SEPARATOR . 'php5apache2_4.dll"';
113 $php5_module_replace = 'PHPIniDir "' . $php_dir . '"' . PHP_EOL
114 . 'LoadModule php5_module ' . $php5_module_loc . PHP_EOL
115 . 'LoadModule access_compat_module modules/mod_access_compat.so';
116
117 $htdocs = DIR_WWW . SET::$s->web_dir;
118 $log_dir = DIR_LOGS . 'apache' . DIRECTORY_SEPARATOR;
119
120 $contents = str_ireplace([
121 'LoadModule access_compat_module modules/mod_access_compat.so',
122 'ServerRoot "c:/Apache24"',
123 'Listen 80',
124 '#ServerName www.example.com:80',
125 '"c:/Apache24/htdocs"',
126 'DirectoryIndex index.html',
127 '"logs/error.log"',
128 '"logs/access.log"',
129 'AddType application/x-gzip .gz .tgz'
130 ],
131 [
132 $php5_module_replace,
133 'ServerRoot "' . rtrim($apache_dir, DIRECTORY_SEPARATOR) . '"',
134 'Listen 80' . PHP_EOL . 'Listen 443',
135 'ServerName localhost:80',
136 '"' . $htdocs . '"',
137 'DirectoryIndex index.php index.php3 index.html index.htm',
138 '"' . $log_dir . 'error.log"',
139 '"' . $log_dir . 'access.log"',
140 'AddType application/x-gzip .gz .tgz' .
141 PHP_EOL .
142 "\tAddType application/x-httpd-php .php" .
143 PHP_EOL .
144 "\tAddType application/x-httpd-php .php3"
145 ],
146 $contents);
147
148 if(file_put_contents($httpd_conf, $contents) !== false) {
149 _echo('httpd.conf edited');
150 } else {
151 die('Failed to edit httpd.conf. Aborting setup.');
152 }
153 }
154 } else {
155 die('Could not find httpd.conf. Aborting.');
156 }
157
158 return $this;
159 }
160
161 162 163 164 165 166
167 protected function copy() {
168 $scan = scandir($this->dest_unzip);
169 Format::formatScandir($scan);
170 $dir = null;
171
172 foreach($scan as $s) {
173 if(is_dir($this->dest_unzip . $s)) {
174 $dir = $this->dest_unzip . $s;
175 break;
176 }
177 }
178
179 if(!$dir) {
180 die('Could not find the apache source directory in the unzipped files. Aborting.');
181 } else {
182 _echo('Copying downloaded contents...');
183 $source = $dir;
184 $this->unzipped_destination = DIR_APACHE . $this->version;
185
186 if(!file_exists($this->unzipped_destination . DIRECTORY_SEPARATOR)) {
187 mkdir($this->unzipped_destination . DIRECTORY_SEPARATOR, 777, true);
188 }
189
190 shell_exec('xcopy /s /e "' . $source . '" "' . $this->unzipped_destination . '"');
191 $this->unzipped_destination .= DIRECTORY_SEPARATOR;
192
193 if(file_exists($this->unzipped_destination . 'bin')) {
194 _echo('Copy successful!');
195 $this->cleanup();
196 $this->updateSettings();
197 } else {
198 die('Failed to copy. Terminating setup.');
199 }
200
201 }
202
203 return $this;
204 }
205
206 207 208 209 210 211
212 protected function updateSettings() {
213 SET::$s->apache_version = $this->version;
214 SET::$s->save();
215
216 return $this;
217 }
218 }