request-feed-update.php
authorChristian Weiske <cweiske@cweiske.de>
Wed, 12 Jun 2013 20:36:33 +0000 (22:36 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 12 Jun 2013 20:36:33 +0000 (22:36 +0200)
README.rst
data/tables.sql
www/request-feed-update.php [new file with mode: 0644]

index 9fff7c4e322afc1d5d8423d21b6e718e4b68c2fd..47954d07330430703dfdfb9df620c58572c4c845 100644 (file)
@@ -2,3 +2,40 @@
 stapibas
 ********
 The standalone Pingback server, written in PHP.
+
+
+
+=================
+Pingback receiver
+=================
+stapibas receives pingbacks for your website and puts them into a database.
+
+
+Setup
+=====
+Let your website send out the following HTTP header::
+
+  X-Pingback: http://stapibas.example.org/xmlrpc.php
+
+That's all.
+
+
+===============
+Pingback sender
+===============
+stapibas is able to send pingbacks out to other websites at behalf of
+your website.
+
+It does this by watching your website's Atom feed.
+Whenever it changes, it fetches the articles that are new or got updated and
+sends out pingbacks to the remote websites.
+
+
+Setup
+=====
+Insert your feed URL in the ``feeds`` database table.
+
+Whenever you update your website, tell stapibas about it via a
+HTTP POST request::
+
+  $ curl -d url=http://example.org/feed/ http://stapibas.example.org/request-feed-update.php
index a8bdda439ea5a888ea1cb470a53ac339e7c41f48..b9b2d9819f1e51964b2cf1ff7f7dcdc55054bea1 100644 (file)
@@ -9,3 +9,15 @@ CREATE TABLE IF NOT EXISTS `pingbacks` (
   PRIMARY KEY (`p_id`),
   UNIQUE KEY `p_id` (`p_id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
+
+
+CREATE TABLE IF NOT EXISTS `feeds` (
+  `f_id` int(11) NOT NULL AUTO_INCREMENT,
+  `f_url` varchar(2048) NOT NULL,
+  `f_updated` datetime NOT NULL,
+  `f_needs_update` tinyint(4) NOT NULL,
+  PRIMARY KEY (`f_id`),
+  UNIQUE KEY `f_id` (`f_id`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
+
+
diff --git a/www/request-feed-update.php b/www/request-feed-update.php
new file mode 100644 (file)
index 0000000..d82de07
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Request that a feed shall get updated, marks it as "requires update"
+ * in the database
+ *
+ * Has to be called via POST, url given in parameter "url".
+ */
+header('HTTP/1.0 500 Internal Server Error');
+header('Content-type: text/plain');
+
+require_once __DIR__ . '/../data/config.php';
+
+if (!isset($_POST['url'])) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "HTTP POST 'url' parameter missing\n";
+    exit(1);
+}
+$url = $_POST['url'];
+if ($url === '') {
+    header('HTTP/1.0 400 Bad Request');
+    echo "'url' parameter is empty\n";
+    exit(1);
+}
+if (filter_var($url, FILTER_VALIDATE_URL) === false) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Invalid URL given\n";
+    exit(1);
+}
+
+
+$db = new PDO($dbdsn, $dbuser, $dbpass);
+$res = $db->query(
+    'SELECT f_id, f_needs_update FROM feeds WHERE f_url = ' . $db->quote($url)
+);
+$row = $res->fetch(PDO::FETCH_OBJ);
+if ($row === false) {
+    header('HTTP/1.0 404 Not Found');
+    echo "Feed URL could not be found in database\n";
+    exit(1);
+}
+if ($row->f_needs_update == 1) {
+    header('HTTP/1.0 200 OK');
+    echo "Already in the queue\n";
+    exit(0);
+}
+
+$db->exec(
+    'UPDATE feeds SET f_needs_update = 1'
+    . ' WHERE f_id = ' . $db->quote($row->f_id)
+);
+
+header('HTTP/1.0 202 Accepted');
+echo "Feed has been put into the queue\n";
+exit(0);
+?>