From 74bae56f053773021875190716f7e999d045d506 Mon Sep 17 00:00:00 2001 From: kobayashi90 Date: Thu, 6 Feb 2025 00:27:38 +0000 Subject: [PATCH] Upload files to "includes" --- includes/config.php | 29 ++++++++++++ includes/functions.php | 105 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 includes/config.php create mode 100644 includes/functions.php diff --git a/includes/config.php b/includes/config.php new file mode 100644 index 0000000..6faac5d --- /dev/null +++ b/includes/config.php @@ -0,0 +1,29 @@ + \ No newline at end of file diff --git a/includes/functions.php b/includes/functions.php new file mode 100644 index 0000000..804890f --- /dev/null +++ b/includes/functions.php @@ -0,0 +1,105 @@ += 1) { + $_SESSION['request_count_second'] = 0; + } + if ($current_time - $_SESSION['last_request_time'] >= 60) { + $_SESSION['request_count_minute'] = 0; + } + + // Increment counters + $_SESSION['request_count_second']++; + $_SESSION['request_count_minute']++; + $_SESSION['last_request_time'] = $current_time; + + // Check if limits are exceeded + if ($_SESSION['request_count_second'] > REQUESTS_PER_SECOND_LIMIT) { + return ['status' => 'error', 'error' => 'Rate limit exceeded: Too many requests per second.']; + } + if ($_SESSION['request_count_minute'] > REQUESTS_PER_MINUTE_LIMIT) { + return ['status' => 'error', 'error' => 'Rate limit exceeded: Too many requests per minute.']; + } + + return ['status' => 'success']; +} + +// Function to send a request to AllDebrid API +function alldebrid_api_request($endpoint, $params = []) { + $params['agent'] = 'SquidDebridEU'; + $params['apikey'] = ALLDEBRID_API_KEY; + $url = ALLDEBRID_API_URL . $endpoint . '?' . http_build_query($params); + + // Use cURL for better error handling + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + + if (curl_errno($ch)) { + return ['status' => 'error', 'error' => 'Failed to connect to AllDebrid API: ' . curl_error($ch)]; + } + curl_close($ch); + + // Log the API response for debugging + error_log("API Response for endpoint '$endpoint': " . print_r($response, true)); + + $data = json_decode($response, true); + if (json_last_error() !== JSON_ERROR_NONE) { + return ['status' => 'error', 'error' => 'Invalid API response: ' . json_last_error_msg()]; + } + + return $data; +} + +// Function to copy an torrent/magnet link +function upload_torrent_magnet($link) { + // Use the correct parameter name for magnets (e.g., "magnets[]") + $response = alldebrid_api_request('magnet/upload', ['magnets[]' => $link]); + if ($response['status'] === 'success') { + return $response['data']; + } + return ['status' => 'error', 'error' => $response['error']['message'] ?? 'Failed to upload torrent/magnet.']; +} + +// Function to upload a .torrent file +function upload_torrent_file($file) { + $response = alldebrid_api_request('magnet/upload/file', [], $file); + if ($response['status'] === 'success') { + return $response['data']; + } + return ['status' => 'error', 'error' => $response['error']['message'] ?? 'Failed to upload .torrent file.']; +} + +// Function to get torrent/magnet status and file list +function get_torrent_status($id) { + $response = alldebrid_api_request('magnet/status', ['id' => $id]); + if ($response['status'] === 'success') { + return $response['data']; + } + return ['status' => 'error', 'error' => $response['error']['message'] ?? 'Failed to get torrent status.']; +} + +// Function to get supported file hosters +function get_supported_filehosts() { + $response = alldebrid_api_request("hosts"); + $hosts = $response['data']['hosts']; + + // filter out alldebrid and example from the hosts + unset($hosts['alldebrid'], $hosts['example']); + + if ($response['status'] === 'success' && isset($response['data']['hosts'])) { + return $hosts; + } else { + return []; + } +} +?> \ No newline at end of file