Version: 0.9

Type: Full Script

Category: HTTP

License: GNU General Public License

Description: This script notifies webmaster on 401,403,404 and 500 errors through e-mail, sms and icq. Very usefull for dynamic site webmasters...



<?
# THIS IS THE CODE I PUT IN MY ERROR PAGES! I USE IT TO DEFINE DYNAMIC VARIABLES
# AND TO INCLUDE MAILME.PHP WHICH USES THOSE VARIABLES!

# You can define whether to send e-mail to webmaster, SMS message to
# webmaster's cell phone, or even ICQ number. Use the $notifylevel
# variable to control how to receive these reports.
#
# 0 = don't use the email capabilities at all
# 1 = send only email to webmaster any time an error is generated, useful
#     for tracking broken links at other sites which link to you
#     (i.e. the 404 was generated by a broken link on someones site)
# 2 = send email to webmaster in any cases and SMS message to his cell phone
#     only if the error's referer contains your domain name (i.e. the 404
#     was generated by a broken link on your site)
# 3 = send email to webmaster in any cases and ICQ message to his ICQ number
#     only if the error's referer contains your domain name (i.e. the 404 was
#     generated by a broken link on your site)
# 4 = send email to webmaster in any cases, and if the error's referer contains
#     your domain name, SMS message to his cell phone and ICQ
#     message to his ICQ number
$notifylevel = 2;

# Define error number. I use only 401 (Authorization Required), 403 (Forbidden)
# 404 (Page Not Found) and 500 (Internal Server Error).
$errornumber = 401;

include($DOCUMENT_ROOT . '/mailme.php');
?>

<?
###################
# MAILME.PHP v0.9 #
###################

# First of all, I would like to apoplogize because some parts of this code
# (most of all, echo commands) contain text in my language (serbian), and
# I really didn't have time to translate everything, so if you have any
# problems (or if you make something even better), plese, don't hesitate to
# write me at somi@rocketmail.com


# --- FIRST, DEFINE VARIABLES! ---
# ---     E D I T A B L E      ---

# Define a domain name. It is best not to use the www. prefix because
# you can use this variable later for e-mail addressing.
$domain = "afwebteam.co.yu";

# Define a short domain name. It will be shown only in messages sent
# to SMS and ICQ for saving number of characters.
$shortdomain = "afwt";

# To whom to send email?
$to = "somi@rocketmail.com";

# To whom to send sms?
$sms_to = "38163581985@sms.mobtel.co.yu";

# To what ICQ number to send message?
$icq_to = "17772503";

# What will be the sender's name of message webmaster recieves through ICQ?
$icq_name = "$shortdomain server -> ERROR $errornumber !";

# Whos e-mail will be shown to webmaster's ICQ when he gets an error notification?
$icq_email = "website@afwebteam.co.yu";

# Define Headers
$headers = "From: servis.yu@afwebteam.co.yu\n";
$headers .="X-Sender: <mail@neobee.net>\n";
$phpver = phpversion();
$headers .= "X-Mailer: PHP " . $phpver . "\n"; //mailer
$headers .= "X-Priority: 1\n"; //1 UrgentMessage, 3 Normal


# --- AND NOW, THE FUNCTIONS! ---
# --- N O T   E D I T A B L E ---
# (unless you know what you're doing)

# Is mail sent ok? For now, it's not! You can use it later in your own IF
# expressions to define the rest of your PHP page. (i.e. to write 'Webmaster
# is notified about this error' or 'Webmaster is not notified about this error.
# Please, send him e-mail here')
$emailsent = False;

# Function that sends e-mail to webmaster, defined in $to variable bellow.
# This function will be proccessed if $notifylevel>0, and only this function
# returns $emailsent variable for checking purposes is the mail sent or not.
function send_email()
  {
   # Request access to the global variables we need
   global $REQUEST_URI, $HTTP_REFERER, $REMOTE_ADDR;
   global $to, $domain, $errornumber, $emailsent, $headers;

   # Create Subject.
   $subject = "Greska $errornumber na adresi: $domain$REQUEST_URI";

   # Build the $errortime variable to contain the date/time of the error.
   $errortime = datum();

   # Create the body of the email message
   $message = "PRIJAVA GRESKE $errornumber !\n\n";
   $message .= "Gresku $errornumber na adresi\n$domain$REQUEST_URI\n";
   $message .= "prijavio je $REMOTE_ADDR u $errortime sa adrese\n$HTTP_REFERER\n\n";

   # Send the mail message.
   if (mail($to,$subject,$message,$headers)) $emailsent = True;
   
   return;
  }

# Function that sends SMS to webmaster's phone, which, in my case, needs only
# to send an e-mail message to my_number@celloperaterdomain.com, as defined in
# $sms_to variable bellow. Maybe you will have to mess with this part a bit if
# your operater allows only text in Subject field, etc. If your cell operater
# does not support E-mail2SMS feature, at www.phpbuilder.com you can find a
# function that was written by bobwhite that sends SMS message through Simplewire
# Network that supports over 100 countries. This function will be proccessed if
# $notifylevel value is 2 or 4.
function send_sms()
  {
   # Request access to the global variables we need
   global $REQUEST_URI, $HTTP_REFERER, $REMOTE_ADDR;
   global $shortdomain, $sms_to, $errornumber, $headers;

   # Build the $errortime variable to contain the date/time of the error.
   $errortime = date("d/m/y u G:i:s\h");

   # Create Subject.
   $body = "Gresku $errornumber na adresi $shortdomain$REQUEST_URI ";
   $body .= "sa adrese $HTTP_REFERER ";
   $body .= "prijavio je $REMOTE_ADDR dana $errortime";

   # Send the mail2SMS message. We don't need subject, because it goes to SMS...
   mail($sms_to,"",$body,$headers);
   
   return;
  }

# Function that sends ICQ message to webmaster through www.icq.com web interface.
# This function is originally written by Thomas Pokorny, but I included (a little
# bit changed to fit my other code) modified version by Michal Taborsky
# <whizz@centrum.cz> that I found on www.phpbuilder.com. This function will be
# proccessed only if $notifylevel value is 3 or 4. They didn't bother to comment it,
# so neither did I. I described the variable usage, though.
function send_icq()
  {
   # Request access to the global variables we need
   global $REQUEST_URI, $HTTP_REFERER, $REMOTE_ADDR;
   global $domain, $icq_to, $icq_name, $icq_email, $errornumber, $headers;

   # Create Subject.
   $subject = "Greska $errornumber na adresi: $domain$REQUEST_URI";

   # Build the $errortime variable to contain the date/time of the error.
   $errortime = datum();

   # Create the body of the email message
   $message = "PRIJAVA GRESKE $errornumber !\n\n";
   $message .= "Gresku $errornumber na adresi\n$domain$REQUEST_URI\n";
   $message .= "prijavio je $REMOTE_ADDR u $errortime sa adrese\n$HTTP_REFERER\n\n";

   # Original ICQSend() code.
   $variables = "from=".RawURLEncode($icq_name)."&fromemail=".RawURLEncode($icq_email)."&subject=".RawURLEncode($subject)."&body=".RawURLEncode($message)."&to=$icq_to&Send=Send+Message";
   $request =
"POST http://wwp.icq.com/scripts/WWPMsg.dll HTTP/1.0
Referer: http://wwp.icq.com/$icq_to
Proxy-Connection: Keep-Alive
User-Agent: Mozilla/4.72 [en] (X11; U; Linux 2.2.17 i686)
Host: wwp.icq.com
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Content-type: application/x-www-form-urlencoded
Content-length: ".strlen($variables)."

   $variables";

   if ($fs = fsockopen("wwp.icq.com",80)) {
      fputs($fs, $request);
      fclose($fs);
   } else echo "<p>Error while connecting to www.icq.com.<br>Webmaster is not notified with ICQ.</p>";
  }

# I use functions bellow to format date/time written in messages to
# webmaster used for $errortime variable. You don't have to use it
# if your language is english, but then you have to modify the rest
# of tis script.
function datum()
  {
   $dan = date("l");
   switch($dan) {
	case "Sunday":
		$dan = "Nedelju"; //change in your language
		break;
	case "Monday":
		$dan = "Ponedeljak"; //change in your language
		break;
	case "Tuesday":
		$dan = "Utorak"; //change in your language
		break;
	case "Wednesday":
		$dan = "Sredu"; //change in your language
		break;
	case "Thursday":
		$dan = "Cetvrtak"; //change in your language
		break;
	case "Friday":
		$dan = "Petak"; //change in your language
		break;
	case "Saturday":
		$dan = "Subotu"; //change in your language
   }
   $mesec = date("n");
   switch($mesec) {
	case "1":
		$mesec = "januara"; //change in your language
		break;
	case "2":
		$mesec = "februara"; //change in your language
		break;
	case "3":
		$mesec = "marta"; //change in your language
		break;
	case "4":
		$mesec = "aprila"; //change in your language
		break;
	case "5":
		$mesec = "maja"; //change in your language
		break;
	case "6":
		$mesec = "juna"; //change in your language
		break;
	case "7":
		$mesec = "jula"; //change in your language
		break;
	case "8":
		$mesec = "avgusta"; //change in your language
		break;
	case "9":
		$mesec = "septembra"; //change in your language
		break;
	case "10":
		$mesec = "oktobra"; //change in your language
		break;
	case "11":
		$mesec = "novembra"; //change in your language
		break;
	case "12":
		$mesec = "decembra"; //change in your language
   }

   $datum = $dan . date(", j. ") . $mesec . date(" Y. u G:i:s\h");
   return $datum;
  }


# --- AND AT THE END, THE CODE! ---

# See whether or not we should send an email report. If $notifylevel>1,
# it will compare $domain with $HTTP_REFERER, because if they match, it
# means that a reffering page is from webmaster's site, and that is
# sometimes an emergency that requires ICQ or SMS for urgent notification.
if ($notifylevel == 1) {
	send_email();
} elseif ($notifylevel == 2) {
	send_email();
	if (eregi($domain,$HTTP_REFERER)) send_sms();
} elseif ($notifylevel == 3) {
	send_email();
	if (eregi($domain,$HTTP_REFERER)) send_icq();
} elseif ($notifylevel == 4) {
	send_email();
	if (eregi($domain,$HTTP_REFERER)) {
		send_sms();
		send_icq();
	};
};


# An example of $emailsent variable usage.
# if ($emailsent) {
#	echo "<p>E-mail sent to $to.</p>";
# } else {
#	echo "<p>E-mail was not sent to $to.<br>";
#	echo "Please, send it manually to <a href'$to'>$to.</a></p>";
# };

?>