From 40693949fe60e5e559bc91151b7c98c6973afbbf Mon Sep 17 00:00:00 2001 From: MHerrlein Date: Tue, 18 Jul 2023 15:24:39 +0200 Subject: [PATCH] first init --- .gitignore | 1 + changelog.md | 8 ++++ composer.json | 33 +++++++++++++ contributing.md | 3 ++ license.md | 21 ++++++++ phpunit.xml | 22 +++++++++ readme.md | 48 +++++++++++++++++++ src/Console/SendPing.php | 32 +++++++++++++ src/LaravelVersionTracker.php | 90 +++++++++++++++++++++++++++++++++++ src/ServiceProvider.php | 29 +++++++++++ 10 files changed, 287 insertions(+) create mode 100644 .gitignore create mode 100644 changelog.md create mode 100644 composer.json create mode 100644 contributing.md create mode 100644 license.md create mode 100644 phpunit.xml create mode 100644 readme.md create mode 100644 src/Console/SendPing.php create mode 100644 src/LaravelVersionTracker.php create mode 100644 src/ServiceProvider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09815ba --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +lastPing.txt diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..a0acfa9 --- /dev/null +++ b/changelog.md @@ -0,0 +1,8 @@ +# Changelog + +All notable changes to `LaravelVersionTracker` will be documented in this file. + +## Version 1.0 + +### Added +- Everything diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6839606 --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "herrleinit/laravelversiontracker", + "description": "This package allows sending the Laravel version via API to a server and storing it there.", + "license": "MIT", + "version": "1.0.0", + "authors": [ + { + "name": "Martin Herrlein", + "email": "info@herrlein.it", + "homepage": "https://www.herrlein.it" + } + ], + "keywords": ["Laravel", "LaravelVersionTracker"], + "require": { + "php": ">=7.2", + "guzzlehttp/guzzle": "*", + "laravel/framework": ">=7.*" + }, + "require-dev": { + }, + "autoload": { + "psr-4": { + "Herrleinit\\LaravelVersionTracker\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Herrleinit\\LaravelVersionTracker\\ServiceProvider" + ] + } + } +} diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..d8a1c98 --- /dev/null +++ b/contributing.md @@ -0,0 +1,3 @@ +# Contributing + +this Repo is private. If you want to contribute, mail to info@herrlein.it diff --git a/license.md b/license.md new file mode 100644 index 0000000..c2abeb4 --- /dev/null +++ b/license.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2013 Martin Herrlein + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..ce34605 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,22 @@ + + + + + ./tests/ + + + + + src/ + + + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..fc9a7ba --- /dev/null +++ b/readme.md @@ -0,0 +1,48 @@ +# LaravelVersionTracker + +track the version of your laravel application to a specific server + +## Installation + +Via Composer + +setup vcs repository before +``` bash +$ composer require herrleinit/laravelversiontracker +``` + +## Usage +Setup .env for +``` bash +TRACKER_SERVER_URL=https://yourserver.com +TRACKER_TOKEN=yourtoken for bearer auth +``` +Once the package is installed, you need nothing more to do. The package will automatically track the version of your application to the server. + +```php + +## Change log + +Please see the [changelog](changelog.md) for more information on what has changed recently. + +## Testing + +``` bash +$ composer test +``` + +## Contributing + +Please see [contributing.md](contributing.md) for details and a todolist. + +## Security + +If you discover any security related issues, please email info@herrlein.it instead of using the issue tracker. + +## Credits + +- Martin Herrlein https://www.herrlein.it + +## License + +MIT. Please see the [license file](license.md) for more information. diff --git a/src/Console/SendPing.php b/src/Console/SendPing.php new file mode 100644 index 0000000..d50e8cb --- /dev/null +++ b/src/Console/SendPing.php @@ -0,0 +1,32 @@ +run(); + return 0; + } +} diff --git a/src/LaravelVersionTracker.php b/src/LaravelVersionTracker.php new file mode 100644 index 0000000..c075a10 --- /dev/null +++ b/src/LaravelVersionTracker.php @@ -0,0 +1,90 @@ +checkInterval()) return false; + $arrData = $this->gatherData(); + $response = $this->sendData($arrData); + return true; + } + + private function checkInterval(): bool + { + $path = __DIR__.'/../lastPing.txt'; + $lastPing = false; + if(file_exists($path))$lastPing = file_get_contents($path); + if(empty($lastPing)) + { + file_put_contents($path,now()->toDateTimeString()); + return true; + } + $now = now(); + + switch(env('TRACKER_INTERVAL')) + { + case 'daily': + $nextPing = Carbon::createFromTimeString($lastPing)->addDay(); + break; + case 'weekly': + $nextPing = Carbon::createFromTimeString($lastPing)->addWeek(); + break; + case 'monthly': + $nextPing = Carbon::createFromTimeString($lastPing)->addMonth(); + break; + case 'yearly': + $nextPing = Carbon::createFromTimeString($lastPing)->addYear(); + break; + default: + $nextPing = Carbon::createFromTimeString($lastPing)->addMonth(); + break; + } + + $diff = $now->diffInMinutes($nextPing,false); + if($diff > 0) return false; + unlink($path); + file_put_contents($path,$now->toDateTimeString()); + return true; + } + + public function gatherData(): array + { + return [ + 'version' => app()->version(), + 'phpversion' => phpversion(), + 'appname' => config('app.name'), + 'url' => config('app.url'), + 'packages' => $this->getInstalledPackages(), + 'audits' => $this->getComposerAudits(), + ]; + } + + private function sendData($arrData): \Illuminate\Http\Client\Response + { + return Http::retry(3,250) + ->withToken(env('TRACKER_TOKEN')) + ->post(env('TRACKER_SERVER'), $arrData); + } + + private function getInstalledPackages(): string + { + $base = base_path().'/'; + return shell_exec("cd $base && composer show -f json"); + } + + private function getComposerAudits(): string | null + { + $base = base_path().'/'; + $composerAudit = shell_exec("cd $base && composer audit -f json"); + $arrAudit = json_decode($composerAudit); + return !empty($arrAudit->advisories) ? json_encode($arrAudit->advisories) : null; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..508d01e --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,29 @@ +run(); + } + + /** + * Register any package services. + * + * @return void + */ + public function register(): void + { + + } +}