更改项目根目录

test
橙橙楊 5 years ago committed by luffy
parent 7d3681fcb3
commit 01b6ab4367
  1. 1
      admin/.htaccess
  2. 13
      admin/Alert.php
  3. 60
      admin/Login.php
  4. 102
      admin/Manager.php
  5. 2
      admin/Question.php
  6. 2
      admin/WasteSorting.php
  7. 4
      config.php
  8. 35
      database/Query.php
  9. 96
      entity/Manager.php
  10. 27
      index.php
  11. 18
      script/db.sql
  12. 1
      script/main_Garbage.sql
  13. 11
      test.php

@ -0,0 +1 @@
#php_value auto_prepend_file "Manager.php"

@ -19,6 +19,19 @@ function response_with_click($message, $class, $script)
EOF;
}
function response($message, $class)
{
echo <<<EOF
<div class="alert $class alert-dismissible fade show col-5 m-auto text-center" role="alert">
<strong>$message</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
EOF;
}
function response_with_href($message, $class, $href)
{
response_with_click($message, $class, <<<EOF

@ -0,0 +1,60 @@
<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/admin/Alert.php";
//IP地址转换
function get_remote_ip()
{
return $_SERVER['REMOTE_ADDR'] == "::1" ? "127.0.0.1" : $_SERVER['REMOTE_ADDR'];
}
function update_token(QueryManager $manager_result, $msg)
{
// 通过验证,更新cookie信息
$manager = $manager_result->getManagerResult();
$manager->setLastIp(get_remote_ip());
$manager->setLastTime(date(default_format));
DbUtil::update("update manager set last_ip=?,last_time=? where manager_name=?", "sss", $manager->getLastIp(), $manager->getLastTime(), $manager->getManagerName());
// 跳转到首页
if ($_SERVER["SCRIPT_NAME"] == "/admin/Manager.php") {
info_res($msg, "/admin/Login.php");
} else {
response($msg, "alert-info");
}
// 登陆身份有效期半小时
setcookie("login_token", $manager->loginToken(), time() + 60 * 15, "/");
}
function check_login()
{
if (!empty($_COOKIE["login_token"]) and !empty($_COOKIE["manager_name"])) {
$manager_result = new QueryManager();
DbUtil::query("select * from manager where manager_name=?", $manager_result, array($_COOKIE["manager_name"]));
// 验证cookie身份信息有效性
if (empty($manager_result) or $_COOKIE["login_token"] != $manager_result->getManagerResult()->loginToken()) {
setcookie("manager_name", "", time() - 3600, "/");
setcookie("login_token", "", time() - 3600, "/");
// error_res("非法验证,请重新登陆", "/admin/Manager.php");
} else {
try {
update_token($manager_result, "您已登录,自动跳转到首页");
return true;
} catch (Exception $e) {
error($e);
// error_res("管理员身份验证异常,请联系管理员", "/admin/Manager.php");
}
}
}
// else {
// info_res("您尚未登陆,跳转到登陆页", "/admin/Manager.php");
// }
return false;
}

@ -0,0 +1,102 @@
<?php
//管理员后台登陆
if (file_exists("../config.php")) {
require_once "../config.php";
} else {
require_once "config.php";
}
require_once __ROOT__ . "/head.php";
getMenu("管理员登陆");
require_once __ROOT__ . "/database/DbUtil.php";
require_once __ROOT__ . "/database/Query.php";
require_once __ROOT__ . "/Log.php";
require_once __ROOT__ . "/admin/Alert.php";
require_once __ROOT__ . "/admin/Login.php";
if (!check_login()) {
if (empty($_REQUEST)) {
echo <<<EOF
<!DOCTYPE html>
<html lang="zh">
<script>
$(function() {
Array.prototype.filter.call($("form.needs-validation"), function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
})
</script>
<body>
<div class="container py-5">
<div class="d-flex justify-content-center align-items-center w-100">
<form class="col-6 needs-validation" novalidate method="post" action="/admin/Manager.php">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">管理员账号</span>
</div>
<input type="text" class="form-control" placeholder="管理员账号" name="manager_name" required>
<div class="invalid-feedback">
管理员账号不为空
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">管理员密码</span>
</div>
<input type="password" class="form-control" placeholder="管理员密码" name="manager_pwd" required>
<div class="invalid-feedback">
管理员密码不为空
</div>
</div>
<button class="btn btn-block btn-info">提交</button>
</form>
</div>
</div>
</body>
</html>
EOF;
}
//else if (!empty($_COOKIE["login_token"]) and !empty($_COOKIE["manager_name"])) {
// $manager_result = new QueryManager();
// DbUtil::query("select * from manager where manager_name=?", $manager_result, array($_COOKIE["manager_name"]));
//// 验证cookie身份信息有效性
// if (empty($manager_result) or $_COOKIE["login_token"] != $manager_result->getManagerResult()->loginToken()) {
// setcookie("manager_name", "", time() - 3600);
// setcookie("login_token", "", time() - 3600);
// error_res("非法验证,请重新登陆", "/admin/Manager.php");
// } else {
// try {
// update_token($manager_result,"您已登录,自动跳转到首页");
// } catch (Exception $e) {
// error($e);
// error_res("管理员身份验证异常,请联系管理员", "/admin/Manager.php");
// }
//
// }
//}
else if (empty($_POST["manager_name"])) {
error_res(array("status" => false, "msg" => "管理员不能为空"), "/admin/Manager.php");
} else if (empty($_POST["manager_pwd"])) {
error_res(array("status" => false, "msg" => "管理员密码不能为空"), "/admin/Manager.php");
} else {
$manager_result = new QueryManager();
DbUtil::query("select * from manager where manager_name=? and manager_pwd=?", $manager_result, array($_POST["manager_name"], md5($_POST["manager_pwd"])));
if (empty($manager_result->getManagerResult())) {
error_res("登陆失败,管理员或密码错误", "/admin/Manager.php");
} else {
update_token($manager_result, "验证成功,自动跳转到首页");
setcookie("manager_name", $manager_result->getManagerResult()->getManagerName(), time() + 60 * 15, "/");
}
}
}

@ -22,7 +22,7 @@ require_once __ROOT__ . "/admin/Alert.php";
getMenu("题目管理");
if (empty($_POST) && empty($_GET)) {
if (empty($_REQUEST)) {
echo <<<EOF
<!DOCTYPE html>
<html lang="zh">

@ -9,7 +9,7 @@ require_once __ROOT__ . "/admin/Alert.php";
getMenu("题目管理");
if (empty($_POST) && empty($_GET)) {
if (empty($_REQUEST)) {
echo <<<EOF
<!DOCTYPE html>
<html lang="zh">

@ -11,4 +11,6 @@ define("default_format", "Y-m-d H:i:s");
//根目录
//define("__ROOT__", dirname(__FILE__));
define("__ROOT__", "D:/JetBrains/PhpstormProjects/WasteSorting");
//define("__ROOT__", "D:/JetBrains/PhpstormProjects/WasteSorting");
//define("__ROOT__", $_SERVER["DOCUMENT_ROOT"]);

@ -4,6 +4,7 @@ require_once __ROOT__ . "/entity/ApiToken.php";
require_once __ROOT__ . "/entity/Garbage.php";
require_once __ROOT__ . "/entity/Question.php";
require_once __ROOT__ . "/entity/Answer.php";
require_once __ROOT__ . "/entity/Manager.php";
require_once __ROOT__ . "/database/DbUtil.php";
//Token查询
abstract class AbstractTokenQuery implements DoExcute
@ -240,6 +241,7 @@ class QueryGarbageWithCategory implements DoExcute
}
}
//查询某个垃圾分类数据量
class QueryGarbageCount implements DoExcute
{
@ -270,3 +272,36 @@ class QueryGarbageCount implements DoExcute
}
//查询管理
class QueryManager implements DoExcute
{
private $manager_result;
public function bind_param(mysqli_stmt $stmt, array $param = null)
{
if (!empty($param)) {
$stmt->bind_param(str_repeat("s", count($param)), $param[0], ...array_slice($param, 1));
}
}
public function doResult(mysqli_stmt $stmt)
{
$stmt->bind_result($col1, $col2, $col3, $col4);
while ($stmt->fetch()) {
$this->manager_result = new Manager($col1, $col2, $col3, $col4);
}
}
/**
* @return mixed
*/
public function getManagerResult()
{
return $this->manager_result;
}
}

@ -0,0 +1,96 @@
<?php
//管理员实体
class Manager
{
private $manager_name;
private $manager_pwd;
private $last_ip;
private $last_time;
/**
* Manager constructor.
* @param $manager_name
* @param $manager_pwd
* @param $last_ip
* @param $last_time
*/
public function __construct($manager_name, $manager_pwd, $last_ip, $last_time)
{
$this->manager_name = $manager_name;
$this->manager_pwd = $manager_pwd;
$this->last_ip = $last_ip;
$this->last_time = $last_time;
}
/**
* @return mixed
*/
public function getManagerName()
{
return $this->manager_name;
}
/**
* @return mixed
*/
public function getManagerPwd()
{
return $this->manager_pwd;
}
/**
* @return mixed
*/
public function getLastIp()
{
return $this->last_ip;
}
/**
* @return mixed
*/
public function getLastTime()
{
return $this->last_time;
}
public function loginToken()
{
return md5($this->last_ip . $this->last_time);
}
/**
* @param mixed $manager_name
*/
public function setManagerName($manager_name): void
{
$this->manager_name = $manager_name;
}
/**
* @param mixed $manager_pwd
*/
public function setManagerPwd($manager_pwd): void
{
$this->manager_pwd = $manager_pwd;
}
/**
* @param mixed $last_ip
*/
public function setLastIp($last_ip): void
{
$this->last_ip = $last_ip;
}
/**
* @param mixed $last_time
*/
public function setLastTime($last_time): void
{
$this->last_time = $last_time;
}
}

@ -2,15 +2,33 @@
require_once "config.php";
require_once __ROOT__ . "/head.php";
getMenu("后台管理");
require_once __ROOT__ . "/admin/Login.php";
if (!check_login()) {
error_res("您尚未登陆,跳转到登陆页", "/admin/Manager.php");
return;
}
require_once __ROOT__ . "/entity/Menu.php";
//菜单信息
$menus = array("API" => new Menu("百度API配置", "/admin/ApiConfig.php"), new Menu("垃圾分类", "/admin/WasteSorting.php"), new Menu("题目管理", "/admin/Question.php"));
getMenu("后台管理");
echo <<<EOF
<script>
function logout() {
if(confirm("确认退出?")){
}
}
</script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
@ -29,9 +47,14 @@ foreach ($menus as $key => $value) {
}
}
$manager_name = $_COOKIE["manager_name"];
echo <<<EOF
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true"><span style="color: black">欢迎管理员<b style="color: red">luffy</b>登陆</span></a>
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true"><span style="color: black">欢迎管理员<b style="color: red">$manager_name</b>登陆</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="logout()"><span style="color: red">退出</span</a>
</li>
</ul>
</div>

@ -59,3 +59,21 @@ create table answer
# 是否正确答案
is_true bool
);
# 管理员表
create table manager
(
#管理员
manager_name varchar(10) primary key,
# 密码
manager_pwd varchar(32),
#上次登录ip
last_ip varchar(15),
# 上次登陆时间
last_time datetime
);
# 超级管理员账号
insert manager (manager_name, manager_pwd)
values ('admin', md5('admin'));

@ -1,3 +1,4 @@
# 初始化垃圾数据
INSERT INTO garbage (name, category, create_at, update_at)
VALUES ('阿司匹林', 2, '2019-07-03 15:51:29.779 ', '2019-07-03 16:01:08.565 ');
INSERT INTO garbage (name, category, create_at, update_at)

@ -1,9 +1,9 @@
<?php
require_once "config.php";
require_once __ROOT__ . "/database/DbUtil.php";
require_once __ROOT__ . "/entity/Question.php";
require_once __ROOT__ . "/entity/Answer.php";
require_once __ROOT__ . "/Log.php";
//require_once __ROOT__ . "/database/DbUtil.php";
//require_once __ROOT__ . "/entity/Question.php";
//require_once __ROOT__ . "/entity/Answer.php";
//require_once __ROOT__ . "/Log.php";
//echo <<<EOF
//<html>
@ -24,6 +24,5 @@ require_once __ROOT__ . "/Log.php";
//EOF;
//var_dump(array_search(new Answer(1, 2, 3, 4), array(new Answer(4, 5, 6, 7), new Answer(2, 2, 3, 4))));
var_dump(array_search(4, array(2, 5, 1)));
var_dump(date_create(default_format));

Loading…
Cancel
Save