forked from laishzh/OEMS
/
getUIDByCookie.cpp
64 lines (49 loc) · 1.2 KB
/
getUIDByCookie.cpp
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
/**
* @file getCIDByCookie.cpp
* @brief
* @author MoShuqi
* @version 1.0
* @date 2012-04-12
*/
#include "getUIDByCookie.h"
uid_t getUIDByCookie(const string &cookie, int &err, PGconn *dbconn)
{
err = PC_UNKNOWNERROR;
PGconn *conn = dbconn;
uid_t userID;
if (PQstatus(conn) != CONNECTION_OK)
{
err = PC_DBERROR;
return userID;
}
if (userID.size() > MAXLEN_COOKIESIZE)
{
err = PC_INPUTFORMATERROR;
return userID;
}
//escapte string of cookie
char ccook[2 * cookie.size() + 1];
PQescapeString(ccook, cookie.c_str(), cookie.size());
string t = ccook;
string sql = "SELECT user_id FROM users WHERE cookie ='"+t+"'";
//Exec the SQL query
PGresult *res = NULL;
res = PQexec(conn, sql.c_str());
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{//If exection failed
err = PC_DBERROR;
PQclear(res);
return userID;
}
if (PQntuples(res) == 0)
{//If userID doesn't match password
err = PC_MISTATCH;
PQclear(res);
return userID;
}
userID = PQgetvalue(res, 0, 0);
//Updated by: Lai
err = PC_SUCCESSFUL;
PQclear(res);
return userID;
}