Annotation of capa/capa51/pProj/capaNewCgi.c, revision 1.2
1.2 ! albertel 1: /* broken start to a new CGI library
! 2: Copyright (C) 1992-2000 Michigan State University
! 3:
! 4: The CAPA system is free software; you can redistribute it and/or
! 5: modify it under the terms of the GNU Library General Public License as
! 6: published by the Free Software Foundation; either version 2 of the
! 7: License, or (at your option) any later version.
! 8:
! 9: The CAPA system is distributed in the hope that it will be useful,
! 10: but WITHOUT ANY WARRANTY; without even the implied warranty of
! 11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
! 12: Library General Public License for more details.
! 13:
! 14: You should have received a copy of the GNU Library General Public
! 15: License along with the CAPA system; see the file COPYING. If not,
! 16: write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
! 17: Boston, MA 02111-1307, USA. */
! 18:
1.1 albertel 19: char *makeword(char *line, char stop)
20: {
21: int x = 0,y;
22: char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
23:
24: for(x=0;((line[x]) && (line[x] != stop));x++)
25: word[x] = line[x];
26:
27: word[x] = '\0';
28: if(line[x]) ++x;
29: y=0;
30:
31: while((line[y++] = line[x++]));
32: return word;
33: }
34:
35: char *fmakeword(FILE *f,char stop,int * cl)
36: {
37: int wsize;
38: char *word;
39: int ll;
40:
41: wsize = 102400;
42: ll=0;
43: word = (char *) malloc(sizeof(char) * (wsize + 1));
44:
45: while(1) {
46: word[ll] = (char)fgetc(f);
47: if(ll==wsize) {
48: word[ll+1] = '\0';
49: wsize+=102400;
50: word = (char *)realloc(word,sizeof(char)*(wsize+1));
51: }
52: --(*cl);
53: if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
54: if(word[ll] != stop) ll++;
55: word[ll] = '\0';
56: return word;
57: }
58: ++ll;
59: }
60: }
61:
62: char x2c(char *what)
63: {
64: register char digit;
65:
66: digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
67: digit *= 16;
68: digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
69: return(digit);
70: }
71:
72: void unescape_url(char *url)
73: {
74: register int x,y;
75:
76: for(x=0,y=0;url[y];++x,++y) {
77: if((url[x] = url[y]) == '%') {
78: url[x] = x2c(&url[y+1]);
79: y+=2;
80: }
81: }
82: url[x] = '\0';
83: }
84:
85: void plustospace(char *str)
86: {
87: register int x;
88:
89: for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
90: }
91:
92: void web_parse_input(char * submissions_str)
93: {
94: int x;
95:
96: for(x=0; x <= m; x++) {
97: if( !strcmp(g_entries[x].name,"CLASS") ) {
98: strncpy(g_class_name,g_entries[x].val,MAX_CLASS_CHAR);
99: }
100: if( !strcmp(g_entries[x].name,"M") ) {
101: sscanf(g_entries[x].val,"%d",&g_run_mode);
102: }
103: if( !strcmp(g_entries[x].name,"SNUM") ) {
104: strncpy(g_student_number,g_entries[x].val,MAX_STUDENT_NUMBER+4);
105: }
106: if( !strcmp(g_entries[x].name,"CAPAID") ) {
107: sscanf(g_entries[x].val,"%d",&g_entered_pin);
108: }
109: if( !strcmp(g_entries[x].name,"SET") ) {
110: sscanf(g_entries[x].val,"%d",&g_set);
111: }
112: if( !strcmp(g_entries[x].name,"VSET") ) {
113: if (g_entries[x].val[0] == '\0') {
114: g_vset=0;
115: } else {
116: sscanf(g_entries[x].val,"%d",&g_vset);
117: }
118: }
119: if( !strcmp(g_entries[x].name,"KND") ) {
120: sscanf(g_entries[x].val,"%d",&g_skind);
121: }
122: if( !strncmp(g_entries[x].name,"INPUT",5) ) {
123: sscanf(g_entries[x].name,"INPUT%d",&q_idx);
124: if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
125: strncpy(g_student_answer[q_idx],g_entries[x].val,MAX_ANSWER_CHAR);
126: }
127: if ( g_student_answer[q_idx][0] != '\0' ) {
128: sprintf(buf,"%d\t%s\t",q_idx,g_student_answer[q_idx]);
129: strcat(submissions_str,buf);
130: }
131: }
132: if( !strncmp(g_entries[x].name,"LAST",4) ) {
133: sscanf(g_entries[x].name,"LAST%d",&q_idx);
134: if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
135: strncpy(g_last_answer[q_idx],g_entries[x].val,MAX_ANSWER_CHAR);
136: }
137: }
138: free(g_entries[x].val);
139: free(g_entries[x].name);
140: }
141: }
142:
143: int web_login()
144: {
145: if( g_entered_pin != 0 ) {
146: g_login_set = capa_PIN(g_student_number,999,g_entered_pin);
147: } else {
148: return WEB_ERR_ENTERED_PIN;
149: }
150:
151: if (!g_login_set) {
152: return WEB_ERR_BADLOGIN;
153: } else {
154: if ( g_login_set > 99 ) { return WEB_ERR_LOGINTOHIGH; }
155: if(g_login_set < g_vset ) {
156: return WEB_ERR_NOTVIEWABLE;
157: }
158: chdir(g_class_fullpath); /* again, to make sure */
159:
160: if ( capa_get_student(g_student_number,&g_student_data) == 0 ) {
161: return WEB_ERR_STUDENT_NOT_EXIST;
162: } else {
163: time(&curtime);
164: if (capa_get_header(&header, g_login_set, wgt, pcr)) {
165: return WEB_ERR_SET_NOT_READY;
166: }
167: if(capa_check_date(CHECK_OPEN_DATE,g_student_data.s_sec,
168: g_login_set) < 0 ) {
169:
170: return WEB_ERR_SET_NOT_OPEN;
171: }
172: }
173: }
174: return (error);
175: }
176:
177: int web_get_input()
178: {
179:
180: envPtr=getenv("REQUEST_METHOD");
181: if (!envPtr ) { return WEB_ERR_REQ_METHOD; }
182: if (strcmp(envPtr,"POST")) { return WEB_ERR_ENV_POST; }
183: envPtr=getenv("CONTENT_TYPE");
184: if (!envPtr ) { return WEB_ERR_CONTENT_TYPE; }
185: if (strcmp(envPtr,"application/x-www-form-urlencoded")) {
186: return WEB_ERR_ENV_CONTENT;
187: }
188: envPtr=getenv("CONTENT_LENGTH");
189: if (!envPtr ) { return WEB_ERR_CONTENT_LENGTH; }
190: content_length=atoi(envPtr);
191:
192: /* read the form into the g_entries array*/
193: for(x=0;content_length && (!feof(stdin));x++) {
194: m=x;
195: g_entries[x].val = fmakeword(stdin,'&',&content_length);
196: plustospace(g_entries[x].val);
197: unescape_url(g_entries[x].val);
198: g_entries[x].name = makeword(g_entries[x].val,'=');
199: }
200:
201: web_parse_input(submissions_str);
202:
203: if ( g_run_mode == WEB_CHECKIN ) {
204: time(&curtime); time_str = ctime(&curtime);
205: time_str[ strlen(time_str)-1 ] = '\0';
206: envPtr=getenv("REMOTE_HOST");
207: envPtr2=getenv("HTTP_USER_AGENT");
208: sprintf(log_str,"%s\t%s\t%s\t%s\t%s\n",g_class_name,g_student_number,
209: time_str,envPtr,envPtr2);
210: if (web_log(log_str) == -1 ) { return WEB_ERR_WEB_LOG; }
211: }
212:
213: getwd(g_cwd);
214:
215: web_getclassdir(&g_cpath, &g_cowner, g_class_name);
216: sprintf(g_class_fullpath,"%s/%s",g_cpath,g_class_name);
217: if( !capa_access(g_class_fullpath, F_OK) == 0 ) { return WEB_ERR_ACCESS; }
218:
219: chdir(g_class_fullpath);
220: if ( g_run_mode == M_CHECKANS) {
221: if (w_log_submissions(g_student_number,g_set,submissions_str) == -1 ) {
222: return WEB_ERR_SUBMMISIONS_LOG;
223: }
224: }
225:
226: result=read_capa_config("capaweb_cgibin_path",buf);
227: if (result != 0 && result != -1) {
228: g_cgibin_path=capa_malloc(strlen(buf)+1,1);
229: strcpy(g_cgibin_path,buf);
230: } else {
231: g_cgibin_path=capa_malloc(strlen("capa-bin")+1,1);
232: strcpy(g_cgibin_path,"capa-bin");
233: }
234: return web_login();
235: }
236:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>