-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshare_note.php
More file actions
54 lines (45 loc) · 1.68 KB
/
Copy pathshare_note.php
File metadata and controls
54 lines (45 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
// share_note.php
require_once '/home/chzruk/public_html/public/bootstrap.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$conn = new mysqli('', '', '', '');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$note_id = $_GET['id'];
$user_id = $_SESSION['user_id'];
// Verificar que la nota exista y pertenezca al usuario
$stmt = $conn->prepare("SELECT * FROM note WHERE id = ? AND user_id = ?");
$stmt->bind_param("ii", $note_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
$note = $result->fetch_assoc();
if (!$note) {
die("Note not found or you do not have permission to share it.");
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$shared_with = $_POST['shared_with'] ?? null;
$permission = $_POST['permission'];
// Generar un código único si se comparte con link
$shared_code = null;
if ($shared_with === 'public') {
$shared_code = bin2hex(random_bytes(16)); // token seguro
}
$stmt = $conn->prepare("INSERT INTO shared_notes
(note_id, shared_by, shared_with, shared_code, permission)
VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("iiiss", $note_id, $user_id, $shared_with, $shared_code, $permission);
if ($stmt->execute()) {
if ($shared_code) {
echo "<p>Note shared publicly! Link: <a href='view_note.php?code=$shared_code'>view_note.php?code=$shared_code</a></p>";
} else {
echo "<script>alert('Note shared successfully!');</script>";
}
} else {
echo "<script>alert('Error: " . addslashes($stmt->error) . "');</script>";
}
}
?>