File:  [LON-CAPA] / capa / capa51 / pProj / capaNewCgi.c
Revision 1.3: download - view: text, annotated - select for diffs
Fri Jul 7 18:33:03 2000 UTC (23 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version5-1-2-first_release, HEAD
- updating GPL notices

    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: 
   19:    As a special exception, you have permission to link this program
   20:    with the TtH/TtM library and distribute executables, as long as you
   21:    follow the requirements of the GNU GPL in regard to all of the
   22:    software in the executable aside from TtH/TtM.
   23: */
   24: 
   25: char *makeword(char *line, char stop)
   26: {
   27:     int x = 0,y;
   28:     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
   29: 
   30:     for(x=0;((line[x]) && (line[x] != stop));x++)
   31:         word[x] = line[x];
   32: 
   33:     word[x] = '\0';
   34:     if(line[x]) ++x;
   35:    y=0;
   36: 
   37:     while((line[y++] = line[x++]));
   38:     return word;
   39: }
   40: 
   41: char *fmakeword(FILE *f,char  stop,int * cl)
   42: {
   43:     int wsize;
   44:     char *word;
   45:     int ll;
   46: 
   47:     wsize = 102400;
   48:     ll=0;
   49:     word = (char *) malloc(sizeof(char) * (wsize + 1));
   50: 
   51:     while(1) {
   52:         word[ll] = (char)fgetc(f);
   53:         if(ll==wsize) {
   54:             word[ll+1] = '\0';
   55:             wsize+=102400;
   56:             word = (char *)realloc(word,sizeof(char)*(wsize+1));
   57:         }
   58:         --(*cl);
   59:         if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
   60:             if(word[ll] != stop) ll++;
   61:             word[ll] = '\0';
   62:             return word;
   63:         }
   64:         ++ll;
   65:     }
   66: }
   67: 
   68: char x2c(char *what)
   69: {
   70:     register char digit;
   71: 
   72:     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
   73:     digit *= 16;
   74:     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
   75:     return(digit);
   76: }
   77: 
   78: void unescape_url(char *url)
   79: {
   80:     register int x,y;
   81: 
   82:     for(x=0,y=0;url[y];++x,++y) {
   83:         if((url[x] = url[y]) == '%') {
   84:             url[x] = x2c(&url[y+1]);
   85:             y+=2;
   86:         }
   87:     }
   88:     url[x] = '\0';
   89: }
   90: 
   91: void plustospace(char *str)
   92: {
   93:     register int x;
   94: 
   95:     for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
   96: }
   97: 
   98: void web_parse_input(char * submissions_str)
   99: {
  100:   int x;
  101: 
  102:   for(x=0; x <= m; x++) {
  103:     if( !strcmp(g_entries[x].name,"CLASS") ) {
  104:       strncpy(g_class_name,g_entries[x].val,MAX_CLASS_CHAR);
  105:     }
  106:     if( !strcmp(g_entries[x].name,"M") ) {
  107:       sscanf(g_entries[x].val,"%d",&g_run_mode);
  108:     }
  109:     if( !strcmp(g_entries[x].name,"SNUM") ) {
  110:       strncpy(g_student_number,g_entries[x].val,MAX_STUDENT_NUMBER+4);
  111:     }
  112:     if( !strcmp(g_entries[x].name,"CAPAID") ) {
  113:       sscanf(g_entries[x].val,"%d",&g_entered_pin);
  114:     }
  115:     if( !strcmp(g_entries[x].name,"SET") ) {
  116:       sscanf(g_entries[x].val,"%d",&g_set);
  117:     }
  118:     if( !strcmp(g_entries[x].name,"VSET") ) {
  119:       if (g_entries[x].val[0] == '\0') {
  120: 	g_vset=0;
  121:       } else {
  122: 	sscanf(g_entries[x].val,"%d",&g_vset);
  123:       }
  124:     }
  125:     if( !strcmp(g_entries[x].name,"KND") ) {
  126:       sscanf(g_entries[x].val,"%d",&g_skind);
  127:     }
  128:     if( !strncmp(g_entries[x].name,"INPUT",5) ) {
  129:       sscanf(g_entries[x].name,"INPUT%d",&q_idx);
  130:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
  131: 	strncpy(g_student_answer[q_idx],g_entries[x].val,MAX_ANSWER_CHAR);
  132:       }
  133:       if ( g_student_answer[q_idx][0] != '\0' ) {
  134: 	sprintf(buf,"%d\t%s\t",q_idx,g_student_answer[q_idx]);
  135: 	strcat(submissions_str,buf);
  136:       }
  137:     }
  138:     if( !strncmp(g_entries[x].name,"LAST",4) ) {
  139:       sscanf(g_entries[x].name,"LAST%d",&q_idx);
  140:       if( q_idx > 0 && q_idx < MAX_PROBLEM_CNT ) {
  141: 	strncpy(g_last_answer[q_idx],g_entries[x].val,MAX_ANSWER_CHAR);
  142:       }
  143:     }
  144:     free(g_entries[x].val);
  145:     free(g_entries[x].name);
  146:   }
  147: }
  148: 
  149: int web_login() 
  150: {
  151:   if( g_entered_pin != 0 ) {
  152:     g_login_set = capa_PIN(g_student_number,999,g_entered_pin);
  153:   } else {
  154:     return WEB_ERR_ENTERED_PIN;
  155:   }
  156: 
  157:   if (!g_login_set) { 
  158:     return WEB_ERR_BADLOGIN;
  159:   } else {
  160:     if ( g_login_set > 99 )  { return WEB_ERR_LOGINTOHIGH; }
  161:     if(g_login_set < g_vset ) {
  162:       return WEB_ERR_NOTVIEWABLE;
  163:     }
  164:     chdir(g_class_fullpath);  /* again, to make sure */
  165:       
  166:     if ( capa_get_student(g_student_number,&g_student_data) == 0 ) {
  167:       return WEB_ERR_STUDENT_NOT_EXIST;
  168:     } else {
  169:       time(&curtime);
  170:       if (capa_get_header(&header, g_login_set, wgt, pcr))  {
  171: 	return WEB_ERR_SET_NOT_READY;
  172:       }
  173:       if(capa_check_date(CHECK_OPEN_DATE,g_student_data.s_sec,
  174: 			 g_login_set) < 0 ) {
  175: 	
  176: 	return WEB_ERR_SET_NOT_OPEN;
  177:       }
  178:     }
  179:   }
  180:   return (error);
  181: }
  182: 
  183: int web_get_input() 
  184: {
  185:   
  186:   envPtr=getenv("REQUEST_METHOD");
  187:   if (!envPtr ) { return WEB_ERR_REQ_METHOD; }
  188:   if (strcmp(envPtr,"POST")) { return WEB_ERR_ENV_POST; }
  189:   envPtr=getenv("CONTENT_TYPE");
  190:   if (!envPtr ) { return WEB_ERR_CONTENT_TYPE; }
  191:   if (strcmp(envPtr,"application/x-www-form-urlencoded")) { 
  192:     return WEB_ERR_ENV_CONTENT; 
  193:   }
  194:   envPtr=getenv("CONTENT_LENGTH");
  195:   if (!envPtr ) { return WEB_ERR_CONTENT_LENGTH; }
  196:   content_length=atoi(envPtr);
  197: 
  198:   /* read the form into the g_entries array*/
  199:   for(x=0;content_length && (!feof(stdin));x++) {
  200:     m=x;
  201:     g_entries[x].val = fmakeword(stdin,'&',&content_length);
  202:     plustospace(g_entries[x].val);
  203:     unescape_url(g_entries[x].val);
  204:     g_entries[x].name = makeword(g_entries[x].val,'=');
  205:   }
  206: 
  207:   web_parse_input(submissions_str);
  208: 
  209:   if ( g_run_mode == WEB_CHECKIN ) {
  210:     time(&curtime); time_str = ctime(&curtime);
  211:     time_str[ strlen(time_str)-1 ] = '\0';
  212:     envPtr=getenv("REMOTE_HOST");
  213:     envPtr2=getenv("HTTP_USER_AGENT");
  214:     sprintf(log_str,"%s\t%s\t%s\t%s\t%s\n",g_class_name,g_student_number,
  215: 	    time_str,envPtr,envPtr2);
  216:     if (web_log(log_str) == -1 ) { return WEB_ERR_WEB_LOG; }
  217:   }
  218: 
  219:   getwd(g_cwd);
  220: 
  221:   web_getclassdir(&g_cpath, &g_cowner, g_class_name);
  222:   sprintf(g_class_fullpath,"%s/%s",g_cpath,g_class_name);
  223:   if( !capa_access(g_class_fullpath, F_OK) == 0 ) { return WEB_ERR_ACCESS; }
  224:     
  225:   chdir(g_class_fullpath);
  226:   if ( g_run_mode == M_CHECKANS) {
  227:     if (w_log_submissions(g_student_number,g_set,submissions_str) == -1 ) {
  228:       return WEB_ERR_SUBMMISIONS_LOG;    
  229:     }
  230:   }
  231:   
  232:   result=read_capa_config("capaweb_cgibin_path",buf);
  233:   if (result != 0 && result != -1) {
  234:     g_cgibin_path=capa_malloc(strlen(buf)+1,1);
  235:     strcpy(g_cgibin_path,buf);
  236:   } else {
  237:     g_cgibin_path=capa_malloc(strlen("capa-bin")+1,1);
  238:     strcpy(g_cgibin_path,"capa-bin");
  239:   }  
  240:   return web_login();
  241: }
  242: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>