Skip to content

Commit

Permalink
v1.5
Browse files Browse the repository at this point in the history
see CHANGELOG
  • Loading branch information
wera-as committed Aug 11, 2023
1 parent 75624a5 commit 4bee4aa
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 152 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@



## Version 1.5 (August 11, 2023)
### Removed

- Removed the inclusion of external libraries as they are not needed.






## Version 1.4.2 (August 11, 2023)
### Optimized

Expand All @@ -16,6 +26,8 @@





## Version 1.4.1 (August 11, 2023)

### Added
Expand Down
30 changes: 2 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ LogLeaf is a versatile and adaptable PHP class designed to log file download eve
- Define custom CSV column names when logging to a CSV file.
- Optionally log IP addresses of users downloading files using an improved IP detection mechanism that accounts for proxies and load balancers.
- Capture browser and operating system details for each download event.
- Choose between basic (in-house) and advanced (using external libraries) detection methods.
- Define custom error messages for specific scenarios to better suit your application's requirements.

## Requirements

- PHP 5.6 or higher.
- For advanced browser and OS detection:
- [Mobile Detect](https://github.com/serbanghita/Mobile-Detect) library.
- [Browser.php](https://github.com/cbschuld/Browser.php) library.

## Installation

Expand All @@ -48,44 +44,23 @@ For PHP 7.0+:
include_once 'php70/LogLeaf.php';
```

3. (Optional) For advanced detection, download and include the following libraries:

- [Mobile Detect](https://github.com/serbanghita/Mobile-Detect)
- [Browser.php](https://github.com/cbschuld/Browser.php)

## Usage

Instantiate the LogLeaf class with your log file's name, file type (either 'txt' or 'csv'), and, if desired, specify the timestamp format, CSV columns, and flags for IP and Browser/OS logging.

For TXT logging:

```php
$loggerTxt = new LogLeaf("downloads.txt", 'txt', 'Y-m-d H:i:s', [], true, true, false);
```

Here, it will log using basic detection.

```php
$loggerTxtAdvanced = new LogLeaf("downloads.txt", 'txt', 'Y-m-d H:i:s', [], true, true, true, 'path/to/MobileDetect.php', 'path/to/Browser.php');
$loggerTxt = new LogLeaf("downloads.txt", 'txt', 'Y-m-d H:i:s', [], true, true);
```

Here, it will log using advanced detection.

For CSV logging:

```php
$csvColumns = ['Timestamp', 'IP', 'Browser', 'OS', 'File'];
$loggerCsv = new LogLeaf("downloads.csv", 'csv', 'Y-m-d H:i:s', $csvColumns, true, true, false);
```

This will create a CSV logger with basic detection.

```php
$loggerCsvAdvanced = new LogLeaf("downloads.csv", 'csv', 'Y-m-d H:i:s', $csvColumns, true, true, true, 'path/to/MobileDetect.php', 'path/to/Browser.php');
$loggerCsv = new LogLeaf("downloads.csv", 'csv', 'Y-m-d H:i:s', $csvColumns, true, true);
```

This will create a CSV logger using advanced detection.

If a custom timestamp format is required, set it using the setTimestampFormat method:

```php
Expand All @@ -111,7 +86,6 @@ The Logger class will throw exceptions in the following scenarios:
- Inability to read or write to the log file.
- Providing an empty file name.
- Mismatch between data provided in the `putLog` method and the specified CSV columns.
- Choosing advanced detection without having the required external libraries.
- Allows users to define custom error messages for specific error scenarios, offering a more tailored logging experience.

## Contributing
Expand Down
83 changes: 21 additions & 62 deletions src/php56/LogLeaf.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ class LogLeaf
*/
private $csvColumns;

/**
* @var bool Use advanced detection method
*/
private $useAdvancedDetection;

/**
* @var string Path to Mobile_Detect library
*/
private $mobileDetectPath;

/**
* @var string Path to Browser library
*/
private $browserDetectPath;

/**
* @var int Week of the last rotation.
*/
Expand Down Expand Up @@ -75,7 +60,7 @@ class LogLeaf
* @param string $browserDetectPath Path to Browser library (optional)
* @throws InvalidArgumentException If the file name is empty
*/
public function __construct($filename, $fileType, $timestampFormat = 'Y-m-d H:i:s', $csvColumns = array(), $logIP = false, $logBrowserOS = false, $useAdvancedDetection = false, $mobileDetectPath = '', $browserDetectPath = '')
public function __construct($filename, $fileType, $timestampFormat = 'Y-m-d H:i:s', $csvColumns = array(), $logIP = false, $logBrowserOS = false)
{
if (empty($filename)) {
throw new InvalidArgumentException($this->errorMessages['emptyFilename']);
Expand All @@ -89,9 +74,6 @@ public function __construct($filename, $fileType, $timestampFormat = 'Y-m-d H:i:
$this->timestampFormat = $timestampFormat;
$this->fileType = $fileType;
$this->csvColumns = $csvColumns;
$this->useAdvancedDetection = $useAdvancedDetection;
$this->mobileDetectPath = $mobileDetectPath;
$this->browserDetectPath = $browserDetectPath;
$this->lastRotationWeek = (int) date('W');

if ($logIP) {
Expand Down Expand Up @@ -211,28 +193,16 @@ public function getLog()
*/
private function getBrowser($user_agent)
{
if ($this->useAdvancedDetection && file_exists($this->browserDetectPath)) {
include_once $this->browserDetectPath;
$browser = new Browser($user_agent);
$detectedBrowser = $browser->getBrowser();
if ($detectedBrowser) {
return $detectedBrowser;
} else {
return $this->errorMessages['browserDetectionFailed'];
}
if (strpos($user_agent, 'Firefox') !== false) {
return 'Firefox';
} elseif (strpos($user_agent, 'Chrome') !== false) {
return 'Chrome';
} elseif (strpos($user_agent, 'Safari') !== false) {
return 'Safari';
} elseif (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Trident') !== false) {
return 'Internet Explorer';
} else {
// Fallback to basic in-house method
if (strpos($user_agent, 'Firefox') !== false) {
return 'Firefox';
} elseif (strpos($user_agent, 'Chrome') !== false) {
return 'Chrome';
} elseif (strpos($user_agent, 'Safari') !== false) {
return 'Safari';
} elseif (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Trident') !== false) {
return 'Internet Explorer';
} else {
return 'Others';
}
return 'Others';
}
}

Expand All @@ -244,29 +214,18 @@ private function getBrowser($user_agent)
*/
private function getOS($user_agent)
{
if ($this->useAdvancedDetection && file_exists($this->mobileDetectPath)) {
include_once $this->mobileDetectPath;
$detect = new Mobile_Detect();
$detectedOS = $detect->getOperatingSystem();
if ($detectedOS) {
return $detectedOS;
} else {
return $this->errorMessages['osDetectionFailed'];
}
if (strpos($user_agent, 'Windows NT') !== false) {
return 'Windows';
} elseif (strpos($user_agent, 'Mac OS X') !== false) {
return 'MacOS';
} elseif (strpos($user_agent, 'Linux') !== false) {
return 'Linux';
} elseif (strpos($user_agent, 'iPhone') !== false || strpos($user_agent, 'iPad') !== false) {
return 'iOS';
} elseif (strpos($user_agent, 'Android') !== false) {
return 'Android';
} else {
if (strpos($user_agent, 'Windows NT') !== false) {
return 'Windows';
} elseif (strpos($user_agent, 'Mac OS X') !== false) {
return 'MacOS';
} elseif (strpos($user_agent, 'Linux') !== false) {
return 'Linux';
} elseif (strpos($user_agent, 'iPhone') !== false || strpos($user_agent, 'iPad') !== false) {
return 'iOS';
} elseif (strpos($user_agent, 'Android') !== false) {
return 'Android';
} else {
return 'Others';
}
return 'Others';
}
}

Expand Down
84 changes: 22 additions & 62 deletions src/php70/LogLeaf.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ class LogLeaf
*/
private $csvColumns;

/**
* @var bool Use advanced detection method
*/
private $useAdvancedDetection;

/**
* @var string Path to Mobile_Detect library
*/
private $mobileDetectPath;

/**
* @var string Path to Browser library
*/
private $browserDetectPath;

/**
* @var int Week of the last rotation.
*/
Expand Down Expand Up @@ -75,7 +60,7 @@ class LogLeaf
* @param string $browserDetectPath Path to Browser library (optional)
* @throws InvalidArgumentException If the file name is empty
*/
public function __construct($filename, $fileType, $timestampFormat = 'Y-m-d H:i:s', $csvColumns = [], $logIP = false, $logBrowserOS = false, $useAdvancedDetection = false, $mobileDetectPath = '', $browserDetectPath = '')
public function __construct($filename, $fileType, $timestampFormat = 'Y-m-d H:i:s', $csvColumns = [], $logIP = false, $logBrowserOS = false)
{
if (empty($filename)) {
throw new InvalidArgumentException($this->errorMessages['emptyFilename']);
Expand All @@ -89,9 +74,6 @@ public function __construct($filename, $fileType, $timestampFormat = 'Y-m-d H:i:
$this->timestampFormat = $timestampFormat;
$this->fileType = $fileType;
$this->csvColumns = $csvColumns;
$this->useAdvancedDetection = $useAdvancedDetection;
$this->mobileDetectPath = $mobileDetectPath;
$this->browserDetectPath = $browserDetectPath;
$this->lastRotationWeek = (int) date('W');

if ($logIP) {
Expand Down Expand Up @@ -211,31 +193,20 @@ public function getLog()
*/
private function getBrowser($user_agent)
{
if ($this->useAdvancedDetection && file_exists($this->browserDetectPath)) {
include_once $this->browserDetectPath;
$browser = new Browser($user_agent);
$detectedBrowser = $browser->getBrowser();
if ($detectedBrowser) {
return $detectedBrowser;
} else {
return $this->errorMessages['browserDetectionFailed'];
}
if (strpos($user_agent, 'Firefox') !== false) {
return 'Firefox';
} elseif (strpos($user_agent, 'Chrome') !== false) {
return 'Chrome';
} elseif (strpos($user_agent, 'Safari') !== false) {
return 'Safari';
} elseif (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Trident') !== false) {
return 'Internet Explorer';
} else {
// Fallback to basic in-house method
if (strpos($user_agent, 'Firefox') !== false) {
return 'Firefox';
} elseif (strpos($user_agent, 'Chrome') !== false) {
return 'Chrome';
} elseif (strpos($user_agent, 'Safari') !== false) {
return 'Safari';
} elseif (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Trident') !== false) {
return 'Internet Explorer';
} else {
return 'Others';
}
return 'Others';
}
}


/**
* Get OS from user agent string
*
Expand All @@ -244,29 +215,18 @@ private function getBrowser($user_agent)
*/
private function getOS($user_agent)
{
if ($this->useAdvancedDetection && file_exists($this->mobileDetectPath)) {
include_once $this->mobileDetectPath;
$detect = new Mobile_Detect();
$detectedOS = $detect->getOperatingSystem();
if ($detectedOS) {
return $detectedOS;
} else {
return $this->errorMessages['osDetectionFailed'];
}
if (strpos($user_agent, 'Windows NT') !== false) {
return 'Windows';
} elseif (strpos($user_agent, 'Mac OS X') !== false) {
return 'MacOS';
} elseif (strpos($user_agent, 'Linux') !== false) {
return 'Linux';
} elseif (strpos($user_agent, 'iPhone') !== false || strpos($user_agent, 'iPad') !== false) {
return 'iOS';
} elseif (strpos($user_agent, 'Android') !== false) {
return 'Android';
} else {
if (strpos($user_agent, 'Windows NT') !== false) {
return 'Windows';
} elseif (strpos($user_agent, 'Mac OS X') !== false) {
return 'MacOS';
} elseif (strpos($user_agent, 'Linux') !== false) {
return 'Linux';
} elseif (strpos($user_agent, 'iPhone') !== false || strpos($user_agent, 'iPad') !== false) {
return 'iOS';
} elseif (strpos($user_agent, 'Android') !== false) {
return 'Android';
} else {
return 'Others';
}
return 'Others';
}
}

Expand Down

0 comments on commit 4bee4aa

Please sign in to comment.