1 #include "../../../include/http/pages/peertrustpage.h"
\r
2 #include "../../../include/stringfunctions.h"
\r
3 #include "../../../include/global.h"
\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
11 std::string returnval="";
\r
12 std::string tempval="";
\r
16 StringFunctions::Convert(startrow,tempval);
\r
17 returnval+="startrow="+tempval;
\r
26 returnval+="namesearch="+namesearch;
\r
35 returnval+="sortby="+sortby;
\r
44 returnval+="sortorder="+sortorder;
\r
47 if(localidentityid>=0)
\r
49 std::string localidentityidstr="";
\r
50 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
55 returnval+="localidentityid="+localidentityidstr;
\r
62 const std::string PeerTrustPage::CreateLocalIdentityDropDown(const std::string &name, const int selectedlocalidentityid)
\r
64 std::string result="";
\r
66 result+="<select name=\""+name+"\">";
\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
71 while(st.RowReturned())
\r
73 int localidentityid=-1;
\r
74 std::string localidentityidstr="";
\r
75 std::string name="";
\r
76 std::string publickey="";
\r
78 st.ResultInt(0,localidentityid);
\r
79 st.ResultText(1,name);
\r
80 st.ResultText(2,publickey);
\r
82 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
84 result+="<option value=\""+localidentityidstr+"\"";
\r
85 if(localidentityid==selectedlocalidentityid)
\r
87 result+=" SELECTED";
\r
89 result+=">"+SanitizeOutput(CreateShortIdentityName(name,publickey))+"</option>";
\r
93 result+="</select>";
\r
98 const std::string PeerTrustPage::GeneratePage(const std::string &method, const std::map<std::string,std::string> &queryvars)
\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
107 std::string startrowstr="0";
\r
108 std::string namesearch="";
\r
110 std::string sortby="";
\r
111 std::string sortorder="";
\r
112 std::string localidentityidstr="";
\r
113 int localidentityid=-1;
\r
115 StringFunctions::Convert(rowsperpage,rowsperpagestr);
\r
117 // get localidentityid from querystring or load one from the database
\r
118 if(queryvars.find("localidentityid")!=queryvars.end())
\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
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
133 if(st.RowReturned())
\r
135 st.ResultInt(0,localidentityid);
\r
136 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
140 st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity;");
\r
142 if(st.RowReturned())
\r
144 st.ResultInt(0,localidentityid);
\r
145 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
151 if(localidentityid!=-1 && queryvars.find("formaction")!=queryvars.end() && (*queryvars.find("formaction")).second=="update" && ValidateFormPassword(queryvars))
\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
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
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
179 for(int i=0; i<identityids.size(); i++)
\r
181 if(oldlmt[i]!=lmt[i] || oldltlt[i]!=ltlt[i] || oldmtc[i]!=mtc[i] || oldtltc[i]!=tltc[i])
\r
183 StringFunctions::Convert(lmt[i],localmessagetrust);
\r
184 StringFunctions::Convert(ltlt[i],localtrustlisttrust);
\r
185 StringFunctions::Convert(identityids[i],identityid);
\r
187 ins.Bind(0,localidentityid);
\r
188 ins.Bind(1,identityid);
\r
194 update.Bind(0,localmessagetrust);
\r
202 update.Bind(1,localtrustlisttrust);
\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
219 // if startrow is specified
\r
220 if(queryvars.find("startrow")!=queryvars.end())
\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
229 StringFunctions::Convert(startrow,startrowstr);
\r
233 if(queryvars.find("sortby")!=queryvars.end())
\r
235 sortby=(*queryvars.find("sortby")).second;
\r
243 if(queryvars.find("sortorder")!=queryvars.end())
\r
245 sortorder=(*queryvars.find("sortorder")).second;
\r
252 // if we are searching by name
\r
253 if(queryvars.find("namesearch")!=queryvars.end())
\r
255 namesearch=(*queryvars.find("namesearch")).second;
\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
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
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
278 content+="<form name=\"frmtrust\" method=\"POST\">";
\r
279 content+=CreateFormPassword();
\r
280 content+="<input type=\"hidden\" name=\"formaction\" value=\"update\">";
\r
281 content+="<input type=\"hidden\" name=\"localidentityid\" value=\""+localidentityidstr+"\">";
\r
282 content+="<input type=\"hidden\" name=\"startrow\" value=\""+startrowstr+"\">";
\r
285 content+="<input type=\"hidden\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";
\r
287 content+="<table class=\"small90\">";
\r
288 content+="<tr><th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"Name",ReverseSort("Name",sortby,sortorder),localidentityid)+"\">Name</a></th>";
\r
289 content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalMessageTrust",ReverseSort("tblIdentityTrust.LocalMessageTrust",sortby,sortorder),localidentityid)+"\">Local Message Trust</a></th>";
\r
290 content+="<th>Message Comment</th>";
\r
291 content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerMessageTrust",ReverseSort("PeerMessageTrust",sortby,sortorder),localidentityid)+"\">Peer Message Trust</a></th>";
\r
292 content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalTrustListTrust",ReverseSort("tblIdentityTrust.LocalTrustListTrust",sortby,sortorder),localidentityid)+"\">Local Trust List Trust</a></th>";
\r
293 content+="<th>Trust Comment</th>";
\r
294 content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerTrustListTrust",ReverseSort("PeerTrustListTrust",sortby,sortorder),localidentityid)+"\">Peer Trust List Trust</a></th>";
\r
295 content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"MessageCount",ReverseSort("MessageCount",sortby,sortorder),localidentityid)+"\">Message Count</a></th>";
\r
296 content+="</tr>\r\n";
\r
298 // get count of identities we are showing
\r
299 sql="SELECT COUNT(*) FROM tblIdentity LEFT JOIN tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID ";
\r
300 sql+="WHERE tblIdentityTrust.LocalIdentityID=? AND tblIdentity.Hidden='false'";
\r
303 sql+=" AND (Name LIKE '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";
\r
306 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
307 st.Bind(0,localidentityid);
\r
310 st.Bind(1,namesearch);
\r
311 st.Bind(2,namesearch);
\r
314 st.ResultInt(0,identitycount);
\r
317 sql="SELECT tblIdentity.IdentityID,Name,tblIdentityTrust.LocalMessageTrust,PeerMessageTrust,tblIdentityTrust.LocalTrustListTrust,PeerTrustListTrust,PublicKey,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment,COUNT(MessageID) AS 'MessageCount',tblIdentity.PublishTrustList ";
\r
318 sql+="FROM tblIdentity LEFT JOIN tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID LEFT JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID ";
\r
319 sql+="WHERE tblIdentityTrust.LocalIdentityID=? AND tblIdentity.Hidden='false'";
\r
322 sql+=" AND (Name LIKE '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";
\r
324 sql+=" GROUP BY tblIdentity.IdentityID";
\r
328 sql+=" Name COLLATE NOCASE";
\r
336 sql+=" "+sortorder;
\r
338 sql+=" LIMIT "+startrowstr+","+rowsperpagestr+";";
\r
339 st=m_db->Prepare(sql);
\r
340 st.Bind(0,localidentityid);
\r
343 st.Bind(1,namesearch);
\r
344 st.Bind(2,namesearch);
\r
348 while(st.RowReturned())
\r
350 std::string identityid;
\r
352 std::string localmessagetrust;
\r
353 std::string peermessagetrust;
\r
354 std::string localtrustlisttrust;
\r
355 std::string peertrustlisttrust;
\r
356 std::string publickey;
\r
357 std::string messagetrustcomment="";
\r
358 std::string trustlisttrustcomment="";
\r
359 std::string messagecountstr="";
\r
360 std::string publishtrustlist="";
\r
362 StringFunctions::Convert(count,countstr);
\r
364 st.ResultText(0,identityid);
\r
365 st.ResultText(1,name);
\r
366 st.ResultText(2,localmessagetrust);
\r
367 st.ResultText(3,peermessagetrust);
\r
368 st.ResultText(4,localtrustlisttrust);
\r
369 st.ResultText(5,peertrustlisttrust);
\r
370 st.ResultText(6,publickey);
\r
371 st.ResultText(7,messagetrustcomment);
\r
372 st.ResultText(8,trustlisttrustcomment);
\r
373 st.ResultText(9,messagecountstr);
\r
374 st.ResultText(10,publishtrustlist);
\r
377 content+="<td title=\""+publickey+"\">";
\r
378 content+="<input type=\"hidden\" name=\"identityid["+countstr+"]\" value=\""+identityid+"\">";
\r
379 content+="<a href=\"peerdetails.htm?identityid="+identityid+"\">";
\r
382 content+=SanitizeOutput(CreateShortIdentityName(name,publickey));
\r
386 content+="[Unknown Name]";
\r
390 content+="<td "+GetClassString(localmessagetrust)+">";
\r
391 content+="<input type=\"hidden\" name=\"oldlocalmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\">";
\r
392 content+="<input type=\"text\" name=\"localmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\"></td>";
\r
393 content+="<td "+GetClassString(localmessagetrust)+">";
\r
394 content+="<input type=\"hidden\" name=\"oldmessagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\">";
\r
395 content+="<input type=\"text\" name=\"messagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\" maxlength=\"50\" class=\"small90\">";
\r
397 content+="<td "+GetClassString(peermessagetrust)+">";
\r
398 content+=peermessagetrust+"</td>";
\r
399 content+="<td "+GetClassString(localtrustlisttrust)+">";
\r
400 content+="<input type=\"hidden\" name=\"oldlocaltrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\">";
\r
401 content+="<input type=\"text\" name=\"localtrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\">";
\r
402 if(publishtrustlist=="false")
\r
407 content+="<td "+GetClassString(localtrustlisttrust)+">";
\r
408 content+="<input type=\"hidden\" name=\"oldtrustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\">";
\r
409 content+="<input type=\"text\" name=\"trustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\" maxlength=\"50\" class=\"small90\">";
\r
411 content+="<td "+GetClassString(peertrustlisttrust)+">";
\r
412 content+=peertrustlisttrust+"</td>";
\r
413 content+="<td>"+messagecountstr+"</td>";
\r
414 content+="</tr>\r\n";
\r
419 if(startrow>0 || startrow+rowsperpage<identitycount)
\r
421 std::string tempstr;
\r
427 StringFunctions::Convert(startrow-rowsperpage,tempstr);
\r
428 content+="<td colspan=\"3\" align=\"left\"><a href=\"peertrust.htm?"+BuildQueryString(startrow-rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\"><-- Previous Page</a></td>";
\r
431 if(startrow+rowsperpage<identitycount)
\r
435 content+="<td></td>";
\r
438 content+="<td colspan=\"3\" align=\"right\"><a href=\"peertrust.htm?"+BuildQueryString(startrow+rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\">Next Page --></a></td>";
\r
443 content+="<tr><td colspan=\"8\"><center><input type=\"submit\" value=\"Update Trust\"></center></td></tr>";
\r
444 content+="</table>";
\r
445 content+="</form>";
\r
447 return StringFunctions::Replace(m_template,"[CONTENT]",content);
\r
450 const std::string PeerTrustPage::GetClassString(const std::string &trustlevel)
\r
453 std::string tempstr;
\r
455 StringFunctions::Convert(trustlevel,tempint);
\r
457 StringFunctions::Convert(tempint,tempstr);
\r
461 return "class=\"trust"+tempstr+"\"";
\r
469 const std::string PeerTrustPage::ReverseSort(const std::string &sortname, const std::string ¤tsortby, const std::string ¤tsortorder)
\r
471 if(sortname==currentsortby)
\r
473 if(currentsortorder=="ASC")
\r
484 return currentsortorder;
\r
488 const bool PeerTrustPage::WillHandleURI(const std::string &uri)
\r
490 if(uri.find("peertrust.")!=std::string::npos)
\r