]> bicyclesonthemoon.info Git - ott/enhance/blob - nofading-cgi.c
2c20885b80599c0e8c4c7a9e245b528736b96111
[ott/enhance] / nofading-cgi.c
1 // nofadingd.cpp
2 // The online interface for the fading enhancement tool
3 // 04.03.2015
4 // 
5 // Copyright (C) 2015  Balthasar Szczepański
6 // 
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
11 // 
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU Affero General Public License for more details.
16 // 
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 // 
20
21 // Requires cgilib (http://www.infodrom.org/projects/cgilib/)
22 // line 146 of cgi.h must be changed from:
23 // extern }
24 // to:
25 // }
26
27 by balthasar_s
28     Fri Jun 12, 2015 6:59 am UTC
29      
30     Forum: Individual XKCD Comic Threads
31     Topic: 1190: "Time"
32     Replies: 93408
33     Views: 10895653
34
35
36
37 #include <cgi.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <sys/wait.h>
43 #include <sys/stat.h>
44
45 #define WGET_PATH  "/usr/bin/wget"
46 #define ENH_PATH   "./nofading"
47 #define RM_PATH    "/bin/rm"
48 #define LOG_PATH   "/eizm/log/nofading/nofading.log"
49 #define USERAGENT  "No fading (1190.bicyclesonthemoon.dnsd.info/nofading/nofading.htm)"
50 #define MSTD_PATH  "/pro/nofading/nf.png"
51
52 int main (int argc, char *argv[]);
53 void mustard(int mustard);
54
55 FILE *pix;
56
57 char url[1024];
58 char path[256];
59 s_cgi *cgi;
60 int t;
61 pid_t sub;
62
63 int main (int argc, char *argv[])
64 {
65         unsigned char buf[1024];
66         struct stat st;
67         unsigned long p;
68         unsigned short q;
69         char **up;
70
71         cgi=cgiInit();
72         
73         up=cgiGetFiles(cgi);
74         if (up)
75         {
76                 sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
77                 sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
78         }
79         else
80         {
81                 sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
82                 sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
83                 
84                 //Why is  there no spawnl() in linux?
85                 sub=fork();
86                 if(sub==0)
87                 {
88                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path,url,(char *)0);
89                         exit(t);
90                 }
91                 waitpid(sub,&t,0);
92                 if(t)mustard(t);
93         }
94         
95         sub=fork();
96         if(sub==0)
97         {
98                 t=execl(ENH_PATH,ENH_PATH,path,path,(cgiGetValue(cgi,"f")!=NULL)?(cgiGetValue(cgi,"f")):"0","q",cgiGetValue(cgi,"a")!=NULL?"0":"a",(char *)0);
99                 exit(t);
100         }
101         waitpid(sub,&t,0);
102         if(t)mustard(t);
103         
104         pix=fopen(path,"rb");
105         if(pix==NULL)
106                 mustard(123);
107         fstat(fileno(pix),&st);
108         p=st.st_size/1024;
109         q=st.st_size%1024;
110         
111         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
112         printf("Content-type: image/png\n\n");
113         for(unsigned long i=0;i<p;++i)
114         {
115                 fread(buf,1,1024,pix);
116                 fwrite(buf,1,1024,stdout);
117         }
118         if(q!=0)
119         {
120                 fread(buf,1,q,pix);
121                 fwrite(buf,1,q,stdout);
122         }
123         fclose(pix);
124         fflush(stdout);
125         mustard(0);//no mustard
126 }
127
128 void mustard(int mustard)
129 {
130         if(mustard)
131         {
132                 cgiRedirect(MSTD_PATH);
133         }
134         sub=fork();
135         if(sub==0)
136         {
137                 t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
138                 exit(t);
139         }
140         waitpid(sub,&t,0);
141         pix=fopen(LOG_PATH,"at");
142         if(pix!=NULL)
143         {
144                 fprintf(pix,"%x %s\n",mustard,url);
145                 fclose(pix);
146         }
147         exit(mustard);
148 }