|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Cachet. |
| 5 | + * |
| 6 | + * (c) Alt Three Services Limited |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CachetHQ\Cachet\Integrations\Core; |
| 13 | + |
| 14 | +use CachetHQ\Cachet\Integrations\Contracts\Beacon as BeaconContract; |
| 15 | +use CachetHQ\Cachet\Models\Component; |
| 16 | +use CachetHQ\Cachet\Models\Incident; |
| 17 | +use CachetHQ\Cachet\Models\Metric; |
| 18 | +use CachetHQ\Cachet\Models\User; |
| 19 | +use CachetHQ\Cachet\Settings\Repository as Setting; |
| 20 | +use Exception; |
| 21 | +use GuzzleHttp\Client; |
| 22 | +use Illuminate\Contracts\Config\Repository; |
| 23 | + |
| 24 | +/** |
| 25 | + * This is the beacon class. |
| 26 | + * |
| 27 | + * @author James Brooks <james@alt-three.com> |
| 28 | + */ |
| 29 | +class Beacon implements BeaconContract |
| 30 | +{ |
| 31 | + /** |
| 32 | + * The beacon url. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + const URL = 'https://cachethq.io/beacon'; |
| 37 | + |
| 38 | + /** |
| 39 | + * The illuminate config instance. |
| 40 | + * |
| 41 | + * @var \Illuminate\Contracts\Config\Repository |
| 42 | + */ |
| 43 | + protected $config; |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a new beacon instance. |
| 47 | + * |
| 48 | + * @param \Illuminate\Contracts\Config\Repository $config |
| 49 | + * |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function __construct(Repository $config) |
| 53 | + { |
| 54 | + $this->config = $config; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Has the install enabled Cachet beacon? |
| 59 | + * |
| 60 | + * @return bool |
| 61 | + */ |
| 62 | + public function enabled() |
| 63 | + { |
| 64 | + return $this->config->get('cachet.beacon'); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Send a beacon to our server. |
| 69 | + * |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + public function send() |
| 73 | + { |
| 74 | + // We don't want any accidental sending of beacons if the installation has explicitly said no. |
| 75 | + if (!$this->enabled()) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (!($contactEmail = User::admins()->active()->first()->email)) { |
| 80 | + $contactEmail = null; |
| 81 | + } |
| 82 | + |
| 83 | + $setting = app(Setting::class); |
| 84 | + |
| 85 | + if (!$installId = $setting->get('install_id', null)) { |
| 86 | + $installId = sha1(str_random(20)); |
| 87 | + |
| 88 | + $setting->set('install_id', $installId); |
| 89 | + } |
| 90 | + |
| 91 | + $payload = [ |
| 92 | + 'install_id' => $installId, |
| 93 | + 'version' => CACHET_VERSION, |
| 94 | + 'docker' => $this->config->get('cachet.is_docker'), |
| 95 | + 'database' => $this->config->get('database.default'), |
| 96 | + 'contact_email' => $contactEmail, |
| 97 | + 'data' => [ |
| 98 | + 'components' => Component::all()->count(), |
| 99 | + 'incidents' => Incident::all()->count(), |
| 100 | + 'metrics' => Metric::all()->count(), |
| 101 | + 'users' => User::all()->count(), |
| 102 | + ], |
| 103 | + ]; |
| 104 | + |
| 105 | + try { |
| 106 | + $client = new Client(); |
| 107 | + $client->post(self::URL, [ |
| 108 | + 'headers' => ['Accept' => 'application/json', 'User-Agent' => defined('CACHET_VERSION') ? 'cachet/'.constant('CACHET_VERSION') : 'cachet'], |
| 109 | + 'json' => $payload, |
| 110 | + ]); |
| 111 | + } catch (Exception $e) { |
| 112 | + // TODO: Log a warning that the beacon could not be sent. |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments