-
Notifications
You must be signed in to change notification settings - Fork 5
/
installer.php
221 lines (206 loc) · 6.53 KB
/
installer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Installation script
*
* This should be executed from the shell as webserver user (usually www-data),
* like this: su www-data -s /bin/sh -c "php installer.php"
*
*/
/**
* Execute an HTTP GET request
*
* @param string $url URL to request
* @param resource $handle Local file resource for getting file
*
* @return bool|string HTTP response or false if failure
*/
function doHttpRequest($url, $handle = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_USERAGENT, 'own-my-money-installer');
curl_setopt($curl, CURLOPT_TIMEOUT, 90);
if ($handle) {
curl_setopt($curl, CURLOPT_FILE, $handle);
curl_setopt($curl, CURLOPT_NOPROGRESS, false);
curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, 'progress');
} else {
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
}
$result = curl_exec($curl);
if (curl_errno($curl)) {
output('download error : ' . curl_error($curl), false);
$result = false;
} else {
$curlInfo = curl_getinfo($curl);
output($curlInfo['download_content_length'] . ' bytes on ' . $curlInfo['total_time'] . ' seconds', false);
}
curl_close($curl);
return $result;
}
/**
* Output content
*
* @param string $content Information to be logged
* @param boolean $insertLine If false, the content will be added to the last log message
*/
function output($content, $insertLine = true)
{
if ($insertLine) {
echo "\r\n";
} else {
echo ' ';
}
echo $content;
}
/**
* Display a question and wait for user answer
*
* @param string $question Question to display
*
* @return string User answer
*/
function prompt($question)
{
output($question, true);
$stdin = fopen('php://stdin', 'r');
$response = fgets($stdin);
fclose($stdin);
return trim($response);
}
/**
* Display download progress
*
* @param string $content Information to be logged
* @param boolean $insertLine If false, the content will be added to the last log message
*/
function progress($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size)
{
static $previousProgress = 0;
if ($download_size == 0) {
$progress = 0;
return;
}
$progress = round($downloaded_size * 100 / $download_size);
if ($progress > $previousProgress) {
$previousProgress = $progress;
output("$progress%...", false);
}
}
$folder = getcwd();
if (!is_writable($folder)) {
$webUser = posix_getpwuid(posix_getuid())['name'];
output("Folder '$folder' is not writable, you should run: chown $webUser $folder -R", true);
return false;
}
//get last release on repository
$url = 'https://api.github.com/repos/nioc/own-my-money/releases/latest';
output('Get last version...', true);
$result = doHttpRequest($url, null);
if (!$result) {
output('failed', false);
return false;
}
output('done', false);
//get version in release response
$release = json_decode($result);
if (!property_exists($release, 'tag_name')) {
output('No release was found', true);
return false;
}
$version = $release->tag_name;
output('Release is '.$version, true);
//iterate on release assets to find the archive
$downloadUrl = null;
if (property_exists($release, 'assets') && is_array($release->assets) && count($release->assets) > 0) {
foreach ($release->assets as $asset) {
if (property_exists($asset, 'name') && $asset->name === "own-my-money-$version.tar.gz" && property_exists($asset, 'browser_download_url')) {
$downloadUrl = $asset->browser_download_url;
$filename = $asset->name;
}
}
}
if ($downloadUrl === null) {
output('No download url was found', true);
return false;
}
//download archive
output('Download archive...', true);
if (!$localHandle = fopen($filename, 'w+')) {
output("unable to write archive at $folder/$filename", false);
return false;
}
$result = doHttpRequest($downloadUrl, $localHandle);
fclose($localHandle);
if (!$result) {
output('failed', false);
return false;
}
output('done', false);
output('', true);
//extract files from archive
$pharData = new PharData($filename);
do {
$rootFolder = prompt('Type your document root folder (default: /var/www):');
if ($rootFolder === '') {
$rootFolder = '/var/www';
}
if (!$isDir = is_dir($rootFolder)) {
output("Folder '$rootFolder' does not exist", true);
}
if (!$isWriteable = is_writable($rootFolder)) {
output("Folder '$rootFolder' is not writable", true);
}
} while (!$isDir || !$isWriteable);
output("Extract archive to $rootFolder...", true);
try {
$pharData->extractTo($rootFolder);
} catch (Exception $e) {
output('failed', false);
output($e->getMessage(), false);
return false;
}
output('done', false);
//create public folder
output('Create web folder...', true);
if (mkdir("$rootFolder/own-my-money/dist")) {
output('done', false);
} else {
output("failed, you have to create it from shell: mkdir $rootFolder/own-my-money/dist", false);
}
//create symolic links
output('Create symolic links...', true);
if (symlink("$rootFolder/own-my-money/server", "$rootFolder/own-my-money/dist/server")) {
output('done', false);
} else {
output("failed, you have to create it from shell: ln -s $rootFolder/own-my-money/server $rootFolder/own-my-money/dist/server", false);
}
output('', true);
//change back-end url
do {
$domain = prompt('Type your domain name (example: https://money.yourdomain.ltd):');
$isValid = filter_var($domain, FILTER_VALIDATE_URL);
if (!$isValid) {
output("'$domain' is not a valid domain", true);
}
} while (!$isValid);
if ($handle = opendir("$rootFolder/own-my-money/money-front-vue/dist/assets/")) {
while (false !== ($entry = readdir($handle))) {
$current = "$rootFolder/own-my-money/money-front-vue/dist/assets/$entry";
if (is_file($current)) {
$content = file_get_contents($current);
$content = str_replace('http://localhost', $domain, $content);
file_put_contents($current, $content);
}
}
closedir($handle);
}
//set permission
output('If you did not use webserver (www-data, apache, ...) user to launch this script, you have to change the owner of files served to your web user:', true);
output("chown www-data $rootFolder/own-my-money/ -RL", true);
output('', true);
//redirect to the setup page
output("Application is installed, you have to configure it, please visit $domain/#/setup", true);
output('', true);
output('', true);