version 0.1.11
[fms.git] / src / http / pages / announceidentitypage.cpp
1 #include "../../../include/http/pages/announceidentitypage.h"\r
2 #include "../../../include/stringfunctions.h"\r
3 #include "../../../include/datetime.h"\r
4 \r
5 #ifdef XMEM\r
6         #include <xmem.h>\r
7 #endif\r
8 \r
9 const std::string AnnounceIdentityPage::CreateLocalIdentityDropDown(const std::string &name, const std::string &selected)\r
10 {\r
11         std::string rval="";\r
12         SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID, Name, PublicKey FROM tblLocalIdentity ORDER BY Name;");\r
13         st.Step();\r
14 \r
15         rval+="<select name=\""+name+"\">";\r
16         while(st.RowReturned())\r
17         {\r
18                 std::string id;\r
19                 std::string name;\r
20                 std::string pubkey;\r
21                 std::string keypart="";\r
22 \r
23                 st.ResultText(0,id);\r
24                 st.ResultText(1,name);\r
25                 st.ResultText(2,pubkey);\r
26 \r
27                 if(pubkey.size()>8)\r
28                 {\r
29                         keypart=pubkey.substr(3,5);\r
30                 }\r
31 \r
32                 rval+="<option value=\""+id+"\" title=\""+pubkey+"\">"+SanitizeOutput(name+keypart)+"...</option>";\r
33                 st.Step();\r
34         }\r
35         rval+="</select>";\r
36         return rval;\r
37 }\r
38 \r
39 const std::string AnnounceIdentityPage::GeneratePage(const std::string &method, const std::map<std::string,std::string> &queryvars)\r
40 {\r
41         DateTime date;\r
42         std::string content;\r
43         int shown=0;\r
44         std::string countstr="";\r
45         std::string uuid;\r
46         std::string lastid="";\r
47         std::string thisid="";\r
48         std::string day="";\r
49         int requestindex=0;\r
50         bool willshow=false;\r
51 \r
52         if(queryvars.find("formaction")!=queryvars.end() && (*queryvars.find("formaction")).second=="announce")\r
53         {\r
54                 SQLite3DB::Statement insert=m_db->Prepare("INSERT INTO tblIdentityIntroductionInserts(LocalIdentityID,Day,UUID,Solution) VALUES(?,?,?,?);");\r
55                 std::string localidentityidstr="";\r
56                 int localidentityid=0;\r
57                 std::vector<std::string> uuids;\r
58                 std::vector<std::string> days;\r
59                 std::vector<std::string> solutions;\r
60 \r
61                 if(queryvars.find("localidentityid")!=queryvars.end())\r
62                 {\r
63                         localidentityidstr=(*queryvars.find("localidentityid")).second;\r
64                         StringFunctions::Convert(localidentityidstr,localidentityid);\r
65                 }\r
66                 CreateArgArray(queryvars,"uuid",uuids);\r
67                 CreateArgArray(queryvars,"day",days);\r
68                 CreateArgArray(queryvars,"solution",solutions);\r
69 \r
70                 for(int i=0; i<solutions.size(); i++)\r
71                 {\r
72                         if(solutions[i]!="")\r
73                         {\r
74                                 insert.Bind(0,localidentityid);\r
75                                 insert.Bind(1,days[i]);\r
76                                 insert.Bind(2,uuids[i]);\r
77                                 insert.Bind(3,solutions[i]);\r
78                                 insert.Step();\r
79                                 insert.Reset();\r
80                         }\r
81                 }\r
82 \r
83         }\r
84 \r
85         content+="<h2>Announce Identity</h2>";\r
86         content+="<form name=\"frmannounce\" method=\"POST\">";\r
87         content+="<input type=\"hidden\" name=\"formaction\" value=\"announce\">";\r
88         content+="<table>";\r
89         content+="<tr><td colspan=\"4\"><center>Select Identity : ";\r
90         content+=CreateLocalIdentityDropDown("localidentityid","");\r
91         content+="</td></tr>";\r
92         content+="<tr><td colspan=\"4\"><center>Type the answers of a few puzzles.  The puzzles are case sensitive.</td></tr>";\r
93         content+="<tr>";\r
94 \r
95 \r
96         date.SetToGMTime();\r
97         date.Add(0,0,0,-1);\r
98         SQLite3DB::Statement st=m_db->Prepare("SELECT UUID,Day,IdentityID,RequestIndex FROM tblIntroductionPuzzleRequests WHERE UUID NOT IN (SELECT UUID FROM tblIdentityIntroductionInserts) AND UUID NOT IN (SELECT UUID FROM tblIntroductionPuzzleInserts) AND Day>='"+date.Format("%Y-%m-%d")+"' AND Found='true' ORDER BY IdentityID, Day DESC, RequestIndex DESC;");\r
99         st.Step();\r
100 \r
101         if(st.RowReturned()==false)\r
102         {\r
103                 content+="<td colspan=\"4\"><center>You must wait for some puzzles to be downloaded.  Check back later.</td>";\r
104         }\r
105         \r
106         while(st.RowReturned() && shown<20)\r
107         {\r
108                 st.ResultText(0,uuid);\r
109                 st.ResultText(1,day);\r
110                 st.ResultText(2,thisid);\r
111                 st.ResultInt(3,requestindex);\r
112 \r
113                 // if we are already inserting a solution for an identity - we shouldn't show any puzzles that are older than the one we are inserting\r
114                 // get the last index # we are inserting this day from this identity\r
115                 // if the index here is greater than the index in the st statement, we will skip this puzzle because we are already inserting a puzzle with a greater index\r
116                 willshow=true;\r
117                 SQLite3DB::Statement st2=m_db->Prepare("SELECT MAX(RequestIndex) FROM tblIdentityIntroductionInserts INNER JOIN tblIntroductionPuzzleRequests ON tblIdentityIntroductionInserts.UUID=tblIntroductionPuzzleRequests.UUID WHERE tblIdentityIntroductionInserts.Day=? AND tblIdentityIntroductionInserts.UUID IN (SELECT UUID FROM tblIntroductionPuzzleRequests WHERE IdentityID=? AND Day=?) GROUP BY tblIdentityIntroductionInserts.Day;");\r
118                 st2.Step();\r
119                 if(st2.RowReturned()==true)\r
120                 {\r
121                         int index=0;\r
122                         st2.ResultInt(0,index);\r
123                         if(index>=requestindex)\r
124                         {\r
125                                 willshow=false;\r
126                         }\r
127                 }\r
128 \r
129                 if(willshow && thisid!=lastid)\r
130                 {\r
131                         StringFunctions::Convert(shown,countstr);\r
132                         if(shown>0 && shown%4==0)\r
133                         {\r
134                                 content+="</tr>\r\n<tr>";\r
135                         }\r
136                         content+="<td>";\r
137                         content+="<img src=\"showcaptcha.htm?UUID="+uuid+"\"><br>";\r
138                         content+="<input type=\"hidden\" name=\"uuid["+countstr+"]\" value=\""+uuid+"\">";\r
139                         content+="<input type=\"hidden\" name=\"day["+countstr+"]\" value=\""+day+"\">";\r
140                         content+="<input type=\"text\" name=\"solution["+countstr+"]\">";\r
141                         content+="</td>\r\n";\r
142                         lastid=thisid;\r
143                         shown++;\r
144                 }\r
145                 \r
146                 st.Step();\r
147         }\r
148 \r
149         content+="</tr><td colspan=\"4\"><center><input type=\"submit\" value=\"Announce\"></td></tr>";\r
150         content+="</table>";\r
151         content+="</form>";\r
152 \r
153         return "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"+StringFunctions::Replace(m_template,"[CONTENT]",content);\r
154 }\r
155 \r
156 const bool AnnounceIdentityPage::WillHandleURI(const std::string &uri)\r
157 {\r
158         if(uri.find("announceidentity.")!=std::string::npos)\r
159         {\r
160                 return true;\r
161         }\r
162         else\r
163         {\r
164                 return false;\r
165         }\r
166 }\r