00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "cgi.h"
00018
00019 #include "math.h"
00020
00021 void Instructions(STREAM htmlout,CGINameValue*Params,int pw)
00022 {
00023 HTMLWrite(htmlout,"<form method=post>\n");
00024 HTMLWrite(htmlout,"Database: <input name=DATABASE value=\"");
00025 HTMLWrite(htmlout,GetFieldValue(Params,"DATABASE"));
00026 HTMLWrite(htmlout,"\">\n");
00027 HTMLWrite(htmlout,"<br>Plot names: <input name=PLOT value=\"");
00028 HTMLWrite(htmlout,GetFieldValue(Params,"PLOT"));
00029 HTMLWrite(htmlout,"\">\n");
00030 HTMLWrite(htmlout,"<br>Password: <input name=_PASSWORD value=\"\">\n");
00031
00032 HTMLWrite(htmlout,"<br>Query: <input name=QUERY value=\"");
00033 HTMLWrite(htmlout,GetFieldValue(Params,"QUERY"));
00034 HTMLWrite(htmlout,"\">\n");
00035 HTMLWrite(htmlout,"<br>Crosstabs: <input name=CROSSTAB value=\"");
00036 HTMLWrite(htmlout,GetFieldValue(Params,"CROSSTAB"));
00037 HTMLWrite(htmlout,"\">\n");
00038 HTMLWrite(htmlout,"<br><input type=submit>\n");
00039
00040 HTMLWrite(htmlout,"</form>\n\n");
00041 }
00042
00043
00044 int CalcCountSummary(CGINameValue* Params,char* fieldname)
00045 {
00046 CGINameValue* start;
00047 start = GetField(Params,fieldname);
00048
00049
00050 if (start)
00051 if (start->value)
00052 return *start->value ? 1 : 0;
00053
00054 if (strchr(fieldname,'_'))
00055 {
00056 char* name = strdup(fieldname);
00057 char* code = strchr(name,'_');
00058 size_t l;
00059 *code=0;
00060 l = strlen(name);
00061 start = GetField(Params,name);
00062 if (!start) {CGIFREE(name); return 0; }
00063 while (start->name)
00064 {
00065 if (strncmp(start->name,name,l))
00066 break;
00067
00068 if (start->value)
00069 if ((start->name[l] == '_' || start->name[l] == 0) && *start->value)
00070 if (HasToken(start->value,code+1,0))
00071 {CGIFREE(name); return 1;}
00072
00073 start++;
00074 }
00075 CGIFREE(name);
00076 }
00077 else
00078 {
00079 start = GetField(Params,fieldname);
00080 if (!start)
00081 {
00082 size_t l = strlen(fieldname);
00083 start = Params;
00084 while (start->name)
00085 {
00086 if (!strncmp(start->name,fieldname,l))
00087 if (start->value)
00088 if ((start->name[l] == '_' || start->name[l] == 0) && *start->value)
00089 return 1;
00090
00091 start++;
00092 }
00093 }
00094 }
00095
00096 return 0;
00097 }
00098
00099 void UpdateStatistics(CGINameValue* count,CGINameValue* sum,CGINameValue* record,
00100 CGINameValue* xtab,CGINameValue* xcount,CGINameValue* xsum)
00101 {
00102 size_t j;
00103 if (count)
00104 for(j=0; count[j].name; j++)
00105 {
00106 int x = atoi(count[j].value);
00107 int d = CalcCountSummary(record,count[j].name);
00108 x += d;
00109
00110 if (d)
00111 {
00112 char temp[32];
00113 sprintf(temp,"%d",x);
00114 SetValue(count+j,temp);
00115
00116 if (xtab)
00117 {
00118 size_t i;
00119 char* name;
00120 CGINameValue* v;
00121 for(i=0; xtab[i].name; i++)
00122 {
00123 name = strdup(xtab[i].value);
00124 if (EvaluateLogic(name,record,0))
00125 {
00126 CGIFREE(name);
00127 name = strdup3(xtab[i].name,".",count[j].name);
00128 v = GetField(xcount,name);
00129 sprintf(temp,"%d",atoi(v->value) + d);
00130 SetValue(v,temp);
00131 }
00132 CGIFREE(name);
00133 }
00134 }
00135 }
00136 }
00137
00138 if (sum)
00139 for(j=0; sum[j].name; j++)
00140 {
00141 double x = strtod(sum[j].value,0);
00142 double d = strtod(GetFieldValue(record,sum[j].name),0);
00143 if (x < 10E16 && x > -10E16 && d < 10E16 && d > -10E16)
00144 {
00145 if (d < 10E16 && d > -10E16)
00146 x += d;
00147 }
00148
00149 if (d != 0.0)
00150 {
00151 char temp[64];
00152 sprintf(temp,"%f",x);
00153 SetValue(sum+j,temp);
00154
00155 if (xtab)
00156 {
00157 size_t i;
00158 char* name;
00159 CGINameValue* v;
00160 for(i=0; xtab[i].name; i++)
00161 {
00162 name = strdup(xtab[i].value);
00163 if (EvaluateLogic(name,record,0))
00164 {
00165 CGIFREE(name);
00166 name = strdup3(xtab[i].name,".",sum[j].name);
00167 v = GetField(xsum,name);
00168 sprintf(temp,"%f",strtod(v->value,0) + d);
00169 SetValue(v,temp);
00170 }
00171 CGIFREE(name);
00172 }
00173
00174 }
00175 }
00176 }
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 #ifdef VPWSCGI
00196 int CGImainPlot(char * argv0,CGINameValue* Params,STREAM htmlout)
00197 #else
00198 int CGImain(char * argv0,CGINameValue* Params,STREAM htmlout)
00199 #endif
00200 {
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 char * database = GetFieldValue(Params,"DATABASE");
00212
00213 char * plot = GetFieldValue(Params,"PLOT");
00214
00215 char * password = GetFieldValue(Params,"_PASSWORD");
00216
00217 char * query = GetFieldValue(Params,"QUERY");
00218
00219 CGINameValue* config = 0;
00220 CGINameValue* databases = 0;
00221 CGINameValue* plots = 0;
00222 CGINameValue* count = 0;
00223 CGINameValue* sum = 0;
00224 CGINameValue* xtabs = 0;
00225 CGINameValue* xcount = 0;
00226 CGINameValue* xsum = 0;
00227 CGINameValue* total = 0;
00228
00229 size_t xtabcount = 0;
00230
00231 char fn[MAXPATH];
00232 int Security = CGI_ALLOW_REPORT;
00233
00234 size_t i;
00235
00236 SendCGIHeader(htmlout,NULL);
00237
00238 if (database[strcspn(database,"/.\\*?$|'\"")] != 0)
00239 {
00240 HTMLWrite(htmlout,"Bad database name: ");
00241 HTMLWrite(htmlout,database);
00242 return 104;
00243 }
00244
00245 if (!*database || !*plot)
00246 {
00247 Instructions(htmlout,Params,0);
00248 return 0;
00249 }
00250
00251 databases = ReadPairedString(database,',',0);
00252
00253 for (i=0; databases[i].name; i++)
00254 {
00255 #ifdef CGI_ALLOW
00256 Security &= GetSecurityFlags(argv0,GetFieldValue(Params,"HOST"),database,password);
00257 #else
00258 Security &= GetSecurityFlags(argv0,0,database,password);
00259 #endif
00260 }
00261
00262 if (!(Security & CGI_ALLOW_REPORT))
00263 {
00264 Instructions(htmlout,Params,1);
00265 return 0;
00266 }
00267
00268
00269 databases = ReadPairedString(database,',',0);
00270 plots = ReadPairedString(plot,',',0);
00271
00272 if (databases)
00273 {
00274 ExpandLocalPath(argv0,fn,databases[0].name,".ini");
00275 config = ReadINIFileSection(fn,"DATABASE",0);
00276
00277 if (config == NULL)
00278 {
00279 Instructions(htmlout,Params,1);
00280
00281 HTMLWrite(htmlout,argv0);
00282 HTMLWrite(htmlout,": A configuration file for the database \"<b>");
00283 HTMLEscape(htmlout,database);
00284 HTMLWrite(htmlout,"</b>\"was not found. Please check your spelling and server setup and try again.<P>\n");
00285
00286 LogError("\nCould not open config file ");
00287 LogError(fn);
00288
00289 DeleteNVP(databases);
00290 DeleteNVP(plots);
00291 return 21;
00292 }
00293 }
00294
00295 if (config == NULL)
00296 {
00297 Instructions(htmlout,Params,1);
00298 return 21;
00299 }
00300 else
00301 {
00302 char statfile[MAXPATH];
00303 char *statistics = GetSetting(config,"SAVESTATNAME",NULLSTR);
00304 ExpandLocalPath(argv0,statfile,statistics,NULLSTR);
00305 count = ReadINIFileSection(statfile, "COUNT",0);
00306 sum = ReadINIFileSection(statfile, "SUM",0);
00307
00308
00309 if (count)
00310 for (i=0; count[i].name; i++)
00311 SetValue(count+i,"0");
00312
00313 if (sum)
00314 for (i=0; sum[i].name; i++)
00315 SetValue(sum+i,"0");
00316 }
00317
00318 xtabs = ReadINIFileSection(fn,"CROSSTAB",0);
00319 if (xtabs == NULL)
00320 {
00321 char* c = GetFieldValue(Params,"CROSSTAB");
00322 if (c)
00323 xtabs = ReadPairedString(c,';',0);
00324 }
00325
00326 if (xtabs)
00327 {
00328 if (count)
00329 {
00330 size_t max = 0;
00331 size_t j=0;
00332 for (i=0; count[i].name; i++) max++;
00333 xcount = NewNVP(xtabcount * max);
00334 for (i=0; i<max; i++)
00335 for (j=0; j<xtabcount; j++)
00336 {
00337 xcount[i*xtabcount + j].name = strdup3(xtabs[j].name,".",count[i].name);
00338 xcount[i*xtabcount + j].value = strdup("0");
00339 }
00340 }
00341
00342 if (sum)
00343 {
00344 size_t max = 0;
00345 size_t j = 0;
00346 for (i=0; sum[i].name; i++) max++;
00347 xsum = NewNVP(xtabcount * max);
00348 for (i=0; i<max; i++)
00349 for (j=0; j<xtabcount; j++)
00350 {
00351 xsum[i*xtabcount + j].name = strdup3(xtabs[j].name,".",sum[i].name);
00352 xsum[i*xtabcount + j].value = strdup("0");
00353 }
00354 }
00355 }
00356
00357 for (i=0; databases[i].name; i++)
00358 {
00359 FILE * db;
00360 CGINameValue* record;
00361
00362 ExpandLocalPath(argv0,fn,databases[i].name,NULLSTR);
00363 if (!DatabaseOpen(fn,&db,&record,0,0))
00364 {
00365 continue;
00366 }
00367
00368 if (*query)
00369 {
00370
00371 while (DatabaseFindNextRecord(db,record,Params,query))
00372 {
00373 UpdateStatistics(count,sum,record,xtabs,xcount,xsum);
00374 }
00375 }
00376 else
00377 {
00378 int flags=0;
00379 while (DatabaseReadRecord(db,record,&flags))
00380 {
00381 if (flags) continue;
00382 UpdateStatistics(count,sum,record,xtabs,xcount,xsum);
00383 }
00384 }
00385
00386 DatabaseClose(db,record);
00387 }
00388
00389 if (sum)
00390 {
00391 for (i=0;sum[i].name;i++)
00392 {
00393 char * x = strdup3(sum[i].name,".SUM","");
00394 CGIFREE(sum[i].name);
00395 sum[i].name = x;
00396 }
00397 }
00398
00399 if (xsum)
00400 {
00401 for (i=0;xsum[i].name;i++)
00402 {
00403 char * x = strdup3(xsum[i].name,".SUM","");
00404 CGIFREE(xsum[i].name);
00405 xsum[i].name = x;
00406 }
00407 }
00408
00409
00410
00411 if (plots)
00412 {
00413 if (count) total = CopyListJoin(count,total,0);
00414 if (sum) total = CopyListJoin(sum,total,0);
00415 if (xcount) total = CopyListJoin(xcount,total,0);
00416 if (xsum) total = CopyListJoin(xsum,total,0);
00417
00418 for (i=0;plots[i].name; i++)
00419 {
00420 FILE* report;
00421 ExpandLocalPath(argv0,fn,plot,".plot");
00422 report = fopen(fn,"rt");
00423 if (!report)
00424 {
00425 ExpandLocalPath(argv0,fn,plot,".rep");
00426 report = fopen(fn,"rt");
00427 }
00428 if (!report)
00429 {
00430 ExpandLocalPath(argv0,fn,plot,".html");
00431 report = fopen(fn,"rt");
00432 }
00433 if (report)
00434 {
00435 RunReport(argv0,htmlout,report,Params,total,0,0,0);
00436 fclose(report);
00437 }
00438 }
00439 }
00440 else
00441 {
00442
00443
00444
00445
00446 HTMLWrite(htmlout,"<pre>");
00447 if (count) DebugShowNVP(htmlout,count);
00448 if (sum) DebugShowNVP(htmlout,sum);
00449 if (xcount) DebugShowNVP(htmlout,xcount);
00450 if (xsum) DebugShowNVP(htmlout,xsum);
00451 HTMLWrite(htmlout,"</pre>");
00452
00453 }
00454
00455 DeleteNVP(count);
00456 DeleteNVP(sum);
00457 DeleteNVP(xcount);
00458 DeleteNVP(xsum);
00459 DeleteNVP(xtabs);
00460 DeleteNVP(databases);
00461 DeleteNVP(plots);
00462 DeleteNVP(total);
00463 return 0;
00464 }