6 public function __construct()
8 require __DIR__ . '/../../data/config.php';
9 $this->db = new \PDO($dbdsn, $dbuser, $dbpass);
10 $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
14 * Store a new comment into the database
16 * @param object $json Micropub create JSON
17 * @param integer $userId ID of the user whom this comment belongs
19 * @return integer Comment ID
22 public function addComment($json, $userId)
24 $stmt = $this->db->prepare(
25 'INSERT INTO comments SET'
26 . ' comment_user_id = :userId'
27 . ', comment_published = NOW()'
28 . ', comment_of_url = :ofUrl'
29 . ', comment_type = :type'
30 . ', comment_json = :json'
35 if (isset($json->properties->{'in-reply-to'})) {
36 $ofUrl = reset($json->properties->{'in-reply-to'});
38 } else if (isset($json->properties->{'like-of'})) {
39 $ofUrl = reset($json->properties->{'like-of'});
43 'Invalid post type, only reply and like allowed',
53 ':json' => json_encode($json),
56 return $this->db->lastInsertId();
60 * @return null|object NULL if not found, comment object otherwise
62 public function getComment($id)
64 $stmt = $this->db->prepare(
65 'SELECT * FROM comments WHERE comment_id = ?'
67 $stmt->execute([$id]);
68 $row = $stmt->fetchObject();
74 $json = json_decode($row->comment_json);
78 $stmt = $this->db->prepare(
79 'SELECT * FROM users WHERE user_id = ?'
81 $stmt->execute([$row->comment_user_id]);
82 $rowUser = $stmt->fetchObject();
83 if ($rowUser === false) {
84 $rowUser = (object) array(
86 'user_name' => 'Anonymous',
87 'user_imageurl' => '',
91 $json->user = $rowUser;
96 * @return null|object NULL if not found, user database row otherwise
98 public function getUser($id)
100 $stmt = $this->db->prepare(
101 'SELECT * FROM users WHERE user_id = ?'
103 $stmt->execute([$id]);
104 $row = $stmt->fetchObject();
106 if ($row === false) {
112 public function findUser($name, $imageurl)
114 $stmt = $this->db->prepare(
115 'SELECT user_id FROM users'
116 . ' WHERE user_name = ? AND user_imageurl = ?'
118 $stmt->execute([$name, $imageurl]);
119 $row = $stmt->fetchObject();
121 if ($row === false) {
124 return $row->user_id;
127 public function createUser($name, $imageurl)
129 $stmt = $this->db->prepare(
130 'INSERT INTO users SET'
131 . ' user_name = :name'
132 . ', user_imageurl = :imageurl'
137 ':imageurl' => $imageurl,
140 return $this->db->lastInsertId();