authentication works
[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         if (isset($json->properties->{'in-reply-to'})) {
35             $ofUrl = reset($json->properties->{'in-reply-to'});
36         }
37         $stmt->execute(
38             array(
39                 ':userId' => $userId,
40                 ':ofUrl'  => $ofUrl,
41                 ':type'   => reset($json->type),
42                 ':json'   => json_encode($json),
43             )
44         );
45         return $this->db->lastInsertId();
46     }
47
48     /**
49      * @return null|object NULL if not found, comment object otherwise
50      */
51     public function getComment($id)
52     {
53         $stmt = $this->db->prepare(
54             'SELECT * FROM comments WHERE comment_id = ?'
55         );
56         $stmt->execute([$id]);
57         $row = $stmt->fetchObject();
58
59         if ($row === false) {
60             return null;
61         }
62
63         $json = json_decode($row->comment_json);
64         $json->Xrow = $row;
65         //FIXME: load user
66
67         $stmt = $this->db->prepare(
68             'SELECT * FROM users WHERE user_id = ?'
69         );
70         $stmt->execute([$row->comment_user_id]);
71         $rowUser = $stmt->fetchObject();
72         if ($rowUser === false) {
73             $rowUser = (object) array(
74                 'user_id'   => 0,
75                 'user_name' => 'Anonymous',
76                 'user_imageurl' => '',
77             );
78         }
79
80         $json->user = $rowUser;
81         return $json;
82     }
83
84     /**
85      * @return null|object NULL if not found, user database row otherwise
86      */
87     public function getUser($id)
88     {
89         $stmt = $this->db->prepare(
90             'SELECT * FROM users WHERE user_id = ?'
91         );
92         $stmt->execute([$id]);
93         $row = $stmt->fetchObject();
94
95         if ($row === false) {
96             return null;
97         }
98         return $row;
99     }
100
101     public function findUser($name, $imageurl)
102     {
103         $stmt = $this->db->prepare(
104             'SELECT user_id FROM users'
105             . ' WHERE user_name = ? AND user_imageurl = ?'
106         );
107         $stmt->execute([$name, $imageurl]);
108         $row = $stmt->fetchObject();
109
110         if ($row === false) {
111             return null;
112         }
113         return $row->user_id;
114     }
115
116     public function createUser($name, $imageurl)
117     {
118         $stmt = $this->db->prepare(
119             'INSERT INTO users SET'
120             . '  user_name = :name'
121             . ', user_imageurl = :imageurl'
122         );
123         $stmt->execute(
124             array(
125                 ':name'     => $name,
126                 ':imageurl' => $imageurl,
127             )
128         );
129         return $this->db->lastInsertId();
130     }
131 }
132 ?>