-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd.php
97 lines (82 loc) · 3.48 KB
/
add.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
// Global database/session initialization.
require_once "./config.php";
if(!isset($_SESSION['loggedin'])){
header("Location: ./login.php");
exit;
}
$current_folder_id = trim($_GET["parent"]);
?>
<html>
<head>
<title>Home - Nilamark</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.svg">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php include 'header.php'; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<label for="url">URL:</label>
<input type="text" id="url" name="url" placeholder="URL...">
<label for="title">Title:</label>
<input type="text" id="title" name="title" placeholder="### - Title...">
<label for="folder">Folder:</label>
<select id="folder" name="folder">
<?php
$sql = $pdo->prepare("SELECT id,title FROM nm_folders ORDER BY id DESC");
$sql->execute();
$folder_list = $sql->fetchAll();
foreach($folder_list as &$folder) {
echo "<option value='" . $folder['id'] . "'> (#" . $folder['id'] . ") " . $folder['title'] . "</option>";
}
?>
</select>
<script>
document.getElementById('folder').value=<?php echo $current_folder_id; ?>;
</script>
<button type="submit" id="submitForm">
<img src="./assets/save.svg" /><span>Save</span>
</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$url = $_POST['url'];
$title = $_POST['title'];
$folder = $_POST['folder'];
$sql_existing = $pdo->prepare("SELECT url FROM nm_bookmarks");
$sql_existing->execute();
$existing_bookmarks_raw = $sql_existing->fetchAll();
$existing_bookmarks = array_map(function($piece){
return (string) $piece;
}, $existing_bookmarks_raw);
if (empty($url) | empty($title) | empty($folder)) {
echo "Missing parameters.";
} else {
$sql_check_existing = $pdo->prepare("SELECT url FROM nm_bookmarks WHERE url = :url");
$sql_check_existing->bindParam(':url', $url);
$sql_check_existing->execute();
$existing_bookmark = $sql_check_existing->fetch(PDO::FETCH_ASSOC);
if ( !$existing_bookmark) {
$sql1 = $pdo->prepare("INSERT INTO nm_bookmarks (url, title, added, lastmodified) VALUES (:url, :title, :timestamp1, :timestamp2)");
$sql1->bindParam(":url", $url, PDO::PARAM_STR);
$sql1->bindParam(":title", $title, PDO::PARAM_STR);
$sql1->bindParam(":timestamp1", time(), PDO::PARAM_STR);
$sql1->bindParam(":timestamp2", time(), PDO::PARAM_STR);
$sql1->execute();
$new_id = $pdo->lastInsertId();
$sql2 = $pdo->prepare("INSERT INTO nm_tree VALUES ('bookmark', :id, :parent_folder, 0)");
$sql2->bindParam(":id", $new_id, PDO::PARAM_STR);
$sql2->bindParam(":parent_folder", $folder, PDO::PARAM_STR);
$sql2->execute();
redirect("./bookmarks.php?folder=" . $folder);
} else {
echo "This URL is already bookmarked!";
}
}
}
?>
<?php require_once "./footer.php"; ?>
</body>
</html>