<?php

if(isset($_GET['source']))
{
    
show_source($_SERVER['SCRIPT_FILENAME']);
    die();
}

class 
HSNUData
{
    private 
$ch;
    private 
$cookiefile;
    private 
$output;
    private 
$error;
    private 
$student_name;
    private 
$item_name = array(
        
'fundamental'    => '基本',
        
'moral'        => '德育獎懲',
        
'absent'    => '缺曠統計',
        
'grade'        => '各次考試所有成績');
    private 
$item_url = array(
        
'fundamental'    => 'fundamental.asp',
        
'moral'        => 'moralculture_%20bonuspenalty.asp',
        
'absent'    => 'absentation_skip_school.asp',
        
'grade'        => 'grade_chart_all.asp');
    private 
$list_name = array(
        
'list_year'    => '歷年成績',
        
'list_all'    => '各式成績',
        
'list_spec'    => '各次考試單科成績');
    private 
$list_url = array(
        
'list_year'    => 'year_accomplishment.asp?action=selection',
        
'list_all'    => 'student_subjects_number.asp?action=open_window_frame',
        
'list_spec'    => 'grade_chart_single_select_subject.asp');
    private 
$option_name;
    private 
$option_url;
    
    private function 
argenc($url)
    {
        
$url urlencode($url);
        
$url str_replace('%3F''?'$url);
        
$url str_replace('%3D''='$url);
        
$url str_replace('%26''&'$url);
        return 
$url;
    }
    
    private function 
fetchurl($url)
    {
        
curl_setopt($this->chCURLOPT_URL$url);
        
$this->output curl_exec($this->ch);
        @
$this->output iconv('Big5''UTF-8//IGNORE'$this->output);
    }
    
    public function 
__construct()
    {
        
$this->ch curl_init();
        
$this->cookiefile tempnam('/tmp''hsnugrades');
        
curl_setopt($this->chCURLOPT_COOKIEFILE$this->cookiefile);
        
curl_setopt($this->chCURLOPT_COOKIEJAR$this->cookiefile);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFERtrue);
    }
    
    public function 
__destruct()
    {
        
curl_close($this->ch);
        
unlink($this->cookiefile);
    }
    
    public function 
printoutput()
    {
        
$this->output str_replace('../',
                        
'http://grades.hs.ntnu.edu.tw/online/',
                        
$this->output);
        
$this->output preg_replace('/<script.*<\/script>/msiU'''$this->output);
        
$this->output preg_replace('/.*<body>/msiU'''$this->output);
        
$this->output preg_replace('/<\/body>.*/msiU'''$this->output);
        echo 
$this->output;
    }
    
    public function 
geterror()
    {
        return 
$this->error;
    }
    
    public function 
getstudentname()
    {
        return 
$this->student_name;
    }
    
    public function 
login($id$pass)
    {        
        
$postdata "division=senior&Loginid=$id&LoginPwd=$pass&Uid=&Enter=LOGIN";
        
curl_setopt($this->ch,
                
CURLOPT_URL,
                
'http://grades.hs.ntnu.edu.tw/online/login.asp');
        
curl_setopt($this->chCURLOPT_POSTtrue);
        
curl_setopt($this->chCURLOPT_POSTFIELDS$postdata);
        
$this->output curl_exec($this->ch);
        
curl_setopt($this->chCURLOPT_POSTfalse);
        
        
// check error
        
$res preg_match_all(
            
'|<input type="hidden" id="msg" name="msg" value="(.*)" />|',
            
$this->output$matchesPREG_PATTERN_ORDER);
        if(
$res)
        {
            @
$this->error iconv('Big5''UTF-8//IGNORE'$matches[1][0]);
            return 
false;
        }
        
        
// fetch data
        
curl_setopt($this->ch,
                
CURLOPT_URL,
                
'http://grades.hs.ntnu.edu.tw/online/student/'
                
'selection_look_over_data.asp?look_over=right_below&school_class=');
        
$this->output curl_exec($this->ch);
        
        
preg_match_all('|onClick="javascript:window\.open\(\'(.*)\',|',
                
$this->output$matchesPREG_PATTERN_ORDER);
        
$url 'http://grades.hs.ntnu.edu.tw/online/student/' $matches[1][0];
        
preg_match_all('|student_name=(.*)&teacher_class=|',
                
$url$matchesPREG_PATTERN_ORDER);
        @
$this->student_name iconv('Big5''UTF-8//IGNORE'$matches[1][0]);
        
        
//$url = $this->argenc($url);
        
curl_setopt($this->chCURLOPT_URL$url);
        
curl_exec($this->ch);
        
        return 
true;
    }
    
    public function 
getitemname($item)
    {
        return 
$this->item_name[$item];
    }

    public function 
getlistname($list)
    {
        return 
$this->list_name[$list];
    }
    
    public function 
fetchdata($item)
    {
        
$url 'http://grades.hs.ntnu.edu.tw/online/selection_student/'
            
$this->item_url[$item];
        
$this->fetchurl($url);
    }

    public function 
fetchlist($list)
    {
        
curl_setopt($this->ch,
                
CURLOPT_URL,
                
'http://grades.hs.ntnu.edu.tw/online/selection_student/'
                
$this->list_url[$list]);
        
$this->output curl_exec($this->ch);
        
        
preg_match_all('|<option value=".*">(.*)</option>|msiU',
                
$this->output$matchesPREG_PATTERN_ORDER);
        
$this->option_name $matches[1];
        
preg_match_all('|<option value="(.*)">.*</option>|msiU',
                
$this->output$matchesPREG_PATTERN_ORDER);
        
$this->option_url $matches[1];
        
        if(
$list=='list_all')
        {
            
$this->option_name array_slice($this->option_name2);
            
$this->option_url array_slice($this->option_url2);
        }
        
        foreach(
$this->option_name as $key => $val)
        {
            
$val str_replace(PHP_EOL''$val);
            @
$this->option_name[$key] = iconv('Big5''UTF-8//IGNORE'$val);
        }
        foreach(
$this->option_url as $key => $val)
        
$this->option_url[$key] = $this->argenc($val);
    }
    
    public function 
getoptions()
    {
        return 
$this->option_name;
    }
    
    public function 
fetchlistitem($listitem)
    {
        
$url 'http://grades.hs.ntnu.edu.tw/online/selection_student/'
            
$this->option_url[$listitem];
        
$this->fetchurl($url);
    }
}

?>