/
knit.php
192 lines (145 loc) · 4.29 KB
/
knit.php
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
<?php
####
# Given a query like '?package=version/name.js' or '?package=version/name.css'
# will dynamically pull together all the files to make that package.
#
# Uses the packages.xml file to decide which files belong to which packages.
#
# To keep debugging code use ?package=version/name.debug.js
# Debugging code in content is delimited by /*!debug*/ and /*gubed!*/
#
# To add a delay (seconds) use ?package=version/name.js&delay=3
#
# To knit a file into the middle of another add the following to the content:
# /*!include:filename.js*/
####
/**** configure ****/
$conf = array(
'path_to_packagelist' => 'packages.xml',
'path_to_root' => '../'
);
/**** main ****/
function errorHandler($msg) {
$msg = str_replace("\\", "\\\\", $msg);
$msg = str_replace("'", "\\'", $msg);
$msg = preg_replace("/[\r\n]/", "\\n", $msg);
echo "window.console && console.log('Knit Error: $msg.');\n";
exit(1);
}
error_reporting(E_ALL);
ini_set('display_errors', 0);
set_exception_handler('errorHandler');
$args = getSafeArgs($_GET); // note: $_GET is already URL decoded
$packages = getPackages($conf['path_to_packagelist']);
if ( empty($args['package']) ) {
throw new Exception('Required GET parameter "package" is empty.');
}
list($version, $packageName, $debug, $type) = parsePackageName($args['package']);
if ($version == '@SRC@') { $version = 'src'; }
$files = getFiles($packages, $packageName, $type);
if ( !empty($args['delay']) ) {
sleep(min($args['delay'], 10)); // maximum sleepiness == 10
}
printHeader($type);
$content = knitFiles($files, $conf['path_to_root'] . $version . '/');
if (!$debug) {
$content = preg_replace(':/\*!debug\*/.*?/\*gubed!\*/:s', '', $content);
}
print($content);
/**** functions ****/
/**
*/
function getSafeArgs($unsafe_args) {
$safe_args = array();
if ( !empty($unsafe_args['package']) ) {
if (!preg_match('!([^/]+)/[a-z.]+\.(js|css)$!', $unsafe_args['package'])) {
throw new Exception('Argument "package" ' . $unsafe_args['package'] . ' has unsafe characters and cannot be used.');
}
else {
$safe_args['package'] = $unsafe_args['package'];
}
}
if ( !empty($unsafe_args['delay']) ) {
if (!preg_match('!^\d+$!', $unsafe_args['delay'])) {
throw new Exception('Argument "delay" ' . $unsafe_args['delay'] . ' must be an integer so cannot be used.');
}
else {
$safe_args['delay'] = $unsafe_args['delay'];
}
}
return $safe_args;
}
/**
*/
function parsePackageName($package) {
list($version, $package) = explode('/', $package);
$parts = explode('.', $package);
$type = array_pop($parts);
$debug = 0;
if ($parts[count($parts)-1] == 'debug') {
$debug = 1;
array_pop($parts);
}
$name = implode('.', $parts);
return array($version, $name, $debug, $type);
}
/**
*/
function getPackages($xmlPath) {
if ( !file_exists($xmlPath) || !is_readable($xmlPath) ) {
throw new Exception("Cannot read any file at '$xmlPath'.");
}
try {
$packages = simplexml_load_file($xmlPath);
}
catch(Exception $e) {
throw new Exception("The file at '$xmlPath' cannot be parsed as XML.");
}
return $packages;
}
/**
*/
function getFiles($packages, $package_name, $type) {
@$file_list = $packages->xpath("/packages/$package_name/$type");
if ( empty($file_list) ) {
throw new Exception("No data is defined for the package named '$package_name'.");
}
$files = array();
foreach ($file_list[0] as $file) {
$files[] = preg_replace('/^src\//', '', $file); // 'src' will be replaced by the root+version
}
return $files;
}
/**
*/
function printHeader($type) {
switch ($type) {
case 'js':
header('Content-Type: text/javascript');
break;
case 'css':
header('Content-Type: text/css');
break;
}
}
/**
*/
function knitFiles($files, $base='') {
$content = '';
foreach ($files as $file) {
$path = $base . $file;
if ( !file_exists($path) || !is_readable($path) ) {
throw new Exception("Cannot read any file at '$path'.");
}
$content .= file_get_contents($base . $file) . "\n";
}
$content = parseInclude($content, $base);
return $content;
}
/**
*/
function parseInclude($content, $base='') {
$replacer = create_function('$args', 'return file_get_contents("'.$base.'include/".$args[1]);');
$content = preg_replace_callback('/\/\*!include:(.+?)\*\//', $replacer, $content);
return $content;
}