-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.php
234 lines (202 loc) · 5.77 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
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
define('SEPERATOR', '/');
define('INS_NAME', '_program');
/**
* Join file
*
* @param string ...$path
* @return string
*/
function joinPath(string ...$path)
{
$num_args = func_num_args();
$args = func_get_args();
$path = trim($args[0], SEPERATOR);
if ($num_args > 1) {
for ($i = 1; $i < $num_args; $i++) {
$path .= SEPERATOR . trim($args[$i], SEPERATOR);
}
}
return $path;
}
/**
* Installer class
* @author Claude Fassinou <dev.claudy@gmail>
* @license
* @copyright 2021
*/
class Installer
{
/**
* Dir can be explored
*
* @var string
*/
protected $explorer = '';
/**
* SplFileInfo[]
*
* @var @var SplFileInfo[]
*/
private $files = [];
/**
* Root of code source
*
* @var string
*/
public $from = __DIR__;
/**
* Final zip file
* @var string
*/
public $to;
/**
* Zip class
* @var ZipArchive
*/
private $zip;
public $errors =[];
public function __construct()
{
$this->zip = new ZipArchive;
$this->zipTo();
}
/**
* Unzip code source
*
* @param string|null $to
* @return void
*/
public function unzip(string $to = null)
{
$this->zipTo();
if ($this->zip->open($to ?? $this->to) === true) {
$su = $this->zip->extractTo('.');
return $this->zip->close();
}
$this->errors []= "Can't open $this->to file";
return false;
}
/**
* Zip code source
*
* @return void
*/
public function zip()
{
$this->scandir($this->from);
// Get real path for our folder
$rootPath = realpath($this->from);
if (!is_file($this->to)) {
$this->zip->open($this->to, ZipArchive::CREATE | ZipArchive::OVERWRITE);
foreach ($this->files as $name => $file) {
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$sourceFilePath = $file->getRealPath();
if (!in_array($file->getFileName(), [basename(__FILE__), $this->to])) {
$relativePath = substr($sourceFilePath, strlen($rootPath) + 1);
$relativePath = str_replace(DIRECTORY_SEPARATOR, SEPERATOR, $relativePath);
// Add current file to archive
$this->zip->addFile($sourceFilePath, $relativePath);
}
}
}
return $this->zip->close();
}
$this->errors [] = "The project named $this->to already exist ";
return false ;
}
/**
* Scan a dir
*
* @param string $dir
* @return void
*/
public function scandir(string $dir)
{
$this->explorer = $dir;
$rootPath = realpath($this->explorer);
// Create recursive directory iterator
/** @var SplFileInfo[] $files*/
$this->files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
}
/**
* Set final zip file name
*
* @param string|null $to
* @return void
*/
private function zipTo(string $to = null)
{
$t = INS_NAME;
$this->to = $to ?? joinPath($t . '.zip');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Installing ...</title>
<style>
body {
background-color: rgb(240, 240, 240);
}
.wrapper {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.box {
width: 400px;
background-color: white;
box-shadow: 0 3px 20px #aba;
padding: 1em;
}
.btn-box {
margin: 12px 0;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box">
<h4>Installing ...</h4>
<div class="btn-box">
<button type="button" onclick="window.location='<?= basename(__FILE__)?>'">Restart</button>
<button type="button" <?= isset($_GET['step']) && 'compress' === $_GET['step'] ? "disabled" :'' ?> onclick="window.location='?step=compress'">Compresser</button>
<button type="button" <?= isset($_GET['step']) && 'install' === $_GET['step'] ? "disabled" :'' ?> onclick="window.location='?step=install'"> Installer </button>
</div>
<?php
if (isset($_GET['step'])) {
$installer = new Installer();
if ('install' === $_GET['step']) {
$success= $installer->unzip();
$message = $success ? 'Installation done !' :'Installation failed';
}
if ('compress' === $_GET['step']) {
$success= $installer->zip();
$message = $success ? '</small>Project compressed with success ! and named to </small>' .$installer->to :'Compression failed';
}
if (isset($success)) {
echo '<mark><b>'.$message.'</b></mark> <br>';
if (!$success) {
echo '<b>Reason(s) ! </b><br>';
foreach ($installer->errors as $error) {
echo $error .'<br>';
}
}
}
}
?>
</div>
</div>
</body>
</html>