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="+StringFunctions::UriEncode(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
236 if(sortby!="Name" && sortby!="tblIdentityTrust.LocalMessageTrust" && sortby!="PeerMessageTrust" && sortby!="tblIdentityTrust.LocalTrustListTrust" && sortby!="PeerTrustListTrust" && sortby!="MessageCount")
\r
247 if(queryvars.find("sortorder")!=queryvars.end())
\r
249 sortorder=(*queryvars.find("sortorder")).second;
\r
250 if(sortorder!="ASC" && sortorder!="DESC")
\r
260 // if we are searching by name
\r
261 if(queryvars.find("namesearch")!=queryvars.end())
\r
263 namesearch=(*queryvars.find("namesearch")).second;
\r
266 content+="<h2>Peer Trust</h2>";
\r
267 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
268 content+="* - This identity is not publishing a trust list<br>";
\r
270 // search drop down
\r
271 content+="<div style=\"text-align:center;margin-bottom:5px;\">";
\r
272 content+="<form name=\"frmsearch\" method=\"POST\" action=\""+m_pagename+"?"+BuildQueryString(0,"","","",localidentityid)+"\">";
\r
273 content+="<input type=\"text\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";
\r
274 content+="<input type=\"submit\" value=\"Search\">";
\r
275 content+="</form>";
\r
278 content+="<div style=\"text-align:center;\">";
\r
279 content+="<form name=\"frmlocalidentity\" method=\"POST\" action=\""+m_pagename+"?"+BuildQueryString(startrow,namesearch,sortby,sortorder,-1)+"\">";
\r
280 content+="Load Trust List of ";
\r
281 content+=CreateLocalIdentityDropDown("localidentityid",localidentityid);
\r
282 content+="<input type=\"submit\" value=\"Load List\">";
\r
283 content+="</form>";
\r
286 content+="<form name=\"frmtrust\" method=\"POST\">";
\r
287 content+=CreateFormPassword();
\r
288 content+="<input type=\"hidden\" name=\"formaction\" value=\"update\">";
\r
289 content+="<input type=\"hidden\" name=\"localidentityid\" value=\""+localidentityidstr+"\">";
\r
290 content+="<input type=\"hidden\" name=\"startrow\" value=\""+startrowstr+"\">";
\r
293 content+="<input type=\"hidden\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";
\r
295 content+="<table class=\"small90\">";
\r
296 content+="<tr><th><a href=\""+m_pagename+"?"+BuildQueryString(startrow,namesearch,"Name",ReverseSort("Name",sortby,sortorder),localidentityid)+"\">Name</a></th>";
\r
297 content+="<th><a href=\""+m_pagename+"?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalMessageTrust",ReverseSort("tblIdentityTrust.LocalMessageTrust",sortby,sortorder),localidentityid)+"\">Local Message Trust</a></th>";
\r
298 content+="<th>Message Comment</th>";
\r
299 content+="<th><a href=\""+m_pagename+"?"+BuildQueryString(startrow,namesearch,"PeerMessageTrust",ReverseSort("PeerMessageTrust",sortby,sortorder),localidentityid)+"\">Peer Message Trust</a></th>";
\r
300 content+="<th><a href=\""+m_pagename+"?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalTrustListTrust",ReverseSort("tblIdentityTrust.LocalTrustListTrust",sortby,sortorder),localidentityid)+"\">Local Trust List Trust</a></th>";
\r
301 content+="<th>Trust Comment</th>";
\r
302 content+="<th><a href=\""+m_pagename+"?"+BuildQueryString(startrow,namesearch,"PeerTrustListTrust",ReverseSort("PeerTrustListTrust",sortby,sortorder),localidentityid)+"\">Peer Trust List Trust</a></th>";
\r
303 content+="<th><a href=\""+m_pagename+"?"+BuildQueryString(startrow,namesearch,"MessageCount",ReverseSort("MessageCount",sortby,sortorder),localidentityid)+"\">Message Count</a></th>";
\r
304 content+="</tr>\r\n";
\r
306 // get count of identities we are showing
\r
307 sql="SELECT COUNT(*) FROM tblIdentity LEFT JOIN tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID ";
\r
308 sql+="WHERE tblIdentityTrust.LocalIdentityID=? AND tblIdentity.Hidden='false'";
\r
311 sql+=" AND (Name LIKE '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";
\r
314 SQLite3DB::Statement st=m_db->Prepare(sql);
\r
315 st.Bind(0,localidentityid);
\r
318 st.Bind(1,namesearch);
\r
319 st.Bind(2,namesearch);
\r
322 st.ResultInt(0,identitycount);
\r
325 sql="SELECT tblIdentity.IdentityID,Name,tblIdentityTrust.LocalMessageTrust,PeerMessageTrust,tblIdentityTrust.LocalTrustListTrust,PeerTrustListTrust,PublicKey,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment,COUNT(MessageID) AS 'MessageCount',tblIdentity.PublishTrustList ";
\r
326 sql+="FROM tblIdentity LEFT JOIN tblIdentityTrust ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID LEFT JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID ";
\r
327 sql+="WHERE tblIdentityTrust.LocalIdentityID=? AND tblIdentity.Hidden='false'";
\r
330 sql+=" AND (Name LIKE '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";
\r
332 sql+=" GROUP BY tblIdentity.IdentityID";
\r
336 sql+=" Name COLLATE NOCASE";
\r
344 sql+=" "+sortorder;
\r
346 sql+=" LIMIT "+startrowstr+","+rowsperpagestr+";";
\r
347 st=m_db->Prepare(sql);
\r
348 st.Bind(0,localidentityid);
\r
351 st.Bind(1,namesearch);
\r
352 st.Bind(2,namesearch);
\r
356 while(st.RowReturned())
\r
358 std::string identityid;
\r
360 std::string localmessagetrust;
\r
361 std::string peermessagetrust;
\r
362 std::string localtrustlisttrust;
\r
363 std::string peertrustlisttrust;
\r
364 std::string publickey;
\r
365 std::string messagetrustcomment="";
\r
366 std::string trustlisttrustcomment="";
\r
367 std::string messagecountstr="";
\r
368 std::string publishtrustlist="";
\r
370 StringFunctions::Convert(count,countstr);
\r
372 st.ResultText(0,identityid);
\r
373 st.ResultText(1,name);
\r
374 st.ResultText(2,localmessagetrust);
\r
375 st.ResultText(3,peermessagetrust);
\r
376 st.ResultText(4,localtrustlisttrust);
\r
377 st.ResultText(5,peertrustlisttrust);
\r
378 st.ResultText(6,publickey);
\r
379 st.ResultText(7,messagetrustcomment);
\r
380 st.ResultText(8,trustlisttrustcomment);
\r
381 st.ResultText(9,messagecountstr);
\r
382 st.ResultText(10,publishtrustlist);
\r
385 content+="<td title=\""+publickey+"\">";
\r
386 content+="<input type=\"hidden\" name=\"identityid["+countstr+"]\" value=\""+identityid+"\">";
\r
387 content+="<a href=\"peerdetails.htm?identityid="+identityid+"\">";
\r
390 content+=SanitizeOutput(CreateShortIdentityName(name,publickey));
\r
394 content+="[Unknown Name]";
\r
398 content+="<td "+GetClassString(localmessagetrust)+">";
\r
399 content+="<input type=\"hidden\" name=\"oldlocalmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\">";
\r
400 content+="<input type=\"text\" name=\"localmessagetrust["+countstr+"]\" value=\""+localmessagetrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\"></td>";
\r
401 content+="<td "+GetClassString(localmessagetrust)+">";
\r
402 content+="<input type=\"hidden\" name=\"oldmessagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\">";
\r
403 content+="<input type=\"text\" name=\"messagetrustcomment["+countstr+"]\" value=\""+SanitizeOutput(messagetrustcomment)+"\" maxlength=\"50\" class=\"small90\">";
\r
405 content+="<td "+GetClassString(peermessagetrust)+">";
\r
406 content+=peermessagetrust+"</td>";
\r
407 content+="<td "+GetClassString(localtrustlisttrust)+">";
\r
408 content+="<input type=\"hidden\" name=\"oldlocaltrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\">";
\r
409 content+="<input type=\"text\" name=\"localtrustlisttrust["+countstr+"]\" value=\""+localtrustlisttrust+"\" size=\"2\" maxlength=\"3\" class=\"small90\">";
\r
410 if(publishtrustlist=="false")
\r
415 content+="<td "+GetClassString(localtrustlisttrust)+">";
\r
416 content+="<input type=\"hidden\" name=\"oldtrustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\">";
\r
417 content+="<input type=\"text\" name=\"trustlisttrustcomment["+countstr+"]\" value=\""+SanitizeOutput(trustlisttrustcomment)+"\" maxlength=\"50\" class=\"small90\">";
\r
419 content+="<td "+GetClassString(peertrustlisttrust)+">";
\r
420 content+=peertrustlisttrust+"</td>";
\r
421 content+="<td>"+messagecountstr+"</td>";
\r
422 content+="</tr>\r\n";
\r
427 if(startrow>0 || startrow+rowsperpage<identitycount)
\r
429 std::string tempstr;
\r
435 StringFunctions::Convert(startrow-rowsperpage,tempstr);
\r
436 content+="<td colspan=\"3\" align=\"left\"><a href=\""+m_pagename+"?"+BuildQueryString(startrow-rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\"><-- Previous Page</a></td>";
\r
439 if(startrow+rowsperpage<identitycount)
\r
443 content+="<td></td>";
\r
446 content+="<td colspan=\"3\" align=\"right\"><a href=\""+m_pagename+"?"+BuildQueryString(startrow+rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\">Next Page --></a></td>";
\r
451 content+="<tr><td colspan=\"8\"><center><input type=\"submit\" value=\"Update Trust\"></center></td></tr>";
\r
452 content+="</table>";
\r
453 content+="</form>";
\r
455 return StringFunctions::Replace(m_template,"[CONTENT]",content);
\r
458 const std::string PeerTrustPage::GetClassString(const std::string &trustlevel)
\r
461 std::string tempstr;
\r
463 StringFunctions::Convert(trustlevel,tempint);
\r
465 StringFunctions::Convert(tempint,tempstr);
\r
469 return "class=\"trust"+tempstr+"\"";
\r
477 const std::string PeerTrustPage::ReverseSort(const std::string &sortname, const std::string ¤tsortby, const std::string ¤tsortorder)
\r
479 if(sortname==currentsortby)
\r
481 if(currentsortorder=="ASC")
\r
492 return currentsortorder;
\r