Skip to content

Commit bce1f7b

Browse files
Update index.php for better logging and debug checks
1 parent 8face8f commit bce1f7b

File tree

1 file changed

+73
-42
lines changed

1 file changed

+73
-42
lines changed

index.php

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,81 @@
11
<?php
22
header('Content-Type: application/json');
3-
header('Access-Control-Allow-Origin: *'); // Allow calls from your WP site
3+
header('Access-Control-Allow-Origin: *');
44
header('Access-Control-Allow-Methods: POST');
55
header('Access-Control-Allow-Headers: Content-Type');
66

7-
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
8-
$input = json_decode(file_get_contents('php://input'), true);
9-
$code = $input['code'] ?? '';
10-
$rules = $input['rules'] ?? '';
11-
12-
if (empty($code)) {
13-
http_response_code(400);
14-
echo json_encode(['error' => 'No code provided']);
15-
exit;
16-
}
17-
18-
// Save code to temp file
19-
$tempCodeFile = sys_get_temp_dir() . '/' . uniqid() . '.java';
20-
file_put_contents($tempCodeFile, $code);
21-
22-
// Save rules to temp XML
23-
$tempRulesFile = sys_get_temp_dir() . '/' . uniqid() . '.xml';
24-
file_put_contents($tempRulesFile, $rules ?: 'rulesets/java/quickstart.xml'); // Fallback to basic rules if none provided
25-
26-
if (!function_exists('shell_exec')) {
27-
http_response_code(500);
28-
echo json_encode(['error' => 'shell_exec is disabled']);
29-
exit;
30-
}
31-
32-
// Run PMD via shell_exec
33-
$pmdOutput = shell_exec('pmd check -d ' . escapeshellarg($tempCodeFile) . ' -R ' . escapeshellarg($tempRulesFile) . ' -f json 2>&1');
34-
35-
// Cleanup
36-
unlink($tempCodeFile);
37-
unlink($tempRulesFile);
38-
39-
if (empty($pmdOutput)) {
40-
echo json_encode(['violations' => []]);
41-
} else {
42-
$issues = json_decode($pmdOutput, true);
43-
$violations = $issues['files'][0]['violations'] ?? [];
44-
echo json_encode(['violations' => $violations]);
45-
}
46-
} else {
7+
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
478
http_response_code(405);
489
echo json_encode(['error' => 'Method not allowed']);
10+
exit;
11+
}
12+
13+
$input = json_decode(file_get_contents('php://input'), true);
14+
$code = $input['code'] ?? '';
15+
$rules = $input['rules'] ?? '';
16+
17+
if ($code === '') {
18+
http_response_code(400);
19+
echo json_encode(['error' => 'No code provided']);
20+
exit;
21+
}
22+
23+
/* ---------- 1. Write source file ---------- */
24+
$srcFile = sys_get_temp_dir() . '/' . uniqid('src_', true) . '.java';
25+
file_put_contents($srcFile, $code);
26+
27+
/* ---------- 2. Write rules file ---------- */
28+
if ($rules !== '' && stripos($rules, '<?xml') === 0) {
29+
$rulesFile = sys_get_temp_dir() . '/' . uniqid('rules_', true) . '.xml';
30+
file_put_contents($rulesFile, $rules);
31+
} else {
32+
// fallback to the tiny rule set shipped with the image
33+
$rulesFile = '/var/www/html/fallback-rules.xml';
34+
}
35+
36+
/* ---------- 3. Verify PMD is reachable ---------- */
37+
$ver = shell_exec('pmd --version 2>&1');
38+
if (trim($ver) === '') {
39+
@unlink($srcFile);
40+
@unlink($rulesFile ?? '');
41+
http_response_code(500);
42+
echo json_encode(['error' => 'PMD binary not found']);
43+
exit;
4944
}
50-
?>
45+
46+
/* ---------- 4. Build the exact command (full path) ---------- */
47+
$cmd = sprintf(
48+
'/usr/local/bin/pmd check -d %s -R %s -f json 2>&1',
49+
escapeshellarg($srcFile),
50+
escapeshellarg($rulesFile)
51+
);
52+
53+
/* ---------- 5. Log the command (visible in Render logs) ---------- */
54+
error_log("PMD COMMAND: $cmd");
55+
56+
/* ---------- 6. Execute PMD ---------- */
57+
$pmdRaw = shell_exec($cmd);
58+
error_log("PMD RAW OUTPUT: " . ($pmdRaw ?: '(empty)'));
59+
60+
/* ---------- 7. Clean up temporary files ---------- */
61+
@unlink($srcFile);
62+
if (isset($rulesFile) && strpos($rulesFile, sys_get_temp_dir()) === 0) {
63+
@unlink($rulesFile);
64+
}
65+
66+
/* ---------- 8. Parse JSON output ---------- */
67+
if ($pmdRaw === null || $pmdRaw === '') {
68+
echo json_encode(['violations' => [], 'debug' => 'PMD returned no data']);
69+
exit;
70+
}
71+
72+
$pmdJson = json_decode($pmdRaw, true);
73+
if (json_last_error() !== JSON_ERROR_NONE) {
74+
http_response_code(500);
75+
echo json_encode(['error' => 'PMD JSON parse error', 'raw' => $pmdRaw]);
76+
exit;
77+
}
78+
79+
/* ---------- 9. Return only the violations ---------- */
80+
$violations = $pmdJson['files'][0]['violations'] ?? [];
81+
echo json_encode(['violations' => $violations, 'debug' => 'OK']);

0 commit comments

Comments
 (0)