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