`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;
--- /dev/null
+<?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);
+ }
+}
+?>
}
/**
- * @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 = ?'
$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' => '',
);
}
);
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));
+ }
}
?>
$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');
}
$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)));