diff --git a/data/index.php b/data/index.php index d562a2c..4bdbe1f 100644 --- a/data/index.php +++ b/data/index.php @@ -134,19 +134,39 @@ $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); - // Check status - if ($status != 200) { - echo ''; - exit(); + if ($status == 200) { + // Parse result from Imgur + $pms = json_decode($output,true); + $page_url = 'https://imgur.com/'.$pms['data']['id']; + $image_url = $pms['data']['link']; + $delete_hash = $pms['data']['deletehash']; + // For debugging: var_dump($pms); + } else { + // Imgur API returned an error, try ImgBB + if ((getenv('IMGBB_KEY'))) { + $imgbb_post = [ + 'key' => getenv('IMGBB_KEY'), + 'image' => base64_encode($data), + 'expiration' => 120 + ]; + $ch = curl_init('https://api.imgbb.com/1/upload'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $imgbb_post); + // Upload image to ImgBB + $output = curl_exec($ch); + curl_close($ch); + // TODO: Parse ImgBB errors + // Parse result + $pms = json_decode($output,true); + $page_url = $pms['data']['url']; // url_viewer is having issues + $image_url = $pms['data']['url']; + } else { + // Show error message + echo ''; + exit(); + } } - // Parse result - $pms = json_decode($output,true); - $id = $pms['data']['id']; - $imgurl = $pms['data']['link']; - $delete_hash = $pms['data']['deletehash']; - // For debugging: var_dump($pms); - // Send to Discord if enabled if (isset($_COOKIE["discord_webhook"])) { $webhook_curl = curl_init(); @@ -161,13 +181,13 @@ "embeds": [ { "title": "'.$software.'", - "url": "https://imgur.com/'.$id.'", + "url": "'.$page_url.'", "color": null, "footer": { "text": "Uploader: '.$_SERVER["HTTP_USER_AGENT"].'" }, "image": { - "url": "'.$imgurl.'" + "url": "'.$image_url.'" } } ], @@ -184,18 +204,25 @@ } // Display result + if (str_contains($page_url, 'imgur')) { + $host_options = ' +
+ '; + } else { + $host_options = 'The Imgur API was unavailable, ImgBB was used instead. You have two minutes to save your image before it is automatically deleted.
'; + } $out = ' '; @@ -232,14 +259,6 @@