Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <!--
- Author: Twily 2015
- -->
- <?php
- error_reporting(0);
- date_default_timezone_set("Europe/Oslo");
- /*
- * Log Format:
- * <UNIX Timestamp>|<Filename>\n
- *
- * Example (analytics.log):
- * ...
- * 1425943113|firefox-css
- * 1425943138|4chan-css
- * 1425943190|homepage
- * ...
- */
- $file ="./analytics.log";
- $tNow =time();
- //$cDate =filectime($file);
- $cDate =1425945438;
- $age =$tNow-$cDate;
- if($age<=86400) { // <= 24 hours
- $aText=" hour";
- $age=floor($age/60/60);
- } elseif($age>86400 && $age<=588000) { // <= 7 days
- $aText=" day";
- $age=floor($age/60/60/24);
- } else { // > 7 days
- $aText=" week";
- $age=floor($age/60/60/24/7);
- }
- $aText .=($age<>1)?'s':'';
- // Offset declarations
- $tHour =$tNow-(60*60);
- $tDay =$tNow-(60*60*24);
- $tWeek =$tNow-(60*60*24*7);
- $tMonth =$tNow-(60*60*24*30);
- $tYear =$tNow-(60*60*24*365);
- $tToday =strtotime('00:00:00');
- // Read log line by line into multidimensional f(ull)Arr(ay)
- $fArr=array();
- $i=0;
- $fh=fopen($file,'r') or die("Failed to read log file");
- if($fh) {
- while(!feof($fh)) {
- $buffer=explode('|',fgets($fh,4096));
- if($buffer[0]>=$tWeek) { // Maximum time frame
- $fArr[$i]['time']=$buffer[0];
- $fArr[$i]['name']=substr($buffer[1],0,-1);
- $i++;
- }
- }
- fclose($fh);
- }
- // Calculate activity/hour between offset and now
- function activity($offset) {
- global $fArr,$max;
- unset($rArr);
- $rArr=array();
- $max=1;
- for($i=0;$i<count($fArr);$i++) {
- if($fArr[$i]['time']>=$offset) {
- $dt=new DateTime('@'.$fArr[$i]['time']);
- $dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
- $hour=intval($dt->format('H'));
- // The index of the r(elevant)Arr(ay) is equal to the current hour of the timestamps day
- $rArr[$hour]+=1;
- // Set highest number of hits
- $max=($max<$rArr[$hour])?$rArr[$hour]:$max;
- }
- }
- return $rArr;
- }
- // Calculate hits/file between offset and now
- function hitcount($offset) {
- global $fArr,$max;
- unset($rArr);
- $rArr=array();
- for($i=0;$i<count($fArr);$i++) {
- // Add everything from offset into r(elevant)Arr(ay)
- if($fArr[$i]['time']>=$offset) array_push($rArr,$fArr[$i]['name']);
- }
- // Sort alphabetically
- // !!(Required for counting the number of repeating equal files in array)!!
- sort($rArr);
- // Calculate hits/file in h(its)Arr(ay)
- unset($hArr);
- $hArr=array();
- $max=1; $j=-1;
- for($i=0;$i<count($rArr);$i++) {
- if($rArr[$i]==$hArr[$j]['name']) {
- // Add +1 to file hit (if equal to last one)
- $hArr[$j]['hits']++;
- // Set highest number of hits
- $max=($max<$hArr[$j]['hits'])?$hArr[$j]['hits']:$max;
- } else {
- // Set next file to 1 hit
- $j++;
- $hArr[$j]['hits']=1;
- }
- $hArr[$j]['name']=$rArr[$i];
- }
- rsort($hArr);
- return $hArr;
- }
- // Generate graph display data/row string
- function generate($hits,$name) {
- global $max;
- $hText =($hits<>1)?' hits':' hit';
- $percent=floor($hits*100/$max);
- $rowStr =" <div class=\"row\">";
- $rowStr.="<div class=\"cell\">".$name."</div>";
- $rowStr.="<div class=\"cell\"><div class=\"bar\" style=\"width: ".$percent."%;\"><span>".$hits.$hText."</span></div></div>";
- $rowStr.="</div>\n";
- return $rowStr;
- }
- ?>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
- <title>twily.info :: analytics</title>
- <style type="text/css">
- html,body {
- font-size: 10pt;
- font-family: "Droid Sans", "DejaVu Sans", "Open Sans", "Liberation Sans", "Segoe UI", Sans;
- background: #17181A;
- color: #CECFD1;
- margin: 0; padding: 12px 15px 12px 12px;
- }
- a:link, a:visited { color: #CB873D; text-decoration: none; }
- a:hover, a:active { color: #F4AA5C; text-decoration: underline; }
- hr {
- margin: 24px auto; padding: 0;
- width: 24px; height: 24px;
- background: #27282B;
- border: 0; border-radius: 24px;
- }
- .table {
- display: table;
- font-family: "Droid Sans Mono", "DejaVu Sans Mono", "Liberation Mono", "Lucida Console", Monospace;
- width: 100%; height: auto;
- background: #111113; color: #AAABAD;
- margin: 0 auto; padding: 3px;
- margin-bottom: 24px;
- }
- .table .row { display: table-row; }
- .table .row .cell {
- display: table-cell;
- height: 20px;
- padding: 1px;
- vertical-align: middle;
- }
- .table .row .cell:nth-child(1) {
- width: 175px;
- text-align: right; padding-right: 6px;
- }
- .table .row .cell .bar {
- height: 20px;
- background: #CB873D;
- border-radius: 1px;
- }
- .table .row .cell .bar span {
- white-space: nowrap; overflow: visible;
- padding-left: 4px;
- color: #111113; font-size: 8pt;
- line-height: 20px;
- }
- .title {
- text-align: center; font-weight: bold;
- width: 100%; height: 18px; line-height: 18px;
- background: #27282B;
- margin: 4px auto; padding: 3px;
- }
- #note { float: left; }
- #back { float: right; }
- </style>
- </head>
- <body>
- <div class="title">Top 10 Last 60 Minutes</div> <!-- ---- 60 Minutes ---- -->
- <div class="table">
- <?php
- // Draw statistics from o(utput)Arr(ay)
- unset($oArr);
- $oArr=array();
- $oArr=hitcount($tHour);
- for($i=0;$i<count($oArr) && $i<10;$i++) echo generate($oArr[$i]['hits'],$oArr[$i]['name']);
- ?>
- </div>
- <div class="title">Top 10 Last 24 Hours</div> <!-- ---- 24 Hours ---- -->
- <div class="table">
- <?php
- // Draw statistics from o(utput)Arr(ay)
- unset($oArr);
- $oArr=array();
- $oArr=hitcount($tDay);
- for($i=0;$i<count($oArr) && $i<10;$i++) echo generate($oArr[$i]['hits'],$oArr[$i]['name']);
- ?>
- </div>
- <div class="title">Top 10 Last 7 Days</div> <!-- ---- 7 Days ---- -->
- <div class="table">
- <?php
- // Draw statistics from o(utput)Arr(ay)
- unset($oArr);
- $oArr=array();
- $oArr=hitcount($tWeek);
- for($i=0;$i<count($oArr) && $i<10;$i++) echo generate($oArr[$i]['hits'],$oArr[$i]['name']);
- ?>
- </div>
- <hr />
- <div class="title">Activity/Hour Last 7 Days <?php echo "(".date("T").")"; ?></div> <!-- ---- Activity 7 Days ---- -->
- <div class="table">
- <?php
- // Draw statistics from o(utput)Arr(ay)
- unset($oArr);
- $oArr=array();
- $oArr=activity($tWeek);
- for($i=0;$i<count($oArr);$i++) echo generate($oArr[$i],sprintf('%02d',$i)." - ".((($i+1)==24)?"00":sprintf('%02d',($i+1)))." ".(($i>=6 && $i<18)?"☀":"☾"));
- ?>
- </div>
- <div id="note">Analytics started <?php echo $age.$aText; ?> ago.</div> <!-- Visitors at this time: 90 746 -->
- <div id="back">
- <a href="javascript:location.reload();" target="_self">Refresh</a> |
- <a href="http://pastebin.com/v3GKeue3" target="_blank">Source</a> |
- <a href="/" target="_self">Return to twily.info</a>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement