From fc203d55bd33277d3754be84da2602c9dc379f8b Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 4 Feb 2020 00:28:51 -0500 Subject: [PATCH] tidy up code in counter --- counter/gcount.php | 315 ++++++++++++++++++++--------------------- counter/logs/index.txt | 2 +- 2 files changed, 157 insertions(+), 160 deletions(-) diff --git a/counter/gcount.php b/counter/gcount.php index 3903e81..f78c312 100644 --- a/counter/gcount.php +++ b/counter/gcount.php @@ -1,159 +1,156 @@ -?page=PAGEID parameter, for example gcount.php?page=test'); -} - -// Remove any illegal chars from the page ID -$page = preg_replace('/[^a-zA-Z0-9\-_\.]/','',$_GET['page']); - -// Stop if $page is not valid -if ( ! strlen($page) ) -{ - die('ERROR: Page ID is missing or contains only invalid chars. Please use only these chars for the page ID: a-z, A-Z, 0-9, ".", "-" and "_"'); -} - -// Get style and extension information -$style = isset($_GET['style']) ? preg_replace('/[^a-zA-Z0-9\-_\.]/','',$_GET['style']) : $default_style; -$ext = isset($_GET['ext']) ? preg_replace('/[^a-zA-Z0-9\-_\.]/','',$_GET['ext']) : $default_ext; - -// Set values for cookie, log file and style flye names -$cname = 'gcount_unique_'.$page; -$logfile = 'logs/' . $page . '.txt'; -$style_dir = 'styles/' . $style . '/'; - -// Does the log file exist? -if ( ! file_exists($logfile) ) -{ - die('ERROR: Log file not found. Make sure there is a file called ' . $page . '.txt inside your logs folder. On most servers file names are CaSe SeNSiTiVe!'); -} - -// Open log file for reading and writing -if ($fp = @fopen($logfile, 'r+')) -{ - // Lock log file from other scripts - $locked = flock($fp, LOCK_EX); - - // Lock successful? - if ($locked) - { - // Let's read current count - $count = intval( trim( fread($fp, filesize($logfile) ) ) ); - - // If counting unique hits is enabled make sure it's a unique hit - if ( $count_unique == 0 || ! isset($_COOKIE[$cname]) ) - { - // Update count by 1 and write the new value to the log file - $count = $count + 1; - rewind($fp); - fwrite($fp, $count); - - // Print the Cookie and P3P compact privacy policy - header('P3P: CP="NOI NID"'); - setcookie($cname, 1, time()+60*60*$unique_hours); - } - } - else - { - // Lock not successful. Better to ignore than to damage the log file - $count = 1; - } - - // Release file lock and close file handle - flock($fp, LOCK_UN); - fclose($fp); -} -else -{ - die("ERROR: Can't read/write to the log file ($logfile). Make sure this file is writable by PHP scripts. On UNIX servers, CHMOD the log file to 666 (rw-rw-rw-)."); -} - -// Is zero-padding enabled? If yes, add zeros if required -if ($min_digits) -{ - $count = sprintf('%0'.$min_digits.'s', $count); -} - -// Print out Javascript code and exit -$len = strlen($count); -for ($i=0; $i<$len; $i++) -{ - echo 'document.write(\'\');'; -} - -exit(); -?> +?page=PAGEID parameter, for example gcount.php?page=test'); +} + +// Remove any illegal chars from the page ID +$page = preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $_GET['page']); + +// Stop if $page is not valid +if (!strlen($page)) { + die('ERROR: Page ID is missing or contains only invalid chars. Please use only these chars for the page ID: a-z, A-Z, 0-9, ".", "-" and "_"'); +} + +// Get style and extension information +$style = isset($_GET['style']) + ? preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $_GET['style']) + : $default_style; + +$ext = isset($_GET['ext']) + ? preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $_GET['ext']) + : $default_ext; + +// Set values for cookie, log file and style flye names +$cname = 'gcount_unique_'.$page; +$logfile = 'logs/' . $page . '.txt'; +$style_dir = 'styles/' . $style . '/'; + +// Does the log file exist? +if (!file_exists($logfile)) { + die('ERROR: Log file not found. Make sure there is a file called ' . $page . '.txt inside your logs folder. On most servers file names are CaSe SeNSiTiVe!'); +} + +// Open log file for reading and writing +if ($fp = @fopen($logfile, 'r+')) { + // Lock log file from other scripts + $locked = flock($fp, LOCK_EX); + + // Lock successful? + if ($locked) { + // Let's read current count + $count = intval(trim(fread($fp, filesize($logfile)))); + + // If counting unique hits is enabled make sure it's a unique hit + if ($count_unique == 0 || ! isset($_COOKIE[$cname])) { + // Update count by 1 and write the new value to the log file + $count = $count + 1; + rewind($fp); + fwrite($fp, $count); + + // Print the Cookie and P3P compact privacy policy + header('P3P: CP="NOI NID"'); + setcookie($cname, 1, time()+60*60*$unique_hours); + } + } + else { + // Lock not successful. Better to ignore than to damage the log file + $count = 1; + } + + // Release file lock and close file handle + flock($fp, LOCK_UN); + fclose($fp); +} +else { + die("ERROR: Can't read/write to the log file ($logfile). Make sure this file is writable by PHP scripts. On UNIX servers, CHMOD the log file to 666 (rw-rw-rw-)."); +} + +// Is zero-padding enabled? If yes, add zeros if required +if ($min_digits) { + $count = sprintf('%0' . $min_digits . 's', $count); +} + +// Print out Javascript code and exit +$len = strlen($count); +for ($i = 0; $i < $len; $i++) { + $url = $base_url . $style_dir . substr($count, $i, 1) . '.' . $ext; + echo 'document.write(\'\');'; +} + +exit(); +?> + diff --git a/counter/logs/index.txt b/counter/logs/index.txt index 58dc5af..4f37670 100644 --- a/counter/logs/index.txt +++ b/counter/logs/index.txt @@ -1 +1 @@ -303 \ No newline at end of file +333 \ No newline at end of file