1 #include "../../include/freenet/siteinserter.h"
\r
7 SiteInserter::SiteInserter()
\r
12 SiteInserter::SiteInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)
\r
17 void SiteInserter::CheckForNeededInsert()
\r
19 // only do 1 insert at a time
\r
20 if(m_inserting.size()==0)
\r
25 SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishFreesite='true' AND (LastInsertedFreesite IS NULL OR LastInsertedFreesite<?);");
\r
26 st.Bind(0,date.Format("%Y-%m-%d"));
\r
29 if(st.RowReturned())
\r
31 int localidentityid=0;
\r
32 st.ResultInt(0,localidentityid);
\r
33 StartInsert(localidentityid);
\r
38 std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const long localidentityid, const std::string &name)
\r
40 std::string content="";
\r
42 content="<h2>FMS site of "+SanitizeOutput(name)+"</h2>";
\r
44 content+="<h3>My last few posts</h3>";
\r
46 SQLite3DB::Statement boardst=m_db->Prepare("SELECT tblBoard.BoardName FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID WHERE tblMessageBoard.MessageID=? ORDER BY tblBoard.BoardName COLLATE NOCASE;");
\r
47 SQLite3DB::Statement st=m_db->Prepare("SELECT tblMessage.Body, tblMessage.Subject, tblMessage.MessageID FROM tblMessage INNER JOIN tblIdentity ON tblMessage.IdentityID=tblIdentity.IdentityID INNER JOIN tblLocalIdentity ON tblIdentity.PublicKey=tblLocalIdentity.PublicKey WHERE tblLocalIdentity.LocalIdentityID=? ORDER BY tblMessage.MessageDate DESC, tblMessage.MessageTime DESC LIMIT 0,10;");
\r
48 st.Bind(0,localidentityid);
\r
51 while(st.RowReturned())
\r
53 std::string post="";
\r
54 std::string subject="";
\r
55 std::string boards="";
\r
58 st.ResultText(0,post);
\r
59 st.ResultText(1,subject);
\r
60 st.ResultInt(2,messageid);
\r
62 boardst.Bind(0,messageid);
\r
64 while(boardst.RowReturned())
\r
66 std::string board="";
\r
67 boardst.ResultText(0,board);
\r
77 content+="<div class=\"post\">";
\r
78 content+="<div class=\"postboards\">";
\r
79 content+=SanitizeOutput(boards);
\r
81 content+="<div class=\"postsubject\">";
\r
82 content+=SanitizeOutput(subject);
\r
84 content+="<div class=\"postbody\">";
\r
85 content+=SanitizeOutput(post);
\r
92 return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);
\r
96 std::string SiteInserter::GenerateLinks(const bool publishtrustlist, const bool publishboardlist)
\r
98 std::string links="";
\r
100 links+="<li><a href=\"index.htm\">Home</a></li>";
\r
101 if(publishtrustlist)
\r
103 links+="<li><a href=\"trustlist.htm\">Trust List</a></li>";
\r
105 if(publishboardlist)
\r
107 // links+="<li><a href=\"boardlist.htm\">Board List</a></li>";
\r
113 void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey, std::map<std::string,std::string> &pages)
\r
115 SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList FROM tblLocalIdentity WHERE LocalIdentityID=?;");
\r
116 st.Bind(0,localidentityid);
\r
119 if(st.RowReturned())
\r
121 std::string htmltemplate="";
\r
122 std::string filename="";
\r
123 std::string name="";
\r
124 std::string key="";
\r
125 std::string publishtrustliststr="";
\r
126 std::string publishboardliststr="";
\r
127 bool publishtrustlist=false;
\r
128 bool publishboardlist=false;
\r
130 st.ResultText(0,name);
\r
131 st.ResultText(1,key);
\r
132 st.ResultText(2,publishtrustliststr);
\r
133 st.ResultText(3,publishboardliststr);
\r
135 publishtrustliststr=="true" ? publishtrustlist=true : publishtrustlist=false;
\r
136 publishboardliststr=="true" ? publishboardlist=true : publishboardlist=false;
\r
138 filename=name+"-template.htm";
\r
139 FILE *infile=fopen(filename.c_str(),"r+b");
\r
142 infile=fopen("site-template.htm","r+b");
\r
146 fseek(infile,0,SEEK_END);
\r
147 long len=ftell(infile);
\r
148 fseek(infile,0,SEEK_SET);
\r
150 std::vector<unsigned char> data;
\r
152 fread(&data[0],1,data.size(),infile);
\r
155 htmltemplate.append(data.begin(),data.end());
\r
157 htmltemplate=StringFunctions::Replace(htmltemplate,"[LINKS]",GenerateLinks(publishtrustlist,publishboardlist));
\r
159 pages["index.htm"]=GenerateIndex(htmltemplate,localidentityid,name);
\r
160 if(publishtrustlist)
\r
162 pages["trustlist.htm"]=GenerateTrustList(htmltemplate,localidentityid,name);
\r
164 if(publishboardlist)
\r
166 // pages["boardlist.htm"]=GenerateBoardList(htmltemplate,localidentityid,name);
\r
170 if(key.find("SSK@")==0)
\r
175 key+=m_messagebase+"/0/";
\r
181 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");
\r
187 std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, const long localidentityid, const std::string &name)
\r
189 std::string content="";
\r
192 date.SetToGMTime();
\r
193 date.Add(0,0,0,-20);
\r
194 SQLite3DB::Statement st=m_db->Prepare("SELECT Name,PublicKey,LocalMessageTrust,LocalTrustListTrust,IdentityID,MessageTrustComment,TrustListTrustComment FROM tblIdentity WHERE PublicKey IS NOT NULL AND LastSeen IS NOT NULL AND LastSeen>=? ORDER BY Name COLLATE NOCASE;");
\r
195 st.Bind(0,date.Format("%Y-%m-%d %H:%M:%S"));
\r
198 content+="<table>";
\r
199 content+="<tr><th colspan=\"5\">";
\r
200 content+="Trust List of "+SanitizeOutput(name);
\r
201 content+="</th></tr>";
\r
202 content+="<tr><td></td><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";
\r
203 while(st.RowReturned())
\r
205 std::string idname="";
\r
206 std::string thisid="";
\r
207 std::string messagetrustcomment="";
\r
208 std::string trustlisttrustcomment="";
\r
209 std::string messagetrust="";
\r
210 std::string trustlisttrust="";
\r
211 std::string publickey="";
\r
212 std::string keypart="";
\r
213 std::string uskkey="";
\r
215 st.ResultText(0,idname);
\r
216 st.ResultText(1,publickey);
\r
217 st.ResultText(2,messagetrust);
\r
218 st.ResultText(3,trustlisttrust);
\r
219 st.ResultText(4,thisid);
\r
220 st.ResultText(5,messagetrustcomment);
\r
221 st.ResultText(6,trustlisttrustcomment);
\r
223 if(publickey.size()>8)
\r
225 keypart=publickey.substr(3,5);
\r
228 if(publickey.find("SSK@")==0)
\r
232 uskkey="USK"+uskkey;
\r
233 uskkey+=m_messagebase+"/0/";
\r
237 content+="<td><a href=\""+uskkey+"\">"+SanitizeOutput(idname+keypart)+"...</a></td>";
\r
238 content+="<td "+GetClassString(messagetrust)+">"+messagetrust+"</td>";
\r
239 content+="<td>"+SanitizeOutput(messagetrustcomment)+"</td>";
\r
240 content+="<td "+GetClassString(trustlisttrust)+">"+trustlisttrust+"</td>";
\r
241 content+="<td>"+SanitizeOutput(trustlisttrustcomment)+"</td>";
\r
242 content+="</tr>\r\n";
\r
247 return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);
\r
251 const std::string SiteInserter::GetClassString(const std::string &trustlevel)
\r
254 std::string tempstr;
\r
256 StringFunctions::Convert(trustlevel,tempint);
\r
258 StringFunctions::Convert(tempint,tempstr);
\r
262 return "class=\"trust"+tempstr+"\"";
\r
270 const bool SiteInserter::HandlePutFailed(FCPMessage &message)
\r
272 std::vector<std::string> idparts;
\r
273 long localidentityid;
\r
275 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
276 StringFunctions::Convert(idparts[1],localidentityid);
\r
278 RemoveFromInsertList(localidentityid);
\r
283 const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)
\r
285 std::vector<std::string> idparts;
\r
286 long localidentityid;
\r
291 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
292 StringFunctions::Convert(idparts[1],localidentityid);
\r
294 SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=? WHERE LocalIdentityID=?;");
\r
295 st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));
\r
296 st.Bind(1,localidentityid);
\r
299 m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite.");
\r
301 RemoveFromInsertList(localidentityid);
\r
306 void SiteInserter::Initialize()
\r
308 m_fcpuniquename="SiteInserter";
\r
311 const std::string SiteInserter::SanitizeOutput(const std::string &input)
\r
313 // must do & first because all other elements have & in them!
\r
314 std::string output=StringFunctions::Replace(input,"&","&");
\r
315 output=StringFunctions::Replace(output,"<","<");
\r
316 output=StringFunctions::Replace(output,">",">");
\r
317 output=StringFunctions::Replace(output,"\"",""");
\r
318 output=StringFunctions::Replace(output," "," ");
\r
322 void SiteInserter::StartInsert(const long &localidentityid)
\r
324 FCPMessage message;
\r
325 std::string localidentityidstr="";
\r
326 std::string sizestr="";
\r
327 std::string uskkey="";
\r
328 std::map<std::string,std::string> pages;
\r
331 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
333 GeneratePages(localidentityid,uskkey,pages);
\r
335 message.SetName("ClientPutComplexDir");
\r
336 message["URI"]=uskkey;
\r
337 message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+message["URI"];
\r
338 message["DefaultName"]="index.htm";
\r
340 // add each page to the message
\r
341 for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)
\r
343 std::string filenumstr;
\r
344 StringFunctions::Convert(filenum,filenumstr);
\r
347 StringFunctions::Convert((*pagei).second.size(),sizestr);
\r
349 message["Files."+filenumstr+".Name"]=(*pagei).first;
\r
350 message["Files."+filenumstr+".UploadFrom"]="direct";
\r
351 message["Files."+filenumstr+".DataLength"]=sizestr;
\r
356 m_fcp->SendMessage(message);
\r
358 // send data of each page
\r
359 for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)
\r
361 m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size());
\r
364 m_inserting.push_back(localidentityid);
\r