You are not logged in.
#1 01 Sep 2007 2:51 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Automated File Changes
I have completed the first functional version of the "PHP Menu Mod" for phpBB2, made from the menu system MadHatter helped me make, but now creates a menu on each viewable page of the forum. The issue is though, that to achieve this goal, I had to change each individual .tpl file (template file) as well as each corresponding .php file for the template files. Resulting in about 20 or 30 files all together, as of where a line of code had to be put in to fit into the php arrays that phpBB uses, and there can be as many as 4-6 different arrays in each file. So installing this mod manually is somewhat of a horrible project.
Therefore, I need a method of automating this process on freshly installed phpBB2 forums. The one thing I recognized though, is that all the arrays have left an open space in the middle of their variables, where the menu variable fits nicely in. Any suggestions for how to achieve this are welcome
Offline
#2 01 Sep 2007 10:51 am
Re: Automated File Changes
you can create a patch script. take an unaltered phpbb2 and your modified version and run it through diff (there are several versions of diff out there, but the one on your linux server should work fine. if you don't have a *nix server then there are several diff programs out there that are great: beyond compare being the best I've ever seen) to create a diff file. with that file you can run patch passing in the diff file you created and it will patch any (same version of phpbb) unaltered phpbb2 install out there. it will most likely patch more than just the same version, unaltered version, but thats all you should support IMO.
patch only works if the user is on a nix machine that has patch installed (it is one of the default utilities that comes with *nix distro's). if they're on windows, then it won't work.
I'd recommend bundling the altered files with the instructions on how to patch them (the line by line replacement methodology) and a patch script. this way if the user can choose what method they want to use. personally I prefer the fully altered files because it allows me to fire up BC and patch the files by eye myself. In the past (for things like upgrades) I've used patch (with phpnuke's version of phpbb) and its typically always worked good.
the other option would be to create a php patch script which would read in the files to be altered, create a backup of the original, insert your modifications (using a regular expression to find / add / modify the original script) and save. this would probably be the easiest for your users to use, and safest from their perspective, but would be the most difficult one for you to do IMO (and if you make an error it could fubar their install).
Offline
#3 01 Sep 2007 3:35 pm
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
I suppose I'll go for safety/functionality, that being a script to find, add and modify. I ran through google and found a quick tutorial on creating pages with php (fwrite) and quickly had the code I need to make the phpBB seperate pages, but if I want to use php to find a specific multi-lined piece of code/text, and replace it with another piece of multi-lined code/text, what function would I be using then? I mean, obviously its combination of using fwrite and the \n (for multiple lines) word, but what functions allows me to search through a file for a specific piece of code and then replace it with something else?
I figure that if I get this replace system working, I'll just track down my own modifications, run some tests and it should work.
EDIT: More googling (wonderful isn't it?) got me to discover the str_replace function, which I used for this script:
Code:
<?php $file1 = "edit.php"; $fh = fopen($file1, 'w'); $cat = str_replace("cat", "house", "I have a cat\n , it is brown"); echo $cat; $stringData = $cat; fwrite($fh, $stringData); fclose($fh); ?>
That changed:
I have a cat
, it is brown
Into:
I have a house
, it is brown
So I'll start experimenting some on the edits, see if it will work large scale to edit all the appropriate files (more or less all files in phpBB) when executed.
Last edited by Butcher (01 Sep 2007 3:50 pm)
Offline
#4 01 Sep 2007 4:55 pm
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Ok, ran into my first issue:
I have two test variables, which look like this:
$1:
Code:
$1 = " );\n \n $template->assign_vars(array(\n 'TOTAL_POSTS' => sprintf($l_total_post_s, $total_posts),\n 'TOTAL_USERS' => sprintf($l_total_user_s, $total_users),\n 'NEWEST_USER' => sprintf($lang['Newest_user'], '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . '">', $newest_user, '</a>'), \n \n 'FORUM_IMG' => $images['forum'],\n 'FORUM_NEW_IMG' => $images['forum_new'],\n 'FORUM_LOCKED_IMG' => $images['forum_locked'],\n \n 'L_FORUM' => $lang['Forum'],\n 'L_TOPICS' => $lang['Topics'],\n 'L_REPLIES' => $lang['Replies'],\n 'L_VIEWS' => $lang['Views'],\n 'L_POSTS' => $lang['Posts'],\n 'L_LASTPOST' => $lang['Last_Post'], \n 'L_NO_NEW_POSTS' => $lang['No_new_posts'],\n 'L_NEW_POSTS' => $lang['New_posts'],\n 'L_NO_NEW_POSTS_LOCKED' => $lang['No_new_posts_locked'], \n 'L_NEW_POSTS_LOCKED' => $lang['New_posts_locked'], \n 'L_ONLINE_EXPLAIN' => $lang['Online_explain'], \n 'L_MODERATOR' => $lang['Moderators'], \n 'L_FORUM_LOCKED' => $lang['Forum_is_locked'],\n 'L_MARK_FORUMS_READ' => $lang['Mark_all_forums'], \n \n 'U_MARK_READ' => append_sid("index.$phpEx?mark=forums"))\n );";
$2:
Code:
$2 = " );\n // Menu Mod\n $contents = trim(file_get_contents("menu.php"));\n $content = preg_replace(array("/^\<\?php/", "/\?\>$/"), array("",""), $contents);\n eval($contents);\n // Menu Mod\n $template->assign_vars(array(\n 'TOTAL_POSTS' => sprintf($l_total_post_s, $total_posts),\n 'TOTAL_USERS' => sprintf($l_total_user_s, $total_users),\n 'NEWEST_USER' => sprintf($lang['Newest_user'], '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . '">', $newest_user, '</a>'), \n // Menu Mod \n 'EXTRA_MENU' => $menu.php,\n // Menu Mod \n 'FORUM_IMG' => $images['forum'],\n 'FORUM_NEW_IMG' => $images['forum_new'],\n 'FORUM_LOCKED_IMG' => $images['forum_locked'],\n \n 'L_FORUM' => $lang['Forum'],\n 'L_TOPICS' => $lang['Topics'],\n 'L_REPLIES' => $lang['Replies'],\n 'L_VIEWS' => $lang['Views'],\n 'L_POSTS' => $lang['Posts'],\n 'L_LASTPOST' => $lang['Last_Post'], \n 'L_NO_NEW_POSTS' => $lang['No_new_posts'],\n 'L_NEW_POSTS' => $lang['New_posts'],\n 'L_NO_NEW_POSTS_LOCKED' => $lang['No_new_posts_locked'], \n 'L_NEW_POSTS_LOCKED' => $lang['New_posts_locked'], \n 'L_ONLINE_EXPLAIN' => $lang['Online_explain'], \n 'L_MODERATOR' => $lang['Moderators'], \n 'L_FORUM_LOCKED' => $lang['Forum_is_locked'],\n 'L_MARK_FORUMS_READ' => $lang['Mark_all_forums'], \n \n 'U_MARK_READ' => append_sid("index.$phpEx?mark=forums"))\n );";
Both are more or less identical, with this function to be applied:
$repl = str_replace($1, $2, $1);
echo $repl;
The problem is though, I get a Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' error, most likely because he won't handle the code within the "", how can I make him read the information within the variables as purely text that needs processing, rather than code that might be incorrect?
Offline
#5 01 Sep 2007 5:34 pm
Re: Automated File Changes
$1 = "every quote in here needs to be escaped";
ex:
$1 = "something \"here\" needs quoting";
there is another way of entering string text into a variable using "here documents" which look like:
$2 = <<< HEREDOC
this does not need to be escaped "" and can have as many ""'s and new
lines in it
as you like. no need to type \n anywhere because this
is a new line.
HEREDOC;
there are a few things to keep in mind about using here docs. first the identifier after the <<< has to match the end (I used "HEREDOC" but it can be whatever without spaces). the end identifier must be on a new line by itself, and must be the first thing on the line (no indenting or spaces). if you include a variable in there, it will print the literal text (not the value of the variable) so "$1" printed inside the here doc syntax will literally print "$1" and not whatever 1 contained.
Offline
#6 02 Sep 2007 5:01 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Hmm, I changed the name and content of $1 to $nr1 and removed all the /n, then wrote it as:
$nr1 = <<< HEREDOC1
stuff without /n
HEREDOC1;
but recieved a Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
on this line:
'NEWEST_USER' => sprintf($lang['Newest_user'], '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . '">', $newest_user, '</a>'),
Does that mean that I used the HEREDOC1 wrong? Because when looking in dreamweaver, it marks it as php still, and not like plain text.
Offline
#7 05 Sep 2007 12:32 am
Re: Automated File Changes
oops, heredoc's will parse variables and constants ($lang and POST_USERS_URL). so it looks like you do have to escape the quotes after all. its been a while since I used heredoc's and had it confused with .net's verbatim identifier for strings which takes the literal value of the string.
you can escape variables by using \$ for variable names, but it will try to find & replace constant values in your string, so forget I mentioned it use a single quoted string and escape other single quotes, or replace single quotes within your string with double quotes.
so
Code:
$some_var = <<< HEREDOC 'NEWEST_USER' => sprintf($lang['Newest_user'], '<a href="' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . '">', $newest_user, '</a>'), HERDOC;
would be written:
$some_var = '
"NEWEST_USER" => sprintf($lang["Newest_user"], "<a href="" . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . "">", $newest_user, "</a>"),
';
Offline
#8 05 Sep 2007 7:32 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Hmm, I tried escaping the variables, but everytime I escape something, something other becomes "unescaped". Is there any way I can get it to threat it as purely text? Not necessarily in PHP, as long as I can get the variable called $nr1 to actually threat the overly long arrays as text alone, and not code?
I have about 20-30 different arrays, that all need to be treated as purely text when performing the switch. There must be some way to do it.
Offline
#9 05 Sep 2007 8:40 am
Re: Automated File Changes
using a single quoted string like I showed above will work. just replace single quotes inside your string with double quotes or escape the single quotes. the only difference between single and double quoted strings in PHP is that double quotes will parse variables in the string and single quoted strings use the literal text. so your array indexes 'NEWEST_USER' and "NEWEST_USER" are exactly the same thing (as is $lang['Newest_user'] and $lang["Newest_user"]). since you're just wanting to insert that text, you want the literal text not a parsed string.
Offline
#10 05 Sep 2007 10:01 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Wonder how it is that Notepad manages to have a functional replace method, but Dreamweaver does not. Anyway, copied and pasted it into Notepad and switched all ' with ", but what should I substitute \n with now if I want it to line up like it used to? For coders sake, if they need to modify anything.
Last edited by Butcher (05 Sep 2007 10:05 am)
Offline
#11 05 Sep 2007 12:34 pm
Re: Automated File Changes
you can enter a new line
like this,
in your string (by pressing enter). \n will be printed as \n instead of newline, so just hit enter to insert the character.
dreamweaver does have a search and replace. CTRL + F. there are different options for it too. not as nice as visual studio's but nice nonetheless. their find and replace are the same dialog (which may be how you missed it).
Offline
#12 05 Sep 2007 1:44 pm
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Problem is that Dreamweaver doesn't actually replace the characters, he claims there are none. At least my version do, hehe. Yeah, it seems I confused what it would actually write when I do the php write function, to what it outputs when I echo $nr1.
So now I just need to throw it together, , probably take me a while, seeing as it requires all files to change. But should be useful to some people nevertheless. Thanks again MadHatter.
Offline
#13 05 Sep 2007 7:07 pm
Re: Automated File Changes
you gotta make sure the drop down list says "source code" because somethings something else is selected and it doesn't work right. It also lets you find based on a regular expression, which is extremely helpful at times.
Offline
#14 19 Sep 2007 11:53 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Ok, done a lot of testing, and amusingly found out that of course it will not be fully automated this way, because if I replace all the ' with ", then it cannot automatically find the sentences I am searching for, as it is a code standard in phpBB. Is there any alternate way of getting it to threat the variable contents are purely text? I cannot think of anything, but there must be some way to deny him to execute content inside a variable.
Offline
#16 20 Sep 2007 3:09 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Problem is, that if I replace " with ', then the str_replace function won't recognize the parts of the file it should. Would it work if I saved the exact part of the file I want to replace as a .txt file, and then set the variable to read it and then process the replace in the big index.php file?
I can't even imagine that there is no determined function for treating variable content as purely text, especially since the script will be working on a lot of files, where all by default has the phpBB coding style, and I cannot rewrite it all cause it would only be compatible with my forum then, and the script cannot be run on any random forum.
Offline
#17 20 Sep 2007 6:10 am
Re: Automated File Changes
not in the code, in your string
ex:
Code:
$1 = ' ); $template->assign_vars(array( \'TOTAL_POSTS\' => sprintf($l_total_post_s, $total_posts), \'TOTAL_USERS\' => sprintf($l_total_user_s, $total_users), \'NEWEST_USER\' => sprintf($lang[\'Newest_user\'], \'<a href="\' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . \'">\', $newest_user, \'</a>\'), \'FORUM_IMG\' => $images[\'forum\'], \'FORUM_NEW_IMG\' => $images[\'forum_new\'], \'FORUM_LOCKED_IMG\' => $images[\'forum_locked\'], \'L_FORUM\' => $lang[\'Forum\'], \'L_TOPICS\' => $lang[\'Topics\'], \'L_REPLIES\' => $lang[\'Replies\'], \'L_VIEWS\' => $lang[\'Views\'], \'L_POSTS\' => $lang[\'Posts\'], \'L_LASTPOST\' => $lang[\'Last_Post\'], \'L_NO_NEW_POSTS\' => $lang[\'No_new_posts\'], \'L_NEW_POSTS\' => $lang[\'New_posts\'], \'L_NO_NEW_POSTS_LOCKED\' => $lang[\'No_new_posts_locked\'], \'L_NEW_POSTS_LOCKED\' => $lang[\'New_posts_locked\'], \'L_ONLINE_EXPLAIN\' => $lang[\'Online_explain\'], \'L_MODERATOR\' => $lang[\'Moderators\'], \'L_FORUM_LOCKED\' => $lang[\'Forum_is_locked\'], \'L_MARK_FORUMS_READ\' => $lang[\'Mark_all_forums\'], \'U_MARK_READ\' => append_sid("index.$phpEx?mark=forums")) );';
notice all the single quotes have been escaped, and your double quotes that wrap the string have been replaced with single quotes. what this allows you to do is add the new line character by pressing return (you don't have to add the escaped version \n in the string), and your $var variables will not be evaluated. the escaped single quotes inside the string are single quotes to the string even though in your code they're escaped, so echo'ing $1 should print out what you had originally put in $1 that was wrapped in double quotes. with single quotes you don't have to escape double quotes, just single quotes.
Offline
#18 20 Sep 2007 9:53 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
So, basically if I escape my quotes and replace the double quotes with single ones, they will be detected as double quotes when running the script? I tried running it with my past editing, where I had replace all double quotes in that piece of text/code with single quotes, but it had no effect, it didn't replace it.
Offline
#19 20 Sep 2007 8:50 pm
Re: Automated File Changes
ok, I'm not trying to insult your intelligence here, but I'm going to go over a few things that may or may not help.
in programming you have basically 2 building blocks: 1) storage, and 2) flow control. flow control are logic tests like if, else, for, while, and so on. storage is a way for you to store some sort of info to the computer for use later (like variables). there are different types of data (numbers, text, complex objects that store state and perform some sort of pre-determined logic). unlike other programming languages, PHP uses implicit type variables which just means that when you declare a variable, its type is not set until you assign it. once you assign it, or reassign it, its type becomes set. if the first character (in your code) to the right of the equal sign ($var =) starts with a number, its type is numeric (depending on whether it has a decimal in it or not, its either an int(eger) or a float(ing point integer). if the first character (in your code) to the left of the equals sign is a single quote or a double quote, its type is a string (the type that holds text). in some languages if the character to the left of the equals sign is a single quote, then its type is a char(acter) which can hold a single letter, and if its a double quote its a string. PHP assumes anything inside a single quote or a double quote is a string. The only difference between a single quoted string and a double quoted string is that the PHP script parser will replace variables inside double quoted strings with the value of that variable. single quoted strings take each character in the string as literal values.
so for example, lets take the text:
each character has an ascii value."hello" world\n
" = 34
h = 104
e = 101
l = 108
l = 108
and so on and so forth. a string is really just an array of characters
now, the problem is how do you allow someone to type out a string like we normally type regardless of what characters are supposed to be represented? some ascii characters don't have keys assigned (that you can type in) so a series of "escaped" characters were born. these characters are written with a backslash and a character. internally they are stored as a single ascii value.
" = \"
' = \'
<line feed> = \n
<carriage return> = \r
<tab> = \t
<backspace> = \b
<null> = \0
now lets look at the difference in implementation as far as PHP sees it:
Code:
$var1 = '"hello" world\n';
the contents of $var1 is a 15 character array ( " h e l l o " <space> w o r l d \ n)
"hello" world\n
Code:
$var2 = "\"hello\" world\n";
the contents of $var2 is a 14 character array ( " h e l l o " <space> w o r l d <new line>)
the single quoted string will take everything inside the outter most quotes as literal characters. notice the \n in the first instance is 2 characters (a back slash and an n).
the double quoted string will take the special characters \" and \n as single characters (" and <line feed> respectively), so while there are literally 27 characters inside the double quoted string, the contents of $var will be as stated above.
now, if our text had a dollar sign inside it:
"hello" world $foo\n
Code:
$var1 = '"hello" world $foo\n';
$var1 contains 20 characters ( " h e l l o " <space> w o r l d <space> $ f o o \ n )
where as:
Code:
$var1 = "\"hello\" world $foo\n";
$var1 contains 14 characters ( " h e l l o " <space> w o r l d <space> ) because $foo is null. now this is kind of a trick because strings in C (which is what PHP is written in) are "null terminated" meaning that there is really a character at the end of each of these strings (read only in memory and not with the string itself) which is the \0 character. since $foo is undefined, it is null and since PHP pre-parses the string to insert the value of $foo, it will insert a null character, which PHP will think is the end of the string, and therefore the line feed character will never be seen.
now, one thing about single quoted strings is that the PHP parser will look for a single quote to be at the beginning and end of the text that you want stored as a string. this means that if the text you're looking to store has a single quote character in it, you will need to escape it (the only escaped character a single quoted string accepts)
now, back to your issue. the text you'll be reading in will have single quotes inside the string. if you want to be able to be consistent, the string that you patch into the normal file needs to be the same. if you hard code that text into your php script, then (as I've been saying) you'll store that in your PHP script as I've shown (as a string that starts and ends with a single quote), which means that you will need to escape the single quote characters in the text you want to store.
you don't need to search for escaped quotes, you dont need to re-write any text from single quotes to double, you only have to escape the single quotes in the text you want to store. when you write that text to the file, it will be written without the escape characters (since as I've shown, the escape characters are what we write to represent certain characters in our source code.
I hope that wasn't too insulting, and actually helped you see what I was talking about.
Offline
#20 21 Sep 2007 5:58 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Not insulting at all, I am still learning PHP. I will try it out, actually had a read about the different data types yesterday when I was googling around.
Offline
#21 23 Sep 2007 10:52 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Ok, tested a little, and I understand the need of escaping it, though, I am a little unsure about my script, I think I am writing something wrong (or maybe not writing the file at all);
Code:
<?php //File 1 echo "Opening file: <b>index.php</b><br>"; $newfile1 = "index.php"; $filehandle1 = fopen($newfile1, 'r') or die("can't create file"); $fh1 = fread($filehandle1, filesize($newfile1)); $nr1 = ' ); $template->assign_vars(array( \'TOTAL_POSTS\' => sprintf($l_total_post_s, $total_posts), \'TOTAL_USERS\' => sprintf($l_total_user_s, $total_users), \'NEWEST_USER\' => sprintf($lang[\'Newest_user\'], \'<a href="\' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . \'">\', $newest_user, \'</a>\'), \'FORUM_IMG\' => $images[\'forum\'], \'FORUM_NEW_IMG\' => $images[\'forum_new\'], \'FORUM_LOCKED_IMG\' => $images[\'forum_locked\'], \'L_FORUM\' => $lang[\'Forum\'], \'L_TOPICS\' => $lang[\'Topics\'], \'L_REPLIES\' => $lang[\'Replies\'], \'L_VIEWS\' => $lang[\'Views\'], \'L_POSTS\' => $lang[\'Posts\'], \'L_LASTPOST\' => $lang[\'Last_Post\'], \'L_NO_NEW_POSTS\' => $lang[\'No_new_posts\'], \'L_NEW_POSTS\' => $lang[\'New_posts\'], \'L_NO_NEW_POSTS_LOCKED\' => $lang[\'No_new_posts_locked\'], \'L_NEW_POSTS_LOCKED\' => $lang[\'New_posts_locked\'], \'L_ONLINE_EXPLAIN\' => $lang[\'Online_explain\'], \'L_MODERATOR\' => $lang[\'Moderators\'], \'L_FORUM_LOCKED\' => $lang[\'Forum_is_locked\'], \'L_MARK_FORUMS_READ\' => $lang[\'Mark_all_forums\'], \'U_MARK_READ\' => append_sid("index.$phpEx?mark=forums")) );'; $nr2 = ' ); // Menu Mod $contents = trim(file_get_contents("menu.php")); $content = preg_replace(array("/^\<\?php/", "/\?\>$/"), array("",""), $contents); eval($contents); // Menu Mod $template->assign_vars(array( "TOTAL_POSTS" => sprintf($l_total_post_s, $total_posts), "TOTAL_USERS" => sprintf($l_total_user_s, $total_users), "NEWEST_USER" => sprintf($lang["Newest_user"], "<a href="" . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . "">", $newest_user, "</a>"), // Menu Mod "EXTRA_MENU" => $menu.php, // Menu Mod "FORUM_IMG" => $images["forum"], "FORUM_NEW_IMG" => $images["forum_new"], "FORUM_LOCKED_IMG" => $images["forum_locked"], "L_FORUM" => $lang["Forum"], "L_TOPICS" => $lang["Topics"], "L_REPLIES" => $lang["Replies"], "L_VIEWS" => $lang["Views"], "L_POSTS" => $lang["Posts"], "L_LASTPOST" => $lang["Last_Post"], "L_NO_NEW_POSTS" => $lang["No_new_posts"], "L_NEW_POSTS" => $lang["New_posts"], "L_NO_NEW_POSTS_LOCKED" => $lang["No_new_posts_locked"], "L_NEW_POSTS_LOCKED" => $lang["New_posts_locked"], "L_ONLINE_EXPLAIN" => $lang["Online_explain"], "L_MODERATOR" => $lang["Moderators"], "L_FORUM_LOCKED" => $lang["Forum_is_locked"], "L_MARK_FORUMS_READ" => $lang["Mark_all_forums"], "U_MARK_READ" => append_sid("index.$phpEx?mark=forums")) ); '; $write1 = str_replace($nr1, $nr2, $filehandle1); $file1 = fopen($newfile1, 'w') or die("can't create file"); $stringdata1 = $write1; fwrite($file1, $stringdata1); echo "Writing to file: <b>index.php</b><br>"; fclose($filehandle1); echo "Closing file: <b>index.php</b><br>"; ?>
What I see happening (or should be happening), is this:
1. It opens the file called index.php
2. It reads and determines the filesize for the file called index.php
3. It searches for the content of variable $nr1 in variable $filehandle1, and replaces it with variable $nr2
4. It writes the changes to index.php
5. It closes the file
Now, obviously I am writing something, cause I get a result of it, but the result is only Resource id #3. But I did run a few echo tests, just to see how the escaping would work, with this script:
Code:
echo $nr1; echo '<br>'; echo '<br>'; echo 'Escaped'; echo '<br>'; echo '<br>'; echo $nr2; echo '<br>'; echo '<br>'; echo 'Original'; echo '<br>'; echo '<br>'; $write = str_replace($nr1, $nr2, $nr1); echo $write; echo '<br>'; echo '<br>'; echo 'Replaced'; ?>
And as you've explained, PHP sees both single and double quoted variables as strings, so it will not make a difference towards the output from the phpBB system (I tested it, it works). But, what makes it write Resource id #3 instead of the variable content?
And by the way, I am really glad you take the time to write explanations like the datatype and quote system one, it helps me a lot towards learning PHP, and understanding how it works, so thank you.
Offline
#22 23 Sep 2007 5:24 pm
Re: Automated File Changes
yea, fopen returns the file handle. str_replace works with strings not file handles, so you need to use something like file_get_contents to extract the file contents to a variable, then use str_replace to modify the text. lastly you write the file w/ fwrite.
Code:
<?php //File 1 echo "Opening file: <b>index.php</b><br>"; $newfile1 = "index.php"; $nr1 = ' ); $template->assign_vars(array( \'TOTAL_POSTS\' => sprintf($l_total_post_s, $total_posts), \'TOTAL_USERS\' => sprintf($l_total_user_s, $total_users), \'NEWEST_USER\' => sprintf($lang[\'Newest_user\'], \'<a href="\' . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . \'">\', $newest_user, \'</a>\'), \'FORUM_IMG\' => $images[\'forum\'], \'FORUM_NEW_IMG\' => $images[\'forum_new\'], \'FORUM_LOCKED_IMG\' => $images[\'forum_locked\'], \'L_FORUM\' => $lang[\'Forum\'], \'L_TOPICS\' => $lang[\'Topics\'], \'L_REPLIES\' => $lang[\'Replies\'], \'L_VIEWS\' => $lang[\'Views\'], \'L_POSTS\' => $lang[\'Posts\'], \'L_LASTPOST\' => $lang[\'Last_Post\'], \'L_NO_NEW_POSTS\' => $lang[\'No_new_posts\'], \'L_NEW_POSTS\' => $lang[\'New_posts\'], \'L_NO_NEW_POSTS_LOCKED\' => $lang[\'No_new_posts_locked\'], \'L_NEW_POSTS_LOCKED\' => $lang[\'New_posts_locked\'], \'L_ONLINE_EXPLAIN\' => $lang[\'Online_explain\'], \'L_MODERATOR\' => $lang[\'Moderators\'], \'L_FORUM_LOCKED\' => $lang[\'Forum_is_locked\'], \'L_MARK_FORUMS_READ\' => $lang[\'Mark_all_forums\'], \'U_MARK_READ\' => append_sid("index.$phpEx?mark=forums")) );'; $nr2 = ' ); // Menu Mod $contents = trim(file_get_contents("menu.php")); $content = preg_replace(array("/^\<\?php/", "/\?\>$/"), array("",""), $contents); eval($contents); // Menu Mod $template->assign_vars(array( "TOTAL_POSTS" => sprintf($l_total_post_s, $total_posts), "TOTAL_USERS" => sprintf($l_total_user_s, $total_users), "NEWEST_USER" => sprintf($lang["Newest_user"], "<a href="" . append_sid("profile.$phpEx?mode=viewprofile&" . POST_USERS_URL . "=$newest_uid") . "">", $newest_user, "</a>"), // Menu Mod "EXTRA_MENU" => $menu.php, // Menu Mod "FORUM_IMG" => $images["forum"], "FORUM_NEW_IMG" => $images["forum_new"], "FORUM_LOCKED_IMG" => $images["forum_locked"], "L_FORUM" => $lang["Forum"], "L_TOPICS" => $lang["Topics"], "L_REPLIES" => $lang["Replies"], "L_VIEWS" => $lang["Views"], "L_POSTS" => $lang["Posts"], "L_LASTPOST" => $lang["Last_Post"], "L_NO_NEW_POSTS" => $lang["No_new_posts"], "L_NEW_POSTS" => $lang["New_posts"], "L_NO_NEW_POSTS_LOCKED" => $lang["No_new_posts_locked"], "L_NEW_POSTS_LOCKED" => $lang["New_posts_locked"], "L_ONLINE_EXPLAIN" => $lang["Online_explain"], "L_MODERATOR" => $lang["Moderators"], "L_FORUM_LOCKED" => $lang["Forum_is_locked"], "L_MARK_FORUMS_READ" => $lang["Mark_all_forums"], "U_MARK_READ" => append_sid("index.$phpEx?mark=forums")) ); '; $index_content = file_get_contents($newfile1); $modified = str_replace($nr1, $nr2, $index_content); // do all your file replacement here echo "Writing to file: <b>index.php</b><br>"; file_put_contents($newfile1, $modified); ?>
Offline
#23 24 Sep 2007 1:30 pm
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
Hmmm, I put it up like this now:
Code:
<?php //File 1 echo "Opening file: <b>index.php</b><br>"; $newfile1 = "index.php"; $file1 = fopen($newfile1, 'w') or die("can't write file"); $nr1 = 'STUFF FOR NR 1'; $nr2 = 'STUFF FOR NR 2'; $index_content = file_get_contents($newfile1); $modified = str_replace($nr1, $nr2, $index_content); $stringdata1 = $modified; file_put_contents($newfile1, $modified); echo "Writing to file: <b>index.php</b><br>"; fclose($file1); echo "Closing file: <b>index.php</b><br>"; ?>
And I get no error message, but it writes nothing O_o
Offline
#24 24 Sep 2007 6:44 pm
Re: Automated File Changes
include the full path to the files.
assuming that your patch script is in the same dir as index.php:
Code:
$base_dir = dirname(__FILE__); $index_file = $base_dir.DIRECTORY_SEPARATOR."index.php"; //...
dirname will strip off the last thing from a path (in this case its the file name, if you passed $base_dir back into dirname, it would return the next folder up and so on), so using __FILE__ will return the full path to your patch script which you can use to look for the files you need to patch.
if your patch script isn't in the same dir as index.php then you'll need to modify the base_dir variable so that it points to the right location.
Offline
#25 25 Sep 2007 10:30 am
- Butcher
- Moderator
- From: Norway
- Registered: Jul 2006
- Posts: 308
Re: Automated File Changes
I replace $newfile1 with:
$newfile1 = $base_dir.DIRECTORY_SEPARATOR."index.php";
And it echoes to: D:\xampp\htdocs\write\index.php, which is right. But it still only cleans the file...
Offline