version 0.2.22
[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                 // insert the ID into the temporary table so we remember the identity if we load the page later\r
123                 m_db->Execute("DELETE FROM tmpLocalIdentityPeerTrustPage;");\r
124                 SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tmpLocalIdentityPeerTrustPage(LocalIdentityID) VALUES(?);");\r
125                 st.Bind(0,localidentityid);\r
126                 st.Step();\r
127         }\r
128         else\r
129         {\r
130                 // try to get the localidentityid if it exists in the temp table, otherwise load the first identity in the database\r
131                 SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tmpLocalIdentityPeerTrustPage;");\r
132                 st.Step();\r
133                 if(st.RowReturned())\r
134                 {\r
135                         st.ResultInt(0,localidentityid);\r
136                         StringFunctions::Convert(localidentityid,localidentityidstr);\r
137                 }\r
138                 else\r
139                 {\r
140                         st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity;");\r
141                         st.Step();\r
142                         if(st.RowReturned())\r
143                         {\r
144                                 st.ResultInt(0,localidentityid);\r
145                                 StringFunctions::Convert(localidentityid,localidentityidstr);\r
146                         }\r
147                         st.Finalize();\r
148                 }\r
149         }\r
150 \r
151         if(localidentityid!=-1 && queryvars.find("formaction")!=queryvars.end() && (*queryvars.find("formaction")).second=="update")\r
152         {\r
153                 std::vector<std::string> identityids;\r
154                 std::vector<std::string> oldlmt;\r
155                 std::vector<std::string> lmt;\r
156                 std::vector<std::string> oldltlt;\r
157                 std::vector<std::string> ltlt;\r
158                 std::vector<std::string> oldmtc;\r
159                 std::vector<std::string> mtc;\r
160                 std::vector<std::string> oldtltc;\r
161                 std::vector<std::string> tltc;\r
162                 int localmessagetrust=0;\r
163                 int localtrustlisttrust=0;\r
164                 int identityid=-1;\r
165 \r
166                 CreateArgArray(queryvars,"identityid",identityids);\r
167                 CreateArgArray(queryvars,"oldlocalmessagetrust",oldlmt);\r
168                 CreateArgArray(queryvars,"localmessagetrust",lmt);\r
169                 CreateArgArray(queryvars,"oldlocaltrustlisttrust",oldltlt);\r
170                 CreateArgArray(queryvars,"localtrustlisttrust",ltlt);\r
171                 CreateArgArray(queryvars,"oldmessagetrustcomment",oldmtc);\r
172                 CreateArgArray(queryvars,"messagetrustcomment",mtc);\r
173                 CreateArgArray(queryvars,"oldtrustlisttrustcomment",oldtltc);\r
174                 CreateArgArray(queryvars,"trustlisttrustcomment",tltc);\r
175 \r
176                 SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) VALUES(?,?);");\r
177                 SQLite3DB::Statement update=m_db->Prepare("UPDATE tblIdentityTrust SET LocalMessageTrust=?, LocalTrustListTrust=?, MessageTrustComment=?, TrustListTrustComment=? WHERE LocalIdentityID=? AND IdentityID=?;");\r
178 \r
179                 for(int i=0; i<identityids.size(); i++)\r
180                 {\r
181                         if(oldlmt[i]!=lmt[i] || oldltlt[i]!=ltlt[i] || oldmtc[i]!=mtc[i] || oldtltc[i]!=tltc[i])\r
182                         {\r
183                                 StringFunctions::Convert(lmt[i],localmessagetrust);\r
184                                 StringFunctions::Convert(ltlt[i],localtrustlisttrust);\r
185                                 StringFunctions::Convert(identityids[i],identityid);\r
186 \r
187                                 ins.Bind(0,localidentityid);\r
188                                 ins.Bind(1,identityid);\r
189                                 ins.Step();\r
190                                 ins.Reset();\r
191 \r
192                                 if(lmt[i]!="")\r
193                                 {\r
194                                         update.Bind(0,localmessagetrust);\r
195                                 }\r
196                                 else\r
197                                 {\r
198                                         update.Bind(0);\r
199                                 }\r
200                                 if(ltlt[i]!="")\r
201                                 {\r
202                                         update.Bind(1,localtrustlisttrust);\r
203                                 }\r
204                                 else\r
205                                 {\r
206                                         update.Bind(1);\r
207                                 }\r
208                                 update.Bind(2,mtc[i]);\r
209                                 update.Bind(3,tltc[i]);\r
210                                 update.Bind(4,localidentityid);\r
211                                 update.Bind(5,identityid);\r
212                                 update.Step();\r
213                                 update.Reset();\r
214                         }\r
215                 }\r
216 \r
217         }\r
218 \r
219         // if startrow is specified\r
220         if(queryvars.find("startrow")!=queryvars.end())\r
221         {\r
222                 startrowstr=(*queryvars.find("startrow")).second;\r
223                 // convert back and forth, just in case a number wasn't passed in startrow\r
224                 StringFunctions::Convert(startrowstr,startrow);\r
225                 if(startrow<0)\r
226                 {\r
227                         startrow=0;\r
228                 }\r
229                 StringFunctions::Convert(startrow,startrowstr);\r
230         }\r
231 \r
232         // sort by\r
233         if(queryvars.find("sortby")!=queryvars.end())\r
234         {\r
235                 sortby=(*queryvars.find("sortby")).second;\r
236         }\r
237         else\r
238         {\r
239                 sortby="Name";\r
240         }\r
241 \r
242         // sort order\r
243         if(queryvars.find("sortorder")!=queryvars.end())\r
244         {\r
245                 sortorder=(*queryvars.find("sortorder")).second;\r
246         }\r
247         else\r
248         {\r
249                 sortorder="ASC";\r
250         }\r
251 \r
252         // if we are searching by name\r
253         if(queryvars.find("namesearch")!=queryvars.end())\r
254         {\r
255                 namesearch=(*queryvars.find("namesearch")).second;\r
256         }\r
257 \r
258         content+="<h2>Peer Trust</h2>";\r
259         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
260         content+="* - This identity is not publishing a trust list<br>";\r
261 \r
262         // search drop down\r
263         content+="<div style=\"text-align:center;margin-bottom:5px;\">";\r
264         content+="<form name=\"frmsearch\" method=\"POST\" action=\"peertrust.htm?"+BuildQueryString(0,"","","",localidentityid)+"\">";\r
265         content+="<input type=\"text\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";\r
266         content+="<input type=\"submit\" value=\"Search\">";\r
267         content+="</form>";\r
268         content+="</div>";\r
269 \r
270         content+="<div style=\"text-align:center;\">";\r
271         content+="<form name=\"frmlocalidentity\" method=\"POST\" action=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,sortby,sortorder,-1)+"\">";\r
272         content+="Load Trust List of ";\r
273         content+=CreateLocalIdentityDropDown("localidentityid",localidentityid);\r
274         content+="<input type=\"submit\" value=\"Load List\">";\r
275         content+="</form>";\r
276         content+="</div>";\r
277 \r
278         content+="<form name=\"frmtrust\" method=\"POST\">";\r
279         content+="<input type=\"hidden\" name=\"formaction\" value=\"update\">";\r
280         content+="<input type=\"hidden\" name=\"localidentityid\" value=\""+localidentityidstr+"\">";\r
281         content+="<input type=\"hidden\" name=\"startrow\" value=\""+startrowstr+"\">";\r
282         if(namesearch!="")\r
283         {\r
284                 content+="<input type=\"hidden\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";\r
285         }\r
286         content+="<table class=\"small90\">";\r
287         content+="<tr><th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"Name",ReverseSort("Name",sortby,sortorder),localidentityid)+"\">Name</a></th>";\r
288         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalMessageTrust",ReverseSort("tblIdentityTrust.LocalMessageTrust",sortby,sortorder),localidentityid)+"\">Local Message Trust</a></th>";\r
289         content+="<th>Message Comment</th>";\r
290         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerMessageTrust",ReverseSort("PeerMessageTrust",sortby,sortorder),localidentityid)+"\">Peer Message Trust</a></th>";\r
291         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalTrustListTrust",ReverseSort("tblIdentityTrust.LocalTrustListTrust",sortby,sortorder),localidentityid)+"\">Local Trust List Trust</a></th>";\r
292         content+="<th>Trust Comment</th>";\r
293         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerTrustListTrust",ReverseSort("PeerTrustListTrust",sortby,sortorder),localidentityid)+"\">Peer Trust List Trust</a></th>";\r
294         content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"MessageCount",ReverseSort("MessageCount",sortby,sortorder),localidentityid)+"\">Message Count</a></th>";\r
295         content+="</tr>\r\n";\r
296         \r
297         // get count of identities we are showing\r
298         sql="SELECT COUNT(*) FROM tblIdentity LEFT JOIN (SELECT IdentityID FROM tblIdentityTrust WHERE LocalIdentityID=?) AS 'tblIdentityTrust' ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID ";\r
299         sql+="WHERE tblIdentity.Hidden='false'";\r
300         if(namesearch!="")\r
301         {\r
302                 sql+=" AND (Name LIKE '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";\r
303         }\r
304         sql+=";";\r
305         SQLite3DB::Statement st=m_db->Prepare(sql);\r
306         st.Bind(0,localidentityid);\r
307         if(namesearch!="")\r
308         {\r
309                 st.Bind(1,namesearch);\r
310                 st.Bind(2,namesearch);\r
311         }\r
312         st.Step();\r
313         st.ResultInt(0,identitycount);\r
314         st.Finalize();\r
315 \r
316         sql="SELECT tblIdentity.IdentityID,Name,tblIdentityTrust.LocalMessageTrust,PeerMessageTrust,tblIdentityTrust.LocalTrustListTrust,PeerTrustListTrust,PublicKey,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment,COUNT(MessageID) AS 'MessageCount',tblIdentity.PublishTrustList ";\r
317         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
318         sql+="WHERE tblIdentity.Hidden='false'";\r
319         if(namesearch!="")\r
320         {\r
321                 sql+=" AND (Name LIKE  '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";\r
322         }\r
323         sql+=" GROUP BY tblIdentity.IdentityID";\r
324         sql+=" ORDER BY";\r
325         if(sortby=="Name")\r
326         {\r
327                 sql+=" Name COLLATE NOCASE";\r
328         }\r
329         else\r
330         {\r
331                 sql+=" "+sortby;\r
332         }\r
333         if(sortorder!="")\r
334         {\r
335                 sql+=" "+sortorder;\r
336         }\r
337         sql+=" LIMIT "+startrowstr+","+rowsperpagestr+";";\r
338         st=m_db->Prepare(sql);\r
339         st.Bind(0,localidentityid);\r
340         if(namesearch!="")\r
341         {\r
342                 st.Bind(1,namesearch);\r
343                 st.Bind(2,namesearch);\r
344         }\r
345         st.Step();\r
346 \r
347         while(st.RowReturned())\r
348         {\r
349                 std::string identityid;\r
350                 std::string name;\r
351                 std::string localmessagetrust;\r
352                 std::string peermessagetrust;\r
353                 std::string localtrustlisttrust;\r
354                 std::string peertrustlisttrust;\r
355                 std::string publickey;\r
356                 std::string messagetrustcomment="";\r
357                 std::string trustlisttrustcomment="";\r
358                 std::string messagecountstr="";\r
359                 std::string publishtrustlist="";\r
360 \r
361                 StringFunctions::Convert(count,countstr);\r
362 \r
363                 st.ResultText(0,identityid);\r
364                 st.ResultText(1,name);\r
365                 st.ResultText(2,localmessagetrust);\r
366                 st.ResultText(3,peermessagetrust);\r
367                 st.ResultText(4,localtrustlisttrust);\r
368                 st.ResultText(5,peertrustlisttrust);\r
369                 st.ResultText(6,publickey);\r
370                 st.ResultText(7,messagetrustcomment);\r
371                 st.ResultText(8,trustlisttrustcomment);\r
372                 st.ResultText(9,messagecountstr);\r
373                 st.ResultText(10,publishtrustlist);\r
374 \r
375                 content+="<tr>";\r
376                 content+="<td title=\""+publickey+"\">";\r
377                 content+="<input type=\"hidden\" name=\"identityid["+countstr+"]\" value=\""+identityid+"\">";\r
378                 content+="<a href=\"peerdetails.htm?identityid="+identityid+"\">";\r
379                 if(name!="")\r
380                 {\r
381                         content+=SanitizeOutput(CreateShortIdentityName(name,publickey));\r
382                 }\r
383                 else\r
384                 {\r
385                         content+="[Unknown Name]";\r
386                 }\r
387                 content+="</a>";\r
388                 content+="</td>";\r
389                 content+="<td "+GetClassString(localmessagetrust)+">";\r
390                 content+="<input type=\"hidden\" name=\"oldlocalmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\">";\r
391                 content+="<input type=\"text\" name=\"localmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\"></td>";\r
392                 content+="<td "+GetClassString(localmessagetrust)+">";\r
393                 content+="<input type=\"hidden\" name=\"oldmessagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\">";\r
394                 content+="<input type=\"text\" name=\"messagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\" maxlength=\"50\" class=\"small90\">";\r
395                 content+="</td>";               \r
396                 content+="<td "+GetClassString(peermessagetrust)+">";\r
397                 content+=peermessagetrust+"</td>";\r
398                 content+="<td "+GetClassString(localtrustlisttrust)+">";\r
399                 content+="<input type=\"hidden\" name=\"oldlocaltrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\">";\r
400                 content+="<input type=\"text\" name=\"localtrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\">";\r
401                 if(publishtrustlist=="false")\r
402                 {\r
403                         content+="*";\r
404                 }\r
405                 content+="</td>";\r
406                 content+="<td "+GetClassString(localtrustlisttrust)+">";\r
407                 content+="<input type=\"hidden\" name=\"oldtrustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\">";\r
408                 content+="<input type=\"text\" name=\"trustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\" maxlength=\"50\" class=\"small90\">";\r
409                 content+="</td>";\r
410                 content+="<td "+GetClassString(peertrustlisttrust)+">";\r
411                 content+=peertrustlisttrust+"</td>";\r
412                 content+="<td>"+messagecountstr+"</td>";\r
413                 content+="</tr>\r\n";\r
414                 st.Step();\r
415                 count++;\r
416         }\r
417         \r
418         if(startrow>0 || startrow+rowsperpage<identitycount)\r
419         {\r
420                 std::string tempstr;\r
421                 int cols=0;\r
422 \r
423                 content+="<tr>";\r
424                 if(startrow>0)\r
425                 {\r
426                         StringFunctions::Convert(startrow-rowsperpage,tempstr);\r
427                         content+="<td colspan=\"3\" align=\"left\"><a href=\"peertrust.htm?"+BuildQueryString(startrow-rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\"><-- Previous Page</a></td>";\r
428                         cols+=3;\r
429                 }\r
430                 if(startrow+rowsperpage<identitycount)\r
431                 {\r
432                         while(cols<5)\r
433                         {\r
434                                 content+="<td></td>";\r
435                                 cols++;\r
436                         }\r
437                         content+="<td colspan=\"3\" align=\"right\"><a href=\"peertrust.htm?"+BuildQueryString(startrow+rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\">Next Page --></a></td>";\r
438                 }\r
439                 content+="</tr>";\r
440         }\r
441 \r
442         content+="<tr><td colspan=\"8\"><center><input type=\"submit\" value=\"Update Trust\"></center></td></tr>";\r
443         content+="</table>";\r
444         content+="</form>";\r
445 \r
446         return "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"+StringFunctions::Replace(m_template,"[CONTENT]",content);\r
447 }\r
448 \r
449 const std::string PeerTrustPage::GetClassString(const std::string &trustlevel)\r
450 {\r
451         int tempint=0;\r
452         std::string tempstr;\r
453 \r
454         StringFunctions::Convert(trustlevel,tempint);\r
455         tempint/=10;\r
456         StringFunctions::Convert(tempint,tempstr);\r
457 \r
458         if(trustlevel!="")\r
459         {\r
460                 return "class=\"trust"+tempstr+"\"";\r
461         }\r
462         else\r
463         {\r
464                 return "";\r
465         }\r
466 }\r
467 \r
468 const std::string PeerTrustPage::ReverseSort(const std::string &sortname, const std::string &currentsortby, const std::string &currentsortorder)\r
469 {\r
470         if(sortname==currentsortby)\r
471         {\r
472                 if(currentsortorder=="ASC")\r
473                 {\r
474                         return "DESC";\r
475                 }\r
476                 else\r
477                 {\r
478                         return "ASC";\r
479                 }\r
480         }\r
481         else\r
482         {\r
483                 return currentsortorder;\r
484         }\r
485 }\r
486 \r
487 const bool PeerTrustPage::WillHandleURI(const std::string &uri)\r
488 {\r
489         if(uri.find("peertrust.")!=std::string::npos)\r
490         {\r
491                 return true;\r
492         }\r
493         else\r
494         {\r
495                 return false;\r
496         }\r
497 }\r