归档
This commit is contained in:
38
includes/lib/Aes.php
Normal file
38
includes/lib/Aes.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace lib;
|
||||
|
||||
/**
|
||||
* AES128加解密类
|
||||
* @author dy
|
||||
*
|
||||
*/
|
||||
class Aes
|
||||
{
|
||||
//密钥
|
||||
private $_secrect_key;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->_secrect_key = md5(SYS_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密方法
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public function encrypt($string)
|
||||
{
|
||||
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($this->_secrect_key), $string, MCRYPT_MODE_CBC, md5(md5($this->_secrect_key))));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密方法
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public function decrypt($string)
|
||||
{
|
||||
return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($this->_secrect_key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($this->_secrect_key))), "12");
|
||||
}
|
||||
}
|
||||
38
includes/lib/AppLoad.php
Normal file
38
includes/lib/AppLoad.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace lib;
|
||||
|
||||
class AppLoad {
|
||||
|
||||
/**
|
||||
* APP 模块加载
|
||||
*
|
||||
* @param string $name 文件默认index
|
||||
* @param string $dir 所在目录(最后请加/)
|
||||
* @return string|void
|
||||
*/
|
||||
static public function load($name = 'index',$dir=null){
|
||||
global $conf;
|
||||
$filename = ROOT.'app/'.$dir.$name;
|
||||
define("INDEX_ROOT",ROOT.'app/');
|
||||
define("STATIC_ROOT",ROOT.'assets/');
|
||||
if(file_exists($filename.'.php')) {
|
||||
return $filename.'.php';
|
||||
}else {
|
||||
http_response_code(404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 【我也不知道这是干嘛的】(慎重删除)
|
||||
* @param $template
|
||||
* @return bool
|
||||
*/
|
||||
static public function exists($template){
|
||||
$filename = TEMPLATE_ROOT.$template.'/index.php';
|
||||
if(file_exists($filename)){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
includes/lib/Cache.php
Normal file
47
includes/lib/Cache.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace lib;
|
||||
/**
|
||||
* 缓存信息(注释待完善)
|
||||
*/
|
||||
class Cache {
|
||||
public function get($key) {
|
||||
global $_CACHE;
|
||||
return $_CACHE[$key];
|
||||
}
|
||||
public function read($key = 'config') {
|
||||
global $DB;
|
||||
$value = $DB->getColumn("SELECT v FROM pre_cache WHERE k=:key LIMIT 1", [':key'=>$key]);
|
||||
return $value;
|
||||
}
|
||||
public function save($key ,$value, $expire=0) {
|
||||
if (is_array($value)) $value = serialize($value);
|
||||
global $DB;
|
||||
return $DB->exec("REPLACE INTO pre_cache VALUES (:key, :value, :expire)", [':key'=>$key, ':value'=>$value, ':expire'=>$expire]);
|
||||
}
|
||||
public function pre_fetch(){
|
||||
global $_CACHE;
|
||||
$_CACHE=array();
|
||||
$cache = $this->read('config');
|
||||
$_CACHE = @unserialize($cache);
|
||||
if(empty($_CACHE['version']))$_CACHE = $this->update();
|
||||
return $_CACHE;
|
||||
}
|
||||
public function update() {
|
||||
global $DB;
|
||||
$cache = array();
|
||||
$result = $DB->getAll("SELECT * FROM pre_config");
|
||||
foreach($result as $row){
|
||||
$cache[ $row['k'] ] = $row['v'];
|
||||
}
|
||||
$this->save('config', $cache);
|
||||
return $cache;
|
||||
}
|
||||
public function clear($key = 'config') {
|
||||
global $DB;
|
||||
return $DB->exec("UPDATE pre_cache SET v='' WHERE k=:key", [':key'=>$key]);
|
||||
}
|
||||
public function clean() {
|
||||
global $DB;
|
||||
return $DB->exec("DELETE FROM pre_cache WHERE expire>0 AND expire<'".time()."'");
|
||||
}
|
||||
}
|
||||
46
includes/lib/Cookie.php
Normal file
46
includes/lib/Cookie.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace lib;
|
||||
|
||||
class Cookie{
|
||||
/**
|
||||
* 删除cookie
|
||||
*
|
||||
* @param string $name cookie名称*
|
||||
* @param string $domain 域名
|
||||
* @return boolean
|
||||
*/
|
||||
public static function del($name,$domain=null)
|
||||
{
|
||||
$domain = isset($domain) ? $domain : null;
|
||||
return isset($_COOKIE[$name]) ? setcookie($name, '', time() - 86400, '/', $domain) : true;
|
||||
}
|
||||
/**
|
||||
* 得到指定cookie的值
|
||||
*
|
||||
* @param string $name cookie名称*
|
||||
*/
|
||||
public static function get($name)
|
||||
{
|
||||
$cookies_en=isset($_COOKIE[$name]) ? (new Aes)->decrypt($_COOKIE[$name]) : null;
|
||||
return trim($cookies_en);
|
||||
}
|
||||
/**
|
||||
* 设置cookie
|
||||
*
|
||||
* @param string $name cookie名称*
|
||||
* @param string $value cookie值*
|
||||
* @param string $expire 过期时间
|
||||
* @param string $path the path to website
|
||||
* @param string $domain the domain
|
||||
* @param string $secure 可选。规定是否通过安全的 HTTPS 连接来传输 cookie。
|
||||
* @return boolean
|
||||
*/
|
||||
public static function set($name,$value,$expire=null,$path=null,$domain=null,$secure=null)
|
||||
{
|
||||
$value= (new Aes)->encrypt($value);
|
||||
$path = isset($path) ? $path : '/';
|
||||
$domain = isset($domain) ? $domain : null;
|
||||
$secure = isset($secure) ? $secure : 0;
|
||||
return setcookie($name, $value, $expire, $path, $domain, $secure);
|
||||
}
|
||||
}
|
||||
211
includes/lib/PdoHelper.php
Normal file
211
includes/lib/PdoHelper.php
Normal file
@@ -0,0 +1,211 @@
|
||||
<?php
|
||||
namespace lib;
|
||||
|
||||
class PdoHelper
|
||||
{
|
||||
private $sqlPrefix = "pre_";//SQL数据表前缀识别字符
|
||||
private $db;
|
||||
private $fetchStyle = \PDO::FETCH_ASSOC;
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* PdoHelper constructor.
|
||||
*
|
||||
* @param array $dbconfig 数据库信息
|
||||
*/
|
||||
function __construct($dbconfig)
|
||||
{
|
||||
$this->prefix = $dbconfig['dbqz'].'_';
|
||||
try {
|
||||
$this->db = new \PDO("mysql:host={$dbconfig['host']};dbname={$dbconfig['dbname']};port={$dbconfig['port']}",$dbconfig['user'],$dbconfig['pwd']);
|
||||
} catch (Exception $e) {
|
||||
exit('链接数据库失败:' . $e->getMessage());
|
||||
}
|
||||
$this->db->exec("set sql_mode = ''");
|
||||
$this->db->exec("set names utf8");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置结果集方式
|
||||
*
|
||||
* @param string $_style
|
||||
*/
|
||||
public function setFetchStyle($_style)
|
||||
{
|
||||
$this->fetchStyle = $_style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换数据表前缀
|
||||
* @param $_sql
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function dealPrefix($_sql){
|
||||
return str_replace($this->sqlPrefix,$this->prefix,$_sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取PDOStatement
|
||||
* @param string $_sql
|
||||
* @param array $_array
|
||||
*
|
||||
* @return \PDOStatement
|
||||
*/
|
||||
public function query($_sql, $_array = null)
|
||||
{
|
||||
$_sql = $this->dealPrefix($_sql);
|
||||
if (is_array($_array)) {
|
||||
$stmt = $this->db->prepare($_sql);
|
||||
if($stmt) $stmt->execute($_array);
|
||||
} else {
|
||||
$stmt = $this->db->query($_sql);
|
||||
}
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询一条结果
|
||||
*
|
||||
* @param string $_sql string
|
||||
* @param array $_array array
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRow($_sql, $_array = null)
|
||||
{
|
||||
$_sql = $this->dealPrefix($_sql);
|
||||
if (is_array($_array)) {
|
||||
$stmt = $this->db->prepare($_sql);
|
||||
if($stmt) $stmt->execute($_array);
|
||||
} else {
|
||||
$stmt = $this->db->query($_sql);
|
||||
}
|
||||
if($stmt) {
|
||||
return $stmt->fetch($this->fetchStyle);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有结果
|
||||
*
|
||||
* @param string $_sql
|
||||
* @param array $_array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll($_sql, $_array = null)
|
||||
{
|
||||
$_sql = $this->dealPrefix($_sql);
|
||||
if (is_array($_array)) {
|
||||
$stmt = $this->db->prepare($_sql);
|
||||
if($stmt) $stmt->execute($_array);
|
||||
} else {
|
||||
$stmt = $this->db->query($_sql);
|
||||
}
|
||||
if($stmt) {
|
||||
return $stmt->fetchAll($this->fetchStyle);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取结果数
|
||||
* @param string $_sql
|
||||
* @param array $_array
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCount($_sql, $_array = null)
|
||||
{
|
||||
$_sql = $this->dealPrefix($_sql);
|
||||
$stmt = $this->db->prepare($_sql);
|
||||
if($stmt) {
|
||||
$stmt->execute($_array);
|
||||
return $stmt->rowCount();
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个字段值
|
||||
* @param string $_sql
|
||||
* @param array $_array
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getColumn($_sql, $_array = null)
|
||||
{
|
||||
$_sql = $this->dealPrefix($_sql);
|
||||
if (is_array($_array)) {
|
||||
$stmt = $this->db->prepare($_sql);
|
||||
if($stmt) $stmt->execute($_array);
|
||||
} else {
|
||||
$stmt = $this->db->query($_sql);
|
||||
}
|
||||
if($stmt) {
|
||||
return $stmt->fetchColumn();
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行语句
|
||||
* @param string $_sql
|
||||
* @param array $_array
|
||||
*
|
||||
* @return int|\PDOStatement
|
||||
*/
|
||||
public function exec($_sql, $_array = null)
|
||||
{
|
||||
$_sql = $this->dealPrefix($_sql);
|
||||
if (is_array($_array)) {
|
||||
$stmt = $this->db->prepare($_sql);
|
||||
if($stmt) {
|
||||
return $stmt->execute($_array);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return $this->db->exec($_sql);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回最后插入行的ID
|
||||
*
|
||||
* @return int|\PDOStatement
|
||||
*/
|
||||
public function lastInsertId()
|
||||
{
|
||||
return $this->db->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回错误信息
|
||||
*
|
||||
* @return string|\PDOStatement
|
||||
*/
|
||||
public function error()
|
||||
{
|
||||
$error = $this->db->errorInfo();
|
||||
return '['.$error[1].']'.$error[2];
|
||||
}
|
||||
|
||||
function __get($name)
|
||||
{
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
$this->db = null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user