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