version 0.2.11
[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 \r
131                 st.ResultText(0,name);\r
132                 st.ResultText(1,key);\r
133                 st.ResultText(2,publishtrustliststr);\r
134                 st.ResultText(3,publishboardliststr);\r
135 \r
136                 publishtrustliststr=="true" ? publishtrustlist=true : publishtrustlist=false;\r
137                 publishboardliststr=="true" ? publishboardlist=true : publishboardlist=false;\r
138 \r
139                 filename=name+"-template.htm";\r
140                 FILE *infile=fopen(filename.c_str(),"r+b");\r
141                 if(!infile)\r
142                 {\r
143                         infile=fopen("site-template.htm","r+b");\r
144                 }\r
145                 if(infile)\r
146                 {\r
147                         fseek(infile,0,SEEK_END);\r
148                         long len=ftell(infile);\r
149                         fseek(infile,0,SEEK_SET);\r
150 \r
151                         std::vector<unsigned char> data;\r
152                         data.resize(len);\r
153                         fread(&data[0],1,data.size(),infile);\r
154                         fclose(infile);\r
155 \r
156                         htmltemplate.append(data.begin(),data.end());\r
157 \r
158                         htmltemplate=StringFunctions::Replace(htmltemplate,"[LINKS]",GenerateLinks(publishtrustlist,publishboardlist));\r
159 \r
160                         pages["index.htm"]=GenerateIndex(htmltemplate,localidentityid,name);\r
161                         if(publishtrustlist)\r
162                         {\r
163                                 pages["trustlist.htm"]=GenerateTrustList(htmltemplate,localidentityid,name);\r
164                         }\r
165                         if(publishboardlist)\r
166                         {\r
167 //                              pages["boardlist.htm"]=GenerateBoardList(htmltemplate,localidentityid,name);\r
168                         }\r
169 \r
170                         // make SSK a USK\r
171                         if(key.find("SSK@")==0)\r
172                         {\r
173                                 key.erase(0,3);\r
174                                 key="USK"+key;\r
175                         }\r
176                         key+=m_messagebase+"/0/";\r
177                         uskkey=key;\r
178 \r
179                 }\r
180                 else\r
181                 {\r
182                         LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");\r
183                 }\r
184 \r
185                 // get extra files that the user wants to add to the Freesite\r
186                 filename=name+"-files.txt";\r
187                 infile=fopen(filename.c_str(),"r+b");\r
188                 if(infile)\r
189                 {\r
190                         std::vector<std::string> files;\r
191 \r
192                         fseek(infile,0,SEEK_END);\r
193                         long len=ftell(infile);\r
194                         fseek(infile,0,SEEK_SET);\r
195 \r
196                         std::vector<unsigned char> data;\r
197                         data.resize(len);\r
198                         fread(&data[0],1,data.size(),infile);\r
199                         fclose(infile);\r
200 \r
201                         // split on \r and \n - on systems with \r\n line endings there will be blank entries, but we'll just skip those\r
202                         std::string filecontent(data.begin(),data.end());\r
203                         StringFunctions::SplitMultiple(filecontent,"\r\n",files);\r
204 \r
205                         for(std::vector<std::string>::iterator i=files.begin(); i!=files.end(); i++)\r
206                         {\r
207                                 if((*i)!="" && (*i).find("index.htm")==std::string::npos && (*i).find("trustlist.htm")==std::string::npos && (*i).find("files.htm")==std::string::npos)\r
208                                 {\r
209                                         filename=(*i);\r
210                                         infile=fopen(filename.c_str(),"r+b");\r
211                                         if(infile)\r
212                                         {\r
213                                                 fseek(infile,0,SEEK_END);\r
214                                                 len=ftell(infile);\r
215                                                 fseek(infile,0,SEEK_SET);\r
216 \r
217                                                 data.resize(len);\r
218                                                 fread(&data[0],1,data.size(),infile);\r
219                                                 fclose(infile);\r
220 \r
221                                                 filecontent="";\r
222                                                 filecontent.append(data.begin(),data.end());\r
223 \r
224                                                 // strip off path from filename\r
225                                                 while(filename.find_first_of("/")!=std::string::npos)\r
226                                                 {\r
227                                                         filename.erase(0,filename.find_first_of("/")+1);\r
228                                                 }\r
229 \r
230                                                 if(filecontent.size()>0)\r
231                                                 {\r
232                                                         pages[filename]=filecontent;\r
233                                                 }\r
234 \r
235                                         }\r
236                                         else\r
237                                         {\r
238                                                 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages could not include user file "+(*i));\r
239                                         }\r
240                                 }\r
241                         }\r
242 \r
243                 }\r
244 \r
245         }\r
246 }\r
247 \r
248 std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, const long localidentityid, const std::string &name)\r
249 {\r
250         std::string content="";\r
251         DateTime date;\r
252 \r
253         date.SetToGMTime();\r
254         date.Add(0,0,0,-20);\r
255         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
256         st.Bind(0,localidentityid);\r
257         st.Bind(1,date.Format("%Y-%m-%d %H:%M:%S"));\r
258         st.Step();\r
259 \r
260         content+="<table>";\r
261         content+="<tr><th colspan=\"5\">";\r
262         content+="Trust List of "+SanitizeOutput(name);\r
263         content+="</th></tr>";\r
264         content+="<tr><td></td><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";\r
265         while(st.RowReturned())\r
266         {\r
267                 std::string idname="";\r
268                 std::string thisid="";\r
269                 std::string messagetrustcomment="";\r
270                 std::string trustlisttrustcomment="";\r
271                 std::string messagetrust="";\r
272                 std::string trustlisttrust="";\r
273                 std::string publickey="";\r
274                 std::string uskkey="";\r
275                 std::string freesiteedition="";\r
276 \r
277                 st.ResultText(0,idname);\r
278                 st.ResultText(1,publickey);\r
279                 st.ResultText(2,messagetrust);\r
280                 st.ResultText(3,trustlisttrust);\r
281                 st.ResultText(4,thisid);\r
282                 st.ResultText(5,messagetrustcomment);\r
283                 st.ResultText(6,trustlisttrustcomment);\r
284                 st.ResultText(7,freesiteedition);\r
285 \r
286                 if(freesiteedition!="")\r
287                 {\r
288                         if(publickey.find("SSK@")==0)\r
289                         {\r
290                                 uskkey=publickey;\r
291                                 uskkey.erase(0,3);\r
292                                 uskkey="USK"+uskkey;\r
293                                 uskkey+=m_messagebase+"/"+freesiteedition+"/";\r
294                         }\r
295                 }\r
296 \r
297                 content+="<tr>";\r
298                 if(freesiteedition!="")\r
299                 {\r
300                         content+="<td><a href=\""+uskkey+"\">"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</a></td>";\r
301                 }\r
302                 else\r
303                 {\r
304                         content+="<td>"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</td>";\r
305                 }\r
306                 content+="<td "+GetClassString(messagetrust)+">"+messagetrust+"</td>";\r
307                 content+="<td>"+SanitizeOutput(messagetrustcomment)+"</td>";\r
308                 content+="<td "+GetClassString(trustlisttrust)+">"+trustlisttrust+"</td>";\r
309                 content+="<td>"+SanitizeOutput(trustlisttrustcomment)+"</td>";\r
310                 content+="</tr>\r\n";\r
311 \r
312                 st.Step();\r
313         }\r
314 \r
315         return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);\r
316 \r
317 }\r
318 \r
319 const std::string SiteInserter::GetClassString(const std::string &trustlevel)\r
320 {\r
321         int tempint=0;\r
322         std::string tempstr;\r
323 \r
324         StringFunctions::Convert(trustlevel,tempint);\r
325         tempint/=10;\r
326         StringFunctions::Convert(tempint,tempstr);\r
327 \r
328         if(trustlevel!="")\r
329         {\r
330                 return "class=\"trust"+tempstr+"\"";\r
331         }\r
332         else\r
333         {\r
334                 return "";\r
335         }\r
336 }\r
337 \r
338 const bool SiteInserter::HandlePutFailed(FCPMessage &message)\r
339 {\r
340         std::vector<std::string> idparts;\r
341         long localidentityid;\r
342 \r
343         StringFunctions::Split(message["Identifier"],"|",idparts);\r
344         StringFunctions::Convert(idparts[1],localidentityid);\r
345 \r
346         RemoveFromInsertList(localidentityid);\r
347 \r
348         m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]);\r
349 \r
350         return true;\r
351 }\r
352 \r
353 const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)\r
354 {\r
355         std::vector<std::string> idparts;\r
356         std::vector<std::string> uriparts;\r
357         long localidentityid;\r
358         int edition=-1;\r
359         DateTime now;\r
360 \r
361         now.SetToGMTime();\r
362 \r
363         StringFunctions::Split(message["Identifier"],"|",idparts);\r
364         StringFunctions::Convert(idparts[1],localidentityid);\r
365 \r
366         // edition is very last part of uri\r
367         StringFunctions::Split(message["URI"],"/",uriparts);\r
368         if(uriparts.size()>0)\r
369         {\r
370                 StringFunctions::Convert(uriparts[uriparts.size()-1],edition);\r
371         }\r
372 \r
373         SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=?, FreesiteEdition=? WHERE LocalIdentityID=?;");\r
374         st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
375         st.Bind(1,edition);\r
376         st.Bind(2,localidentityid);\r
377         st.Step();\r
378 \r
379         m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite.");\r
380 \r
381         RemoveFromInsertList(localidentityid);\r
382 \r
383         return true;\r
384 }\r
385 \r
386 void SiteInserter::Initialize()\r
387 {\r
388         m_fcpuniquename="SiteInserter";\r
389 }\r
390 \r
391 const std::string SiteInserter::SanitizeOutput(const std::string &input)\r
392 {\r
393         // must do & first because all other elements have & in them!\r
394         std::string output=StringFunctions::Replace(input,"&","&amp;");\r
395         output=StringFunctions::Replace(output,"<","&lt;");\r
396         output=StringFunctions::Replace(output,">","&gt;");\r
397         output=StringFunctions::Replace(output,"\"","&quot;");\r
398         output=StringFunctions::Replace(output," ","&nbsp;");\r
399         return output;\r
400 }\r
401 \r
402 void SiteInserter::StartInsert(const long &localidentityid)\r
403 {\r
404         FCPMessage message;\r
405         std::string localidentityidstr="";\r
406         std::string sizestr="";\r
407         std::string uskkey="";\r
408         std::map<std::string,std::string> pages;\r
409         int filenum=0;\r
410 \r
411         StringFunctions::Convert(localidentityid,localidentityidstr);\r
412 \r
413         GeneratePages(localidentityid,uskkey,pages);\r
414 \r
415         message.SetName("ClientPutComplexDir");\r
416         message["URI"]=uskkey;\r
417         message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+message["URI"];\r
418         message["DefaultName"]="index.htm";\r
419 \r
420         // add each page to the message\r
421         for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)\r
422         {\r
423                 std::string filenumstr;\r
424                 StringFunctions::Convert(filenum,filenumstr);\r
425 \r
426                 sizestr="0";\r
427                 StringFunctions::Convert((*pagei).second.size(),sizestr);\r
428 \r
429                 message["Files."+filenumstr+".Name"]=(*pagei).first;\r
430                 message["Files."+filenumstr+".UploadFrom"]="direct";\r
431                 message["Files."+filenumstr+".DataLength"]=sizestr;\r
432 \r
433                 filenum++;\r
434         }\r
435 \r
436         m_fcp->SendMessage(message);\r
437 \r
438         // send data of each page\r
439         for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)\r
440         {\r
441                 m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size());\r
442         }\r
443 \r
444         m_inserting.push_back(localidentityid);\r
445 \r
446 }\r