Laravel-Backpack /
CRUD
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Backpack\CRUD; |
||
| 4 | |||
| 5 | trait Stats |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Check if the application is running in normal conditions |
||
| 9 | * (production env, not in console, not in unit tests). |
||
| 10 | * |
||
| 11 | * @return void |
||
| 12 | */ |
||
| 13 | private function runningInProduction() |
||
| 14 | { |
||
| 15 | if ($this->app->environment('local')) { |
||
| 16 | return false; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 17 | } |
||
| 18 | |||
| 19 | if ($this->app->runningInConsole()) { |
||
| 20 | return false; |
||
|
0 ignored issues
–
show
|
|||
| 21 | } |
||
| 22 | |||
| 23 | if ($this->app->runningUnitTests()) { |
||
| 24 | return false; |
||
|
0 ignored issues
–
show
|
|||
| 25 | } |
||
| 26 | |||
| 27 | return true; |
||
|
0 ignored issues
–
show
|
|||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Send usage statistics to the BackpackForLaravel.com website. |
||
| 32 | * Used to track unlicensed usage and general usage statistics. |
||
| 33 | * |
||
| 34 | * No GDPR implications, since no client info is send, only server info. |
||
| 35 | * |
||
| 36 | * @return void |
||
| 37 | */ |
||
| 38 | private function sendUsageStats() |
||
| 39 | { |
||
| 40 | // only send usage stats in production |
||
| 41 | if (! $this->runningInProduction()) { |
||
| 42 | return; |
||
| 43 | } |
||
| 44 | |||
| 45 | // only send stats every ~100 pageloads |
||
| 46 | if (rand(1, 100) != 1) { |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | $url = 'https://backpackforlaravel.com/api/stats'; |
||
| 51 | $method = 'PUT'; |
||
| 52 | $stats = [ |
||
| 53 | 'URL' => url('') ?? false, |
||
| 54 | 'HTTP_HOST' => $_SERVER['HTTP_HOST'] ?? false, |
||
| 55 | 'APP_URL' => $_SERVER['APP_URL'] ?? false, |
||
| 56 | 'APP_ENV' => $this->app->environment() ?? false, |
||
| 57 | 'APP_DEBUG' => $_SERVER['APP_DEBUG'] ?? false, |
||
| 58 | 'SERVER_ADDR' => $_SERVER['SERVER_ADDR'] ?? false, |
||
| 59 | 'SERVER_ADMIN' => $_SERVER['SERVER_ADMIN'] ?? false, |
||
| 60 | 'SERVER_NAME' => $_SERVER['SERVER_NAME'] ?? false, |
||
| 61 | 'SERVER_PORT' => $_SERVER['SERVER_PORT'] ?? false, |
||
| 62 | 'SERVER_PROTOCOL' => $_SERVER['SERVER_PROTOCOL'] ?? false, |
||
| 63 | 'SERVER_SOFTWARE' => $_SERVER['SERVER_SOFTWARE'] ?? false, |
||
| 64 | 'DB_CONNECTION' => $_SERVER['DB_CONNECTION'] ?? false, |
||
| 65 | 'LARAVEL_VERSION' => $this->app->version() ?? false, |
||
| 66 | 'BACKPACK_CRUD_VERSION' => \Composer\InstalledVersions::getVersion('backpack/crud') ?? false, |
||
| 67 | 'BACKPACK_PRO_VERSION' => backpack_pro(), |
||
| 68 | 'BACKPACK_LICENSE' => config('backpack.base.license_code') ?? false, |
||
| 69 | 'BACKPACK_TOKEN_USERNAME' => config('backpack.base.token_username') ?? false, |
||
| 70 | 'BACKPACK_URL' => backpack_url('') ?? false, |
||
| 71 | ]; |
||
| 72 | |||
| 73 | // send this info to the main website to store it in the db |
||
| 74 | if (function_exists('exec') && extension_loaded('curl')) { |
||
| 75 | $this->makeCurlRequest($method, $url, $stats); |
||
| 76 | } else { |
||
| 77 | $this->makeGuzzleRequest($method, $url, $stats); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Make a request using CURL. |
||
| 83 | * |
||
| 84 | * It spins up a separate process for this, and doesn't listen for a response, |
||
| 85 | * so it has minimal to no impact on pageload. |
||
| 86 | * |
||
| 87 | * @param string $method HTTP Method to use for the request. |
||
| 88 | * @param string $url URL to point the request at. |
||
| 89 | * @param array $payload The data you want sent to the URL. |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | private function makeCurlRequest($method, $url, $payload) |
||
| 93 | { |
||
| 94 | $cmd = 'curl -X '.$method." -H 'Content-Type: application/json'"; |
||
| 95 | $cmd .= " -d '".json_encode($payload)."' "."'".$url."'"; |
||
| 96 | $cmd .= ' > /dev/null 2>&1 &'; |
||
| 97 | |||
| 98 | exec($cmd, $output, $exit); |
||
| 99 | |||
| 100 | return $exit == 0; |
||
|
0 ignored issues
–
show
|
|||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Make a request using Guzzle. |
||
| 105 | * |
||
| 106 | * This request happens in the same process as the page load, |
||
| 107 | * and Guzzle listens for a response, so depending on the API latency and |
||
| 108 | * geographic location this is usually slower than CURL. However, |
||
| 109 | * unlike CURL, it works on most machines, so it's reliable. |
||
| 110 | * |
||
| 111 | * @param string $method HTTP Method to use for the request. |
||
| 112 | * @param string $url URL to point the request at. |
||
| 113 | * @param array $payload The data you want sent to the URL. |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | private function makeGuzzleRequest($method, $url, $payload) |
||
| 117 | { |
||
| 118 | try { |
||
| 119 | $client = new \GuzzleHttp\Client(); |
||
| 120 | $res = $client->request($method, $url, [ |
||
| 121 | 'form_params' => $payload, |
||
| 122 | 'http_errors' => false, |
||
| 123 | 'connect_timeout' => 0.5, |
||
| 124 | 'timeout' => 0.5, |
||
| 125 | ]); |
||
| 126 | } catch (\GuzzleHttp\Exception\GuzzleException $e) { |
||
| 127 | // do nothing |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 |