]> bicyclesonthemoon.info Git - ott/enhance/blob - pal-cgi.c
0ffe0dcd5aadd8ccf26870d268730c2166cb2df0
[ott/enhance] / pal-cgi.c
1 // insertframe.c\r
2 // The online interface for the tools to insert and extract indexed images\r
3 // 25.06.2015\r
4 // \r
5 // Copyright (C) 2015  Balthasar SzczepaƄski\r
6 // \r
7 // This program is free software: you can redistribute it and/or modify\r
8 // it under the terms of the GNU Affero General Public License as\r
9 // published by the Free Software Foundation, either version 3 of the\r
10 // License, or (at your option) any later version.\r
11 // \r
12 // This program is distributed in the hope that it will be useful,\r
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 // GNU Affero General Public License for more details.\r
16 // \r
17 // You should have received a copy of the GNU Affero General Public License\r
18 // along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19 // \r
20 \r
21 // Requires cgilib (http://www.infodrom.org/projects/cgilib/)\r
22         \r
23 #include <cgi.h>\r
24 #include <stdio.h>\r
25 #include <stdlib.h>\r
26 #include <unistd.h>\r
27 #include <string.h>\r
28 #include <sys/wait.h>\r
29 #include <sys/stat.h>\r
30 \r
31 #define WGET_PATH  "/usr/bin/wget"\r
32 #define INS_PATH   "./insert"\r
33 #define EXT_PATH   "./extract"\r
34 #define DIF_PATH   "./seediff"\r
35 \r
36 #define RM_PATH    "/bin/rm"\r
37 #define LOG_PATH   "/eizm/log/insertframe/insertframe.log"\r
38 #define USERAGENT  "Hidden frames. (1190.bicyclesonthemoon.dnsd.info/insertframe/insertframe.htm)"\r
39 #define MSTD_PATH  "/pro/insertframe/nclr.png"\r
40 \r
41 int main (int argc, char *argv[]);\r
42 void mustard(int mustard);\r
43 \r
44 FILE *pix;\r
45 unsigned short act;\r
46 \r
47 \r
48 char url0[1024];\r
49 char path0[256];\r
50 char url1[1024];\r
51 char path1[256];\r
52 s_cgi *cgi;\r
53 int t;\r
54 pid_t sub;\r
55 \r
56 \r
57 int main (int argc, char *argv[])\r
58 {\r
59         unsigned char buf[1024];\r
60         struct stat st;\r
61         unsigned long p, i;\r
62         unsigned short q;\r
63         char **up;\r
64         \r
65         // unsigned short r0, r1;\r
66         unsigned char n0=0;\r
67         unsigned char n1=0;\r
68 \r
69         cgi=cgiInit();\r
70         \r
71         sprintf(path0,"%s",(cgiGetValue(cgi,"a")!=NULL)?(cgiGetValue(cgi,"a")):"0");\r
72         act=atoi(path0);\r
73         // sprintf(path0,"%s",(cgiGetValue(cgi,"r0")!=NULL)?(cgiGetValue(cgi,"r0")):"1");\r
74         // r0=atoi(path0);\r
75         // sprintf(path0,"%s",(cgiGetValue(cgi,"r1")!=NULL)?(cgiGetValue(cgi,"r1")):"0");\r
76         // r1=atoi(path0);\r
77 \r
78         up=cgiGetFiles(cgi);\r
79         if (up)\r
80         {\r
81                 if (cgiGetFile(cgi,up[0])!=NULL)\r
82                 {\r
83                         sprintf(path0,"%s",cgiGetFile(cgi,up[0])->tmpfile);\r
84                         sprintf(url0,"%s",cgiGetFile(cgi,up[0])->filename);\r
85                         n0=1;\r
86                 }\r
87                 if (cgiGetFile(cgi,up[1])!=NULL)\r
88                 {\r
89                         sprintf(path1,"%s",cgiGetFile(cgi,up[1])->tmpfile);\r
90                         sprintf(url1,"%s",cgiGetFile(cgi,up[1])->filename);\r
91                         n1=1;\r
92                 }\r
93         }\r
94         \r
95         if(!n0)\r
96         {\r
97                 sprintf(url0,"%s",(cgiGetValue(cgi,"inpix0")!=NULL)?(cgiGetValue(cgi,"inpix0")):"");\r
98                 sprintf(path0,"/var/tmp/%lu_0",(unsigned long)getpid());\r
99                 \r
100                 sub=fork();\r
101                 if(sub==0)\r
102                 {\r
103                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path0,url0,(char *)0);\r
104                         exit(t);\r
105                 }\r
106                 waitpid(sub,&t,0);\r
107                 if(t)mustard(t);\r
108         }\r
109         \r
110         if(!n1)\r
111         {\r
112                 sprintf(url1,"%s",(cgiGetValue(cgi,"inpix1")!=NULL)?(cgiGetValue(cgi,"inpix1")):"");\r
113                 sprintf(path1,"/var/tmp/%lu_1",(unsigned long)getpid());\r
114                 \r
115                 if(!act)\r
116                 {\r
117                         sub=fork();\r
118                         if(sub==0)\r
119                         {\r
120                                 t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path1,url1,(char *)0);\r
121                                 exit(t);\r
122                         }\r
123                         waitpid(sub,&t,0);\r
124                         if(t)mustard(t);\r
125                 }\r
126         }\r
127         \r
128         sub=fork();\r
129         if(sub==0)\r
130         {\r
131                 switch (act)\r
132                 {\r
133                 case 0:\r
134                         t=execl(INS_PATH,INS_PATH,path0,path1,path0,(cgiGetValue(cgi,"r0")!=NULL)?(cgiGetValue(cgi,"r0")):"1",(cgiGetValue(cgi,"r1")!=NULL)?(cgiGetValue(cgi,"r1")):"0","q",(char *)0);\r
135                         exit(t);\r
136                 case 1:\r
137                 case 2:\r
138                         t=execl(EXT_PATH,EXT_PATH,path0,path0,path1,"q",(char *)0);\r
139                         exit(t);\r
140                 case 3:\r
141                         t=execl(DIF_PATH,DIF_PATH,path0,path0,"q",(char *)0);\r
142                         exit(t);\r
143                 default:\r
144                         exit(act);\r
145                 }\r
146         }\r
147         waitpid(sub,&t,0);\r
148         if(t)mustard(t);\r
149         \r
150         pix=fopen((act==2?path1:path0),"rb");\r
151         if(pix==NULL)\r
152                 mustard(123);\r
153         fstat(fileno(pix),&st);\r
154         p=st.st_size/1024;\r
155         q=st.st_size%1024;\r
156         \r
157         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));\r
158         printf("Content-type: image/png\n\n");\r
159         for(i=0;i<p;++i)\r
160         {\r
161                 fread(buf,1,1024,pix);\r
162                 fwrite(buf,1,1024,stdout);\r
163         }\r
164         if(q!=0)\r
165         {\r
166                 fread(buf,1,q,pix);\r
167                 fwrite(buf,1,q,stdout);\r
168         }\r
169         fclose(pix);\r
170         fflush(stdout);\r
171         mustard(0);//no mustard\r
172         return(0);\r
173 }\r
174 \r
175 void mustard(int mustard)\r
176 {\r
177         if(mustard)\r
178         {\r
179                 cgiRedirect(MSTD_PATH);\r
180         }\r
181         sub=fork();\r
182         if(sub==0)\r
183         {\r
184                 t=execl(RM_PATH,RM_PATH,"-f",path0,(char *)0);\r
185                 exit(t);\r
186         }\r
187         waitpid(sub,&t,0);\r
188         \r
189         sub=fork();\r
190         if(sub==0)\r
191         {\r
192                 t=execl(RM_PATH,RM_PATH,"-f",path1,(char *)0);\r
193                 exit(t);\r
194         }\r
195         waitpid(sub,&t,0);\r
196         \r
197         pix=fopen(LOG_PATH,"at");\r
198         if(pix!=NULL)\r
199         {\r
200                 fprintf(pix,"%x %x %s %s\n",mustard,act,url0,url1);\r
201                 fclose(pix);\r
202         }\r
203         exit(mustard);\r
204 }\r
205 \r
206 \r