This commit is contained in:
2020-03-25 17:08:02 +08:00
parent 6b9202d341
commit 0d2ef31222
35 changed files with 1212 additions and 130 deletions

275
extend/com/Datatable.php Normal file
View File

@@ -0,0 +1,275 @@
<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: colin <colin@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace com;
use think\facade\Config;
use think\facade\Db;
/**
* 数据库管理类
* @author colin <colin@tensent.cn>
*/
class Datatable {
protected $table; /*数据库操作的表*/
protected $fields = array(); /*数据库操作字段*/
protected $charset = 'utf8'; /*数据库操作字符集*/
public $prefix = ''; /*数据库操作表前缀*/
protected $model_table_prefix = ''; /*模型默认创建的表前缀*/
protected $engine_type = 'MyISAM'; /*数据库引擎*/
protected $key = 'id'; /*数据库主键*/
public $sql = ''; /*最后生成的sql语句*/
protected $typeAlist = array(
"text" => "VARCHAR",
"string" => "VARCHAR",
"password" => "VARCHAR",
"textarea" => "TEXT",
"bool" => "INT",
"select" => "INT",
"num" => "INT",
"decimal" => "DECIMAL",
"tags" => "VARCHAR",
"datetime" => "INT",
"date" => "INT",
"editor" => "TEXT",
"bind" => "INT",
"image" => "INT",
"images" => "VARCHAR",
"attach" => "VARCHAR",
);
/**
* 初始化数据库信息
* @author colin <colin@tensent.cn>
*/
public function __construct() {
//创建DB对象
$this->prefix = Config::get('database.prefix');
$this->model_table_prefix = Config::get('model_table_prefix');
}
/**
* @title 初始化表
* @description 初始化创建表
* @Author molong
* @DateTime 2017-06-11
* @param string $table 表名
* @return void 空
*/
public function initTable($table = '', $comment = '', $pk = 'id') {
$this->table = $this->getTablename($table, true);
$sql = $this->generateField($pk, 'int', 11, '', '主键', true);
$primary = $pk ? "PRIMARY KEY (`" . $pk . "`)" : '';
$generatesql = $sql . ',';
$create = "CREATE TABLE IF NOT EXISTS `" . $this->table . "`("
. $generatesql
. $primary
. ") ENGINE=" . $this->engine_type . " AUTO_INCREMENT=1 DEFAULT CHARSET=" . $this->charset . " ROW_FORMAT=DYNAMIC COMMENT='" . $comment . "';";
$this->sql = $create;
return $this;
}
/**
* 快速创建ID字段
* @var length 字段的长度
* @var comment 字段的描述
* @author colin <colin@tensent.cn>
*/
public function generateField($key = '', $type = '', $length = 11, $default = '', $comment = '主键', $is_auto_increment = false) {
if ($key && $type) {
$auto_increment = $is_auto_increment ? 'AUTO_INCREMENT' : '';
$field_type = $length ? $type . '(' . $length . ')' : $type;
$signed = in_array($type, array('int', 'float', 'double')) ? 'signed' : '';
$comment = $comment ? "COMMENT '" . $comment . "'" : "";
$default = $default ? "DEFAULT '" . $default . "'" : "";
$sql = "`{$key}` {$field_type} {$signed} NOT NULL {$default} $auto_increment {$comment}";
}
return $sql;
}
/**
* 追加字段
* @var $table 追加字段的表名
* @var $attr 属性列表
* @var $is_more 是否为多条同时插入
* @author colin <colin@tensent.cn>
*/
public function columField($table, $attr = array()) {
$field_attr['table'] = $table ? $this->getTablename($table, true) : $this->table;
$field_attr['name'] = $attr['name'];
$field_attr['type'] = $attr['type'] ? $this->typeAlist[$attr['type']] : 'varchar';
if (intval($attr['length']) && $attr['length']) {
$field_attr['length'] = "(" . $attr['length'] . ")";
} else {
$field_attr['length'] = "";
}
$field_attr['is_null'] = $attr['is_must'] ? 'NOT NULL' : 'NULL';
$field_attr['default'] = $attr['value'] != '' ? 'DEFAULT "' . $attr['value'] . '"' : '';
$field_attr['comment'] = (isset($attr['remark']) && $attr['remark']) ? $attr['remark'] : $attr['title'];
$field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `' . $attr['after'] . '`' : ' AFTER `id`';
$field_attr['action'] = (isset($attr['action']) && $attr['action']) ? $attr['action'] : 'ADD';
//确认表是否存在
if ($field_attr['action'] == 'ADD') {
$this->sql = "ALTER TABLE `{$field_attr['table']}` ADD `{$field_attr['name']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}' {$field_attr['after']}";
} elseif ($field_attr['action'] == 'CHANGE') {
$field_attr['oldname'] = (isset($attr['oldname']) && $attr['oldname']) ? $attr['oldname'] : '';
$this->sql = "ALTER TABLE `{$field_attr['table']}` CHANGE `{$field_attr['oldname']}` `{$field_attr['name']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}'";
}
return $this;
}
/**
* 删除字段
* @var $table 追加字段的表名
* @var $field 字段名
* @author colin <colin@tensent.cn>
*/
public function delField($table, $field) {
$table = $table ? $this->getTablename($table, true) : $this->table;
$this->sql = "ALTER TABLE `$table` DROP `$field`";
return $this;
}
/**
* 删除数据表
* @var $table 追加字段的表名
* @author colin <colin@tensent.cn>
*/
public function delTable($table) {
$table = $table ? $this->getTablename($table, true) : $this->table;
$this->sql = "DROP TABLE `$table`";
return $this;
}
/**
* 结束表
* @var $engine_type 数据库引擎
* @var $comment 表注释
* @var $charset 数据库编码
* @author colin <colin@tensent.cn>
*/
public function endTable($comment, $engine_type = null, $charset = null) {
if (null != $charset) {
$this->charset = $charset;
}
if (null != $engine_type) {
$this->engine_type = $engine_type;
}
$end = "ENGINE=" . $this->engine_type . " AUTO_INCREMENT=1 DEFAULT CHARSET=" . $this->charset . " ROW_FORMAT=DYNAMIC COMMENT='" . $comment . "';";
$this->sql .= ")" . $end;
return $this;
}
/**
* 创建动作
* @return int 0
* @author colin <colin@tensent.cn>
*/
public function create() {
$res = Db::execute($this->sql);
return $res !== false;
}
/**
* create的别名
* @return int 0
* @author colin <colin@tensent.cn>
*/
public function query() {
return $this->create();
}
/**
* 获取最后生成的sql语句
* @author colin <colin@tensent.cn>
*/
public function getLastSql() {
return $this->sql;
}
/**
* 获取指定的表名
* @var $table 要获取名字的表名
* @var $prefix 获取表前缀? 默认为不获取 false
* @author colin <colin@tensent.cn>
*/
public function getTablename($table, $prefix = false) {
if (false == $prefix) {
$this->table = $this->model_table_prefix . $table;
} else {
$this->table = $this->prefix . $this->model_table_prefix . $table;
}
return $this->table;
}
/**
* 获取指定表名的所有字段及详细信息
* @var $table 要获取名字的表名 可以为sent_tengsu_photo、tengsu_photo、photo
* @author colin <colin@tensent.cn>
*/
public function getFields($table) {
if (false == $table) {
$table = $this->table; //为空调用当前table
} else {
$table = $table;
}
$patten = "/\./";
if (!preg_match_all($patten, $table)) {
//匹配_
$patten = "/_+/";
if (!preg_match_all($patten, $table)) {
$table = $this->prefix . $this->model_table_prefix . $table;
} else {
//匹配是否包含表前缀,如果是 那么就是手动输入
$patten = "/$this->prefix/";
if (!preg_match_all($patten, $table)) {
$table = $this->prefix . $table;
}
}
}
$sql = "SHOW FULL FIELDS FROM $table";
return Db::query($sql);
}
/**
* 确认表是否存在
* @var $table 表名 可以为sent_tengsu_photo、tengsu_photo、photo
* @author colin <colin@tensent.cn>
* @return boolen
*/
public function CheckTable($table) {
//获取表名
$this->table = $this->getTablename($table, true);
$result = Db::execute("SHOW TABLES LIKE '%$this->table%'");
return $result;
}
/**
* 确认字段是否存在
* @var $table 表名 可以为sent_tengsu_photo、tengsu_photo、photo
* @var $field 字段名 要检查的字段名
* @author colin <colin@tensent.cn>
* @return boolen
*/
public function CheckField($table, $field) {
//检查字段是否存在
$table = $this->getTablename($table, true);
if (!Db::query("Describe $table $field")) {
return false;
} else {
return true;
}
}
}

354
extend/com/Ueditor.php Normal file
View File

@@ -0,0 +1,354 @@
<?php
/**
* Ueditor插件
* @author Nintendov
*/
namespace com;
class Ueditor {
public $uid; //要操作的用户id 如有登录需要则去掉注释
private $output; //要输出的数据
private $st;
private $rootpath = '/uploads';
private $config = '';
public function __construct($uid = '') {
//uid 为空则导入当前会话uid
if ('' === $uid) {
$this->uid = session('user_auth.uid');
}
//导入设置
$this->config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(app()->getAppPath() . "ueditor.json")), true);
$action = htmlspecialchars($_GET['action']);
switch ($action) {
case 'config':
$result = json_encode($this->config);
break;
case 'uploadimage':
$config = array(
"pathFormat" => $this->config['imagePathFormat'],
"maxSize" => $this->config['imageMaxSize'],
"allowFiles" => $this->config['imageAllowFiles'],
);
$fieldName = $this->config['imageFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'uploadscrawl':
$config = array(
"pathFormat" => $this->config['scrawlPathFormat'],
"maxSize" => $this->config['scrawlMaxSize'],
"allowFiles" => $this->config['scrawlAllowFiles'],
"oriName" => "scrawl.png",
);
$fieldName = $this->config['scrawlFieldName'];
$result = $this->uploadBase64($config, $fieldName);
break;
case 'uploadvideo':
$config = array(
"pathFormat" => $this->config['videoPathFormat'],
"maxSize" => $this->config['videoMaxSize'],
"allowFiles" => $this->config['videoAllowFiles'],
);
$fieldName = $this->config['videoFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'uploadfile':
// default:
$config = array(
"pathFormat" => $this->config['filePathFormat'],
"maxSize" => $this->config['fileMaxSize'],
"allowFiles" => $this->config['fileAllowFiles'],
);
$fieldName = $this->config['fileFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'listfile':
$config = array(
'allowFiles' => $this->config['fileManagerAllowFiles'],
'listSize' => $this->config['fileManagerListSize'],
'path' => $this->config['fileManagerListPath'],
);
$result = $this->listFile($config);
break;
case 'listimage':
$config = array(
'allowFiles' => $this->config['imageManagerAllowFiles'],
'listSize' => $this->config['imageManagerListSize'],
'path' => $this->config['imageManagerListPath'],
);
$result = $this->listFile($config);
break;
case 'catchimage':
$config = array(
"pathFormat" => $this->config['catcherPathFormat'],
"maxSize" => $this->config['catcherMaxSize'],
"allowFiles" => $this->config['catcherAllowFiles'],
"oriName" => "remote.png",
);
$fieldName = $this->config['catcherFieldName'];
$result = $this->saveRemote($config, $fieldName);
break;
default:
$result = json_encode(array(
'state' => 'wrong require',
));
break;
}
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
$this->output = htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
$this->output = json_encode(array(
'state' => 'callback参数不合法',
));
}
} else {
$this->output = $result;
}
}
/**
*
* 输出结果
* @param data 数组数据
* @return 组合后json格式的结果
*/
public function output() {
return $this->output;
}
/**
* 上传文件方法
*
*/
private function uploadFile($config, $fieldName) {
$file = request()->file('upfile');
if (is_array($config['allowFiles'])) {
$config['allowFiles'] = str_replace('.', '', implode(',', $config['allowFiles']));
}else{
$config['allowFiles'] = '';
}
$info = $file->validate(array('size'=>$config['maxSize'], 'ext'=>$config['allowFiles']))->move($config['pathFormat'], true, false);
if ($info) {
$data = array(
'state' => "SUCCESS",
'url' => str_replace("\\", '/', substr($info->getPathname(), 1)),
'title' => $info->getSaveName(),
'original' => $info->getFilename(),
'type' => '.' . $info->getExtension(),
'size' => $info->getSize(),
);
} else {
$data = array(
"state" => $file->getError(),
);
}
return json_encode($data);
}
/**
*
* Enter description here ...
*/
private function uploadBase64($config, $fieldName) {
$data = array();
$base64Data = $_POST[$fieldName];
$img = base64_decode($base64Data);
$path = $this->getFullPath($config['pathFormat']);
if (strlen($img) > $config['maxSize']) {
$data['states'] = 'too large';
return json_encode($data);
}
$rootpath = $this->rootpath;
//替换随机字符串
$imgname = uniqid() . '.png';
$filename = $path . $imgname;
if (\Think\Storage::put($rootpath, $filename, $img)) {
$data = array(
'state' => 'SUCCESS',
'url' => $rootpath . $filename,
'title' => $imgname,
'original' => 'scrawl.png',
'type' => '.png',
'size' => strlen($img),
);
} else {
$data = array(
'state' => 'cant write',
);
}
return json_encode($data);
}
/**
* 列出文件夹下所有文件,如果是目录则向下
*/
private function listFile($config) {
$allowFiles = substr(str_replace(".", "|", join("", $config['allowFiles'])), 1);
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $config['listSize'];
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
$end = $start + $size;
$rootpath = $this->rootpath;
$path = $config['path'];
$files = \Think\Storage::listFile($rootpath, $path, $allowFiles);
//return $files;
if (!count($files)) {
return json_encode(array(
"state" => "no match file",
"list" => array(),
"start" => $start,
"total" => count($files),
));
}
/* 获取指定范围的列表 */
$len = count($files);
for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
$list[] = $files[$i];
}
//倒序
//for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
// $list[] = $files[$i];
//}
/* 返回数据 */
$result = json_encode(array(
"state" => "SUCCESS",
"list" => $list,
"start" => $start,
"total" => count($files),
));
return $result;
}
/**
*
* Enter description here ...
*/
private function saveRemote($config, $fieldName) {
$list = array();
if (isset($_POST[$fieldName])) {
$source = $_POST[$fieldName];
} else {
$source = $_GET[$fieldName];
}
foreach ($source as $imgUrl) {
$upload = new \org\Upload();
$imgUrl = htmlspecialchars($imgUrl);
$imgUrl = str_replace("&amp;", "&", $imgUrl);
//http开头验证
if (strpos($imgUrl, "http") !== 0) {
$data = array('state' => '不是http链接');
return json_encode($data);
}
//格式验证(扩展名验证和Content-Type验证)
$fileType = strtolower(strrchr($imgUrl, '.'));
if (!in_array($fileType, $config['allowFiles']) || stristr($heads['Content-Type'], "image")) {
$data = array("state" => "错误文件格式");
return json_encode($data);
}
//打开输出缓冲区并获取远程图片
ob_start();
$context = stream_context_create(
array('http' => array(
'follow_location' => false, // don't follow redirects
))
);
readfile($imgUrl, false, $context);
$img = ob_get_contents();
ob_end_clean();
preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
$path = $this->getFullPath($config['pathFormat']);
if (strlen($img) > $config['maxSize']) {
$data['states'] = 'too large';
return json_encode($data);
}
$rootpath = $this->rootpath;
$imgname = uniqid() . '.png';
$filename = $path . $imgname;
$oriName = $m ? $m[1] : "";
if (\Think\Storage::put($rootpath, $filename, $img)) {
array_push($list, array(
"state" => 'SUCCESS',
"url" => \Think\Storage::getPath($rootpath, $filename),
"size" => strlen($img),
"title" => $imgname,
"original" => $oriName,
"source" => htmlspecialchars($imgUrl),
));
} else {
array_push($list, array('state' => '文件写入失败'));
}
}
/* 返回抓取数据 */
return json_encode(array(
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list,
));
}
/**
* 规则替换命名文件
* @param $path
* @return string
*/
private function getFullPath($path) {
//替换日期事件
$t = time();
$d = explode('-', date("Y-y-m-d-H-i-s"));
$format = $path;
$format = str_replace("{yyyy}", $d[0], $format);
$format = str_replace("{yy}", $d[1], $format);
$format = str_replace("{mm}", $d[2], $format);
$format = str_replace("{dd}", $d[3], $format);
$format = str_replace("{hh}", $d[4], $format);
$format = str_replace("{ii}", $d[5], $format);
$format = str_replace("{ss}", $d[6], $format);
$format = str_replace("{uid}", $this->uid, $format);
return $format;
}
private function format_exts($exts) {
$data = array();
foreach ($exts as $key => $value) {
$data[] = ltrim($value, '.');
}
return $data;
}
}