00001
00002
00003
00004
00005
00006
00007 #ifndef __RAOSOFT_CGI_H
00008 #define __RAOSOFT_CGI_H
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #define RAOSOFT_CGI_VERSION "6.4.2"
00020 #define RAOSOFT_CGI_COPYRIGHT "Copyright 2000-2007 by Raosoft Inc. All Rights Reserved."
00021
00501 #if defined(__MWERKS__) && defined(__INTEL__) && !defined(_Windows)
00502 #define __WIN32__
00503 #ifndef XP_WIN
00504 #define XP_WIN
00505 #endif
00506 #endif
00507
00508 #ifdef __MWERKS__
00509 #pragma once
00510 #endif
00511
00512 #if defined(_WIN32_WCE) || defined(UNDER_CE)
00513 #ifndef __WINCE__
00514 #define __WINCE__
00515 #endif
00516 #ifndef XP_WIN
00517 #define XP_WIN
00518 #endif
00519 #undef __WIN32__
00520 #ifndef UNICODE
00521 #define UNICODE
00522 #endif
00523 #define VPWSCGI
00524 #undef CGI_ISAPI
00525 #undef CGI_FAST
00526 #undef CGI_WINOLD
00527 #undef CGI_ODBC
00528 #elif defined(_Windows) || defined(_WIN32) || defined(WIN32) || defined(__WIN32__) ||defined(_WIN64) || defined(WIN64) || defined(__WIN64__) || defined(_WINDOWS)
00529 #ifndef XP_WIN
00530 #define XP_WIN
00531 #endif
00532 #endif
00533
00534 #ifdef _MSC_VER
00535 #ifndef XP_WIN
00536 #define XP_WIN
00537 #endif
00538 #ifdef _UNICODE
00539 #ifndef UNICODE
00540 #define UNICODE
00541 #endif
00542 #endif
00543 #ifndef __WINCE__
00544 #define access _access
00545 #define getcwd _getcwd
00546 #define read _read
00547 #define write _write
00548 #define close _close
00549 #define open _open
00550 #define lseek _lseek
00551 #define getcwd _getcwd
00552 #endif
00553 #endif
00554
00555 #ifdef __DLL__
00556 #define CGI_ISAPI
00557 #endif
00558
00559
00560 #ifdef __WINCE__
00561 #else
00562 #include <sys/stat.h>
00563 #endif
00564
00565
00566 #if defined(__WINCE__)
00567 #include <windows.h>
00568 #else
00569
00570 #ifdef XP_WIN
00571 #define _WINSOCKAPI_
00572 #include <windows.h>
00573 #if defined (__BORLANDC__)
00574 #include <dir.h>
00575 #include <process.h>
00576 #include <io.h>
00577 #elif defined(_MSC_VER)
00578 #include <direct.h>
00579 #include <process.h>
00580 #include <io.h>
00581 #else
00582 #include <unistd.h>
00583 #endif
00584 #endif
00585
00586 #include <time.h>
00587 #include <fcntl.h>
00588 #ifdef XP_POSIX
00589 #include <unistd.h>
00590 #endif
00591 #endif
00592
00593 #include <stddef.h>
00594 #include <string.h>
00595 #include <stdio.h>
00596 #include <stdlib.h>
00597 #include <ctype.h>
00598
00599 #if defined(__cplusplus) && !defined(_cplusplus)
00600 #define _cplusplus __cplusplus
00601 #endif
00602
00603 #define NULLSTR ""
00604
00605 #ifdef CGI_VPWS
00606 #define VPWSCGI
00607 #if defined(CGI_ISAPI) || defined(CGI_WINOLD)|| defined(CGI_FAST)
00608 VPWS is incompatible with CGI_ISAPI, CGI_WINOLD, and CGI_FAST
00609 #endif
00610 #endif
00611
00612 #ifdef CGI_WINOLD
00613 #if defined(CGI_ISAPI) || defined(CGI_FAST)
00614 You may only define one of CGI_FAST, CGI_ISAPI, and CGI_WINOLD
00615 #endif
00616 #define WINCGI
00617 #endif
00618
00619 #ifdef CGI_ISAPI
00620 #if defined(CGI_FAST) || defined(CGI_WINOLD)
00621 You may only define one of CGI_FAST, CGI_ISAPI, and CGI_WINOLD
00622 #endif
00623 #define ISAPICGI
00624 #include <httpext.h>
00625 #endif
00626
00627 #ifdef CGI_FAST
00628 #if defined(CGI_ISAPI) || defined(CGI_WINOLD)
00629 You may only define one of CGI_FAST, CGI_ISAPI, and CGI_WINOLD
00630 #endif
00631 #define FASTCGI
00632 #include "fcgiapp.h"
00633 #endif
00634
00635 #if defined(CGI_WINOLD)|| defined(CGI_VPWS) || defined(CGI_ISAPI) || defined(CGI_FAST)
00636 #else
00637 #define CONSOLECGI
00638 #endif
00639
00640 #ifdef CGI_MESSAGE
00641
00642 #if defined(FASTCGI)
00643 #pragma message ( "Building EZSurvey for the FastCGI library" )
00644 #endif
00645 #if defined (ISAPICGI)
00646 #pragma message ( "Building EZSurvey for ISAPI" )
00647 #endif
00648 #if defined (WINCGI)
00649 #pragma message ( "Building EZSurvey for WIN-CGI" )
00650 #endif
00651 #if defined (VPWSCGI)
00652 #pragma message ( "Building EZSurvey for Very Personal Web Server" )
00653 #endif
00654 #if defined(CONSOLECGI)
00655 #pragma message ( "Building EZSurvey for standard CGI" )
00656 #endif
00657 #if defined (CGI_ODBC)
00658 #pragma message ( "Including ODBC support" )
00659 #endif
00660 #if defined (CGI_ALLOW)
00661 #pragma message ( "Including domain name access restriction support" )
00662 #endif
00663
00664 #endif
00665
00666 #ifndef NULL
00667 #define NULL null
00668 #endif
00669
00670 #ifndef MAXPATH
00671 #define MAXPATH 1024
00672 #endif
00673
00674 #if defined(__WINCE__) || defined (__WIN32__)
00675 #ifndef XP_WIN
00676 #define XP_WIN
00677 #endif
00678 #endif
00679
00680 #ifndef XP_WIN
00681 #define XP_POSIX
00682 #endif
00683
00684 #define MAXBUF 8196
00685 #define MAXDATA 65535
00686
00687
00688 #ifdef _cplusplus
00689 extern "C" {
00690 #endif
00691
00700 typedef struct
00701 {
00702 char * name;
00703 char * value;
00704 int last;
00705 } CGINameValue;
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00722 typedef struct {
00723 char* data;
00724 size_t size, used;
00725 void *Next, *Last;
00726 } TextBuffer;
00727
00728 TextBuffer * NewBuffer(int length);
00729 void DeleteBuffer(TextBuffer* t) ;
00730
00731 size_t BufferRead(TextBuffer* t, char* out,size_t length);
00732 size_t BufferWrite(TextBuffer* start,char* text);
00733 size_t BufferWriteL(TextBuffer* start,char* text,size_t length);
00734
00735 size_t BufferSize(TextBuffer* t);
00736 char* CopyBuffer(TextBuffer* t);
00737
00738 #ifdef CGI_ODBC
00739
00742 typedef struct
00743 {
00744 TextBuffer* ErrorMessage;
00745 void* Opaque;
00746 } SQLDB;
00747
00749 SQLDB* ODBCConnect(char * service, char* uid, char* pwd, TextBuffer* errors);
00750 int ODBCDisconnect(SQLDB* database);
00751
00752 int ODBCSelect(SQLDB* database,char* command,void** handle);
00753 CGINameValue* ODBCRead(void* handle);
00754 int ODBCFinish(void* handle,int *numModified);
00755
00756 CGINameValue* ODBCQuery(SQLDB* database, char* command,int *numModified);
00757
00758 int ODBCExec(SQLDB* database,char* command,int *numModified);
00759 int ODBCCommit(SQLDB* database);
00760 #endif
00761
00762 #define CGIMALLOC(s) (void*)malloc(s);
00763 #define CGIFREE(p) {if (p) free(p); p=NULL;}
00764 #define CGIFOPEN(n,f) fopen(n,f);
00765 #define CGIFCLOSE(h) {if (h) {fclose(h); h=0;}}
00766
00771
00772 #ifdef WINCGI
00773 int SplitArgs (char *str, char *argv[],int max);
00774 #endif
00775
00776 void swapchars(char * str, char find, char repl);
00777
00778 #ifndef __BORLANDC__
00779 int stricmp(const char * a, const char * b);
00780 int strnicmp(const char * a,const char * b, size_t length);
00781 #endif
00782 #ifndef strdup
00783 char * strdup(const char *s);
00784 #endif
00785
00786 char * strdup3(char* a,char* b,char*c);
00787
00788 char * strndup(char *s,size_t length);
00789
00790 char * strn2istr(char *s1, char *s2,size_t l2);
00791
00792 char * stristr(char *s1, char *s2);
00793
00796 int strmatch(char * c1,char * c2, size_t len,char* end,int cs);
00797
00799 void ClearToken(char * c,char * code);
00800
00802 int HasToken(char * c, char * code,int * loc);
00803 int HasTokenI(char * c, char * code,int * loc);
00804
00811 char * PopList2(char* list,int end);
00816 char * PopList(CGINameValue*list,char* key,int end);
00817
00821 void ExpandLocalPath(char * argv0,char * out,char*in,char* extension);
00822
00824 FILE* FileOpen(char* argv0, char* file,char* extension);
00825
00827 int ExpandUrl(char *url);
00828
00829 #ifdef _Windows
00830 #define SLASH "\\"
00831 #else
00832 #define SLASH "/"
00833 #endif
00834 #ifdef _cplusplus
00835 }
00836 #endif
00837
00851
00852 #if defined(VPWSCGI)
00853 #ifdef _cplusplus
00854 class InternetStream;
00855 #else
00856 typedef void InternetStream;
00857 #endif
00858 #endif
00859 typedef struct
00860 {
00861 #ifdef FASTCGI
00862 FCGX_Stream * strp;
00863 FCGX_ParamArray envp;
00864 #endif
00865 #ifdef ISAPICGI
00866 EXTENSION_CONTROL_BLOCK *ecb;
00867 char buffer[MAX_PATH];
00868 DWORD pos;
00869 #endif
00870 #ifdef VPWSCGI
00871 InternetStream* s;
00872 #endif
00873 FILE* f;
00874 TextBuffer* t;
00875 } EZSSTREAM;
00876 #define STREAM EZSSTREAM *
00877
00878 #define DATABASE FILE
00879
00880 #ifdef _cplusplus
00881 extern "C" {
00882 #endif
00883
00884
00885
00886
00887
00900 #ifdef VPWSCGI
00901
00902 extern int CGImainLIST(char * argv0,CGINameValue* Params,STREAM htmlout);
00903 extern int CGImainEZS(char * argv0,CGINameValue* Params,STREAM htmlout);
00904 extern int CGImainWF(char * argv0,char* url,CGINameValue* Params,STREAM htmlout);
00905
00906 #else
00907 extern int CGImain(char * progname,
00908 CGINameValue* argv,
00909 STREAM htmlout);
00910 #endif
00911
00912 int SendCGIHeader(STREAM htmlout,char * header);
00913
00914 #define CGI_DEFAULT_HEADER "Content-type: text/html; charset: utf-8\n\n"
00915
00918 #define CGI_ALLOW_NONE 0
00919 #define CGI_ALLOW_READ 1
00920 #define CGI_ALLOW_UPDATE 2
00921 #define CGI_ALLOW_APPEND 4
00922 #define CGI_ALLOW_DELETE 8
00923 #define CGI_ALLOW_WRITE 16
00924 #define CGI_ALLOW_ADMIN 32
00925 #define CGI_ALLOW_REPORT 64
00926 #define CGI_ALLOW_ALL 127
00927
00935 char * GetSetting(CGINameValue*section,char * key, char* def);
00936
00937 #ifndef VPWSCGI
00938 char * GetEnvironment(STREAM htmlout,char* key);
00939 #endif
00940
00941
00942
00943
00944
00946 STREAM NewStream(int size);
00947
00948 void DeleteStream(STREAM d);
00949
00951 char * CopyStream(STREAM s);
00952
00953 void HTMLEscape(STREAM htmlout, char* out);
00954
00955 void HTMLWriteL(STREAM htmlout, char* out,size_t length);
00956
00957
00958
00959
00960
00961 void HTMLWrite(STREAM htmlout, char* out);
00962
00963
00964 void HTMLSend(STREAM htmlout, TextBuffer* in);
00965
00969 void HTMLPrintf(STREAM htmlout,char * formatting, ...);
00970
00975 int HTMLWriteFile(STREAM htmlout,char * filename);
00976 void HTMLWriteFileF(STREAM htmlout,FILE* f);
00980 int SaveDataToASC(CGINameValue *data,char * filename,int*pos);
00982 int SaveDataToXML(CGINameValue *data,char * filename,int*pos);
00986 void WriteXML(FILE* f,CGINameValue* data);
00987
00999 int SendMail(char * recip, char * subject,
01000 char * textnote, char* server, char* profile, char* name, char* password);
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01021 #ifndef CGI_FAST
01022 void SetLoggingLevel(int i);
01023
01024 void LogMessage(char * c);
01025
01026 void LogError(char * c);
01027
01028 void LogTrx(CGINameValue* data);
01029
01030 void LogConnection(char* program,char* host);
01031
01032 void LogStartup(STREAM htmlout,char* argv0);
01033 #endif
01034
01038 int CheckAllowDeny(char * host, char * allow,char * deny);
01039
01041 int GetSecurityFlags(char* argv0,char* host,char* database,char* pwd);
01042
01044 void GetTime(char * date,char * time,int type);
01045
01046
01047
01048
01049
01050 void ResetList(CGINameValue* header);
01051
01052 int DatabaseSaveNewRecord(char * filename, CGINameValue * values,int*pos);
01053 int DatabaseSaveIndex(char * indexfile, CGINameValue * Params,int pos);
01054 int DatabaseOpen(char * filename,DATABASE **database,CGINameValue **header,int extra, int rw);
01055 int DatabaseReset(DATABASE * database);
01056 int DatabaseFFD(DATABASE * database,int skiprecords);
01057
01059 int DatabaseReadRecord(DATABASE * database, CGINameValue * v,int*flags);
01061 int DatabaseDeleteRecord(char * database, int record);
01064 int DatabaseFindNextRecord(DATABASE * database,CGINameValue* header,
01065 CGINameValue* cgidata,
01066 char* query);
01068 int DatabaseFindFast(DATABASE * database,CGINameValue* header,
01069 CGINameValue* query,int* pos);
01070
01077 int DatabaseSearchRecord(DATABASE * database,char * find,int casesensitive);
01078
01080 void DatabaseClose(DATABASE * database, CGINameValue * header);
01081
01082
01083
01084
01085
01088 int ReadUntilWordS2(FILE * in,STREAM out,char * check1, char * check2);
01089
01090 int ReadUntilWordS(FILE * in,STREAM out,char * check);
01091
01092 int ReadUntilWord2(FILE * in,char * out, size_t max,char * check1, char * check2);
01093 int ReadUntilWord(FILE * in,char * out, size_t max,char * check);
01094
01096 int ReadNextWord(FILE* source,char* name,size_t length);
01097
01099 int ReadNextToken(FILE* source,char*buf, int size,int end,int * isSyntax);
01100
01104 int ReadUntilChar(FILE * in, char * out, size_t max, char * stop, int skip);
01105
01106
01107
01108
01109
01110
01112 CGINameValue* NewNVP(size_t count);
01113
01116 void ExtendNVP(CGINameValue* v, size_t count);
01117
01119 void DeleteNVP(CGINameValue* v);
01120
01121 void DebugShowNVP(STREAM htmlout,CGINameValue* v);
01122
01123 int FileSize(char* filename);
01124
01126 CGINameValue* ReadHTMLAttributes(FILE * source,size_t max);
01127
01128 CGINameValue* ReadINIFileSection(char * file, char * section,int reserve);
01129
01131 CGINameValue* ReadPairedValues(int argc, char ** argv,int reserve);
01132
01134 CGINameValue* ReadPairedString(char *,char delim,int reserve);
01135
01136
01137
01138
01139
01140 int SOAPExec(char* host, int port, char* function, char* action, char* data);
01141
01142 int WinsockConnect(char* host, int port, unsigned int*s);
01143
01144
01145
01146
01147
01152 char *EvaluateExpression(CGINameValue*v, CGINameValue*v2, char * equation);
01153 char *EvaluateExpressionL(CGINameValue*v, CGINameValue*v2, char* equation, size_t length);
01154
01161 double EvaluateAlgebra(CGINameValue*v, CGINameValue*v2, char * equation);
01162
01163 int IsNumber(char* value,size_t len);
01164
01174 int EvaluateComparison(char* Comparison,CGINameValue* v,CGINameValue* Var2,char** lastc);
01175
01182 int EvaluateLogic(char* line,CGINameValue* Variables,CGINameValue* Var2);
01183
01184 CGINameValue * GetField(CGINameValue * v, char * fieldname);
01185
01187 #define GetFieldValue(d,f) GetSetting(d,f,"")
01188
01189 size_t ListLength(CGINameValue*header);
01190
01194 CGINameValue* CopyList(CGINameValue* header,size_t extra);
01195
01196 CGINameValue* CopyListJoin(CGINameValue* a,CGINameValue* b,size_t extra);
01197
01199 CGINameValue* CopyListDeep(CGINameValue* header,size_t extra);
01200
01205 CGINameValue* CopyListN(CGINameValue* header,size_t extra,...);
01206
01207
01208
01209
01210
01211 #define RemoveFieldValue(a,b) SetFieldValue(a,b,NULL)
01212 #define RemoveField(a,b) RenameField(a,b,NULL)
01213
01214 int RenameField(CGINameValue*d, char* oldname, char* newname);
01215
01216 int SetFieldValue(CGINameValue*d, char* fieldname, char* value);
01217 void SetValue(CGINameValue*d, char* value);
01218 void SetName(CGINameValue*d, char* name);
01219
01220 int IsValueSelected(CGINameValue * data, char * fieldname, char * value);
01221
01222
01223
01224
01231 typedef char*(*ScriptFunctionCall)
01232 (void* Global, char* cmd, int argc, char** argv,CGINameValue* Variables);
01233
01234 typedef struct
01235 {
01236 char * name;
01237 int parameters;
01238 ScriptFunctionCall run;
01239 } ScriptFunction;
01240
01241 typedef struct
01242 {
01243 char * name;
01244 CGINameValue* data;
01245 } ScriptObject;
01246
01247 typedef struct
01248 {
01249 STREAM htmlout;
01250 int endchar, lastLine, curpos;
01251 CGINameValue* config;
01252 char* argv0;
01253 ScriptObject* Objects;
01254 ScriptFunction* Functions,* F2;
01255 } ScriptEnvironment;
01256
01257
01258 extern char* JStoFixed(ScriptEnvironment* Global,char* cmd, int argc, char** argv, CGINameValue* Params);
01259 extern char* JSsubstr(ScriptEnvironment* Global,char* cmd, int argc, char** argv, CGINameValue* Params);
01260 extern char* JSIndexOf(ScriptEnvironment* Global,char* cmd, int argc, char** argv, CGINameValue* Params);
01261 extern char* JSstrlen(ScriptEnvironment* Global,char* cmd, int argc, char** argv, CGINameValue* Params);
01262 extern char* JSPrint(ScriptEnvironment* Global,char* cmd, int argc, char** argv, CGINameValue* Params);
01263 extern char* JSEval(ScriptEnvironment* Global, char* cmd, int argc, char** argv, CGINameValue* Params);
01264 extern char* JSReport(ScriptEnvironment* Env, char* cmd, int argc, char** argv, CGINameValue* Params);
01265 extern char* JSRandom(ScriptEnvironment* Env, char* cmd, int argc, char** argv, CGINameValue* Params);
01266 extern char* JSHasRecord(ScriptEnvironment* Env, char* cmd, int argc, char** argv, CGINameValue* Params);
01267 extern char* JSGetEnv(ScriptEnvironment* Env, char* cmd, int argc, char** argv, CGINameValue* Params);
01268 extern char* JSToNumber(ScriptEnvironment* Env, char* cmd, int argc, char** argv, CGINameValue* Params);
01269 extern char* JSHTMLEscape(ScriptEnvironment* Env, char* cmd, int argc, char** argv, CGINameValue* Params);
01270 extern char* JSTimeStamp(ScriptEnvironment* Env, char* cmd, int argc, char** argv, CGINameValue* Params);
01271
01272
01273
01274 int RunScript(ScriptEnvironment* Env, FILE* script, CGINameValue* Variables);
01275
01276 int PrintScriptError(STREAM htmlout,ScriptEnvironment* Env, FILE* script, int errcode);
01277
01278
01279
01280
01293 int RunReport(char* argv0,
01294 STREAM htmlout, FILE* source,
01295 CGINameValue * cgidata,
01296 CGINameValue * dbdata,
01297 CGINameValue * unused,
01298 int preset,
01299 char* stop);
01300
01301 int RunReportF(char* argv0,
01302 STREAM htmlout, FILE* source,
01303 CGINameValue * cgidata,
01304 CGINameValue * dbdata,
01305 CGINameValue * unused,
01306 int preset,
01307 char* stop,
01308 char* lastData,
01309 ScriptFunction*extra);
01310
01319
01320
01321
01322
01327 #ifdef WINCGI
01328 int InitWindowsCGI(char * setupfile,STREAM* htmlin,STREAM* htmlout);
01329 void DoneWindowsCGI(STREAM htmlin,STREAM htmlout);
01330 #endif
01331
01332 #ifndef ISAPICGI
01333
01334 int CheckCGISendMethod(STREAM htmlin);
01335
01336 CGINameValue* ReadCGIGetData(STREAM htmlin,int extra);
01337
01338 CGINameValue* ReadCGIPostData(STREAM htmlin,int extra);
01339 #endif
01340
01341 int B64encode(char* in, char* out);
01342 void B64decode(char* text);
01343 void qpdecode(char* text);
01344 void UTF7toUTF8(char* text);
01345 size_t UTF8ToUCS2C(char* in,int *c);
01346
01347 char* ANSItoUTF8(char* s);
01348 int ContainsANSIChars(char* s);
01350 #ifdef _cplusplus
01351 }
01352 #endif
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371 #endif