Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-23-2010, 04:56 AM
Digital Jedi's Avatar
Digital Jedi Digital Jedi is offline
 
Join Date: Oct 2006
Location: PopCulturalReferenceLand
Posts: 5,171
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Using str_replace in MEMBERINFO

I've been searching for a few days, and I can't find anything to suggest why str_replace isn't working in MEMBERINFO for me. I've tried using both a custom template and simply putting the replacement code in a variable. I can' tell if I'm using the wrong hook, using outdated code or what.

For example, using a custome template
Code:
$find= '<li class=\"thead\"><a href=\"moderator.php?$session[sessionurl]do=useroptions&amp;u=$userinfo[userid]\">$vbphrase[edit_user_profile]</a></li>'; 
$vbulletin->templatecache['MEMBERINFO'] = str_replace($find, fetch_template(djs_delete_link) . $find, $vbulletin->templatecache['MEMBERINFO']);
I'm looking at some other plugins, but none of them look right around the Edit Profile link, which is where I'm looking. Does it have to do with the if condition the block of code sits in? I recall reading that str_replace doesn't play nice with if conditionals, but I thought that was if you were searching for them, not searching for a string inside of one. This seems like it should be working.
Reply With Quote
  #2  
Old 12-23-2010, 05:20 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You don't escape double quotes when they are used between single quotes. Only single quotes get escaped between single quotes. That is why it is not working for you.

And this I have never seen before:

Quote:
fetch_template(djs_delete_link)
Reply With Quote
  #3  
Old 12-23-2010, 05:37 AM
Digital Jedi's Avatar
Digital Jedi Digital Jedi is offline
 
Join Date: Oct 2006
Location: PopCulturalReferenceLand
Posts: 5,171
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Boofo View Post
You don't escape double quotes when they are used between single quotes. Only single quotes get escaped between single quotes. That is why it is not working for you.
I tried with and without. There's an article here on vB.org that stresses that you have to escape double quotes because that's the way the code is stored in memory:

Quote:
Originally Posted by mfyvie View Post
Code:
$find = '<table class=\"tborder\" cellpadding=\"$stylevar[cellpadding]\" cellspacing=\"$stylevar[cellspacing]\" border=\"0\" width=\"100%\" align=\"center\" id=\"woltable\">';
This is what we are going to look for, we've stored it in a variable called $find to make it easier to handle. Did you notice something? We've changed things slightly. Every time we have a double quote we need to "escape" it. We do this by adding a backslash character to tell PHP that we really do want a double quote stored in the variable, rather than having PHP think that we are ending or beginning a string using the double quote character. We've also enclosed our string in single quotes. Now you might be saying that at this point "But single quotes mean I don't have to escape double quotes". This is true, but not in this case. If we actually looked inside the template, as it is stored in memory, we'd find those backslash characters. If we want our search to match, we have to put them in. We could have also used the addslashes() function to do this, but we are keeping this example simple, so we put them in by hand. Remember what you see when editing the template via the style manager is not always the same as what is actually stored in memory!
Nevertheless, I'm always game, especially since the article is a few years old and I tried the code you sent me. In fact, I tried this same method (with and without quotes) before I tried using a template.

Code:
$find = '<li class="thead"><a href="moderator.php?$session[sessionurl]do=useroptions&amp;u=$userinfo[userid]">$vbphrase[edit_user_profile]</a></li>';
$add_before = '<li class="thead"><a href="admincp/user.php?do=remove&amp;u=$userinfo[userid]" style="color:red; font-face:bold">Delete User Profile</a></li>';

$vbulletin->templatecache['MEMBERINFO'] = str_replace($find, $add_before . $find, $vbulletin->templatecache['MEMBERINFO']);
I keep picking different hooks, but nothing seems to work, and it seems like it should.

Quote:
Originally Posted by Boofo View Post
And this I have never seen before:
Quote:
fetch_template(djs_delete_link)
Is there a more current way to fetch a template? That article is from 2007, but I've not been able to find anything more current. At least not for vB3.
Reply With Quote
  #4  
Old 12-23-2010, 05:53 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I meant you can't fetch a template in the middle of a str_replace, AFAIK. Also, this is one of the lines I have in one of my mods for a str_replace:

HTML Code:
$find2 = '<input type="checkbox" name="cookieuser" id="cb_cookieuser" value="1" tabindex="1" />';

I don't escape the double quotes and it works fine.
Reply With Quote
  #5  
Old 12-23-2010, 06:32 AM
Digital Jedi's Avatar
Digital Jedi Digital Jedi is offline
 
Join Date: Oct 2006
Location: PopCulturalReferenceLand
Posts: 5,171
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Boofo View Post
I meant you can't fetch a template in the middle of a str_replace, AFAIK. Also, this is one of the lines I have in one of my mods for a str_replace:

HTML Code:
$find2 = '<input type="checkbox" name="cookieuser" id="cb_cookieuser" value="1" tabindex="1" />';

I don't escape the double quotes and it works fine.
Ah, I see what you mean. For the life of me, I can't find the article where I learned to do that. I know I can't be that crazy. But I could be crazy and a little sleepy.
Reply With Quote
  #6  
Old 12-23-2010, 09:25 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I believe you. You're not crazy.
Reply With Quote
  #7  
Old 12-23-2010, 11:58 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I read the first post and also thought for sure it was the backslashes. Anyway, to see what was going on I put this code in a plugin using member_complete:

PHP Code:
$fp fopen('some path/out.txt''w');
fwrite($fp$vbulletin->templatecache['MEMBERINFO']);
fclose($fp); 
then I searched the output file for your string and found this:

PHP Code:
.(($show['edit_profile']) ? ("
    <li class=\"thead\"><a href=\"moderator.php?" 
$GLOBALS['vbulletin']->session->vars['sessionurl'] . "do=useroptions&amp;u=$userinfo[userid]\">$vbphrase[edit_user_profile]</a></li>
"
) : ("")).
So I guess that explains it. And I notice that the $userinfo and $vbphrase variables didn't get treated like that, so it seems it's only certain special variables that you can't match in a str_replace like that.

It also looks like you should need the backslashes, but then I can't explain why Boofo's example works, so .

Anyway, I guess you could probably make the $find be only the part before $session (or match something before that line instead if that doesn't narrow it down). Maybe you could put an html comment in the right place just to have something to match.

ETA: ...or maybe you could just copy the line as it appears above and use that.
Reply With Quote
  #8  
Old 12-23-2010, 12:11 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Then he would also have to do the replace line parsed. The only way to know what that is is to manually add the code to the template first and then go to the db and look at the template code for that after it is parsed and use that for the replacement. I used to do it that way at one time.

The escaped backslashes are there after it is the db, not before. Look at the Delete User Link in Profile mod I just did and see how I had to do it for vb 4. vb 3 was a lot easier to str_replace.
Reply With Quote
  #9  
Old 12-23-2010, 05:08 PM
Digital Jedi's Avatar
Digital Jedi Digital Jedi is offline
 
Join Date: Oct 2006
Location: PopCulturalReferenceLand
Posts: 5,171
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That would explain a lot. Just tried adding it to the template and it looks like this:

PHP Code:
".(($show['edit_profile']) ? ("
                        
<li class=\"thead\"><a href=\"moderator.php?" $GLOBALS['vbulletin']->session->vars['sessionurl'] . "do=useroptions&amp;u=$userinfo[userid]\">$vbphrase[edit_user_profile]</a></li>

                        <li class=\"thead\"><a href=\"admincp/user.php?do=remove&amp;u=
$userinfo[userid]\" style=\"color:red; font-face:bold\">Delete User Profile</a></li>
                    "
) : ("")).
I'm not familiar with finding matches with variables in them. What's the syntax for that, as I get a parse error if I try it as is?
Reply With Quote
  #10  
Old 12-23-2010, 10:27 PM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The 1 character causing the whole thing not to match is the ? after "moderator.php" - everything before it and everything after it (with kh99's code) matches.. and all the double quotes must be escaped as well, I tried it and it didn't work without escaping them, though I agree with Boofo it's not logical this needs to be done.

But anyway the code I have the replacement working on is on hook member_start.

This code works fine:
PHP Code:
$find'<li class=\"thead\"><a href=\"moderator.php'
$find3 "xxx.php";

$vbulletin->templatecache['MEMBERINFO'] = str_replace($find,  $find3$vbulletin->templatecache['MEMBERINFO']); 
As does this:

PHP Code:
$find=  $GLOBALS['vbulletin']->session->vars['sessionurl'] . 'do=useroptions&amp;u=$userinfo[userid]\">$vbphrase[edit_user_profile]</a></li>'
$find3 "xxx.php";

$vbulletin->templatecache['MEMBERINFO'] = str_replace($find,  $find3$vbulletin->templatecache['MEMBERINFO']); 
It's only the ? screwing everything up... I tried escaping it just for the hell of it- no help.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:43 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05121 seconds
  • Memory Usage 2,293KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (2)bbcode_html
  • (5)bbcode_php
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete