check if pingback has been registered already
authorChristian Weiske <cweiske@cweiske.de>
Sat, 15 Jun 2013 14:20:38 +0000 (16:20 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Sat, 15 Jun 2013 14:20:38 +0000 (16:20 +0200)
src/stapibas/Feed/PingUrls.php
src/stapibas/Pingback/DbStorage.php [new file with mode: 0644]
src/stapibas/Pingback/Mailer.php [new file with mode: 0644]
www/xmlrpc.php

index 1e98f0a4ea53bf9167cd5cc1e361f1c758b821db..69045b521fb467bb5e9b93c095d78d5e00c5fb4e 100644 (file)
@@ -61,7 +61,8 @@ class Feed_PingUrls
 
         $this->log->info('Pinging %d URLs..', count($options));
         $res = $this->db->query(
 
         $this->log->info('Pinging %d URLs..', count($options));
         $res = $this->db->query(
-            'SELECT fe_url, feu_id, feu_url FROM feedentries, feedentryurls'
+            'SELECT fe_url, feu_id, feu_url, feu_tries'
+            . ' FROM feedentries, feedentryurls'
             . ' WHERE fe_id = feu_fe_id'
             . $this->sqlNeedsUpdate()
             . ' AND (' . implode(' OR ', $options) . ')'
             . ' WHERE fe_id = feu_fe_id'
             . $this->sqlNeedsUpdate()
             . ' AND (' . implode(' OR ', $options) . ')'
@@ -116,14 +117,17 @@ class Feed_PingUrls
             );
         } else {
             //error
             );
         } else {
             //error
-            $this->log->err('Error: ' . $res->getCode() . ': ' . $res->getMessage());
+            $code = $res->getCode();
+            $this->log->err('Error: ' . $code . ': ' . $res->getMessage());
             $httpRes = $res->getResponse();
             if ($httpRes) {
                 $this->log->info(
                     'Pingback response: Status code ' . $httpRes->getStatus()
                     . ', headers: ' . print_r($httpRes->getHeader(), true)
                 );
             $httpRes = $res->getResponse();
             if ($httpRes) {
                 $this->log->info(
                     'Pingback response: Status code ' . $httpRes->getStatus()
                     . ', headers: ' . print_r($httpRes->getHeader(), true)
                 );
-                //. ', body: ' .$httpRes->getBody()
+                if ($code == 100 || $code == 101 || $code == -32600) {
+                    $this->log->info('HTTP body: ' . $httpRes->getBody());
+                }
             }
             $this->db->exec(
                 'UPDATE feedentryurls SET'
             }
             $this->db->exec(
                 'UPDATE feedentryurls SET'
diff --git a/src/stapibas/Pingback/DbStorage.php b/src/stapibas/Pingback/DbStorage.php
new file mode 100644 (file)
index 0000000..61c374e
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+namespace stapibas;
+
+class Pingback_DbStorage
+    implements \PEAR2\Services\Pingback\Server\Callback\IStorage,
+    \PEAR2\Services\Pingback\Server\Callback\ILink
+{
+    public function __construct(PDO $db)
+    {
+        $this->db = $db;
+    }
+
+    public function storePingback(
+        $target, $source, $sourceBody, \HTTP_Request2_Response $res
+    ) {
+        if ($this->alreadyExists($target, $source)) {
+            throw new \Exception(
+                'Pingback from ' . $source . ' to ' . $target
+                . ' has already been registered.',
+                48
+            );
+        }
+        $stmt = $this->db->prepare(
+            'INSERT INTO pingbacks SET'
+            . '  p_source = :source'
+            . ', p_target = :target'
+            . ', p_time = NOW()'
+            . ', p_client_ip = :ip'
+            . ', p_client_agent = :agent'
+            . ', p_client_referer = :referer'
+            . ', p_needs_review = 1'
+            . ', p_use = 1'
+            . ', p_needs_update = 1'
+        );
+        $stmt->execute(
+            array(
+                ':source'  => $source,
+                ':target'  => $target,
+                ':ip'      => isset($_SERVER['REMOTE_ADDR'])
+                    ? $_SERVER['REMOTE_ADDR'] : '',
+                ':agent'   => isset($_SERVER['HTTP_USER_AGENT'])
+                    ? $_SERVER['HTTP_USER_AGENT'] : '',
+                ':referer' => isset($_SERVER['HTTP_REFERER'])
+                    ? $_SERVER['HTTP_REFERER'] : '',
+            )
+        );
+    }
+
+    protected function alreadyExists($target, $source)
+    {
+        $res = $this->db->query(
+            'SELECT COUNT(*) as count FROM pingbacks'
+            . ' WHERE p_source = ' . $this->db->quote($source)
+            . ' AND p_target = ' . $this->db->quote($target)
+        );
+        $answer = $res->fetch(\PDO::FETCH_OBJ);
+        return $answer->count > 0;
+    }
+
+    /**
+     * Verifies that a link from $source to $target exists.
+     *
+     * @param string $target     Target URI that should be linked in $source
+     * @param string $source     Pingback source URI that should link to target
+     * @param string $sourceBody Content of $source URI
+     * @param object $res        HTTP response from fetching $source
+     *
+     * @return boolean True if $source links to $target
+     *
+     * @throws Exception When something fatally fails
+     */
+    public function verifyLinkExists(
+        $target, $source, $sourceBody, \HTTP_Request2_Response $res
+    ) {
+        return false;
+    }
+}
+?>
diff --git a/src/stapibas/Pingback/Mailer.php b/src/stapibas/Pingback/Mailer.php
new file mode 100644 (file)
index 0000000..3b9e3ff
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace stapibas;
+
+class Pingback_Mailer
+    implements \PEAR2\Services\Pingback\Server\Callback\IStorage
+{
+    public function storePingback(
+        $target, $source, $sourceBody, \HTTP_Request2_Response $res
+    ) {
+        mail(
+            'cweiske@cweiske.de',
+            'New pingback',
+            "A pingback just came in, for\n"
+            . '> '  . $target . "\n"
+            . "from\n"
+            . '> ' . $source . "\n"
+            . "\n\nLove, stapibas",
+            "From: stapibas <server@cweiske.de>"
+        );
+    }
+}
+?>
index 9221e2b76d286f4918d6bae80d0deb8260fe0e45..2a2c67477e3af1da210ed99c7b222b5251c169dd 100644 (file)
@@ -1,83 +1,19 @@
 <?php
 <?php
+namespace stapibas;
 /**
  * Simply stores all pingbacks in the database.
  */
 /**
  * Simply stores all pingbacks in the database.
  */
+header('HTTP/1.0 500 Internal Server error');
+header('Content-type: text/plain');
+
 require_once __DIR__ . '/../data/config.php';
 require_once 'stapibas/autoloader.php';
 
 $db = new PDO($dbdsn, $dbuser, $dbpass);
 require_once __DIR__ . '/../data/config.php';
 require_once 'stapibas/autoloader.php';
 
 $db = new PDO($dbdsn, $dbuser, $dbpass);
-
-class PingbackStorage
-    implements \PEAR2\Services\Pingback\Server\Callback\IStorage,
-    \PEAR2\Services\Pingback\Server\Callback\ILink
-{
-    public function __construct(PDO $db)
-    {
-        $this->db = $db;
-    }
-
-    public function storePingback(
-        $target, $source, $sourceBody, \HTTP_Request2_Response $res
-    ) {
-        $stmt = $this->db->prepare(
-            'INSERT INTO pingbacks'
-            . ' (p_source, p_target, p_time, p_client_ip, p_client_agent, p_client_referer)'
-            . ' VALUES(:source, :target, NOW(), :ip, :agent, :referer)'
-        );
-        $stmt->execute(
-            array(
-                ':source'  => $source,
-                ':target'  => $target,
-                ':ip'      => isset($_SERVER['REMOTE_ADDR'])
-                    ? $_SERVER['REMOTE_ADDR'] : '',
-                ':agent'   => isset($_SERVER['HTTP_USER_AGENT'])
-                    ? $_SERVER['HTTP_USER_AGENT'] : '',
-                ':referer' => isset($_SERVER['HTTP_REFERER'])
-                    ? $_SERVER['HTTP_REFERER'] : '',
-            )
-        );
-    }
-
-    /**
-     * Verifies that a link from $source to $target exists.
-     *
-     * @param string $target     Target URI that should be linked in $source
-     * @param string $source     Pingback source URI that should link to target
-     * @param string $sourceBody Content of $source URI
-     * @param object $res        HTTP response from fetching $source
-     *
-     * @return boolean True if $source links to $target
-     *
-     * @throws Exception When something fatally fails
-     */
-    public function verifyLinkExists(
-        $target, $source, $sourceBody, \HTTP_Request2_Response $res
-    ) {
-        return false;
-    }
-}
-
-class PingbackMailer
-    implements \PEAR2\Services\Pingback\Server\Callback\IStorage
-{
-    public function storePingback(
-        $target, $source, $sourceBody, \HTTP_Request2_Response $res
-    ) {
-        mail(
-            'cweiske@cweiske.de',
-            'New pingback',
-            "A pingback just came in, for\n"
-            . '> '  . $target . "\n"
-            . "from\n"
-            . '> ' . $source . "\n"
-            . "\n\nLove, stapibas",
-            "From: stapibas <server@cweiske.de>"
-        );
-    }
-}
+$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
 $s = new \PEAR2\Services\Pingback\Server();
 
 $s = new \PEAR2\Services\Pingback\Server();
-$s->addCallback(new PingbackStorage($db));
-$s->addCallback(new PingbackMailer());
+$s->addCallback(new Pingback_DbStorage($db));
+$s->addCallback(new Pingback_Mailer());
 $s->run();
 ?>
 $s->run();
 ?>