From 4bee4aae5a45c02496d533ba96d078f710ed886a Mon Sep 17 00:00:00 2001 From: ClearanceClarence Date: Fri, 11 Aug 2023 13:42:09 +0200 Subject: [PATCH] v1.5 see CHANGELOG --- CHANGELOG.md | 12 +++++++ README.md | 30 ++-------------- src/php56/LogLeaf.php | 83 +++++++++++------------------------------- src/php70/LogLeaf.php | 84 ++++++++++++------------------------------- 4 files changed, 57 insertions(+), 152 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 120fc64..2a43586 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -16,6 +26,8 @@ + + ## Version 1.4.1 (August 11, 2023) ### Added diff --git a/README.md b/README.md index dc111fd..dd8a3c2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -48,11 +44,6 @@ 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. @@ -60,32 +51,16 @@ Instantiate the LogLeaf class with your log file's name, file type (either 'txt' 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 @@ -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 diff --git a/src/php56/LogLeaf.php b/src/php56/LogLeaf.php index b3f05b4..e882d54 100644 --- a/src/php56/LogLeaf.php +++ b/src/php56/LogLeaf.php @@ -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. */ @@ -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']); @@ -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) { @@ -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'; } } @@ -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'; } } diff --git a/src/php70/LogLeaf.php b/src/php70/LogLeaf.php index 0eccb50..def8011 100644 --- a/src/php70/LogLeaf.php +++ b/src/php70/LogLeaf.php @@ -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. */ @@ -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']); @@ -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) { @@ -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 * @@ -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'; } }