vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Help with plugin array and option settings (https://vborg.vbsupport.ru/showthread.php?t=264820)

Simon Lloyd 06-06-2011 11:19 PM

Help with plugin array and option settings
 
Firstly sorry if the title is misleading, i am trying to create a plugin that should check for each instance in an array, the array should look like array('FOO', 'BAR', 'FOOBAR');, however i am trying to read a multi line textarea in to the array like this: array($vbulletin->options['bs_sel_list']); but it only works if there is only one item in the textarea, if i put two or more, each on its own line or on the same line it doesn't work, i then tried this array(explode("\r\n", $vbulletin->options['bs_sel_list'])); but it doesn't make any difference!

How can i read the $vbulletin->options['bs_sel_list'] in to the array to get it to look like array('FOO', 'BAR', 'FOOBAR');

Any help or advice is appreciated!

kh99 06-06-2011 11:43 PM

Have you tried just explode("\n", $vbulletin->options['bs_sel_list']) ? (explode returns an array so you don't need array())

Simon Lloyd 06-06-2011 11:52 PM

Thanks, there was a little change with that, my list in bs_sel_list looks like this example
(foo)123
(bar + 1ht) there
foobar(foo) + there
With your suggestion it only finds foobar(foo) + there the last one in the array

Any further suggestions?

Thanks for your help by the way :)

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

Actually i think your suggestion does work!, i think i had my for each check the wrong way round...a couple more tests and i'll confirm it :)

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

Yep it works!, thanks for the suggestion too ;)

BirdOPrey5 06-08-2011 12:34 AM

I wonder if ti varies by server? Because all my mods that use multi-line text boxes I separate each line into array using this exact code:
PHP Code:

 $up_data explode ("\r\n"$vbulletin->options['bop5up_data']); 

And it works all the time.

Simon Lloyd 06-08-2011 01:02 AM

BoP5, that is correct and thats what KH99 was getting at, if you check my example i state its as Array(explode.... which was my downfall, removing Array( allowed it to work, the other issue was my fault because im still green at this :)

BirdOPrey5 06-08-2011 01:13 AM

Sorry I thought the difference was "\n" as opposed to "\r\n" - I was just saying i know \r\n works. Never tried just \n. I hadn't even noticed the array() call.

kh99 06-08-2011 02:16 AM

Joe's right. I was suggesting just "\n" but it looks like there is "\r\n" between each line, so if you only use a "\n" it works but you end up with a "\r" at the end of each string. I guess if you later trim() the strings either would work, but Joe's way is correct.

In any case, I'm glad you got it working.

Simon Lloyd 06-08-2011 02:35 AM

While i have your attention guys, any idea why this doesn't seem to work, im using the hook global_complete
Quote:

function banned_redirect($to, $code = '307 Temporary Redirect') {
header("HTTP/1.1 ".$code);
header("Location: http://$to");
exit();
}
if ($vbulletin->options['bsactive'])
{
$user_agent = strtolower(getenv('HTTP_USER_AGENT'));
$bots = explode("\r\n", $vbulletin->options['bs_sel_list']);
$n = sizeof($bots);
for ($i=0;$i<$n;$i++) {
if (strstr($user_agent,$bots[$i])) banned_redirect('www.google.com.cn/search?hl=zh-CN&q=crystal+light+centrum&meta=');
}
}
For some reason it's not or doesn't seem to be redirecting the spiders, i use http://www.botsvsbrowsers.com/ to check if the user agent is getting redirected as it should, right now i'm trying one for yandex and one for baidu here's that agent:
Quote:

Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)

kh99 06-08-2011 02:42 AM

Hmm...well, the only thing I can think of is, were you always using the "\r\n" in the explode? If you were using "\n" it could be that the extra "\r" that I mentioned above was messing things up for you.

Simon Lloyd 06-08-2011 02:47 AM

:), always used the \r\n but either way it doesn't seem to work in the plugin, i have tried changing the execution order but no go, and im pretty sure the syntax is right?

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

infact if i have the execution order earlier than another plugin using the same hook that checks user agents the plugin doesn't work, so it kind of looks like its stopping the user agent from getting through any further but it's not redirecting????

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

I have also substituted strtolower($_SERVER['HTTP_USER_AGENT']); for strtoupper($_SERVER['HTTP_USER_AGENT']); but no change

kh99 06-08-2011 03:02 AM

Can you write files via php on your server? I like to debug stuff by printing things to a file then looking at it after loading the page. Like sometimes I just throw in a

Code:

fwrite(fopen("output.txt", "w"), print_r($var, true));

...I think that actually shouldn't work on a "live" server ( unless you put in a path to a different directory) because the web server user usually doesn't have write access. But anyway the point is that it will probably save time if you can figure out some way to see what's going on in there.

Simon Lloyd 06-08-2011 03:17 AM

I have dedicated so have all the access i need!, do i just use that line like that or does it need modifying to suit?

kh99 06-08-2011 03:20 AM

You'd want to replace $var with the variable you want to see (or any message you want to print out). And of course you could put a path on the file name if you need to. And if you want to print out in more than one place you can separate the fopen() from fwrite()s. (like $fp = fopen("output.txt", "w"); fwrite($fp, "Hello World"); etc)

That the kind of thing I've been using, but maybe someone else has a better idea.

Simon Lloyd 06-08-2011 03:24 AM

lol, now you've lost me!, firstly the line does work on a live server :), i've changed it to this (if its correct) fwrite(fopen("output.txt", "w"), print_r($bots[$i], true)); and put it ight after the redirect statement. Below is what it gave:
MOZILLA/5.0 (COMPATIBLE; BAIDUSPIDER/2.0; +HTTP://WWW.BAIDU.COM/SEARCH/SPIDER.HTML)


Does that mean the plugin is working?

kh99 06-08-2011 03:31 AM

At least you know that the plugin is running. I'm not sure exactly where you put the line.

The way that line is above, if you fopen with "w" it will truncate the file, so if you put it in a loop you're only seeing the last time through the loop. You can change it to "a" to append, but then it will add to the file every time you run it. (I guess the thing to do is to put one in at the beginning with "w" and if you put it in more than once, make the others "a").

Anyway, I'm sure you get the idea. You can see what code's executing and what the string values are and you should be able to figure out where it's failing (unless it's working and just not doing what you expect) :)

Simon Lloyd 06-08-2011 03:32 AM

Thanks very much for this help, thats really useful :)

kh99 06-08-2011 03:42 AM

Oh, I see now you said that you put the line right after the redirect (I guess its too late for me to be thinking). Do you mean after the call to banned_redirect()? Then I guess strstr() isn't matching or else it wouldn't get there.

Anyway, let us know how it goes.

Simon Lloyd 06-08-2011 03:50 AM

Right, i put the line like below and tried different variations:
PHP Code:

[I]if (stristr($user_agent,$bots[$i])) banned_redirect('www.google.com.cn/search?hl=zh-CN&q=crystal+light+centrum&meta=');
fwrite(fopen("output.txt""a"), print_r(stristr($user_agent,$bots[$i]),true));
} [/
I

If i used
PHP Code:

 fwrite(fopen("output.txt""a"), print_r(stristr($user_agent,$bots[$i]),true)); 

i get a very long string of repeating UA's, one thing of note is that when one finished another one started like this:
Quote:

MOZILLA/5.0 (COMPATIBLE; YANDEXBOT/3.0; +HTTP://YANDEX.COM/BOTS)MOZILLA/5.0 (COMPATIBLE; BAIDUSPIDER/2.0; +HTTP://WWW.BAIDU.COM/SEARCH/SPIDER.H...A/5.0......etc
so i guess it's seeing the UA like MOZILLA/5.0 (COMPATIBLE; YANDEXBOT/3.0; +HTTP://YANDEX.COM/BOTS)MOZILLA/5.0 does that sound feasible or am i missing something?, my other plugin's use the same explode....etc and deliver emails dependant on criteria, they also strstr the same string and they have no trouble?

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

It's 6am here and im off home to bed in a couple of hours, maybe i'll catch you later :)

kh99 06-08-2011 04:37 AM

Oh right, there's no newline in that fwrite so you're just getting multiple prints coming out on the same line. And I think what I said earlier wasn't exactly correct - if you want to print out a string you don't need the print_r. And I find when printing out strings it's a little less confusing if you put some quotes in there. So you'd probably want something like:

fwrite(fopen("output.txt", "a"), "'" . stristr($user_agent,$bots[$i]) . "'\n");

Simon Lloyd 06-08-2011 01:04 PM

For some reason all i get with that line is '' on many lines but no info?

kh99 06-08-2011 02:27 PM

That's actually what I'd expect to see if the stristr() was never matching anything. Maybe try this:

Code:

...
$fp = fopen("output.txt", "w");
fwrite($fp, "User Agent: '" . $user_agent . "'\n");
for ($i=0;$i<$n;$i++) {
fwrite($fp, "bots[$i]: '" . $bots[$i] . "'\n");
if (strstr($user_agent,$bots[$i]))
{
    fwrite($fp, "Matched.\n");
    banned_redirect('www.google.com.cn/search?hl=zh-CN&q=crystal+light+centrum&meta=');
}
}
    fwrite($fp, "\nNothing matched.\n");
}


Simon Lloyd 06-08-2011 03:26 PM

KH99, thanks for that, could you amend the code so that it keeps appending, right now it just gives one set of results which i assume are the last set to be written.
Quote:

User Agent: 'MOZILLA/5.0 (COMPATIBLE; GOOGLEBOT/2.1; +HTTP://WWW.GOOGLE.COM/BOT.HTML)'
bots[0]: 'MOZILLA/5.0 (COMPATIBLE; YANDEXBOT/3.0; +HTTP://YANDEX.COM/BOTS)'
bots[1]: 'MOZILLA/5.0 (COMPATIBLE; BAIDUSPIDER/2.0; +HTTP://WWW.BAIDU.COM/SEARCH/SPIDER.HTML)'

Nothing matched.


kh99 06-08-2011 03:33 PM

Yeah, just change the "w" to "a" in the fopen line, like:

$fp = fopen("output.txt", "a");

and maybe add an extra \n to the "Nothing Matched" message so that there'll be some separation between runs.

Simon Lloyd 06-08-2011 03:48 PM

Well it does seem to be working as intended
Quote:

User Agent: 'MOZILLA/5.0 (COMPATIBLE; YANDEXBOT/3.0; +HTTP://YANDEX.COM/BOTS)'
bots[0]: 'MOZILLA/5.0 (COMPATIBLE; YANDEXBOT/3.0; +HTTP://YANDEX.COM/BOTS)'
Matched.
So why isn't it redirecting them?, if i use the useragent at www.botsvsbrowsers.com the UA can still access the site!?

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

I guess that somehow this particular plugin has to be initiated at first call of the forum so it's in the header as im assuming the page is already loading before this gets executed so no redirect takes place???

What do you think?

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

I changed the fwrite line and removed the others to only show matched, and sure enough they still do, i've tried different hooks but no redirect?
Quote:

Matched bots[1]: . MOZILLA/5.0 (COMPATIBLE; BAIDUSPIDER/2.0; +HTTP://WWW.BAIDU.COM/SEARCH/SPIDER.HTML) .
With User Agent: . MOZILLA/5.0 (COMPATIBLE; BAIDUSPIDER/2.0; +HTTP://WWW.BAIDU.COM/SEARCH/SPIDER.HTML)
--------------- Added [DATE]1307557610[/DATE] at [TIME]1307557610[/TIME] ---------------

Maybe it is working, ran it for around 15 minutes and BAIDU disappeared!, juts doing some more tests over 30 minutes and will report back:)

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

Yep, found a hook....etc that works, thanks for all your diagnostic help getting the output for each of the actions, thats something i'll always use now :)

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

One more question (honest!) how would i date and time stamp this entry
PHP Code:

fwrite($fp"Matched bots[$i]: . $bots[$i] . \nWith User Agent:  . $user_agent . \n\n"); 


BirdOPrey5 06-08-2011 08:51 PM

Quote:

Originally Posted by Simon Lloyd (Post 2205107)
One more question (honest!) how would i date and time stamp this entry

1st... Please use the [CODE] [/CODE] bb code for code and not [QUOTE] [/QUOTE] in the future as it's easier to read and the code will remain when someone quotes your post.

To date it use the php date() function.

I would try this:

Code:

fwrite($fp, "Matched bots[$i]: . $bots[$i] . \nWith User Agent: . $user_agent . " . date('m-d-Y H:i:s') . " \n\n");

Simon Lloyd 06-09-2011 04:45 AM

Code changed as requested :), ialready tried a version of the date function
PHP Code:

$DateOfRequest date("Y-m-d H:i:s"mktime($_REQUEST["Hour"],$_REQUEST["Min"],$_REQUEST
 
["Sec"],$_REQUEST["Month"],$_REQUEST["Day"],$_REQUEST["Year"])); 

But it produces a 1900 date with 00:00 time, i'll cut it down to just the date function and see what results i get, thanks.

BirdOPrey5 06-09-2011 11:32 AM

Quote:

Originally Posted by Simon Lloyd (Post 2205361)
Code changed as requested :), ialready tried a version of the date function
PHP Code:

$DateOfRequest date("Y-m-d H:i:s"mktime($_REQUEST["Hour"],$_REQUEST["Min"],$_REQUEST
 
["Sec"],$_REQUEST["Month"],$_REQUEST["Day"],$_REQUEST["Year"])); 

But it produces a 1900 date with 00:00 time, i'll cut it down to just the date function and see what results i get, thanks.

Don't give it a time, it will auto use the current time.

Simon Lloyd 06-09-2011 02:41 PM

Thanks Boofo but i'm not sure what you're referring to :), as for the date/time stamp BoP5's suggestion of cutting down my original stamp works, i can't understand why mine gave just 1900 and time 00:00?


All times are GMT. The time now is 03:52 AM.

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.01217 seconds
  • Memory Usage 1,833KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (6)bbcode_php_printable
  • (8)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (28)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete