str_replace($basePath, '', $fullPath), 'source' => 'local'];
}
}
}
}
closedir($handle);
}
}
// Fetch MIDI files from URL (parse HTML directory index)
function fetchMidiFromUrl($url, &$files) {
if (empty($url)) {
return;
}
$baseUrl = rtrim($url, '/') . '/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($content === false || $httpCode != 200) {
echo 'Error: Failed to fetch URL: ' . htmlspecialchars($url) . ' (HTTP ' . $httpCode . ', Error: ' . htmlspecialchars($error) . ')
';
return;
}
$doc = new DOMDocument();
@$doc->loadHTML($content);
$links = $doc->getElementsByTagName('a');
$seenPaths = array_map(function($file) { return normalizeUrl($file['path']); }, $files);
foreach ($links as $link) {
$href = $link->getAttribute('href');
if (preg_match('/\.(mid|midi|kar)$/i', $href)) {
if (stripos($href, 'md5sum') !== false) {
continue;
}
if (!preg_match('#^https?://#i', $href)) {
$href = $baseUrl . ltrim($href, '/');
}
$normalizedHref = normalizeUrl($href);
if (!in_array($normalizedHref, $seenPaths)) {
$ch = curl_init($href);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$fileHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($fileHttpCode == 200) {
$files[] = ['path' => $href, 'source' => 'import'];
$seenPaths[] = $normalizedHref;
} else {
echo 'Warning: Cannot access MIDI file: ' . htmlspecialchars($href) . ' (HTTP ' . $fileHttpCode . ')
';
}
}
}
}
}
// Initialize
$midiFiles = [];
$absoluteMidiPath = realpath($baseMidiDirectory);
$currentScript = basename($_SERVER['PHP_SELF']);
$playIndex = isset($_GET['play_index']) ? (int)$_GET['play_index'] : -1;
$refreshDuration = max((int)($_GET['refresh_time'] ?? 120), 1);
$autoPlay = isset($_GET['auto_play']) ? (int)$_GET['auto_play'] : 1;
$midiUrl = isset($_GET['midi_url']) && !empty(trim($_GET['midi_url'])) ? trim($_GET['midi_url']) : (isset($_GET['midi_source']) ? trim($_GET['midi_source']) : '');
$loop = isset($_GET['loop']) ? (int)$_GET['loop'] : (isset($_SESSION['loop']) ? (int)$_SESSION['loop'] : 0);
$_SESSION['loop'] = $loop;
// Load or rebuild playlist
if (!isset($_SESSION['midiFiles']) || $_SESSION['midiUrl'] !== $midiUrl) {
if (!is_dir($baseMidiDirectory)) {
echo 'Error: MIDI directory "' . htmlspecialchars($baseMidiDirectory) . '" not found.
';
} elseif (!($handle = opendir($baseMidiDirectory))) {
echo 'Error: Cannot open MIDI directory "' . htmlspecialchars($baseMidiDirectory) . '". Check permissions.
';
} else {
scanMidiDirectory($baseMidiDirectory, $baseMidiDirectory, $midiFiles, $ignoredDirectories);
fetchMidiFromUrl($midiUrl, $midiFiles);
usort($midiFiles, function($a, $b) {
if ($a['source'] !== $b['source']) {
return $a['source'] === 'import' ? -1 : 1;
}
return $a['source'] === 'import' ?
strcmp(basename($a['path']), basename($b['path'])) :
strcmp($a['path'], $b['path']);
});
// Remove duplicates based on normalized path and source
$uniqueFiles = [];
$seenKeys = [];
foreach ($midiFiles as $file) {
$key = normalizeUrl($file['path']) . '|' . $file['source'];
if (!in_array($key, $seenKeys)) {
$uniqueFiles[] = $file;
$seenKeys[] = $key;
}
}
$midiFiles = $uniqueFiles;
// Store in session
$_SESSION['midiFiles'] = $midiFiles;
$_SESSION['midiUrl'] = $midiUrl;
}
} else {
$midiFiles = $_SESSION['midiFiles'];
}
// Playback logic
if (!empty($midiFiles) && $playIndex >= 0 && $playIndex < count($midiFiles)) {
$currentMidiFile = $midiFiles[$playIndex];
$midiPlayerSrc = $currentMidiFile['source'] === 'local' ? $baseMidiDirectory . $currentMidiFile['path'] : $currentMidiFile['path'];
if ($currentMidiFile['source'] === 'import') {
$tempDir = 'temp_midi/';
if (!is_dir($tempDir)) {
mkdir($tempDir, 0755, true);
}
$tempFile = $tempDir . basename($currentMidiFile['path']);
if (!file_exists($tempFile)) {
$ch = curl_init($currentMidiFile['path']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$midiContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($midiContent !== false && $httpCode == 200) {
file_put_contents($tempFile, $midiContent);
$midiPlayerSrc = $tempFile;
} else {
echo 'Error: Failed to download MIDI: ' . htmlspecialchars($currentMidiFile['path']) . ' (HTTP ' . $httpCode . ')
';
$nextIndex = ($playIndex + 1) % count($midiFiles);
$refreshUrl = buildUrl($currentScript, [
'play_index' => $nextIndex,
'refresh_time' => $refreshDuration,
'auto_play' => $autoPlay,
'midi_url' => $midiUrl,
'loop' => $loop
]);
echo "";
exit;
}
} else {
$midiPlayerSrc = $tempFile;
}
}
$nextIndex = ($playIndex + 1) % count($midiFiles);
$prevIndex = ($playIndex - 1 + count($midiFiles)) % count($midiFiles);
$refreshUrl = buildUrl($currentScript, [
'play_index' => $loop ? $playIndex : $nextIndex,
'refresh_time' => $refreshDuration,
'auto_play' => $autoPlay,
'midi_url' => $midiUrl,
'loop' => $loop
]);
$skipUrl = buildUrl($currentScript, [
'play_index' => $nextIndex,
'refresh_time' => $refreshDuration,
'auto_play' => $autoPlay,
'midi_url' => $midiUrl,
'loop' => $loop
]);
$backUrl = buildUrl($currentScript, [
'play_index' => $prevIndex,
'refresh_time' => $refreshDuration,
'auto_play' => $autoPlay,
'midi_url' => $midiUrl,
'loop' => $loop
]);
$autoPlayUrl = buildUrl($currentScript, [
'play_index' => $playIndex,
'refresh_time' => $refreshDuration,
'auto_play' => $autoPlay ? 0 : 1,
'midi_url' => $midiUrl,
'loop' => $loop
]);
$loopUrl = buildUrl($currentScript, [
'play_index' => $playIndex,
'refresh_time' => $refreshDuration,
'auto_play' => $autoPlay,
'midi_url' => $midiUrl,
'loop' => $loop ? 0 : 1
]);
$stopUrl = buildUrl($currentScript, ['midi_url' => $midiUrl]);
?>
No MIDI files found in the directory or from the provided URL.';
} else {
foreach ($midiFiles as $index => $file) {
$displayName = $file['source'] === 'import' ? '[Import] ' . htmlspecialchars(basename($file['path'])) : htmlspecialchars($file['path']);
$fileUrl = buildUrl($currentScript, [
'play_index' => $index,
'refresh_time' => $refreshDuration,
'auto_play' => $autoPlay,
'midi_url' => $midiUrl,
'loop' => $loop
]);
echo "
";
}
}
?>
| Click a MIDI to start the playlist. |
|
|