]> bicyclesonthemoon.info Git - ott/enhance/blob - bluenh-cgi.c
Online interface for bluenh. Something is no yes.
[ott/enhance] / bluenh-cgi.c
1 /*
2 bluenh-cgi.c is autogenerated from bluenh-cgi.1.c
3 Online interface for npb
4 04.12.2022
5
6 Copyright (C) 2013, 2014, 2022  Balthasar SzczepaƄski
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 Requires cgilib (http://www.infodrom.org/projects/cgilib/)
22 */
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29
30 #include <cgi.h>
31
32 #include "online-core.h"
33 #include "nh.h"
34
35 int bluenh (char *inpix, char *outpix, unsigned a, unsigned b, unsigned c, unsigned d, unsigned e, unsigned f);
36
37 int main (int argc, char *argv[])
38 {
39         s_cgi *cgi;
40         char in_tmp[256];
41         char out_tmp[256];
42         char *in_path;
43         char *t;
44         unsigned a, b, c, d, e, f;
45         
46         int r=0;
47         int r1=0;
48         int r2=0;
49         int r3=0;
50         
51         do {
52                 make_tmp_path(in_tmp, 256, 0, "_in");
53                 make_tmp_path(out_tmp, 256, 0, "_out");
54                 
55                 cgi=cgiInit();
56                 
57                 r = get_file(cgi,"inpix", in_tmp, &in_path);
58                 if (r)
59                         break;
60                 
61                 t = cgiGetValue(cgi, "a");
62                 if (t != NULL)
63                         sscanf(t,"%u",&a);
64                 else
65                         a = 0;
66                 
67                 t = cgiGetValue(cgi, "b");
68                 if (t != NULL)
69                         sscanf(t,"%u",&b);
70                 else
71                         b = 0;
72                 
73                 t = cgiGetValue(cgi, "c");
74                 if (t != NULL)
75                         sscanf(t,"%u",&c);
76                 else
77                         c = 0;
78                 
79                 t = cgiGetValue(cgi, "d");
80                 if (t != NULL)
81                         sscanf(t,"%u",&d);
82                 else
83                         d = 0;
84                 
85                 t = cgiGetValue(cgi, "e");
86                 if (t != NULL)
87                         sscanf(t,"%u",&e);
88                 else
89                         e = 0;
90                 
91                 t = cgiGetValue(cgi, "f");
92                 if (t != NULL)
93                         sscanf(t,"%u",&f);
94                 else
95                         f = 0;
96                 
97                 r = bluenh(in_path, out_tmp, a, b, c, d, e, f);
98                 if (r)
99                         break;
100                 
101                 r1 = send_file(out_tmp, "image/png", 0);
102         } while (0);
103         if (r)
104         {
105                 r1 = send_data(nh, nh_size, "image/png", 500);
106         }
107         r2 = rm(in_tmp);
108         r3 = rm(out_tmp);
109         if (r)
110                 return r;
111         if (r1)
112                 return r1;
113         if (r2)
114                 return r2;
115         if (r3)
116                 return r3;
117         return 0;
118 }
119
120 int bluenh (char *inpix, char *outpix, unsigned a, unsigned b, unsigned c, unsigned d, unsigned e, unsigned f)
121 {
122         char ca[4];
123         char cb[4];
124         char cc[4];
125         char cd[4];
126         char ce[4];
127         char cf[4];
128         
129         pid_t sub;
130         int r;
131         
132         snprintf(ca, 4, "%u", a);
133         snprintf(cb, 4, "%u", b);
134         snprintf(cc, 4, "%u", c);
135         snprintf(cd, 4, "%u", d);
136         snprintf(ce, 4, "%u", e);
137         snprintf(cf, 4, "%u", f);
138         
139         sub = fork();
140         if (sub == 0)
141         {
142                 r = execl(BLUENH_PATH, inpix, outpix, ca, cb, cc, cd, ce, cf, (char *)0);
143                 exit(r);
144         }
145         waitpid(sub, &r, 0);
146         return r;
147 }
148
149 /*
150
151
152
153
154 //  //  //Download images and 
155    //  // ENHANCE them!
156   //  //  
157  //  //   Requires cgilib
158 //  //  //line 146 of cgi.h
159    //  // must be changed
160   //  //  from "extern }"
161  //  //   to "}"
162 //  //  //
163    //  // ~~bicyclesonthemoon
164
165 #include <stdio.h>
166 #include <stdlib.h>
167 #include <unistd.h>
168 #include <string.h>
169 #include <sys/wait.h>
170 #include <sys/stat.h>
171
172 #define WGET_PATH  "/usr/bin/wget"
173 #define ENH_PATH   "./bluenh"
174 #define ENH_PATH2  "./bluenh2"
175 #define RM_PATH    "/bin/rm"
176 #define LOG_PATH   "/eizm/log/bluenh/bluenh.log"
177 #define USERAGENT  "Bluenhancement\x20(1190.bicyclesonthemoon.dnsd.info/bluenh/bluenh.htm)"
178 #define MSTD_PATH  "/pro/bluenh/nh.png"
179
180 int main (int argc, char *argv[]);
181 void mustard(int mustard);
182
183 FILE *pix;
184
185 char url[1024];
186 char path[256];
187 s_cgi *cgi;
188 int t;
189 pid_t sub;
190
191 int main (int argc, char *argv[])
192 {
193         unsigned char buf[1024];
194         struct stat st;
195         unsigned long p;
196         unsigned short q;
197         char **up;
198         bool enh2;
199         
200         cgi=cgiInit();
201         
202         up=cgiGetFiles(cgi);
203         if (up)
204         {
205                 sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
206                 sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
207         }
208         else
209         {
210                 sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
211                 sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
212                 
213                 //Why is  there no spawnl() in linux?
214                 sub=fork();
215                 if(sub==0)
216                 {
217                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path,url,(char *)0);
218                         exit(t);
219                 }
220                 waitpid(sub,&t,0);
221                 if(t)mustard(t);
222         }
223         
224         enh2=(cgiGetValue(cgi,"bluenh2")!=NULL);
225         sub=fork();
226         if(sub==0)
227         {
228                 t=execl(enh2?ENH_PATH2:ENH_PATH,enh2?ENH_PATH2:ENH_PATH,path,path,cgiGetValue(cgi,"a")!=NULL?cgiGetValue(cgi,"a"):"0",cgiGetValue(cgi,"b")!=NULL?cgiGetValue(cgi,"b"):"127",cgiGetValue(cgi,"c")!=NULL?cgiGetValue(cgi,"c"):"255",cgiGetValue(cgi,"d")!=NULL?cgiGetValue(cgi,"d"):"0",cgiGetValue(cgi,"e")!=NULL?cgiGetValue(cgi,"e"):"127",cgiGetValue(cgi,"f")!=NULL?cgiGetValue(cgi,"f"):"255","q",(char *)0);
229                 exit(t);
230         }
231         waitpid(sub,&t,0);
232         if(t)mustard(t);
233         
234         pix=fopen(path,"rb");
235         if(pix==NULL)
236                 mustard(123);
237         fstat(fileno(pix),&st);
238         p=st.st_size/1024;
239         q=st.st_size%1024;
240         
241         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
242         printf("Content-type: image/png\n\n");
243         for(unsigned long i=0;i<p;++i)
244         {
245                 fread(buf,1,1024,pix);
246                 fwrite(buf,1,1024,stdout);
247         }
248         if(q!=0)
249         {
250                 fread(buf,1,q,pix);
251                 fwrite(buf,1,q,stdout);
252         }
253         fclose(pix);
254         fflush(stdout);
255         mustard(0);//no mustard
256 }
257
258 void mustard(int mustard)
259 {
260         if(mustard)
261         {
262                 cgiRedirect(MSTD_PATH);
263         }
264         sub=fork();
265         if(sub==0)
266         {
267                 t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
268                 exit(t);
269         }
270         waitpid(sub,&t,0);
271         pix=fopen(LOG_PATH,"at");
272         if(pix!=NULL)
273         {
274                 fprintf(pix,"%x %s\n",mustard,url);
275                 fclose(pix);
276         }
277         exit(mustard);
278 }
279
280 */