Reputation: 2813
I have a PHP file, and I am writing a script at the moment which needs to replace a function with another one. The best way I thought of doing this would be to look between the two line numbers (which will always be the same), 126 and 136, and then replace between those two lines.
What I cannot do is include any of the original function in the modifier script so I cannot just look through the file for the function name etc (I am create a script to modify another paid script that I don't own the copyright to so I cannot include any of it).
The function that I need to replace is
function upload_supports_preview($upload) {
if($upload->thumbnail != 'none') {
return true;
}
return false;
}
and I have to replace it with
function upload_supports_preview($upload) {
if($upload->type == 'video' || $upload->type == 'audio' || $upload->type == 'image') {
return true;
}
return false;
}
Upvotes: 0
Views: 171
Reputation: 10214
It doesn't sound like a very good idea to me, and this isn't a very nice solution but it should work.
$filename = '/home/xyz.php';
// this loads the file into an array
$lines = file($filename);
// edit each line of code
$lines[126] = '// put some code here';
$lines[127] = '// put some code here';
$lines[128] = '// put some code here';
$lines[129] = '// put some code here';
$lines[130] = '// put some code here';
$lines[131] = '// put some code here';
$lines[132] = '// put some code here';
$lines[133] = '// put some code here';
$lines[134] = '// put some code here';
$lines[135] = '// put some code here';
$lines[136] = '// put some code here';
// write the file back to disk
// this joins the array back into a string, using the current end of line marker from your system, which could be different to what the source file used, but will still work
file_put_contents($filename, join(PHP_EOL, $lines));
Upvotes: 1
Reputation: 42885
runkit
allows you to undefine a function at runtime, then you can redefine it the way you want to. This means you don't have to alter the script, but can inject the code you want. Check the copyright though, most likely you violate it, if the script is published under a commercial license.
Upvotes: 0