1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*
* 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;
}
|