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