Search code examples
phpphp-extension

Manipulate PHP-Files before they are parsed


I have managed to write a small, compilable extension for php, that prints "test" on every ZEND_RINIT.

I tested it with an php-file that loads multiple other files, which again load others [...]. The problem is, that the extension just prints "test" once, so I assume it does not fire each time a new file is loaded, how can i get my extension to do so?

Also the event is fired before the file is loaded, that is what i want, but therefore zend_get_executed_filename() is empty and I am not able to get the file content...

My final goal is to validate each file before executing the script. I planned on doing so by validating a file signature that is appended to the file.

Pseudocode Validation:

decrypt(signature, rsa.pub) = sha(filecontent)

Pseudocode Signing:

signature = encrypt(sha(filecontent), rsa.priv)
file += signature

Or is there an even better way to validate the files (i want them to be signed) before executing the script in them?

Thanks in advance!


Solution

  • ZEND_RINIT is called on request startup, that is when you execute your php-file. It will never be called again during execution of your PHP file. Only on next startup. You have to find a way to hook into all of the file load functions of PHP.