version 0.2.7
[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 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         }\r
186 }\r
187 \r
188 std::string SiteInserter::GenerateTrustList(const std::string &htmltemplate, const long localidentityid, const std::string &name)\r
189 {\r
190         std::string content="";\r
191         DateTime date;\r
192 \r
193         date.SetToGMTime();\r
194         date.Add(0,0,0,-20);\r
195         SQLite3DB::Statement st=m_db->Prepare("SELECT Name,PublicKey,tblIdentityTrust.LocalMessageTrust,tblIdentityTrust.LocalTrustListTrust,tblIdentity.IdentityID,tblIdentityTrust.MessageTrustComment,tblIdentityTrust.TrustListTrustComment 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
196         st.Bind(0,localidentityid);\r
197         st.Bind(1,date.Format("%Y-%m-%d %H:%M:%S"));\r
198         st.Step();\r
199 \r
200         content+="<table>";\r
201         content+="<tr><th colspan=\"5\">";\r
202         content+="Trust List of "+SanitizeOutput(name);\r
203         content+="</th></tr>";\r
204         content+="<tr><td></td><th>Message Trust</th><th>Message Comment</th><th>Trust List Trust</th><th>Trust Comment</th></tr>";\r
205         while(st.RowReturned())\r
206         {\r
207                 std::string idname="";\r
208                 std::string thisid="";\r
209                 std::string messagetrustcomment="";\r
210                 std::string trustlisttrustcomment="";\r
211                 std::string messagetrust="";\r
212                 std::string trustlisttrust="";\r
213                 std::string publickey="";\r
214                 std::string uskkey="";\r
215 \r
216                 st.ResultText(0,idname);\r
217                 st.ResultText(1,publickey);\r
218                 st.ResultText(2,messagetrust);\r
219                 st.ResultText(3,trustlisttrust);\r
220                 st.ResultText(4,thisid);\r
221                 st.ResultText(5,messagetrustcomment);\r
222                 st.ResultText(6,trustlisttrustcomment);\r
223 \r
224                 if(publickey.find("SSK@")==0)\r
225                 {\r
226                         uskkey=publickey;\r
227                         uskkey.erase(0,3);\r
228                         uskkey="USK"+uskkey;\r
229                         uskkey+=m_messagebase+"/0/";\r
230                 }\r
231 \r
232                 content+="<tr>";\r
233                 content+="<td><a href=\""+uskkey+"\">"+SanitizeOutput(CreateShortIdentityName(idname,publickey))+"</a></td>";\r
234                 content+="<td "+GetClassString(messagetrust)+">"+messagetrust+"</td>";\r
235                 content+="<td>"+SanitizeOutput(messagetrustcomment)+"</td>";\r
236                 content+="<td "+GetClassString(trustlisttrust)+">"+trustlisttrust+"</td>";\r
237                 content+="<td>"+SanitizeOutput(trustlisttrustcomment)+"</td>";\r
238                 content+="</tr>\r\n";\r
239 \r
240                 st.Step();\r
241         }\r
242 \r
243         return StringFunctions::Replace(htmltemplate,"[CONTENT]",content);\r
244 \r
245 }\r
246 \r
247 const std::string SiteInserter::GetClassString(const std::string &trustlevel)\r
248 {\r
249         int tempint=0;\r
250         std::string tempstr;\r
251 \r
252         StringFunctions::Convert(trustlevel,tempint);\r
253         tempint/=10;\r
254         StringFunctions::Convert(tempint,tempstr);\r
255 \r
256         if(trustlevel!="")\r
257         {\r
258                 return "class=\"trust"+tempstr+"\"";\r
259         }\r
260         else\r
261         {\r
262                 return "";\r
263         }\r
264 }\r
265 \r
266 const bool SiteInserter::HandlePutFailed(FCPMessage &message)\r
267 {\r
268         std::vector<std::string> idparts;\r
269         long localidentityid;\r
270 \r
271         StringFunctions::Split(message["Identifier"],"|",idparts);\r
272         StringFunctions::Convert(idparts[1],localidentityid);\r
273 \r
274         RemoveFromInsertList(localidentityid);\r
275 \r
276         return true;\r
277 }\r
278 \r
279 const bool SiteInserter::HandlePutSuccessful(FCPMessage &message)\r
280 {\r
281         std::vector<std::string> idparts;\r
282         long localidentityid;\r
283         DateTime now;\r
284 \r
285         now.SetToGMTime();\r
286 \r
287         StringFunctions::Split(message["Identifier"],"|",idparts);\r
288         StringFunctions::Convert(idparts[1],localidentityid);\r
289 \r
290         SQLite3DB::Statement st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedFreesite=? WHERE LocalIdentityID=?;");\r
291         st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
292         st.Bind(1,localidentityid);\r
293         st.Step();\r
294 \r
295         m_log->WriteLog(LogFile::LOGLEVEL_INFO,"SiteInserter::HandlePutSuccessful successfully inserted Freesite.");\r
296 \r
297         RemoveFromInsertList(localidentityid);\r
298 \r
299         return true;\r
300 }\r
301 \r
302 void SiteInserter::Initialize()\r
303 {\r
304         m_fcpuniquename="SiteInserter";\r
305 }\r
306 \r
307 const std::string SiteInserter::SanitizeOutput(const std::string &input)\r
308 {\r
309         // must do & first because all other elements have & in them!\r
310         std::string output=StringFunctions::Replace(input,"&","&amp;");\r
311         output=StringFunctions::Replace(output,"<","&lt;");\r
312         output=StringFunctions::Replace(output,">","&gt;");\r
313         output=StringFunctions::Replace(output,"\"","&quot;");\r
314         output=StringFunctions::Replace(output," ","&nbsp;");\r
315         return output;\r
316 }\r
317 \r
318 void SiteInserter::StartInsert(const long &localidentityid)\r
319 {\r
320         FCPMessage message;\r
321         std::string localidentityidstr="";\r
322         std::string sizestr="";\r
323         std::string uskkey="";\r
324         std::map<std::string,std::string> pages;\r
325         int filenum=0;\r
326 \r
327         StringFunctions::Convert(localidentityid,localidentityidstr);\r
328 \r
329         GeneratePages(localidentityid,uskkey,pages);\r
330 \r
331         message.SetName("ClientPutComplexDir");\r
332         message["URI"]=uskkey;\r
333         message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+message["URI"];\r
334         message["DefaultName"]="index.htm";\r
335 \r
336         // add each page to the message\r
337         for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)\r
338         {\r
339                 std::string filenumstr;\r
340                 StringFunctions::Convert(filenum,filenumstr);\r
341 \r
342                 sizestr="0";\r
343                 StringFunctions::Convert((*pagei).second.size(),sizestr);\r
344 \r
345                 message["Files."+filenumstr+".Name"]=(*pagei).first;\r
346                 message["Files."+filenumstr+".UploadFrom"]="direct";\r
347                 message["Files."+filenumstr+".DataLength"]=sizestr;\r
348 \r
349                 filenum++;\r
350         }\r
351 \r
352         m_fcp->SendMessage(message);\r
353 \r
354         // send data of each page\r
355         for(std::map<std::string,std::string>::iterator pagei=pages.begin(); pagei!=pages.end(); pagei++)\r
356         {\r
357                 m_fcp->SendRaw(&(*pagei).second[0],(*pagei).second.size());\r
358         }\r
359 \r
360         m_inserting.push_back(localidentityid);\r
361 \r
362 }\r