-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhumanize.php
More file actions
86 lines (70 loc) · 2.44 KB
/
Copy pathhumanize.php
File metadata and controls
86 lines (70 loc) · 2.44 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
<?php
require_once __DIR__ . '/inc/Humanizer.php';
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || !isset($input['code']) || !isset($input['language'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing required fields: code, language']);
exit;
}
$code = $input['code'];
$language = $input['language'];
$intensity = isset($input['intensity']) ? (int)$input['intensity'] : 50;
$intensity = max(10, min(100, $intensity));
$validLanguages = ['javascript', 'python', 'cpp', 'java', 'php', 'csharp', 'go', 'rust', 'ruby', 'swift', 'typescript'];
if (!in_array($language, $validLanguages)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid language']);
exit;
}
if (empty(trim($code))) {
http_response_code(400);
echo json_encode(['error' => 'Code cannot be empty']);
exit;
}
$startTime = microtime(true);
$config = [
'version' => '2.0.0',
'max_code_length' => 100000,
];
if (strlen($code) > $config['max_code_length']) {
http_response_code(400);
echo json_encode(['error' => 'Code too long (max 100KB)']);
exit;
}
$humanizer = new Humanizer($config);
try {
$humanized = $humanizer->humanize($code, $language, $intensity);
$executionTime = round((microtime(true) - $startTime) * 1000, 2);
$changes = count(array_diff(explode("\n", $humanized), explode("\n", $code)));
echo json_encode([
'success' => true,
'original' => $code,
'humanized' => $humanized,
'language' => $language,
'intensity' => $intensity,
'changes' => $changes,
'execution_time' => $executionTime . 'ms',
'stats' => [
'original_lines' => count(explode("\n", $code)),
'humanized_lines' => count(explode("\n", $humanized)),
'original_chars' => strlen($code),
'humanized_chars' => strlen($humanized),
],
]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['error' => 'Humanization failed: ' . $e->getMessage()]);
}