Mostafa Elkady
Mostafa Elkady

Reputation: 5801

check the source of a file

i want php code to open a file and search in it for some dangerous words like

if(preg_match("/(echo|zend|print|print_r|phpinfo|
symlink|ini_set|telnet|cgi|
eval|base64_encode|base64_decode)/iU",$filesource)){
echo 'error';
}

how i can make it

Upvotes: 0

Views: 105

Answers (2)

thomasmalt
thomasmalt

Reputation: 1752

pretty much like you outlined :)

But have you looked at tools like phpcs and phpcpd?

You can open up any php file like a regular text file and parse through it without including it in your script.

<?php
$filedata = file_get_contents('/path/to/file.php');
if (preg_match('/foo/', $filedata) {
    echo "file contained 'foo'\n";
}

Upvotes: 0

Gumbo
Gumbo

Reputation: 655559

If you want to parse PHP code, use token_get_all to get an array of language tokens.

Upvotes: 3

Related Questions