Facebook to email

Facebook to email Git Source Tree

Root/database.php

1<?php
2/**
3 * Copyright 2014 Grégory Soutadé
4 *
5 * This file is part of f2email.
6 *
7 * f2email is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * f2email is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with f2email. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21class Database
22{
23 private $db;
24
25 function __construct($address, $user, $password, $database)
26 {
27 $this->db = mysqli_connect($address, $user, $password, $database);
28
29 if (mysqli_connect_errno($this->db))
30 {
31 die("Error connecting to MySQL " . mysqli_connect_error());
32 }
33
34 return true;
35 }
36
37 function escape($s) { return $this->db->real_escape_string($s); }
38
39 function get_error() { return mysqli_error($this->db); }
40
41 function insert_user_or_update_token($user, $access_token)
42 {
43 $result = $this->db->query("SELECT * FROM fb_user WHERE id='" . $user["id"] . "' LIMIT 1");
44 $ret ;
45
46 if ($result && $result->num_rows == 1) {
47 // Update
48 $ret = $this->db->query("UPDATE fb_user SET access_token='$access_token', token_expired=false WHERE id=" . $user["id"]);
49 }
50 else
51 {
52 // Insert
53 $ret = $this->db->query("INSERT INTO fb_user (id, email, access_token, token_expired) VALUES (" . $user["id"] . ",'" . $user["email"] . "','" . $access_token . "', 0)");
54 }
55
56 return $ret;
57 }
58
59 function disable_user($user)
60 {
61 $ret = $this->db->query("UPDATE fb_user SET token_expired=true, expiration_date=DATE_ADD(now( ) , INTERVAL +1 MONTH) WHERE id=" . $user["id"]);
62 return $ret;
63 }
64
65 function remove_user($user)
66 {
67 $this->db->query("DELETE FROM module_user_per_fb_id_pref WHERE user_id=" . $user["id"]);
68 $this->db->query("DELETE FROM module_user_per_fb_id WHERE user_id=" . $user["id"]);
69 $this->db->query("DELETE FROM module_user_global_pref WHERE user_id=" . $user["id"]);
70 $this->db->query("DELETE FROM custom_fb_id WHERE user_id=" . $user["id"]);
71 $this->db->query("DELETE FROM fb_last_fetch WHERE user_id=" . $user["id"]);
72 $this->db->query("DELETE FROM fb_user WHERE id=" . $user["id"]);
73
74 return true;
75 }
76
77 function remove_expired_users()
78 {
79 $result = $this->db->query("SELECT * FROM fb_user WHERE token_expired=1 AND expiration_date < NOW()");
80
81 if ($result)
82 {
83 while ($user = $result->fetch_assoc())
84 {
85 $this->remove_user($user);
86 }
87 }
88 }
89
90 function insert_or_load_module($module, $user)
91 {
92 $result = $this->db->query("SELECT id,name FROM module WHERE id=" . $module->getID());
93 $ret = true;
94
95 if ($result && $result->num_rows > 0) {
96 $row = $result->fetch_assoc();
97 // Update
98 if ($row["name"] != $module->getName())
99 {
100 die("ID conflict with module " . $module->getName() . " and " . $row["name"]);
101 }
102
103 $result = $this->db->query("SELECT value FROM module_user_global_pref WHERE user_id=" . $user["id"] . " AND module_id=" . $module->getID());
104
105 if ($result->num_rows == 1) {
106 $row = $result->fetch_assoc();
107 $module->setGlobalPref($row["value"], false);
108 }
109 }
110 else
111 {
112 // Insert
113 $ret = $this->db->query("INSERT INTO module (id, name, has_global_pref, has_local_pref) VALUES (" . $module->getID() . ", '" . $module->getName() . "'," . $module->hasGlobalPref() . "," . $module->hasLocalPref() .")");
114 }
115
116 return $ret;
117 }
118
119 function set_module_local_pref($user, $fb_object_id, $module_id, $value)
120 {
121 $fb_object_id = $this->db->real_escape_string($fb_object_id);
122 $module_id = $this->db->real_escape_string($module_id);
123 $value = $this->db->real_escape_string($value);
124
125 if ($value == "")
126 {
127 $this->db->query("DELETE FROM module_user_per_fb_id_pref WHERE user_id=" . $user["id"] . " AND fb_object_id=" . $fb_object_id . " AND module_id=" . $module_id);
128 return "REPLACE INTO module_user_per_fb_id_pref (user_id, fb_object_id, module_id, value) VALUES (" . $user["id"] . ", " . $fb_object_id . ", " . $module_id . ", '" . $value . "')";
129 }
130 else
131 {
132 $this->db->query("REPLACE INTO module_user_per_fb_id_pref (user_id, fb_object_id, module_id, value) VALUES (" . $user["id"] . ", " . $fb_object_id . ", " . $module_id . ", '" . $value . "')");
133 return "REPLACE INTO module_user_per_fb_id_pref (user_id, fb_object_id, module_id, value) VALUES (" . $user["id"] . ", " . $fb_object_id . ", " . $module_id . ", '" . $value . "')";
134 }
135
136 }
137
138 function set_module_global_pref($user, $module_id, $value)
139 {
140 $module_id = $this->db->real_escape_string($module_id);
141 $value = $this->db->real_escape_string($value);
142
143 if ($value == "")
144 return $this->db->query("DELETE FROM module_user_global_pref WHERE user_id=" . $user["id"] . " AND module_id=" . $module_id);
145 else
146 return $this->db->query("REPLACE INTO module_user_global_pref (user_id, module_id, value) VALUES (" . $user["id"] . ", " . $module_id . ", '" . $value . "')");
147 }
148
149 function get_module_global_pref($module, $user)
150 {
151 $result = $this->db->query("SELECT value FROM module_user_global_pref WHERE user_id=" . $user["id"] . " AND module_id=" . $module->getID());
152
153 if ($result)
154 {
155 $row = $result->fetch_assoc();
156 return $row["value"];
157 }
158 else
159 return "";
160 }
161
162 function get_module_local_pref($module, $user)
163 {
164 $result = $this->db->query("SELECT fb_object_id, value FROM module_user_per_fb_id_pref WHERE user_id=" . $user["id"] ." AND module_id=" . $module->getID());
165
166 if ($result)
167 {
168 while ($row = $result->fetch_assoc())
169 {
170 $module->setLocalPref($row["value"], $row["fb_object_id"], false);
171 }
172 }
173 }
174
175 function get_module_per_fb_id($user)
176 {
177 $ret = array();
178
179 $result = $this->db->query("SELECT fb_object_id, module_id FROM module_user_per_fb_id WHERE user_id=" . $user["id"]);
180
181 if ($result)
182 {
183 while ($row = $result->fetch_assoc())
184 {
185 $ret[$row["fb_object_id"]] = $row["module_id"];
186 }
187 }
188
189 return $ret;
190 }
191
192 function get_users($user_id)
193 {
194 // All users
195 if ($user_id == 0)
196 $result = $this->db->query("SELECT * FROM fb_user");
197 else
198 $result = $this->db->query("SELECT * FROM fb_user WHERE id=" . $user_id);
199
200 return $result;
201 }
202
203 function get_last_fetch($user, $fb_object_id)
204 {
205 $fb_object_id = $this->db->real_escape_string($fb_object_id);
206
207 $result = $this->db->query("SELECT ids FROM fb_last_fetch WHERE user_id=" . $user["id"] . " AND fb_object_id=" . $fb_object_id);
208
209 if ($result && $result->num_rows == 1)
210 {
211 $row = $result->fetch_assoc();
212 return explode(",", $row["ids"]) ;
213 }
214 else
215 return array();
216 }
217
218 function set_last_fetch($user, $fb_object_id, $value)
219 {
220 return $this->db->query("REPLACE INTO fb_last_fetch (user_id, fb_object_id, ids) VALUES (" . $user["id"] . ", " . $fb_object_id . ", '" . join(",", $value) . "')");
221 }
222
223 function link_fb_object($user, $fb_object_id, $value)
224 {
225 $fb_object_id = $this->db->real_escape_string($fb_object_id);
226 $value = $this->db->real_escape_string($value);
227
228 if ($value == -1)
229 return $this->db->query("DELETE FROM module_user_per_fb_id WHERE user_id=" . $user["id"] . " AND fb_object_id=" . $fb_object_id);
230 else
231 return $this->db->query("REPLACE INTO module_user_per_fb_id (user_id, fb_object_id, module_id) VALUES (" . $user["id"] . ", " . $fb_object_id . ", " . $value . ")");
232 }
233
234 function get_module_for_fb_object($user, $fb_object_id)
235 {
236 $fb_object_id = $this->db->real_escape_string($fb_object_id);
237
238 $result = $this->db->query("SELECT module_id FROM module_user_per_fb_id WHERE user_id=" . $user["id"] . " AND fb_object_id=" . $fb_object_id);
239
240 if ($result && $result->num_rows == 1)
241 {
242 $row = $result->fetch_assoc();
243 return $row["module_id"] ;
244 }
245 else
246 return -1;
247 }
248
249 function get_customs($user_id)
250 {
251 $ret = array();
252
253 $result = $this->db->query("SELECT fb_object_id FROM custom_fb_id WHERE user_id=" . $user_id);
254
255 if ($result)
256 {
257 while ($row = $result->fetch_assoc())
258 {
259 array_push($ret, $row["fb_object_id"]);
260 }
261 }
262
263 return $ret;
264 }
265
266 function add_custom($user_id, $fb_object_id)
267 {
268 $result = $this->db->query("INSERT INTO custom_fb_id (user_id, fb_object_id) VALUES (" . $user_id . ", " . $fb_object_id . ")");
269
270 return $result;
271 }
272
273 function remove_custom($user_id, $fb_object_id)
274 {
275 $this->db->query("DELETE FROM fb_last_fetch WHERE user_id=" . $user_id . " AND fb_object_id=" . $fb_object_id);
276
277 $this->db->query("DELETE FROM module_user_per_fb_id_pref WHERE user_id=" . $user_id . " AND fb_object_id=" . $fb_object_id);
278
279 $this->db->query("DELETE FROM custom_fb_id WHERE user_id=" . $user_id . " AND fb_object_id=" . $fb_object_id);
280
281 return true;
282 }
283}
284?>

Archive Download this file

Branches