LogPageNotFound Widget

Susan Ottwell's adaptation of my LogPageNotFound extra puts the page-not-found report in a Manager dashboard widget


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.

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. System -> 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

LogPageNotFoundWidget Snippet

<?php
/**
* LogPageNotFoundWidget Snippet Code
*
* Copyright 2011-2023 Susan Ottwell and Bob Ray
*
* 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 Susan Ottwell <https://sottwell.com> Bob Ray <https://bobsguides.com> 
*
* Description: The LogPageNotFoundWidget snippet presents the contents of the 
* Page Not Found Log Report as a widget on the MODX dashboard.
*
*/

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

if (isset($_POST['clearlog'])) {
   file_put_contents($file, "");
}
$fp = fopen ($file, 'r');
$output = '';
 $output .= '<br><form action="" method="post">
   <input type="submit" name="clearlog" value="Clear Log">
</form><hr>';

if ($fp) {
   $output .= '<table class="classy" style="width:100%;">';
   $output .= "\n" . '<thead>';
   $output .= "\n" . '   <tr>';
   $output .= "\n" . '     <th style="width:25%;"><b>Page</b></th>';
   $output .= "\n" . '      <th><b>Time</b></th>';
   $output .= "\n" . '      <th><b>IP</b></th>';
   $output .= "\n" . '      <th><b>User Agent</b></th>';
   $output .= "\n" . '      <th><b>Host</b></th>';
   $output .= "\n" . '      <th><b>Referer</b></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 = strip_tags($item);
           $item = urldecode($item);
           $item = htmlspecialchars($item, ENT_QUOTES, 'UTF-8');
           $output .= "\n      " . '<td style="border-bottom:1px solid #E5E5E5;">' . $item . '</td>';

       }
       $output .= "\n   </tr>";
       $i++;
   }
   $output .= "\n</tbody>";
   $output .= "\n</table>";

   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.



Comments (4)

  1. Susan OttwellAug 21, 2015 at 11:05 AM

    The widget will cause an infinite loop of error messages, until the server shuts it down, leading to a Manager white-page if the core/logs directory is lost, perhaps in an upgrade. It needs a check for the existence of the path.

  2. Susan OttwellAug 21, 2015 at 11:12 AM

    Sorry... wrong widget being blamed for the loop.

  3. adam enthovenAug 16, 2017 at 06:22 AM

    Hi, I can't get it to work in 2.5.7-pl. the widget just won't show up in the dashboard. Can you confirm it should work in Modx 2.5.7-pl?
    tnx

  4. Bob RayJun 12, 2018 at 05:34 PM

    Sorry, I don't have any way to test it in that version.


Please login to comment.

  (Login)