Skip to content

Instantly share code, notes, and snippets.

@jarthod
Created July 7, 2022 19:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jarthod/936bcffbcdcf9e7ed135818023210dc5 to your computer and use it in GitHub Desktop.
Quickly mute or unmute multiple checks in bulk using this small PHP page made by Eric Bouquerel from Bol d'Air (@Boldairdev)
body {
font-family: sans-serif;
}
header, .params, .submit {
margin: 0 auto;
width: 100%;
text-align: center;
color :#3d5b76;
padding-bottom: 25px;
}
.submit {
padding-top: 25px;
}
.btn-primary {
background-color: #3d5b76;
color: #fff;
padding: 8px;
border: 1px solid white;
border-radius: 4px;
font-weight: 600;
}
.btn-primary:hover {
background-color:#fff ;
color: #3d5b76;
padding: 8px;
border: 1px solid #3d5b76;
border-radius: 4px;
font-weight: 600;
}
table {
border-collapse: collapse;
border: 0;
margin: 0 auto;
}
th, td {
border-collapse: collapse;
padding: 15px;
text-align: left;
font-weight: normal;
margin: 0;
}
th,td {
border-collapse: collapse;
border-top: 1px solid #888;
border-bottom: 1px solid #888;
}
th.left, td.left {
border-collapse: collapse;
border: 0;
border-bottom: 1px solid #888;
border-left: 1px solid #888;
}
th.right, td.right{
border-collapse: collapse;
border: 0;
border-bottom: 1px solid #888;
border-right: 1px solid #888;
}
th.left, th.right {
border-top: 1px solid #888;
}
th {
background-color: #3d5b76;
color: #fff;
}
tr:nth-child(even) {
background-color: Lightblue;
}
<?php
// call the request Class that will actually communicate with the API
require "RestRequest.inc.php";
/* Get the APIKey set intentionally above Document Root, so it's never directly accessible;
(the file just contains '<?php $api_key="xxxxxxxxxxx"; ?>'
*/
require "../updownapi.php";
// prepare the needed parameters to communicate with the api
$base_url = "https://updown.io/api/";
$request = new RestRequest();
$request->setApiKey($api_key);
$request->addCurlHeader('X-API-KEY', $api_key);
$request->addCurlHeader('Accept', $request->getAcceptType());
// grab the list of existing checks
// $request = new RestRequest($base_url.'checks?api-key='.$api_key, 'GET');
//
function getAllChecks () {
global $request;
global $base_url;
$request->setUrl($base_url.'checks');
$request->setVerb('GET');
$request->execute();
$allchecks = json_decode( $request->getResponseBody() );
return $allchecks;
}
if(isset($_POST['posted'])) {
foreach ($_POST as $key => $value) {
$$key = $value;
}
// checking the mute duration parameter
//
if (!isset($mutefor)){
$arg = array('mute_until'=>'');
} else {
if ($mutefor =='') {
$arg = array('mute_until'=>'');
} else {
$t = time();
$t2 = $t +$mutefor;
$now = date('c',$t);
$until = date('c',$t2);
$arg = array('mute_until'=>$until);
}
}
$returned = array() ;
if(isset ($tokens)) {
for ($i=0; $i < count($tokens); ++$i) {
$request->setRequestBody($arg);
$request->setVerb('PUT');
$request->setUrl($base_url.'checks/'.$tokens[$i]);
$request->execute();
$returned[$i] = json_decode($request->getResponseBody());
}
//reget the checks acfter muting
$allchecks=array();
$request->flush();
$allchecks = getAllChecks();
$formstr= '<table><thead><th class="left"><input type="checkbox" id="checkall" value=""/></th><th>check</th><th class="right">Mute Until</th></thead><tbody>';
for ($i=0; $i < count($allchecks) ; $i++) {
$formstr.='<tr><td class="left"><input type="checkbox" class="tk" name="tokens[]" id="token'.$i.'" value="'. $allchecks[$i]->token .'"></td>
<td>'. $allchecks[$i]->alias .' ('. $allchecks[$i]->url .')</td><td class="right">'. $allchecks[$i]->mute_until .'</td></tr>';
}
$formstr.='</tbody></table>';
}
} else {
$allchecks=array();
$request->flush();
$allchecks = getAllChecks($base_url);
$formstr= '<table><thead><th class="left"><input type="checkbox" id="checkall" value=""/></th><th>check</th><th class="right">Mute Until</th></thead><tbody>';
for ($i=0; $i < count($allchecks) ; $i++) {
$formstr.='<tr><td class="left"><input type="checkbox" class="tk" name="tokens[]" id="token'.$i.'" value="'. $allchecks[$i]->token .'"></td><td>'. $allchecks[$i]->alias .' ('. $allchecks[$i]->url .')</td><td class="right">'. $allchecks[$i]->mute_until .'</td></tr>';
}
$formstr.='</tbody></table>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mute/Unmute Updown.io checks</title>
<link rel="stylesheet" type="text/css" href="reqchecks.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<header>
<h1>Mute / Unmute Updown.io Alerts</h1>
<p> Use this form to mute alerts from updown.io</p>
</header>
<form action="<?php echo $_SERVER['PHP_SELF'] ;?>" method="post">
<div id="params" class="params">
<select name="mutefor">
<option value="" selected>Unmute</option>
<option value="600">10 minute</option>
<option value="1200">20 minute</option>
<option value="1800">30 minute</option>
<option value="3600">1 hour</option>
</select>
</div>
<div id="checkslist">
<?php echo $formstr;?>
</div>
<div id="submit" class="submit">
<input type="hidden" name="posted" value="posted">
<input type="submit" name="submit" class="btn-primary">
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#checkall').click(
function() {
if( $('#checkall').prop('checked') ) {
$('#checkslist .tk').prop('checked', true);
} else {
$('#checkslist .tk').prop('checked', false);
}
});
});
</script>
</body>
</html>
<?php
class RestRequest
{
protected $url;
protected $verb;
protected $requestBody;
protected $requestLength;
protected $apiKey;
protected $username;
protected $password;
protected $acceptType;
protected $responseBody;
protected $responseInfo;
protected $curlHttpHeaders;
public function __construct ($url = null, $verb = 'GET', $requestBody = null)
{
$this->url = $url;
$this->verb = $verb;
$this->requestBody = $requestBody;
$this->requestLength = 0;
$this->username = null;
$this->password = null;
$this->acceptType = 'application/json';
$this->responseBody = null;
$this->responseInfo = null;
$this->apiKey = null;
$this->curlHttpHeaders = null;
if ($this->requestBody !== null)
{
$this->buildPostBody();
}
}
public function flush ()
{
$this->requestBody = null;
$this->requestLength = 0;
$this->verb = 'GET';
$this->responseBody = null;
$this->responseInfo = null;
}
public function execute ()
{
$ch = curl_init();
$this->setAuth($ch);
try
{
switch (strtoupper($this->verb))
{
case 'GET':
$this->executeGet($ch);
break;
case 'POST':
$this->executePost($ch);
break;
case 'PUT':
$this->executePut($ch);
break;
case 'DELETE':
$this->executeDelete($ch);
break;
default:
throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');
}
}
catch (InvalidArgumentException $e)
{
curl_close($ch);
throw $e;
}
catch (Exception $e)
{
curl_close($ch);
throw $e;
}
}
public function buildPostBody ($data = null)
{
$data = ($data !== null) ? $data : $this->requestBody;
if (!is_array($data))
{
throw new InvalidArgumentException('Invalid data input for postBody. Array expected');
}
$data = http_build_query($data, '', '&');
$this->requestBody = $data;
}
protected function executeGet ($ch)
{
$this->doExecute($ch);
}
protected function executePost ($ch)
{
if (!is_string($this->requestBody))
{
$this->buildPostBody();
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
curl_setopt($ch, CURLOPT_POST, 1);
$this->doExecute($ch);
}
protected function executePut ($ch)
{
if (!is_string($this->requestBody))
{
$this->buildPostBody();
}
$this->requestLength = strlen($this->requestBody);
// $fh = fopen('php://memory', 'rw');
// fwrite($fh, $this->requestBody);
// rewind($fh);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
// curl_setopt($ch, CURLOPT_INFILE, $fh);
// curl_setopt($ch, CURLOPT_INFILESIZE, $this->requestLength);
// curl_setopt($ch, CURLOPT_PUT, true);
$this->doExecute($ch);
//fclose($fh);
}
protected function executeDelete ($ch)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$this->doExecute($ch);
}
protected function doExecute (&$curlHandle)
{
$this->setCurlOpts($curlHandle);
$this->responseBody = curl_exec($curlHandle);
$this->responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
}
protected function setCurlOpts (&$curlHandle)
{
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
curl_setopt($curlHandle, CURLOPT_URL, $this->url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
if (!is_null($this->curlHttpHeaders)) {
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $this->curlHttpHeaders);
}
}
protected function setAuth (&$curlHandle)
{
if ($this->username !== null && $this->password !== null)
{
curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curlHandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
}
}
public function addCurlHeader($label, $value) {
$this->curlHttpHeaders[] = $label.':'.$value;
}
public function getCurlHeaders() {
return $this->curlHttpHeaders;
}
public function getAcceptType ()
{
return $this->acceptType;
}
public function setAcceptType ($acceptType)
{
$this->acceptType = $acceptType;
}
public function getRequestBody ()
{
return $this->requestBody;
}
public function setRequestBody ($requestBody)
{
$this->requestBody = $requestBody;
}
public function getApiKey ()
{
return $this->apiKey;
}
public function setApiKey ($apiKey)
{
$this->apiKey = $apiKey;
}
public function getPassword ()
{
return $this->password;
}
public function setPassword ($password)
{
$this->password = $password;
}
public function getResponseBody ()
{
return $this->responseBody;
}
public function getResponseInfo ()
{
return $this->responseInfo;
}
public function getUrl ()
{
return $this->url;
}
public function setUrl ($url)
{
$this->url = $url;
}
public function getUsername ()
{
return $this->username;
}
public function setUsername ($username)
{
$this->username = $username;
}
public function getVerb ()
{
return $this->verb;
}
public function setVerb ($verb)
{
$this->verb = $verb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment