Advertisement
Guest User

kaiser / Wordpress 404-Mail-Notification

a guest
Feb 28th, 2010
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.96 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Your friendly 404-Deamon
  4. Plugin URI: http://not-going-to-come-anytime.com
  5. Description: Mails you a 404-Notification. This will even send you an email if you have got a missing favicon. It can be a little bit annoying, when you get mails for all crap of stuff. Feel free to extend the exclusions at the bottom. Currently it only excludes 404 if the admin lands on non-existand URLs or google and yahoo! visits you (props: toscho).
  6. Version: 0.2
  7. Author: F. J. Kaiser
  8. Author URI: http://unserkaiser.com
  9. License: GPL2
  10.  
  11. // Copyright (c) 2009-2010 F. J. Kaiser. All rights reserved.
  12. // http://unserkaiser.com
  13. //
  14. // Note: Don't expect any support for this release.
  15. //
  16. // Released under the GPL license
  17. // http://www.opensource.org/licenses/gpl-license.php
  18. //
  19. // This is an add-on for WordPress
  20. // http://wordpress.org/
  21. //
  22. // **********************************************************************
  23. // This program is distributed in the hope that it will be useful, but
  24. // WITHOUT ANY WARRANTY;
  25. // without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. // **********************************************************************
  28. */
  29.  
  30. load_plugin_textdomain( '404-deamon', '/wp-content/plugins/404-deamon' );
  31.  
  32. if( !class_exists('deamon_404') ):
  33. class deamon_404 {
  34.     var $page_uri;
  35.     var $look_up_ip;
  36.     var $guest_user_agent;
  37.     var $guest_referer;
  38.     // this is not necessarily the end... go further and extend
  39.     // var ;
  40.  
  41.     function deamon_404() {
  42.         if (function_exists(pods_url_variable)) {
  43.             // for Wordpress only: Your WP-URL
  44.             $this->page_uri = get_bloginfo('url');
  45.             // if you, like me: LOOOOOVE Pods, then go and grap a copy over at http://podscms.com
  46.             $this->page_uri .= "/" . pods_url_variable('last');
  47.         } elseif (!function_exists(pods_url_variable)) {
  48.             // or stick with a simple PHP-Function
  49.             // if you don't use WP, then use the following lines
  50.             $this->page_uri = 'http';
  51.             // Take https if is on
  52.             if ($_SERVER["HTTPS"] == "on") {
  53.                 $this->page_uri .= "s";
  54.             }
  55.             $this->page_uri_php .= "://";
  56.             if ($_SERVER["SERVER_PORT"] != "80") {
  57.                 $this->page_uri .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  58.             } else {
  59.                 $this->page_uri .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  60.             }
  61.         }
  62.  
  63.         // get some additional data about our users and guest
  64.         // don´t forget to delete the data if it´s no enemy
  65.         // we want to save the privacy of our users and guests
  66.         (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $this->look_up_ip = $_SERVER['HTTP_X_FORWARDED_FOR'] : $this->look_up_ip = $_SERVER['REMOTE_ADDR'] );
  67.         (isset($_SERVER['HTTP_USER_AGENT']) ? $this->guest_user_agent = addslashes($_SERVER['HTTP_USER_AGENT']) : '');
  68.         (isset($_SERVER['HTTP_REFERER']) ? $this->guest_referer = addslashes($_SERVER['HTTP_REFERER']) : '');
  69.         // another type or setting the requested uri
  70.         //$guest_req_method = $_SERVER['REQUEST_METHOD'] . ' http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  71.     }
  72. } // End deamon_404 class def
  73. endif;
  74.  
  75. // add it to the 404-Template
  76. add_filter('404_template', 'mail_404');
  77. // Here goes the function to construct and send the email:
  78. function mail_404($deamonmail) {
  79. $deamon = new deamon_404();
  80.  
  81.     $sitename = get_bloginfo('url');
  82.     $domain = str_replace('http://', '', $sitename);
  83.  
  84.     // set up the current User to get some data about him/her
  85.     global $current_user;
  86.     get_currentuserinfo();
  87.         // Let's see, if it's registered User, or an unknown Guest
  88.         if( is_user_logged_in() ) {
  89.             $user_or_guest = "The User ";
  90.             $user_or_guest .= $current_user->user_login;
  91.             $user_or_guest .= " (Mail: ";
  92.             $user_or_guest .= $current_user->user_email;
  93.             $user_or_guest .= ") has ";
  94.         } elseif( !is_user_logged_in() ) {
  95.             $user_or_guest = "A guest has ";
  96.         }
  97.  
  98.     $to = get_bloginfo('admin_email');
  99.     $subject = "Some love from your friendly 404-Deamon";
  100.     $message = "Dear Admin!\n\n";
  101.         $message .= "Notification:\n";
  102.         $message .= $user_or_guest;
  103.         $message .= "tried to reach a page that's not here (";
  104.         $message .= $deamon->page_uri;
  105.         $message .= ").\n\n";
  106.         $message .= "Users Agent:";
  107.         $message .= $deamon->guest_user_agent;
  108.         $message .= "\n";
  109.         $message .= "Referer:";
  110.         $message .= $deamon->guest_referer;
  111.         $message .= "\n";
  112.         // $message .= "Request Method:";
  113.         // $message .= $guest_req_method;
  114.         // $message .= "\n";
  115.         $message .= "IP: http://ip-lookup.net/?ip=";
  116.         $message .= $deamon->look_up_ip;
  117.         $message .= "\n\n";
  118.             $message .= "Your friendly 404-Deamon from the neighborhood\n";
  119.             $message .= "-----------\n";
  120.             $message .= "That's an automatic mail.\n";
  121.             $message .= "Please don't answer.\n";
  122.             $message .= "I'm too shy to reply.\n";
  123.             $message .= "-----------";
  124.  
  125.     $sitename = get_bloginfo('url');
  126.     $domain = str_replace('http://', '', $sitename);
  127.         $headers = "From: 404-Deamon: Missing <404_deamon@";
  128.         $headers .= $domain;
  129.         $headers .= ">\r\n";
  130.     $attachments = '';
  131.  
  132.     /*
  133.     * Start: This part isn't working at the moment.. don't know why
  134.     * but not as long as i'm not looking for a sollution it will stay this way
  135.     $content_type = "text/html";
  136.     apply_filters( 'wp_mail_content_type', $content_type );
  137.     * End: but it doesn't hurt anyway
  138.     */
  139.  
  140.     return $deamonmail;
  141. }
  142.  
  143. function test_data() {
  144. $deamon = new deamon_404();
  145.  
  146.     // set up the current User to get some data about him/her
  147.     global $current_user;
  148.     get_currentuserinfo();
  149.  
  150.     echo "<div class='container'>";
  151.         echo "<div class='span-24 last'>";
  152.  
  153.                 echo 'Page URL: ' . $deamon->page_uri . "<br />";
  154.                 echo 'Username: ' . $current_user->user_login . "<br />";
  155.                 echo 'User email: ' . $current_user->user_email . "<br />";
  156.                 echo 'User level: ' . $current_user->user_level . "<br />";
  157.                 echo 'User first name: ' . $current_user->user_firstname . "<br />";
  158.                 echo 'User last name: ' . $current_user->user_lastname . "<br />";
  159.                 echo 'User display name: ' . $current_user->display_name . "<br />";
  160.                 echo 'User ID: ' . $current_user->ID . "<br />";
  161.                 echo 'User IP-LookUp: ' . $deamon->look_up_ip . "<br />";
  162.                 echo 'User-Agent: ' . $deamon->guest_user_agent . "<br />";
  163.                 echo 'User Referer: ' . $deamon->guest_referer;
  164.         echo "</div>";
  165.     echo "</div>";
  166. }
  167. add_action('hook_above_title', 'test_data', 0) ; // TEST
  168.  
  169. // that's all folks
  170. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement