Skip to content

Commit

Permalink
Merge pull request #152 from untrobotics/URW-170-AdminBot-Update-Send…
Browse files Browse the repository at this point in the history
…Messages

URW-170 Update Discord bot Send_Messages functionality
  • Loading branch information
sebastian-king authored Mar 19, 2024
2 parents 9d7e850 + d1bf946 commit 21f2e27
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 25 deletions.
66 changes: 44 additions & 22 deletions api/discord/bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,52 @@ public static function send_message($message, $channel_id, $attachments = null)
$data = $message;
}

// Add files to array
$files = array();
if ($attachments) {
foreach ($attachments as $k => $attachment) {
$file = tmpfile();
$path = stream_get_meta_data($file)['uri'];

//$content = file_get_contents($attachment['url']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $attachment['url']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);

//error_log($content);

file_put_contents($path, $content);
//error_log($attachment['url']);
//error_log($path);
//error_log($content);

$files["attachment{$k}"] = new CURLFile($path, $attachment['type'], "attachment{$k}." . mime2ext($attachment['type']));
}
}
foreach ($attachments as $k => $attachment) {
if(isset($attachment['bin'])) // Function caller passed the raw data to the arg
{
// Since we have the data, we need to create a tmpfile to store that data for the CURLFile
$file = tmpfile();
$path = stream_get_meta_data($file)['uri'];
file_put_contents($path,$attachment['bin']);
$file_type = $attachment['type'];
$file_mime = ext2mime($file_type);
}
else if(isset($attachment['path'])) // File exists on local machine
{
$path = $attachment['path'];
$file_type = $attachment['type'];
$file_mime = ext2mime($file_type);
}
else // We assume the attachment is an online file that we need to download
{ // sebastian only insane people put curly braces on the same line
//$content = file_get_contents($attachment['url']);
$file = tmpfile();
$path = stream_get_meta_data($file)['uri'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $attachment['url']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);

//error_log($content);

file_put_contents($path, $content);
$file_mime = $attachment['type'];
$file_type = mime2ext($file_mime);
//error_log($attachment['url']);
//error_log($path);
//error_log($content);

/*$files["attachment{$k}"] = new CURLFile($path, $attachment['type'], "attachment{$k}." . mime2ext($attachment['type']));*/
}

// CURLFile is a file with a bunch of delimiters in the binary so it can be sent as Form-data
$files["attachment{$k}"] = new CURLFile($path, $file_mime, "attachments{$k}." . $file_type);
}
}
//error_log(var_export($files, true));

return static::send_api_request("/channels/{$channel_id}/messages", 'POST', 'multipart/form-data', $data, $files);
Expand Down
122 changes: 119 additions & 3 deletions template/functions/mime2ext.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,128 @@
<?php
const MIMES = array(
"png"=>array("image/png","image/x-png"),
"bmp"=>array("image/bmp","image/x-bmp","image/x-bitmap","image/x-xbitmap","image/x-win-bitmap","image/x-windows-bmp","image/ms-bmp","image/x-ms-bmp","application/bmp","application/x-bmp","application/x-win-bitmap"),
"gif"=>array("image/gif"),
"jpeg"=>array("image/jpeg","image/pjpeg"),
"jpg"=>array("image/jpg","image/pjpg"),
"xspf"=>array("application/xspf+xml"),
"vlc"=>array("application/videolan"),
"wmv"=>array("video/x-ms-wmv","video/x-ms-asf"),
"au"=>array("audio/x-au"),
"ac3"=>array("audio/ac3"),
"flac"=>array("audio/x-flac"),
"ogg"=>array("audio/ogg","video/ogg","application/ogg"),
"kmz"=>array("application/vnd.google-earth.kmz"),
"kml"=>array("application/vnd.google-earth.kml+xml"),
"rtx"=>array("text/richtext"),
"rtf"=>array("text/rtf"),
"jar"=>array("application/java-archive","application/x-java-application","application/x-jar"),
"zip"=>array("application/x-zip","application/zip","application/x-zip-compressed","application/s-compressed","multipart/x-zip"),
"7zip"=>array("application/x-compressed"),
"xml"=>array("application/xml","text/xml"),
"svg"=>array("image/svg+xml"),
"3g2"=>array("video/3gpp2"),
"3gp"=>array("video/3gp","video/3gpp"),
"mp4"=>array("video/mp4"),
"m4a"=>array("audio/x-m4a"),
"f4v"=>array("video/x-f4v"),
"flv"=>array("video/x-flv"),
"webm"=>array("video/webm"),
"aac"=>array("audio/x-acc"),
"m4u"=>array("application/vnd.mpegurl"),
"pdf"=>array("application/pdf","application/octet-stream"),
"pptx"=>array("application/vnd.openxmlformats-officedocument.presentationml.presentation"),
"ppt"=>array("application/powerpoint","application/vnd.ms-powerpoint","application/vnd.ms-office","application/msword"),
"docx"=>array("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
"xlsx"=>array("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application/vnd.ms-excel"),
"xl"=>array("application/excel"),
"xls"=>array("application/msexcel","application/x-msexcel","application/x-ms-excel","application/x-excel","application/x-dos_ms_excel","application/xls","application/x-xls"),
"xsl"=>array("text/xsl"),
"mpeg"=>array("video/mpeg"),
"mov"=>array("video/quicktime"),
"avi"=>array("video/x-msvideo","video/msvideo","video/avi","application/x-troff-msvideo"),
"movie"=>array("video/x-sgi-movie"),
"log"=>array("text/x-log"),
"txt"=>array("text/plain"),
"css"=>array("text/css"),
"html"=>array("text/html"),
"wav"=>array("audio/x-wav","audio/wave","audio/wav"),
"xhtml"=>array("application/xhtml+xml"),
"tar"=>array("application/x-tar"),
"tgz"=>array("application/x-gzip-compressed"),
"psd"=>array("application/x-photoshop","image/vnd.adobe.photoshop"),
"exe"=>array("application/x-msdownload"),
"js"=>array("application/x-javascript"),
"mp3"=>array("audio/mpeg","audio/mpg","audio/mpeg3","audio/mp3"),
"rar"=>array("application/x-rar","application/rar","application/x-rar-compressed"),
"gzip"=>array("application/x-gzip"),
"hqx"=>array("application/mac-binhex40","application/mac-binhex","application/x-binhex40","application/x-mac-binhex40"),
"cpt"=>array("application/mac-compactpro"),
"bin"=>array("application/macbinary","application/mac-binary","application/x-binary","application/x-macbinary"),
"oda"=>array("application/oda"),
"ai"=>array("application/postscript"),
"smil"=>array("application/smil"),
"mif"=>array("application/vnd.mif"),
"wbxml"=>array("application/wbxml"),
"wmlc"=>array("application/wmlc"),
"dcr"=>array("application/x-director"),
"dvi"=>array("application/x-dvi"),
"gtar"=>array("application/x-gtar"),
"php"=>array("application/x-httpd-php","application/php","application/x-php","text/php","text/x-php","application/x-httpd-php-source"),
"swf"=>array("application/x-shockwave-flash"),
"sit"=>array("application/x-stuffit"),
"z"=>array("application/x-compress"),
"mid"=>array("audio/midi"),
"aif"=>array("audio/x-aiff","audio/aiff"),
"ram"=>array("audio/x-pn-realaudio"),
"rpm"=>array("audio/x-pn-realaudio-plugin"),
"ra"=>array("audio/x-realaudio"),
"rv"=>array("video/vnd.rn-realvideo"),
"jp2"=>array("image/jp2","video/mj2","image/jpx","image/jpm"),
"tiff"=>array("image/tiff"),
"eml"=>array("message/rfc822"),
"pem"=>array("application/x-x509-user-cert","application/x-pem-file"),
"p10"=>array("application/x-pkcs10","application/pkcs10"),
"p12"=>array("application/x-pkcs12"),
"p7a"=>array("application/x-pkcs7-signature"),
"p7c"=>array("application/pkcs7-mime","application/x-pkcs7-mime"),
"p7r"=>array("application/x-pkcs7-certreqresp"),
"p7s"=>array("application/pkcs7-signature"),
"crt"=>array("application/x-x509-ca-cert","application/pkix-cert"),
"crl"=>array("application/pkix-crl","application/pkcs-crl"),
"pgp"=>array("application/pgp"),
"gpg"=>array("application/gpg-keys"),
"rsa"=>array("application/x-pkcs7"),
"ics"=>array("text/calendar"),
"zsh"=>array("text/x-scriptzsh"),
"cdr"=>array("application/cdr","application/coreldraw","application/x-cdr","application/x-coreldraw","image/cdr","image/x-cdr","zz-application/zz-winassoc-cdr"),
"wma"=>array("audio/x-ms-wma"),
"vcf"=>array("text/x-vcard"),
"srt"=>array("text/srt"),
"vtt"=>array("text/vtt"),
"ico"=>array("image/x-icon","image/x-ico","image/vnd.microsoft.icon"),
"csv"=>array("text/x-comma-separated-values","text/comma-separated-values","application/vnd.msexcel"),
"json"=>array("application/json","text/json")
);
function mime2ext($mime){
$all_mimes = '{"png":["image\/png","image\/x-png"],"bmp":["image\/bmp","image\/x-bmp","image\/x-bitmap","image\/x-xbitmap","image\/x-win-bitmap","image\/x-windows-bmp","image\/ms-bmp","image\/x-ms-bmp","application\/bmp","application\/x-bmp","application\/x-win-bitmap"],"gif":["image\/gif"],"jpeg":["image\/jpeg","image\/pjpeg"],"xspf":["application\/xspf+xml"],"vlc":["application\/videolan"],"wmv":["video\/x-ms-wmv","video\/x-ms-asf"],"au":["audio\/x-au"],"ac3":["audio\/ac3"],"flac":["audio\/x-flac"],"ogg":["audio\/ogg","video\/ogg","application\/ogg"],"kmz":["application\/vnd.google-earth.kmz"],"kml":["application\/vnd.google-earth.kml+xml"],"rtx":["text\/richtext"],"rtf":["text\/rtf"],"jar":["application\/java-archive","application\/x-java-application","application\/x-jar"],"zip":["application\/x-zip","application\/zip","application\/x-zip-compressed","application\/s-compressed","multipart\/x-zip"],"7zip":["application\/x-compressed"],"xml":["application\/xml","text\/xml"],"svg":["image\/svg+xml"],"3g2":["video\/3gpp2"],"3gp":["video\/3gp","video\/3gpp"],"mp4":["video\/mp4"],"m4a":["audio\/x-m4a"],"f4v":["video\/x-f4v"],"flv":["video\/x-flv"],"webm":["video\/webm"],"aac":["audio\/x-acc"],"m4u":["application\/vnd.mpegurl"],"pdf":["application\/pdf","application\/octet-stream"],"pptx":["application\/vnd.openxmlformats-officedocument.presentationml.presentation"],"ppt":["application\/powerpoint","application\/vnd.ms-powerpoint","application\/vnd.ms-office","application\/msword"],"docx":["application\/vnd.openxmlformats-officedocument.wordprocessingml.document"],"xlsx":["application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application\/vnd.ms-excel"],"xl":["application\/excel"],"xls":["application\/msexcel","application\/x-msexcel","application\/x-ms-excel","application\/x-excel","application\/x-dos_ms_excel","application\/xls","application\/x-xls"],"xsl":["text\/xsl"],"mpeg":["video\/mpeg"],"mov":["video\/quicktime"],"avi":["video\/x-msvideo","video\/msvideo","video\/avi","application\/x-troff-msvideo"],"movie":["video\/x-sgi-movie"],"log":["text\/x-log"],"txt":["text\/plain"],"css":["text\/css"],"html":["text\/html"],"wav":["audio\/x-wav","audio\/wave","audio\/wav"],"xhtml":["application\/xhtml+xml"],"tar":["application\/x-tar"],"tgz":["application\/x-gzip-compressed"],"psd":["application\/x-photoshop","image\/vnd.adobe.photoshop"],"exe":["application\/x-msdownload"],"js":["application\/x-javascript"],"mp3":["audio\/mpeg","audio\/mpg","audio\/mpeg3","audio\/mp3"],"rar":["application\/x-rar","application\/rar","application\/x-rar-compressed"],"gzip":["application\/x-gzip"],"hqx":["application\/mac-binhex40","application\/mac-binhex","application\/x-binhex40","application\/x-mac-binhex40"],"cpt":["application\/mac-compactpro"],"bin":["application\/macbinary","application\/mac-binary","application\/x-binary","application\/x-macbinary"],"oda":["application\/oda"],"ai":["application\/postscript"],"smil":["application\/smil"],"mif":["application\/vnd.mif"],"wbxml":["application\/wbxml"],"wmlc":["application\/wmlc"],"dcr":["application\/x-director"],"dvi":["application\/x-dvi"],"gtar":["application\/x-gtar"],"php":["application\/x-httpd-php","application\/php","application\/x-php","text\/php","text\/x-php","application\/x-httpd-php-source"],"swf":["application\/x-shockwave-flash"],"sit":["application\/x-stuffit"],"z":["application\/x-compress"],"mid":["audio\/midi"],"aif":["audio\/x-aiff","audio\/aiff"],"ram":["audio\/x-pn-realaudio"],"rpm":["audio\/x-pn-realaudio-plugin"],"ra":["audio\/x-realaudio"],"rv":["video\/vnd.rn-realvideo"],"jp2":["image\/jp2","video\/mj2","image\/jpx","image\/jpm"],"tiff":["image\/tiff"],"eml":["message\/rfc822"],"pem":["application\/x-x509-user-cert","application\/x-pem-file"],"p10":["application\/x-pkcs10","application\/pkcs10"],"p12":["application\/x-pkcs12"],"p7a":["application\/x-pkcs7-signature"],"p7c":["application\/pkcs7-mime","application\/x-pkcs7-mime"],"p7r":["application\/x-pkcs7-certreqresp"],"p7s":["application\/pkcs7-signature"],"crt":["application\/x-x509-ca-cert","application\/pkix-cert"],"crl":["application\/pkix-crl","application\/pkcs-crl"],"pgp":["application\/pgp"],"gpg":["application\/gpg-keys"],"rsa":["application\/x-pkcs7"],"ics":["text\/calendar"],"zsh":["text\/x-scriptzsh"],"cdr":["application\/cdr","application\/coreldraw","application\/x-cdr","application\/x-coreldraw","image\/cdr","image\/x-cdr","zz-application\/zz-winassoc-cdr"],"wma":["audio\/x-ms-wma"],"vcf":["text\/x-vcard"],"srt":["text\/srt"],"vtt":["text\/vtt"],"ico":["image\/x-icon","image\/x-ico","image\/vnd.microsoft.icon"],"csv":["text\/x-comma-separated-values","text\/comma-separated-values","application\/vnd.msexcel"],"json":["application\/json","text\/json"]}';
$all_mimes = json_decode($all_mimes,true);
foreach ($all_mimes as $key => $value) {
foreach (MIMES as $key => $value) {
if(array_search($mime,$value) !== false) {
return $key;
}
}
return false;
}

function ext2mime($ext)
{
global $mimes;
foreach(MIMES as $key=>$value)
{
if($key===$ext)
{
return $value[0];
}
}
return false;
}
?>

0 comments on commit 21f2e27

Please sign in to comment.