summaryrefslogblamecommitdiff
path: root/blog/sbs_2/cgi-src/get-post-content.cpp
blob: 62cd5d85515d4e73fb28d001852c14a6d2247a89 (plain) (tree)


















































































                                                                                                                                                      
/*
 * Copyright 2017 Chris Xiong
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/*
 * Get content of a post
 * query parameters:
 * p=<string>: post file name
 * returned object:
 * HTML fragment of the post
 */
#include <dirent.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include "cgiutils.hpp"
#define stripr(s) s[strlen(s)-1]=='\n'?s[strlen(s)-1]=0:0
std::vector<std::string> fn;
char buf[262144];
size_t sz;
void encrypt()
{
	std::string b(buf,sz);
	for(size_t p=b.find("<encrypted"),i=0;p!=std::string::npos;p=b.find("<encrypted",p),++i)
	{
		size_t tag_end=b.find(">",p);
		std::string attrib=b.substr(p+11,tag_end-p-11);
		DOMAttrib a(attrib);
		std::string cont=b.substr(tag_end+1,b.find("</encrypted>",p)-tag_end-1);
		unsigned hash=5381;
		std::string key=a.getAttrib("key");
		a.eraseAttrib("key");
		for(size_t j=0;j<cont.length();++j)
		{
			hash=((hash<<5)+hash)+*reinterpret_cast<unsigned char*>(&cont[j]);
			cont[j]^=key[j%key.length()];
		}
		a.setAttrib("encont",base64_encode(cont));
		a.setAttrib("hash",std::to_string(hash));
		a.setAttrib("id","encrypted"+std::to_string(i));
		cont="Encrypted content here. Click <a href=\"javascript:void(0)\" onclick=\"decryptui("+std::to_string(i)+")\">here</a> to decrypt.";
		b.replace(p,b.find("</encrypted>",p)-p,"<encrypted "+a.to_string()+">"+cont);
		p=b.find("</encrypted>",p);
	}
	memcpy(buf,b.c_str(),b.length());sz=b.length();
}
int main(int argc,char** argv,char** envp)
{
	QueryStrParser a;
	HTTPHeader h;
	if(!a.exist("p")){h.setStatusCode(400);h.print();return 0;}
	FILE* f=fopen(("/var/www/html/blog/content/"+a.value("p")+".txt").c_str(),"r");
	if(!f){h.setStatusCode(400);h.print();return 0;}
	h.appendHeader("Content-type: text/plain; charset=utf-8");
	h.print();
	sz=fread(buf,sizeof(char),262144,f);
	encrypt();
	fwrite(buf,sizeof(char),sz,stdout);
	fclose(f);
	return 0;
}