-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinstructor
executable file
·40 lines (33 loc) · 1.2 KB
/
instructor
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
#!/usr/bin/env php
<?php
// Ensure script name is provided
if ($argc < 2) {
fwrite(STDERR, "Instructor CLI\n");
fwrite(STDERR, "Usage: instructor <script> [args...]\n");
fwrite(STDERR, "\n");
fwrite(STDERR, " Scripts:\n");
fwrite(STDERR, " setup - publish Instructor bundled assets to your project dir\n");
fwrite(STDERR, " hub - view and execute Instructor examples\n");
fwrite(STDERR, "\n");
exit(1);
}
$script = $argv[1];
// Sanity check: prevent directory traversal and ensure valid script name
if (strpos($script, '..') !== false || !preg_match('/^[a-zA-Z0-9_-]+$/', $script)) {
fwrite(STDERR, "Invalid script name.\n");
exit(1);
}
// Determine the path to the script
$binDir = __DIR__;
$scriptPath = realpath($binDir . '/../scripts/' . $script . '.php');
if (!$scriptPath || !file_exists($scriptPath)) {
fwrite(STDERR, "Script '$script' not found.\n");
exit(1);
}
// Remove the script name from arguments
$args = array_slice($argv, 2);
// Update $_SERVER['argv'] and $_SERVER['argc'] for the included script
$_SERVER['argv'] = array_merge([$scriptPath], $args);
$_SERVER['argc'] = count($_SERVER['argv']);
// Include and execute the target script
require $scriptPath;