Skip to content

Commit

Permalink
Update index.php
Browse files Browse the repository at this point in the history
  • Loading branch information
injahow committed Aug 6, 2021
1 parent 64f605c commit 80d84d1
Showing 1 changed file with 33 additions and 46 deletions.
79 changes: 33 additions & 46 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
define('AUTH', false);
define('AUTH_SECRET', 'meting-secret');

function auth($name)
{
return hash_hmac('sha1', $name, AUTH_SECRET);
}

if (!isset($_GET['type']) || !isset($_GET['id'])) {
include __DIR__ . '/public/index.html';
exit;
Expand Down Expand Up @@ -99,14 +94,20 @@ function auth($name)
echo $playlist;
} else {

if (!in_array($type, ['name', 'artist', 'url', 'cover', 'lrc', 'single'])) {
echo '{"error":"unknown type"}';
exit;
}

if (APCU_CACHE) {
$apcu_time = $type == 'url' ? 1800 : 7200;
$apcu_key = $server . $type . $id;
if (apcu_exists($apcu_key)) {
$data = apcu_fetch($apcu_key);
if (in_array($type, ['url', 'cover'])) {
header('Location: ' . apcu_fetch($apcu_key));
header('Location: ' . $data);
} else {
echo apcu_fetch($apcu_key);
echo $data;
}
exit;
}
Expand All @@ -129,26 +130,33 @@ function auth($name)
apcu_store($song_apcu_key, $song, $apcu_time);
}

$song = json_decode($song)[0];
$data = song2data($api, json_decode($song)[0], $type);

switch ($type) {
case 'name':
if (APCU_CACHE) {
apcu_store($apcu_key, $data, $apcu_time);
}

if (APCU_CACHE) {
apcu_store($apcu_key, $song->name, $apcu_time);
}
if (in_array($type, ['url', 'cover'])) {
header('Location: ' . $data);
} else {
echo $data;
}
}

function auth($name)
{
return hash_hmac('sha1', $name, AUTH_SECRET);
}

echo $song->name;
function song2data($api, $song, $type)
{
switch ($type) {
case 'name':
return $song->name;
break;

case 'artist':
$artist = implode('/', $song->artist);

if (APCU_CACHE) {
apcu_store($apcu_key, $artist, $apcu_time);
}

echo $artist;
return implode('/', $song->artist);
break;

case 'url':
Expand All @@ -160,11 +168,7 @@ function auth($name)
$m_url = str_replace('http', 'https', $m_url);
}

if (APCU_CACHE) {
apcu_store($apcu_key, $m_url, $apcu_time);
}

header('Location: ' . $m_url);
return $m_url;
break;

case 'cover':
Expand All @@ -173,11 +177,7 @@ function auth($name)
exit;
}

if (APCU_CACHE) {
apcu_store($apcu_key, $c_url, $apcu_time);
}

header('Location: ' . $c_url);
return $c_url;
break;

case 'lrc':
Expand Down Expand Up @@ -209,30 +209,17 @@ function auth($name)
$lrc = $lrc_data->lyric;
}

if (APCU_CACHE) {
apcu_store($apcu_key, $lrc, $apcu_time);
}

echo $lrc;
return $lrc;
break;

case 'single':
$single = json_encode(array(
return json_encode(array(
'name' => $song->name,
'artist' => implode('/', $song->artist),
'url' => API_URI . '?server=' . $song->source . '&type=url&id=' . $song->url_id . (AUTH ? '&auth=' . auth($song->source . 'url' . $song->url_id) : ''),
'cover' => API_URI . '?server=' . $song->source . '&type=cover&id=' . $song->url_id . (AUTH ? '&auth=' . auth($song->source . 'cover' . $song->url_id) : ''),
'lrc' => API_URI . '?server=' . $song->source . '&type=lrc&id=' . $song->lyric_id . (AUTH ? '&auth=' . auth($song->source . 'lrc' . $song->lyric_id) : '')
));

if (APCU_CACHE) {
apcu_store($apcu_key, $single, $apcu_time);
}

echo $single;
break;

default:
echo '{"error":"unknown type"}';
}
}

0 comments on commit 80d84d1

Please sign in to comment.