Skip to content

Instantly share code, notes, and snippets.

@splitbrain
Created May 27, 2016 08: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 splitbrain/46920f97e4688a2ff6b143a081564fe3 to your computer and use it in GitHub Desktop.
Save splitbrain/46920f97e4688a2ff6b143a081564fe3 to your computer and use it in GitHub Desktop.
log js and non js accesses
<?php
/**
* DokuWiki Plugin jscheck (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <andi@splitbrain.org>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class action_plugin_jscheck extends DokuWiki_Action_Plugin {
private $visitor_id;
private $sequence_id;
/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handle_dokuwiki_started');
$controller->register_hook('TPL_CONTENT_DISPLAY', 'AFTER', $this, 'handle_content_display');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
}
/**
* initialize sequence and visitor
*
* @param Doku_Event $event
* @param $param
*/
public function handle_dokuwiki_started(Doku_Event $event, $param) {
$this->initIDs();
}
/**
* log the non JS visit and output script
*
* @param Doku_Event $event
* @param $param
*/
public function handle_content_display(Doku_Event $event, $param) {
$this->writeLog(false);
$self = DOKU_BASE . 'lib/exe/ajax.php?t='.time();
$sequ = hsc($this->sequence_id);
echo preg_replace('/\s+/', '', "
<script>
/* JsCheck */
jQuery(function(){
jQuery.post(
'$self',
{
call: 'jscheck',
sequence_id: '$sequ'
}
);
});
</script>
");
}
/**
* log the JS visit
*
* @param Doku_Event $event
* @param $param
*/
public function handle_ajax(Doku_Event $event, $param) {
if($event->data != 'jscheck') return;
$event->preventDefault();
$event->stopPropagation();
$this->initIDs();
// "all cache killers known to man"
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$this->writeLog(true);
}
/**
* log the access
*
* @param boolean $isjs is this a javascript call?
*/
private function writeLog($isjs) {
global $conf;
$logfile = $conf['cachedir'] . '/jscheck.log';
$now = explode(' ', microtime());
$now = $now[1].'.'.substr($now[0], 2);
$log = join("\t", array(
$now,
$_SERVER['REMOTE_USER'] ? $_SERVER['REMOTE_USER'] : '-',
$this->visitor_id,
$this->sequence_id,
$isjs ? 'js' : 'all',
$_SERVER['HTTP_USER_AGENT']
));
$log .= "\n";
file_put_contents($logfile, $log, FILE_APPEND);
}
/**
* initialize the visitor and sequence id
*/
private function initIDs() {
global $conf;
if (!isset($_COOKIE['visitorid'])) {
$expire = time() + 3 * 30 * 24 * 60 * 60;
$this->visitor_id = uniqid('v-', true);
setcookie('visitorid', $this->visitor_id, $expire, DOKU_SESSION_PATH, '', ($conf['securecookie'] && is_ssl()),
true);
} else {
$this->visitor_id = $_COOKIE['visitorid'];
}
if(isset($_REQUEST['sequence_id'])) {
$this->sequence_id = $_REQUEST['sequence_id'];
} else {
$this->sequence_id = uniqid('s-', true);
}
}
}
// vim:ts=4:sw=4:et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment