My and my group are trying to create a simple PHP code editor that also executes the code without the use of a library.
We know the risks that come with eval(), but since it's all running on a local server it shouldn't be a problem.
The Regex part is definitely the problem since we have no clue how that works and because it's copied from the internet.
What we are trying to do using Regex is to limit the input to certain words/characters/numbers etc. The problem is that it will fail when trying the input contains words like "Echo"
<textarea id="area" cols="70" rows="30"></textarea>
<button id="submit">Submit</button>
<script>
$('#submit').click(function (e) {
e.preventDefault();
var info = $('#area').val();
$.ajax({
type: "POST",
url: 'pages/assignments/response.php',
data: {
area: info
},
success: function (response) {
console.log(response);
}
});
});
</script>
<?php
if (!empty($_POST['area'])) {
runEval($_POST['area']);
};
function runEval($data)
{
$characters = '[a-zA-Z0-9]*';
$functions = '';
$operators = '[\w-><$(){}|_+=":;!&*%$]';
$regexp = '/^((' . $characters . '|' . $functions . '\s*\((?1)+\)|\((?1)+\))(?:' . $operators . '(?2))?)+/';
if (preg_match($regexp, $data)) {
eval('$result = ' . $data . ';');
echo $result;
} else {
return false;
}
}
?>