version 0.2.17
[fms.git] / src / http / pages / peertrustpage.cpp
1 #include "../../../include/http/pages/peertrustpage.h"\r
2 #include "../../../include/stringfunctions.h"\r
3 #include "../../../include/global.h"\r
4 \r
5 #ifdef XMEM\r
6         #include <xmem.h>\r
7 #endif\r
8 \r
9 const std::string PeerTrustPage::BuildQueryString(const long startrow, const std::string &namesearch, const std::string &sortby, const std::string &sortorder, const int localidentityid)\r
10 {\r
11         std::string returnval="";\r
12         std::string tempval="";\r
13         \r
14         if(startrow>=0)\r
15         {\r
16                 StringFunctions::Convert(startrow,tempval);\r
17                 returnval+="startrow="+tempval;\r
18         }\r
19 \r
20         if(namesearch!="")\r
21         {\r
22                 if(returnval!="")\r
23                 {\r
24                         returnval+="&";\r
25                 }\r
26                 returnval+="namesearch="+namesearch;\r
27         }\r
28 \r
29         if(sortby!="")\r
30         {\r
31                 if(returnval!="")\r
32                 {\r
33                         returnval+="&";\r
34                 }\r
35                 returnval+="sortby="+sortby;\r
36         }\r
37 \r
38         if(sortorder!="")\r
39         {\r
40                 if(returnval!="")\r
41                 {\r
42                         returnval+="&";\r
43                 }\r
44                 returnval+="sortorder="+sortorder;\r
45         }\r
46 \r
47         if(localidentityid>=0)\r
48         {\r
49                 std::string localidentityidstr="";\r
50                 StringFunctions::Convert(localidentityid,localidentityidstr);\r
51                 if(returnval!="")\r
52                 {\r
53                         returnval+="&";\r
54                 }\r
55                 returnval+="localidentityid="+localidentityidstr;\r
56         }\r
57 \r
58         return returnval;\r
59 \r
60 }\r
61 \r
62 const std::string PeerTrustPage::CreateLocalIdentityDropDown(const std::string &name, const int selectedlocalidentityid)\r
63 {\r
64         std::string result="";\r
65         \r
66         result+="<select name=\""+name+"\">";\r
67         \r
68         SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID,Name,PublicKey FROM tblLocalIdentity WHERE PublicKey IS NOT NULL ORDER BY Name COLLATE NOCASE;");\r
69         st.Step();\r
70 \r
71         while(st.RowReturned())\r
72         {\r
73                 int localidentityid=-1;\r
74                 std::string localidentityidstr="";\r
75                 std::string name="";\r
76                 std::string publickey="";\r
77 \r
78                 st.ResultInt(0,localidentityid);\r
79                 st.ResultText(1,name);\r
80                 st.ResultText(2,publickey);\r
81 \r
82                 StringFunctions::Convert(localidentityid,localidentityidstr);\r
83 \r
84                 result+="<option value=\""+localidentityidstr+"\"";\r
85                 if(localidentityid==selectedlocalidentityid)\r
86                 {\r
87                         result+=" SELECTED";\r
88                 }\r
89                 result+=">"+SanitizeOutput(CreateShortIdentityName(name,publickey))+"</option>";\r
90                 st.Step();\r
91         }\r
92 \r
93         result+="</select>";\r
94 \r
95         return result;\r
96 }\r
97 \r
98 const std::string PeerTrustPage::GeneratePage(const std::string &method, const std::map<std::string,std::string> &queryvars)\r
99 {\r
100         int count=0;\r
101         std::string countstr;\r
102         std::string content="";\r
103         int identitycount=0;            // total number of ids we know\r
104         int rowsperpage=25;                     // how many ids to show per page\r
105         std::string rowsperpagestr;\r
106         int startrow=0;\r
107         std::string startrowstr="0";\r
108         std::string namesearch="";\r
109         std::string sql;\r
110         std::string sortby="";\r
111         std::string sortorder="";\r
112         std::string localidentityidstr="";\r
113         int localidentityid=-1;\r
114 \r
115         StringFunctions::Convert(rowsperpage,rowsperpagestr);\r
116 \r
117         // get localidentityid from querystring or load one from the database\r
118         if(queryvars.find("localidentityid")!=queryvars.end())\r
119         {\r
120                 localidentityidstr=(*queryvars.find("localidentityid")).second;\r
121                 StringFunctions::Convert(localidentityidstr,localidentityid);\r
122         }\r
123         else\r
124         {\r
125                 SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity;");\r
126                 st.Step();\r
127                 if(st.RowReturned())\r
128                 {\r
129                         st.ResultInt(0,localidentityid);\r
130                         StringFunctions::Convert(localidentityid,localidentityidstr);\r
131                 }\r
132         }\r
133 \r
134         if(localidentityid!=-1 && queryvars.find("formaction")!=queryvars.end() && (*queryvars.find("formaction")).second=="update")\r
135         {\r
136                 std::vector<std::string> identityids;\r
137                 std::vector<std::string> oldlmt;\r
138                 std::vector<std::string> lmt;\r
139                 std::vector<std::string> oldltlt;\r
140                 std::vector<std::string> ltlt;\r
141                 std::vector<std::string> oldmtc;\r
142                 std::vector<std::string> mtc;\r
143                 std::vector<std::string> oldtltc;\r
144                 std::vector<std::string> tltc;\r
145                 int localmessagetrust=0;\r
146                 int localtrustlisttrust=0;\r
147                 int identityid=-1;\r
148 \r
149                 CreateArgArray(queryvars,"identityid",identityids);\r
150                 CreateArgArray(queryvars,"oldlocalmessagetrust",oldlmt);\r
151                 CreateArgArray(queryvars,"localmessagetrust",lmt);\r
152                 CreateArgArray(queryvars,"oldlocaltrustlisttrust",oldltlt);\r
153                 CreateArgArray(queryvars,"localtrustlisttrust",ltlt);\r
154                 CreateArgArray(queryvars,"oldmessagetrustcomment",oldmtc);\r
155                 CreateArgArray(queryvars,"messagetrustcomment",mtc);\r
156                 CreateArgArray(queryvars,"oldtrustlisttrustcomment",oldtltc);\r
157                 CreateArgArray(queryvars,"trustlisttrustcomment",tltc);\r
158 \r
159                 SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) VALUES(?,?);");\r
160                 SQLite3DB::Statement update=m_db->Prepare("UPDATE tblIdentityTrust SET LocalMessageTrust=?, LocalTrustListTrust=?, MessageTrustComment=?, TrustListTrustComment=? WHERE LocalIdentityID=? AND IdentityID=?;");\r
161 \r
162                 for(int i=0; i<identityids.size(); i++)\r
163                 {\r
164                         if(oldlmt[i]!=lmt[i] || oldltlt[i]!=ltlt[i] || oldmtc[i]!=mtc[i] || oldtltc[i]!=tltc[i])\r
165                         {\r
166                                 StringFunctions::Convert(lmt[i],localmessagetrust);\r
167                                 StringFunctions::Convert(ltlt[i],localtrustlisttrust);\r
168                                 StringFunctions::Convert(identityids[i],identityid);\r
169 \r
170                                 ins.Bind(0,localidentityid);\r
171                                 ins.Bind(1,identityid);\r
172                                 ins.Step();\r
173                                 ins.Reset();\r
174 \r
175                                 if(lmt[i]!="")\r
176                                 {\r
177                                         update.Bind(0,localmessagetrust);\r
178                                 }\r
179                                 else\r
180                                 {\r
181                                         update.Bind(0);\r
182                                 }\r
183                                 if(ltlt[i]!="")\r
184                                 {\r
185                                         update.Bind(1,localtrustlisttrust);\r
186                                 }\r
187                                 else\r
188                                 {\r
189                                         update.Bind(1);\r
190                                 }\r
191                                 update.Bind(2,mtc[i]);\r
192                                 update.Bind(3,tltc[i]);\r
193                                 update.Bind(4,localidentityid);\r
194                                 update.Bind(5,identityid);\r
195                                 update.Step();\r
196                                 update.Reset();\r
197                         }\r
198                 }\r
199 \r
200         }\r
201 \r
202         // if startrow is specified\r
203         if(queryvars.find("startrow")!=queryvars.end())\r
204         {\r
205                 startrowstr=(*queryvars.find("startrow")).second;\r
206                 // convert back and forth, just in case a number wasn't passed in startrow\r
207                 StringFunctions::Convert(startrowstr,startrow);\r
208                 if(startrow<0)\r
209                 {\r
210                         startrow=0;\r
211                 }\r
212                 StringFunctions::Convert(startrow,startrowstr);\r
213         }\r
214 \r
215         // sort by\r
216         if(queryvars.find("sortby")!=queryvars.end())\r
217         {\r
218                 sortby=(*queryvars.find("sortby")).second;\r
219         }\r
220         else\r
221         {\r
222                 sortby="Name";\r
223         }\r
224 \r
225         // sort order\r
226         if(queryvars.find("sortorder")!=queryvars.end())\r
227         {\r
228                 sortorder=(*queryvars.find("sortorder")).second;\r
229         }\r
230         else\r
231         {\r
232                 sortorder="ASC";\r
233         }\r
234 \r
235         // if we are searching by name\r
236         if(queryvars.find("namesearch")!=queryvars.end())\r
237         {\r
238                 namesearch=(*queryvars.find("namesearch")).second;\r
239         }\r
240 \r
241         content+="<h2>Peer Trust</h2>";\r
242         content+="Message Trust is how much you trust the identity to post good messages. Trust List Trust is how much weight you want the trust list of that identity to have when calculating the total. The local trust levels are set by you, and the peer trust levels are calculated by a weighted average using other identities' trust lists.  Trust is recalculated once an hour from received trust lists.  You must have at least 1 identity created and have received the SSK keypair for it from Freenet before setting trust.<br>";\r
243         content+="* - This identity is not publishing a trust list<br>";\r
244 \r
245         // search drop down\r
246         content+="<div style=\"text-align:center;margin-bottom:5px;\">";\r
247         content+="<form name=\"frmsearch\" method=\"POST\" action=\"peertrust.htm?"+BuildQueryString(0,"","","",localidentityid)+"\">";\r
248         content+="<input type=\"text\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";\r
249         content+="<input type=\"submit\" value=\"Search\">";\r
250         content+="</form>";\r
251         content+="</div>";\r
252 \r
253         content+="<div style=\"text-align:center;\">";\r
254         content+="<form name=\"frmlocalidentity\" method=\"POST\" action=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,sortby,sortorder,-1)+"\">";\r
255         content+="Load Trust List of ";\r
256         content+=CreateLocalIdentityDropDown("localidentityid",localidentityid);\r
257         content+="<input type=\"submit\" value=\"Load List\">";\r
258         content+="</form>";\r
259         content+="</div>";\r
260 \r
261         content+="<form name=\"frmtrust\" method=\"POST\">";\r
262         content+="<input type=\"hidden\" name=\"formaction\" value=\"update\">";\r
263         content+="<input type=\"hidden\" name=\"localidentityid\" value=\""+localidentityidstr+"\">";\r
264         content+="<input type=\"hidden\" name=\"startrow\" value=\""+startrowstr+"\">";\r
265         if(namesearch!="")\r
266         {\r
267                 content+="<input type=\"hidden\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";\r
268         }\r
269         content+="<table class=\"small90\">";\r
270         content+="<tr><th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"Name",ReverseSort("Name",sortby,sortorder),localidentityid)+"\">Name</a></th>";\r
271         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalMessageTrust",ReverseSort("tblIdentityTrust.LocalMessageTrust",sortby,sortorder),localidentityid)+"\">Local Message Trust</a></th>";\r
272         content+="<th>Message Comment</th>";\r
273         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerMessageTrust",ReverseSort("PeerMessageTrust",sortby,sortorder),localidentityid)+"\">Peer Message Trust</a></th>";\r
274         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalTrustListTrust",ReverseSort("tblIdentityTrust.LocalTrustListTrust",sortby,sortorder),localidentityid)+"\">Local Trust List Trust</a></th>";\r
275         content+="<th>Trust Comment</th>";\r
276         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerTrustListTrust",ReverseSort("PeerTrustListTrust",sortby,sortorder),localidentityid)+"\">Peer Trust List Trust</a></th>";\r
277         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"MessageCount",ReverseSort("MessageCount",sortby,sortorder),localidentityid)+"\">Message Count</a></th>";\r
278         content+="</tr>\r\n";\r
279         \r
280         // get count of identities we are showing\r
281         sql="SELECT COUNT(*) FROM tblIdentity LEFT JOIN (SELECT IdentityID FROM tblIdentityTrust WHERE LocalIdentityID=?) AS 'tblIdentityTrust' ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID ";\r
282         sql+="WHERE tblIdentity.Hidden='false'";\r
283         if(namesearch!="")\r
284         {\r
285                 sql+=" AND (Name LIKE '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";\r
286         }\r
287         sql+=";";\r
288         SQLite3DB::Statement st=m_db->Prepare(sql);\r
289         st.Bind(0,localidentityid);\r
290         if(namesearch!="")\r
291         {\r
292                 st.Bind(1,namesearch);\r
293                 st.Bind(2,namesearch);\r
294         }\r
295         st.Step();\r
296         st.ResultInt(0,identitycount);\r
297         st.Finalize();\r
298 \r
299         sql="SELECT tblIdentity.IdentityID,Name,tblIdentityTrust.LocalMessageTrust,PeerMessageTrust,tblIdentityTrust.LocalTrustListTrust,PeerTrustListTrust,PublicKey,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment,COUNT(MessageID) AS 'MessageCount',tblIdentity.PublishTrustList ";\r
300         sql+="FROM tblIdentity LEFT JOIN (SELECT LocalIdentityID,IdentityID,LocalMessageTrust,LocalTrustListTrust,MessageTrustComment,TrustListTrustComment FROM tblIdentityTrust WHERE LocalIdentityID=?) AS tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID LEFT JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID ";\r
301         sql+="WHERE tblIdentity.Hidden='false'";\r
302         if(namesearch!="")\r
303         {\r
304                 sql+=" AND (Name LIKE  '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";\r
305         }\r
306         sql+=" GROUP BY tblIdentity.IdentityID";\r
307         sql+=" ORDER BY";\r
308         if(sortby=="Name")\r
309         {\r
310                 sql+=" Name COLLATE NOCASE";\r
311         }\r
312         else\r
313         {\r
314                 sql+=" "+sortby;\r
315         }\r
316         if(sortorder!="")\r
317         {\r
318                 sql+=" "+sortorder;\r
319         }\r
320         sql+=" LIMIT "+startrowstr+","+rowsperpagestr+";";\r
321         st=m_db->Prepare(sql);\r
322         st.Bind(0,localidentityid);\r
323         if(namesearch!="")\r
324         {\r
325                 st.Bind(1,namesearch);\r
326                 st.Bind(2,namesearch);\r
327         }\r
328         st.Step();\r
329 \r
330         while(st.RowReturned())\r
331         {\r
332                 std::string identityid;\r
333                 std::string name;\r
334                 std::string localmessagetrust;\r
335                 std::string peermessagetrust;\r
336                 std::string localtrustlisttrust;\r
337                 std::string peertrustlisttrust;\r
338                 std::string publickey;\r
339                 std::string messagetrustcomment="";\r
340                 std::string trustlisttrustcomment="";\r
341                 std::string messagecountstr="";\r
342                 std::string publishtrustlist="";\r
343 \r
344                 StringFunctions::Convert(count,countstr);\r
345 \r
346                 st.ResultText(0,identityid);\r
347                 st.ResultText(1,name);\r
348                 st.ResultText(2,localmessagetrust);\r
349                 st.ResultText(3,peermessagetrust);\r
350                 st.ResultText(4,localtrustlisttrust);\r
351                 st.ResultText(5,peertrustlisttrust);\r
352                 st.ResultText(6,publickey);\r
353                 st.ResultText(7,messagetrustcomment);\r
354                 st.ResultText(8,trustlisttrustcomment);\r
355                 st.ResultText(9,messagecountstr);\r
356                 st.ResultText(10,publishtrustlist);\r
357 \r
358                 content+="<tr>";\r
359                 content+="<td title=\""+publickey+"\">";\r
360                 content+="<input type=\"hidden\" name=\"identityid["+countstr+"]\" value=\""+identityid+"\">";\r
361                 content+="<a href=\"peerdetails.htm?identityid="+identityid+"\">";\r
362                 if(name!="")\r
363                 {\r
364                         content+=SanitizeOutput(CreateShortIdentityName(name,publickey));\r
365                 }\r
366                 else\r
367                 {\r
368                         content+="[Unknown Name]";\r
369                 }\r
370                 content+="</a>";\r
371                 content+="</td>";\r
372                 content+="<td "+GetClassString(localmessagetrust)+">";\r
373                 content+="<input type=\"hidden\" name=\"oldlocalmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\">";\r
374                 content+="<input type=\"text\" name=\"localmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\"></td>";\r
375                 content+="<td "+GetClassString(localmessagetrust)+">";\r
376                 content+="<input type=\"hidden\" name=\"oldmessagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\">";\r
377                 content+="<input type=\"text\" name=\"messagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\" maxlength=\"50\" class=\"small90\">";\r
378                 content+="</td>";               \r
379                 content+="<td "+GetClassString(peermessagetrust)+">";\r
380                 content+=peermessagetrust+"</td>";\r
381                 content+="<td "+GetClassString(localtrustlisttrust)+">";\r
382                 content+="<input type=\"hidden\" name=\"oldlocaltrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\">";\r
383                 content+="<input type=\"text\" name=\"localtrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\">";\r
384                 if(publishtrustlist=="false")\r
385                 {\r
386                         content+="*";\r
387                 }\r
388                 content+="</td>";\r
389                 content+="<td "+GetClassString(localtrustlisttrust)+">";\r
390                 content+="<input type=\"hidden\" name=\"oldtrustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\">";\r
391                 content+="<input type=\"text\" name=\"trustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\" maxlength=\"50\" class=\"small90\">";\r
392                 content+="</td>";\r
393                 content+="<td "+GetClassString(peertrustlisttrust)+">";\r
394                 content+=peertrustlisttrust+"</td>";\r
395                 content+="<td>"+messagecountstr+"</td>";\r
396                 content+="</tr>\r\n";\r
397                 st.Step();\r
398                 count++;\r
399         }\r
400         \r
401         if(startrow>0 || startrow+rowsperpage<identitycount)\r
402         {\r
403                 std::string tempstr;\r
404                 int cols=0;\r
405 \r
406                 content+="<tr>";\r
407                 if(startrow>0)\r
408                 {\r
409                         StringFunctions::Convert(startrow-rowsperpage,tempstr);\r
410                         content+="<td colspan=\"3\" align=\"left\"><a href=\"peertrust.htm?"+BuildQueryString(startrow-rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\"><-- Previous Page</a></td>";\r
411                         cols+=3;\r
412                 }\r
413                 if(startrow+rowsperpage<identitycount)\r
414                 {\r
415                         while(cols<5)\r
416                         {\r
417                                 content+="<td></td>";\r
418                                 cols++;\r
419                         }\r
420                         content+="<td colspan=\"3\" align=\"right\"><a href=\"peertrust.htm?"+BuildQueryString(startrow+rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\">Next Page --></a></td>";\r
421                 }\r
422                 content+="</tr>";\r
423         }\r
424 \r
425         content+="<tr><td colspan=\"8\"><center><input type=\"submit\" value=\"Update Trust\"></center></td></tr>";\r
426         content+="</table>";\r
427         content+="</form>";\r
428 \r
429         return "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"+StringFunctions::Replace(m_template,"[CONTENT]",content);\r
430 }\r
431 \r
432 const std::string PeerTrustPage::GetClassString(const std::string &trustlevel)\r
433 {\r
434         int tempint=0;\r
435         std::string tempstr;\r
436 \r
437         StringFunctions::Convert(trustlevel,tempint);\r
438         tempint/=10;\r
439         StringFunctions::Convert(tempint,tempstr);\r
440 \r
441         if(trustlevel!="")\r
442         {\r
443                 return "class=\"trust"+tempstr+"\"";\r
444         }\r
445         else\r
446         {\r
447                 return "";\r
448         }\r
449 }\r
450 \r
451 const std::string PeerTrustPage::ReverseSort(const std::string &sortname, const std::string &currentsortby, const std::string &currentsortorder)\r
452 {\r
453         if(sortname==currentsortby)\r
454         {\r
455                 if(currentsortorder=="ASC")\r
456                 {\r
457                         return "DESC";\r
458                 }\r
459                 else\r
460                 {\r
461                         return "ASC";\r
462                 }\r
463         }\r
464         else\r
465         {\r
466                 return currentsortorder;\r
467         }\r
468 }\r
469 \r
470 const bool PeerTrustPage::WillHandleURI(const std::string &uri)\r
471 {\r
472         if(uri.find("peertrust.")!=std::string::npos)\r
473         {\r
474                 return true;\r
475         }\r
476         else\r
477         {\r
478                 return false;\r
479         }\r
480 }\r