<?php
$no = "nobody";
$cname = "nobody";
$filename = "";
$mailaddr = "";

session_start();
if(isset($_SESSION['logout']))
{
    if($_SESSION['logout'] == 1)
    {
        $_SERVER['PHP_AUTH_USER'] = 'nobody';
    }
}
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] == 'nobody') {
    $_SESSION['logout'] = 0;
    authenticate();
    echo 'Sorry, the username/password are necessary';
    exit;
}
else if ((!SearchInFile("POSTDR",$_SERVER['PHP_AUTH_USER']))&&
    (!SearchInFile("PHD",$_SERVER['PHP_AUTH_USER']))&&
    (!SearchInFile("MS2",$_SERVER['PHP_AUTH_USER']))&&
    (!SearchInFile("MS1",$_SERVER['PHP_AUTH_USER']))&&
    (!SearchInFile("OTH",$_SERVER['PHP_AUTH_USER'])))
{
    authenticate();
    echo 'Sorry, your username is not in our list';
    exit;
}
else if ($_SERVER['PHP_AUTH_PW']=="70156667" ||
    $_SERVER['PHP_AUTH_PW']=="easylab9" ||
    $_SERVER['PHP_AUTH_PW']=="iwnblchen")
{
    $CurrUserNo = $_SERVER['PHP_AUTH_USER'];
    $_SESSION['logout'] = 0;
}
else
{
    authenticate();
    echo 'Sorry, your password is wrong';
    exit;
}


function authenticate()
{
    //$_SESSION['logout'] = 1;
    header('WWW-Authenticate: Basic realm="常用暱稱"');
    header('HTTP/1.0 401 Unauthorized');
    //$_SESSION['logout'] = 2;
    exit;
}


/*
 * Searching list files to check if the m_no exists.
 * If yes, return true
 * If no, return false
 */
function SearchInFile($PesnClass, $m_no)
{
    $PESN_FILE = 'PESN_'.$PesnClass.'.lst';
    $fp = @fopen( $PESN_FILE , "r" )
        or die( "Can't open $PESN_FILE!!!" ) ;

    while(!feof($fp))
    {
        $str = fgets($fp, 128);

        if (sscanf($str,"%s %s %s %s", $no, $cname, $filename, $mailaddr)!=4)
            continue;

        if ($no==$m_no) {
            @fclose($fp);
            return true;
        }
    }
    @fclose($fp);
    return false;
}
?>

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style TYPE="text/css">
body {
    Font:12pt;
    Font-family: "Microsoft JhengHei","Times New Roman";
}
.button {
    font-size: 8pt;
    font-family: "Microsoft JhengHei", sans-serif;
/*	border-color: #0000ff;*/
    height: 14pt;
    background: #ffdd99;
    cursor: hand;
}

</style>

</head>

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $PESNFILE = $_POST['PESNFILE'];
    $fileid = $_POST['fileid'];
    echo $fileid;
    if (!empty($fileid))
    {
        echo "PESN=".$PESNFILE."<BR>";
        echo "123";
        echo "Fileid=".$fileid."<BR>";
        echo "obj_userfile=".'userfile'."<BR>";
        echo "File=".$_FILES['userfile']['tmp_name']."<BR>";
        echo "File type=".$_FILES['userfile']['type']."<BR>";

        FileUpload($fileid);
        echo '<script>open("main2.php?TMSP="+String((new Date()).getTime()),"_self");</script>';
    }
}
?>

<body bgcolor=#ffffea>

<script language="javascript">
function openppt(no)
{
    open("upload-ppt/"+no+".ppt");
}
function opendoc(no,sel)
{
    open("upload-doc/"+no+"-a.doc");
}

function openxls(file)
{
    open("upload-xls/"+file);
}
function directopendoc(file)
{
    open("upload-doc/"+file);
}

function back_to_index()
{
    var location = "/var/www/html/index.html";
    location.href = location;
}

</script>
<a href="main.php">Return to Version 1.0</a>

<font style="color:#483d8b;font-style:'italic';"><h1>Speed Web Research Log</h1></font>

<table width=100%>
<tr width=100% style=\"Font-size:10pt;Color:#708090;Font-style:'italic';\"><td>


<div>
The sample file for general purposes:
<input TYPE="button" id='f1' class="button" onclick="directopendoc('PrjForm.doc');" value="download">
</div>

<div>
The sample file for MS2:
<input TYPE="button" id='f2' class="button" onclick="directopendoc('PrjFormForMS2.doc');" value="download">
</div>

<div>
The sample file for MS1:
<input TYPE="button" id='f3' class="button" onclick="directopendoc('PrjFormForMS1.doc');" value="download">
</div>
<br />
<table width="90%">
<tr><td colspan=2><b>Tips on how to write your research log:</b></td></tr>
<tr><td style="vertical-align:top;"><b>1.</b></td><td> A research log should be filled on a regular weekly basis by one who is conducting research (including 2nd-year MS grads, Ph.D. grads, and post-docs) to plan and manage one's time and works.</td></tr>
<tr><td style="vertical-align:top;"><b>2.</b></td><td> In a research log, there could be multiple projects and multiple tasks under a project. Projects are independent from each other, while tasks under a project have dependency or correlation with each other. Usually a paper, a research topic, or coursework in general is a project, while "survey", "experiment", "design", "implementation", or "writing" is a task.</td></tr>
<tr><td style="vertical-align:top;"><b>3.</b></td><td> If in a week you've spent 30% of your time on Task 1 of Project A, 40% on Task 2 of Project A, and 30% on Task 2 of Project B, then you fill up three entries in the log. In each entry, E% means percentage of effort in that week (i.e., 30%, 40%, and 30% as the above) and C% means percentage of completion of that task "till" that week. Thus E% of all entries you've added in that week should sum up to 100%, while C% should be increasing weekly for a specific task.</td></tr>
<tr><td style="vertical-align:top;"><b>4.</b></td><td> In the remark of an entry, you list accomplishments, milestones, ideas, etc. You may also put plans in an entry for the next coming weeks. So a research log could be a planning too in addition to a logging tool.</td></tr>
<tr><td style="vertical-align:top;"><b>5.</b></td><td> From the research log, you should be able to see what tasks you have spent your time on recently, how long a project or task has last, what bottlenecks you've encountered in the past few months, and what milestones and ideas you've generated so far.</td></tr>
</table>
<br />

</td></tr>
<tr width=100%><td><?php ShowPESNList("NBL");?></td></tr>
<tr width=100%><td><?php ShowPESNList("POSTDR");?></td></tr>
<tr width=100%><td><?php ShowPESNList("PHD");?></td></tr>
<tr width=100%><td><?php ShowPESNList("MS2");?></td></tr>
<tr width=100%><td><?php ShowPESNList("MS1");?></td></tr>
</table>

<?php
function FileUpload($fileid)
{
    if ($fileid=='doc') {
        if ($_FILES['userfile']['type']!="application/msword")
        {
            echo '<script>alert("Inconsistent Upload file. Upload Fail!");</script>';
            return;
        }
        $uploaddir = '/srv/http/speedlab/enotes/upload-doc/';
        $uploadfile = $uploaddir . $_SERVER['PHP_AUTH_USER'] . "-a." . $fileid ;
    }
    else if ($fileid=='xls' || $fileid=='xlsx') {
        if ($_FILES['userfile']['type']!="application/vnd.ms-excel" &&  $_FILES['userfile']['type']!="application/octet-stream")
        {
            echo '<script>alert("Inconsistent Upload file. Upload Fail!");</script>';
            return;
        }
        $uploaddir = '/srv/http/speedlab/enotes/upload-xls/';
        $uploadfile = $uploaddir . "Paper_Submission_Management." . $fileid ;
    }


    else {
        if (($_FILES['userfile']['type']!="application/vnd.ms-powerpoint")&&
            ($_FILES['userfile']['type']!="application/vnd.openxmlformats-officedocument.presentationml.presentation"))
        {
            echo '<script>alert("Inconsistent Upload file. Upload Fail!");</script>';
            return;
        }
        $uploaddir = '/srv/http/speedlab/enotes/upload-ppt/';
        $uploadfile = $uploaddir . $_SERVER['PHP_AUTH_USER'] . "." . $fileid ;
    }

    print "<pre>";
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo '<script>alert("Upload OK!");</script>';
    } else {
        echo '<script>alert("Upload Fail!");</script>';
    }
    print "</pre>";
}

function UploadFileID($PESN_FILE,$m_no,$m_fileid)
{
    $fp_r = @fopen( $PESN_FILE , "r" ) or die( "Can't open $PESN_FILE!" ) ;
    $fp_tmp = fopen( "/var/tmp/tmpfile" , "w" );
    while(!feof($fp_r))
    {
        $str = fgets($fp_r, 128);
        if (sscanf($str,"%s %s %s %s",$no,$cname,$tmpfileid,$mailaddr)!=4)
            continue;

        if (($no==$m_no)&&($m_fileid>$tmpfileid))
            $str2=$no." ".$cname." ".$m_fileid." ".$mailaddr."\n";
        else
            $str2=$no." ".$cname." ".$tmpfileid." ".$mailaddr."\n";
        fputs($fp_tmp,$str2);
    }
    @fclose($fp_r);
    @fclose($fp_tmp);
    copy("/var/tmp/tmpfile",$PESN_FILE) or die("copy failure!");
}

/*
 * Show MS1, MS2, PHD lists
 */
function ShowPESNList($PesnClass)
{
    global $CurrUserNo;

    echo "<div>";
    echo "<a href=\"mailto: $PesnClass@speed.cis.nctu.edu.tw\"><font style=\"Color:#191970;Font-family:'Arial Black'\">$PesnClass List</font></a>";
    echo "</div>";
    echo "<table WIDTH=90% ALIGN=left borderColor=#4169e1 border=1 cellpadding=0 cellspacing=0>";
    echo "<tr bgcolor=#b0c4de align=center style=\"Font-weight:bold\">";
    echo "	<td width=10%>Name</td>";
    echo "	<td width=10%>ID</td>";
    echo "	<td width=20%>E-mail</td>";
    echo "	<td width=10%>Last Update</td>";
    echo "	<td width=8%>Log</td>";
    echo "	<td width=8%>Slides</td>";
    echo "	<td>Upload File</td>";

    echo "</tr>\n";
    $PESN_FILE = "PESN_".$PesnClass.".lst";
    $fp_r = @fopen( $PESN_FILE , "r" ) or die( "Can't open $PESN_FILE!" ) ;

    while(!feof($fp_r))
    {
        $str = fgets($fp_r, 128);
        if (sscanf($str,"%s %s %s %s",$no,$cname,$fileid,$mailaddr)!=4)
            continue;

        $filename1 = "$no-$fileid.doc";
        $filename2 = "$no.ppt";

        if (file_exists("upload-doc/$filename1") || file_exists("upload-ppt/$filename2"))
        {
            $i = 0;
            $j = 0;
            if(file_exists("upload-doc/$filename1"))
                $i = filemtime("upload-doc/$filename1");
            if(file_exists("upload-ppt/$filename2"))
                $j = filemtime("upload-ppt/$filename2");

            if ( $i>$j )
                $LastUpdate = date("y/m/d H:i", $i);
            else
                $LastUpdate = date("y/m/d H:i", $j);
        } else {
            $LastUpdate="nonexistence";
        }
?>
            <tr align=center>
            <td><?php echo $cname?></td>
            <td><?php echo $no?></td>
            <td><a href="mailto:<?php echo $mailaddr?>"><?php echo $mailaddr?></a></td>
            <td><?php echo $LastUpdate?></td>
            <td>
            <input TYPE=button class="button" onclick="opendoc('<?php echo $no?>','<?=$no?>');" value="open">
            </td>

            <td>
            <?php
                $afilename="upload-ppt/".$no.".ppt";
                if (file_exists($afilename)){
                    echo '<input TYPE=button class="button" onclick="openppt(' . $no . ');" value="open">';
                } else {
                    echo "---";
                }
            ?>
            </td>

<?php
        
            echo "<td align=left>";
        if ($CurrUserNo==$no)
        {
            $currfilename="upload-doc/".$no."-".$fileid.".doc";
            if ((file_exists($currfilename))&&(filesize($currfilename)>1024*1024*20))
                $acc=1;
            else
                $acc=0;
?>

            <form enctype="multipart/form-data" action="main2.php" method="POST">
                <input type=hidden id="PESNFILE" name="PESNFILE" value="<?php echo $PESN_FILE?>">
        From : <input name="userfile" type="file"
            style="Font-size:9pt;border-color:#ccccff;height:14pt;background:#ffffff;"><BR>
        To : <?php echo $no?>.<select id="fileid" name='fileid'>

<?php
            for($i=97;$i<=ord($fileid)+$acc;$i++)
                echo "<option value='".chr($i)."'>".chr($i)."</option>"
?>

            <option selected value='doc'>doc</option>
            <option value='ppt'>ppt</option>
            </select>
            <input type="submit" value="Upload" class="button" style="background:#cc8888">
        </form>

<?php
        }
        else echo "&nbsp";
        echo "</td></tr>\n";
    }

    echo "</table><P>"	;
    @fclose($fp_r);

}
?>



<?php

function ShowPESN_POSTDR($PesnClass)
{
    global $CurrUserNo;

    echo "<a href=\"mailto: $PesnClass@speed.cis.nctu.edu.tw\"><font style=\"Color:#191970;Font-family:'Arial Black'\">Paper Submission Management</font></a>";
    echo "<table WIDTH=90% ALIGN=left borderColor=#4169e1 border=1 cellpadding=0 cellspacing=0>";
    echo "<tr bgcolor=#b0c4de align=center style=\"Font-weight:bold\">";
    echo "<td width=10%>Name</td>";
    echo "<td width=10%>ID</td>";
    echo "<td width=20%>E-mail</td>";
    echo "<td width=10%>Last Update</td>";
    echo "<td width=8%>xls</td>";
    echo "<td width=8%>Slides</td>";
    echo "<td>Upload File</td>";
    echo "</tr>\n";
    $PESN_FILE = "PESN_".$PesnClass.".lst";
    $fp_r = @fopen( $PESN_FILE , "r" )
        or die( "Can't open $PESN_FILE!" ) ;

    while(!feof($fp_r))
    {
        $str = fgets($fp_r, 128);
        if (sscanf($str,"%s %s %s %s",$no,$cname,$fileid,$mailaddr)!=4)
            continue;
        //$filename="$no-$fileid.doc";

        $file1="Paper_Submission_Management.xls";
        $file2="Paper_Submission_Management.xlsx";

        if (file_exists("upload-xls/$file1") || file_exists("upload-xls/$file2")) {
            $i = filemtime("upload-xls/$file1");
            $j = filemtime("upload-xls/$file2");
            if ( $i>$j )
                $filename1=$file1;
            else
                $filename1=$file2;
        }

        if (file_exists("upload-xls/$filename1") ) {
            $i = filemtime("upload-xls/$filename1");
            $LastUpdate = date("y/m/d H:i", $i);
        } else
            $LastUpdate="nonexistence";
?>
        <tr align=center>
        <td><?php echo $cname?></td>
        <td><?php echo $no?></td>
        <td><a href="mailto:<?php echo $mailaddr?>"><?=$mailaddr?></a></td>
        <td><?php echo $LastUpdate?></td>
        <td>

        <?php echo $no?>
        <select id='sel_fn_<?php echo $no?>' style="height:14pt;Font-size:9pt;">

        <?php
            for($i=97;$i<=ord($fileid);$i++)
            echo "<option selected value='".chr($i)."'>".chr($i)."</option>"
        ?>

        </select>.doc
            <input TYPE=button class="button" onclick="openxls('<?php echo $filename1?>');"
            value="open">
        </td>
        <td><?php
            $afilename="upload-ppt/".$no.".ppt";
        if (file_exists($afilename)){ ?>

        </td>
<?php
            echo "<td align=left>";
        if ($CurrUserNo==$no)
        {
            $currfilename="upload-doc/".$no."-".$fileid.".doc";
            if ((file_exists($currfilename))&&(filesize($currfilename)>1024*1024*20))
                $acc=1;
            else
                $acc=0;
?>
    <form enctype="multipart/form-data" action="main2.php" method="POST">
            <input type=hidden id="PESNFILE" name="PESNFILE" value="<?php echo $PESN_FILE?>">
        From: <input name="userfile" type="file" class="button"><BR>
        To&nbsp&nbsp: <?php echo $no?>.<select id="fileid" name='fileid'>
<?php
            for($i=97;$i<=ord($fileid)+$acc;$i++)
                echo "<option value='".chr($i)."'>".chr($i)."</option>";
?>
            <option selected value='xls'>xls</option>
        <option value='xlsx'>xlsx</option>
            </select>
            <input  style="button" type="submit" value="Upload" class="button">
    </form>
<?php
        }
        else echo "&nbsp";
        echo "</td>";
?>

<?php
        echo "</tr>\n";
        }

        echo "</table><P>"	;
        @fclose($fp_r);
    }
}
?>


</body>
</html>
