ak_404_log)) { $wpdb->ak_404_log = $wpdb->prefix.'ak_404_log'; } $this->url_404 = isset($_SERVER['REQUEST_URI']) ? 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] : ''; $this->url_refer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; $this->remote_addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $this->remote_host = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : ''; $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $this->mailto = ''; $this->mail_enabled = 0; $this->rss_limit = 100; $this->date = gmdate('Y-m-d H:i:s'); $this->options = array( 'mailto' => 'email', 'mail_enabled' => 'int', 'rss_limit' => 'int' ); } function install() { global $wpdb; $result = $wpdb->query(" CREATE TABLE `$wpdb->ak_404_log` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `url_404` TEXT NOT NULL , `url_refer` TEXT NULL , `remote_addr` VARCHAR(255) NULL , `remote_host` TEXT NULL , `user_agent` TEXT NULL , `date_gmt` DATETIME NOT NULL ) "); add_option('ak404_mailto', $this->mailto); add_option('ak404_mail_enabled', $this->mail_enabled); add_option('ak404_rss_limit', $this->rss_limit); } function upgrade() { global $wpdb; $col_data = $wpdb->get_results(" SHOW COLUMNS FROM $wpdb->ak_404_log "); $cols = array(); foreach ($col_data as $col) { $cols[] = $col->Field; } // 1.3 schema upgrade if (!in_array('remote_addr', $cols)) { $wpdb->query(" ALTER TABLE `$wpdb->ak_404_log` ADD `remote_addr` VARCHAR(255) DEFAULT NULL AFTER `url_refer` "); } if (!in_array('remote_host', $cols)) { $wpdb->query(" ALTER TABLE `$wpdb->ak_404_log` ADD `remote_host` VARCHAR(255) DEFAULT NULL AFTER `remote_addr` "); } } function update_settings() { if (!current_user_can('manage_options')) { return; } $options_arr = array(); foreach ($this->options as $option => $type) { if (isset($_POST[$option])) { switch ($type) { case 'email': $value = stripslashes($_POST[$option]); if (!ak_check_email_address($value)) { $value = ''; } break; case 'int': $value = intval($_POST[$option]); break; default: $value = stripslashes($_POST[$option]); } $options_arr['ak404_'.$option] .= $value; } else { $options_arr['ak404_'.$option] .= $this->options; } } update_option('ak404_options', serialize($options_arr)); $this->upgrade(); } function get_settings() { $settings = unserialize(get_option('ak404_options')); foreach ($this->options as $option => $type) { if (empty($settings)) { $this->$option = get_option('ak404_'.$option); } else { $this->$option = $settings['ak404_'.$option]; } switch ($type) { case 'email': $this->$option = $this->$option; break; case 'int': $this->$option = intval($this->$option); break; } } } function log_404() { global $wpdb; if (empty($this->url_404)) { return; } $result = $wpdb->insert( $wpdb->ak_404_log, array( 'url_404' => $this->url_404, 'url_refer' => $this->url_refer, 'remote_addr' => $this->remote_addr, 'remote_host' => $this->remote_host, 'user_agent' => $this->user_agent, 'date_gmt' => $this->date ) ); if ($result === false) { return false; } $this->mail_404(); } function surpress_notification($regexs = array()) { $regexs = apply_filters('ak404_surpress_notification', $regexs); if (count($regexs)) { foreach ($regexes as $regex) { if (preg_match($regex, $this->url_404)) { return true; } } } return false; } function mail_404() { if (!empty($this->mailto) && $this->mail_enabled){//} && !$this->surpress_notification()) { $to = esc_html($this->mailto); $subject = __('404: ', '404-notifier').esc_html($this->url_404); $message = __('404 Report - a file not found error was registered on your site.', '404-notifier')."\n\n" .__('404 URL: ', '404-notifier').esc_html($this->url_404)."\n\n" .__('Referred by: ', '404-notifier').esc_html($this->url_refer)."\n\n" .__('Remote Address: ', '404-notifier').esc_html($this->remote_addr)."\n\n" .__('Remote Host: ', '404-notifier').esc_html($this->remote_host)."\n\n" .__('User Agent: ', '404-notifier').esc_html($this->user_agent)."\n\n" .__('Time: ', '404-notifier').esc_html(mysql2date('Y-m-d H:i:s', $this->date))."\n\n"; $headers = 'From: '.esc_html($this->mailto)."\r\n" .'Reply-To: '.esc_html($this->mailto)."\r\n" .'X-Mailer: PHP/'.phpversion(); wp_mail($to, $subject, $message, $headers); } } function dashboard_page() { global $wpdb; echo('
'); CF_Admin::admin_header(__('404 Notifier Logs', '404-notifier'),'404-Notifier', N404_Version, '404-notifier'); echo('
'); $per_page = 20; $pagenum = isset($_GET['paged']) ? absint($_GET['paged']) : 0; if (empty($pagenum)) $pagenum = 1; $offset = ($pagenum - 1) * $per_page; $events = $wpdb->get_results(" SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->ak_404_log ORDER BY date_gmt DESC LIMIT $offset, $per_page "); $overall_count = $wpdb->get_var("SELECT FOUND_ROWS()"); if ($overall_count > 0) { $num_pages = ceil($overall_count / $per_page); $page_links = paginate_links(array( 'base' => add_query_arg('paged', '%#%'), 'format' => '', 'prev_text' => __('«'), 'next_text' => __('»'), 'total' => $num_pages, 'current' => $pagenum )); if ($page_links) { echo '
'; $page_links_text = sprintf(''.__('Displaying %s–%s of %s', '404-notifier').'%s', number_format_i18n(($pagenum-1) * $per_page+1), number_format_i18n(min($pagenum * $per_page, $overall_count)), number_format_i18n($overall_count), $page_links ); echo $page_links_text.'
'; } echo(' '); $rowclass = ' class="alternate"'; foreach ($events as $event) { $rowclass = ' class="alternate"' == $rowclass ? '' : ' class="alternate"'; echo(' '); } echo('
'.__('404 URL', '404-notifier').' '.__('Referring URL', '404-notifier').' '.__('Remote Address', '404-notifier').' '.__('Remote Host', '404-notifier').' '.__('User Agent', '404-notifier').' '.__('Date', '404-notifier').'
'.__('404 URL', '404-notifier').' '.__('Referring URL', '404-notifier').' '.__('Remote Address', '404-notifier').' '.__('Remote Host', '404-notifier').' '.__('User Agent', '404-notifier').' '.__('Date', '404-notifier').'
'.esc_html($event->url_404).' '.(isset($event->url_refer) && !empty($event->url_refer) ? ''.esc_html($event->url_refer).'' : ''.__('N/A', '404-notifier').'').' '.(isset($event->remote_addr) && !empty($event->remote_addr) ? esc_html($event->remote_addr) : ''.__('N/A', '404-notifier').'').' '.(isset($event->remote_host) && !empty($event->remote_host) ? esc_html($event->remote_host) : ''.__('N/A', '404-notifier').'').' '.(isset($event->user_agent) && !empty($event->user_agent) ? esc_html($event->user_agent) : ''.__('N/A', '404-notifier').'').' '.esc_html(mysql2date('D, d M Y H:i:s', $event->date_gmt, false)).'
'); if ($page_links) { echo '
'.$page_links_text.'
'; } } // $overall_count > 0 else { echo '

'.__('No logs to display…', '404-notifier').'

'; } echo '
'; CF_Admin::callouts('404-notifier'); } function options_form() { echo('
'); CF_Admin::admin_header(__('404 Notifier Options', '404-notifier'), '404-Notifier', N404_Version, '404-notifier'); echo('
mail_enabled, '1', false).'/>

'.wp_nonce_field('404-notifier', '_wpnonce', true, false).' '.wp_referer_field(false).'

'); CF_Admin::callouts('404-notifier'); echo('
'); } function rss_feed() { global $wpdb; $events = $wpdb->get_results(" SELECT * FROM $wpdb->ak_404_log ORDER BY date_gmt DESC LIMIT $this->rss_limit "); header('Content-type: text/xml; charset='.get_option('blog_charset'), true); echo ''; ?> <?php _e('404 Report for: ', '404-notifier'); bloginfo_rss('name'); ?> 0) { foreach ($events as $event) { $content = '

'.__('404 URL: ', '404-notifier').''.esc_url($event->url_404).'

'."\n".'

'.__('Referring URL: ', '404-notifier').''.esc_url($event->url_refer).'

'.__('User Agent: ', '404-notifier').esc_html($event->user_agent).'

'; ?> <![CDATA[<?php echo('404: '.esc_html($event->url_404)); ?>]]> url_404)); ?>]]> date_gmt, false); ?> id); ?> ]]> ]]>
get_col(" SHOW TABLES LIKE '$wpdb->ak_404_log' "); if (!in_array($wpdb->ak_404_log, $tables)) { $ak404->install(); } } function ak404_init() { global $ak404; $ak404 = new ak_404; $ak404->get_settings(); } add_action('init', 'ak404_init'); function ak404_log() { if (is_404()) { global $ak404; if (!is_a($ak404, 'ak_404')) { $ak404 = new ak_404; } $ak404->log_404(); } } add_action('shutdown', 'ak404_log'); function ak404_admin_menu() { add_submenu_page( 'index.php', __('404 Notifier Logs', '404-notifier'), __('404 Logs', '404-notifier'), 'manage_options', basename(N404_FILE), 'ak404_dashboard_page' ); add_options_page( __('404 Notifier Options', '404-notifier'), __('404 Notifier', '404-notifier'), 'manage_options', basename(N404_FILE), 'ak404_options_form' ); } add_action('admin_menu', 'ak404_admin_menu'); function ak404_dashboard_page() { global $ak404; $ak404->dashboard_page(); } function ak404_options_form() { global $ak404; $ak404->options_form(); } function ak404_request_handler() { global $ak404; if (!empty($_POST['ak_action'])) { switch($_POST['ak_action']) { case 'update_404_settings': if (!check_admin_referer('404-notifier')) { die(); } $ak404->update_settings(); header('Location: '.admin_url('options-general.php?page='.basename(__FILE__).'&updated=true')); die(); break; } } if (!empty($_GET['ak_action'])) { switch($_GET['ak_action']) { case '404_feed': $ak404->rss_feed(); break; } } } add_action('init', 'ak404_request_handler', 99); function ak404_plugin_action_links($links, $file) { return CF_Admin::plugin_action_links($links, $file, N404_FILE, '404-notifier'); } add_filter('plugin_action_links', 'ak404_plugin_action_links', 10, 2); function ak404_main_dashboard_widget() { global $wpdb; $events = $wpdb->get_results(" SELECT * FROM $wpdb->ak_404_log ORDER BY date_gmt DESC LIMIT 5 "); if (count($events) > 0) { echo '

'.__('View all').'

'); } else { echo '

'.__('No logs to display…', '404-notifier').'

'; } } function ak404_add_dashboard_widgets() { wp_add_dashboard_widget('ak404_dashboard_widget', __('Recent 404 Logs', '404-notifier'), 'ak404_main_dashboard_widget'); } add_action('wp_dashboard_setup', 'ak404_add_dashboard_widgets'); //Multisite utility and integration functions function ak404_is_multisite() { return CF_Admin::is_multisite(); } function ak404_is_network_activation() { return CF_Admin::is_network_activation(); } function ak404_activate_for_network() { CF_Admin::activate_for_network('ak404_activate_single'); } function ak404_activate_plugin_for_new_blog($blog_id) { CF_Admin::activate_plugin_for_new_blog(N404_FILE, $blog_id, 'ak404_activate_single'); } add_action( 'wpmu_new_blog', 'ak404_new_blog'); function ak404_switch_blog() { global $wpdb; $wpdb->ak_404_log = $wpdb->prefix.'ak_404_log'; } add_action('switch_blog' , 'ak404_switch_blog'); ?>