-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap-admin.php
408 lines (362 loc) · 13.5 KB
/
sitemap-admin.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
session_start();
ini_set('max_execution_time', 300); // 5 minutes max execution time
// Enhanced Configuration
$config = [
'base_url' => (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]",
'public_html_path' => $_SERVER['DOCUMENT_ROOT'],
'allowed_extensions' => [
'html', 'htm', 'shtml', 'xhtml', 'xml', 'asp', 'aspx',
'jsp', 'php', 'do', 'cgi'
],
'excluded_extensions' => [
'txt', 'json', 'md', 'git', 'svg', 'jpg', 'jpeg', 'png', 'gif',
'pdf', 'css', 'js', 'ico', 'woff', 'woff2', 'ttf', 'eot', 'map'
],
'excluded_directories' => [
'cgi-bin', 'css', 'js', 'images', 'img', 'backend-libs', 'admin',
'test', 'tests', 'temp', 'tmp', 'cache', 'includes', 'vendor',
'node_modules', 'assets', 'dist', 'build', 'uploads', 'fonts'
],
'sitemap_path' => $_SERVER['DOCUMENT_ROOT'] . '/sitemap.xml',
'robots_path' => $_SERVER['DOCUMENT_ROOT'] . '/robots.txt',
'password' => 'admin', // Changed password
'default_changefreq' => 'weekly',
'max_urls' => 50000,
'batch_size' => 1000,
'debug' => false // Enable for debugging
];
// Authentication check with debugging
function checkAuth() {
global $config;
if ($config['debug']) {
error_log("Session admin status: " . (isset($_SESSION['admin']) ? $_SESSION['admin'] : 'not set'));
}
return (isset($_SESSION['admin']) && $_SESSION['admin'] === true);
}
// Handle login
if (isset($_POST['password'])) {
global $config;
if ($config['debug']) {
error_log("Login attempt - Posted password: " . $_POST['password']);
error_log("Configured password: " . $config['password']);
}
if (trim($_POST['password']) === trim($config['password'])) {
$_SESSION['admin'] = true;
if ($config['debug']) {
error_log("Login successful - Session admin set to true");
}
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
} else {
$error = "Incorrect password";
if ($config['debug']) {
error_log("Login failed - Incorrect password");
}
}
}
// Logout handler
if (isset($_GET['logout'])) {
session_destroy();
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
// Improved file scanning function
function scanDirectory($dir, &$results = array(), $depth = 0) {
global $config;
// Limit directory depth to prevent infinite recursion
if ($depth > 10) return $results;
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') continue;
$path = realpath($dir . DIRECTORY_SEPARATOR . $file);
$relativePath = str_replace($config['public_html_path'], '', $path);
// Skip excluded directories
$dirName = basename($dir);
if (in_array(strtolower($dirName), $config['excluded_directories'])) {
continue;
}
if (is_dir($path)) {
// Skip processing if directory is in excluded list
if (!in_array(basename($path), $config['excluded_directories'])) {
$results[] = [
'type' => 'directory',
'path' => $relativePath,
'included' => isset($_SESSION['included'][$relativePath]) ? true : false
];
scanDirectory($path, $results, $depth + 1);
}
} else {
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
// Only include allowed file types
if (in_array($ext, $config['allowed_extensions'])) {
$results[] = [
'type' => 'file',
'path' => $relativePath,
'included' => isset($_SESSION['included'][$relativePath]) ? true : false,
'lastmod' => filemtime($path)
];
}
}
}
return $results;
}
// Get canonical URL
function getCanonicalUrl($path) {
global $config;
// Remove index files from URL
$url = rtrim($config['base_url'], '/') . $path;
$url = preg_replace('/(index\.(php|html?|cgi))$/i', '', $url);
// Remove trailing slash for non-directory URLs
if (!is_dir($config['public_html_path'] . $path)) {
$url = rtrim($url, '/');
}
// Convert to lowercase for consistency
$url = strtolower($url);
// Encode URL properly
return str_replace(' ', '%20', $url);
}
// Calculate priority based on path depth
function calculatePriority($path) {
$depth = substr_count($path, '/');
return max(0.1, 1.0 - ($depth * 0.2));
}
// Generate sitemap XML
function generateSitemap($files) {
global $config;
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$urlset = $xml->createElement('urlset');
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$urlset->setAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
$xml->appendChild($urlset);
// Add homepage
$url = $xml->createElement('url');
$loc = $xml->createElement('loc', $config['base_url'] . '/');
$lastmod = $xml->createElement('lastmod', date('c'));
$changefreq = $xml->createElement('changefreq', 'daily');
$priority = $xml->createElement('priority', '1.0');
$url->appendChild($loc);
$url->appendChild($lastmod);
$url->appendChild($changefreq);
$url->appendChild($priority);
$urlset->appendChild($url);
$urlCount = 1;
foreach ($files as $file) {
if ($urlCount >= $config['max_urls']) break;
// Only include files that are explicitly checked
if (!isset($_SESSION['included'][$file['path']]) || $file['type'] === 'directory') continue;
$url = $xml->createElement('url');
// Use canonical URL
$canonicalUrl = getCanonicalUrl($file['path']);
$loc = $xml->createElement('loc', $canonicalUrl);
// Use ISO 8601 date format
$lastmod = $xml->createElement('lastmod', date('c', $file['lastmod']));
// Calculate priority based on URL depth
$urlPriority = calculatePriority($file['path']);
$priority = $xml->createElement('priority', number_format($urlPriority, 1));
// Set change frequency based on file modification time
$age = time() - $file['lastmod'];
$changefreq = $xml->createElement('changefreq',
$age < 86400 ? 'daily' :
($age < 604800 ? 'weekly' :
($age < 2592000 ? 'monthly' : 'yearly'))
);
$url->appendChild($loc);
$url->appendChild($lastmod);
$url->appendChild($changefreq);
$url->appendChild($priority);
$urlset->appendChild($url);
$urlCount++;
}
// Compress the sitemap
$xml->save($config['sitemap_path']);
if (function_exists('gzencode')) {
file_put_contents($config['sitemap_path'] . '.gz', gzencode($xml->saveXML()));
}
}
// Generate robots.txt
function generateRobotsTxt($files) {
global $config;
$content = "User-agent: *\n";
$content .= "Allow: /\n\n";
// Explicitly disallow admin and system directories
foreach ($config['excluded_directories'] as $dir) {
$content .= "Disallow: /$dir/\n";
}
// Add Crawl-delay directive
$content .= "\nCrawl-delay: 1\n\n";
// Add both regular and gzipped sitemap locations
$content .= "Sitemap: " . $config['base_url'] . "/sitemap.xml\n";
if (function_exists('gzencode')) {
$content .= "Sitemap: " . $config['base_url'] . "/sitemap.xml.gz\n";
}
file_put_contents($config['robots_path'], $content);
}
// Handle form submission for sitemap generation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['generate'])) {
if (checkAuth()) {
// Clear previous selection
$_SESSION['included'] = array();
// Store new selection
if (isset($_POST['included']) && is_array($_POST['included'])) {
foreach ($_POST['included'] as $path => $value) {
$_SESSION['included'][$path] = true;
}
}
$files = scanDirectory($config['public_html_path']);
generateSitemap($files);
generateRobotsTxt($files);
$_SESSION['message'] = 'Sitemap and robots.txt generated successfully!';
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sitemap Generator</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.file-list {
margin: 20px 0;
}
.file-item {
margin: 5px 0;
padding: 5px;
border: 1px solid #ddd;
}
.directory {
font-weight: bold;
background: #f5f5f5;
}
.controls {
position: sticky;
top: 0;
background: white;
padding: 10px 0;
border-bottom: 1px solid #ddd;
z-index: 100;
}
.message {
padding: 10px;
background: #e6ffe6;
border: 1px solid #99ff99;
margin: 10px 0;
}
.error {
padding: 10px;
background: #ffe6e6;
border: 1px solid #ff9999;
margin: 10px 0;
}
.login-form {
max-width: 300px;
margin: 100px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
}
.login-form input[type="password"] {
width: 100%;
padding: 8px;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 4px;
}
.login-form input[type="submit"],
button {
padding: 8px 16px;
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.login-form input[type="submit"]:hover,
button:hover {
background: #45a049;
}
.logout {
float: right;
}
.debug-info {
background: #fff3cd;
padding: 10px;
margin: 10px 0;
border: 1px solid #ffeeba;
border-radius: 4px;
}
</style>
</head>
<body>
<?php if (!checkAuth()): ?>
<!-- Login Form -->
<div class="login-form">
<h2>Login to Sitemap Generator</h2>
<?php if (isset($error)): ?>
<div class="error"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if ($config['debug']): ?>
<div class="debug-info">
Debug Mode Active
</div>
<?php endif; ?>
<form method="post">
<input type="password" name="password" placeholder="Enter password" required>
<input type="submit" value="Login">
</form>
</div>
<?php else: ?>
<!-- Sitemap Generator Interface -->
<h1>Sitemap Generator</h1>
<a href="?logout" class="logout">Logout</a>
<?php if (isset($_SESSION['message'])): ?>
<div class="message">
<?php
echo htmlspecialchars($_SESSION['message']);
unset($_SESSION['message']);
?>
</div>
<?php endif; ?>
<form method="post">
<div class="controls">
<button type="submit" name="generate">Generate Sitemap & robots.txt</button>
<button type="button" onclick="toggleAll(true)">Select All</button>
<button type="button" onclick="toggleAll(false)">Deselect All</button>
</div>
<div class="file-list">
<?php
$files = scanDirectory($config['public_html_path']);
foreach ($files as $file):
?>
<div class="file-item <?php echo $file['type'] === 'directory' ? 'directory' : ''; ?>">
<label>
<input type="checkbox"
name="included[<?php echo htmlspecialchars($file['path']); ?>]"
value="1"
<?php echo isset($_SESSION['included'][$file['path']]) ? 'checked' : ''; ?>>
<?php echo htmlspecialchars($file['path']); ?>
</label>
</div>
<?php endforeach; ?>
</div>
</form>
<script>
function toggleAll(state) {
document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
checkbox.checked = state;
});
}
</script>
<?php endif; ?>
</body>
</html>