-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
124 lines (108 loc) · 3.41 KB
/
db.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
<?php
if (!defined("PHORUM")) return;
// The table name for storing authentication associations.
$GLOBALS["PHORUM"]["socialauth_table"] =
"{$GLOBALS["PHORUM"]["DBCONFIG"]["table_prefix"]}_user_socialauth";
/**
* Retrieve the information for an authentication id association.
*
* @param string $auth_id
* The authentication id for which to retrieve information.
* @return NULL|array
* An array containing data for the OpenID (fields are user_id,
* add_datetime and openid) or NULL if the OpenID is not available.
*/
function socialauth_db_get($auth_id)
{
global $PHORUM;
$auth_id = phorum_db_interact(DB_RETURN_QUOTED, $auth_id);
return phorum_db_interact(
DB_RETURN_ASSOC,
"SELECT *
FROM {$PHORUM['socialauth_table']}
WHERE auth_id = '$auth_id'"
);
}
/**
* Retrieve the authentication id's that are associated to a given user.
*
* @param integer $user_id
* The id of the user for which to retrieve the authentication associations.
* @return array
* An array containing data record for the authentication associations.
* Each data record is an array that contains the fields user_id,
* add_datetime and auth_id.
*/
function socialauth_get_for_user($user_id)
{
global $PHORUM;
settype($user_id, 'int');
return phorum_db_interact(
DB_RETURN_ASSOCS,
"SELECT *
FROM {$PHORUM['socialauth_table']}
WHERE user_id = $user_id"
);
}
/**
* Associate an authentication id with a user.
*
* @param string $auth_id
* The authentication id to associate with a user.
* @param integer $user_id
* The id of the user to associate the OpenID with.
*/
function socialauth_db_associate($auth_id, $user_id)
{
global $PHORUM;
settype($user_id, 'int');
// Check if the user exists.
$user = phorum_db_user_get($user_id);
if ($user === NULL) trigger_error(
"socialauth_db_associate(): " .
"cannot associate $auth_id with user " .
"id $user_id: no such user", E_USER_ERROR
);
// Check if the association already exists.
$assoc = socialauth_db_get($auth_id);
if ($assoc !== NULL) {
if ($assoc['user_id'] === $user_id) {
return; // already set to the correct user_id
} else trigger_error(
"socialauth_db_associate(): " .
"cannot associate $auth_id with user " .
"id $user_id: the OpenID is already associated with user " .
"id {$assoc['user_id']}", E_USER_ERROR
);
}
// All is ok. Create the association.
$auth_id = phorum_db_interact(DB_RETURN_QUOTED, $auth_id);
phorum_db_interact(
DB_RETURN_RES,
"INSERT INTO {$PHORUM['socialauth_table']}
(auth_id, user_id, add_datetime)
VALUES ('$auth_id', $user_id, " . time() . ")"
);
}
/**
* Disassociate an authentication id from a user.
*
* @param string $auth_id
* The authentication id to disassociate from a user.
* @param integer $user_id
* The id of the user to disassociate the authentication id from.
*/
function socialauth_db_disassociate($auth_id, $user_id)
{
global $PHORUM;
$auth_id = phorum_db_interact(DB_RETURN_QUOTED, $auth_id);
settype($user_id, 'int');
phorum_db_interact(
DB_RETURN_RES,
"DELETE FROM {$PHORUM['socialauth_table']}
WHERE auth_id = '$auth_id'
AND
user_id = $user_id"
);
}
?>