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
30 SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishFreesite='true' AND (LastInsertedFreesite IS NULL OR LastInsertedFreesite<?);");
\r
31 st.Bind(0,date.Format("%Y-%m-%d"));
\r
34 if(st.RowReturned())
\r
36 int localidentityid=0;
\r
37 st.ResultInt(0,localidentityid);
\r
38 StartInsert(localidentityid);
\r
43 std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const long localidentityid, const std::string &name)
\r
45 std::string content="";
\r
47 content+="<h3>My last few posts</h3>";
\r
49 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
50 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
51 st.Bind(0,localidentityid);
\r
54 while(st.RowReturned())
\r
56 std::string post="";
\r
57 std::string subject="";
\r
58 std::string boards="";
\r
61 st.ResultText(0,post);
\r
62 st.ResultText(1,subject);
\r
63 st.ResultInt(2,messageid);
\r
65 boardst.Bind(0,messageid);
\r
67 while(boardst.RowReturned())
\r
69 std::string board="";
\r
70 boardst.ResultText(0,board);
\r
80 content+="<div class=\"post\">";
\r
81 content+="<div class=\"postboards\">";
\r
82 content+=SanitizeOutput(boards);
\r
84 content+="<div class=\"postsubject\">";
\r
85 content+=SanitizeOutput(subject);
\r
87 content+="<div class=\"postbody\">";
\r
88 content+=SanitizeOutput(post);
\r
89 //post=SanitizeOutput(post);
\r
90 //StringFunctions::Replace(post,"\r\n","<br>");
\r
98 return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);
\r
102 std::string SiteInserter::GenerateLinks(const bool publishtrustlist, const bool publishboardlist)
\r
104 std::string links="";
\r
106 links+="<li><a href=\"index.htm\">Home</a></li>";
\r
107 if(publishtrustlist)
\r
109 links+="<li><a href=\"trustlist.htm\">Trust List</a></li>";
\r
111 if(publishboardlist)
\r
113 // links+="<li><a href=\"boardlist.htm\">Board List</a></li>";
\r
119 void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey, std::map<std::string,std::string> &pages)
\r
121 SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList, FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID=?;");
\r
122 st.Bind(0,localidentityid);
\r
125 if(st.RowReturned())
\r
127 std::string htmltemplate="";
\r
128 std::string filename="";
\r
129 std::string name="";
\r
130 std::string key="";
\r
131 std::string publishtrustliststr="";
\r
132 std::string publishboardliststr="";
\r
133 bool publishtrustlist=false;
\r
134 bool publishboardlist=false;
\r
135 std::string editionstr="";
\r
137 st.ResultText(0,name);
\r
138 st.ResultText(1,key);
\r
139 st.ResultText(2,publishtrustliststr);
\r
140 st.ResultText(3,publishboardliststr);
\r
141 st.ResultText(4,editionstr);
\r
143 publishtrustliststr=="true" ? publishtrustlist=true : publishtrustlist=false;
\r
144 publishboardliststr=="true" ? publishboardlist=true : publishboardlist=false;
\r
145 // no edition exists - start at 0
\r
150 // previous edition exists - add 1
\r
154 StringFunctions::Convert(editionstr,edition);
\r
156 StringFunctions::Convert(edition,editionstr);
\r
159 filename=name+"-template.htm";
\r
160 FILE *infile=fopen(filename.c_str(),"r+b");
\r
163 infile=fopen("site-template.htm","r+b");
\r
167 fseek(infile,0,SEEK_END);
\r
168 long len=ftell(infile);
\r
169 fseek(infile,0,SEEK_SET);
\r
171 std::vector<unsigned char> data;
\r
173 fread(&data[0],1,data.size(),infile);
\r
176 htmltemplate.append(data.begin(),data.end());
\r
178 htmltemplate=StringFunctions::Replace(htmltemplate,"[LINKS]",GenerateLinks(publishtrustlist,publishboardlist));
\r
179 htmltemplate=StringFunctions::Replace(htmltemplate,"[IDENTITYNAME]",SanitizeOutput(name));
\r
181 pages["index.htm"]=GenerateIndex(htmltemplate,localidentityid,name);
\r
182 if(publishtrustlist)
\r
184 pages["trustlist.htm"]=GenerateTrustList(htmltemplate,localidentityid,name);
\r
186 if(publishboardlist)
\r
188 // pages["boardlist.htm"]=GenerateBoardList(htmltemplate,localidentityid,name);
\r
192 if(key.find("SSK@")==0)
\r
197 key+=m_messagebase+"/"+editionstr+"/";
\r
203 LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");
\r
206 // get extra files that the user wants to add to the Freesite
\r
207 filename=name+"-files.txt";
\r
208 infile=fopen(filename.c_str(),"r+b");
\r
211 std::vector<std::string> files;
\r
213 fseek(infile,0,SEEK_END);
\r
214 long len=ftell(infile);
\r
215 fseek(infile,0,SEEK_SET);
\r
217 std::vector<unsigned char> data;
\r
219 fread(&data[0],1,data.size(),infile);
\r
222 // split on \r and \n - on systems with \r\n line endings there will be blank entries, but we'll just skip those
\r
223 std::string filecontent(data.begin(),data.end());
\r
224 StringFunctions::SplitMultiple(filecontent,"\r\n",files);
\r
226 for(std::vector<std::string>::iterator i=files.begin(); i!=files.end(); i++)
\r
228 if((*i)!="" && (*i).find("index.htm")==std::string::npos && (*i).find("trustlist.htm")==std::string::npos && (*i).find("files.htm")==std::string::npos)
\r
231 infile=fopen(filename.c_str(),"r+b");
\r
234 fseek(infile,0,SEEK_END);
\r
236 fseek(infile,0,SEEK_SET);
\r
239 fread(&data[0],1,data.size(),infile);
\r
243 filecontent.append(data.begin(),data.end());
\r
245 // strip off path from filename
\r
246 while(filename.find_first_of("/")!=std::string::npos)
\r
248 filename.erase(0,filename.find_first_of("/")+1);
\r
251 if(filecontent.size()>0)
\r
253 pages[filename]=filecontent;
\r
259 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages could not include user file "+(*i));
\r
269 std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, const long localidentityid, const std::string &name)
\r
271 std::string content="";
\r
274 date.SetToGMTime();
\r
275 date.Add(0,0,0,-20);
\r
276 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
277 st.Bind(0,localidentityid);
\r
278 st.Bind(1,date.Format("%Y-%m-%d %H:%M:%S"));
\r
281 content+="<table>";
\r
282 content+="<tr><th colspan=\"5\">";
\r
283 content+="Trust List of "+SanitizeOutput(name);
\r
284 content+="</th></tr>";
\r
285 content+="<tr><td></td><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";
\r
286 while(st.RowReturned())
\r
288 std::string idname="";
\r
289 std::string thisid="";
\r
290 std::string messagetrustcomment="";
\r
291 std::string trustlisttrustcomment="";
\r
292 std::string messagetrust="";
\r
293 std::string trustlisttrust="";
\r
294 std::string publickey="";
\r
295 std::string uskkey="";
\r
296 std::string freesiteedition="";
\r
298 st.ResultText(0,idname);
\r
299 st.ResultText(1,publickey);
\r
300 st.ResultText(2,messagetrust);
\r
301 st.ResultText(3,trustlisttrust);
\r
302 st.ResultText(4,thisid);
\r
303 st.ResultText(5,messagetrustcomment);
\r
304 st.ResultText(6,trustlisttrustcomment);
\r
305 st.ResultText(7,freesiteedition);
\r
307 if(freesiteedition!="")
\r
309 if(publickey.find("SSK@")==0)
\r
313 uskkey="USK"+uskkey;
\r
314 uskkey+=m_messagebase+"/"+freesiteedition+"/";
\r
319 if(freesiteedition!="")
\r
321 content+="<td><a href=\""+uskkey+"\">"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</a></td>";
\r
325 content+="<td>"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</td>";
\r
327 content+="<td "+GetClassString(messagetrust)+">"+messagetrust+"</td>";
\r
328 content+="<td>"+SanitizeOutput(messagetrustcomment)+"</td>";
\r
329 content+="<td "+GetClassString(trustlisttrust)+">"+trustlisttrust+"</td>";
\r
330 content+="<td>"+SanitizeOutput(trustlisttrustcomment)+"</td>";
\r
331 content+="</tr>\r\n";
\r
336 return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);
\r
340 const std::string SiteInserter::GetClassString(const std::string &trustlevel)
\r
343 std::string tempstr;
\r
345 StringFunctions::Convert(trustlevel,tempint);
\r
347 StringFunctions::Convert(tempint,tempstr);
\r
351 return "class=\"trust"+tempstr+"\"";
\r
359 const bool SiteInserter::HandlePutFailed(FCPMessage &message)
\r
361 std::vector<std::string> idparts;
\r
362 long localidentityid;
\r
364 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
365 StringFunctions::Convert(idparts[1],localidentityid);
\r
367 RemoveFromInsertList(localidentityid);
\r
369 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]);
\r
374 const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)
\r
376 std::vector<std::string> idparts;
\r
377 std::vector<std::string> uriparts;
\r
378 long localidentityid;
\r
384 StringFunctions::Split(message["Identifier"],"|",idparts);
\r
385 StringFunctions::Convert(idparts[1],localidentityid);
\r
387 // edition is very last part of uri
\r
388 StringFunctions::Split(message["URI"],"/",uriparts);
\r
389 if(uriparts.size()>0)
\r
391 StringFunctions::Convert(uriparts[uriparts.size()-1],edition);
\r
394 SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=?, FreesiteEdition=? WHERE LocalIdentityID=?;");
\r
395 st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));
\r
396 st.Bind(1,edition);
\r
397 st.Bind(2,localidentityid);
\r
400 m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite.");
\r
402 RemoveFromInsertList(localidentityid);
\r
407 void SiteInserter::Initialize()
\r
409 m_fcpuniquename="SiteInserter";
\r
412 const std::string SiteInserter::SanitizeOutput(const std::string &input)
\r
414 // must do & first because all other elements have & in them!
\r
415 std::string output=StringFunctions::Replace(input,"&","&");
\r
416 output=StringFunctions::Replace(output,"<","<");
\r
417 output=StringFunctions::Replace(output,">",">");
\r
418 output=StringFunctions::Replace(output,"\"",""");
\r
419 output=StringFunctions::Replace(output," "," ");
\r
423 const bool SiteInserter::StartInsert(const long &localidentityid)
\r
425 FCPMessage message;
\r
426 std::string localidentityidstr="";
\r
427 std::string sizestr="";
\r
428 std::string uskkey="";
\r
429 std::map<std::string,std::string> pages;
\r
432 StringFunctions::Convert(localidentityid,localidentityidstr);
\r
434 GeneratePages(localidentityid,uskkey,pages);
\r
436 message.SetName("ClientPutComplexDir");
\r
437 message["URI"]=uskkey;
\r
438 message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+message["URI"];
\r
439 message["DefaultName"]="index.htm";
\r
441 // add each page to the message
\r
442 for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)
\r
444 std::string filenumstr;
\r
445 StringFunctions::Convert(filenum,filenumstr);
\r
448 StringFunctions::Convert((*pagei).second.size(),sizestr);
\r
450 message["Files."+filenumstr+".Name"]=(*pagei).first;
\r
451 message["Files."+filenumstr+".UploadFrom"]="direct";
\r
452 message["Files."+filenumstr+".DataLength"]=sizestr;
\r
457 m_fcp->SendMessage(message);
\r
459 // send data of each page
\r
460 for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)
\r
462 m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size());
\r
465 m_inserting.push_back(localidentityid);
\r