df4831c247fe15d26dbfbac2343865e5e7898ef4
[fms.git] / src / freenet / siteinserter.cpp
1 #include "../../include/freenet/siteinserter.h"\r
2 #include "../../include/global.h"\r
3 \r
4 #ifdef XMEM\r
5         #include <xmem.h>\r
6 #endif\r
7 \r
8 SiteInserter::SiteInserter()\r
9 {\r
10         Initialize();\r
11 }\r
12 \r
13 SiteInserter::SiteInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)\r
14 {\r
15         Initialize();\r
16 }\r
17 \r
18 void SiteInserter::CheckForNeededInsert()\r
19 {\r
20         // only do 1 insert at a time\r
21         if(m_inserting.size()==0)\r
22         {\r
23                 DateTime date;\r
24                 date.SetToGMTime();\r
25 \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
28 \r
29                 st.Step();\r
30                 if(st.RowReturned())\r
31                 {\r
32                         int localidentityid=0;\r
33                         st.ResultInt(0,localidentityid);\r
34                         StartInsert(localidentityid);\r
35                 }\r
36         }\r
37 }\r
38 \r
39 std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const long localidentityid, const std::string &name)\r
40 {\r
41         std::string content="";\r
42 \r
43         content="<h2>FMS site of "+SanitizeOutput(name)+"</h2>";\r
44 \r
45         content+="<h3>My last few posts</h3>";\r
46 \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
50         st.Step();\r
51 \r
52         while(st.RowReturned())\r
53         {\r
54                 std::string post="";\r
55                 std::string subject="";\r
56                 std::string boards="";\r
57                 int messageid=0;\r
58 \r
59                 st.ResultText(0,post);\r
60                 st.ResultText(1,subject);\r
61                 st.ResultInt(2,messageid);\r
62 \r
63                 boardst.Bind(0,messageid);\r
64                 boardst.Step();\r
65                 while(boardst.RowReturned())\r
66                 {\r
67                         std::string board="";\r
68                         boardst.ResultText(0,board);\r
69                         if(boards!="")\r
70                         {\r
71                                 boards+=",";\r
72                         }\r
73                         boards+=board;\r
74                         boardst.Step();\r
75                 }\r
76                 boardst.Reset();\r
77 \r
78                 content+="<div class=\"post\">";\r
79                 content+="<div class=\"postboards\">";\r
80                 content+=SanitizeOutput(boards);\r
81                 content+="</div>";\r
82                 content+="<div class=\"postsubject\">";\r
83                 content+=SanitizeOutput(subject);\r
84                 content+="</div>";\r
85                 content+="<div class=\"postbody\">";\r
86                 content+=SanitizeOutput(post);\r
87                 content+="</div>";\r
88                 content+="</div>";\r
89 \r
90                 st.Step();\r
91         }\r
92 \r
93         return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);\r
94 \r
95 }\r
96 \r
97 std::string SiteInserter::GenerateLinks(const bool publishtrustlist, const bool publishboardlist)\r
98 {\r
99         std::string links="";\r
100         links+="<ul>";\r
101         links+="<li><a href=\"index.htm\">Home</a></li>";\r
102         if(publishtrustlist)\r
103         {\r
104                 links+="<li><a href=\"trustlist.htm\">Trust List</a></li>";\r
105         }\r
106         if(publishboardlist)\r
107         {\r
108 //              links+="<li><a href=\"boardlist.htm\">Board List</a></li>";\r
109         }\r
110         links+="</ul>";\r
111         return links;\r
112 }\r
113 \r
114 void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey, std::map<std::string,std::string> &pages)\r
115 {\r
116         SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList, FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID=?;");\r
117         st.Bind(0,localidentityid);\r
118         st.Step();\r
119 \r
120         if(st.RowReturned())\r
121         {\r
122                 std::string htmltemplate="";\r
123                 std::string filename="";\r
124                 std::string name="";\r
125                 std::string key="";\r
126                 std::string publishtrustliststr="";\r
127                 std::string publishboardliststr="";\r
128                 bool publishtrustlist=false;\r
129                 bool publishboardlist=false;\r
130                 std::string editionstr="";\r
131 \r
132                 st.ResultText(0,name);\r
133                 st.ResultText(1,key);\r
134                 st.ResultText(2,publishtrustliststr);\r
135                 st.ResultText(3,publishboardliststr);\r
136                 st.ResultText(4,editionstr);\r
137 \r
138                 publishtrustliststr=="true" ? publishtrustlist=true : publishtrustlist=false;\r
139                 publishboardliststr=="true" ? publishboardlist=true : publishboardlist=false;\r
140                 // no edition exists - start at 0\r
141                 if(editionstr=="")\r
142                 {\r
143                         editionstr="0";\r
144                 }\r
145                 // previous edition exists - add 1\r
146                 else\r
147                 {\r
148                         int edition=0;\r
149                         StringFunctions::Convert(editionstr,edition);\r
150                         edition++;\r
151                         StringFunctions::Convert(edition,editionstr);\r
152                 }\r
153 \r
154                 filename=name+"-template.htm";\r
155                 FILE *infile=fopen(filename.c_str(),"r+b");\r
156                 if(!infile)\r
157                 {\r
158                         infile=fopen("site-template.htm","r+b");\r
159                 }\r
160                 if(infile)\r
161                 {\r
162                         fseek(infile,0,SEEK_END);\r
163                         long len=ftell(infile);\r
164                         fseek(infile,0,SEEK_SET);\r
165 \r
166                         std::vector<unsigned char> data;\r
167                         data.resize(len);\r
168                         fread(&data[0],1,data.size(),infile);\r
169                         fclose(infile);\r
170 \r
171                         htmltemplate.append(data.begin(),data.end());\r
172 \r
173                         htmltemplate=StringFunctions::Replace(htmltemplate,"[LINKS]",GenerateLinks(publishtrustlist,publishboardlist));\r
174 \r
175                         pages["index.htm"]=GenerateIndex(htmltemplate,localidentityid,name);\r
176                         if(publishtrustlist)\r
177                         {\r
178                                 pages["trustlist.htm"]=GenerateTrustList(htmltemplate,localidentityid,name);\r
179                         }\r
180                         if(publishboardlist)\r
181                         {\r
182 //                              pages["boardlist.htm"]=GenerateBoardList(htmltemplate,localidentityid,name);\r
183                         }\r
184 \r
185                         // make SSK a USK\r
186                         if(key.find("SSK@")==0)\r
187                         {\r
188                                 key.erase(0,3);\r
189                                 key="USK"+key;\r
190                         }\r
191                         key+=m_messagebase+"/"+editionstr+"/";\r
192                         uskkey=key;\r
193 \r
194                 }\r
195                 else\r
196                 {\r
197                         LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");\r
198                 }\r
199 \r
200                 // get extra files that the user wants to add to the Freesite\r
201                 filename=name+"-files.txt";\r
202                 infile=fopen(filename.c_str(),"r+b");\r
203                 if(infile)\r
204                 {\r
205                         std::vector<std::string> files;\r
206 \r
207                         fseek(infile,0,SEEK_END);\r
208                         long len=ftell(infile);\r
209                         fseek(infile,0,SEEK_SET);\r
210 \r
211                         std::vector<unsigned char> data;\r
212                         data.resize(len);\r
213                         fread(&data[0],1,data.size(),infile);\r
214                         fclose(infile);\r
215 \r
216                         // split on \r and \n - on systems with \r\n line endings there will be blank entries, but we'll just skip those\r
217                         std::string filecontent(data.begin(),data.end());\r
218                         StringFunctions::SplitMultiple(filecontent,"\r\n",files);\r
219 \r
220                         for(std::vector<std::string>::iterator i=files.begin(); i!=files.end(); i++)\r
221                         {\r
222                                 if((*i)!="" && (*i).find("index.htm")==std::string::npos && (*i).find("trustlist.htm")==std::string::npos && (*i).find("files.htm")==std::string::npos)\r
223                                 {\r
224                                         filename=(*i);\r
225                                         infile=fopen(filename.c_str(),"r+b");\r
226                                         if(infile)\r
227                                         {\r
228                                                 fseek(infile,0,SEEK_END);\r
229                                                 len=ftell(infile);\r
230                                                 fseek(infile,0,SEEK_SET);\r
231 \r
232                                                 data.resize(len);\r
233                                                 fread(&data[0],1,data.size(),infile);\r
234                                                 fclose(infile);\r
235 \r
236                                                 filecontent="";\r
237                                                 filecontent.append(data.begin(),data.end());\r
238 \r
239                                                 // strip off path from filename\r
240                                                 while(filename.find_first_of("/")!=std::string::npos)\r
241                                                 {\r
242                                                         filename.erase(0,filename.find_first_of("/")+1);\r
243                                                 }\r
244 \r
245                                                 if(filecontent.size()>0)\r
246                                                 {\r
247                                                         pages[filename]=filecontent;\r
248                                                 }\r
249 \r
250                                         }\r
251                                         else\r
252                                         {\r
253                                                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages could not include user file "+(*i));\r
254                                         }\r
255                                 }\r
256                         }\r
257 \r
258                 }\r
259 \r
260         }\r
261 }\r
262 \r
263 std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, const long localidentityid, const std::string &name)\r
264 {\r
265         std::string content="";\r
266         DateTime date;\r
267 \r
268         date.SetToGMTime();\r
269         date.Add(0,0,0,-20);\r
270         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
271         st.Bind(0,localidentityid);\r
272         st.Bind(1,date.Format("%Y-%m-%d %H:%M:%S"));\r
273         st.Step();\r
274 \r
275         content+="<table>";\r
276         content+="<tr><th colspan=\"5\">";\r
277         content+="Trust List of "+SanitizeOutput(name);\r
278         content+="</th></tr>";\r
279         content+="<tr><td></td><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";\r
280         while(st.RowReturned())\r
281         {\r
282                 std::string idname="";\r
283                 std::string thisid="";\r
284                 std::string messagetrustcomment="";\r
285                 std::string trustlisttrustcomment="";\r
286                 std::string messagetrust="";\r
287                 std::string trustlisttrust="";\r
288                 std::string publickey="";\r
289                 std::string uskkey="";\r
290                 std::string freesiteedition="";\r
291 \r
292                 st.ResultText(0,idname);\r
293                 st.ResultText(1,publickey);\r
294                 st.ResultText(2,messagetrust);\r
295                 st.ResultText(3,trustlisttrust);\r
296                 st.ResultText(4,thisid);\r
297                 st.ResultText(5,messagetrustcomment);\r
298                 st.ResultText(6,trustlisttrustcomment);\r
299                 st.ResultText(7,freesiteedition);\r
300 \r
301                 if(freesiteedition!="")\r
302                 {\r
303                         if(publickey.find("SSK@")==0)\r
304                         {\r
305                                 uskkey=publickey;\r
306                                 uskkey.erase(0,3);\r
307                                 uskkey="USK"+uskkey;\r
308                                 uskkey+=m_messagebase+"/"+freesiteedition+"/";\r
309                         }\r
310                 }\r
311 \r
312                 content+="<tr>";\r
313                 if(freesiteedition!="")\r
314                 {\r
315                         content+="<td><a href=\""+uskkey+"\">"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</a></td>";\r
316                 }\r
317                 else\r
318                 {\r
319                         content+="<td>"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</td>";\r
320                 }\r
321                 content+="<td "+GetClassString(messagetrust)+">"+messagetrust+"</td>";\r
322                 content+="<td>"+SanitizeOutput(messagetrustcomment)+"</td>";\r
323                 content+="<td "+GetClassString(trustlisttrust)+">"+trustlisttrust+"</td>";\r
324                 content+="<td>"+SanitizeOutput(trustlisttrustcomment)+"</td>";\r
325                 content+="</tr>\r\n";\r
326 \r
327                 st.Step();\r
328         }\r
329 \r
330         return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);\r
331 \r
332 }\r
333 \r
334 const std::string SiteInserter::GetClassString(const std::string &trustlevel)\r
335 {\r
336         int tempint=0;\r
337         std::string tempstr;\r
338 \r
339         StringFunctions::Convert(trustlevel,tempint);\r
340         tempint/=10;\r
341         StringFunctions::Convert(tempint,tempstr);\r
342 \r
343         if(trustlevel!="")\r
344         {\r
345                 return "class=\"trust"+tempstr+"\"";\r
346         }\r
347         else\r
348         {\r
349                 return "";\r
350         }\r
351 }\r
352 \r
353 const bool SiteInserter::HandlePutFailed(FCPMessage &message)\r
354 {\r
355         std::vector<std::string> idparts;\r
356         long localidentityid;\r
357 \r
358         StringFunctions::Split(message["Identifier"],"|",idparts);\r
359         StringFunctions::Convert(idparts[1],localidentityid);\r
360 \r
361         RemoveFromInsertList(localidentityid);\r
362 \r
363         m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]);\r
364 \r
365         return true;\r
366 }\r
367 \r
368 const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)\r
369 {\r
370         std::vector<std::string> idparts;\r
371         std::vector<std::string> uriparts;\r
372         long localidentityid;\r
373         int edition=-1;\r
374         DateTime now;\r
375 \r
376         now.SetToGMTime();\r
377 \r
378         StringFunctions::Split(message["Identifier"],"|",idparts);\r
379         StringFunctions::Convert(idparts[1],localidentityid);\r
380 \r
381         // edition is very last part of uri\r
382         StringFunctions::Split(message["URI"],"/",uriparts);\r
383         if(uriparts.size()>0)\r
384         {\r
385                 StringFunctions::Convert(uriparts[uriparts.size()-1],edition);\r
386         }\r
387 \r
388         SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=?, FreesiteEdition=? WHERE LocalIdentityID=?;");\r
389         st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
390         st.Bind(1,edition);\r
391         st.Bind(2,localidentityid);\r
392         st.Step();\r
393 \r
394         m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite.");\r
395 \r
396         RemoveFromInsertList(localidentityid);\r
397 \r
398         return true;\r
399 }\r
400 \r
401 void SiteInserter::Initialize()\r
402 {\r
403         m_fcpuniquename="SiteInserter";\r
404 }\r
405 \r
406 const std::string SiteInserter::SanitizeOutput(const std::string &input)\r
407 {\r
408         // must do & first because all other elements have & in them!\r
409         std::string output=StringFunctions::Replace(input,"&","&amp;");\r
410         output=StringFunctions::Replace(output,"<","&lt;");\r
411         output=StringFunctions::Replace(output,">","&gt;");\r
412         output=StringFunctions::Replace(output,"\"","&quot;");\r
413         output=StringFunctions::Replace(output," ","&nbsp;");\r
414         return output;\r
415 }\r
416 \r
417 void SiteInserter::StartInsert(const long &localidentityid)\r
418 {\r
419         FCPMessage message;\r
420         std::string localidentityidstr="";\r
421         std::string sizestr="";\r
422         std::string uskkey="";\r
423         std::map<std::string,std::string> pages;\r
424         int filenum=0;\r
425 \r
426         StringFunctions::Convert(localidentityid,localidentityidstr);\r
427 \r
428         GeneratePages(localidentityid,uskkey,pages);\r
429 \r
430         message.SetName("ClientPutComplexDir");\r
431         message["URI"]=uskkey;\r
432         message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+message["URI"];\r
433         message["DefaultName"]="index.htm";\r
434 \r
435         // add each page to the message\r
436         for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)\r
437         {\r
438                 std::string filenumstr;\r
439                 StringFunctions::Convert(filenum,filenumstr);\r
440 \r
441                 sizestr="0";\r
442                 StringFunctions::Convert((*pagei).second.size(),sizestr);\r
443 \r
444                 message["Files."+filenumstr+".Name"]=(*pagei).first;\r
445                 message["Files."+filenumstr+".UploadFrom"]="direct";\r
446                 message["Files."+filenumstr+".DataLength"]=sizestr;\r
447 \r
448                 filenum++;\r
449         }\r
450 \r
451         m_fcp->SendMessage(message);\r
452 \r
453         // send data of each page\r
454         for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)\r
455         {\r
456                 m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size());\r
457         }\r
458 \r
459         m_inserting.push_back(localidentityid);\r
460 \r
461 }\r