-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
243 lines (214 loc) · 8.17 KB
/
index.php
File metadata and controls
243 lines (214 loc) · 8.17 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
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
<?php
/**
* Project Structure Viewer - Standalone Web Entry Point
*
* This file allows you to use the library directly on a web server
* without Composer installation. Just upload the entire project folder
* and access this file via browser.
*
* @author Walter Nuñez
* @version 1.0
* @encoding UTF-8
*/
// Set proper headers for HTML output
header('Content-Type: text/html; charset=UTF-8');
// Error handling for development
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Manual autoloader for standalone usage
spl_autoload_register(function ($class) {
// Convert namespace to file path
$prefix = 'ProjectStructureViewer\\';
$baseDir = __DIR__ . '/src/';
// Check if the class uses our namespace
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
// Get the relative class name
$relativeClass = substr($class, $len);
// Replace namespace separators with directory separators
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
// If the file exists, require it
if (file_exists($file)) {
require $file;
}
});
// Import the main class
use ProjectStructureViewer\ProjectStructureViewer;
try {
// Get parameters from URL
$projectRoot = $_GET['path'] ?? __DIR__;
$format = $_GET['format'] ?? 'html';
$title = $_GET['title'] ?? null;
// Validate and sanitize project root
if (!is_dir($projectRoot)) {
throw new Exception('El directorio especificado no existe: ' . htmlspecialchars($projectRoot));
}
$projectRoot = realpath($projectRoot);
if ($projectRoot === false) {
throw new Exception('No se pudo resolver la ruta del directorio.');
}
// Security check: prevent access to system directories
// But allow access to the current project directory even if it's in /var/www
$currentProjectDir = realpath(__DIR__);
$isCurrentProject = ($projectRoot === $currentProjectDir);
if (!$isCurrentProject) {
$restrictedPaths = [
'/etc', '/var', '/usr', '/bin', '/sbin', '/root', '/sys', '/proc',
'C:\\Windows', 'C:\\Program Files', 'C:\\Program Files (x86)',
'C:\\Users\\All Users', 'C:\\ProgramData'
];
foreach ($restrictedPaths as $restrictedPath) {
if (strpos($projectRoot, $restrictedPath) === 0) {
throw new Exception('Acceso denegado a directorios del sistema.');
}
}
}
// Create viewer instance
$viewer = new ProjectStructureViewer($projectRoot);
// Generate title if not provided
if ($title === null) {
$title = 'Estructura del Proyecto: ' . basename($projectRoot);
}
// Handle different output formats
switch (strtolower($format)) {
case 'json':
header('Content-Type: application/json; charset=UTF-8');
echo $viewer->toJson();
break;
case 'array':
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($viewer->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
case 'compact':
header('Content-Type: application/json; charset=UTF-8');
$structure = $viewer->generateStructure();
echo json_encode($structure, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
case 'html':
default:
$viewer->display($title);
break;
}
} catch (Exception $e) {
// Error page
http_response_code(500);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Project Structure Viewer - Error</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #333;
line-height: 1.6;
margin: 0;
padding: 20px;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.error-container {
max-width: 600px;
background: #fff;
padding: 40px;
border-radius: 15px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
text-align: center;
}
.error-icon {
font-size: 4em;
margin-bottom: 20px;
}
.error-header h1 {
color: #e74c3c;
margin: 0 0 10px 0;
font-size: 2em;
}
.error-message {
background: #f8f9fa;
border-left: 4px solid #e74c3c;
padding: 20px;
margin: 20px 0;
text-align: left;
border-radius: 5px;
font-family: 'Courier New', monospace;
color: #721c24;
}
.error-help {
background: #e8f5e8;
padding: 20px;
border-radius: 8px;
margin: 20px 0;
text-align: left;
}
.error-help h3 {
margin-top: 0;
color: #155724;
}
.usage-examples {
background: #f1f3f4;
padding: 15px;
border-radius: 5px;
margin: 10px 0;
font-family: 'Courier New', monospace;
font-size: 0.9em;
text-align: left;
}
.back-link {
display: inline-block;
background: #667eea;
color: white;
padding: 12px 25px;
text-decoration: none;
border-radius: 25px;
margin-top: 20px;
transition: background 0.3s;
}
.back-link:hover {
background: #5a67d8;
}
</style>
</head>
<body>
<div class="error-container">
<div class="error-icon">⚠️</div>
<div class="error-header">
<h1>Error en Project Structure Viewer</h1>
</div>
<div class="error-message">
<strong>Error:</strong> <?php echo htmlspecialchars($e->getMessage()); ?>
</div>
<div class="error-help">
<h3>💡 Soluciones Posibles:</h3>
<ul>
<li>Verifica que el directorio existe y es accesible</li>
<li>Asegúrate de que los archivos PHP tienen los permisos correctos</li>
<li>Comprueba que todas las clases están en la carpeta <code>src/</code></li>
<li>Intenta acceder desde el directorio raíz del proyecto</li>
</ul>
<h3>📖 Ejemplos de Uso:</h3>
<div class="usage-examples">
<strong>Uso básico:</strong><br>
<?php echo htmlspecialchars($_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI'])); ?>/index.php<br><br>
<strong>Directorio específico:</strong><br>
<?php echo htmlspecialchars($_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI'])); ?>/index.php?path=/ruta/al/proyecto<br><br>
<strong>Formato JSON:</strong><br>
<?php echo htmlspecialchars($_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI'])); ?>/index.php?format=json<br><br>
<strong>Con título personalizado:</strong><br>
<?php echo htmlspecialchars($_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI'])); ?>/index.php?title=Mi%20Proyecto
</div>
</div>
<a href="javascript:history.back()" class="back-link">← Volver</a>
</div>
</body>
</html>
<?php
}
?>