Update includes/functions.php
This commit is contained in:
parent
74bae56f05
commit
23abb63bb8
1 changed files with 174 additions and 104 deletions
|
|
@ -1,105 +1,175 @@
|
||||||
<?php
|
<?php
|
||||||
// Include config.php only if it hasn't been included already
|
// Include config.php only if it hasn't been included already
|
||||||
if (!defined('ALLDEBRID_API_KEY')) {
|
if (!defined('ALLDEBRID_API_KEY')) {
|
||||||
require __DIR__ . '/config.php';
|
require __DIR__ . '/config.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to check and enforce rate limits
|
// Function to check and enforce rate limits
|
||||||
function enforce_rate_limits() {
|
function enforce_rate_limits() {
|
||||||
$current_time = time();
|
$current_time = time();
|
||||||
|
|
||||||
// Reset counters if the time window has passed
|
// Reset counters if the time window has passed
|
||||||
if ($current_time - $_SESSION['last_request_time'] >= 1) {
|
if ($current_time - $_SESSION['last_request_time'] >= 1) {
|
||||||
$_SESSION['request_count_second'] = 0;
|
$_SESSION['request_count_second'] = 0;
|
||||||
}
|
}
|
||||||
if ($current_time - $_SESSION['last_request_time'] >= 60) {
|
if ($current_time - $_SESSION['last_request_time'] >= 60) {
|
||||||
$_SESSION['request_count_minute'] = 0;
|
$_SESSION['request_count_minute'] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment counters
|
// Increment counters
|
||||||
$_SESSION['request_count_second']++;
|
$_SESSION['request_count_second']++;
|
||||||
$_SESSION['request_count_minute']++;
|
$_SESSION['request_count_minute']++;
|
||||||
$_SESSION['last_request_time'] = $current_time;
|
$_SESSION['last_request_time'] = $current_time;
|
||||||
|
|
||||||
// Check if limits are exceeded
|
// Check if limits are exceeded
|
||||||
if ($_SESSION['request_count_second'] > REQUESTS_PER_SECOND_LIMIT) {
|
if ($_SESSION['request_count_second'] > REQUESTS_PER_SECOND_LIMIT) {
|
||||||
return ['status' => 'error', 'error' => 'Rate limit exceeded: Too many requests per second.'];
|
return ['status' => 'error', 'error' => 'Rate limit exceeded: Too many requests per second.'];
|
||||||
}
|
}
|
||||||
if ($_SESSION['request_count_minute'] > REQUESTS_PER_MINUTE_LIMIT) {
|
if ($_SESSION['request_count_minute'] > REQUESTS_PER_MINUTE_LIMIT) {
|
||||||
return ['status' => 'error', 'error' => 'Rate limit exceeded: Too many requests per minute.'];
|
return ['status' => 'error', 'error' => 'Rate limit exceeded: Too many requests per minute.'];
|
||||||
}
|
}
|
||||||
|
|
||||||
return ['status' => 'success'];
|
return ['status' => 'success'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to send a request to AllDebrid API
|
// Function to send a GET request to AllDebrid API
|
||||||
function alldebrid_api_request($endpoint, $params = []) {
|
function alldebrid_api_get_request($endpoint, $params = []) {
|
||||||
$params['agent'] = 'SquidDebridEU';
|
$params['agent'] = 'SquidDebridEU';
|
||||||
$params['apikey'] = ALLDEBRID_API_KEY;
|
$params['apikey'] = ALLDEBRID_API_KEY;
|
||||||
$url = ALLDEBRID_API_URL . $endpoint . '?' . http_build_query($params);
|
$url = ALLDEBRID_API_URL . $endpoint . '?' . http_build_query($params);
|
||||||
|
|
||||||
// Use cURL for better error handling
|
// Use cURL for better error handling
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
|
|
||||||
if (curl_errno($ch)) {
|
if (curl_errno($ch)) {
|
||||||
return ['status' => 'error', 'error' => 'Failed to connect to AllDebrid API: ' . curl_error($ch)];
|
return ['status' => 'error', 'error' => 'Failed to connect to AllDebrid API: ' . curl_error($ch)];
|
||||||
}
|
}
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
// Log the API response for debugging
|
// Log the API response for debugging
|
||||||
error_log("API Response for endpoint '$endpoint': " . print_r($response, true));
|
error_log("API Response for endpoint '$endpoint': " . print_r($response, true));
|
||||||
|
|
||||||
$data = json_decode($response, true);
|
$data = json_decode($response, true);
|
||||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
return ['status' => 'error', 'error' => 'Invalid API response: ' . json_last_error_msg()];
|
return ['status' => 'error', 'error' => 'Invalid API response: ' . json_last_error_msg()];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to copy an torrent/magnet link
|
// Function to send a POST request to AllDebrid API
|
||||||
function upload_torrent_magnet($link) {
|
function alldebrid_api_post_request($endpoint, $params = []) {
|
||||||
// Use the correct parameter name for magnets (e.g., "magnets[]")
|
$params['agent'] = 'SquidDebridEU';
|
||||||
$response = alldebrid_api_request('magnet/upload', ['magnets[]' => $link]);
|
$params['apikey'] = ALLDEBRID_API_KEY;
|
||||||
if ($response['status'] === 'success') {
|
$url = ALLDEBRID_API_URL . $endpoint;
|
||||||
return $response['data'];
|
|
||||||
}
|
$ch = curl_init();
|
||||||
return ['status' => 'error', 'error' => $response['error']['message'] ?? 'Failed to upload torrent/magnet.'];
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
}
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
// Function to upload a .torrent file
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
|
||||||
function upload_torrent_file($file) {
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||||
$response = alldebrid_api_request('magnet/upload/file', [], $file);
|
'Content-Type: application/json'
|
||||||
if ($response['status'] === 'success') {
|
]);
|
||||||
return $response['data'];
|
|
||||||
}
|
$response = curl_exec($ch);
|
||||||
return ['status' => 'error', 'error' => $response['error']['message'] ?? 'Failed to upload .torrent file.'];
|
|
||||||
}
|
if (curl_errno($ch)) {
|
||||||
|
return ['status' => 'error', 'error' => 'Failed to connect to AllDebrid API: ' . curl_error($ch)];
|
||||||
// Function to get torrent/magnet status and file list
|
}
|
||||||
function get_torrent_status($id) {
|
|
||||||
$response = alldebrid_api_request('magnet/status', ['id' => $id]);
|
curl_close($ch);
|
||||||
if ($response['status'] === 'success') {
|
|
||||||
return $response['data'];
|
$data = json_decode($response, true);
|
||||||
}
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
return ['status' => 'error', 'error' => $response['error']['message'] ?? 'Failed to get torrent status.'];
|
return ['status' => 'error', 'error' => 'Invalid API response: ' . json_last_error_msg()];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to get supported file hosters
|
return $data;
|
||||||
function get_supported_filehosts() {
|
}
|
||||||
$response = alldebrid_api_request("hosts");
|
|
||||||
$hosts = $response['data']['hosts'];
|
// Function to send a POST request to AllDebrid API with correct Content Type for File Uploading
|
||||||
|
function alldebrid_api_upload_file($endpoint, $filePath) {
|
||||||
// filter out alldebrid and example from the hosts
|
if (!file_exists($filePath)) {
|
||||||
unset($hosts['alldebrid'], $hosts['example']);
|
return ['status' => 'error', 'error' => 'File not found.'];
|
||||||
|
}
|
||||||
if ($response['status'] === 'success' && isset($response['data']['hosts'])) {
|
|
||||||
return $hosts;
|
$file = new CURLFile($filePath, 'application/x-bittorrent', basename($filePath));
|
||||||
} else {
|
$params = [
|
||||||
return [];
|
'agent' => 'SquidDebridEU',
|
||||||
}
|
'apikey' => ALLDEBRID_API_KEY,
|
||||||
}
|
'files[]' => $file
|
||||||
|
];
|
||||||
|
|
||||||
|
$url = ALLDEBRID_API_URL . $endpoint;
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: multipart/form-data']);
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
|
||||||
|
if (curl_errno($ch)) {
|
||||||
|
return ['status' => 'error', 'error' => 'Failed to connect to AllDebrid API: ' . curl_error($ch)];
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
$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_get_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($filePath) {
|
||||||
|
$response = alldebrid_api_upload_file('magnet/upload/file', $filePath);
|
||||||
|
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_get_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_get_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 [];
|
||||||
|
}
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue