corrade-http-templates

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 1  →  ?path2? @ 2
/massInvites/sendMassInvites.php
@@ -0,0 +1,194 @@
<?php
 
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
## This is a script that sends a group message using Corrade. ##
###########################################################################
 
###########################################################################
## CONFIGURATION ##
###########################################################################
 
# Set this to the name of the group.
$GROUP = 'My Group';
# Set this to the group password.
$PASSWORD = 'mypassword';
# Set this to Corrade's HTTP Server URL.
$URL = 'http://corrade.internal.sever:8080';
 
###########################################################################
## INTERNALS ##
###########################################################################
 
# If no avatars have been sent, then bail.
if(!isset($_POST['avatars'])) return;
 
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function wasKeyValueGet($key, $data) {
return array_reduce(
explode(
"&",
$data
),
function($o, $p) {
$x = explode("=", $p);
return array_shift($x) != $o ? $o : array_shift($x);
},
$key
);
}
 
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function wasArrayToCSV($a) {
return implode(
',',
array_map(
function($o) {
$o = str_replace('"', '""', $o);
switch(
(strpos($o, ' ') !== FALSE) ||
(strpos($o, '"') !== FALSE) ||
(strpos($o, ',') !== FALSE) ||
(strpos($o, '\r') !== FALSE) ||
(strpos($o, '\n') !== FALSE)
)
{
case TRUE:
return '"' . $o . '"';
default:
return $o;
}
},
$a
)
);
}
 
###########################################################################
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
###########################################################################
function wasCSVToArray($csv) {
$l = array();
$s = array();
$m = "";
for ($i = 0; $i < strlen($csv); ++$i) {
switch ($csv{$i}) {
case ',':
if (sizeof($s) == 0 || !current($s) == '"') {
array_push($l, $m);
$m = "";
break;
}
$m .= $csv{$i};
continue;
case '"':
if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) {
$m .= $csv{$i};
++$i;
break;
}
if (sizeof($s) == 0|| !current($s) == $csv[$i]) {
array_push($s, $csv{$i});
continue;
}
array_pop($s);
break;
default:
$m .= $csv{$i};
break;
}
}
array_push($l, $m);
return $l;
}
 
####
# I. Build the list of invites to send to Corrade.
$invites = array();
foreach(
array_filter(explode("\n", $_POST['avatars']), 'trim') as $avatar) {
$nameUUID = preg_split("/[\s]+/", $avatar);
switch(count($nameUUID)) {
case 2:
case 1:
array_push($invites, $avatar);
break;
}
}
 
# Do not bother with an empty list of invites
if(count($invites) == 0) {
echo implode(PHP_EOL, $invites);
return -1;
}
 
####
# II. Build the command by name.
$params = array(
'command' => 'batchinvite',
'group' => $GROUP,
'password' => $PASSWORD,
'avatars' => wasArrayToCSV($invites)
);
 
####
# III. Escape the data to be sent to Corrade.
array_walk($params,
function(&$value, $key) {
$value = rawurlencode($key)."=".rawurlencode($value);
}
);
$postvars = implode('&', $params);
 
####
# IV. Use curl to send the message.
if (!($curl = curl_init())) {
print 0;
return;
}
curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
$result = curl_exec($curl);
curl_close($curl);
 
####
# V. Grab the status of the command.
$status = urldecode(
wasKeyValueGet(
"success",
$result
)
);
 
 
####
# VI. Check the status of the command.
switch($status) {
case "False":
return -1;
}
 
####
# V. Return any avatars or UUIDs for which invites could not be sent.
echo implode(
PHP_EOL,
wasCSVToArray(
urldecode(
wasKeyValueGet(
"data",
$result
)
)
)
);
 
return 0;
 
?>