f3b18ae3a1da275864dcd9cdf29f3d0f9b931230
[anoweco.git] / src / anoweco / Storage.php
1 <?php
2 namespace anoweco;
3
4 class Storage
5 {
6     public function __construct()
7     {
8         require __DIR__ . '/../../data/config.php';
9         $this->db = new \PDO($dbdsn, $dbuser, $dbpass);
10         $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
11     }
12
13     /**
14      * Store a new comment into the database
15      *
16      * @param object  $json   Micropub create JSON
17      * @param integer $userId ID of the user whom this comment belongs
18      *
19      * @return integer Comment ID
20      * @throws \Exception
21      */
22     public function addComment($json, $userId)
23     {
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'
31         );
32
33         $ofUrl = '';
34         $type  = null;
35         if (isset($json->properties->{'in-reply-to'})) {
36             $ofUrl = reset($json->properties->{'in-reply-to'});
37             $type = 'reply';
38         } else if (isset($json->properties->{'like-of'})) {
39             $ofUrl = reset($json->properties->{'like-of'});
40             $type  = 'like';
41         } else {
42             throw new \Exception(
43                 'Invalid post type, only reply and like allowed',
44                 400
45             );
46         }
47
48         $stmt->execute(
49             array(
50                 ':userId' => $userId,
51                 ':ofUrl'  => $ofUrl,
52                 ':type'   => $type,
53                 ':json'   => json_encode($json),
54             )
55         );
56         return $this->db->lastInsertId();
57     }
58
59     /**
60      * @return null|object NULL if not found, comment object otherwise
61      */
62     public function getComment($id)
63     {
64         $stmt = $this->db->prepare(
65             'SELECT * FROM comments WHERE comment_id = ?'
66         );
67         $stmt->execute([$id]);
68         $row = $stmt->fetchObject();
69
70         if ($row === false) {
71             return null;
72         }
73
74         $json = json_decode($row->comment_json);
75         $json->Xrow = $row;
76         //FIXME: load user
77
78         $stmt = $this->db->prepare(
79             'SELECT * FROM users WHERE user_id = ?'
80         );
81         $stmt->execute([$row->comment_user_id]);
82         $rowUser = $stmt->fetchObject();
83         if ($rowUser === false) {
84             $rowUser = (object) array(
85                 'user_id'   => 0,
86                 'user_name' => 'Anonymous',
87                 'user_imageurl' => '',
88             );
89         }
90
91         $json->user = $rowUser;
92         return $json;
93     }
94
95     /**
96      * @return null|object NULL if not found, user database row otherwise
97      */
98     public function getUser($id)
99     {
100         $stmt = $this->db->prepare(
101             'SELECT * FROM users WHERE user_id = ?'
102         );
103         $stmt->execute([$id]);
104         $row = $stmt->fetchObject();
105
106         if ($row === false) {
107             return null;
108         }
109         return $row;
110     }
111
112     public function findUser($name, $imageurl)
113     {
114         $stmt = $this->db->prepare(
115             'SELECT user_id FROM users'
116             . ' WHERE user_name = ? AND user_imageurl = ?'
117         );
118         $stmt->execute([$name, $imageurl]);
119         $row = $stmt->fetchObject();
120
121         if ($row === false) {
122             return null;
123         }
124         return $row->user_id;
125     }
126
127     public function createUser($name, $imageurl)
128     {
129         $stmt = $this->db->prepare(
130             'INSERT INTO users SET'
131             . '  user_name = :name'
132             . ', user_imageurl = :imageurl'
133         );
134         $stmt->execute(
135             array(
136                 ':name'     => $name,
137                 ':imageurl' => $imageurl,
138             )
139         );
140         return $this->db->lastInsertId();
141     }
142 }
143 ?>