From ca522c29a1cc10f665130d21d45ede99ddaa8ac7 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 8 Aug 2016 14:45:57 +0200 Subject: [PATCH] send linkback when a post is created --- data/schema.sql | 1 + src/anoweco/Linkback.php | 57 ++++++++++++++++++++++++++++++++++++++++ src/anoweco/Storage.php | 22 +++++++++++----- www/comment.php | 2 +- www/micropub.php | 2 ++ 5 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 src/anoweco/Linkback.php diff --git a/data/schema.sql b/data/schema.sql index 2861787..e69c150 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -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 index 0000000..800a06a --- /dev/null +++ b/src/anoweco/Linkback.php @@ -0,0 +1,57 @@ +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); + } +} +?> diff --git a/src/anoweco/Storage.php b/src/anoweco/Storage.php index f3b18ae..4508f10 100644 --- a/src/anoweco/Storage.php +++ b/src/anoweco/Storage.php @@ -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)); + } } ?> diff --git a/www/comment.php b/www/comment.php index b1fe2d7..43dbd2e 100644 --- a/www/comment.php +++ b/www/comment.php @@ -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'); diff --git a/www/micropub.php b/www/micropub.php index 0dd476a..e028782 100644 --- a/www/micropub.php +++ b/www/micropub.php @@ -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))); -- 2.30.2