version 0.2.4
[fms.git] / src / http / pages / localidentitiespage.cpp
1 #include "../../../include/http/pages/localidentitiespage.h"\r
2 #include "../../../include/stringfunctions.h"\r
3 #include "../../../include/http/identityexportxml.h"\r
4 \r
5 #ifdef XMEM\r
6         #include <xmem.h>\r
7 #endif\r
8 \r
9 const std::string LocalIdentitiesPage::CreateTrueFalseDropDown(const std::string &name, const std::string &selected)\r
10 {\r
11         std::string rval="";\r
12 \r
13         rval+="<select name=\""+name+"\">";\r
14         rval+="<option value=\"true\"";\r
15         if(selected=="true")\r
16         {\r
17                 rval+=" SELECTED";\r
18         }\r
19         rval+=">true</option>";\r
20         rval+="<option value=\"false\"";\r
21         if(selected=="false")\r
22         {\r
23                 rval+=" SELECTED";\r
24         }\r
25         rval+=">false</option>";\r
26         rval+="</select>";\r
27 \r
28         return rval;\r
29 }\r
30 \r
31 const std::string LocalIdentitiesPage::GeneratePage(const std::string &method, const std::map<std::string,std::string> &queryvars)\r
32 {\r
33         int count;\r
34         std::string countstr;\r
35         std::string content="";\r
36 \r
37         if(queryvars.find("formaction")!=queryvars.end())\r
38         {\r
39                 int id;\r
40                 std::vector<std::string> ids;\r
41                 std::vector<std::string> singleuse;\r
42                 std::vector<std::string> publishtrustlist;\r
43                 std::vector<std::string> publishboardlist;\r
44                 std::vector<std::string> publishfreesite;\r
45 \r
46                 CreateArgArray(queryvars,"chkidentityid",ids);\r
47                 CreateArgArray(queryvars,"singleuse",singleuse);\r
48                 CreateArgArray(queryvars,"publishtrustlist",publishtrustlist);\r
49                 CreateArgArray(queryvars,"publishboardlist",publishboardlist);\r
50                 CreateArgArray(queryvars,"publishfreesite",publishfreesite);\r
51 \r
52                 if((*queryvars.find("formaction")).second=="update")\r
53                 {\r
54                         SQLite3DB::Statement update=m_db->Prepare("UPDATE tblLocalIdentity SET SingleUse=?, PublishTrustList=?, PublishBoardList=?, PublishFreesite=? WHERE LocalIdentityID=?;");\r
55                         for(int i=0; i<ids.size(); i++)\r
56                         {\r
57                                 if(ids[i]!="")\r
58                                 {\r
59                                         StringFunctions::Convert(ids[i],id);\r
60                                         update.Bind(0,singleuse[i]);\r
61                                         update.Bind(1,publishtrustlist[i]);\r
62                                         update.Bind(2,publishboardlist[i]);\r
63                                         update.Bind(3,publishfreesite[i]);\r
64                                         update.Bind(4,id);\r
65                                         update.Step();\r
66                                         update.Reset();\r
67                                 }\r
68                         }\r
69                 }\r
70                 if((*queryvars.find("formaction")).second=="delete")\r
71                 {\r
72                         SQLite3DB::Statement del=m_db->Prepare("DELETE FROM tblLocalIdentity WHERE LocalIdentityID=?;");\r
73                         for(int i=0; i<ids.size(); i++)\r
74                         {\r
75                                 if(ids[i]!="")\r
76                                 {\r
77                                         StringFunctions::Convert(ids[i],id);\r
78                                         del.Bind(0,id);\r
79                                         del.Step();\r
80                                         del.Reset();\r
81                                 }\r
82                         }\r
83                 }\r
84                 if((*queryvars.find("formaction")).second=="export")\r
85                 {\r
86                         IdentityExportXML xml;\r
87                         SQLite3DB::Statement exp=m_db->Prepare("SELECT Name,PublicKey,PrivateKey,SingleUse,PublishTrustList,PublishBoardList,PublishFreesite FROM tblLocalIdentity WHERE PublicKey IS NOT NULL AND PrivateKey IS NOT NULL;");\r
88                         exp.Step();\r
89                         while(exp.RowReturned())\r
90                         {\r
91                                 std::string name="";\r
92                                 std::string publickey="";\r
93                                 std::string privatekey="";\r
94                                 std::string tempval="";\r
95                                 bool singleuse=false;\r
96                                 bool publishtrustlist=false;\r
97                                 bool publishboardlist=false;\r
98                                 bool publishfreesite=false;\r
99 \r
100                                 exp.ResultText(0,name);\r
101                                 exp.ResultText(1,publickey);\r
102                                 exp.ResultText(2,privatekey);\r
103                                 exp.ResultText(3,tempval);\r
104                                 if(tempval=="true")\r
105                                 {\r
106                                         singleuse=true;\r
107                                 }\r
108                                 exp.ResultText(4,tempval);\r
109                                 if(tempval=="true")\r
110                                 {\r
111                                         publishtrustlist=true;\r
112                                 }\r
113                                 exp.ResultText(5,tempval);\r
114                                 if(tempval=="true")\r
115                                 {\r
116                                         publishboardlist=true;\r
117                                 }\r
118                                 exp.ResultText(6,tempval);\r
119                                 if(tempval=="true")\r
120                                 {\r
121                                         publishfreesite=true;\r
122                                 }\r
123 \r
124                                 xml.AddIdentity(name,publickey,privatekey,singleuse,publishtrustlist,publishboardlist,publishfreesite);\r
125 \r
126                                 exp.Step();\r
127                         }\r
128                         return "HTTP/1.1 200 OK\r\nContent-Type: text/xml\r\nContent-Disposition: attachment; filename=identities.xml\r\n\r\n"+xml.GetXML();\r
129                 }\r
130                 if((*queryvars.find("formaction")).second=="import")\r
131                 {\r
132                         if(queryvars.find("file")!=queryvars.end())\r
133                         {\r
134                                 IdentityExportXML xml;\r
135                                 if(xml.ParseXML((*queryvars.find("file")).second))\r
136                                 {\r
137                                         SQLite3DB::Statement imp=m_db->Prepare("INSERT INTO tblLocalIdentity(Name,PublicKey,PrivateKey,SingleUse,PublishTrustList,PublishBoardList,PublishFreesite) VALUES(?,?,?,?,?,?,?);");\r
138                                         for(int i=0; i<xml.GetCount(); i++)\r
139                                         {\r
140                                                 std::string tempval="false";\r
141                                                 imp.Bind(0,xml.GetName(i));\r
142                                                 imp.Bind(1,xml.GetPublicKey(i));\r
143                                                 imp.Bind(2,xml.GetPrivateKey(i));\r
144                                                 if(xml.GetSingleUse(i))\r
145                                                 {\r
146                                                         tempval="true";\r
147                                                 }\r
148                                                 else\r
149                                                 {\r
150                                                         tempval="false";\r
151                                                 }\r
152                                                 imp.Bind(3,tempval);\r
153                                                 if(xml.GetPublishTrustList(i))\r
154                                                 {\r
155                                                         tempval="true";\r
156                                                 }\r
157                                                 else\r
158                                                 {\r
159                                                         tempval="false";\r
160                                                 }\r
161                                                 imp.Bind(4,tempval);\r
162                                                 if(xml.GetPublishBoardList(i))\r
163                                                 {\r
164                                                         tempval="true";\r
165                                                 }\r
166                                                 else\r
167                                                 {\r
168                                                         tempval="false";\r
169                                                 }\r
170                                                 imp.Bind(5,tempval);\r
171                                                 if(xml.GetPublishFreesite(i))\r
172                                                 {\r
173                                                         tempval="true";\r
174                                                 }\r
175                                                 else\r
176                                                 {\r
177                                                         tempval="false";\r
178                                                 }\r
179                                                 imp.Bind(6,tempval);\r
180                                                 imp.Step();\r
181                                                 imp.Reset();\r
182                                         }\r
183                                 }\r
184                         }\r
185                 }\r
186         }\r
187 \r
188         content+="<h2>Local Identities</h2>";\r
189 \r
190         content+="<table><tr><th>Export Identities</th><th>Import Identities</th></tr>";\r
191         content+="<tr><td>";\r
192         content+="<form name=\"frmexport\" method=\"POST\">";\r
193         content+="<input type=\"hidden\" name=\"formaction\" value=\"export\">";\r
194         content+="<input type=\"submit\" value=\"Export Identities\">";\r
195         content+="</form>";\r
196         content+="</td><td>";\r
197         content+="<form name=\"frmimport\" method=\"POST\" enctype=\"multipart/form-data\">";\r
198         content+="<input type=\"hidden\" name=\"formaction\" value=\"import\">";\r
199         content+="<input type=\"file\" name=\"file\">";\r
200         content+="<input type=\"submit\" value=\"Import Identities\">";\r
201         content+="</form>";\r
202         content+="</td></tr></table>";\r
203 \r
204         content+="<hr>";\r
205 \r
206         content+="<form name=\"frmlocalidentity\" method=\"POST\">";\r
207         content+="<input type=\"hidden\" name=\"formaction\" value=\"update\">";\r
208         content+="<table><tr><td></td><th>Name</th><th>Single Use</th><th>Publish Trust List</th><th>Publish Board List</th><th>Publish Freesite</th><th>Announced? *</th></tr>";\r
209 \r
210         SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID,tblLocalIdentity.Name,tblLocalIdentity.PublicKey,tbLLocalIdentity.PublishTrustList,tblLocalIdentity.SingleUse,tblLocalIdentity.PublishBoardList,tblIdentity.IdentityID,tblLocalIdentity.PublishFreesite FROM tblLocalIdentity LEFT JOIN tblIdentity ON tblLocalIdentity.PublicKey=tblIdentity.PublicKey ORDER BY tblLocalIdentity.Name;");\r
211         st.Step();\r
212 \r
213         count=0;\r
214         while(st.RowReturned())\r
215         {\r
216                 StringFunctions::Convert(count,countstr);\r
217                 std::string id="";\r
218                 std::string name="";\r
219                 std::string publickey="";\r
220                 std::string publishtrustlist="";\r
221                 std::string singleuse="";\r
222                 std::string keypart="";\r
223                 std::string publishboardlist="";\r
224                 std::string publishfreesite="";\r
225 \r
226                 st.ResultText(0,id);\r
227                 st.ResultText(1,name);\r
228                 st.ResultText(2,publickey);\r
229                 st.ResultText(3,publishtrustlist);\r
230                 st.ResultText(4,singleuse);\r
231                 st.ResultText(5,publishboardlist);\r
232                 st.ResultText(7,publishfreesite);\r
233 \r
234                 if(publickey.size()>8)\r
235                 {\r
236                         keypart=publickey.substr(3,5);\r
237                 }\r
238 \r
239                 content+="<tr>";\r
240                 content+="<td><input type=\"checkbox\" name=\"chkidentityid["+countstr+"]\" value=\""+id+"\"></td>";\r
241                 content+="<td title=\""+publickey+"\">"+SanitizeOutput(name+keypart)+"...</td>";\r
242                 content+="<td>"+CreateTrueFalseDropDown("singleuse["+countstr+"]",singleuse)+"</td>";\r
243                 content+="<td>"+CreateTrueFalseDropDown("publishtrustlist["+countstr+"]",publishtrustlist)+"</td>";\r
244                 content+="<td>"+CreateTrueFalseDropDown("publishboardlist["+countstr+"]",publishboardlist)+"</td>";\r
245                 content+="<td>"+CreateTrueFalseDropDown("publishfreesite["+countstr+"]",publishfreesite)+"</td>";\r
246                 if(st.ResultNull(6))\r
247                 {\r
248                         content+="<td>No</td>";\r
249                 }\r
250                 else\r
251                 {\r
252                         content+="<td>Yes</td>";\r
253                 }\r
254                 content+="</tr>";\r
255                 st.Step();\r
256                 count++;\r
257         }\r
258 \r
259         content+="<tr><td colspan=\"5\"><center><input type=\"submit\" value=\"Update Selected\"> <input type=\"submit\" value=\"Delete Selected\"></td></tr>";\r
260         content+="</table>";\r
261         content+="<p class=\"paragraph\">* An identity is considered successfully announced when you have downloaded a trust list from someone that contains the identity.</p>";\r
262         content+="<p class=\"paragraph\">Single Use Identities will automatically be deleted 7 days after creation.</p>";\r
263 \r
264         return "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"+StringFunctions::Replace(m_template,"[CONTENT]",content);\r
265 }\r
266 \r
267 const bool LocalIdentitiesPage::WillHandleURI(const std::string &uri)\r
268 {\r
269         if(uri.find("localidentities.")!=std::string::npos)\r
270         {\r
271                 return true;\r
272         }\r
273         else\r
274         {\r
275                 return false;\r
276         }\r
277 }\r