version 0.1.11
[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 \r
45                 CreateArgArray(queryvars,"chkidentityid",ids);\r
46                 CreateArgArray(queryvars,"singleuse",singleuse);\r
47                 CreateArgArray(queryvars,"publishtrustlist",publishtrustlist);\r
48                 CreateArgArray(queryvars,"publishboardlist",publishboardlist);\r
49 \r
50                 if((*queryvars.find("formaction")).second=="update")\r
51                 {\r
52                         SQLite3DB::Statement update=m_db->Prepare("UPDATE tblLocalIdentity SET SingleUse=?, PublishTrustList=?, PublishBoardList=? WHERE LocalIdentityID=?;");\r
53                         for(int i=0; i<ids.size(); i++)\r
54                         {\r
55                                 if(ids[i]!="")\r
56                                 {\r
57                                         StringFunctions::Convert(ids[i],id);\r
58                                         update.Bind(0,singleuse[i]);\r
59                                         update.Bind(1,publishtrustlist[i]);\r
60                                         update.Bind(2,publishboardlist[i]);\r
61                                         update.Bind(3,id);\r
62                                         update.Step();\r
63                                         update.Reset();\r
64                                 }\r
65                         }\r
66                 }\r
67                 if((*queryvars.find("formaction")).second=="delete")\r
68                 {\r
69                         SQLite3DB::Statement del=m_db->Prepare("DELETE FROM tblLocalIdentity WHERE LocalIdentityID=?;");\r
70                         for(int i=0; i<ids.size(); i++)\r
71                         {\r
72                                 if(ids[i]!="")\r
73                                 {\r
74                                         StringFunctions::Convert(ids[i],id);\r
75                                         del.Bind(0,id);\r
76                                         del.Step();\r
77                                         del.Reset();\r
78                                 }\r
79                         }\r
80                 }\r
81                 if((*queryvars.find("formaction")).second=="export")\r
82                 {\r
83                         IdentityExportXML xml;\r
84                         SQLite3DB::Statement exp=m_db->Prepare("SELECT Name,PublicKey,PrivateKey,SingleUse,PublishTrustList,PublishBoardList FROM tblLocalIdentity WHERE PublicKey IS NOT NULL AND PrivateKey IS NOT NULL;");\r
85                         exp.Step();\r
86                         while(exp.RowReturned())\r
87                         {\r
88                                 std::string name="";\r
89                                 std::string publickey="";\r
90                                 std::string privatekey="";\r
91                                 std::string tempval="";\r
92                                 bool singleuse=false;\r
93                                 bool publishtrustlist=false;\r
94                                 bool publishboardlist=false;\r
95 \r
96                                 exp.ResultText(0,name);\r
97                                 exp.ResultText(1,publickey);\r
98                                 exp.ResultText(2,privatekey);\r
99                                 exp.ResultText(3,tempval);\r
100                                 if(tempval=="true")\r
101                                 {\r
102                                         singleuse=true;\r
103                                 }\r
104                                 exp.ResultText(4,tempval);\r
105                                 if(tempval=="true")\r
106                                 {\r
107                                         publishtrustlist=true;\r
108                                 }\r
109                                 exp.ResultText(5,tempval);\r
110                                 if(tempval=="true")\r
111                                 {\r
112                                         publishboardlist=true;\r
113                                 }\r
114 \r
115                                 xml.AddIdentity(name,publickey,privatekey,singleuse,publishtrustlist,publishboardlist);\r
116 \r
117                                 exp.Step();\r
118                         }\r
119                         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
120                 }\r
121                 if((*queryvars.find("formaction")).second=="import")\r
122                 {\r
123                         if(queryvars.find("file")!=queryvars.end())\r
124                         {\r
125                                 IdentityExportXML xml;\r
126                                 if(xml.ParseXML((*queryvars.find("file")).second))\r
127                                 {\r
128                                         SQLite3DB::Statement imp=m_db->Prepare("INSERT INTO tblLocalIdentity(Name,PublicKey,PrivateKey,SingleUse,PublishTrustList,PublishBoardList) VALUES(?,?,?,?,?,?);");\r
129                                         for(int i=0; i<xml.GetCount(); i++)\r
130                                         {\r
131                                                 std::string tempval="false";\r
132                                                 imp.Bind(0,xml.GetName(i));\r
133                                                 imp.Bind(1,xml.GetPublicKey(i));\r
134                                                 imp.Bind(2,xml.GetPrivateKey(i));\r
135                                                 if(xml.GetSingleUse(i))\r
136                                                 {\r
137                                                         tempval="true";\r
138                                                 }\r
139                                                 else\r
140                                                 {\r
141                                                         tempval="false";\r
142                                                 }\r
143                                                 imp.Bind(3,tempval);\r
144                                                 if(xml.GetPublishTrustList(i))\r
145                                                 {\r
146                                                         tempval="true";\r
147                                                 }\r
148                                                 else\r
149                                                 {\r
150                                                         tempval="false";\r
151                                                 }\r
152                                                 imp.Bind(4,tempval);\r
153                                                 if(xml.GetPublishBoardList(i))\r
154                                                 {\r
155                                                         tempval="true";\r
156                                                 }\r
157                                                 else\r
158                                                 {\r
159                                                         tempval="false";\r
160                                                 }\r
161                                                 imp.Bind(5,tempval);\r
162                                                 imp.Step();\r
163                                                 imp.Reset();\r
164                                         }\r
165                                 }\r
166                         }\r
167                 }\r
168         }\r
169 \r
170         content+="<h2>Local Identities</h2>";\r
171 \r
172         content+="<table><tr><th>Export Identities</th><th>Import Identities</th></tr>";\r
173         content+="<tr><td>";\r
174         content+="<form name=\"frmexport\" method=\"POST\">";\r
175         content+="<input type=\"hidden\" name=\"formaction\" value=\"export\">";\r
176         content+="<input type=\"submit\" value=\"Export Identities\">";\r
177         content+="</form>";\r
178         content+="</td><td>";\r
179         content+="<form name=\"frmimport\" method=\"POST\" enctype=\"multipart/form-data\">";\r
180         content+="<input type=\"hidden\" name=\"formaction\" value=\"import\">";\r
181         content+="<input type=\"file\" name=\"file\">";\r
182         content+="<input type=\"submit\" value=\"Import Identities\">";\r
183         content+="</form>";\r
184         content+="</td></tr></table>";\r
185 \r
186         content+="<hr>";\r
187 \r
188         content+="<form name=\"frmlocalidentity\" method=\"POST\">";\r
189         content+="<input type=\"hidden\" name=\"formaction\" value=\"update\">";\r
190         content+="<table><tr><td></td><th>Name</th><th>Single Use</th><th>Publish Trust List</th><th>Publish Board List</th><th>Announced? *</th></tr>";\r
191 \r
192         SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID,tblLocalIdentity.Name,tblLocalIdentity.PublicKey,tbLLocalIdentity.PublishTrustList,tblLocalIdentity.SingleUse,tblLocalIdentity.PublishBoardList,tblIdentity.IdentityID FROM tblLocalIdentity LEFT JOIN tblIdentity ON tblLocalIdentity.PublicKey=tblIdentity.PublicKey ORDER BY tblLocalIdentity.Name;");\r
193         st.Step();\r
194 \r
195         count=0;\r
196         while(st.RowReturned())\r
197         {\r
198                 StringFunctions::Convert(count,countstr);\r
199                 std::string id="";\r
200                 std::string name="";\r
201                 std::string publickey="";\r
202                 std::string publishtrustlist="";\r
203                 std::string singleuse="";\r
204                 std::string keypart="";\r
205                 std::string publishboardlist="";\r
206 \r
207                 st.ResultText(0,id);\r
208                 st.ResultText(1,name);\r
209                 st.ResultText(2,publickey);\r
210                 st.ResultText(3,publishtrustlist);\r
211                 st.ResultText(4,singleuse);\r
212                 st.ResultText(5,publishboardlist);\r
213 \r
214                 if(publickey.size()>8)\r
215                 {\r
216                         keypart=publickey.substr(3,5);\r
217                 }\r
218 \r
219                 content+="<tr>";\r
220                 content+="<td><input type=\"checkbox\" name=\"chkidentityid["+countstr+"]\" value=\""+id+"\"></td>";\r
221                 content+="<td title=\""+publickey+"\">"+SanitizeOutput(name+keypart)+"...</td>";\r
222                 content+="<td>"+CreateTrueFalseDropDown("singleuse["+countstr+"]",singleuse)+"</td>";\r
223                 content+="<td>"+CreateTrueFalseDropDown("publishtrustlist["+countstr+"]",publishtrustlist)+"</td>";\r
224                 content+="<td>"+CreateTrueFalseDropDown("publishboardlist["+countstr+"]",publishboardlist)+"</td>";\r
225                 if(st.ResultNull(6))\r
226                 {\r
227                         content+="<td>No</td>";\r
228                 }\r
229                 else\r
230                 {\r
231                         content+="<td>Yes</td>";\r
232                 }\r
233                 content+="</tr>";\r
234                 st.Step();\r
235                 count++;\r
236         }\r
237 \r
238         content+="<tr><td colspan=\"5\"><center><input type=\"submit\" value=\"Update Selected\"> <input type=\"submit\" value=\"Delete Selected\" onClick=\"if(confirm('Delete Selected Identities?')){frmlocalidentity.formaction.value='delete';}else{return false;}\"></td></tr>";\r
239         content+="</table>";\r
240         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
241 \r
242         return "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"+StringFunctions::Replace(m_template,"[CONTENT]",content);\r
243 }\r
244 \r
245 const bool LocalIdentitiesPage::WillHandleURI(const std::string &uri)\r
246 {\r
247         if(uri.find("localidentities.")!=std::string::npos)\r
248         {\r
249                 return true;\r
250         }\r
251         else\r
252         {\r
253                 return false;\r
254         }\r
255 }\r