Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 10-30-2009, 06:32 PM
imported_silkroad imported_silkroad is offline
 
Join Date: Dec 2003
Posts: 563
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default REGEX in vB Template Conditional

Hello!

We have some code we want to omit from the template if the USER_AGENT is GoogleBot, Mediapartners-Google, etc. For example (not working code)

Code:
<if condition="!in_array($_SERVER['HTTP_USER_AGENT'], array('GoogleBot','Mediapartners-Google'))">
<!-- this is the code -->
</if>
The user agent strings are:
So, the template condition above works well for Mediaparters-Google but does not match the longer GoogleBot string.

Is there a way to simply add some type of REGEX to the conditional, so 'GoogleBot' will match the longer user agent string?

Thanks!!
Reply With Quote
  #2  
Old 10-31-2009, 02:28 AM
RenatoMN's Avatar
RenatoMN RenatoMN is offline
 
Join Date: Nov 2008
Location: Brazil
Posts: 63
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You will need to create a Plugin.

Product: vBulletin
Name: Hide Content From Bots
Hook: showthread_start
Execution order: 5
PHP Code:
// bots list ([] = add a new item in the end of an array)
$hide_content_agent_list[] = "GoogleBot";
$hide_content_agent_list[] = "Mediapartners-Google";
$hide_content_agent_list[] = "ad more...";

// get the user agent
$hide_content_useragent $_SERVER['HTTP_USER_AGENT'];

// the default is true, false for bots (<else />)
$show_for_humans true;

foreach(
$hide_content_agent_list as $hcag) {
    if (
preg_match("/$hcag/i""$hide_content_useragent")) {
        
$show_for_humans false;
        break;
    }

Plugin is active: Yes //do not forget it

Then use the condition either this:

Code:
<if condition="$show_for_humans">
code to be show only for humans
</if>
Code:
<if condition="$show_for_humans">
code to be show only for humans
<else />
the replacement text for bots
</if>
Reply With Quote
  #3  
Old 10-31-2009, 06:05 AM
imported_silkroad imported_silkroad is offline
 
Join Date: Dec 2003
Posts: 563
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks! I'll give it a "plug" and see how it works!

--------------- Added [DATE]1256975400[/DATE] at [TIME]1256975400[/TIME] ---------------

I could not get this to work (for our purposes) in the hook location showthread_start, but I was able to get it to work in postbit_display_start.

Perhaps this is because the content we are masking is in the content of the postbit template?
Reply With Quote
  #4  
Old 10-31-2009, 01:31 PM
RenatoMN's Avatar
RenatoMN RenatoMN is offline
 
Join Date: Nov 2008
Location: Brazil
Posts: 63
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yes. Sorry for not asking or better explaining this.

Leave as postbit_display_start if you're not going to use it elsewhere, or try another location, like global_start if you're going to use it widely in your boards.
Reply With Quote
  #5  
Old 10-31-2009, 03:33 PM
imported_silkroad imported_silkroad is offline
 
Join Date: Dec 2003
Posts: 563
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by RenatoMN View Post
Yes. Sorry for not asking or better explaining this.

Leave as postbit_display_start if you're not going to use it elsewhere, or try another location, like global_start if you're going to use it widely in your boards.
Hi RenatoMN!

Oh! No need to apologize. Your code worked fine. Here is final version I used:

PHP Code:
// bots list ([] = add a new item in the end of an array) 
$hide_content_agent_list[] = "GoogleBot"
$hide_content_agent_list[] = "Mediapartners-Google"

// get the user agent 
$hide_content_useragent $_SERVER['HTTP_USER_AGENT']; 

// the default is true, false for bots (<else />) 
$show_for_humans TRUE

foreach(
$hide_content_agent_list as $hcag) { 
    if (
preg_match("/$hcag/i""$hide_content_useragent")) { 
        
$show_for_humans FALSE
        break; 
    } 

I may add some more bots later. Thanks again! This is very useful for everyone.
Reply With Quote
  #6  
Old 10-31-2009, 05:37 PM
RenatoMN's Avatar
RenatoMN RenatoMN is offline
 
Join Date: Nov 2008
Location: Brazil
Posts: 63
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm glad it worked (I didn't even tested )

If you (or anybody) want this as a product, with ACP options (a bot list management), send me a PM so we can discuss details and price.

Regards,
Reply With Quote
  #7  
Old 11-01-2009, 05:43 AM
imported_silkroad imported_silkroad is offline
 
Join Date: Dec 2003
Posts: 563
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

FWIW, I cannot get this plugin to work in postbit if I move it to the hook global_start however it works fine if the hook is postbit_display_start.
Reply With Quote
Reply

Thread Tools
Display Modes

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 07:20 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.13814 seconds
  • Memory Usage 2,227KB
  • Queries Executed 13 (?)
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_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)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_postinfo_query
  • fetch_postinfo
  • 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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete