1 #include "../../include/freenet/siteinserter.h"
\r
2 #include "../../include/global.h"
\r
8 SiteInserter::SiteInserter()
\r
13 SiteInserter::SiteInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)
\r
18 void SiteInserter::CheckForNeededInsert()
\r
20 // only do 1 insert at a time
\r
21 if(m_inserting.size()==0)
\r
26 SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishFreesite='true' AND (LastInsertedFreesite IS NULL OR LastInsertedFreesite<?);");
\r
27 st.Bind(0,date.Format("%Y-%m-%d"));
\r
30 if(st.RowReturned())
\r
32 int localidentityid=0;
\r
33 st.ResultInt(0,localidentityid);
\r
34 StartInsert(localidentityid);
\r
39 std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const long localidentityid, const std::string &name)
\r
41 std::string content="";
\r
43 content="<h2>FMS site of "+SanitizeOutput(name)+"</h2>";
\r
45 content+="<h3>My last few posts</h3>";
\r
47 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
48 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
49 st.Bind(0,localidentityid);
\r
52 while(st.RowReturned())
\r
54 std::string post="";
\r
55 std::string subject="";
\r
56 std::string boards="";
\r
59 st.ResultText(0,post);
\r
60 st.ResultText(1,subject);
\r
61 st.ResultInt(2,messageid);
\r
63 boardst.Bind(0,messageid);
\r
65 while(boardst.RowReturned())
\r
67 std::string board="";
\r
68 boardst.ResultText(0,board);
\r
78 content+="<div class=\"post\">";
\r
79 content+="<div class=\"postboards\">";
\r
80 content+=SanitizeOutput(boards);
\r
82 content+="<div class=\"postsubject\">";
\r
83 content+=SanitizeOutput(subject);
\r
85 content+="<div class=\"postbody\">";
\r
86 content+=SanitizeOutput(post);
\r
87 //post=SanitizeOutput(post);
\r
88 //StringFunctions::Replace(post,"\r\n","<br>");
\r
96 return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);
\r
100 std::string SiteInserter::GenerateLinks(const bool publishtrustlist, const bool publishboardlist)
\r
102 std::string links="";
\r
104 links+="<li><a href=\"index.htm\">Home</a></li>";
\r
105 if(publishtrustlist)
\r
107 links+="<li><a href=\"trustlist.htm\">Trust List</a></li>";
\r
109 if(publishboardlist)
\r
111 // links+="<li><a href=\"boardlist.htm\">Board List</a></li>";
\r
117 void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey, std::map<std::string,std::string> &pages)
\r
119 SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList, FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID=?;");
\r
120 st.Bind(0,localidentityid);
\r
123 if(st.RowReturned())
\r
125 std::string htmltemplate="";
\r
126 std::string filename="";
\r
127 std::string name="";
\r
128 std::string key="";
\r
129 std::string publishtrustliststr="";
\r
130 std::string publishboardliststr="";
\r
131 bool publishtrustlist=false;
\r
132 bool publishboardlist=false;
\r
133 std::string editionstr="";
\r
135 st.ResultText(0,name);
\r
136 st.ResultText(1,key);
\r
137 st.ResultText(2,publishtrustliststr);
\r
138 st.ResultText(3,publishboardliststr);
\r
139 st.ResultText(4,editionstr);
\r
141 publishtrustliststr=="true" ? publishtrustlist=true : publishtrustlist=false;
\r
142 publishboardliststr=="true" ? publishboardlist=true : publishboardlist=false;
\r
143 // no edition exists - start at 0
\r
148 // previous edition exists - add 1
\r
152 StringFunctions::Convert(editionstr,edition);
\r
154 StringFunctions::Convert(edition,editionstr);
\r
157 filename=name+"-template.htm";
\r
158 FILE *infile=fopen(filename.c_str(),"r+b");
\r
161 infile=fopen("site-template.htm","r+b");
\r
165 fseek(infile,0,SEEK_END);
\r
166 long len=ftell(infile);
\r
167 fseek(infile,0,SEEK_SET);
\r
169 std::vector<unsigned char> data;
\r
171 fread(&data[0],1,data.size(),infile);
\r
174 htmltemplate.append(data.begin(),data.end());
\r
176 htmltemplate=StringFunctions::Replace(htmltemplate,"[LINKS]",GenerateLinks(publishtrustlist,publishboardlist));
\r
178 pages["index.htm"]=GenerateIndex(htmltemplate,localidentityid,name);
\r
179 if(publishtrustlist)
\r
181 pages["trustlist.htm"]=GenerateTrustList(htmltemplate,localidentityid,name);
\r
183 if(publishboardlist)
\r
185 // pages["boardlist.htm"]=GenerateBoardList(htmltemplate,localidentityid,name);
\r
189 if(key.find("SSK@")==0)
\r
194 key+=m_messagebase+"/"+editionstr+"/";
\r
200 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");
\r
203 // get extra files that the user wants to add to the Freesite
\r
204 filename=name+"-files.txt";
\r
205 infile=fopen(filename.c_str(),"r+b");
\r
208 std::vector<std::string> files;
\r
210 fseek(infile,0,SEEK_END);
\r
211 long len=ftell(infile);
\r
212 fseek(infile,0,SEEK_SET);
\r
214 std::vector<unsigned char> data;
\r
216 fread(&data[0],1,data.size(),infile);
\r
219 // split on \r and \n - on systems with \r\n line endings there will be blank entries, but we'll just skip those
\r
220 std::string filecontent(data.begin(),data.end());
\r
221 StringFunctions::SplitMultiple(filecontent,"\r\n",files);
\r
223 for(std::vector<std::string>::iterator i=files.begin(); i!=files.end(); i++)
\r
225 if((*i)!="" && (*i).find("index.htm")==std::string::npos && (*i).find("trustlist.htm")==std::string::npos && (*i).find("files.htm")==std::string::npos)
\r
228 infile=fopen(filename.c_str(),"r+b");
\r
231 fseek(infile,0,SEEK_END);
\r
233 fseek(infile,0,SEEK_SET);
\r
236 fread(&data[0],1,data.size(),infile);
\r
240 filecontent.append(data.begin(),data.end());
\r
242 // strip off path from filename
\r
243 while(filename.find_first_of("/")!=std::string::npos)
\r
245 filename.erase(0,filename.find_first_of("/")+1);
\r
248 if(filecontent.size()>0)
\r
250 pages[filename]=filecontent;
\r
256 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages could not include user file "+(*i));
\r
266 std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, const long localidentityid, const std::string &name)
\r
268 std::string content="";
\r
271 date.SetToGMTime();
\r
272 date.Add(0,0,0,-20);
\r
273 SQLite3DB::Statement st=m_db->Prepare("SELECT Name,PublicKey,tblIdentityTrust.LocalMessageTrust,tblIdentityTrust.LocalTrustListTrust,tblIdentity.IdentityID,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment,tblIdentity.FreesiteEdition FROM tblIdentity LEFT JOIN (SELECT IdentityID,LocalMessageTrust,LocalTrustListTrust,MessageTrustComment,TrustListTrustComment FROM tblIdentityTrust WHERE LocalIdentityID=?) AS 'tblIdentityTrust' ON tblIdentity.IdentityID=tblIdentityTrust.IdentityID WHERE PublicKey IS NOT NULL AND LastSeen IS NOT NULL AND LastSeen>=? ORDER BY Name COLLATE NOCASE;");
\r
274 st.Bind(0,localidentityid);
\r
275 st.Bind(1,date.Format("%Y-%m-%d %H:%M:%S"));
\r
278 content+="<table>";
\r
279 content+="<tr><th colspan=\"5\">";
\r
280 content+="Trust List of "+SanitizeOutput(name);
\r
281 content+="</th></tr>";
\r
282 content+="<tr><td></td><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";
\r
283 while(st.RowReturned())
\r
285 std::string idname="";
\r
286 std::string thisid="";
\r
287 std::string messagetrustcomment="";
\r
288 std::string trustlisttrustcomment="";
\r
289 std::string messagetrust="";
\r
290 std::string trustlisttrust="";
\r
291 std::string publickey="";
\r
292 std::string uskkey="";
\r
293 std::string freesiteedition="";
\r
295 st.ResultText(0,idname);
\r
296 st.ResultText(1,publickey);
\r
297 st.ResultText(2,messagetrust);
\r
298 st.ResultText(3,trustlisttrust);
\r
299 st.ResultText(4,thisid);
\r
300 st.ResultText(5,messagetrustcomment);
\r
301 st.ResultText(6,trustlisttrustcomment);
\r
302 st.ResultText(7,freesiteedition);
\r
304 if(freesiteedition!="")
\r
306 if(publickey.find("SSK@")==0)
\r
310 uskkey="USK"+uskkey;
\r
311 uskkey+=m_messagebase+"/"+freesiteedition+"/";
\r
316 if(freesiteedition!="")
\r
318 content+="<td><a href=\""+uskkey+"\">"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</a></td>";
\r
322 content+="<td>"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</td>";
\r
324 content+="<td "+GetClassString(messagetrust)+">"+messagetrust+"</td>";
\r
325 content+="<td>"+SanitizeOutput(messagetrustcomment)+"</td>";
\r
326 content+="<td "+GetClassString(trustlisttrust)+">"+trustlisttrust+"</td>";
\r
327 content+="<td>"+SanitizeOutput(trustlisttrustcomment)+"</td>";
\r
328 content+="</tr>\r\n";
\r
333 return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);
\r
337 const std::string SiteInserter::GetClassString(const std::string &trustlevel)
\r
340 std::string tempstr;
\r
342 StringFunctions::Convert(trustlevel,tempint);
\r
344 StringFunctions::Convert(tempint,tempstr);
\r
348 return "class=\"trust"+tempstr+"\"";
\r
356 const bool SiteInserter::HandlePutFailed(FCPMessage &message)
\r
358 std::vector<std::string> idparts;
\r
359 long localidentityid;
\r
361 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
362 StringFunctions::Convert(idparts[1],localidentityid);
\r
364 RemoveFromInsertList(localidentityid);
\r
366 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]);
\r
371 const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)
\r
373 std::vector<std::string> idparts;
\r
374 std::vector<std::string> uriparts;
\r
375 long localidentityid;
\r
381 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
382 StringFunctions::Convert(idparts[1],localidentityid);
\r
384 // edition is very last part of uri
\r
385 StringFunctions::Split(message["URI"],"/",uriparts);
\r
386 if(uriparts.size()>0)
\r
388 StringFunctions::Convert(uriparts[uriparts.size()-1],edition);
\r
391 SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=?, FreesiteEdition=? WHERE LocalIdentityID=?;");
\r
392 st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));
\r
393 st.Bind(1,edition);
\r
394 st.Bind(2,localidentityid);
\r
397 m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite.");
\r
399 RemoveFromInsertList(localidentityid);
\r
404 void SiteInserter::Initialize()
\r
406 m_fcpuniquename="SiteInserter";
\r
409 const std::string SiteInserter::SanitizeOutput(const std::string &input)
\r
411 // must do & first because all other elements have & in them!
\r
412 std::string output=StringFunctions::Replace(input,"&","&");
\r
413 output=StringFunctions::Replace(output,"<","<");
\r
414 output=StringFunctions::Replace(output,">",">");
\r
415 output=StringFunctions::Replace(output,"\"",""");
\r
416 output=StringFunctions::Replace(output," "," ");
\r
420 const bool SiteInserter::StartInsert(const long &localidentityid)
\r
422 FCPMessage message;
\r
423 std::string localidentityidstr="";
\r
424 std::string sizestr="";
\r
425 std::string uskkey="";
\r
426 std::map<std::string,std::string> pages;
\r
429 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
431 GeneratePages(localidentityid,uskkey,pages);
\r
433 message.SetName("ClientPutComplexDir");
\r
434 message["URI"]=uskkey;
\r
435 message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+message["URI"];
\r
436 message["DefaultName"]="index.htm";
\r
438 // add each page to the message
\r
439 for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)
\r
441 std::string filenumstr;
\r
442 StringFunctions::Convert(filenum,filenumstr);
\r
445 StringFunctions::Convert((*pagei).second.size(),sizestr);
\r
447 message["Files."+filenumstr+".Name"]=(*pagei).first;
\r
448 message["Files."+filenumstr+".UploadFrom"]="direct";
\r
449 message["Files."+filenumstr+".DataLength"]=sizestr;
\r
454 m_fcp->SendMessage(message);
\r
456 // send data of each page
\r
457 for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)
\r
459 m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size());
\r
462 m_inserting.push_back(localidentityid);
\r