Browse Source

Add badge support

pull/4/head
Robert Miles 4 years ago
parent
commit
4f48e595b4
  1. 11
      badges/badge.php
  2. 24
      badges/index.php
  3. 14
      badges/shieldsio.php

11
badges/badge.php

@ -0,0 +1,11 @@
<?php
include 'shieldsio.php';
if (!isset($_GET["channel"])) {
$badge = get_badge();
header("Location: {$badge}");
} else {
$badge = get_badge($_GET["channel"]);
header("Location: {$badge}");
}
die();
?>

24
badges/index.php

@ -0,0 +1,24 @@
<?php
$title = "status badges";
$desc = "status badges for online user counts";
include '../header.php';
?>
<h1>tilde.chat online status badges</h1>
<p>thanks to <a href="https://shields.io/">shields.io</a> and custom badge support, we now have user count badges!</p>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Example</th><th>URL</th><th>Result</th>
</tr>
</thead>
<tr>
<td>Network user count</td><td>https://tilde.chat/badges/badge.php</td><td><img src="badge.php"></td>
</tr>
<tr>
<td>User count for #meta</td><td>https://tilde.chat/badges/badge.php?channel=%23meta</td><td><img src="badge.php?channel=%23meta"></td>
</tr>
<tr>
<td>Invalid channel name (error handling)</td><td>https://tilde.chat/badges/badge.php?channel=invalid</td><td><img src="badge.php?channel=invalid"></td>
</tr>
</table>
<?php include '../footer.php'; ?>

14
badges/shieldsio.php

@ -0,0 +1,14 @@
<?php
function get_badge($channel_name=FALSE) {
if ($channel_name==FALSE) {
return "https://img.shields.io/badge/dynamic/json.svg?url=https://tilde.chat/stats.json&label=chat&query=$.usercount&suffix=%20online";
}
$stats = json_decode(file_get_contents("https://tilde.chat/stats.json")) ?? array("channels"=>[]);
foreach ($stats->channels as $key=>$channel) {
if ($channel->name==$channel_name) {
return "https://img.shields.io/badge/dynamic/json.svg?url=https://tilde.chat/stats.json&label=chat&query=$.channels[$key].usercount&suffix=%20online";
}
}
return "https://img.shields.io/badge/badge-invalid%20channel-red.svg";
}
?>
Loading…
Cancel
Save