-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathrun.php
More file actions
66 lines (41 loc) · 1.61 KB
/
run.php
File metadata and controls
66 lines (41 loc) · 1.61 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
<?php
$php = FFI::load('php.h');
const ZEND_HANDLE_FILENAME = 1;
const ZEND_HANDLE_FP = 2;
const ZEND_HANDLE_STREAM = 3;
const PHP_PLUS_EXTENSION = '.plus';
const OPEN_TAG = '<?php' . PHP_EOL;
// When a file will be opened by the PHP Engine, it checks if the
// function zend_stream_open_function is defined (default is undefined)
// if the function is defined, the engine calls it instead of using the
// default function.
$php->zend_stream_open_function = function($filename, $handle) use ($php) {
$handle->type = ZEND_HANDLE_STREAM;
// We are using isatty to be able to read each char of the file and
// append the open tag if necessary
$handle->handle->stream->isatty = 1;
$file = fopen($filename, 'r');
$filenameLength = strlen($filename);
// is the file extension .plus?
$extension = substr($filename, $filenameLength - strlen(PHP_PLUS_EXTENSION), strlen(PHP_PLUS_EXTENSION));
$isPhpPlus = $extension === PHP_PLUS_EXTENSION;
$currentChar = 0;
$handle->handle->stream->reader = function($handle, $buf, $sizeOfBuf) use (&$currentChar, $file, $isPhpPlus) {
// Appends the open tag at the beginning of the file
if ($isPhpPlus && $currentChar < strlen(OPEN_TAG)) {
$char = OPEN_TAG[$currentChar++];
FFI::memcpy($buf, $char, $sizeOfBuf);
return true;
}
// Reads the file
if ($char = fread($file, $sizeOfBuf)) {
FFI::memcpy($buf, $char, $sizeOfBuf);
return true;
}
// EOF
return false;
};
};
include 'hello.php';
include 'hello.plus';
// var_dump($php);