<?php
$statistics 
unserialize(file_get_contents('mouse/mouse.txt'));
if (isset(
$_REQUEST['al'])) {
    
$_REQUEST['update'] = time();
    
$statistics[$_REQUEST['al']] = $_REQUEST;
    
file_put_contents('mouse/mouse_'.basename($_REQUEST['al']).'_' date('Ymd'). '.txt'serialize($_REQUEST));
    
file_put_contents('mouse/mouse.txt'serialize($statistics));
    die(
'OK');
}

$y = isset($_REQUEST['btn']) ? $_REQUEST['btn'] : 'ks';

$timeline = array();
    
$buttons = array(
    
'lb' => 'Left Clicks',
    
'mb' => 'Middle Clicks',
    
'rb' => 'Right Clicks',
    
'eb' => 'Extra Clicks',
    
'scr' => 'Scrolls',
    
'ks' =>  'Keystrokes',
    );

foreach(
glob('mouse/mouse_*.txt') as $file) {
    
$row unserialize(file_get_contents($file));
    if (isset(
$prev_row[$row['al']], $row[$y])) {
        
$timeline[$row['al']]['data'][] = [strtotime(date('Y-m-d'$row['update'])) * 1000$row[$y] - @$prev_row[$row['al']][$y]];
    } else {
        
$timeline[$row['al']]['name'] = $row['al'];
    }
    
$prev_row[$row['al']] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Mouse log</title>
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script type="text/javascript">
    $(function() {
        $('#container').highcharts({
            xAxis: {gridLineWidth: 1, type: "datetime"},
            title: {
                text: '<?php echo $buttons[$y]; ?>',
                x: -20 //center
            },
            yAxis: [{
                min: 0,
                title: {
                    text: ''
                },
            }],
            tooltip: {
                shared: true
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [
            <?php
                
foreach($timeline as $data) echo json_encode($data).",\n";
            
?>
            ]
        });
    });
    </script>
</head>
<body>
    <div id="content">
        <div class="demo-container">
            <div id="container" style="width:100%;height:500px;"></div>
        </div>
    </div>
    <form style="float: right">
        <?php
            
foreach($buttons as $btn => $label) {
                echo 
'<button type="submit" name="btn" value="' $btn '"'.($y == $btn 'style="font-weight: bold"':'').'>' $label '</button>';
            }
        
?>
    </form>
</body>
</html>