Page 1 of 1
PHP code into PHP VAR converter...
Posted: Sun May 22, 2005 12:39 am
by jclarkkent2003
Hi guys,
I have a script I wrote, and it's about 40 KB in size, and it's nearly HALF html and HALF php code.
I need to be able to turn this ENTIRE 40 KB file, into one LONG PHP string. The reasons for doing this are my own reasons, but I just need to be able to have the ENTIRE file, in a PHP string var like $file_string.
So if I did <? echo $file_string; ?> , it would be the EXACT same as if I just did <? include $external_40kb_file; ?>.
Can I get some assistance.
Also, after that, if I could run a script on the $file_string var, and remove ALL tags, so I can see the code as search engines would see it, that would be lovely.
Code: Select all
############### The file allready exists
if(file_exists($basedir.$filelocation))
{
$lastaction = "$file allready exists.";
html_header();
displaydir();
}
############### Give the user a textarea to write the contents of file
else
{
$lastaction = "Creating $file";
html_header();
echo "<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">\n";
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"file\" VALUE=\"$file\">\n";
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"createfile\">\n";
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"wdir\" VALUE=\"$wdir\">\n";
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"done\" VALUE=\"1\">\n";
echo "<TEXTAREA NAME=\"code\" rows=\"$textrows\" cols=\"$textcols\">\n";
############### The user selected to use a html template. Put it inside the textarea
if(isset($html))
{
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n\n";
echo "<html>\n";
echo "<head>\n\n";
echo "<title>Untitled</title>\n";
echo "</head>\n";
echo "<body>\n\n";
echo "</body>\n";
echo "</html>";
}
echo "</TEXTAREA><BR>\n";
echo "<center><INPUT TYPE=\"SUBMIT\" NAME=\"confirm\" VALUE=\"Create\">\n";
echo "<INPUT TYPE=\"SUBMIT\" NAME=\"cancel\" VALUE=\"Cancel\"><br>";
$ext = strrchr ( $file , "." );
if(!strcasecmp ($ext, ".txt"))
{
echo "<input type=\"checkbox\" name=\"convert\" value=\"yes\"><font size =\"-2\" face=\"arial, helvetica\">(convert line returns to BR)</font> ";
}
echo "</center><BR>\n</FORM>";
}
}
echo $html_ender;
break;
}
?>
This is only a VERY VERY small portion of the 40 kb file I need to make into a string var. ( I want to base64_encode this $string var is just ONE of the reasons for having the entire file's code in a php string var. )
Posted: Sun May 22, 2005 12:49 am
by php_wiz_kid
Well, if you opened the file like this:
Code: Select all
$filename = "external_40kb_file.php";
$file_open = fopen($filename, 'r');
$file_string = fread($file_open, filesize($filename));
echo $file_string;
This would store the contents of the PHP file into a string. I don't think you'd be able to echo it and it would show the actual data. It would just output it all as text.
I don't know if you can do this. I'm sure there is a way but it might be super complicated. Just try this and see what it does for you.
Posted: Sun May 22, 2005 6:46 am
by timvw
The easiest to get the file contents in a string is with
http://www.php.net/file_get_contents
Code: Select all
$string = file_get_contents('whatever.file');
With heredoc (
http://php.belnet.be/manual/en/language ... ax.heredoc) you can make things easy too:
Just add the following to the beginning of your file:
And at the end:
If you include it now, you can simply echo $string

Posted: Sun May 22, 2005 9:59 am
by phpScott
instead of echo "some text";
do
Code: Select all
$big_string_var = "";
$big_string_var .= "some text";
$big_string_var .= "some more text";
will get everything into $big_string_var to do what you want with.
Posted: Sun May 22, 2005 11:22 am
by jclarkkent2003
thanks for help, I got it.
timvw was right on the money with the command, however I did NOT need the <<EOF and EOF>>, that just gave me errors and with php wiz kid coming in behind, thanks all you guys for helping me out.
This is the CORRECT code that works perfectly.
FILENAME TEST.php and the 40kb file is named "php.txt"
Code: Select all
<?
$string = file_get_contents('php.txt');
$string2 = base64_encode($string);
eval(base64_decode($string2));
?>
Aside from that working, you guys find any problems with using EVAL ? I actually disabled Eval() on my server so it will be more secure, but I don't know any other ways to make it work, ECHO'ing it did as you guys expected, it showed it in text and didn't parse as php.
You guys know of any more commands that would "parse" it like eval, so I don't have to re-enable eval. would System() and/or any other comands work? (they are disabled to but I'd like to know ALL my options)
Peacez~!
Posted: Sun May 22, 2005 11:42 am
by John Cartwright
Why are you encoding and then decoding?
Posted: Sun May 22, 2005 1:39 pm
by timvw
just start output_buffering... include the file... and gather the stuff that is in the buffer.... ready

Posted: Sun May 22, 2005 7:06 pm
by jclarkkent2003
well i'm not going to be encoding then decoding in the same script, i'm going to encode, and copy the encoded string into a file and then decode it, so the decoded version is never really shown.
I have a purpose for it, don't worry about, I am just trying to make my free script i'm going to pass around, much harder to "rip" for someone to see the code as possible, but I cannot use zend, ion cube or things like that, it has to be 100% decodable on ANY server with basic php installed (installing zend / ion cube or thigns like that is too much for this free script).
Cheers.
Timvw, I'm not sure I have any idea what your talking about output-buffering, include the file, and gather whats in the buffer?
Are you talking about how to read what's encoded? Example?
Posted: Sun May 22, 2005 7:29 pm
by John Cartwright
ob_get_contents() should get you started.
Posted: Sun May 22, 2005 9:53 pm
by php_wiz_kid
Check out all the ob_ functions in the PHP manual. Output buffering can be VERY useful with things like this.
Posted: Sun May 22, 2005 10:07 pm
by php_wiz_kid
To start output buffering use the ob_start() function. When this is activated nothing is sent from the script except headers (getting this from the manual). Everything is stored in an internal buffer.
Nothing will be output until you end and flush the buffer using ob_end_flush(). If you want to exit the buffer and not use the contents of the buffer then use ob_end_clean(). If you want to flush or clean the contents of the buffer but you don't want to exit the buffer then use ob_flush() or ob_clean() accordingly.
If you want to store the contents of the buffer then use ob_get_contents().
ob_get_contents() is nice if you want to end the buffer but you don't want to use the contents of the buffer right away. Here's an example.
Code: Select all
ob_start();
echo "cool";
$output_buffer = ob_get_contents();
ob_end_clean();
//Do whatever you want before you want to output the contents of the buffer.
echo $output_buffer;
I hope this helps with buffers.
Posted: Mon May 23, 2005 1:11 am
by jclarkkent2003
ok thanks, I get how buffers work, i read this page:
http://us3.php.net/manual/en/function.ob-start.php
but the thing i don't get, is how does it relate with what I am trying to do (I'm trying to ENCODE a php script without using ion cube or zend or any other special install thing) and make it as hard as possible to decode BUT not impossible (nothing's impossible).
So far, I did this:
Code: Select all
<?
$string = file_get_contents('php.txt');
$string2 = base64_encode($string);
eval(base64_decode($string2));
?>
and it works, and the code I distrubute FREE looks like this:
Code: Select all
<?
$string2 = "THISISWHEREASUPERLONGBASE64STRINGISSTORED;
eval(base64_decode($string2));
?>
What can I do with buffers and this? I'm thinking maybe a WAY that I can have it CALL or INCLUDE or REQUIRE a config.txt from MY server, which has the DECODE key and maybe some other variables like time, so key's are only good for a few days and then the script cannot be decoded (they must redownload the new version) or something, maybe a way to encode the base64 'ed encoded string (since it's all numbers and letters and symbols, is there a way I can encode that further maybe with a PASSWORD or something, I could put a table in the script which based on time will rotate passwords and verify with my config.txt on MY server and if they match, it decodes and run, otherwise it wont' run and can't be decoded since they don't have the password?
Thanks~!
Posted: Mon May 23, 2005 4:31 pm
by jclarkkent2003
ok, I tested the code
Code: Select all
<?
ob_start();
echo base64_decode("Z2xvYmFsICRob21lLCAkdGFyZ2V0OwoKJGJhc2VkaXIgPSAkRE9DVU1FT==");
$output_buffer = ob_get_contents();
ob_end_clean();
//Do whatever you want before you want to output the contents of the buffer.
echo $output_buffer;
?>
and it showed the code as text, and did NOT parse it (please note, that base64 encoded portion is not the real thing, the real thing is VERY long and I could not fit it in here.
So what would I be able to use buffers for?