Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions public/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
// Handle rem user
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
try {
$idToRemove = (int) $_GET['id'];
$idToRemove = (string) $_GET['id'];
if ($idToRemove === $auth->getCurrentUserId()) {
throw new Exception("Não é possível remover a si próprio.");
}
Expand All @@ -79,7 +79,7 @@
<link rel="stylesheet" href="/css/admin.css">
</head>
<body>
<nav class="navbar glass-panel admin-nav">
<nav class="navbar glass-panel dashboard-nav">
<a href="/dashboard.php" class="nav-logo-link"><h2 class="text-gradient nav-logo-text">Web Storage</h2></a>
<div class="nav-links nav-links-override">
<div class="profile-dropdown" id="profileDropdown">
Expand All @@ -91,6 +91,10 @@
<svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path></svg>
Meus Arquivos
</a>
<a href="/shares.php" class="dropdown-item dropdown-item-override">
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"></path></svg>
Compartilhamentos
</a>
<?php if (isset($auth) && $auth->isAdmin()): ?>
<a href="/admin.php" class="dropdown-item dropdown-item-override">
<svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
Expand Down Expand Up @@ -148,7 +152,7 @@
<tbody>
<?php foreach ($users as $user): ?>
<tr class="file-row">
<td>#<?= $user['id'] ?></td>
<td><?= $user['id'] ?></td>
<td>
<div class="file-name">
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path></svg>
Expand Down Expand Up @@ -203,7 +207,7 @@
</select>
</div>

<button type="submit" class="btn btn-primary btn-block btn-submit-override">
<button type="submit" class="btn btn-primary btn-block btn-submit-override" name="action" value="add_user">
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24" class="icon-align"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
Criar Usuário
</button>
Expand Down
6 changes: 5 additions & 1 deletion public/change_password.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<link rel="stylesheet" href="/css/change_password.css">
</head>
<body>
<nav class="navbar glass-panel change-password-nav">
<nav class="navbar glass-panel dashboard-nav">
<a href="/dashboard.php" class="nav-logo-link"><h2 class="text-gradient nav-logo-text">Web Storage</h2></a>
<div class="nav-links nav-links-override">
<div class="profile-dropdown" id="profileDropdown">
Expand All @@ -62,6 +62,10 @@
<svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path></svg>
Meus Arquivos
</a>
<a href="/shares.php" class="dropdown-item dropdown-item-override">
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"></path></svg>
Compartilhamentos
</a>
<?php if (isset($auth) && $auth->isAdmin()): ?>
<a href="/admin.php" class="dropdown-item dropdown-item-override">
<svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
Expand Down
6 changes: 5 additions & 1 deletion public/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<link rel="stylesheet" href="/css/edit.css">
</head>
<body>
<nav class="navbar glass-panel edit-nav">
<nav class="navbar glass-panel dashboard-nav">
<a href="/dashboard.php" class="nav-logo-link"><h2 class="text-gradient nav-logo-text">Web Storage</h2></a>
<div class="nav-links nav-links-override">
<div class="profile-dropdown" id="profileDropdown">
Expand All @@ -73,6 +73,10 @@
<svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path></svg>
Meus Arquivos
</a>
<a href="/shares.php" class="dropdown-item dropdown-item-override">
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"></path></svg>
Compartilhamentos
</a>
<?php if (isset($auth) && $auth->isAdmin()): ?>
<a href="/admin.php" class="dropdown-item dropdown-item-override">
<svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
Expand Down
2 changes: 1 addition & 1 deletion public/shares.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function formatExpiry($date) {
<svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path></svg>
Meus Arquivos
</a>
<a href="/shares.php" class="dropdown-item dropdown-item-override active" style="background: rgba(0,0,0,0.05);">
<a href="/shares.php" class="dropdown-item dropdown-item-override">
<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"></path></svg>
Compartilhamentos
</a>
Expand Down
2 changes: 1 addition & 1 deletion src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function mustChangePassword(): bool {
return isset($_SESSION['must_change_password']) && $_SESSION['must_change_password'];
}

public function getCurrentUserId(): ?int {
public function getCurrentUserId(): ?string {
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
Expand Down
4 changes: 2 additions & 2 deletions src/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class FileManager {
private StorageInterface $storage;
private int $userId;
private string $userId;

public function __construct(int $userId, StorageInterface $storage) {
public function __construct(string $userId, StorageInterface $storage) {
$this->userId = $userId;
$this->storage = $storage;
$this->storage->createDirectory($this->getUserPrefix());
Expand Down
12 changes: 6 additions & 6 deletions src/ShareManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public function __construct(Database $db) {
/**
* Creates a new share link for a file.
*
* @param int $userId The ID of the user who owns the file.
* @param string $userId The ID of the user who owns the file.
* @param string $filename The name of the file to share.
* @param string $duration The duration of the share ('1h', '1d', or 'forever').
* @return string The generated UUID for the share link.
*/
public function createShare(int $userId, string $filename, string $duration): string {
public function createShare(string $userId, string $filename, string $duration): string {
$uuid = $this->generateUuidV4();
$expiresAt = null;

Expand Down Expand Up @@ -58,10 +58,10 @@ public function getShare(string $uuid): ?array {
/**
* Lists all shares for a specific user.
*
* @param int $userId The ID of the user.
* @param string $userId The ID of the user.
* @return array List of shares.
*/
public function listShares(int $userId): array {
public function listShares(string $userId): array {
$stmt = $this->pdo->prepare("
SELECT * FROM shared_files
WHERE user_id = ?
Expand All @@ -75,10 +75,10 @@ public function listShares(int $userId): array {
* Deletes a specific share.
*
* @param string $uuid The UUID of the share.
* @param int $userId The ID of the user (for security).
* @param string $userId The ID of the user (for security).
* @return bool True on success.
*/
public function deleteShare(string $uuid, int $userId): bool {
public function deleteShare(string $uuid, string $userId): bool {
$stmt = $this->pdo->prepare("
DELETE FROM shared_files
WHERE uuid = ? AND user_id = ?
Expand Down
13 changes: 7 additions & 6 deletions src/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ public function __construct(Database $database) {
$this->db = $database->getConnection();
}

public function addUser(string $username, string $password, string $role = 'user'): int {
public function addUser(string $username, string $password, string $role = 'user'): string {
if ($this->getUserByUsername($username)) {
throw new Exception("Username already exists.");
}

$hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $this->db->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
$stmt = $this->db->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?) RETURNING id");
$stmt->execute([$username, $hash, $role]);
return (int) $this->db->lastInsertId();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return (string) $result['id'];
}

public function changePassword(int $userId, string $currentPassword, string $newPassword): bool {
public function changePassword(string $userId, string $currentPassword, string $newPassword): bool {
$stmt = $this->db->prepare("SELECT password FROM users WHERE id = ?");
$stmt->execute([$userId]);
$user = $stmt->fetch();
Expand All @@ -42,7 +43,7 @@ public function changePassword(int $userId, string $currentPassword, string $new
return $updateStmt->execute([$hash, $userId]);
}

public function removeUser(int $id): bool {
public function removeUser(string $id): bool {
// Find user first to prevent removing the last admin (logic simplified for now)
$stmt = $this->db->prepare("DELETE FROM users WHERE id = ?");
return $stmt->execute([$id]);
Expand All @@ -60,7 +61,7 @@ public function getUserByUsername(string $username): ?array {
return $user ?: null;
}

public function getUserById(int $id): ?array {
public function getUserById(string $id): ?array {
$stmt = $this->db->prepare("SELECT id, username, role, created_at FROM users WHERE id = ?");
$stmt->execute([$id]);
$user = $stmt->fetch();
Expand Down
4 changes: 2 additions & 2 deletions src/init_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
echo "Creating users table...\n";
$pdo->exec("
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(50) DEFAULT 'user',
Expand All @@ -31,7 +31,7 @@
$pdo->exec("
CREATE TABLE IF NOT EXISTS shared_files (
uuid UUID PRIMARY KEY,
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
user_id uuid REFERENCES users(id) ON DELETE CASCADE,
filename TEXT NOT NULL,
expires_at TIMESTAMP NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Expand Down
4 changes: 2 additions & 2 deletions tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function setUp(): void {
$pdo->exec("DROP TABLE IF EXISTS users CASCADE;");
$pdo->exec("
CREATE TABLE users (
id SERIAL PRIMARY KEY,
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(50) DEFAULT 'user',
Expand All @@ -47,7 +47,7 @@ public function testValidLogin() {
$this->assertTrue($this->auth->login('admin', 'adminpass'));
$this->assertTrue($this->auth->isLoggedIn());
$this->assertTrue($this->auth->isAdmin());
$this->assertEquals(1, $this->auth->getCurrentUserId());
$this->assertMatchesRegularExpression('/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i', $this->auth->getCurrentUserId());
}

public function testInvalidLogin() {
Expand Down
Loading
Loading