]> bicyclesonthemoon.info Git - ott/enhance/blob - npbd-ong1.cpp
reveal online
[ott/enhance] / npbd-ong1.cpp
1 // npbd.cpp - copied from the original NewpixbOTTification but not actually used
2 // The online interface for the NewpixbOTTification and 403fication software
3 // 16.01.2015
4 // 
5 // Copyright (C) 2014  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 #include <cgi.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <sys/wait.h>
33 #include <sys/stat.h>
34
35 #define WGET_PATH  "/usr/bin/wget"
36 #define NPB_PATH   "/eizm/pro/ong1/npb"
37 #define MEG_PATH   "./403"
38 #define RM_PATH    "/bin/rm"
39 #define LOG_PATH   "/eizm/log/npb/npb.log"
40 #define LOG_PATH2  "/eizm/log/npb/403.log"
41 #define USERAGENT  "NewpixbOTTification\x20(1190.bicyclesonthemoon.dnsd.info/npb/npb.htm)"
42 #define USERAGENT2 "403fication\x20(1190.bicyclesonthemoon.dnsd.info/npb/403.htm)"
43 #define MSTD_PATH  "/pro/npb/mpb.png"
44 #define MSTD_PATH2 "/pro/npb/503.png"
45
46 int main (int argc, char *argv[]);
47 void mustard(int mustard);
48
49 FILE *pix;
50 unsigned char buf[1024];
51 char url[1024];
52 char path[256];
53 char method[256];
54 s_cgi *cgi;
55 int t;
56 pid_t sub;
57 struct stat st;
58 unsigned long p;
59 unsigned short q;
60 char **up;
61 bool meg=false;
62
63 int main (int argc, char *argv[])
64 {
65         cgi=cgiInit();
66         
67         sprintf(path,"%s",(getenv("PATH_INFO")!=NULL)?(getenv("PATH_INFO")):"");
68         if(strcmp(path,"/403")==0)
69         {
70                 meg=true;
71                 //mustard(0);
72         }
73         
74         up=cgiGetFiles(cgi);
75         if (up)
76         {
77                 sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
78                 sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
79         }
80         else
81         {
82                 sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
83                 sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
84                 
85                 //Why is  there no spawnl() in linux?
86                 sub=fork();
87                 if(sub==0)
88                 {
89                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",meg?USERAGENT2:USERAGENT,"-O",path,url,(char *)0);
90                         exit(t);
91                 }
92                 waitpid(sub,&t,0);
93                 if(t)mustard(t);
94         }
95         
96         sub=fork();
97         if(sub==0)
98         {
99                 t=execl(meg?MEG_PATH:NPB_PATH,meg?MEG_PATH:NPB_PATH,path,path,"q",(char *)0);
100                 exit(t);
101         }
102         waitpid(sub,&t,0);
103         if(t)mustard(t);
104         
105         pix=fopen(path,"rb");
106         if(pix==NULL)
107                 mustard(123);
108         fstat(fileno(pix),&st);
109         p=st.st_size/1024;
110         q=st.st_size%1024;
111         
112         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
113         printf("Content-type: image/png\n\n");
114         for(unsigned long i=0;i<p;++i)
115         {
116                 fread(buf,1,1024,pix);
117                 fwrite(buf,1,1024,stdout);
118         }
119         if(q!=0)
120         {
121                 fread(buf,1,q,pix);
122                 fwrite(buf,1,q,stdout);
123         }
124         fclose(pix);
125         fflush(stdout);
126         mustard(0);//no mustard
127 }
128
129 void mustard(int mustard)
130 {
131         if(mustard)
132         {
133                 cgiRedirect(meg?MSTD_PATH2:MSTD_PATH);
134         }
135         sub=fork();
136         if(sub==0)
137         {
138                 t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
139                 exit(t);
140         }
141         waitpid(sub,&t,0);
142         // pix=fopen(meg?LOG_PATH2:LOG_PATH,"at");
143         // if(pix!=NULL)
144         // {
145                 // fprintf(pix,"%x %s\n",mustard,url);
146                 // fclose(pix);
147         // }
148         exit(mustard);
149 }