PDA

View Full Version : Help with plugin array and option settings


Simon Lloyd
06-06-2011, 11:19 PM
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 1307408504 at 1307408504 ---------------

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 1307410678 at 1307410678 ---------------

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:
$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
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: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 1307505034 at 1307505034 ---------------

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 1307505279 at 1307505279 ---------------

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

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:
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));
}

If i used 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:MOZILLA/5.0 (COMPATIBLE; YANDEXBOT/3.0; +HTTP://YANDEX.COM/BOTS)MOZILLA/5.0 (COMPATIBLE; BAIDUSPIDER/2.0; +HTTP://WWW.BAIDU.COM/SEARCH/SPIDER.HTML)MOZILLA/5.0......etcso 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 1307508696 at 1307508696 ---------------

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:

...
$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.
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 intendedUser 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 1307551911 at 1307551911 ---------------

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 1307555486 at 1307555486 ---------------

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?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 1307557610 at 1307557610 ---------------

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 1307558959 at 1307558959 ---------------

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 1307565957 at 1307565957 ---------------

One more question (honest!) how would i date and time stamp this entry
fwrite($fp, "Matched bots[$i]: . $bots[$i] . \nWith User Agent: . $user_agent . \n\n");

BirdOPrey5
06-08-2011, 08:51 PM
One more question (honest!) how would i date and time stamp this entry

1st... Please use the bb code for code and not 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 (http://php.net/manual/en/function.date.php).

I would try this:


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$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
Code changed as requested :), ialready tried a version of the date function$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?