
Bot Check mod by wirewolf - http://academyphotos.net/

Checks to see if a USER AGENT visiting a page is a Bot as listed in a bot_array (you can add or subtract bots if you want).
You can select to write to a log, botcheck (viewed in the Admin CP), send an email to Admin or do both.
Has a link in the log and in the email to check the Whois of the ip address of the agent (may be a simulator).

See // set options: at the start of the script:
To enable / disable logging, go to Admin CP => config => Logging and statistics => Logging mode
bot_check will log under 'Normal' or 'All'
To DISABLE emails, set SEND_MAIL to false
To DISABLE bot_check completely (no logging or emails) set BOT_CHECK to false, Just leave the bot_check(); function call in your pages. You can always ENABLE again by setting BOT_CHECK to true

I originally wrote the script as a plugin. After installing the plugin, you would have to point to the bot_check.php file by including in a custom footer. But then it would be included in every copperemine page. Including ones where it really doesn't make sense to have a bot recorded page hit (like message boxes, etc,). While I generally don't like to add mods to core files, I added this to the top of include/functions.inc.php so it's easy to locate later in case of an upgrade. And this way, you can be very specific as to where to add the bot_check function call. I found that index.php, displayimage.php, thumbnails.php and some of my custom pages are enough. If bots are hitting those main pages a lot, changes are they are hitting others too.

Now, how do you know if it's a real bot or is it a simulator? In the log file and in the emails, you can click a link to DNS Stuff for each bot and actually see if the IP is of a real bot or not.

So,

First: open include/functions.inc.php (SAVE A WORKING COPY FOR BACK UP!)

At the top of the file, just below:

/**
* Coppermine Photo Gallery - functions.inc.php
*
* This file has almost all the functions of Coppermine
*
* @copyright  2002,2006 Gregory DEMAR, Coppermine Dev Team
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License V2
* @package Coppermine
* @version  $Id: functions.inc.php 3275 2006-09-03 10:10:47Z gaugau $
*/

Add:

/* bot_check, by wirewolf
* Add this to the bottom of any coppermine page you want to check for a bot visit:
* bot_check();
* A good place is just after pagefooter(); - thus:
* pagefooter();
* bot_check();
* Look for any occurence of pagefooter(); in index.php, thumbnails.php
* and displayimage.php, or any custom page you have created in coppermine that has pagefooter();
*/

function bot_check()
{

// set options:
define('BOT_CHECK', true); // true = bot_check - ENABLED, false = bot_check - DISABLED
define('SEND_MAIL', true); // Sends an e-mail - true -send email, false - do not send email
// to enable / disable logging, go to Admin CP => config => Logging and statistics => Logging mode
// bot_check will log under 'Normal' or 'All' To DISABLE bot_check completely
// (no logging or emails) set BOT_CHECK above to false
// end set options

global $CONFIG;

if (BOT_CHECK)
	{
	 
	$bot_found ='';
	$bot_array = array( // bot array, add more if you want:
		'Googlebot', // who else?
		'msnbot', // yeah, them too!
		'ia_archiver', // haven't seen much of them, but why not!
		'Slurp', // got to have Yahoo!
		'psbot', // picsearch
		'Lycos_spider' // are they still around?
	);
	
	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	foreach ($bot_array as $key => $bot_value)
	{
	if (strpos($user_agent, $bot_value) != false) // do we have a bot?
		{
		$bot_found = 'yes';
		break;
		}
	}
			
	if ($bot_found == 'yes') // action to take if bot found
	{
	$bot_date = localised_date( mktime(), '%A, %d. %B %Y @ %H:%M' );
	$bot_ip = $_SERVER['REMOTE_ADDR'];
	$bot_address = '<a target="_blank" href="http://www.dnsstuff.com/tools/ipall.ch?domain='.$bot_ip.'">'.$bot_ip.'</a>';	   $found_agent = $_SERVER['HTTP_USER_AGENT'];
	$bot_page = $_SERVER['REQUEST_URI'];
	   
	   // see what action to take
	   if ($CONFIG['log_mode']) // writes to the log file
	   {
		   $log = 'botcheck';
		   $log = 'logs/'.$log.'.log.php';
		   $text = "$bot_date IP: $bot_address\nAgent: $found_agent\nPage: $bot_page";
		   if (!file_exists($log))
			   {
			   $log_header = implode('',file('logs/log_header.inc.php'));
			   } else { $log_header = ''; }
			   $fp = fopen($log,'a');
			   fwrite($fp,$log_header);
			   fwrite($fp,$text."\n\n");
			   fclose($fp);
		} // end log action

		if (SEND_MAIL) // send admin email
		{
			if ($CONFIG['log_mode'])
				{
				$also_log = 'This has also been entered in the botlog file';
				} else { $also_log = ''; }
				include 'mailer.inc.php';
				$bot_subject = 'A Search Engine Bot Has Visited Your Gallery';
					 
$bot_message = <<<EOT
Date: $bot_date<br />
Page: $bot_page<br />
Agent: $found_agent<br />
Whois (Click this link to verify user agent): $bot_address<br />
$also_log
EOT;

cpg_mail('admin', $bot_subject, $bot_message, 'text/plain');

   } // end actions
  } // end check for user agent
 } // end if bot_check enabled
} // end bot_check function

// end bot_check

save and upload include/functions.inc.php

Second: Add this function call to the bottom of any coppermine page you want to check for a bot visit:

bot_check();

A good place is just after pagefooter(); - thus:

pagefooter();
bot_check();

Look for any occurence of pagefooter(); in index.php, thumbnails.php
and displayimage.php, or any custom page you have created (and want to track bot hits) in coppermine that has pagefooter();

To check the bot_check function, go to a bot simulator like:
http://www.xml-sitemaps.com/se-bot-simulator.html , enter a page url that has the bot_check(); from your site,
pick Google or Yahoo from the drop down and hit 'SUBMIT". Or, for those with Firefox and the User Agent Switcher tool, select a switch and hit some of your pages with the bot_check();

Go check your email and take a look at the botcheck log file.
You should see what looks like Google, MSN or Yahoo user agent, but the ip address will be from the 
simulators' server or your IP (if using the User Agent Switcher tool in Firefox), not the real bot.

If the bots in the array are hitting your pages and you're getting a lot of bot hits and a ton of emails,
change - define('SEND_MAIL', true); to false. You can go and check the botcheck log when ever you want.

To DISABLE the bot_check() function, just set 'BOT_CHECK' to false (it's like a master switch) 
Just leave the bot_check(); in the pages you have.
You can turn it on or off any time with the 'BOT_CHECK' switch

A little note about getting indexed by the Bots. Since Coppermine is a image gallery, I can't stress enough the importance of having text descriptions for the displayimage pages. Bots loved textual content! They may see the link to an image, but they can't 'read' an image. The more (key)words you have in the image descriptions, the better! Also, try and create some custom pages. Like a site map, policy page, maybe some article pages related to your sites' theme and add the bot_check function to them.

A well written robots.txt file that steers the bots away from files you don't want them to index, to ones you do want indexed.

Meanwhile, enjoy using the bot_check mod. May the Bots be with you!

wirewolf
