File: /var/www/dk/wp-content/themes/food/page-no_auth_server.php
<?php
/* Template Name: no_auth_server */
///
/// 没有登录可访问API
//$result = new stdClass();
//$result->rc = ApiErrorDesc::ERR_URL[0];
//$result->msg = ApiErrorDesc::ERR_URL[1];
echo 'no_auth_server';
/**
* 子类自动加载
*/
class NoauthAutoloader
{
public static function myAutoload($name)
{
$ver = isset($_REQUEST['v']) ? intval($_REQUEST['v']) : 0; //接口版本
$class_path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
$file_path = $class_path; // $verArr[0]; // piece1
if (intval($ver) > 0) {
$file = get_stylesheet_directory() . '/NoauthController/v' . $ver . '/' . $file_path . '.php';
} else {
$file = get_stylesheet_directory() . '/NoauthController/' . $file_path . '.php';
}
// echo "<br>file == " .$file;
if (file_exists($file)) {
// echo '存在文件';
if (intval($ver) > 0) {
$infilename = 'NoauthController/v' . $ver . '/' . $file_path . '.php';
} else {
$infilename = 'NoauthController/' . $file_path . '.php';
}
// echo "infilename == " .$infilename;
include_once($infilename);
if (class_exists($file_path, false)) {
return true;
}
} else {
$result = new stdClass();
$result->rc = ApiErrorDesc::ERR_CONTROLLER[0];
$result->msg = ApiErrorDesc::ERR_CONTROLLER[1];
echo_json($result);
die();
}
return false;
}
}
spl_autoload_register('NoauthAutoloader::myAutoload');
/**
* PHP单一文件框架设计简析
* 1、MVC架构实现
* 2、URL路由原理
*/
//URL路由原理
/**
* 路由作用
* 获取url中的c和a变量,执行c类对应的方法a,实现不同的路由
*/
class NoauthApp
{
public $c;
public $a;
public function run()
{
$c = isset($_REQUEST['c']) ? htmlspecialchars($_REQUEST['c']) : "Index"; //url提供类名字的变量名
$a = isset($_REQUEST['a']) ? htmlspecialchars($_REQUEST['a']) : "Index"; //url提供方法名字的变量名
$c = ucfirst(strtolower($c)) . "Noauth";
echo 'c ==' . $c . PHP_EOL;
echo 'a ==' . $a;
if (class_exists($c) && method_exists($c, $a)) {
echo 'c == 1';
// $o = new $c();
// $o->$a();
// $o = new IndexNoauth();
// $o->Index();
} else {
echo 'c == 2';
$result = new stdClass();
$result->rc = ApiErrorDesc::ERR_CONTROLLER[0];
$result->msg = ApiErrorDesc::ERR_CONTROLLER[1];
echo_json($result);
die();
}
}
}
/**
* 控制器
* 所有功能控制器继承该类
*/
class NoauthController
{
public $result;
public $debug;
public $ver;
//允许的请求方式
protected static $method_type = array('get', 'post', 'put', 'patch', 'delete');
public function __construct()
{
$this->result = new stdClass();
$this->result->rc = ApiErrorDesc::ERR_CONTROLLER[0];
$this->result->msg = ApiErrorDesc::ERR_CONTROLLER[1];
$this->debug = isset($_REQUEST['debugmod']) ? intval($_REQUEST['debugmod']) : 0;
$this->ver = isset($_REQUEST['v']) ? intval($_REQUEST['v']) : 0;
// if ($this->debug) {
// echo 'ver -> ' . $this->ver;
// }
}
function __destruct()
{
// print_r($this->result);
// echo_json($this->result);
if (isset($this->result->requestContentType)) {
$requestContentType = $this->result->requestContentType;
unset($this->result->requestContentType);
if (strpos($requestContentType, 'application/json') !== false) {
// $response = $this->encodeJson($rawData);
// echo $response;
echo_json($this->result);
} else if (strpos($requestContentType, 'text/html') !== false) {
$response = $this->encodeHtml($this->result);
echo $response;
} else if (strpos($requestContentType, 'application/xml') !== false) {
$response = $this->encodeXml($this->result);
echo $response;
} else {
echo_json($this->result);
}
} else {
echo_json($this->result);
}
// if ($this->ver == 2) {
// echo preg_replace_callback(
// '/\\\\u([0-9a-zA-Z]{4})/',
// function ($matches) {
// return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16');
// },
// $this->result
// );
// } else {
// echo_json($this->result);
// }
}
// public function __construct()
// {
// $this->view = new View();
// $this->model = new Model();
// }
private $httpVersion = "HTTP/1.1";
public function setHttpHeaders($statusCode = 200, $contentType = 'application/json')
{
$statusMessage = $this->getHttpStatusMessage($statusCode);
header($this->httpVersion . " " . $statusCode . " " . $statusMessage);
header("Content-Type:" . $contentType);
}
//$requestContentType = $_SERVER['HTTP_ACCEPT'];
//$this ->setHttpHeaders($requestContentType, $statusCode);
public function getHttpStatusMessage($statusCode)
{
$httpStatus = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported');
return ($httpStatus[$statusCode]) ? $httpStatus[$statusCode] : $httpStatus[500];
}
public function encodeHtml($responseData)
{
$htmlResponse = "<table>";
foreach ($responseData as $key => $value) {
$htmlResponse .= "<tr><td>" . $key . "</td><td>" . $value . "</td></tr>";
}
$htmlResponse .= "</table>";
return $htmlResponse;
}
public function encodeJson($responseData)
{
$jsonResponse = json_encode($responseData);
return $jsonResponse;
}
public function encodeXml($responseData)
{
// 创建 SimpleXMLElement 对象
$xml = new SimpleXMLElement('<?xml version="1.0"?><site></site>');
foreach ($responseData as $key => $value) {
$xml->addChild($key, $value);
}
return $xml->asXML();
}
/**
* 网站首页
*/
public function Index()
{
$this->result->rc = ApiErrorDesc::ERR_URL[0];
$this->result->msg = ApiErrorDesc::ERR_URL[1];
}
}
class IndexNoauth extends NoauthController
{
/// 网站首页
public function Index()
{
$this->result->rc = ApiErrorDesc::ERR_CONTROLLER[0];
$this->result->msg = ApiErrorDesc::ERR_CONTROLLER[1];
}
}
//单一文件入口
$app = new NoauthApp();
$app->run();