PHP code into PHP VAR converter...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

PHP code into PHP VAR converter...

Post 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. )
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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:

Code: Select all

<?php
$string = <<<EOF
And at the end:

Code: Select all

EOF;
If you include it now, you can simply echo $string :)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

Post 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~!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why are you encoding and then decoding?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

just start output_buffering... include the file... and gather the stuff that is in the buffer.... ready :)
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

ob_get_contents() should get you started.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Check out all the ob_ functions in the PHP manual. Output buffering can be VERY useful with things like this.
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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.
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

Post 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~!
jclarkkent2003
Forum Contributor
Posts: 123
Joined: Sat Dec 04, 2004 9:14 pm

Post 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?
Post Reply