version 0.2.20
[fms.git] / src / freenet / siteinserter.cpp
index 9db9651..e54a28e 100644 (file)
@@ -22,6 +22,10 @@ void SiteInserter::CheckForNeededInsert()
        {\r
                DateTime date;\r
                date.SetToGMTime();\r
+               date.SetHour(0);\r
+               date.SetMinute(0);\r
+               date.SetSecond(0);\r
+               date.Normalize();\r
 \r
                SQLite3DB::Statement st=m_db->Prepare("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishFreesite='true' AND (LastInsertedFreesite IS NULL OR LastInsertedFreesite<?);");\r
                st.Bind(0,date.Format("%Y-%m-%d"));\r
@@ -40,8 +44,6 @@ std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const l
 {\r
        std::string content="";\r
 \r
-       content="<h2>FMS site of "+SanitizeOutput(name)+"</h2>";\r
-\r
        content+="<h3>My last few posts</h3>";\r
 \r
        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
@@ -84,6 +86,9 @@ std::string SiteInserter::GenerateIndex(const std::string &htmltemplate, const l
                content+="</div>";\r
                content+="<div class=\"postbody\">";\r
                content+=SanitizeOutput(post);\r
+               //post=SanitizeOutput(post);\r
+               //StringFunctions::Replace(post,"\r\n","<br>");\r
+               //content+=post;\r
                content+="</div>";\r
                content+="</div>";\r
 \r
@@ -113,7 +118,7 @@ std::string SiteInserter::GenerateLinks(const bool publishtrustlist, const bool
 \r
 void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey, std::map<std::string,std::string> &pages)\r
 {\r
-       SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList FROM tblLocalIdentity WHERE LocalIdentityID=?;");\r
+       SQLite3DB::Statement st=m_db->Prepare("SELECT Name, PrivateKey, PublishTrustList, PublishBoardList, FreesiteEdition FROM tblLocalIdentity WHERE LocalIdentityID=?;");\r
        st.Bind(0,localidentityid);\r
        st.Step();\r
 \r
@@ -127,14 +132,29 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
                std::string publishboardliststr="";\r
                bool publishtrustlist=false;\r
                bool publishboardlist=false;\r
+               std::string editionstr="";\r
 \r
                st.ResultText(0,name);\r
                st.ResultText(1,key);\r
                st.ResultText(2,publishtrustliststr);\r
                st.ResultText(3,publishboardliststr);\r
+               st.ResultText(4,editionstr);\r
 \r
                publishtrustliststr=="true" ? publishtrustlist=true : publishtrustlist=false;\r
                publishboardliststr=="true" ? publishboardlist=true : publishboardlist=false;\r
+               // no edition exists - start at 0\r
+               if(editionstr=="")\r
+               {\r
+                       editionstr="0";\r
+               }\r
+               // previous edition exists - add 1\r
+               else\r
+               {\r
+                       int edition=0;\r
+                       StringFunctions::Convert(editionstr,edition);\r
+                       edition++;\r
+                       StringFunctions::Convert(edition,editionstr);\r
+               }\r
 \r
                filename=name+"-template.htm";\r
                FILE *infile=fopen(filename.c_str(),"r+b");\r
@@ -156,6 +176,7 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
                        htmltemplate.append(data.begin(),data.end());\r
 \r
                        htmltemplate=StringFunctions::Replace(htmltemplate,"[LINKS]",GenerateLinks(publishtrustlist,publishboardlist));\r
+                       htmltemplate=StringFunctions::Replace(htmltemplate,"[IDENTITYNAME]",SanitizeOutput(name));\r
 \r
                        pages["index.htm"]=GenerateIndex(htmltemplate,localidentityid,name);\r
                        if(publishtrustlist)\r
@@ -173,7 +194,7 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
                                key.erase(0,3);\r
                                key="USK"+key;\r
                        }\r
-                       key+=m_messagebase+"/0/";\r
+                       key+=m_messagebase+"/"+editionstr+"/";\r
                        uskkey=key;\r
 \r
                }\r
@@ -182,6 +203,66 @@ void SiteInserter::GeneratePages(const long localidentityid, std::string &uskkey
                        LogFile::Instance()->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages unable to open "+filename+" or site-template.htm.");\r
                }\r
 \r
+               // get extra files that the user wants to add to the Freesite\r
+               filename=name+"-files.txt";\r
+               infile=fopen(filename.c_str(),"r+b");\r
+               if(infile)\r
+               {\r
+                       std::vector<std::string> files;\r
+\r
+                       fseek(infile,0,SEEK_END);\r
+                       long len=ftell(infile);\r
+                       fseek(infile,0,SEEK_SET);\r
+\r
+                       std::vector<unsigned char> data;\r
+                       data.resize(len);\r
+                       fread(&data[0],1,data.size(),infile);\r
+                       fclose(infile);\r
+\r
+                       // split on \r and \n - on systems with \r\n line endings there will be blank entries, but we'll just skip those\r
+                       std::string filecontent(data.begin(),data.end());\r
+                       StringFunctions::SplitMultiple(filecontent,"\r\n",files);\r
+\r
+                       for(std::vector<std::string>::iterator i=files.begin(); i!=files.end(); i++)\r
+                       {\r
+                               if((*i)!="" && (*i).find("index.htm")==std::string::npos && (*i).find("trustlist.htm")==std::string::npos && (*i).find("files.htm")==std::string::npos)\r
+                               {\r
+                                       filename=(*i);\r
+                                       infile=fopen(filename.c_str(),"r+b");\r
+                                       if(infile)\r
+                                       {\r
+                                               fseek(infile,0,SEEK_END);\r
+                                               len=ftell(infile);\r
+                                               fseek(infile,0,SEEK_SET);\r
+\r
+                                               data.resize(len);\r
+                                               fread(&data[0],1,data.size(),infile);\r
+                                               fclose(infile);\r
+\r
+                                               filecontent="";\r
+                                               filecontent.append(data.begin(),data.end());\r
+\r
+                                               // strip off path from filename\r
+                                               while(filename.find_first_of("/")!=std::string::npos)\r
+                                               {\r
+                                                       filename.erase(0,filename.find_first_of("/")+1);\r
+                                               }\r
+\r
+                                               if(filecontent.size()>0)\r
+                                               {\r
+                                                       pages[filename]=filecontent;\r
+                                               }\r
+\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::GeneratePages could not include user file "+(*i));\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+               }\r
+\r
        }\r
 }\r
 \r
@@ -285,6 +366,8 @@ const bool SiteInserter::HandlePutFailed(FCPMessage &message)
 \r
        RemoveFromInsertList(localidentityid);\r
 \r
+       m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"SiteInserter::HandlePutFailed failed to insert Freesite, Freenet error code : "+message["Code"]);\r
+\r
        return true;\r
 }\r
 \r
@@ -337,7 +420,7 @@ const std::string SiteInserter::SanitizeOutput(const std::string &input)
        return output;\r
 }\r
 \r
-void SiteInserter::StartInsert(const long &localidentityid)\r
+const bool SiteInserter::StartInsert(const long &localidentityid)\r
 {\r
        FCPMessage message;\r
        std::string localidentityidstr="";\r
@@ -381,4 +464,6 @@ void SiteInserter::StartInsert(const long &localidentityid)
 \r
        m_inserting.push_back(localidentityid);\r
 \r
+       return true;\r
+\r
 }\r