-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php.backup
More file actions
626 lines (539 loc) · 22.5 KB
/
Copy pathindex.php.backup
File metadata and controls
626 lines (539 loc) · 22.5 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<?php
require_once __DIR__ . '/router.php';
date_default_timezone_set('America/Toronto');
$routes = [
'/' => [
'title' => 'Dashboard',
'icon' => 'bi-speedometer2',
'file' => __DIR__ . '/view/dashboard.php',
],
'/kanban' => [
'title' => 'Kanban',
'icon' => 'bi-kanban',
'file' => __DIR__ . '/view/kanban.php',
],
'/api/kanban' => [
'title' => 'Kanban API',
'icon' => 'bi-kanban',
'file' => __DIR__ . '/api/kanban.php',
'nav' => false,
],
'/roadmap' => [
'title' => 'Roadmap',
'icon' => 'bi-map',
'file' => __DIR__ . '/view/roadmap.php',
],
'/design' => [
'title' => 'Design',
'icon' => 'bi-brush',
'file' => __DIR__ . '/view/design.php',
],
'/next' => [
'title' => 'Next',
'icon' => 'bi-arrow-right-square',
'file' => __DIR__ . '/view/next.php',
],
'/agents' => [
'title' => 'Agents',
'icon' => 'bi-robot',
'file' => __DIR__ . '/view/agents.php',
],
];
$managedMarkdownFiles = [
'KANBAN.md' => "# Kanban\n\n## Todo\n\n## In Progress\n\n## Done\n",
'ROADMAP.md' => "# Roadmap\n\n## Now\n\n- [ ] Define immediate priorities\n\n## Next\n\n- [ ] Plan upcoming work\n\n## Later\n\n- [ ] Capture long-term ideas\n",
'DESIGN.md' => "# Design\n\n",
'NEXT.md' => "# Next\n\n",
];
$projectsDirectory = __DIR__ . '/projects';
$configDirectory = __DIR__ . '/config';
$projectsConfigFile = $configDirectory . '/projects.json';
$projectActionResult = null;
function e(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
function ensureDirectory(string $directory): ?string
{
if (is_dir($directory)) {
return null;
}
if (@mkdir($directory, 0775, true) === false && !is_dir($directory)) {
return 'Unable to create directory: ' . $directory;
}
return null;
}
function slugifyProject(string $value): string
{
$value = strtolower(trim($value));
$value = preg_replace('/[^a-z0-9]+/', '-', $value);
$value = trim((string) $value, '-');
return $value !== '' ? $value : 'project';
}
function readProjectsConfig(string $configFile): array
{
if (!is_file($configFile)) {
return [
'active_project' => null,
'projects' => [],
];
}
$decoded = json_decode((string) file_get_contents($configFile), true);
if (!is_array($decoded)) {
return [
'active_project' => null,
'projects' => [],
];
}
$decoded['active_project'] = $decoded['active_project'] ?? null;
$decoded['projects'] = is_array($decoded['projects'] ?? null) ? $decoded['projects'] : [];
return $decoded;
}
function writeProjectsConfig(string $configFile, array $config): ?string
{
$json = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if ($json === false) {
return 'Unable to encode projects configuration.';
}
if (@file_put_contents($configFile, $json . "\n") === false) {
return 'Unable to write projects configuration.';
}
return null;
}
function readFirstMarkdownHeading(string $file): ?string
{
if (!is_file($file)) {
return null;
}
foreach (preg_split('/\R/', (string) file_get_contents($file)) as $line) {
if (preg_match('/^#\s+(.+)$/', trim($line), $matches) === 1) {
return trim($matches[1]);
}
}
return null;
}
function detectGitRepository(string $projectPath): ?string
{
$gitConfig = $projectPath . '/.git/config';
if (!is_file($gitConfig)) {
return null;
}
$content = (string) file_get_contents($gitConfig);
if (preg_match('/\[remote "origin"\][\s\S]*?url\s*=\s*(.+)/', $content, $matches) === 1) {
return trim($matches[1]);
}
return null;
}
function sanitizeGitRepositoryForDisplay(?string $repository): string
{
$repository = trim((string) $repository);
if ($repository === '') {
return '—';
}
$repository = preg_replace('#^(https?://)[^/@]+@#', '$1', $repository);
return $repository ?? '—';
}
function discoverProjects(string $projectsDirectory): array
{
$projects = [];
if (!is_dir($projectsDirectory)) {
return $projects;
}
$entries = glob($projectsDirectory . '/*', GLOB_ONLYDIR) ?: [];
foreach ($entries as $entry) {
$realEntry = realpath($entry);
if ($realEntry === false || !is_dir($realEntry)) {
continue;
}
$slug = basename($realEntry);
$readmeTitle = readFirstMarkdownHeading($realEntry . '/README.md');
$projects[$slug] = [
'name' => $readmeTitle ?: ucwords(str_replace('-', ' ', $slug)),
'slug' => $slug,
'path' => $realEntry,
'git_repository' => detectGitRepository($realEntry),
'created_at' => null,
'updated_at' => date('c'),
];
}
ksort($projects, SORT_NATURAL | SORT_FLAG_CASE);
return $projects;
}
function syncProjectsConfig(string $projectsDirectory, string $configFile): array
{
$config = readProjectsConfig($configFile);
$discoveredProjects = discoverProjects($projectsDirectory);
foreach ($discoveredProjects as $slug => $project) {
$existing = is_array($config['projects'][$slug] ?? null) ? $config['projects'][$slug] : [];
$config['projects'][$slug] = array_merge($project, $existing, [
'name' => $existing['name'] ?? $project['name'],
'slug' => $slug,
'path' => $project['path'],
'git_repository' => $existing['git_repository'] ?? $project['git_repository'],
'updated_at' => date('c'),
]);
}
foreach (array_keys($config['projects']) as $slug) {
if (!isset($discoveredProjects[$slug])) {
$config['projects'][$slug]['missing'] = true;
$config['projects'][$slug]['updated_at'] = date('c');
} else {
unset($config['projects'][$slug]['missing']);
}
}
if ($config['active_project'] === null && $config['projects'] !== []) {
$config['active_project'] = array_key_first($config['projects']);
}
writeProjectsConfig($configFile, $config);
return $config;
}
function createProject(string $projectsDirectory, string $configFile, string $projectName, ?string $gitRepository = null): array
{
$projectName = trim($projectName);
$gitRepository = trim((string) $gitRepository);
if ($projectName === '') {
return [
'success' => false,
'message' => 'Project name is required.',
];
}
$slug = slugifyProject($projectName);
$projectPath = $projectsDirectory . '/' . $slug;
if (file_exists($projectPath)) {
return [
'success' => false,
'message' => 'A project with this slug already exists: ' . $slug,
];
}
if (@mkdir($projectPath, 0775, true) === false && !is_dir($projectPath)) {
return [
'success' => false,
'message' => 'Unable to create project directory.',
];
}
$readme = '# ' . $projectName . "\n\nProject notes and overview.\n";
if (@file_put_contents($projectPath . '/README.md', $readme) === false) {
return [
'success' => false,
'message' => 'Project directory was created, but README.md could not be written.',
];
}
$config = syncProjectsConfig($projectsDirectory, $configFile);
$config['projects'][$slug] = array_merge($config['projects'][$slug] ?? [], [
'name' => $projectName,
'slug' => $slug,
'path' => realpath($projectPath) ?: $projectPath,
'git_repository' => $gitRepository !== '' ? $gitRepository : null,
'created_at' => date('c'),
'updated_at' => date('c'),
]);
$config['active_project'] = $slug;
$writeError = writeProjectsConfig($configFile, $config);
if ($writeError !== null) {
return [
'success' => false,
'message' => $writeError,
];
}
return [
'success' => true,
'message' => 'Project created: ' . $projectName,
];
}
function ensureRewriteConfiguration(string $root, bool $isApache): array
{
if ($isApache === false) {
return [
'enabled' => false,
'message' => 'Automatic rewrite setup is only available for Apache.',
];
}
$htaccessFile = $root . '/.htaccess';
$managedBlock = <<<'HTACCESS'
# Project Management front controller
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]
</IfModule>
HTACCESS;
if (is_file($htaccessFile)) {
$currentContent = (string) file_get_contents($htaccessFile);
if (str_contains($currentContent, '# Project Management front controller')) {
return [
'enabled' => true,
'message' => '.htaccess already contains Project Management rewrite rules.',
];
}
$newContent = rtrim($currentContent) . "\n\n" . $managedBlock . "\n";
} else {
$newContent = $managedBlock . "\n";
}
$written = @file_put_contents($htaccessFile, $newContent);
if ($written === false) {
return [
'enabled' => false,
'message' => 'Unable to write .htaccess. Check folder permissions.',
];
}
return [
'enabled' => true,
'message' => '.htaccess rewrite rules were created automatically.',
];
}
function renderServerRoutingStatus(array $rewriteStatus, bool $isApache, bool $isNginx, bool $isBuiltinServer): void
{
?>
<div class="card border-0 shadow-sm mt-4">
<div class="card-body p-4">
<h2 class="h5 mb-3">
<i class="bi bi-hdd-network"></i>
Server routing status
</h2>
<p class="mb-2">
<strong>Detected server:</strong>
<code><?= e($_SERVER['SERVER_SOFTWARE'] ?? PHP_SAPI); ?></code>
</p>
<?php if ($isApache): ?>
<div class="alert <?= $rewriteStatus['enabled'] ? 'alert-success' : 'alert-warning'; ?> mb-0">
<?= e($rewriteStatus['message']); ?>
</div>
<?php elseif ($isNginx): ?>
<div class="alert alert-info mb-0">
For Nginx, add <code>try_files $uri $uri/ /index.php?$query_string;</code> to this site's <code>location /</code> block.
</div>
<?php elseif ($isBuiltinServer): ?>
<div class="alert alert-info mb-0">
For PHP's built-in server, start it with <code>php -S localhost:8000 index.php</code> so requests route through the front controller.
</div>
<?php else: ?>
<div class="alert alert-info mb-0">
Configure your web server to route unknown paths to <code>index.php</code>.
</div>
<?php endif; ?>
</div>
</div>
<?php
}
function renderProjectManager(Router $router, array $projectsConfig, ?array $projectActionResult, array $setupErrors): void
{
$projects = $projectsConfig['projects'] ?? [];
?>
<div class="card border-0 shadow-sm mt-4">
<div class="card-body p-4">
<div class="d-flex flex-wrap align-items-start justify-content-between gap-3 mb-3">
<div>
<h2 class="h5 mb-1">
<i class="bi bi-folder"></i>
Managed projects
</h2>
<p class="text-muted mb-0">
Projects are stored in <code>projects/</code> and synced to <code>config/projects.json</code>.
</p>
</div>
</div>
<?php if ($projectActionResult !== null): ?>
<div class="alert <?= $projectActionResult['success'] ? 'alert-success' : 'alert-warning'; ?>">
<?= e($projectActionResult['message']); ?>
</div>
<?php endif; ?>
<?php if ($setupErrors !== []): ?>
<div class="alert alert-warning">
<?= e(implode(' ', $setupErrors)); ?>
</div>
<?php endif; ?>
<form method="post" action="/" class="row g-3 align-items-end mb-4">
<input type="hidden" name="action" value="create_project">
<div class="col-lg-5">
<label for="project_name" class="form-label">New project name</label>
<input type="text" class="form-control" id="project_name" name="project_name" placeholder="Core Web" required>
</div>
<div class="col-lg-5">
<label for="git_repository" class="form-label">Git repository <span class="text-muted">optional</span></label>
<input type="text" class="form-control" id="git_repository" name="git_repository" placeholder="git@github.com:LaswitchTech/core.git">
</div>
<div class="col-lg-2 d-grid">
<button type="submit" class="btn btn-primary">
<i class="bi bi-plus-lg"></i>
Create
</button>
</div>
</form>
<?php if ($projects === []): ?>
<div class="alert alert-info mb-0">
No managed projects found yet. Create one above or add folders inside <code>projects/</code>.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-sm align-middle mb-0">
<thead>
<tr>
<th>Project</th>
<th>Slug</th>
<th>Git repository</th>
<th>Path</th>
<th>Status</th>
<th class="text-end">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($projects as $project): ?>
<tr>
<td class="fw-semibold"><?= e((string) ($project['name'] ?? 'Unnamed project')); ?></td>
<td><code><?= e((string) ($project['slug'] ?? '')); ?></code></td>
<td class="small text-muted"><?= e(sanitizeGitRepositoryForDisplay($project['git_repository'] ?? null)); ?></td>
<td class="small text-muted"><?= e((string) ($project['path'] ?? '')); ?></td>
<td>
<?php if (!empty($project['missing'])): ?>
<span class="badge text-bg-warning">Missing folder</span>
<?php elseif (($projectsConfig['active_project'] ?? null) === ($project['slug'] ?? null)): ?>
<span class="badge text-bg-success">Active</span>
<?php else: ?>
<span class="badge text-bg-secondary">Available</span>
<?php endif; ?>
</td>
<td class="text-end">
<?php if (empty($project['missing']) && !empty($project['slug'])): ?>
<div class="btn-group btn-group-sm" role="group" aria-label="Project actions">
<a href="<?= e($router->projectRouteUrl('/kanban', (string) $project['slug'])); ?>" class="btn btn-outline-primary" title="Kanban">
<i class="bi bi-kanban"></i>
</a>
<a href="<?= e($router->projectRouteUrl('/roadmap', (string) $project['slug'])); ?>" class="btn btn-outline-secondary" title="Roadmap">
<i class="bi bi-map"></i>
</a>
<a href="<?= e($router->projectRouteUrl('/design', (string) $project['slug'])); ?>" class="btn btn-outline-secondary" title="Design">
<i class="bi bi-brush"></i>
</a>
<a href="<?= e($router->projectRouteUrl('/next', (string) $project['slug'])); ?>" class="btn btn-outline-secondary" title="Next">
<i class="bi bi-arrow-right-square"></i>
</a>
<a href="<?= e($router->projectRouteUrl('/agents', (string) $project['slug'])); ?>" class="btn btn-outline-secondary" title="Agents">
<i class="bi bi-robot"></i>
</a>
</div>
<?php else: ?>
<span class="text-muted">—</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
function renderIncludedPage(Router $router, ?string $file, ?array $selectedProject, ?string $routeError, array $rewriteStatus, bool $isApache, bool $isNginx, bool $isBuiltinServer, array $projectsConfig, ?array $projectActionResult, array $setupErrors): void
{
if ($routeError !== null) {
?>
<div class="alert alert-warning">
<strong>Project routing error.</strong> <?= e($routeError); ?>
</div>
<a href="/" class="btn btn-outline-secondary">
<i class="bi bi-arrow-left"></i>
Go back
</a>
<?php
return;
}
if ($file === null) {
?>
<section class="hero-card card border-0 shadow-sm">
<div class="card-body p-4 p-lg-5">
<div class="row align-items-center g-4">
<div class="col-lg-8">
<span class="badge text-bg-primary rounded-pill mb-3">Dashboard</span>
<h1 class="display-6 fw-bold mb-3">Project command center</h1>
<p class="lead text-muted mb-0">
Use this dashboard to create, discover, and manage local Markdown-backed projects.
</p>
</div>
<div class="col-lg-4">
<div class="d-grid gap-2">
<a href="<?= e($router->routeUrl('/kanban')); ?>" class="btn btn-primary btn-lg">
<i class="bi bi-kanban"></i>
Open Kanban
</a>
<a href="<?= e($router->routeUrl('/roadmap')); ?>" class="btn btn-outline-secondary btn-lg">
<i class="bi bi-map"></i>
View Roadmap
</a>
<a href="<?= e($router->routeUrl('/design')); ?>" class="btn btn-outline-secondary btn-lg">
<i class="bi bi-brush"></i>
View Design
</a>
<a href="<?= e($router->routeUrl('/next')); ?>" class="btn btn-outline-secondary btn-lg">
<i class="bi bi-arrow-right-square"></i>
View Next
</a>
<a href="<?= e($router->routeUrl('/agents')); ?>" class="btn btn-outline-secondary btn-lg">
<i class="bi bi-robot"></i>
View Agents
</a>
</div>
</div>
</div>
</div>
</section>
<?php renderProjectManager($router, $projectsConfig, $projectActionResult, $setupErrors); ?>
<?php renderServerRoutingStatus($rewriteStatus, $isApache, $isNginx, $isBuiltinServer); ?>
<?php
return;
}
if (!is_file($file)) {
?>
<div class="alert alert-warning">
<strong>Page not found.</strong> The file <code><?= e(basename($file)); ?></code> does not exist yet.
</div>
<?php
return;
}
$selectedProject = $selectedProject;
include $file;
}
$setupErrors = [];
foreach ([$projectsDirectory, $configDirectory] as $directory) {
$error = ensureDirectory($directory);
if ($error !== null) {
$setupErrors[] = $error;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'create_project') {
$projectActionResult = createProject(
$projectsDirectory,
$projectsConfigFile,
(string) ($_POST['project_name'] ?? ''),
(string) ($_POST['git_repository'] ?? '')
);
}
$projectsConfig = syncProjectsConfig($projectsDirectory, $projectsConfigFile);
$serverSoftware = $_SERVER['SERVER_SOFTWARE'] ?? '';
$isApache = stripos($serverSoftware, 'apache') !== false;
$isNginx = stripos($serverSoftware, 'nginx') !== false;
$isBuiltinServer = PHP_SAPI === 'cli-server';
$rewriteStatus = ensureRewriteConfiguration(__DIR__, $isApache);
$router = new Router($routes);
$path = $router->currentPath();
$routeMatch = $router->resolve($path, $projectsConfig);
$route = $routeMatch['route'];
$selectedProject = $routeMatch['project'];
$routeError = $routeMatch['error'];
if ($routeError === null && is_array($route) && ($route['file'] ?? null) === __DIR__ . '/api/kanban.php') {
include $route['file'];
exit;
}
http_response_code($route === null || $routeError !== null ? 404 : 200);
$pageTitle = $route['title'] ?? 'Not Found';
$layoutFile = __DIR__ . '/layout/index.php';
if (!is_file($layoutFile)) {
http_response_code(500);
echo 'Layout file not found: ' . e($layoutFile);
exit;
}
require $layoutFile;