I need to process some user-provided code on the server using PHP. The code is about to cover some very basic programming capabilities, for example: variables, literals, (preferably) functions, and some associated operations.
An option is to use the dangerous function of eval()
. For my specific case, it's overwhelmingly & redundantly full featured, apart from its security issues and performance bottlenecks. Sanitizing the tokens using token_get_all()
protects against Murphy, not Machiavelli! Regardless of its downsides, it's truly capable of what I'm tiring to achieve.
I've also checked the Symphony's ExpressionLanguage; it has some shortcomings:
- it cannot detect the "variables" on its own (they should be introduced and known beforehand)
- it lacks basic variable functionalities (only initializes them: no assignment functionality)
- it's designed only for "one liner" expressions
Alas! a more sophisticated ExpressionLanguage would've sufficed.
I'm looking for something that allows some very basic "programming" capability to the users. Is there such a thing, if so, what is it? (even though it's written in another language, but is utilizable somehow on a server.)
If such a thing is not around, then how should I treat the eval()
to not to drawn me?! Or, as a last-resort, how may I design a such a simple programming capability? (Please elaborate on the matters :)
As per comments below, here is a list of "programming" features the code syntax needs to support. It would've sufficed if the followings were supported in addition to what the ExpressionLanguage systax provides:
- The sequence flow: executing the instruction one after another (contrary to the "one liner" nature of the ExpressionLanguage)
- Local variable declaration (and their detections afterwards, of course)
- Variable assignments from expressions (any composition of literals, function invocations, operators)
- Passing variables to functions
- Flow control constructs: at least a conditional construct (e.g:
if
) and a repetition construct (e.g:for
loop)