-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
186 lines (146 loc) · 6.05 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/* *********************************************************************
* Configure here first
*********************************************************************/
//Configure this first! Update the $usage_url to your own:
//URL should look something like: www.start.ca/support/usage/api?key=123456ABCDEFG12345ABCDEF
//For more information, visit http://www.start.ca/support/usage/api
$usage_url = "";
//Set your timezone
date_default_timezone_set("America/New_York");
//Set your monthly limit in GB. The default below is 200GB.
$limit = 200;
//Show daily usage - Adds a section below to view your daily usage (only works on your current Start Communications network)
$show_daily_usage = false;
/* **********************************************************************
* End of configuration
**********************************************************************/
if ($usage_url == "") {
die("Please enter your Start Communications URL which can be found <a href='http://www.start.ca/support/usage/api'>here</a>");
}
$monthly_limit = "";
$bandwidth_used = "";
$bandwidth_remaining = "";
$bandwidth_remaining_percentage = 0.0;
function get_string_between($string, $start, $end) {
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}
function get_usage($url) {
global $limit;
global $monthly_limit;
global $bandwidth_used;
global $bandwidth_remaining;
global $bandwidth_remaining_percentage;
$usage_stats = @file_get_contents($url);
if ($usage_stats === "false") {
get_usage($url);
} else {
$limit = $limit * 1000000000.0;
$total = get_string_between($usage_stats, "<total>", "</total>");
$total = floatval(get_string_between($total, "<download>", "</download>"));
$used = get_string_between($usage_stats, "<used>", "</used>");
$used = floatval(get_string_between($used, "<download>", "</download>"));
$free = get_string_between($usage_stats, "<grace>", "</grace>");
$free = floatval(get_string_between($free, "<download>", "</download>"));
$monthly_limit = number_format($limit / 1000000000, 2); //. " GB";
$bandwidth_used = number_format($used / 1000000000, 2); //. " GB";
$bandwidth_remaining = number_format(($limit - $used) / 1000000000, 2); //. " GB";
$bandwidth_remaining_percentage = number_format($used * 100.0/ $limit, 1);
}
}
function get_estimated_usage() {
//Get how much you have used so far
global $bandwidth_used;
//Get number of days passed in month
$current_day = date('j');
//Divide how much you have used so far by the number of days passed in the month
$average_bandwidth_per_day = $bandwidth_used / $current_day * 1.0;
//With the result above, multiply it by the number of days in the given month
$days_in_current_month = date('t');
$estimated_usage = number_format($average_bandwidth_per_day * $days_in_current_month, 2);
return $estimated_usage . " GB";
}
function days_remaining() {
$days_in_current_month = date('t');
$current_day = date('j');
$days_until_renewal = $days_in_current_month - $current_day + 1;
return $days_until_renewal;
}
function get_balanced_usage() {
global $monthly_limit;
global $bandwidth_used;
$days_in_current_month = date('t');
$current_day = date('j');
$daily_bandwidth = $monthly_limit * 1.0 / $days_in_current_month;
$max_bandwidth_today = $daily_bandwidth * $current_day;
if ($bandwidth_used > $monthly_limit) {
return "<span class='overage'>Over Usage</span>";
} else if ($bandwidth_used < $max_bandwidth_today - 5.0) {
return "<span class='good-standing'>Excellent</span>";
} else if ($bandwidth_used >= $max_bandwidth_today - 5.0 && $bandwidth_used <= $max_bandwidth_today + 5.0) {
return "<span class='moderate-standing'>Moderate</span>";
} else {
return "<span class='bad-standing'>Poor</span>";
}
}
function return_daily_url() {
$current_month = date("n");
$current_year = date("Y");
return "https://www.start.ca/support/usage/?year=" . $current_year . "&month=" . $current_month;
}
function get_daily_usage() {
$usage_stats = @file_get_contents(return_daily_url());
if ($usage_stats === false) {
get_daily_usage();
} else {
return get_string_between($usage_stats, "<table>", "</table>");
}
}
get_usage($usage_url);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Start Communications Usage</title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" type="text/css" href="style.css">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,300,700,900' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="container">
<div class="start-title">Start Communications Usage</div>
<div class="usage-stats">
<strong>Monthly Limit:</strong> <?php echo $monthly_limit . " GB"; ?><br /><br />
<strong>Bandwidth Remaining:</strong> <?php echo $bandwidth_remaining . " GB"; ?><br /><br />
<strong>Days Until Renewal:</strong> <?php echo days_remaining(); ?><br /><br />
<strong>Estimated Usage:</strong> <?php echo get_estimated_usage(); ?><br /><br />
<strong>Usage Status:</strong> <?php echo get_balanced_usage(); ?>
</div>
<hr>
<strong>Bandwidth Used:</strong> <?php echo $bandwidth_used . " GB (" . $bandwidth_remaining_percentage . "% used)"; ?><br />
<div class="bar">
<div class="percentage" style="width: <?php echo $bandwidth_remaining_percentage; ?>%;"> </div>
</div>
</div>
<?php if ($show_daily_usage) { ?>
<div>
<table class="daily-usage-container">
<tbody>
<?php echo get_daily_usage(); ?>
<tr height="60px">
<td valign="bottom" colspan="2"><a href="<?php echo return_daily_url(); ?>" target="_blank">Check daily usage</a></td>
<td colspan="2"> </td>
</tr>
</tbody>
</table>
</div>
<?php } ?>
</body>
</html>