-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.php
294 lines (280 loc) · 6.81 KB
/
index.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
define('__PHP52__', version_compare((float)phpversion(), (float)'5.2.17', '<='));
define('__PHP53__', version_compare((float)phpversion(), (float)'5.3', '<='));
function p($obj)
{
echo '<pre>';
print_r($obj);
echo '</pre>';
}
function copyFile($source, $dest)
{
$is_dot = array ('.', '..');
if (is_dir($source))
{
if (__PHP53__)
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source),
RecursiveIteratorIterator::SELF_FIRST
);
}
else
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
}
foreach ($iterator as $file)
{
if (__PHP52__)
{
if (in_array($file->getBasename(), $is_dot))
continue;
}
elseif (__PHP53__)
{
if ($file->isDot())
continue;
}
if ($file->isDir())
mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName(), true);
else
copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
}
unset($iterator, $file);
}
else
copy($source, $dest);
return true;
}
function addIndex($path, $cli = false)
{
$is_dot = array ('.', '..');
$file_extension = substr(strrchr($path, '.'), 1);
if (is_dir($path))
{
if (__PHP53__)
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST
);
}
else
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
}
foreach ($iterator as $pathname => $file)
{
if (__PHP52__)
{
if (in_array($file->getBasename(), $is_dot))
continue;
}
elseif (__PHP53__)
{
if ($file->isDot())
continue;
}
$name = (string)trim($file->getFilename());
$exp = explode('\\', $pathname);
$dirname = isset($exp[0])? $exp[0].'/' : '';
if(count($exp) === 2 && $file->isFile())
{
if (!file_exists($dirname.'index.php'))
{
if (copyFile('sources/index.php', $dirname.'index.php') === true)
continue;
}
}
else
{
if ($file->isDir())
{
$dirname = str_replace('\\', '/', $file->getPathname().'/');
if (!file_exists($dirname.'index.php'))
{
if (copyFile('sources/index.php', $dirname.'index.php') === true)
continue;
}
}
}
}
unset($iterator, $pathname, $file);
$msg = 'index.php added in '.$path;
if ($cli === true)
echo $msg."\n";
else
p($msg);
}
elseif ($file_extension === 'zip')
{
if (class_exists('ZipArchive'))
{
$add_index = array();
$zip = new ZipArchive();
$res = $zip->open($path);
if ($res === true)
{
for ($i = 0; $i < $zip->numFiles; $i++)
{
$stat = $zip->statIndex($i);
if (!empty($stat))
{
$file_info = pathinfo($stat['name']);
if (!empty($file_info))
{
$dirname = trim($file_info['dirname']);
$filename = trim($file_info['filename']);
$basename = trim($file_info['basename']);
if (!in_array($dirname, $is_dot))
{
$getFromName = $zip->getFromName($dirname.'/index.php');
if (empty($getFromName))
{
$add_index[] = $dirname.'/';
}
}
}
}
}
$add_index = array_unique($add_index);
foreach ($add_index as $dir_path)
{
if ($zip->addFile('sources/index.php', $dir_path.'index.php') === true)
continue;
}
unset($add_index, $dir_path);
$zip->close();
unset($zip);
$msg = 'index.php added in '.$path;
if ($cli === true)
echo $msg."\n";
else
p($msg);
}
}
else
{
if ($cli === true)
echo "You need to install ZipArchive\npecl install zip\n";
else
p('You need to install ZipArchive<br />pecl install zip');
}
}
else
{
$msg = $path.' isn\'t a directory or zip file';
if ($cli === true)
echo $msg."\n";
else
p($msg);
}
}
if (php_sapi_name() === 'cli')
{
if (isset($argv) && (isset($argc) && $argc >= 2))
{
array_shift($argv);
foreach($argv as $dir)
addIndex($dir, true);
}
}
elseif (isset($argv) && (isset($argc) && $argc < 2))
{
echo 'Usage: php [directory...]';
echo "\n\t".'php index.php /var/www/prestashop1611/modules/mymodule/'."\n";
}
else
{
if(isset($_GET['path']))
{
$get_paths = $_GET['path'];
$paths = explode(',', $get_paths);
foreach($paths as $path)
addIndex(trim(strip_tags($path)));
}
else
{
if(!empty($_POST))
{
$get_paths = $_POST['path'];
$paths = explode(',', $get_paths);
foreach($paths as $path)
addIndex(trim(strip_tags($path)));
}
else
{
echo '<!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">
<title>Auto Index</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/theme.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic" rel="stylesheet" type="text/css">
</head>
<body id="page-top" class="index">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header page-scroll">
<a class="navbar-brand">Auto Index <sup><small>v 1.0.1</small></sup></a>
</div>
</div>
</nav>
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form name="sentMessage" id="contactForm" action="index.php" method="post">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Path to your directory</label>
<input type="text" class="form-control" placeholder="/var/www/modules/mymodule/" id="path">
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success">
</div>
<div class="row">
<div class="form-group col-xs-12">
<input type="submit" class="btn btn-success btn-lg" value="Add index" />
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<script src="js/jquery.js"></script>
<script>
$(".btn").on("click", function (e) {
e.preventDefault();
$("#success").html("<pre>Adding index file is in progressing...</pre>");
$.ajax({
type: "POST",
url: "index.php",
dataType: "html",
data: {
path : $("#path").val(),
},
success : function(data) {
$("#success").html(data);
}
});
});
</script>
</body>
</html>';
}
}
}