[FEAT] First functional version.

This commit is contained in:
NADAL Jean-Baptiste
2026-02-18 10:08:48 +01:00
parent 5c93000873
commit bc6e603af4
32 changed files with 5618 additions and 30 deletions

122
api/lib/Auth.php Normal file
View File

@@ -0,0 +1,122 @@
<?php
class Auth {
private const JWT_SECRET = 'ohmj_secret_key_change_in_production';
private const JWT_ALGO = 'HS256';
private const JWT_EXPIRY = 3600; // 1 hour
private string $usersFile;
public function __construct(string $usersFile = null) {
$this->usersFile = $usersFile ?? __DIR__ . '/../config/users.json';
}
public function login(string $username, string $password): array {
$users = $this->loadUsers();
foreach ($users['users'] as $user) {
if ($user['username'] === $username) {
if (password_verify($password, $user['password'])) {
$token = $this->generateToken($user);
return [
'success' => true,
'token' => $token,
'user' => [
'username' => $user['username'],
'role' => $user['role']
]
];
}
return ['success' => false, 'error' => 'Invalid password'];
}
}
return ['success' => false, 'error' => 'User not found'];
}
public function verifyToken(string $token): ?array {
$parts = explode('.', $token);
if (count($parts) !== 3) {
return null;
}
list($header, $payload, $signature) = $parts;
// Verify signature
$expectedSignature = base64_encode(
hash_hmac('sha256', "$header.$payload", self::JWT_SECRET, true)
);
if (!hash_equals($expectedSignature, $signature)) {
return null;
}
// Decode payload
$payloadData = json_decode(base64_decode($payload), true);
// Check expiry
if (isset($payloadData['exp']) && $payloadData['exp'] < time()) {
return null;
}
return $payloadData;
}
public function requireAuth(string $token): array {
$payload = $this->verifyToken($token);
if ($payload === null) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
exit;
}
return $payload;
}
public function requireAdmin(string $token): array {
$payload = $this->requireAuth($token);
if ($payload['role'] !== 'admin') {
http_response_code(403);
echo json_encode(['error' => 'Forbidden']);
exit;
}
return $payload;
}
private function generateToken(array $user): string {
$header = base64_encode(json_encode([
'alg' => self::JWT_ALGO,
'typ' => 'JWT'
]));
$payload = base64_encode(json_encode([
'username' => $user['username'],
'role' => $user['role'],
'iat' => time(),
'exp' => time() + self::JWT_EXPIRY
]));
$signature = base64_encode(
hash_hmac('sha256', "$header.$payload", self::JWT_SECRET, true)
);
return "$header.$payload.$signature";
}
private function loadUsers(): array {
if (!file_exists($this->usersFile)) {
return ['users' => []];
}
$content = file_get_contents($this->usersFile);
return json_decode($content, true) ?? ['users' => []];
}
public function hashPassword(string $password): string {
return password_hash($password, PASSWORD_BCRYPT);
}
}

View File

@@ -13,6 +13,10 @@ class ScoreScanner {
foreach ($directories as $dir) {
if ($dir === '.' || $dir === '..') continue;
// Ignorer les fichiers cachés et non-numériques
if (strpos($dir, '.') === 0) continue;
if (!is_numeric($dir)) continue;
$scorePath = $this->scoresPath . $dir;
if (!is_dir($scorePath)) continue;
@@ -38,12 +42,16 @@ class ScoreScanner {
return null;
}
$ini = parse_ini_file($iniFile);
$ini = @parse_ini_file($iniFile, true);
if ($ini === false) {
return null;
}
return [
'id' => $id,
'name' => $ini['name'] ?? 'Inconnu',
'compositor' => $ini['compositor'] ?? 'Inconnu'
'name' => $ini['info']['name'] ?? 'Inconnu',
'compositor' => $ini['info']['compositor'] ?? 'Inconnu',
'ressource' => $ini['info']['ressource'] ?? null
];
}
@@ -60,17 +68,28 @@ class ScoreScanner {
}
$instruments = [];
$entries = scandir($scoreDir);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..' || $entry === 'score.ini') continue;
// New structure: NUM/PIECE/INSTRUMENT/VERSION/
// First get all piece directories
$pieceDirs = scandir($scoreDir);
foreach ($pieceDirs as $pieceDir) {
if ($pieceDir === '.' || $pieceDir === '..' || $pieceDir === 'score.ini') continue;
if (strpos($pieceDir, '.') === 0) continue;
if (!is_dir($scoreDir . '/' . $pieceDir)) continue;
$instrumentPath = $scoreDir . '/' . $entry;
if (!is_dir($instrumentPath)) continue;
// Then get instrument directories
$instrumentDirs = scandir($scoreDir . '/' . $pieceDir);
$instrument = $this->getInstrumentInfo($id, $entry);
if ($instrument) {
$instruments[] = $instrument;
foreach ($instrumentDirs as $instrumentId) {
if ($instrumentId === '.' || $instrumentId === '..') continue;
if (strpos($instrumentId, '.') === 0) continue;
if (!is_dir($scoreDir . '/' . $pieceDir . '/' . $instrumentId)) continue;
$instrument = $this->getInstrumentInfo($id, $instrumentId, $pieceDir);
if ($instrument) {
$instruments[] = $instrument;
}
}
}
@@ -82,8 +101,13 @@ class ScoreScanner {
return array_merge($basicInfo, ['instruments' => $instruments]);
}
private function getInstrumentInfo($scoreId, $instrumentId) {
$instrumentPath = $this->scoresPath . $scoreId . '/' . $instrumentId;
private function getInstrumentInfo($scoreId, $instrumentId, $pieceId = null) {
// New structure: NUM/PIECE/INSTRUMENT/VERSION/
if ($pieceId !== null) {
$instrumentPath = $this->scoresPath . $scoreId . '/' . $pieceId . '/' . $instrumentId;
} else {
$instrumentPath = $this->scoresPath . $scoreId . '/' . $instrumentId;
}
if (!is_dir($instrumentPath)) {
return null;
@@ -95,11 +119,12 @@ class ScoreScanner {
$entries = scandir($instrumentPath);
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') continue;
if (strpos($entry, '.') === 0) continue;
$partPath = $instrumentPath . '/' . $entry;
if (!is_dir($partPath)) continue;
$part = $this->getPartInfo($scoreId, $instrumentId, $entry);
$part = $this->getPartInfo($scoreId, $instrumentId, $entry, $pieceId);
if ($part) {
$parts[] = $part;
}
@@ -117,12 +142,18 @@ class ScoreScanner {
return [
'id' => $instrumentId,
'title' => $title,
'piece' => $pieceId,
'parts' => $parts
];
}
private function getPartInfo($scoreId, $instrumentId, $partId) {
$partPath = $this->scoresPath . $scoreId . '/' . $instrumentId . '/' . $partId;
private function getPartInfo($scoreId, $instrumentId, $partId, $pieceId = null) {
// New structure: NUM/PIECE/INSTRUMENT/VERSION/
if ($pieceId !== null) {
$partPath = $this->scoresPath . $scoreId . '/' . $pieceId . '/' . $instrumentId . '/' . $partId;
} else {
$partPath = $this->scoresPath . $scoreId . '/' . $instrumentId . '/' . $partId;
}
if (!is_dir($partPath)) {
return null;
@@ -133,6 +164,7 @@ class ScoreScanner {
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') continue;
if (strpos($entry, '.') === 0) continue;
$filePath = $partPath . '/' . $entry;
if (!is_file($filePath)) continue;
@@ -140,10 +172,20 @@ class ScoreScanner {
// Vérifier que c'est un PDF
if (strtolower(pathinfo($entry, PATHINFO_EXTENSION)) !== 'pdf') continue;
$relativePath = $pieceId !== null
? "$scoreId/$pieceId/$instrumentId/$partId/$entry"
: "$scoreId/$instrumentId/$partId/$entry";
$parsed = $this->parseFilename(pathinfo($entry, PATHINFO_FILENAME));
$files[] = [
'name' => pathinfo($entry, PATHINFO_FILENAME),
'filename' => $entry,
'path' => $scoreId . '/' . $instrumentId . '/' . $partId . '/' . $entry
'path' => $relativePath,
'part' => $parsed['part'],
'key' => $parsed['key'],
'clef' => $parsed['clef'],
'variant' => $parsed['variant']
];
}
@@ -191,4 +233,272 @@ class ScoreScanner {
return $names[$code] ?? $code;
}
private function parseFilename(string $filename): array {
$result = [
'part' => null,
'key' => null,
'clef' => null,
'variant' => null
];
// Normalize: replace - by _ and lowercase
$name = strtolower(str_replace('-', '_', $filename));
// Extract clef (clesol, clefa)
if (preg_match('/(clesol|clefa)$/', $name, $m)) {
$result['clef'] = $m[1];
$name = preg_replace('/_(clesol|clefa)$/', '', $name);
}
// Extract variant (solo, etc.)
$variants = ['solo', 'default'];
foreach ($variants as $variant) {
if (strpos($name, $variant) !== false) {
$result['variant'] = $variant;
$name = str_replace('_' . $variant, '', $name);
break;
}
}
// Extract key (tonalités: sib, mib, fa, do, etc.)
$keys = ['sib', 'mib', 'reb', 'dob', 'solb', 'lab', 'mibemol', 'sibemol'];
foreach ($keys as $key) {
if (preg_match('/_' . $key . '/', $name)) {
$result['key'] = $key;
$name = preg_replace('/_' . $key . '/', '', $name);
break;
}
}
// If no compound keys found, try single keys
if ($result['key'] === null) {
$singleKeys = ['fa', 'do', 'ut', 'sol', 're', 'mi', 'si'];
foreach ($singleKeys as $key) {
if (preg_match('/_' . $key . '(?:_|$)/', $name)) {
$result['key'] = $key;
$name = preg_replace('/_' . $key . '/', '', $name);
break;
}
}
}
// Extract part number (1, 2, 1_et_2, etc.) - should be last
// Handle formats: 1, 2, 1_et_2
if (preg_match('/_(\d+(?:_\w+)*)$/', $name, $m)) {
$result['part'] = $m[1];
$name = preg_replace('/_\d+(?:_\w+)*$/', '', $name);
}
return $result;
}
// API Methods
public function listScores(): array {
return $this->getAllScores();
}
public function getScore(string $id): ?array {
return $this->getScoreDetail($id);
}
public function getPieces(string $scoreId): array {
$scoreDir = $this->scoresPath . $scoreId;
$iniFile = $scoreDir . '/score.ini';
if (!file_exists($iniFile)) {
return [];
}
$ini = @parse_ini_file($iniFile, true);
if ($ini === false) {
return [];
}
$pieces = [];
// Check if [pieces] section exists
if (isset($ini['pieces'])) {
$piecesSection = $ini['pieces'];
$count = intval($piecesSection['count'] ?? 1);
for ($i = 1; $i <= $count; $i++) {
$pieces[] = [
'id' => $i,
'name' => $piecesSection[$i] ?? "Pièce $i"
];
}
}
return $pieces;
}
public function getInstruments(string $scoreId, ?string $pieceId = null): array {
$scoreDir = $this->scoresPath . $scoreId;
if (!is_dir($scoreDir)) {
return [];
}
$instruments = [];
// New structure: NUM/PIECE/INSTRUMENT/VERSION/
$pieceDirs = scandir($scoreDir);
foreach ($pieceDirs as $pieceDir) {
if ($pieceDir === '.' || $pieceDir === '..' || $pieceDir === 'score.ini') continue;
if (strpos($pieceDir, '.') === 0) continue;
if (!is_dir($scoreDir . '/' . $pieceDir)) continue;
// Filter by piece if specified
if ($pieceId !== null && $pieceDir !== $pieceId) continue;
$instrumentDirs = scandir($scoreDir . '/' . $pieceDir);
foreach ($instrumentDirs as $instrumentId) {
if ($instrumentId === '.' || $instrumentId === '..') continue;
if (strpos($instrumentId, '.') === 0) continue;
if (!is_dir($scoreDir . '/' . $pieceDir . '/' . $instrumentId)) continue;
$instrument = $this->getInstrumentInfo($scoreId, $instrumentId, $pieceDir);
if ($instrument) {
$instruments[] = $instrument;
}
}
}
// Sort by title
usort($instruments, function($a, $b) {
return strcmp($a['title'], $b['title']);
});
return $instruments;
}
public function createScore(string $id, string $name, string $compositor): array {
$scoreDir = $this->scoresPath . $id;
if (is_dir($scoreDir)) {
return ['success' => false, 'error' => 'Score already exists'];
}
if (!mkdir($scoreDir, 0755, true)) {
return ['success' => false, 'error' => 'Failed to create directory'];
}
$iniContent = "[info]\nname = $name\ncompositor = $compositor\n\n[pieces]\ncount = 1\n";
if (file_put_contents($scoreDir . '/score.ini', $iniContent) === false) {
return ['success' => false, 'error' => 'Failed to create score.ini'];
}
return [
'success' => true,
'score' => [
'id' => $id,
'name' => $name,
'compositor' => $compositor
]
];
}
public function updateScore(string $scoreId, ?string $name, ?string $compositor): array {
$scoreDir = $this->scoresPath . $scoreId;
$iniFile = $scoreDir . '/score.ini';
if (!file_exists($iniFile)) {
return ['success' => false, 'error' => 'Score not found'];
}
$ini = @parse_ini_file($iniFile, true);
if ($ini === false) {
return ['success' => false, 'error' => 'Failed to parse score.ini'];
}
// Update values
if ($name !== null) {
$ini['info']['name'] = $name;
}
if ($compositor !== null) {
$ini['info']['compositor'] = $compositor;
}
// Rebuild ini content
$content = "[info]\n";
$content .= "name = " . ($ini['info']['name'] ?? '') . "\n";
$content .= "compositor = " . ($ini['info']['compositor'] ?? '') . "\n\n";
$content .= "[pieces]\n";
if (isset($ini['pieces'])) {
foreach ($ini['pieces'] as $key => $value) {
$content .= "$key = $value\n";
}
}
if (file_put_contents($iniFile, $content) === false) {
return ['success' => false, 'error' => 'Failed to write score.ini'];
}
return ['success' => true];
}
public function deleteScore(string $scoreId): array {
$scoreDir = $this->scoresPath . $scoreId;
if (!is_dir($scoreDir)) {
return ['success' => false, 'error' => 'Score not found'];
}
// Recursive delete
$this->deleteDirectory($scoreDir);
return ['success' => true];
}
private function deleteDirectory(string $dir): void {
if (!is_dir($dir)) return;
$items = scandir($dir);
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$path = $dir . '/' . $item;
if (is_dir($path)) {
$this->deleteDirectory($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
public function uploadPdf(string $scoreId, array $file, string $piece, string $instrument, string $version, string $filename): array {
$scoreDir = $this->scoresPath . $scoreId;
if (!is_dir($scoreDir)) {
return ['success' => false, 'error' => 'Score not found'];
}
// Create directory structure: scoreId/piece/instrument/version
$targetDir = $scoreDir . '/' . $piece . '/' . $instrument . '/' . $version;
if (!mkdir($targetDir, 0755, true)) {
return ['success' => false, 'error' => 'Failed to create directory'];
}
// Determine filename
if (empty($filename)) {
$filename = $file['name'];
}
$targetPath = $targetDir . '/' . $filename;
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
return ['success' => true, 'path' => "$scoreId/$piece/$instrument/$version/$filename"];
}
return ['success' => false, 'error' => 'Failed to move uploaded file'];
}
}