version 0.2.7
[fms.git] / src / http / pages / peertrustpage.cpp
index 9429ede..21b0730 100644 (file)
@@ -1,11 +1,12 @@
 #include "../../../include/http/pages/peertrustpage.h"\r
 #include "../../../include/stringfunctions.h"\r
+#include "../../../include/global.h"\r
 \r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
 \r
-const std::string PeerTrustPage::BuildQueryString(const long startrow, const std::string &namesearch, const std::string &sortby, const std::string &sortorder)\r
+const std::string PeerTrustPage::BuildQueryString(const long startrow, const std::string &namesearch, const std::string &sortby, const std::string &sortorder, const int localidentityid)\r
 {\r
        std::string returnval="";\r
        std::string tempval="";\r
@@ -43,10 +44,57 @@ const std::string PeerTrustPage::BuildQueryString(const long startrow, const std
                returnval+="sortorder="+sortorder;\r
        }\r
 \r
+       if(localidentityid>=0)\r
+       {\r
+               std::string localidentityidstr="";\r
+               StringFunctions::Convert(localidentityid,localidentityidstr);\r
+               if(returnval!="")\r
+               {\r
+                       returnval+="&";\r
+               }\r
+               returnval+="localidentityid="+localidentityidstr;\r
+       }\r
+\r
        return returnval;\r
 \r
 }\r
 \r
+const std::string PeerTrustPage::CreateLocalIdentityDropDown(const std::string &name, const int selectedlocalidentityid)\r
+{\r
+       std::string result="";\r
+       \r
+       result+="<select name=\""+name+"\">";\r
+       \r
+       SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID,Name,PublicKey FROM tblLocalIdentity WHERE PublicKey IS NOT NULL ORDER BY Name COLLATE NOCASE;");\r
+       st.Step();\r
+\r
+       while(st.RowReturned())\r
+       {\r
+               int localidentityid=-1;\r
+               std::string localidentityidstr="";\r
+               std::string name="";\r
+               std::string publickey="";\r
+\r
+               st.ResultInt(0,localidentityid);\r
+               st.ResultText(1,name);\r
+               st.ResultText(2,publickey);\r
+\r
+               StringFunctions::Convert(localidentityid,localidentityidstr);\r
+\r
+               result+="<option value=\""+localidentityidstr+"\"";\r
+               if(localidentityid==selectedlocalidentityid)\r
+               {\r
+                       result+=" SELECTED";\r
+               }\r
+               result+=">"+SanitizeOutput(CreateShortIdentityName(name,publickey))+"</option>";\r
+               st.Step();\r
+       }\r
+\r
+       result+="</select>";\r
+\r
+       return result;\r
+}\r
+\r
 const std::string PeerTrustPage::GeneratePage(const std::string &method, const std::map<std::string,std::string> &queryvars)\r
 {\r
        int count=0;\r
@@ -61,9 +109,28 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
        std::string sql;\r
        std::string sortby="";\r
        std::string sortorder="";\r
+       std::string localidentityidstr="";\r
+       int localidentityid=-1;\r
 \r
        StringFunctions::Convert(rowsperpage,rowsperpagestr);\r
 \r
+       // get localidentityid from querystring or load one from the database\r
+       if(queryvars.find("localidentityid")!=queryvars.end())\r
+       {\r
+               localidentityidstr=(*queryvars.find("localidentityid")).second;\r
+               StringFunctions::Convert(localidentityidstr,localidentityid);\r
+       }\r
+       else\r
+       {\r
+               SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity;");\r
+               st.Step();\r
+               if(st.RowReturned())\r
+               {\r
+                       st.ResultInt(0,localidentityid);\r
+                       StringFunctions::Convert(localidentityid,localidentityidstr);\r
+               }\r
+       }\r
+\r
        if(queryvars.find("formaction")!=queryvars.end() && (*queryvars.find("formaction")).second=="update")\r
        {\r
                std::vector<std::string> identityids;\r
@@ -77,7 +144,7 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                std::vector<std::string> tltc;\r
                int localmessagetrust=0;\r
                int localtrustlisttrust=0;\r
-               int identityid;\r
+               int identityid=-1;\r
 \r
                CreateArgArray(queryvars,"identityid",identityids);\r
                CreateArgArray(queryvars,"oldlocalmessagetrust",oldlmt);\r
@@ -88,8 +155,9 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                CreateArgArray(queryvars,"messagetrustcomment",mtc);\r
                CreateArgArray(queryvars,"oldtrustlisttrustcomment",oldtltc);\r
                CreateArgArray(queryvars,"trustlisttrustcomment",tltc);\r
-               \r
-               SQLite3DB::Statement update=m_db->Prepare("UPDATE tblIdentity SET LocalMessageTrust=?, LocalTrustListTrust=?, MessageTrustComment=?, TrustListTrustComment=? WHERE IdentityID=?;");\r
+\r
+               SQLite3DB::Statement ins=m_db->Prepare("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) VALUES(?,?);");\r
+               SQLite3DB::Statement update=m_db->Prepare("UPDATE tblIdentityTrust SET LocalMessageTrust=?, LocalTrustListTrust=?, MessageTrustComment=?, TrustListTrustComment=? WHERE LocalIdentityID=? AND IdentityID=?;");\r
 \r
                for(int i=0; i<identityids.size(); i++)\r
                {\r
@@ -99,6 +167,11 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                                StringFunctions::Convert(ltlt[i],localtrustlisttrust);\r
                                StringFunctions::Convert(identityids[i],identityid);\r
 \r
+                               ins.Bind(0,localidentityid);\r
+                               ins.Bind(1,identityid);\r
+                               ins.Step();\r
+                               ins.Reset();\r
+\r
                                if(lmt[i]!="")\r
                                {\r
                                        update.Bind(0,localmessagetrust);\r
@@ -117,7 +190,8 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                                }\r
                                update.Bind(2,mtc[i]);\r
                                update.Bind(3,tltc[i]);\r
-                               update.Bind(4,identityid);\r
+                               update.Bind(4,localidentityid);\r
+                               update.Bind(5,identityid);\r
                                update.Step();\r
                                update.Reset();\r
                        }\r
@@ -165,59 +239,69 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
        }\r
 \r
        content+="<h2>Peer Trust</h2>";\r
-       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.";\r
-       content+="<div style=\"text-align:center;\">";\r
-       content+="<form name=\"frmsearch\" method=\"POST\" action=\"peertrust.htm\">";\r
+       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.";\r
+       \r
+       // search drop down\r
+       content+="<div style=\"text-align:center;margin-bottom:5px;\">";\r
+       content+="<form name=\"frmsearch\" method=\"POST\" action=\"peertrust.htm?"+BuildQueryString(0,"","","",localidentityid)+"\">";\r
        content+="<input type=\"text\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";\r
        content+="<input type=\"submit\" value=\"Search\">";\r
        content+="</form>";\r
        content+="</div>";\r
+\r
+       content+="<div style=\"text-align:center;\">";\r
+       content+="<form name=\"frmlocalidentity\" method=\"POST\" action=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,sortby,sortorder,-1)+"\">";\r
+       content+="Load Trust List of ";\r
+       content+=CreateLocalIdentityDropDown("localidentityid",localidentityid);\r
+       content+="<input type=\"submit\" value=\"Load List\">";\r
+       content+="</form>";\r
+       content+="</div>";\r
+\r
        content+="<form name=\"frmtrust\" method=\"POST\">";\r
        content+="<input type=\"hidden\" name=\"formaction\" value=\"update\">";\r
+       content+="<input type=\"hidden\" name=\"localidentityid\" value=\""+localidentityidstr+"\">";\r
        content+="<input type=\"hidden\" name=\"startrow\" value=\""+startrowstr+"\">";\r
        if(namesearch!="")\r
        {\r
                content+="<input type=\"hidden\" name=\"namesearch\" value=\""+SanitizeOutput(namesearch)+"\">";\r
        }\r
        content+="<table class=\"small90\">";\r
-       content+="<tr><th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"Name",ReverseSort("Name",sortby,sortorder))+"\">Name</a></th>";\r
-       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"LocalMessageTrust",ReverseSort("LocalMessageTrust",sortby,sortorder))+"\">Local Message Trust</a></th>";\r
+       content+="<tr><th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"Name",ReverseSort("Name",sortby,sortorder),localidentityid)+"\">Name</a></th>";\r
+       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalMessageTrust",ReverseSort("tblIdentityTrust.LocalMessageTrust",sortby,sortorder),localidentityid)+"\">Local Message Trust</a></th>";\r
        content+="<th>Message Comment</th>";\r
-       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerMessageTrust",ReverseSort("PeerMessageTrust",sortby,sortorder))+"\">Peer Message Trust</a></th>";\r
-       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"LocalTrustListTrust",ReverseSort("LocalTrustListTrust",sortby,sortorder))+"\">Local Trust List Trust</a></th>";\r
+       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerMessageTrust",ReverseSort("PeerMessageTrust",sortby,sortorder),localidentityid)+"\">Peer Message Trust</a></th>";\r
+       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"tblIdentityTrust.LocalTrustListTrust",ReverseSort("tblIdentityTrust.LocalTrustListTrust",sortby,sortorder),localidentityid)+"\">Local Trust List Trust</a></th>";\r
        content+="<th>Trust Comment</th>";\r
-       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerTrustListTrust",ReverseSort("PeerTrustListTrust",sortby,sortorder))+"\">Peer Trust List Trust</a></th>";\r
-       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"MessageCount",ReverseSort("MessageCount",sortby,sortorder))+"\">Message Count</a></th>";\r
+       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"PeerTrustListTrust",ReverseSort("PeerTrustListTrust",sortby,sortorder),localidentityid)+"\">Peer Trust List Trust</a></th>";\r
+       content+="<th><a href=\"peertrust.htm?"+BuildQueryString(startrow,namesearch,"MessageCount",ReverseSort("MessageCount",sortby,sortorder),localidentityid)+"\">Message Count</a></th>";\r
        content+="</tr>\r\n";\r
        \r
+//TODO - modify this query\r
        // get count of identities we are showing\r
-       sql="SELECT COUNT(*) FROM tblIdentity";\r
+       sql="SELECT COUNT(*) FROM tblIdentity LEFT JOIN (SELECT IdentityID FROM tblIdentityTrust WHERE LocalIdentityID=?) AS 'tblIdentityTrust' ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID ";\r
+       sql+="WHERE tblIdentity.Hidden='false'";\r
        if(namesearch!="")\r
        {\r
-               sql+=" WHERE Name LIKE '%' || ? || '%' AND tblIdentity.Hidden='false'";\r
-       }\r
-       else\r
-       {\r
-               sql+=" WHERE tblIdentity.Hidden='false'";       \r
+               sql+=" AND (Name LIKE '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";\r
        }\r
        sql+=";";\r
        SQLite3DB::Statement st=m_db->Prepare(sql);\r
+       st.Bind(0,localidentityid);\r
        if(namesearch!="")\r
        {\r
-               st.Bind(0,namesearch);\r
+               st.Bind(1,namesearch);\r
+               st.Bind(2,namesearch);\r
        }\r
        st.Step();\r
        st.ResultInt(0,identitycount);\r
        st.Finalize();\r
 \r
-       sql="SELECT tblIdentity.IdentityID,Name,LocalMessageTrust,PeerMessageTrust,LocalTrustListTrust,PeerTrustListTrust,PublicKey,MessageTrustComment,TrustListTrustComment,COUNT(MessageID) AS 'MessageCount' FROM tblIdentity LEFT JOIN tblMessage ON tblIdentity.IdentityID=tblMessage.IdentityID";\r
+       sql="SELECT tblIdentity.IdentityID,Name,tblIdentityTrust.LocalMessageTrust,PeerMessageTrust,tblIdentityTrust.LocalTrustListTrust,PeerTrustListTrust,PublicKey,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment,COUNT(MessageID) AS 'MessageCount' ";\r
+       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
+       sql+="WHERE tblIdentity.Hidden='false'";\r
        if(namesearch!="")\r
        {\r
-               sql+=" WHERE (Name LIKE  '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%') AND tblIdentity.Hidden='false'";\r
-       }\r
-       else\r
-       {\r
-               sql+=" WHERE tblIdentity.Hidden='false'";       \r
+               sql+=" AND (Name LIKE  '%' || ? || '%' OR PublicKey LIKE '%' || ? || '%')";\r
        }\r
        sql+=" GROUP BY tblIdentity.IdentityID";\r
        sql+=" ORDER BY";\r
@@ -235,10 +319,11 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
        }\r
        sql+=" LIMIT "+startrowstr+","+rowsperpagestr+";";\r
        st=m_db->Prepare(sql);\r
+       st.Bind(0,localidentityid);\r
        if(namesearch!="")\r
        {\r
-               st.Bind(0,namesearch);\r
                st.Bind(1,namesearch);\r
+               st.Bind(2,namesearch);\r
        }\r
        st.Step();\r
 \r
@@ -251,7 +336,6 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                std::string localtrustlisttrust;\r
                std::string peertrustlisttrust;\r
                std::string publickey;\r
-               std::string keypart="";\r
                std::string messagetrustcomment="";\r
                std::string trustlisttrustcomment="";\r
                std::string messagecountstr="";\r
@@ -269,18 +353,13 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                st.ResultText(8,trustlisttrustcomment);\r
                st.ResultText(9,messagecountstr);\r
 \r
-               if(publickey.size()>8)\r
-               {\r
-                       keypart=publickey.substr(3,5);\r
-               }\r
-\r
                content+="<tr>";\r
                content+="<td title=\""+publickey+"\">";\r
                content+="<input type=\"hidden\" name=\"identityid["+countstr+"]\" value=\""+identityid+"\">";\r
                content+="<a href=\"peerdetails.htm?identityid="+identityid+"\">";\r
                if(name!="")\r
                {\r
-                       content+=SanitizeOutput(name+keypart)+"...";\r
+                       content+=SanitizeOutput(CreateShortIdentityName(name,publickey));\r
                }\r
                else\r
                {\r
@@ -321,7 +400,7 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                if(startrow>0)\r
                {\r
                        StringFunctions::Convert(startrow-rowsperpage,tempstr);\r
-                       content+="<td colspan=\"3\" align=\"left\"><a href=\"peertrust.htm?"+BuildQueryString(startrow-rowsperpage,namesearch,sortby,sortorder)+"\"><-- Previous Page</a></td>";\r
+                       content+="<td colspan=\"3\" align=\"left\"><a href=\"peertrust.htm?"+BuildQueryString(startrow-rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\"><-- Previous Page</a></td>";\r
                        cols+=3;\r
                }\r
                if(startrow+rowsperpage<identitycount)\r
@@ -331,7 +410,7 @@ const std::string PeerTrustPage::GeneratePage(const std::string &method, const s
                                content+="<td></td>";\r
                                cols++;\r
                        }\r
-                       content+="<td colspan=\"3\" align=\"right\"><a href=\"peertrust.htm?"+BuildQueryString(startrow+rowsperpage,namesearch,sortby,sortorder)+"\">Next Page --></a></td>";\r
+                       content+="<td colspan=\"3\" align=\"right\"><a href=\"peertrust.htm?"+BuildQueryString(startrow+rowsperpage,namesearch,sortby,sortorder,localidentityid)+"\">Next Page --></a></td>";\r
                }\r
                content+="</tr>";\r
        }\r