Annotation of capa/capa51/Discuss/Ver1.3/capadiscuss, revision 1.6

1.1       albertel    1: #!/usr/ucb/perl
                      2: #
                      3: # CAPA Discussion Forum
1.6     ! albertel    4: # version 1.3.0
1.1       albertel    5: #
                      6: # Michigan State University
                      7: # Department of Physics and Astronomy
                      8: #
                      9: # Ryan J. Shaltry
                     10: # tech4@msue.msu.edu
1.6     ! albertel   11: # Guy Albertelli
        !            12: # albertel@msu.edu
1.1       albertel   13: 
                     14: print "Content-type: text/html\n\n";
                     15: 
                     16: use Benchmark;
                     17: $BenchmarkT0 = new Benchmark;
                     18: #require "capadiscuss.setup";
                     19: require "../capadiscuss_html";
                     20: 
                     21: ################################################
                     22: ## Configuration - Change these appropriately ##
                     23: ################################################
                     24: $discussurl = "capadiscuss";
                     25: $capasbin = "/capa-bin";
                     26: use Cwd;
                     27: $capabin = cwd();
                     28: @capabinparts = split /\//, $capabin;
                     29: $capabin = join "/", @capabinparts[0..$#capabinparts-1];
                     30: 
                     31: # capadiscuss.setup
                     32: $debug = 0;
                     33: ################################################
                     34: ## End Configuration ###########################
                     35: ################################################
                     36: 
                     37: &form_info;
                     38: 
                     39: $yp = "19";
                     40: $validadmin = "unchecked";
                     41: 
                     42:   # define variables
                     43:   $class    = $form{'CLASS'};
                     44:   $snum     = $form{'SNUM'};
                     45:   $capaid   = $form{'CAPAID'};
                     46:   $setid    = $form{'SETID'};
                     47:   $action   = $form{'ACTION'};
                     48:   $file     = $form{'FILE'};
                     49:   $probnum  = $form{'PROBNUM'};
                     50:   $replynum = $form{'REPLYNUM'};
                     51:   $psetid   = $form{'PSETID'};
                     52: if ($snum eq "A12345678") {$debug=1;}
                     53: if ($snum eq "a12345678") {$debug=1;}
                     54:   if($psetid eq "") { $psetid = $setid; }
                     55: if ($debug) {print "GetTeacherInfo<BR>";$BenchmarkTa = new Benchmark;}
                     56:   &GetTeacherInfo;
                     57: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                     58: 
1.2       albertel   59:   # check for valid studentnumber/capaid, an exit will occur here if the set wasn't open
1.1       albertel   60: if ($debug) {print "ValidateUser<BR>";$BenchmarkTa = new Benchmark;}
                     61:   &ValidateUser;
                     62: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                     63: 
                     64:   # grab username from student number
                     65: if ($debug) {print "UserInfo<BR>";$BenchmarkTa = new Benchmark;}
                     66:   &UserInfo($snum);
                     67: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                     68: 
                     69: if ($debug) {print "GetDateTime<BR>";$BenchmarkTa = new Benchmark;}
                     70:   &GetDateTime;
                     71: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                     72: 
                     73: if ($debug) {print $action . "<BR>";$BenchmarkTa = new Benchmark;}
                     74:   # call appropriate sr
                     75:   if($action eq "newset"                   ) { &NewSet;                }
                     76:   if($action eq "viewset" || $action eq "" ) { &ViewSet;               }
                     77:   if($action eq "viewproblem"              ) { &ViewProblem;           }
                     78:   if($action eq "post"                     ) { &Post;                  }
                     79:   if($action eq "postsubmit"               ) { &PostSubmit;            }
                     80:   if($action eq "delmsgpre"                ) { &DelMsgPre;             }
                     81:   if($action eq "delmsg"                   ) { &DelMsg;                }
                     82:   if($action eq "hidemsgpre"               ) { &HideMsgPre;            }
                     83:   if($action eq "hidemsg"                  ) { &HideMsg;               }
                     84:   if($action eq "unhidemsgpre"             ) { &UnHideMsgPre;          }
                     85:   if($action eq "unhidemsg"                ) { &UnHideMsg;             }
                     86: 
                     87:   if($action eq "admin"                    ) { &AdminMain;             }
                     88:   if($action eq "adminusersearch"          ) { &AdminUserSearch;       }
                     89:   if($action eq "adminusersearchsubmit"    ) { &AdminUserSearchSubmit; }
                     90: 
                     91:   if($action eq "viewinfofile"             ) { &ViewInfoFile;          }
                     92: 
                     93:   if($action eq "topmsg"                   ) { &TopMsg("top");        }
                     94:   if($action eq "untopmsg"                 ) { &TopMsg("");            }
                     95: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                     96: 
                     97: $BenchmarkT1 = new Benchmark;
                     98: if($debug) { print "<BR>",timestr(timediff($BenchmarkT1,$BenchmarkT0)),"\n";}
                     99: 
                    100: exit;
                    101: 
                    102: #######################
                    103: ## Begin Subroutines ##
                    104: #######################
1.2       albertel  105: sub GetSetList {
                    106:   opendir(DDIR,"$capadir/$class/discussion");
1.3       albertel  107:   @sets = grep(/^[0-9]+$/,sort(readdir(DDIR)));
1.2       albertel  108:   closedir(DDIR);
1.3       albertel  109:   if ($debug) {print "<br> Sets:". @sets ."<BR>\n";}
1.2       albertel  110: }
                    111: 
                    112: sub SetListHeader {
                    113:   $pdset = "<select name=\"SETID\">\n";
                    114:   foreach $setnum(@sets) {
                    115:     $selected = "";
                    116:     if($setnum eq $setid) { $selected = "selected"; }
                    117:     $pdset .= "<option value=\"$setnum\" $selected>$setnum\n";
                    118:   }
                    119:   $pdset .= "</select>\n";
                    120: }
1.1       albertel  121: 
1.3       albertel  122: sub GetInfoList {
                    123:   local(@temp,@alldir,@setdir);
                    124: #  if ($debug) {print "<br> GetInfoList <br>";}
                    125:   opendir(DDIR,"$capadir/$class/discussion");
                    126:   @alldir = sort(grep(/.info$/,readdir(DDIR)));
                    127:   closedir(DDIR);
                    128: 
                    129:   opendir(FORUM,"$capadir/$class/discussion/$setid");
                    130:   @setdir = sort(grep(/.info$/,readdir(FORUM)));
                    131:   closedir(FORUM);
                    132: 
                    133:   foreach $file(@setdir) { @setdir2 = (@setdir2,"$setid/$file"); }
                    134:   @infofiles = (@alldir,@setdir2);
                    135:   $infostring = "";
                    136: #  if ($debug) {print "<br> GetInfoList" . @infofiles."<br>";}
                    137:   foreach $infofile(@infofiles) {
                    138: #    if ($debug) {print "<br> GetInfoList" . $infofile."<br>";}
                    139:     @temp = reverse split /\//, $infofile;
                    140:     $infodesc = $temp[0];
                    141:     $infodesc =~ s/.info//g;
                    142:     $infodesc =~ s/_/ /g;
                    143:     $infostring .= &InfoButtonHTML;
                    144:   }
                    145:   if ($infostring ne "") {
                    146:  #   $infostring = "</tr><tr>" . $infostring;
                    147:   }
                    148: #  if ($debug) {print "<br> GetInfoList" . $infostring."<br>";}
                    149: }
                    150: 
1.1       albertel  151: sub NewSet {
                    152:   &Log("NewSet");
                    153: 
                    154:   if($setid <= $psetid) {   #grab capaid
1.3       albertel  155:     if ($debug) {print "NewSet<BR>";$BenchmarkTa = new Benchmark;}
1.1       albertel  156:     @allcapaid = `allcapaid -s $setid -e $setid -stu $snum -c $capadir/$class -i`;
                    157: 
                    158:     $capaid = $allcapaid[2];
                    159:     chop $capaid;
                    160:     chop $capaid;
                    161:     &ViewSet;
                    162: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                    163:     return;
                    164:   }
                    165: 
1.3       albertel  166:   if ($debug) { print "<br> GetSetList\n"; }
1.2       albertel  167:   &GetSetList;
1.3       albertel  168:   if ($debug) { print "<br> EndGetSetList\n"; }
1.2       albertel  169:   &SetListHeader;
1.1       albertel  170:   &NewSetHTML;
                    171:   #exit;
                    172: }
                    173: 
                    174: sub ViewSet {
                    175:   if ($debug) {print "InsideViewSet<BR>";$BenchmarkTa = new Benchmark;}
                    176:   &Log("ViewSet");
                    177:   &CheckAdmin;
                    178:   $adminbutton = "";
                    179:   if($validadmin eq "yes") {
                    180:     $adminbutton = &AdminButtonHTML;
                    181:   }
                    182: 
                    183:   $capasbin = "$capasbin/$teachdir/capasbin";
                    184:   &SetJumpBox;
1.3       albertel  185:   $backfunction="viewset";
                    186:   &GetInfoList;
                    187: 
1.1       albertel  188:   opendir(FORUM,"$capadir/$class/discussion/$setid");
                    189:     @dir = sort(readdir(FORUM));
                    190:   closedir(FORUM);
                    191: 
                    192:   foreach $i(@dir) {
                    193:     $probnum = &RemZero(substr($i,0,6));
                    194:     $found[$probnum]++;
                    195:   }
                    196:   $maxprobs = &MaxProbs($setid);
                    197: 
                    198:   # split table (if more than 15 problems)
                    199:   $bigtable = "no";
                    200:   if($maxprobs > 5) {
                    201:     $bigtable = "yes";
                    202:     $col1size = int ($maxprobs / 2);
                    203:     $col2size = $maxprobs - $col1size;
                    204:     if($col1size < $col2size) {
                    205:       $t = $col1size;
                    206:       $col1size = $col2size;
                    207:       $col2size = $t;
                    208:     }
                    209:   }
                    210: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>2",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                    211:   &ViewSetTitleHTML;
                    212: 
                    213:   if($bigtable eq "no") { &ViewSetTopSmallHTML; }
                    214:   else                  { &ViewSetTopBigHTML; }
                    215: 
                    216:   $col = 1;
                    217:   for($probnum=1; $probnum<=$maxprobs; $probnum++) {
                    218:     if($col eq "1" && $probnum > $col1size && $bigtable eq "yes") {
                    219:       $col = 2;
                    220:       &ViewSetEndColHTML;
                    221:       &ViewSetTopSmallHTML;
                    222:     }
                    223:     if($found[$probnum] ne "") {
                    224:       opendir(MSG,"$capadir/$class/discussion/$setid");
                    225:         @msgs = grep(/.msg/,sort(readdir(MSG)));
                    226:       closedir(MSG);
                    227:       foreach $msg(@msgs) {
                    228:         if(&RemZero(substr($msg,0,6)) eq $probnum) {
                    229:           push(@good,$msg);
                    230:         }
                    231:       }
                    232: 
                    233:       &OpenMsg("$capadir/$class/discussion/$setid/$good[$#good]");
                    234:       $lastpost = "$date at $time";
                    235:       $posts = $found[$probnum];
                    236:     }
                    237:     else {
                    238:       $lastpost = "--";
                    239:       $posts = "0"; }
                    240:     &ViewSetGutsHTML;
                    241:   }
                    242:   &ViewSetBotHTML;
                    243: if($debug) {$BenchmarkTb = new Benchmark;print "<BR>3",timestr(timediff($BenchmarkTb,$BenchmarkTa)),"\n";}
                    244:   #exit;
                    245: }
                    246: 
                    247: sub ViewProblem {
                    248:   &Log("ViewProblem");
                    249: 
                    250:   # check if user is administrator (for admin button)
                    251:   &CheckAdmin;
                    252:   if($validadmin eq "yes") {
                    253:     $adminbutton = &AdminButtonHTML;
                    254:   }
                    255:   else {
                    256:     $adminbutton = "";
                    257:   }
                    258: 
                    259:   opendir (MSGS,"$capadir/$class/discussion/$setid");
                    260:     @msgs = sort(readdir(MSGS));
                    261:   closedir(MSGS);
                    262: 
                    263:   # search for top message
                    264:   @top = grep (/top/, @msgs);
                    265:   $topmsg = "";
                    266: #  print "$#top";
                    267:   if($#top ne "0") {
                    268:     $pnum = &AddZero($probnum);
                    269:     foreach $entry(@top) {
                    270:       $find = $entry =~ s/\A$pnum/$pnum/o;
                    271:       if($find eq "1") {
                    272:         $topmsg = $entry;
                    273:         @msgs = grep(!/$topmsg/,@msgs);
                    274:         unshift(@msgs,$topmsg);
                    275:         print "$topmsg";
                    276:       }
                    277:     }
                    278:   }
                    279: 
                    280:   $msgfind = "no";
                    281:   $msgcount = 0;
                    282: 
                    283:   $capasbin = "$capasbin/$teachdir/capasbin";
                    284: 
                    285:   foreach $i(@msgs) {
                    286:     $pn = &RemZero(substr($i,0,6));
                    287:     if($pn eq $probnum) {
                    288:       $msgcount++;
                    289:       $msgfind = "yes";
                    290:       &OpenMsg("$capadir/$class/discussion/$setid/$i");
                    291: 
                    292:       if($status eq "hidden" && $validadmin ne "yes") {
                    293:         $msgcount--;
                    294:         $msgfind = "no";
                    295:       }
                    296: 
                    297:       if($msgcount eq "1") {
1.3       albertel  298: 	&GetInfoList;
1.1       albertel  299:         $postbutton = &ReplyButtonHTML;
                    300:         &ProblemJumpBox;
                    301:         &ViewProblemTopHTML;
                    302:       }
                    303: 
                    304:       ($token1,$token2) = split(/\|/,$poster);
                    305:       chop $token2;
                    306:       if($token1 eq "Anonymous") {
                    307:          if($validadmin eq "yes") {
                    308:            $poster = "$token2 (anonymously)";
                    309:          }
                    310:          else {
                    311:            $poster = "$token1";
                    312:            $email = "";
                    313:          }
                    314:       }
                    315:       $topfind = grep(/-top.msg/,$i);
                    316:       if($validadmin eq "yes") {
                    317:         $file = "$i";
                    318:         $delbutton = &DelButtonHTML;
                    319:         $hidebutton = &HideButtonHTML;
                    320:         $topbutton = &TopButtonHTML;
                    321:         if($topfind eq "1") { $topbutton = &UnTopButtonHTML; }
                    322:       }
                    323:       $subjectbgcolor = "#92D1D0";
                    324:       if($topfind eq "1") { $subjectbgcolor = "blue"; }
                    325:       $messagenote = "";
                    326:       if($validadmin eq "yes" || $status eq "public") {
                    327:         if($status eq "hidden") {
                    328:           $subjectbgcolor = "red";
                    329:           $hidebutton = &UnHideButtonHTML;
                    330:           $messagenote = "Hidden by $hiddenuser on $hiddendate at $hiddentime from $hiddenaddr";
                    331:         }
1.5       albertel  332: #	$message =~ s/</&lt;/g;
                    333: #	$message =~ s/>/&gt;/g;
1.1       albertel  334:         &ViewProblemGutsHTML;
                    335:       }
                    336:     }
                    337:   }
                    338: 
                    339:   if($msgfind eq "no") {
1.3       albertel  340:     &GetInfoList;
1.1       albertel  341:     $postbutton = &PostNewButtonHTML;
                    342:     &ProblemJumpBox;
                    343:     &ViewProblemTopHTML;
                    344:     &ViewProblemNoneHTML;
                    345:     &ViewProblemBotHTML;
                    346:   } else {
                    347:     &ViewProblemBotHTML;
                    348:   }
                    349:   
                    350:   #exit;
                    351: }
                    352: 
                    353: sub ViewInfoFile {
1.4       albertel  354:   &Log("InfoFile");
1.3       albertel  355:   open(INFOFILE,"$capadir/$class/discussion/$file");
1.1       albertel  356:     @file = <INFOFILE>;
                    357:   closedir(INFOFILE);
                    358: 
                    359:   print "<html><head></head><body bgcolor=\"#ffffff\">";
                    360:   $backfunction = $form{'BACKFUNCTION'};
                    361:   &InfoBackButtonHTML;
                    362:   foreach $line(@file) {
                    363:     print "$line";
                    364:   }
                    365: 
                    366:   #exit;
                    367: }
                    368: 
                    369: sub Post {
                    370:   local(@msgs,$pn,$msg);
                    371: 
                    372:   # grab last message in thread
                    373:   opendir(MSGS,"$capadir/$class/discussion/$setid");
                    374:     @msgs = grep(/.msg/,sort(readdir(MSGS)));
                    375:   closedir(MSGS); 
                    376: 
                    377:   foreach $msg(@msgs) {
                    378:     $pn = &RemZero(substr($msg,0,6));
                    379:     if($pn eq $probnum) {
                    380:       $lastfile = $msg;
                    381:     }
                    382:   }
                    383: 
                    384:   $PostType = "New Message";
                    385:   if($lastfile ne "") {
                    386:     &OpenMsg("$capadir/$class/discussion/$setid/$lastfile");
                    387:     if($status eq "public" || $validadmin eq "yes") {
                    388:       ($poster,$rposter) = split(/\|/,$poster);
                    389:       $inreplyto = &InReplyToHTML;
                    390:       $PostType = "Reply";
                    391:     }
                    392:   }
                    393: 
                    394:   &PostHTML;
                    395:   #exit;
                    396: }
                    397: 
                    398: sub PostSubmit {
                    399:   local($message,$subject,$date);
                    400: 
                    401:   $message = $form{'message'};
                    402:   $subject = $form{'subject'};
                    403:   
                    404:   if($message eq "" || $subject eq "") { &MFPostReplyHTML; }
                    405: 
                    406:   &Log("Post");
                    407: 
                    408:   # find last message in thread
                    409:   opendir(FORUM,"$capadir/$class/discussion/$setid");
                    410:     @dir = sort(readdir(FORUM));
                    411:   closedir(FORUM);
                    412: 
                    413:   $probnum = &AddZero($probnum);
                    414:   $replynum = "000000";
                    415:   foreach $msg(@dir) {
                    416:     if(substr($msg,0,6) eq $probnum) {
                    417:         $replynum = &AddZero(&RemZero(substr($msg,7,6)) + 1);
                    418:     }
                    419:   }
                    420: 
                    421:   $date = substr($RunonDate,0,4) . $yp . substr($RunonDate,4,2);
                    422:   &WriteMsg("$capadir/$class/discussion/$setid/$probnum-$replynum-$date-000000.msg");
                    423: 
                    424:   $probnum = &RemZero($probnum);
                    425:   &PostDoneHTML;
                    426:   #exit;
                    427: }
                    428: 
                    429: sub DelMsgPre {
                    430:   &CheckAdmin('kill');
                    431:   &OpenMsg("$capadir/$class/discussion/$setid/$file");
                    432:   &DelMsgPreHTML;
                    433:   #exit;
                    434: }
                    435: 
                    436: sub DelMsg {
                    437:   &CheckAdmin('kill');
                    438:   unlink("$capadir/$class/discussion/$setid/$file");
                    439:   &DelMsgDoneHTML;
                    440:   #exit;
                    441: }
                    442: 
                    443: sub HideMsgPre {
                    444:   &CheckAdmin('kill');
                    445:   &OpenMsg("$capadir/$class/discussion/$setid/$file");
                    446:   &HideMsgPreHTML;
                    447:   #exit;
                    448: }
                    449: 
                    450: sub HideMsg {
                    451:   &CheckAdmin('kill');
                    452:   open(MSG,"$capadir/$class/discussion/$setid/$file");
                    453:     @msg = <MSG>;
                    454:   close(MSG);
                    455: 
                    456:   unlink("$capadir/$class/discussion/$setid/$file");
                    457: 
                    458:   open(MSG,">$capadir/$class/discussion/$setid/$file");
                    459:     for($i=0;$i<=5;$i++) {
                    460:       print MSG "$msg[$i]";
                    461:     }
                    462:     print MSG "hidden|$username|$HyphenDate|$Time|$ENV{'REMOTE_ADDR'}\n";
                    463:   close(MSG);
                    464: 
                    465:   &HideMsgDoneHTML;
                    466: }
                    467: 
                    468: sub UnHideMsgPre {
                    469:   &CheckAdmin('kill');
                    470:   &OpenMsg("$capadir/$class/discussion/$setid/$file");
                    471:   &UnHideMsgPreHTML;
                    472:   #exit;
                    473: }
                    474: 
                    475: sub UnHideMsg {
                    476:   &CheckAdmin('kill');
                    477:   open(MSG,"$capadir/$class/discussion/$setid/$file");
                    478:     @msg = <MSG>;
                    479:   close(MSG);
                    480: 
                    481:   unlink("$capadir/$class/discussion/$setid/$file");
                    482: 
                    483:   open(MSG,">$capadir/$class/discussion/$setid/$file");
                    484:     for($i=0;$i<=5;$i++) {
                    485:       print MSG "$msg[$i]";
                    486:     }
                    487:     print MSG "public||||}\n";
                    488:   close(MSG);
                    489: 
                    490:   &UnHideMsgDoneHTML;
                    491: }
                    492: 
                    493: 
                    494: sub TopMsg {
                    495:   &CheckAdmin('kill');
                    496: 
                    497:   $ext = shift(@_);
                    498:   
                    499:   open(MSG,"$capadir/$class/discussion/$setid/$file");
                    500:     @msg = <MSG>;
                    501:   close(MSG);
                    502:   
                    503:   unlink("$capadir/$class/discussion/$setid/$file");
                    504:   
                    505:   ($filename, $extension) = split(/\./,$file);
                    506:   $filename = "$filename-$ext.msg";
                    507:   open(MSG,">$capadir/$class/discussion/$setid/$filename");
                    508:     for($i=0;$i<=6;$i++) {
                    509:       print MSG "$msg[$i]";
                    510:     }
                    511:   close(MSG);
                    512:   
                    513:   if($ext eq "") {
                    514:     $function = "restored to its original position in the thread.";
                    515:   }
                    516:   else {
                    517:     $function = "moved to the top of the thread list.";
                    518:   }
                    519: 
                    520:   &TopMsgDoneHTML;
                    521: }
                    522: 
                    523: ###############################
                    524: ## Administrator Subroutines ##
                    525: ###############################
                    526: 
                    527: sub AdminMain {
                    528:   &CheckAdmin('kill');
                    529:   &AdminMainHTML;
                    530:   #exit;
                    531: }
                    532: 
                    533: 
                    534: sub AdminUserSearch {
                    535:   &CheckAdmin('kill');
                    536:   &AdminUserSearchHTML;
                    537:   #exit;
                    538: }
                    539: 
                    540: 
                    541: sub AdminUserSearchSubmit {
                    542:   local($sname,$semail,$saction,$sset,$sprob,$sdate,$stime);
                    543:   &CheckAdmin('kill');
                    544: 
                    545:   $query = $form{'usersearch'};
                    546:   open(LOG,"$capadir/$class/discussion/logs/access.log");
                    547:     @log = <LOG>;
                    548:   close(LOG);
                    549: 
                    550:   @linematches = grep /$query/, @log;
                    551:   
                    552:   foreach $line(@linematches) { print $line; }
                    553: 
                    554:   #exit;
                    555: }
                    556: 
                    557: ######################
                    558: ## Misc Subroutines ##
                    559: ######################
                    560: 
                    561: sub OpenMsg {
                    562:   local($filename,$statusl);
                    563: 
                    564:   $filename = shift(@_);
                    565:   
                    566:   open (MSG,"$filename");
                    567:     @msg = <MSG>;
                    568:   close(MSG);
                    569: 
                    570:   $poster   = $msg[0];
                    571:   $date     = $msg[1];
                    572:   $time     = $msg[2];
                    573:   $subject  = $msg[3];
                    574:   $email    = $msg[4];
                    575:   $message  = $msg[5];
                    576:   $statusl  = $msg[6];
                    577: 
                    578:   ($status, $hiddenuser, $hiddendate, $hiddentime, $hiddenaddr) = split(/\|/,$statusl);
                    579: }
                    580: 
                    581: sub WriteMsg {
                    582:   local($filename,$message);
                    583: 
                    584:   $filename = shift(@_);
                    585:   # add sr to grab e-mail address of user
                    586: 
1.6     ! albertel  587:   
        !           588:   # <B> <I> <P> <A> <LI> <OL> <UL> <EM> <BR> <TT> <STRONG> <BLOCKQUOTE> <DIV .*> <DIV> <P .
        !           589:   %html=(B=>1, I=>1, P=>1, A=>1, LI=>1, OL=>1, UL=>1, EM=>1, BR=>1, TT=>1, STRONG=>1,
        !           590: 	 BLOCKQUOTE=>1, DIV=>1, IMG=>1);
        !           591: 
1.1       albertel  592:   $message = $form{'message'};
1.6     ! albertel  593: 
1.5       albertel  594:   $message =~ s/((\n\r)|(\n))\n/<p>/g;
1.1       albertel  595:   $message =~ s/\n/<br>/g;
                    596: 
1.6     ! albertel  597:   $message =~ s/\<(\/?\s*(\w+)[^\>\<]*)/
        !           598:     {($html{uc($2)}&(length($1)<1000))?"\<$1":"\&lt;$1"}/ge;
        !           599:   $message =~ s/(\<?\s*(\w+)[^\<\>]*)\>/
        !           600:     {($html{uc($2)}&(length($1)<1000))?"$1\>":"$1\&gt;"}/ge;
        !           601: 
        !           602: #  $message =~ s/</&lt;/g;
        !           603: #  $message =~ s/>/&gt;/g;
        !           604: 
1.1       albertel  605:   # check if posted anonymously
                    606:   if($form{'ANONYMOUS'} eq "yes") {
                    607:     $username = "Anonymous|$username";
                    608:   }
                    609: 
                    610:   open (MSG,">$filename");
                    611:     print MSG "$username\n";
                    612:     print MSG "$HyphenDate\n";
                    613:     print MSG "$Time\n";
                    614:     print MSG "$subject\n";
                    615:     print MSG "$email\n";
                    616:     print MSG "$message\n";
                    617:     print MSG "public||||\n";
                    618:   close(MSG);
                    619: }
                    620: 
                    621: sub GetTeacherInfo {
                    622:   local($capa5dir,$classname,$tdir,@classconf);
                    623: 
                    624:   # open/read class.conf file
                    625:   open (CLASSCONF,"$capabin/class.conf");
                    626:     @classconf = <CLASSCONF>;
                    627:   close(CLASSCONF);
                    628: 
                    629:   $found eq "no";
                    630:   foreach $line(@classconf) {
                    631:     ($classname,$capa5dir,$tdir) = split(/[\t\ ]+/,$line);
                    632:     if($classname eq $class) {
                    633:       $teachdir = $tdir;
                    634:       $capadir  = $capa5dir;
                    635:       $found    = "yes";
                    636:       $cgidirurl = join "/", $capasbin,$teachdir;
                    637:     }
                    638:   }
                    639:   if($found eq "no") { &InvalidVarsHTML; }
                    640: }
                    641: 
                    642: sub CheckAdmin {
                    643:   local($asection,@adminsections,$line,@capaconfig,$command,$value,
                    644:         $adminsection,@classl,$course,$coursenum,$section,$stunum,$a);
                    645: 
                    646:   $mode = shift(@_);
                    647: 
                    648:   if ($validadmin eq "unchecked") {
                    649:     $validadmin = "no";
                    650:     
                    651:     # open 'capa.config' file (for admin section read)
                    652:     open(CAPACONFIG,"$capadir/$class/capa.config");
                    653:     @capaconfig = <CAPACONFIG>;
                    654:     close(CAPACONFIG);
                    655:     foreach $line(@capaconfig) {
                    656:       chop $line;
                    657:       ($command,$value) = split(/\ =\ /,$line);
                    658:       if($command eq "admin_section") {
                    659: 	$adminsection = $value;
                    660: 	last;
                    661:       }
                    662:     }
                    663:     @adminsections = split(/\,/,$adminsection);
                    664:     for ($asection=0; $asection <= $#adminsections; $asection++) {
                    665:       $adminsections[$asection]=&RemZero($adminsections[$asection])
                    666:     }
                    667:     # open 'classl' file
                    668:     open(CLASSL,"$capadir/$class/classl");
                    669:     @classl = <CLASSL>;
                    670:     close(CLASSL);
                    671:     
                    672:     $snum =~ tr/a-z/A-Z/;
                    673:     LINE: foreach $line(@classl) {
                    674:       ($course,$coursenum,$section,$stunum,$a) = split(/\ +/,$line);
                    675:       $stunum =~ tr/a-z/A-Z/;
                    676:       if ( $stunum ne $snum ) { next LINE;}
                    677:       $section = &RemZero($section);
                    678:       foreach $asection(@adminsections) {
                    679: 	if($section eq $asection ) {
                    680: 	  $validadmin = "yes";
                    681: 	  last LINE;
                    682: 	}
                    683:       }
                    684:     }
                    685:   }
                    686:   if($validadmin eq "no" && $mode eq "kill") { &NotAdminHTML; }
                    687: }
                    688: 
                    689: sub ValidateUser {
                    690:   local(@allcapaid,@classl,$line);
                    691: 
                    692:   open (CLASSL,"$capadir/$class/classl");
                    693:     @classl = <CLASSL>;
                    694:   close(CLASSL);
                    695: 
                    696:   $valid = "no";
                    697:   foreach $line(@classl) {
                    698:     if(substr($line,15,8) eq substr($snum,1,8)) {
                    699:       $valid = yes;
                    700:     }
                    701:   }
                    702:   if($valid eq "no") { &InvalidVarsHTML; }
                    703: 
                    704:   # check w/ allcapaid program
1.6     ! albertel  705:   @allcapaid = `allcapaid -s $setid -e $setid -stu $snum -c $capadir/$class -i -checkopen`;
1.1       albertel  706: 
                    707:   chop $allcapaid[2];
                    708:   chop $allcapaid[2];
1.2       albertel  709:   if ($debug) {print "capaid:" . $allcapaid[2] . "<BR>\n";}
1.1       albertel  710:   if($allcapaid[2] ne $capaid) {
1.2       albertel  711:     if ($allcapaid[2] =~ /Open/) { &NotOpen; exit; }
                    712:     if($action ne "newset") { &BadPassHTML; exit; }
1.1       albertel  713:     #&NewSet;
                    714:   }
1.2       albertel  715: }
                    716: 
                    717: sub NotOpen {
                    718:   &GetSetList;
                    719:   &SetListHeader;
                    720:   &ViewSetTitleHTML;
                    721: print <<NotOpen2;
                    722: This set is not yet open. Please choose a different set.
                    723: </body>
                    724: </html>
                    725: NotOpen2
1.1       albertel  726: }
                    727: 
                    728: sub UserInfo {
                    729:   local($line,@classl,$last,$first,$middle);
                    730: 
                    731:   open (CLASSL,"$capadir/$class/classl");
                    732:     @classl = <CLASSL>;
                    733:   close(CLASSL);
                    734: 
                    735:   foreach $line(@classl) {
                    736:     if(substr($line,15,8) eq substr($snum,1,8)) {
                    737:       $username = substr($line,24,36);
                    738:       $email    = substr($line,60,45);
                    739:     }
                    740:   }
                    741:   ($last, $first, $middle) = split(/\ +/,$username);
                    742: 
                    743:   chop $last;
                    744:   $username = "$first $last";
                    745:   $email    =~ s/\ //g;
                    746:   chop $email;
                    747: }
                    748: 
                    749: sub MaxProbs {
                    750:   open (SETRECORD,"$capadir/$class/records/set$setid.db");
                    751:   $setrecord = <SETRECORD>;
                    752:   close(SETRECORD);
                    753:   chop $setrecord;
                    754:   return $setrecord;
                    755: }
                    756: 
                    757: sub ProblemJumpBox {
                    758:   $pdprob = "<select name=\"PROBNUM\">\n";
                    759:   for($jumpnum=1;$jumpnum<=&MaxProbs;$jumpnum++) {
                    760:     $selected = "";
                    761:     if($jumpnum eq $probnum) { $selected = " selected"; }
                    762:     $pdprob .= "  <option value=$jumpnum$selected>$jumpnum\n";
                    763:   }
                    764:   $pdprob .= "</select>";
                    765: }
                    766: 
                    767: sub SetJumpBox {
1.3       albertel  768:   &GetSetList;
                    769:   &SetListHeader;
1.1       albertel  770: }
                    771: 
                    772: sub AddZero {
                    773:   $string = shift(@_);
                    774:   
                    775:   while(length($string) < 6) {
                    776:     $string = "0$string";
                    777:   }
                    778:   return $string;
                    779: }
                    780: 
                    781: sub RemZero {
                    782:   $string = shift(@_);
                    783:   $string =~ s/^0*//g;
                    784:   return $string;
                    785: }
                    786: 
                    787: sub FormatDate {
                    788:   $string = shift(@_);
                    789:   
                    790:   $month = substr($string,0,2);
                    791:   $day   = substr($string,2,2);
                    792:   $year  = substr($string,4,4);
                    793: 
                    794:   $string = "$month-$day-$year";
                    795: 
                    796:   return $string;
                    797: }
                    798: 
                    799: ####################
                    800: ## Get SubRoutine ##
                    801: ####################
                    802: sub form_info {
                    803:   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
                    804: 
                    805:   # Split the name-value pairs
                    806:   @pairs = split(/&/, $buffer);
                    807: 
                    808:   foreach $pair (@pairs) {
                    809:      ($name, $value) = split(/=/, $pair);
1.6     ! albertel  810:      if ($name eq "message") {$allow_html=1;} else {$allow_html=0;}
1.1       albertel  811:      # Un-Webify plus signs and %-encoding
                    812:      $value =~ tr/+/ /;
                    813:      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                    814:      $value =~ s/<!--(.|\n)*-->//g;
                    815: 
                    816:      if ($allow_html != 1) {
                    817:         $value =~ s/<([^>]|\n)*>//g;
                    818:      }
                    819:      $form{$name} = $value;
                    820:   }
                    821: }
                    822: 
                    823: ##################
                    824: ## Date/Time sr ##
                    825: ##################
                    826: sub GetDateTime  {
                    827:   ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);
                    828:   $mon++;
                    829: 
                    830:   if ($hour < 12) {
                    831:   $AMPM = "AM";
                    832:   }
                    833: 
                    834:   if ($hour > 12) {
                    835:   $hour = $hour - 12;
                    836:     $AMPM = "PM";
                    837:   }  	
                    838:   if ($hour == 12) {
                    839:     $AMPM = "PM";
                    840:   }  	
                    841: 	
                    842:   if ($hour == 0) {
                    843:     $hour = "12";
                    844:   }
                    845: 
                    846:   $min = sprintf ("%2d", $min);
                    847:   $min =~tr/ /0/;
                    848:   $mon = sprintf ("%2d", $mon);
                    849:   $mon =~tr/ /0/;
                    850:   $mday = sprintf ("%2d", $mday);
                    851:   $mday =~tr/ /0/;
                    852:   $HyphenDate = ("$mon" . "-" . "$mday" . "-" . "$year");
                    853:   $RunonDate = ("$mon$mday$year");
                    854:   $Time = ("$hour" . ":" . "$min" . " " . "$AMPM");
                    855: }
                    856: 
                    857: ########################
                    858: ## Logging Subroutine ##
                    859: ########################
                    860: sub Log {
                    861:   $logmsg = shift(@_);
                    862:   
                    863:   &GetDateTime;
                    864: 
                    865:   &Lock("lock.file");
                    866:   open(LOG,">>$capadir/$class/discussion/logs/access.log");
1.4       albertel  867:     print LOG "$snum|$capaid|$username|$email|$logmsg|$setid|$probnum|$HyphenDate|$Time|$file\n";
1.1       albertel  868:   close(LOG);
                    869:   &Unlock("lock.file");
                    870: }
                    871: 
                    872: ############################
                    873: ## Lock/Unlock Subroutine ##
                    874: ############################
                    875: sub Lock {
                    876:   local ($lockname) = @_;
                    877:   local ($endtime);
                    878:   $endtime = 15;
                    879:   $endtime = time + $endtime;
                    880:   
                    881:   while (-e $lockname && time < $endtime) {
                    882:     open (LOCKFILE, ">$lockname");
                    883:   }
                    884:   
                    885: sub Unlock {
                    886:   local ($lockname) = @_;
                    887:   close (LOCKFILE);
                    888:   unlink ($lockname);
                    889:   }
                    890: }
                    891: 
                    892: 
                    893: ######################
                    894: ## HTML Subroutines ##
                    895: ######################
                    896: 
                    897: ## CapaDiscuss HTML Subroutines
                    898: 
                    899: # capadiscuss_html
                    900: 
                    901: ## end capadiscuss

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