1<?php
2
3namespace WHMCS\Module\Widget;
4
5use WHMCS\Module\AbstractWidget;
6
7/**
8 * Health Widget.
9 *
10 * @copyright Copyright (c) WHMCS Limited 2005-2021
11 * @license https://www.whmcs.com/eula/ WHMCS Eula
12 */
13class Health extends AbstractWidget
14{
15 protected $title = 'System Health';
16 protected $description = 'An overview of System Health.';
17 protected $weight = 500;
18 protected $cache = true;
19 protected $requiredPermission = 'Health and Updates';
20
21 public function getData()
22 {
23 return localApi('GetHealthStatus', array());
24 }
25
26 public function generateOutput($data)
27 {
28 if (
29 !is_array($data)
30 ||
31 empty($data['result'])
32 ||
33 $data['result'] === 'error'
34 ) {
35 /*
36 * A previous version may have cached an erroneous response due to PHP 7.4 not being itemized
37 * in Php environment class. We can retry obtaining that data
38 */
39 $data = $this->fetchData(true);
40 }
41
42 $countSuccess = count($data['checks']['success'] ?? []);
43 $countWarnings = count($data['checks']['warning'] ?? []);
44 $countDanger = count($data['checks']['danger'] ?? []);
45
46 $totalCount = $countSuccess + $countDanger;
47
48 $countPercent = $totalCount > 0
49 ? round (($countSuccess / $totalCount) * 100, 0)
50 : 100; // fallback to avoid dividing by 0
51
52 $ratingMsg = '<span style="color:#49a94d;">Good</span>';
53 $ratingIcon = '<i class="pe-7s-help2" style="color:#49a94d;"></i>';
54 if ($countPercent < 50) {
55 $ratingMsg = '<span class="color-pink">Poor</span>';
56 $ratingIcon = '<i class="pe-7s-close-circle color-pink"></i>';
57 }
58
59 return <<<EOF
60<div class="widget-content-padded icon-stats">
61
62 <div class="row">
63 <div class="col-sm-7">
64 <div class="item">
65 <div class="icon-holder text-center">
66 {$ratingIcon}
67 </div>
68 <div class="data">
69 <div class="note">
70 Overall Rating
71 </div>
72 <div class="number">
73 {$ratingMsg}
74 </div>
75 </div>
76 </div>
77 </div>
78 <div class="col-sm-5 text-right">
79 <a href="systemhealthandupdates.php" class="btn btn-default btn-sm">
80 <i class="fas fa-arrow-right"></i> View Issues
81 </a>
82 </div>
83 </div>
84
85 <div class="clear:both;"></div>
86
87 <div class="progress">
88 <div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="{$countPercent}" aria-valuemin="0" aria-valuemax="100" style="width: {$countPercent}%">
89 <span class="sr-only">{$countPercent}% Complete (success)</span>
90 </div>
91 </div>
92
93 <div class="row">
94 <div class="col-sm-6 text-center">
95 <i class="fas fa-exclamation-triangle"></i> {$countWarnings} Warnings
96 </div>
97 <div class="col-sm-6 text-center text-danger">
98 <i class="fas fa-times"></i> {$countDanger} Needing Attention
99 </div>
100 </div>
101
102</div>
103EOF;
104 }
105}
106