Skip to content

Commit

Permalink
Testing new API structure
Browse files Browse the repository at this point in the history
  • Loading branch information
LckySndays committed Aug 7, 2016
1 parent c1ac737 commit c1b9f49
Show file tree
Hide file tree
Showing 7 changed files with 782 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="container">

<h1>Lanyang API - Test-Pages</h1>
<form action="lib/api/0.0.1/lanyang-api.php" method="post">
<form action="lib/api/0.0.2/api.php" method="post">

Action:
<input type="radio" name="action" value="validate" checked> validate
Expand Down
50 changes: 50 additions & 0 deletions lib/api/0.0.2/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

header('Access-Control-Allow-Origin: *');

$action = "none";

if(isset($_POST['action'])){
$action = htmlspecialchars($_POST['action']);
}

if ($action == "validate"){

require_once('stu-account.php');

if(!isset($_POST['uid']) || !isset($_POST['pass'])){
echo "missing parameter";
}else{
$response = new StuAccount();
$response->login($_POST['uid'], $_POST['pass']);
$response->output();
}
}

if ($action == "stu-info"){

require_once('stu-info.php');

if(!isset($_POST['uid']) || !isset($_POST['pass'])){
echo "missing parameter";
}else{
$response = new StuInfo();
$response->parse_stu_info($_POST['uid'], $_POST['pass']);
$response->output();
}
}

if ($action == "stu-score"){

require_once('stu-score.php');

if(!isset($_POST['uid']) || !isset($_POST['pass'])){
echo "missing parameter";
}else{
$response = new StuScore();
$response->parse_stu_Score($_POST['uid'], $_POST['pass']);
$response->output();
}
}

?>
96 changes: 96 additions & 0 deletions lib/api/0.0.2/class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

class LanyangAPI{

private $filename = "";
private $version = "";
private $dependencies = "";
private $description = "";
private $response = "";

private $isLogin = "FALSE";


public function getFilename(){
return $this->filename;
}

public function getVersion(){
return $this->version;
}

public function getDependencies(){
return $this->dependencies;
}

public function getDescription(){
return $this->description;
}

public function getIsLogin(){
return $this->isLogin;
}

public function getResponse(){
return $this->response;
}

public function setFilename($value){
$this->filename = $value;
}

public function setVersion($value){
$this->version = $value;
}

public function setDependencies($value){
$this->dependencies = $value;
}

public function setDescription($value){
$this->description = $value;
}

public function setIsLogin($value){
$this->isLogin = $value;
}

public function setResponse($value){
$this->response = $value;
}

public function output(){
$json = array(
"information" => array(
"filename" => $this->filename,
"version" => $this->version,
"dependencies" => $this->dependencies,
"description" => $this->description
),
"response" => $this->response
);

echo "<pre>";
echo $this->json_encode_unicode($json);
echo "</pre>";
}

public function json_encode_unicode($data) {
if (defined('JSON_UNESCAPED_UNICODE')) {
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i',
function ($m) {
$d = pack("H*", $m[1]);
$r = mb_convert_encoding($d, "UTF8", "UTF-16BE");
return $r!=="?" && $r!=="" ? $r : $m[0];
}, json_encode($data)
);
}

}




?>
72 changes: 72 additions & 0 deletions lib/api/0.0.2/lanyang-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

header('Access-Control-Allow-Origin: *');

$action = "error";
$status = "error";
$response = "error";

if(isset($_POST['action'])){
$action = htmlspecialchars($_POST['action']);
}

if ($action == "validate"){
include("stu-account.php");

if(!isset($_POST['uid']) || !isset($_POST['pass'])){
$status = "Fail";
}else{
$status = "OK";
$response = login($_POST['uid'], $_POST['pass']);
}
}

if ($action == "stu-info"){
include("stu-info.php");

if(!isset($_POST['uid']) || !isset($_POST['pass'])){
$status = "Fail";
}else{
$status = "OK";
$response = parse_stu_info($_POST['uid'], $_POST['pass']);
}
}

if ($action == "stu-score"){
include("stu-score.php");

if(!isset($_POST['uid']) || !isset($_POST['pass'])){
$status = "Fail";
}else{
$status = "OK";
$response = parse_stu_score($_POST['uid'], $_POST['pass']);
}
}

$json = array(
"action" => $action,
"timestamp" => date("Y-m-d H:i:s"),
"status" => $status,
"response" => $response
);

$final = json_encode_unicode($json);

echo "<pre>";
echo $final;
echo "</pre>";

function json_encode_unicode($data) {
if (defined('JSON_UNESCAPED_UNICODE')) {
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i',
function ($m) {
$d = pack("H*", $m[1]);
$r = mb_convert_encoding($d, "UTF8", "UTF-16BE");
return $r!=="?" && $r!=="" ? $r : $m[0];
}, json_encode($data)
);
}

?>
50 changes: 50 additions & 0 deletions lib/api/0.0.2/stu-account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
require_once('class.php');
$filename = "stu-account.php";
$version = "0.0.2";
$dependencies = "none";
$description = "validate student account credential";

class StuAccount extends LanyangAPI{

function __construct(){
parent::setFilename($GLOBALS['filename']);
parent::setVersion($GLOBALS['version']);
parent::setDependencies($GLOBALS['dependencies']);
parent::setDescription($GLOBALS['description']);
}

function login($username, $password){

$status = "";

$curl = curl_init();
$url = "portal.tku.edu.tw/NEAI/login2.do?action=EAI&myurl=http://portal.tku.edu.tw/aissinfo/emis/tmw0012.aspx&ln=en_US&username=$username&password=$password&loginbtn=Login";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, 1);

$curlResult = curl_exec($curl);
curl_close($curl);

if (strpos($curlResult, 'Tamkang University Single Sign On(SSO)') !== false) {
$status = "FAILED";
parent::setIsLogin("FALSE");
}else{
$status = "OK";
parent::setIsLogin("TRUE");
}

$response = array(
"username" => $username,
"status" => $status
);

return parent::setResponse($response);
}

}


?>
Loading

0 comments on commit c1b9f49

Please sign in to comment.