This repository has been archived by the owner on Jul 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.php
86 lines (75 loc) · 2.64 KB
/
manage.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
<? session_start();
require_once "lib/config.php";
require_once "lib/Feed.php";
$error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$feedurl = trim ( $_POST["feedurl"] );
if ( empty ( $feedurl ) ) {
$error = "No feed given";
} else if (filter_var($feedurl, FILTER_VALIDATE_URL) === false) {
$error = "Not a valid url";
} else {
$exists = $db->prepare("SELECT * FROM feeds WHERE `link` = :link LIMIT 0, 1");
$exists->execute( [":link" => $feedurl ] );
if ( $exists->rowCount() != 0 ) {
$feed = Feed::feedFromRow( $exists->fetch(PDO::FETCH_ASSOC) );
} else {
$feed = Feed::feedFromUrl( $feedurl );
$request = "INSERT INTO `feeds` (`title`, `summary`, `link`) VALUES ( :title, :summary, :link)";
$statement = $db->prepare($request);
$statement->execute( [":title" => $feed->title, ":summary" => $feed->summary, ":link" => $feed->link] );
$feed->id = $db->lastInsertId();
}
// test if the feed is already subscribed
$exists = $db->prepare( "SELECT * FROM subscriptions WHERE user_id = :user_id AND feed_id = :feed_id" );
$exists->execute([":user_id" => $_SESSION["user_id"], ":feed_id" => $feed->id]);
if ( $exists->rowCount() != 0 ) {
$error = "Already subscribed";
} else {
// insert the subscription into the db
$request = "INSERT INTO `subscriptions` (`user_id`, `feed_id`) VALUES (:user_id, :feed_id)";
$statement = $db->prepare( $request );
$statement->execute([":user_id" => $_SESSION["user_id"], ":feed_id" => $feed->id]);
$error = "Subscribed to " . $feed->title . "!";
}
}
}
$error = $error == "" ? $error : $error . "<br />";
?>
<!-- imports -->
<link rel="stylesheet" type="text/css" href="lib/style.css" />
<script type="text/javascript" src="lib/script.js"></script>
<!-- header -->
<? include("lib/header.php")?>
<!-- content -->
<body onLoad="getSubscriptions()">
<table><thead>
<tr>
<td><b>Last Published</b></td>
<td><b>Title</b></td>
<td align="right"><b>Feed ID</b></td>
<td align="right"><b>Star</b></td>
<td align="right"><b>Unsubscribe</b></td>
</tr>
</thead><tbody id="posttable" >
<tr>
<td></td>
<td id="bottomText" colspan="4">RSS-feed will be displayed here [...]</td>
</tr>
</tbody><tfoot>
<tr height="20em"></tr>
<tr>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<td><h1>Add New Feed</h1></td>
<td align="center" colspan="3">
<input id="search" type="text" name="feedurl" onkeyup="showFeeds(this.value)" required>
</td>
<td align="center">
<input onclick="refreshArticles()" type="submit" value="Add" name="add">
</td>
</tr>
</form>
<tr><td></td><td colspan="1" align="center" id="searchresults"></td><td></td></tr>
</tfoot></table>
<span class="error"><?php echo $error ?></span>
</body>