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 2861787e2e911f9d3d9cdbb7e03b8c8cb7f49c26..e69c15080e330f0db436cfe2e053aa1d71055410 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_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;
 
   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 f3b18ae3a1da275864dcd9cdf29f3d0f9b931230..4508f1017713da520a90903453c64285ae6f24d0 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 = ?'
     {
         $stmt = $this->db->prepare(
             'SELECT * FROM comments WHERE comment_id = ?'
@@ -75,15 +77,13 @@ class Storage
         $json->Xrow = $row;
         //FIXME: load user
 
         $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(
         $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' => '',
             );
         }
                 'user_imageurl' => '',
             );
         }
@@ -139,5 +139,13 @@ class Storage
         );
         return $this->db->lastInsertId();
     }
         );
         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 b1fe2d762e601b40df33ab5a0c50a3eab53591ff..43dbd2eb76495eea0f7eb4147f46d145da8f5d8c 100644 (file)
@@ -18,7 +18,7 @@ if (!is_numeric($_GET['id'])) {
 $id = intval($_GET['id']);
 
 $storage = new Storage();
 $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');
 if ($comment === null) {
     header('HTTP/1.0 404 Not Found');
     header('Content-Type: text/plain');
index 0dd476a17f877b41bc9975471addeb8d1f633f50..e028782695fa5ea686b464fa2f125763eb620984 100644 (file)
@@ -83,8 +83,10 @@ function handleCreate($json, $token)
     }
 
     $storage = new Storage();
     }
 
     $storage = new Storage();
+    $lb      = new Linkback();
     try {
         $id = $storage->addComment($json, $userId);
     try {
         $id = $storage->addComment($json, $userId);
+        $lb->ping($id);
 
         header('HTTP/1.0 201 Created');
         header('Location: ' . Urls::full(Urls::comment($id)));
 
         header('HTTP/1.0 201 Created');
         header('Location: ' . Urls::full(Urls::comment($id)));