Skip to content

Instantly share code, notes, and snippets.

@sottwell
Last active December 21, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sottwell/874cd0edc17d9a714aa9 to your computer and use it in GitHub Desktop.
Save sottwell/874cd0edc17d9a714aa9 to your computer and use it in GitHub Desktop.
Page Not Found Report Dashboard Widget to work with BobRay's LogPageNotFound extra - MODX Revolution
<?php
/**
* PageNotFoundLogReport
* Copyright 2011-2013 Bob Ray
*
* PageNotFoundLogReport 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.
*
* PageNotFoundLogReport 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
* PageNotFoundLogReport; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA
*
* @package logpagenotfound
* @author Bob Ray <http://bobsguides.com>
*
* Description: The PageNotFoundLogReport snippet presents the contents of the Page Not Found log as a table.
*
* /
/* Modified: January, 2013 by sottwell for use as a Dashboard widget */
$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 style="background:#DDE3EA;color:#000;">';
$output .= "\n" . ' <th style="width:25%;padding:8px 0;text-align:center;">Page</th>';
$output .= "\n" . ' <th style="width:15%;text-align:center;">Time</th>';
$output .= "\n" . ' <th style="text-align:center;">IP</th>';
$output .= "\n" . ' <th style="width:25%;text-align:center;">User Agent</th>';
$output .= "\n" . ' <th style="text-align:center;">Host</th>';
$output .= "\n" . ' <th style="text-align:center;">Referer</th>';
$output .= "\n" . ' </tr>';
$output .= "\n" . '</thead>';
$output .= "\n" . '<tbody>';
while (($line = fgets($fp)) !== false) {
$style = $i%2? 'style="background:#F9F9F9;border:1px solid #000;"' : '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);
$item = htmlentities($item, ENT_QUOTES);
$output .= "\n " . '<td style="padding:0 1em;">' . $item . '</td>';
}
$output .= "\n </tr>";
}
$output .= "\n</tbody>";
$output .= "\n</table>";
$output .= '<hr>
<form action="" method="post">
<input type="submit" name="clearlog" value="Clear Log">
</form><br>';
fclose($fp);
} else {
$output = 'Could not open: ' . $file;
}
return $output;
@sottwell
Copy link
Author

This widget code is a variant of BobRay's report snippet for his LogPageNotFound package. It can simply be pasted into an Inline PHP Widget-type widget's content field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment