Skip to content

Commit

Permalink
Update counter.php
Browse files Browse the repository at this point in the history
Several updates to how file locking works if using 'USE_FLOCK'. Version bumped to 4.0.6. A few changes in an attempt to fix #3
  • Loading branch information
ericsizemore committed Jun 25, 2023
1 parent fc0488b commit 2a7e605
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author Eric Sizemore <[email protected]>
* @package SV's Simple Counter
* @link https://www.secondversion.com/
* @version 4.0.4
* @version 4.0.6
* @copyright (C) 2006 - 2023 Eric Sizemore
* @license GNU Lesser General Public License
*/
Expand All @@ -20,7 +20,7 @@
* @author Eric Sizemore <[email protected]>
* @package SV's Simple Counter
* @link https://www.secondversion.com/
* @version 4.0.4
* @version 4.0.6
* @copyright (C) 2006 - 2023 Eric Sizemore
* @license GNU Lesser General Public License
*
Expand Down Expand Up @@ -126,7 +126,6 @@ private function getIpAddress(bool $trustProxyHeaders = false): string
}
}
}
unset($ips);

if (!$ip AND isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
Expand Down Expand Up @@ -179,20 +178,20 @@ private function isReservedIp(string $ipaddress): bool
*/
private function isPublicIp(string $ipaddress): bool
{
return (bool)(!self::isPrivateIp($ipaddress) AND !self::isReservedIp($ipaddress));
return (!self::isPrivateIp($ipaddress) AND !self::isReservedIp($ipaddress));
}

/**
* We use this function to open and read/write to files.
*
* @param string $file Filename
* @param string $mode Mode (r, w, a, etc..)
* @param string $mode Mode (r, w, a)
* @param string $data If writing to the file, the data to write
* @return mixed
* @return string|false
*
* @throws Exception
*/
private function readWriteFile(string $file, string $mode, string $data = ''): mixed
private function readWriteFile(string $file, string $mode, string $data = ''): string|false
{
if (!\file_exists($file) OR !\is_writable($file)) {
throw new Exception(\sprintf("'%s' does not exist or is not writable.", $file));
Expand All @@ -202,18 +201,43 @@ private function readWriteFile(string $file, string $mode, string $data = ''): m
throw new Exception(\sprintf("'%s' could not be opened.", $file));
}

$return = null;
$return = '';

if (self::USE_FLOCK AND \flock($fp, \LOCK_EX)) {
if ($mode == 'r') {
$return = \fread($fp, \filesize($file));
} else {
\fwrite($fp, $data);
\clearstatcache();

//
$filesize = \filesize($file);

if (self::USE_FLOCK) {
// If using file locking, we will use shared for 'r' mode or exclusive for 'w' or 'a' mode
switch ($mode) {
case 'r':
// Shared lock
if (\flock($fp, \LOCK_SH)) {
$return = \fread($fp, $filesize);
} else {
throw new Exception(\sprintf("Unable to acquire lock on '%s'", $file));
}
break;
case 'a':
case 'w':
// Exclusive lock
if (\flock($fp, \LOCK_EX)) {
\fwrite($fp, $data);
} else {
throw new Exception(\sprintf("Unable to acquire lock on '%s'", $file));
}
break;
default:
// Invalid mode
throw new Exception(\sprintf("Invalid mode '%s' specified, must be either read ('r') or write ('w'/'a')", $mode));
}
// Attempt to release the lock
\flock($fp, \LOCK_UN);
} else {
// We are not using file locks
if ($mode == 'r') {
$return = \fread($fp, \filesize($file));
$return = \fread($fp, $filesize);
} else {
\fwrite($fp, $data);
}
Expand All @@ -223,11 +247,26 @@ private function readWriteFile(string $file, string $mode, string $data = ''): m
return $return;
}

/**
* Gathers a list of IPs and the number of times they occur from the ip log file.
*
* ** currently not in use, to be used for an admin page **
*
* @return array
*/
public function listIps(): array
{
$ips = \trim(self::readWriteFile(self::IP_FILE, 'r'));
$ips = \preg_split("# #", $ips, -1, \PREG_SPLIT_NO_EMPTY);

return \array_count_values($ips);
}

/**
* Processes the visitor (adds to count/etc. if needed) and
* then displays current count.
*/
public function process()
public function process(): void
{
$display = '';

Expand Down Expand Up @@ -269,4 +308,4 @@ public function process()
}

// Instantiate and process.
\Esi\SimpleCounter\Counter::getInstance()->process();
Counter::getInstance()->process();

0 comments on commit 2a7e605

Please sign in to comment.