D:\wwwroot\eme-cn.com\framework\base\db\MysqlPdoDriver.php
{
$dbArr = [];
if (false == $isMaster && !empty($this->config['DB_SLAVE'])) {
$master = $this->config;
unset($master['DB_SLAVE']);
foreach ($this->config['DB_SLAVE'] as $k=>$v) {
$dbArr[] = array_merge($master, $this->config['DB_SLAVE'][$k]);
}
shuffle($dbArr);
} else {
$dbArr[] = $this->config;
}
$pdo = null;
$error = '';
foreach ($dbArr as $db) {
$dsn = "mysql:host={$db['DB_HOST']};port={$db['DB_PORT']};dbname={$db['DB_NAME']};charset={$db['DB_CHARSET']}";
try {
$pdo = new \PDO($dsn, $db['DB_USER'], $db['DB_PWD']);
break;
} catch (PDOException $e) {
$error = $e->getMessage();
}
}
if (!$pdo) {
throw new \Exception('connect database error :'.$error, 500);
}
$pdo->exec("set names {$db['DB_CHARSET']}");
return $pdo;
}
protected function _getReadLink()
{
if (!isset($this->readLink)) {
try {
$this->readLink = $this->_connect(false);
} catch (\Exception $e) {
D:\wwwroot\eme-cn.com\framework\base\db\MysqlPdoDriver.php
{
$dbArr = [];
if (false == $isMaster && !empty($this->config['DB_SLAVE'])) {
$master = $this->config;
unset($master['DB_SLAVE']);
foreach ($this->config['DB_SLAVE'] as $k=>$v) {
$dbArr[] = array_merge($master, $this->config['DB_SLAVE'][$k]);
}
shuffle($dbArr);
} else {
$dbArr[] = $this->config;
}
$pdo = null;
$error = '';
foreach ($dbArr as $db) {
$dsn = "mysql:host={$db['DB_HOST']};port={$db['DB_PORT']};dbname={$db['DB_NAME']};charset={$db['DB_CHARSET']}";
try {
$pdo = new \PDO($dsn, $db['DB_USER'], $db['DB_PWD']);
break;
} catch (PDOException $e) {
$error = $e->getMessage();
}
}
if (!$pdo) {
throw new \Exception('connect database error :'.$error, 500);
}
$pdo->exec("set names {$db['DB_CHARSET']}");
return $pdo;
}
protected function _getReadLink()
{
if (!isset($this->readLink)) {
try {
$this->readLink = $this->_connect(false);
} catch (\Exception $e) {
D:\wwwroot\eme-cn.com\framework\base\db\MysqlPdoDriver.php
return $pdo;
}
protected function _getReadLink()
{
if (!isset($this->readLink)) {
try {
$this->readLink = $this->_connect(false);
} catch (\Exception $e) {
$this->readLink = $this->_getWriteLink();
}
}
return $this->readLink;
}
protected function _getWriteLink()
{
if (!isset($this->writeLink)) {
$this->writeLink = $this->_connect(true);
}
return $this->writeLink;
}
public function __destruct()
{
if ($this->writeLink) {
$this->writeLink = null;
}
if ($this->readLink) {
$this->readLink = null;
}
}
}
D:\wwwroot\eme-cn.com\framework\base\db\MysqlPdoDriver.php
} catch (PDOException $e) {
$error = $e->getMessage();
}
}
if (!$pdo) {
throw new \Exception('connect database error :'.$error, 500);
}
$pdo->exec("set names {$db['DB_CHARSET']}");
return $pdo;
}
protected function _getReadLink()
{
if (!isset($this->readLink)) {
try {
$this->readLink = $this->_connect(false);
} catch (\Exception $e) {
$this->readLink = $this->_getWriteLink();
}
}
return $this->readLink;
}
protected function _getWriteLink()
{
if (!isset($this->writeLink)) {
$this->writeLink = $this->_connect(true);
}
return $this->writeLink;
}
public function __destruct()
{
if ($this->writeLink) {
$this->writeLink = null;
}
D:\wwwroot\eme-cn.com\framework\base\db\MysqlPdoDriver.php
public function __construct($config = [])
{
$this->config = $config;
}
public function select($table, array $condition = [], $field = '*', $order = null, $limit = null)
{
$field = !empty($field) ? $field : '*';
$order = !empty($order) ? ' ORDER BY '.$order : '';
$limit = !empty($limit) ? ' LIMIT '.$limit : '';
$table = $this->_table($table);
$condition = $this->_where($condition);
return $this->query("SELECT {$field} FROM {$table} {$condition['_where']} $order $limit", $condition['_bindParams']);
}
public function query($sql, array $params = [])
{
$sth = $this->_bindParams($sql, $params, $this->_getReadLink());
Hook::listen('dbQueryBegin', [$sql, $params]);
if ($sth->execute()) {
$data = $sth->fetchAll(\PDO::FETCH_ASSOC);
Hook::listen('dbQueryEnd', [$this->getSql(), $data]);
return $data;
}
$err = $sth->errorInfo();
Hook::listen('dbException', [$this->getSql(), $err[2]]);
throw new \Exception('Database SQL: "'.$this->getSql().'". ErrorInfo: '.$err[2], 500);
}
public function execute($sql, array $params = [])
{
$sth = $this->_bindParams($sql, $params, $this->_getWriteLink());
Hook::listen('dbExecuteBegin', [$sql, $params]);
if ($sth->execute()) {
$affectedRows = $sth->rowCount();
D:\wwwroot\eme-cn.com\framework\base\db\MysqlPdoDriver.php
{
protected $config = [];
protected $writeLink = null;
protected $readLink = null;
protected $sqlMeta = ['sql'=>'', 'params'=>[], 'link'=>null];
public function __construct($config = [])
{
$this->config = $config;
}
public function select($table, array $condition = [], $field = '*', $order = null, $limit = null)
{
$field = !empty($field) ? $field : '*';
$order = !empty($order) ? ' ORDER BY '.$order : '';
$limit = !empty($limit) ? ' LIMIT '.$limit : '';
$table = $this->_table($table);
$condition = $this->_where($condition);
return $this->query("SELECT {$field} FROM {$table} {$condition['_where']} $order $limit", $condition['_bindParams']);
}
public function query($sql, array $params = [])
{
$sth = $this->_bindParams($sql, $params, $this->_getReadLink());
Hook::listen('dbQueryBegin', [$sql, $params]);
if ($sth->execute()) {
$data = $sth->fetchAll(\PDO::FETCH_ASSOC);
Hook::listen('dbQueryEnd', [$this->getSql(), $data]);
return $data;
}
$err = $sth->errorInfo();
Hook::listen('dbException', [$this->getSql(), $err[2]]);
throw new \Exception('Database SQL: "'.$this->getSql().'". ErrorInfo: '.$err[2], 500);
}
public function execute($sql, array $params = [])
D:\wwwroot\eme-cn.com\framework\base\Model.php
$limit = $this->options['limit'];
$this->options['limit'] = '';
$table = $this->_getTable();
$where = $this->_getWhere();
//Pagination
if (!empty($this->options['pager'])) {
$count = $this->getDb()->Count($table, $where);
$this->_pager(
$this->options['pager']['page'],
$this->options['pager']['pageSize'],
$this->options['pager']['scope'] = 10,
$count
);
$this->options['pager'] = [];
$limit = $this->pager['offset'].','.$this->pager['limit'];
}
return $this->getDb()->select($table, $where, $field, $order, $limit);
}
public function insert()
{
if (empty($this->options['data']) || !is_array($this->options['data'])) {
return false;
}
return $this->getDb()->insert($this->_getTable(), $this->_getData());
}
public function update()
{
if (empty($this->options['where']) || !is_array($this->options['where'])) {
return false;
}
if (empty($this->options['data']) || !is_array($this->options['data'])) {
return false;
}
D:\wwwroot\eme-cn.com\app\admin\model\ConfigModel.php
<?php
namespace app\admin\model;
use app\base\model\BaseModel;
/**
* 网站配置.
*/
class ConfigModel extends BaseModel
{
/**
* 获取信息.
*
* @return array 网站配置
*/
public function getInfo()
{
$list = $this->select();
$config = [];
foreach ($list as $key => $value) {
$config[$value['name']] = $value['data'];
}
return $config;
}
/**
* 更新信息.
*
* @param int $siteId 站点配置ID
*
* @return bool 更新状态
*/
public function saveData()
{
$data = request('post.');
if (empty($data)) {
$this->error = '数据创建失败!';
D:\wwwroot\eme-cn.com\app\base\controller\BaseController.php
if (!config('APP_SYSTEM')) {
if (1 != config('APP_STATE') || 1 != config('APP_INSTALL')) {
$this->error('该应用尚未开启!', false);
}
}
$this->setCont();
//执行初始化
if (method_exists($this, 'init')) {
$this->init();
}
}
/**
* 设置站点基本信息.
*/
protected function setCont()
{
// 读取站点配置
$siteConfig = target('admin/Config')->getInfo();
// 多语言
if (defined('LANG_OPEN')) {
// 赋值到模板
$lang_list = unserialize(LANG_CONFIG)['LANG_LIST'];
foreach ($lang_list as $key=>$item) {
if (config('REWRITE_ON')) {
$url = '/'.$key;
} else {
$url = '?'.http_build_query([
'lang' => $key,
]);
}
// 是否已选择(用于高亮判断)
$lang_list[$key]['changed'] = ($key == APP_LANG) ? true : false;
$lang_list[$key]['url'] = $url;
}
D:\wwwroot\eme-cn.com\app\base\controller\BaseController.php
$lock = ROOT_PATH.'install.lock';
if (!is_file($lock)) {
$this->redirect(url('install/Index/index'));
}
//引入扩展函数
require_once APP_PATH.'base/util/Function.php';
//引入当前模块配置
$config = load_config('config');
if (!empty($config)) {
foreach ((array) $config as $key => $value) {
config($key, $value);
}
}
//判断模块是否开启
if (!config('APP_SYSTEM')) {
if (1 != config('APP_STATE') || 1 != config('APP_INSTALL')) {
$this->error('该应用尚未开启!', false);
}
}
$this->setCont();
//执行初始化
if (method_exists($this, 'init')) {
$this->init();
}
}
/**
* 设置站点基本信息.
*/
protected function setCont()
{
// 读取站点配置
$siteConfig = target('admin/Config')->getInfo();
// 多语言
if (defined('LANG_OPEN')) {
// 赋值到模板
$lang_list = unserialize(LANG_CONFIG)['LANG_LIST'];
D:\wwwroot\eme-cn.com\app\home\controller\SiteController.php
<?php
namespace app\home\controller;
use app\base\controller\BaseController;
/**
* 前台公共类.
*/
class SiteController extends BaseController
{
public function __construct()
{
parent::__construct();
//设置手机版参数
if (isset($_GET['mobile']) || MOBILE) {
config('tpl_name', config('mobile_tpl'));
}
//设置常量
define('TPL_NAME', config('tpl_name'));
//访问统计
target('duxcms/TotalVisitor')->addData();
target('duxcms/TotalSpider')->addData();
}
/**
* 前台模板显示 调用内置的模板引擎.
*
* @param string $name 模板名
* @param bool $type 模板输出
*
* @return void
*/
protected function siteDisplay($name = '', $type = true)
{
// 多语言
if (defined('LANG_OPEN')) {
$tpl = THEME_NAME.'/'.TPL_NAME.'/'.APP_LANG.'/'.$name;
D:\wwwroot\eme-cn.com\framework\base\App.php
Hook::listen('routeParseUrl', [Config::get('REWRITE_RULE'), Config::get('REWRITE_ON')]);
//default route
if (!defined('APP_NAME') || !defined('CONTROLLER_NAME') || !defined('ACTION_NAME')) {
Route::parseUrl(Config::get('REWRITE_RULE'), Config::get('REWRITE_ON'));
}
if (defined('API_STATUS')) {
$controller = '\\app\\api\\controller\\IndexController';
$action = 'index';
} else {
$controller = '\app\\'.APP_NAME.'\controller\\'.CONTROLLER_NAME.'Controller';
$action = ACTION_NAME;
}
//execute action
if (!class_exists($controller)) {
throw new \Exception("Controller '{$controller}' not found", 404);
}
$obj = new $controller();
if (!method_exists($obj, $action)) {
throw new \Exception("Action '{$controller}::{$action}()' not found", 404);
}
Hook::listen('actionBefore', [$obj, $action]);
$obj->$action();
Hook::listen('actionAfter', [$obj, $action]);
} catch (\Exception $e) {
// //关闭调试或者是线上版本,不显示详细错误
// if (false==config('DEBUG') || 'production' == config('ENV')) {
// $home = new \app\home\controller\IndexController();
// $home->error404();
// } else {
// throw $e;
// }
Hook::listen('appError', [$e]);
}
Hook::listen('appEnd');
}
D:\wwwroot\eme-cn.com\framework\core.php
foreach (glob($fileDIR.'*.php') as $file) {
$fileList[$fileDIR][] = $file;
}
}
$fileBase = $baseDir.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.$className;
foreach ($fileList[$fileDIR] as $file) {
if (false !== stripos($file, $fileBase)) {
require $file;
return true;
}
}
}
}
return false;
});
App::run();
D:\wwwroot\eme-cn.com\index.php
<?php
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
header('Content-Type: text/html; charset=UTF-8');
echo 'PHP环境不能低于5.6';
exit;
}
require 'framework/core.php';