Rev 8899 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8899 | Rev 12248 | ||
---|---|---|---|
Line 38... | Line 38... | ||
38 | gdImagePtr image=NULL; |
38 | gdImagePtr image=NULL; |
39 | int testcolor; |
39 | int testcolor; |
40 | 40 | ||
41 | void fbase(void) |
41 | void fbase(void) |
42 | { |
42 | { |
43 |
|
43 | char *p, *m, *p1, *p2; |
44 |
|
44 | p=getenv("w_insdraw_filebase"); |
45 |
|
45 | m=getenv("module_dir"); if(m==NULL) m=""; |
46 |
|
46 | if(strncmp(m,"modules/",strlen("modules/"))!=0) { |
47 |
|
47 | snprintf(filebase,sizeof(filebase),"gifs"); return; |
48 |
|
48 | } |
49 |
|
49 | if(p==NULL || *p==0) { |
50 |
|
50 | if(strncmp(m,"modules/adm/",strlen("modules/adm"))==0) |
51 |
|
51 | snprintf(filebase,sizeof(filebase),"%s/insdraw gifs",m); |
52 |
|
52 | else |
53 |
|
53 | snprintf(filebase,sizeof(filebase),"%s gifs",m); |
54 |
|
54 | return; |
55 |
|
55 | } |
56 |
|
56 | for(p1=find_word_start(p); *p1; p1=find_word_start(p2)) { |
57 |
|
57 | p2=find_word_end(p1); if(*p2) *p2++=0; |
58 |
|
58 | if(strstr(p1,"..")!=NULL) continue; |
59 | snprintf(filebase+strlen(filebase), |
- | |
60 | sizeof(filebase)-strlen(filebase), |
- | |
61 | " %s/%s",m,p1); |
- | |
62 | } |
- | |
63 | snprintf(filebase+strlen(filebase), |
59 | snprintf(filebase+strlen(filebase), |
64 | sizeof(filebase)-strlen(filebase), |
60 | sizeof(filebase)-strlen(filebase), |
- | 61 | " %s/%s",m,p1); |
|
- | 62 | } |
|
- | 63 | snprintf(filebase+strlen(filebase), |
|
- | 64 | sizeof(filebase)-strlen(filebase), |
|
65 |
|
65 | " gifs"); |
66 | } |
66 | } |
67 | 67 | ||
68 | /* File opening: with security */ |
68 | /* File opening: with security */ |
69 | FILE *open4read(char *n) |
69 | FILE *open4read(char *n) |
70 | { |
70 | { |
71 |
|
71 | char *p, *p1, *p2; |
72 |
|
72 | int t; |
73 |
|
73 | FILE *f; |
74 |
|
74 | n=find_word_start(n); |
75 |
|
75 | if(*n==0) return NULL; |
76 |
|
76 | p=filebase; |
77 |
|
77 | p1=n+strlen(n)-4;if(p1<n || strcasecmp(p1,".gif")!=0) t=1; else t=0; |
78 |
|
78 | if(p!=NULL && *p!=0) { |
79 |
|
79 | char pbuf[MAX_LINELEN+1]; |
80 |
|
80 | snprintf(pbuf,sizeof(pbuf),"%s",p); |
81 |
|
81 | p=find_word_start(pbuf); if(strstr(p,"..")!=NULL) return NULL; |
82 |
|
82 | if(*n=='/' || strstr(n,"..")!=NULL) return NULL; |
83 | /* prohibit unusual file/dir names */ |
83 | /* prohibit unusual file/dir names */ |
84 |
|
84 | for(p1=p;*p1;p1++) |
85 |
|
85 | if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL) |
86 |
|
86 | return NULL; |
87 |
|
87 | for(p1=n;*p1;p1++) |
88 |
|
88 | if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL) |
89 |
|
89 | return NULL; |
90 |
|
90 | f=NULL; |
91 |
|
91 | for(p1=p; *p1; p1=find_word_start(p2)) { |
92 |
|
92 | if(*p1=='/') return NULL; |
93 |
|
93 | p2=find_word_end(p1); |
94 |
|
94 | if(*p2) *p2++=0; |
95 |
|
95 | snprintf(namebuf,sizeof(namebuf),"%s/%s",p1,n); |
96 |
|
96 | f=fopen(namebuf,"r"); if(f!=NULL) goto imgtype; |
97 | } |
- | |
98 | p1=getenv("w_wims_session"); |
- | |
99 | if(p1!=NULL && strstr(p1,"..")==NULL && strncmp(n,"insert",6)==0) { |
- | |
100 | snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n); |
- | |
101 | f=fopen(namebuf,"r"); |
- | |
102 | } |
- | |
103 | } |
97 | } |
104 |
|
98 | p1=getenv("w_wims_session"); |
- | 99 | if(p1!=NULL && strstr(p1,"..")==NULL && strncmp(n,"insert",6)==0) { |
|
105 | snprintf(namebuf,sizeof(namebuf),"%s",n); |
100 | snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n); |
106 | f=fopen(namebuf,"r"); |
101 | f=fopen(namebuf,"r"); |
107 | } |
102 | } |
- | 103 | } |
|
- | 104 | else { |
|
- | 105 | snprintf(namebuf,sizeof(namebuf),"%s",n); |
|
- | 106 | f=fopen(namebuf,"r"); |
|
- | 107 | } |
|
108 |
|
108 | imgtype: |
109 |
|
109 | if(t && f!=NULL) { |
110 |
|
110 | char tbuf[1024],sbuf[4096]; |
111 |
|
111 | fclose(f); f=NULL; |
112 |
|
112 | p1=getenv("TMPDIR"); if(p1==NULL || *p1==0) p1="."; |
113 |
|
113 | snprintf(tbuf,sizeof(tbuf),"%s/drawfile_.gif",p1); |
114 |
|
114 | snprintf(sbuf,sizeof(sbuf),"convert %s %s",namebuf,tbuf); |
115 |
|
115 | if (system(sbuf)) fprintf(stderr,"system failed"); |
116 |
|
116 | f=fopen(tbuf,"r"); |
117 |
|
117 | } |
118 |
|
118 | if(f!=NULL) snprintf(oldfile,sizeof(oldfile),"%s",n); |
119 |
|
119 | return f; |
120 | } |
120 | } |
121 | 121 | ||
122 | int getvalue(char *p, int n) |
122 | int getvalue(char *p, int n) |
123 | { |
123 | { |
124 |
|
124 | int t; |
125 |
|
125 | char *p1; |
126 |
|
126 | if(n<0) n=VLEN; |
127 |
|
127 | for(t=0; t<n; t++,p=p1) { |
128 |
|
128 | p1=find_item_end(p); if(*p1) *p1++=0; |
129 |
|
129 | p=find_word_start(p); |
130 |
|
130 | if(*p==0) break; |
131 |
|
131 | v[t]=strevalue(p); |
132 |
|
132 | if(!isfinite(v[t])) exit(-1); |
133 |
|
133 | } |
134 |
|
134 | return t; |
135 | } |
135 | } |
136 | 136 | ||
137 | /* test one condition. Returns 1 if met. */ |
137 | /* test one condition. Returns 1 if met. */ |
138 | int test(char *p) |
138 | int test(char *p) |
139 | { |
139 | { |
140 |
|
140 | char *p1; |
141 |
|
141 | int i,t; |
142 |
|
142 | double d; |
143 |
|
143 | p=find_word_start(p); p1=find_item_end(p); |
144 |
|
144 | if(*p1) *p1++=0; else exit(-1); |
145 |
|
145 | prec=0; |
146 |
|
146 | *p=tolower(*p); |
147 |
|
147 | if(strncasecmp(p,"polygon",3)==0 || *p=='g') { |
148 |
|
148 | int cross; |
149 |
|
149 | double x0,y0,a1,b1,c1,a2,b2,c2,s1,s2,s3,s4; |
150 |
|
150 | t=getvalue(p1,-1); if(t<6) exit(-1); |
151 |
|
151 | cross=0; x0=-19; y0=-19; |
152 |
|
152 | a1=clicky-y0;b1=x0-clickx;c1=clickx*y0-x0*clicky; |
153 |
|
153 | v[t]=v[0];v[t+1]=v[1]; |
154 |
|
154 | for(i=2;i<t+2;i+=2) { |
155 |
|
155 | a2=v[i+1]-v[i-1];b2=v[i-2]-v[i]; |
156 |
|
156 | c2=v[i]*v[i-1]-v[i-2]*v[i+1]; |
157 |
|
157 | s1=a1*v[i-2]+b1*v[i-1]+c1; |
158 |
|
158 | s2=a1*v[i]+b1*v[i+1]+c1; |
159 |
|
159 | s3=a2*x0+b2*y0+c2; |
160 |
|
160 | s4=a2*clickx+b2*clicky+c2; |
161 |
|
161 | if(s1==0) continue; |
162 |
|
162 | if(s1*s2<=0 && s3*s4<=0) cross++; |
163 |
|
163 | } |
164 |
|
164 | return cross&1; |
165 |
|
165 | } |
166 |
|
166 | if(*p=='p' || strncasecmp(p,"point",3)==0) { |
167 |
|
167 | t=getvalue(p1,-1); if(t<2) exit(-1); |
168 |
|
168 | for(i=0;i<t;i+=2) { |
169 |
|
169 | d=sqrt(pow(v[i]-clickx,2)+pow(v[i+1]-clicky,2)); |
170 |
|
170 | if(d<=4) return 1; |
171 |
|
171 | if(d<=7) {prec=1; return 1;} |
172 | } |
- | |
173 | return 0; |
- | |
174 | } |
- | |
175 | if(strncasecmp(p,"rectangle",1)==0) { |
- | |
176 | double x1,x2,y1,y2; |
- | |
177 | if(getvalue(p1,4)!=4) exit(-1); |
- | |
178 | x1=min(v[0],v[2]); x2=max(v[0],v[2]); |
- | |
179 | y1=min(v[1],v[3]); y2=max(v[1],v[3]); |
- | |
180 | if(clickx<x1 || clickx>x2 || clicky<y1 || clicky>y2) return 0; |
- | |
181 | else return 1; |
- | |
182 | } |
- | |
183 | if(strncasecmp(p,"circle",1)==0) { |
- | |
184 | double dist; |
- | |
185 | if(getvalue(p1,3)!=3 || v[2]<0) exit(-1); |
- | |
186 | dist=sqrt(pow(v[0]-clickx,2)+pow(v[1]-clicky,2)); |
- | |
187 | if(dist>v[2]/2) return 0; else return 1; |
- | |
188 | } |
172 | } |
- | 173 | return 0; |
|
- | 174 | } |
|
- | 175 | if(strncasecmp(p,"rectangle",1)==0) { |
|
- | 176 | double x1,x2,y1,y2; |
|
- | 177 | if(getvalue(p1,4)!=4) exit(-1); |
|
- | 178 | x1=min(v[0],v[2]); x2=max(v[0],v[2]); |
|
- | 179 | y1=min(v[1],v[3]); y2=max(v[1],v[3]); |
|
- | 180 | if(clickx<x1 || clickx>x2 || clicky<y1 || clicky>y2) return 0; |
|
- | 181 | else return 1; |
|
- | 182 | } |
|
- | 183 | if(strncasecmp(p,"circle",1)==0) { |
|
- | 184 | double dist; |
|
- | 185 | if(getvalue(p1,3)!=3 || v[2]<0) exit(-1); |
|
- | 186 | dist=sqrt(pow(v[0]-clickx,2)+pow(v[1]-clicky,2)); |
|
- | 187 | if(dist>v[2]/2) return 0; else return 1; |
|
- | 188 | } |
|
189 |
|
189 | if(strncasecmp(p,"ellipse",1)==0) { |
190 |
|
190 | double dist; |
191 |
|
191 | if(getvalue(p1,4)!=4) exit(-1); |
192 |
|
192 | if(v[2]<=0 || v[3]<=0) exit(-1); |
193 |
|
193 | dist=sqrt(pow(2*(v[0]-clickx)/v[2],2)+pow(2*(v[1]-clicky)/v[3],2)); |
194 |
|
194 | if(dist>1) return 0; else return 1; |
195 |
|
195 | return 0; |
196 |
|
196 | } |
197 |
|
197 | if(strncasecmp(p,"bound",1)==0) { |
198 |
|
198 | char *p2; |
199 |
|
199 | FILE *f; |
200 |
|
200 | int c, T; |
201 |
|
201 | p2=find_item_end(p1); if(*p2) *p2++=0; |
202 |
|
202 | T=getvalue(p2,2); if(T!=2 && T!=0) exit(-1); |
203 |
|
203 | p1=find_word_start(p1); *find_word_end(p1)=0; |
204 |
|
204 | if(*p1==0) exit(-1); |
205 |
|
205 | if(strcmp(p1,oldfile)!=0) { |
206 |
|
206 | if(image) gdImageDestroy(image); |
207 |
|
207 | f=open4read(p1); if(f==NULL) exit(-1); |
208 |
|
208 | image=gdImageCreateFromGif(f); fclose(f); |
209 | if(T==0) { |
- | |
210 | testcolor=gdImageGetPixel(image,1,1); |
- | |
211 | } |
- | |
212 | else { |
- | |
213 | testcolor=gdImageColorAllocate(image,2,3,5); |
- | |
214 | gdImageFill(image,clickx,clicky,testcolor); |
- | |
215 | } |
- | |
216 | } |
- | |
217 | if(T==0) { |
209 | if(T==0) { |
218 |
|
210 | testcolor=gdImageGetPixel(image,1,1); |
219 | if(c!=testcolor) return 1; |
- | |
220 | } |
211 | } |
221 | else { |
212 | else { |
- | 213 | testcolor=gdImageColorAllocate(image,2,3,5); |
|
- | 214 | gdImageFill(image,clickx,clicky,testcolor); |
|
- | 215 | } |
|
- | 216 | } |
|
- | 217 | if(T==0) { |
|
222 |
|
218 | c=gdImageGetPixel(image,clickx,clicky); |
223 |
|
219 | if(c!=testcolor) return 1; |
224 |
|
220 | } |
225 |
|
221 | else { |
- | 222 | c=gdImageGetPixel(image,v[0],v[1]); |
|
- | 223 | if(c==testcolor) return 1; |
|
226 | } |
224 | } |
- | 225 | return 0; |
|
- | 226 | } |
|
227 |
|
227 | exit(-1); |
228 | } |
228 | } |
229 | 229 | ||
230 | static |
230 | static |
231 | int oneline(char *p) |
231 | int oneline(char *p) |
232 | { |
232 | { |
233 |
|
233 | char *p1, *p2; |
234 |
|
234 | int t, rev; |
235 | 235 | ||
236 |
|
236 | if(strparchr(p,'|')!=NULL) { |
237 |
|
237 | t=0; for(p1=p;p1;p1=p2) { |
238 |
|
238 | p2=strparchr(p1,'|'); if(p2!=NULL) *p2++=0; |
239 |
|
239 | t|=oneline(p1); |
240 |
|
240 | } |
241 |
|
241 | return t; |
242 |
|
242 | } |
243 |
|
243 | if(strparchr(p,'&')!=NULL) { |
244 |
|
244 | t=1; for(p1=p;p1;p1=p2) { |
245 |
|
245 | p2=strparchr(p1,'&'); if(p2!=NULL) *p2++=0; |
246 |
|
246 | t&=oneline(p1); |
247 | } |
- | |
248 | return t; |
- | |
249 | } |
- | |
250 | p1=find_word_start(p); rev=0; |
- | |
251 | if(*p1=='^') { |
- | |
252 | rev=1; p1=find_word_start(++p1); |
- | |
253 | } |
- | |
254 | if(*p1==0) return rev^1; |
- | |
255 | if(*p1=='(') { |
- | |
256 | p1++; p2=find_matching(p1,')'); |
- | |
257 | if(p2==NULL) exit(-1); |
- | |
258 | *p2=0; return oneline(p1)^rev; |
- | |
259 | } |
247 | } |
- | 248 | return t; |
|
- | 249 | } |
|
- | 250 | p1=find_word_start(p); rev=0; |
|
- | 251 | if(*p1=='^') { |
|
- | 252 | rev=1; p1=find_word_start(++p1); |
|
- | 253 | } |
|
- | 254 | if(*p1==0) return rev^1; |
|
- | 255 | if(*p1=='(') { |
|
- | 256 | p1++; p2=find_matching(p1,')'); |
|
- | 257 | if(p2==NULL) exit(-1); |
|
- | 258 | *p2=0; return oneline(p1)^rev; |
|
- | 259 | } |
|
260 | return test(p1)^rev; |
260 | return test(p1)^rev; |
261 | } |
261 | } |
262 | 262 | ||
263 | /* Returns the number of lines matching the click coordinates */ |
263 | /* Returns the number of lines matching the click coordinates */ |
264 | int main() |
264 | int main() |
265 | { |
265 | { |
266 |
|
266 | char *p, *p2, *p3; |
267 |
|
267 | int i,j; |
268 | 268 | ||
269 |
|
269 | fbase(); |
270 |
|
270 | p=getenv("wims_exec_parm"); if(p==NULL || *p==0) return -1; |
271 |
|
271 | p=find_word_start(p); p2=strchr(p,'\n'); |
272 |
|
272 | if(p2==NULL) p2=p+strlen(p); else *p2++=0; |
273 |
|
273 | p3=strchr(p,','); if(p3==NULL) return -1; else *p3++=0; |
274 |
|
274 | clickx=atoi(p); clicky=atoi(p3); |
275 |
|
275 | for(i=1,p=find_word_start(p2);*p;i++, p=find_word_start(p2)) { |
276 |
|
276 | fprintf(stderr,"Line %d.\n",i); |
277 |
|
277 | p2=strchr(p,'\n'); if(p2==NULL) p2=p+strlen(p); else *p2++=0; |
278 |
|
278 | if(*p==0) continue; |
279 |
|
279 | j=oneline(p); |
280 |
|
280 | if(j) {printf("%d %d",i,prec); return 0;} |
281 |
|
281 | } |
282 |
|
282 | printf("-1"); return 0; |
283 | } |
283 | } |