send linkback when a post is created
authorChristian Weiske <cweiske@cweiske.de>
Mon, 8 Aug 2016 12:45:57 +0000 (14:45 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 8 Aug 2016 12:46:22 +0000 (14:46 +0200)
data/schema.sql
src/anoweco/Linkback.php [new file with mode: 0644]
src/anoweco/Storage.php
www/comment.php
www/micropub.php

index 2861787..e69c150 100644 (file)
@@ -8,6 +8,7 @@ CREATE TABLE `comments` (
   `comment_of_url` varchar(2048) NOT NULL,
   `comment_type` varchar(32) NOT NULL,
   `comment_json` mediumtext NOT NULL,
+  `comment_pingstate` varchar(6) NOT NULL,
   PRIMARY KEY (`comment_id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
diff --git a/src/anoweco/Linkback.php b/src/anoweco/Linkback.php
new file mode 100644 (file)
index 0000000..800a06a
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+namespace anoweco;
+
+class Linkback
+{
+    protected $lbc;
+
+    protected function initLbc()
+    {
+        $this->lbc = new \PEAR2\Services\Linkback\Client();
+        $req = $this->lbc->getRequest();
+        /*
+        $req->setConfig(
+            array(
+                'ssl_verify_peer' => false,
+                'ssl_verify_host' => false
+            )
+        );
+        */
+        $headers = $req->getHeaders();
+        $req->setHeader('user-agent', 'anoweco');
+        $this->lbc->setRequestTemplate($req);
+    }
+
+    public function ping($postId)
+    {
+        $this->initLbc();
+        $storage = new Storage();
+        $rowPost = $storage->getJsonComment($postId)->Xrow;
+
+        $from = Urls::full(Urls::comment($postId));
+        $to   = $rowPost->comment_of_url;
+
+        try {
+            $res = $this->lbc->send($from, $to);
+            if (!$res->isError()) {
+                //all ok
+                $error = false;
+            } else {
+                //some error
+                error_log($res->getMessage());
+                $error = true;
+            }
+        } catch (\Exception $e) {
+            error_log($e->getMessage());
+            $error = true;
+        }
+
+        if ($error) {
+            $pingState = $rowPost->comment_pingstate + 1;
+        } else {
+            $pingState = 'ok';
+        }
+        $storage->setPostPingState($postId, $pingState);
+    }
+}
+?>
index f3b18ae..4508f10 100644 (file)
@@ -57,9 +57,11 @@ class Storage
     }
 
     /**
-     * @return null|object NULL if not found, comment object otherwise
+     * @return null|object NULL if not found, JSON comment object otherwise
+     *                     - "Xrow" property contains the database row object
+     *                     - "user" property contains the user db row object
      */
-    public function getComment($id)
+    public function getJsonComment($id)
     {
         $stmt = $this->db->prepare(
             'SELECT * FROM comments WHERE comment_id = ?'
@@ -75,15 +77,13 @@ class Storage
         $json->Xrow = $row;
         //FIXME: load user
 
-        $stmt = $this->db->prepare(
-            'SELECT * FROM users WHERE user_id = ?'
-        );
+        $stmt = $this->db->prepare('SELECT * FROM users WHERE user_id = ?');
         $stmt->execute([$row->comment_user_id]);
         $rowUser = $stmt->fetchObject();
         if ($rowUser === false) {
             $rowUser = (object) array(
-                'user_id'   => 0,
-                'user_name' => 'Anonymous',
+                'user_id'       => 0,
+                'user_name'     => 'Anonymous',
                 'user_imageurl' => '',
             );
         }
@@ -139,5 +139,13 @@ class Storage
         );
         return $this->db->lastInsertId();
     }
+
+    public function setPostPingState($postId, $pingstate)
+    {
+        $stmt = $this->db->prepare(
+            'UPDATE comments SET comment_pingstate = ? WHERE comment_id = ?'
+        );
+        $stmt->execute(array($pingstate, $postId));
+    }
 }
 ?>
index b1fe2d7..43dbd2e 100644 (file)
@@ -18,7 +18,7 @@ if (!is_numeric($_GET['id'])) {
 $id = intval($_GET['id']);
 
 $storage = new Storage();
-$comment = $storage->getComment($id);
+$comment = $storage->getJsonComment($id);
 if ($comment === null) {
     header('HTTP/1.0 404 Not Found');
     header('Content-Type: text/plain');
index 0dd476a..e028782 100644 (file)
@@ -83,8 +83,10 @@ function handleCreate($json, $token)
     }
 
     $storage = new Storage();
+    $lb      = new Linkback();
     try {
         $id = $storage->addComment($json, $userId);
+        $lb->ping($id);
 
         header('HTTP/1.0 201 Created');
         header('Location: ' . Urls::full(Urls::comment($id)));