<?php
//============================================================================
// WIS Class. PHP Class to read data from WIS - Information system.
// Version: 0.3b
// Copyright (c) Tomas Kavalek, kavalek.net 2006
// All rights reserved
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Copy of GNU Lesser General Public License at: http://www.gnu.org/copyleft/lesser.txt
//
// For support contact tomas@kavalek.net
//============================================================================
class WIS {
  var 
$wis_host;      // Name of host - server name (protocol not required)
  
var $wis_file;      // File from host
  
var $wis_username;  // Authentication - username
  
var $wis_password;  // Authentication - password
  
var $wis_port;      // Host port - (HTTPS - 443)
  
var $wis_content;   // Content of file

  //------------------------------------------------------------------------
  
  /** 
   * Constructor Create WIS object.
   *
   * @param string $username   Login
   * @param string $password   Password
   * @param string $auth       Authentication method
   * @param string $host       Host
   * @param string $file       File
   * @param string $port       Port number
   * @return null
   */
    
function WIS($username,
                 
$password,
                 
$host "wis.fit.vutbr.cz",
                 
$file "/FIT/st/study-v.php",
                 
$port 443)  {
      
register_shutdown_function(array(&$this'_WIS'));
      
$this->wis_username $username;
      
$this->wis_password $password;
      
$this->wis_host $host;
      
$this->wis_file $file;
      
$this->wis_port $port;
      
$this->getAuthenticatedFile();
    }

  
//------------------------------------------------------------------------
    
  /**
   * In-case sensitive function str_replace
   *
   * This function is supported in PHP 5 only. So for other versions
   * there is same function by bradhuizenga AT softhome DOT net.
   * Thanx.
   * 
   * @param string $input      HTML source code
   * @return string
   */ 
    
function stri_replace($find$replace$string) {
      if(!
is_array($find)) $find = array($find);
      if(!
is_array($replace)) {
        if(!
is_array($find)) $replace = array($replace);
        else {
          
$c count($find);
          
$rString $replace;
          unset(
$replace);
          for(
$i 0$i $c$i++) {
            
$replace[$i] = $rString;
          }
        }
      }
      foreach(
$find as $fKey => $fItem) {
        
$between explode(strtolower($fItem),strtolower($string));
        
$pos 0;
        foreach(
$between as $bKey => $bItem) {
          
$between[$bKey] = substr($string,$pos,strlen($bItem));
          
$pos += strlen($bItem) + strlen($fItem);
        }
        
$string implode($replace[$fKey],$between);
      }
      return(
$string);
    }

  
//------------------------------------------------------------------------

  /** 
   * Remove all diacritical marks from input text.
   *
   * @param string $input      Input text
   * @return string
   */  
    
function removeDiacriticalMarks($input) {
      return 
strtr($input"áäèïéìëíòóöø¹»úùüý¾ÁÄÈÏÉÌËÍÒÓÖØ©«ÚÙÜÝ®",
                           
"aacdeeeinoorstuuuyzAACDEEEINOORSTUUUYZ");
    }

  
//------------------------------------------------------------------------

  /** 
   * Get authenticated file and return its content.
   *
   * @param null
   * @return bool
   */  
    
function getAuthenticatedFile() {
        if(!
function_exists("curl_init")) {
          
$fp pfsockopen("ssl://" $this->wis_host$this->wis_port$errno$errstr);
          
$result "";
          if(!
$fp) {
            
printf("Error %s | Error code %s<br />\n"$errstr$errno);
            return 
false;
          } else {
            
fputs($fp"GET $this->wis_file HTTP/1.1\r\n");
            
fputs($fp"Host: $this->wis_host\r\n");
            
fputs($fp"Authorization: Basic " .
                       
base64_encode("$this->wis_username:$this->wis_password") . 
                       
"\r\n");
            
fputs($fp"Connection: close\r\n\r\n");
            while(!
feof($fp)) $result .= fgets($fp128);
            
fclose($fp);
            
$result strstr($result"<!DOCTYPE");
            
$this->wis_content $this->removeDiacriticalMarks($result);
            return 
true;
          }
        
// Modification by Vojtech Beil (vojtabeil@c-box.cz)
        
} else {
        
$ch curl_init();
        
        
curl_setopt($chCURLOPT_URL"https://" $this->wis_host $this->wis_file);
        
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
        
curl_setopt($chCURLOPT_SSL_VERIFYHOST2);
        
curl_setopt($chCURLOPT_HEADERfalse);
        
curl_setopt($chCURLOPT_HTTPHEADER, array("Authorization: Basic " base64_encode("$this->wis_username:$this->wis_password")));
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
        
$result curl_exec($ch);
        
        
$this->wis_content $this->removeDiacriticalMarks($result);
        
        if(
curl_errno($ch)) {
          return 
false;
        } else {
          
curl_close($ch);
          return 
true;
        }
      } 
    }
    
  
//------------------------------------------------------------------------

  /** 
   * Get student name.
   *
   * @param null
   * @return string
   */  
    
function getName() {
      
$nameContent strstr($this->wis_content$this->wis_username);
      return 
substr($nameContentstrpos($nameContent"<b>"),
                    
strpos($nameContent"</b>") - 6); 
    }
    
  
//------------------------------------------------------------------------

  /** 
   * Get course values.
   *
   * @param string $course     Course ID
   * @return string
   */
    
function getCourse($course) {
      
$courseContent strstr($this->wis_content$course);
      
$course substr($courseContent0strpos($courseContent"</tr>"));
      
$course eregi_replace("<[/]{0,1}b>"""$course);
      
$course eregi_replace("<a[^<]*>([^<]*)</a>""\\1"$course);
      
$course eregi_replace("<[^<]*>""&nbsp;"$course);
      
$course $this->stri_replace("&nbsp;&nbsp;&nbsp;&nbsp;""&nbsp;"$course);
      
$course $this->stri_replace("&nbsp;&nbsp;&nbsp;""&nbsp;"$course);
      
$course $this->stri_replace("&nbsp;&nbsp;""&nbsp;"$course);
      return 
$course;
    }
    
  
//------------------------------------------------------------------------

  /** 
   * Explode course values accord ID.
   * Supported ID:
   *   - ZKR, PREDMET, TYP, UK, BODY, ZAP, ZN, KR, DATUM, TERM
   *   - case sensitive -> UpperCase!      
   *
   * @param string $course     Course value to parse   
   * @param array $id          Value ID
   * @return string
   */
    
function parseCourse($course$id) {
      
$courseValue = array();
      
$result "";
      list(
$courseValue["ZKR"],
           
$courseValue["PREDMET"],
           
$courseValue["TYP"],
           
$courseValue["UK"],
           
$courseValue["BODY"],
           
$courseValue["ZAP"],
           
$courseValue["ZN"],
           
$courseValue["KR"],
           
$courseValue["DATUM"],
           
$courseValue["TERM"]) = explode("&nbsp;"$course);
      for(
$i 0$i count($id); $i++) {
        
$result .= $courseValue[$id[$i]] . "&nbsp;";
      }
      return 
$result;
    }
    
  
//------------------------------------------------------------------------

  /** 
   * Get course values.
   *
   * @param array $courses    Course IDs
   * @param string $br        Brake mark    
   * @return null
   */
    
function printCourses($courses$br "<br />\n") {
      for(
$i 0$i count($courses); $i++)
        
printf("%s%s"$this->getCourse($courses[$i]), $br);
    }
    
  
//------------------------------------------------------------------------


  /** 
   * Get course parsed values.
   *
   * @param array $courses    Course IDs
   * @param string $id        Value ID - key
   * @param string $br        Brake mark   
   * @return null
   */
    
function printCoursesParsed($courses$id$br "<br />\n") {
      for(
$i 0$i count($courses); $i++) {
        
$course $this->getCourse($courses[$i]);
        
$courseParsed $this->parseCourse($course$id);
        
printf("%s%s"$courseParsed$br);
      }
    }
    
  
//------------------------------------------------------------------------

  /**
   * Descruction of WIS
   *
   * @param null
   * @return nothing
   */
    
function _WIS () {
      return;
    }
}
?>