This repository was archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.inc.php
189 lines (150 loc) · 4.18 KB
/
user.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
Home-Finder
Copyright (C) 2013-2018
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (__FILE__ == $_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']){
die("Direct access forbidden");
}
require_once("utils.php");
require_once("consts.php");
class HFUser {
private $userId;
private $basic_info;
private $isLoggedIn;
public function __construct ($facebook)
{
$this->user_id = $facebook->getUser();
if ($this->user_id) {
try {
// Fetch the viewer's basic information
//$basic = $facebook->api('/me');
$this->basic_info = $facebook->api('/'.$this->user_id.'?access_token='.$facebook->getAccessToken());
$this->isLoggedIn = true;
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
$result = $e->getResult();
//var_dump($result);
$this->isLoggedIn = false;
}
}
}
}
function isLoggedIn()
{
return $this->isLoggedIn;
}
private function getBasicData($field)
{
if (!$this->isLoggedIn)
{
throw new Exception( 'User not logged in!');
}
if ( !array_key_exists($field, $this->basic_info) )
{
throw new Exception( 'Missing field: ' . $field . '!');
}
return $this->basic_info[$field];
}
function getId()
{
return $this->getBasicData("id");
}
function getLastVisit($city, $jstype)
{
if (!$this->isLoggedIn)
{
throw new Exception( 'User not logged in!');
}
$city_id= get_array_index(unserialize(CITY_KEY_IDS), $city);
if ( (empty($city)) || ($city_id < 0) )
{
throw new Exception( 'Invalid argument: City!');
}
$jstype_id = get_array_index(unserialize(JS_TYPES), $jstype);
if ( (empty($jstype)) || ($jstype_id < 0) )
{
throw new Exception( 'Invalid argument: JSType!');
}
$db = new db_link();
if (!$db)
{
throw new Exception( 'DB Error 1!');
}
if (!$db->prepare(QUERY_GET_LAST_VISIT, "SELECT last_visit FROM lastvisit2 WHERE user_id = $1 AND city = $2 AND jstype = $3"))
{
throw new Exception( 'DB Error 2!');
}
$lastvisit_result = $db->execute(QUERY_GET_LAST_VISIT, array($this->getId(), $city_id, $jstype_id));
if (!$lastvisit_result)
{
throw new Exception( 'DB Error 3!');
}
if ($db->num_rows($lastvisit_result) != 1)
{
throw new Exception( 'DB Error 4!');
}
if ($lastvisit_row = $db->fetch_assoc($lastvisit_result))
{
return $lastvisit_row['last_visit'];
}
else
{
throw new Exception( 'DB Error 5!');
}
}
function setLastVisit($lastVisit, $city, $jstype)
{
if (!$this->isLoggedIn)
{
throw new Exception( 'User not logged in!');
}
if ( (!is_numeric($lastVisit)) || ($lastVisit <= 0) )
{
throw new Exception( 'Invalid argument: LastVisit!');
}
$city_id= get_array_index(unserialize(CITY_KEY_IDS), $city);
if ( (empty($city)) || ($city_id < 0) )
{
throw new Exception( 'Invalid argument: City!');
}
$jstype_id = get_array_index(unserialize(JS_TYPES), $jstype);
if ( (empty($jstype)) || ($jstype_id < 0) )
{
throw new Exception( 'Invalid argument: JSType!');
}
$db = new db_link();
if (!$db)
{
throw new Exception( 'DB Error 1!');
}
if (!$db->prepare(QUERY_UPDATE_LAST_VISIT, "SELECT updateLastVisit2($1, $2, $3, $4)"))
{
throw new Exception( 'DB Error 2!');
}
$lastvisit_result = $db->execute(QUERY_UPDATE_LAST_VISIT, array($this->getId(),
$city_id,
$jstype_id,
$lastVisit));
if (!$lastvisit_result)
{
throw new Exception( 'DB Error 3!');
}
}
function __destruct()
{
}
}
?>