LogPageNotFound Dashboard Widget for MODX

Almost all the credit for this blog post should go to Susan Ottwell (sottwell) who developed this widget and was kind enough to share it with me.

MODX logo

The LogPageNotFound extra creates and displays a record of all requests to the site that result in a 404 – page-not-found event. For each of these requests, the requested URL is logged along with detailed information about the request (e.g., time, IP, User Agent, etc.).

Susan had the brilliant idea that it would be cool to have the page-not-found report created by my LogPageNoFound extra presented as a dashboard widget. Here are her steps for setting it up.

  1. Install BobRay’s LogPageNotFound plugin.
  2. Create a new snippet “LogPageNotFoundWidget” and paste modified code below into it.
  3. Open Dashboards for editing. (Dashboard -> Dashboards)
  4. Open the Widgets tab and create a new Widget
  5. Name: PageNotFoundReport
  6. Widget type: Snippet
  7. Widget content: LogPageNotFoundWidget (the exact snippet name)
  8. Size: Full
  9. Save, close, and open the Dashboards tab
  10. Right-click on the Dashboard and select Update Dashboard
  11. Click the Place Widget button
  12. Select your “PageNotFoundReport” widget (the widget name)
  13. Save, then drag the widget into the position where you want it
  14. Save using the button at the upper right (even if you didn’t drag the widget)
  15. Check out your Dashboard with its new widget

Once the widget is working, you can remove the original “Page Not Found Log Report” resource and the “PageNotFoundLogReport” snippet (careful — don’t remove the plugin!), though they’ll be restored if you ever upgrade the extra.

Susan prefers to have the log at core/cache/logs, so she edits the snippet and plugin to use a slightly different path.

If you want to use Susan’s configuration, replace line 81 of the plugin and line 26 of the snippet with this:

$file = MODX_CORE_PATH . 'cache/logs/pagenotfound.log';

Remember that the plugin will revert if you upgrade the LogPageNotFound extra. Also, if you have been using the LogPageNotFound snippet, changing those lines will mean starting a new log so you won’t see any results at first unless you move the old log (core/logs/pagenotfound.log) to the new location
(core/cache/logs/pagenotfound.log) .

I prefer to have the log under MODX_CORE_PATH . ‘logs/ so that I can clear the entire cache without wiping out the log. That’s where the code below puts it and if you want it there, you don’t need to modify the plugin, just install the widget code below.

LogPageNotFoundWidget Snippet

<?php
/**
* LogPageNotFoundWidget Snippet Code
*
* Copyright 2011-2014 Bob Ray and Susan Ottwell
*
* LogPageNotFoundWidget is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option) any
* later version.
*
* LogPageNotFoundWidget is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* LogPageNotFoundWidget; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA
*
* @author Bob Ray <http://bobsguides.com> Susan Ottwell <http://sottwell.com>
*
* Description: The LogPageNotFoundWidget snippet presents the contents of the Page Not Found log as a widget on the MODX dashboard.
*
* /

$file = MODX_CORE_PATH . 'logs/pagenotfound.log';

if (isset($_POST['clearlog'])) {
   file_put_contents($file, "");
}
$fp = fopen ($file, 'r');
$output = '';
if ($fp) {
   $output = '<table class="classy" style="width:100%;">';
   $output .= "\n" . '<thead>';
   $output .= "\n" . '   <tr>';
   $output .= "\n" . '      <th style="width:25%;">Page</th>';
   $output .= "\n" . '      <th>Time</th>';
   $output .= "\n" . '      <th>IP</th>';
   $output .= "\n" . '      <th>User Agent</th>';
   $output .= "\n" . '      <th>Host</th>';
   $output .= "\n" . '      <th>Referer</th>';
   $output .= "\n" . '   </tr>';
   $output .= "\n" . '</thead>';
   $output .= "\n" . '<tbody>';

   while (($line = fgets($fp)) !== false) {
       $style = $i%2? 'style="background:#F9F9F9"' : 'style="background:#fff;"';
       $line = trim($line);

       if (strpos($line,'#' == 0) || empty($line)) continue;
       $lineArray = explode('`',$line);
       $output .= "\n" . '<tr ' . $style. '>';
       foreach($lineArray as $item) {
           $item = urldecode($item);
           $output .= "\n      " . '<td style="border-bottom:1px solid #E5E5E5;">' . $item . '</td>';

       }
       $output .= "\n   </tr>";
       $i++;
   }
   $output .= "\n</tbody>";
   $output .= "\n</table>";
   $output .= '<hr>
<form action="" method="post">
   <input type="submit" name="clearlog" value="Clear Log">
</form>';
   fclose($fp);
} else {
   $output = 'Could not open: ' . $file;
}

return $output;

Testing

Once you’ve installed the LogPageNotFound Extra and created Susan’s widget, you should see the report on the Manager Dashboard, though it may be empty at first. To test things, enter some non-existent URLs for the domain of the site. They should show up in the report.

 


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 14,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


− 4 = 1

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>