-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
149 lines (124 loc) · 4.11 KB
/
Copy pathapi.php
File metadata and controls
149 lines (124 loc) · 4.11 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
<?php
/**
* XSLT Pipeline API
*
* RESTful API for applying sequential XSLT 3.0 transformations
*/
header('Content-Type: application/xml; charset=utf-8');
require_once __DIR__ . '/src/transformer.php';
// Get request method
$method = $_SERVER['REQUEST_METHOD'];
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Extract path components
$pathParts = array_filter(explode('/', $path));
// Simple router
if (end($pathParts) === 'transform') {
switch ($method) {
case 'GET':
handleGetTransform();
break;
case 'POST':
handlePostTransform();
break;
default:
sendError(405, 'Method not allowed');
}
} else {
sendError(404, 'Endpoint not found');
}
/**
* Handle GET /transform?lemma-id=...&target=...
*/
function handleGetTransform() {
$lemmaId = isset($_GET['lemma-id']) ? trim($_GET['lemma-id']) : null;
$target = isset($_GET['target']) ? trim($_GET['target']) : null;
if (!$lemmaId || !$target) {
sendError(400, 'Missing required parameters: lemma-id, target');
return;
}
performTransform($lemmaId, $target);
}
/**
* Handle POST /transform with JSON body
*/
function handlePostTransform() {
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
sendError(400, 'Invalid JSON in request body');
return;
}
$lemmaId = isset($input['lemma-id']) ? trim($input['lemma-id']) : null;
$target = isset($input['target']) ? trim($input['target']) : null;
if (!$lemmaId || !$target) {
sendError(400, 'Missing required fields in JSON: lemma-id, target');
return;
}
performTransform($lemmaId, $target);
}
/**
* Perform the actual transformation
*/
function performTransform($lemmaId, $target) {
try {
$baseDir = dirname(__FILE__);
// Validate inputs - prevent directory traversal
if (preg_match('/[\/\\.]/', $lemmaId) || preg_match('/[\/\\.]/', $target)) {
sendError(400, 'Invalid lemma-id or target format');
return;
}
$inputFile = $baseDir . DIRECTORY_SEPARATOR . 'input' . DIRECTORY_SEPARATOR . $lemmaId . '.xml';
$transformDir = $baseDir . DIRECTORY_SEPARATOR . 'transforms' . DIRECTORY_SEPARATOR . $target;
// Check if files exist
if (!file_exists($inputFile)) {
sendError(404, "Input file not found: input/$lemmaId.xml");
return;
}
if (!is_dir($transformDir)) {
sendError(404, "Transform directory not found: transforms/$target");
return;
}
// Create transformer and perform transformation
$transformer = new Transformer();
$result = $transformer->transform($inputFile, $transformDir, true);
$contentType = getContentTypeForTarget($target);
if (!headers_sent() && PHP_SAPI !== 'cli') {
header('Content-Type: ' . $contentType);
}
// Return XML result
echo $result;
} catch (Exception $e) {
sendError(500, 'Transformation error: ' . $e->getMessage());
}
}
/**
* Send error response
*/
function sendError($statusCode, $message) {
http_response_code($statusCode);
echo sprintf('<?xml version="1.0" encoding="UTF-8"?><error><code>%d</code><message>%s</message></error>',
$statusCode,
htmlspecialchars($message)
);
}
function getContentTypeForTarget($target) {
$extension = substr($target, strrpos($target, '-') + 1);
return getContentTypeForExtension($extension);
}
/**
* Get content type based on file extension
*
* @param string $extension
* @return string
*/
function getContentTypeForExtension($extension) {
switch (strtolower($extension)) {
case 'xml':
return 'application/xml';
case 'csv':
return 'text/csv';
case 'tsv':
return 'text/tab-separated-values';
default:
return 'application/octet-stream';
}
}