public function uninstall() { $db_config = array(); $db_config['DB_TYPE'] = C('DB_TYPE'); $db_config['DB_HOST'] = C('DB_HOST'); $db_config['DB_NAME'] = C('DB_NAME'); $db_config['DB_USER'] = C('DB_USER'); $db_config['DB_PWD'] = C('DB_PWD'); $db_config['DB_PORT'] = C('DB_PORT'); $db_config['DB_PREFIX'] = C('DB_PREFIX'); $db = Db::getInstance($db_config); //读取插件sql文件 $sqldata = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . '/Addons/' . $this->info['name'] . '/uninstall.sql'); $sqlFormat = $this->sql_split($sqldata, $db_config['DB_PREFIX']); $counts = count($sqlFormat); for ($i = 0; $i < $counts; $i++) { $sql = trim($sqlFormat[$i]); $db->execute($sql); //执行语句 } //删除钩子 $Hooks = M("Hooks"); $map['name'] = array('in', 'DisplayFocus'); $res = $Hooks->where($map)->delete(); if ($res == false) { session('addons_install_error', $Hooks->getError()); return false; } return true; }
public function step3() { if (session('step') != 2) { $this->redirect('step2'); } $this->display(); //连接数据库 $dbconfig = session('db_config'); $db = Db::getInstance($dbconfig); //创建数据表 create_tables($db, $dbconfig['DB_PREFIX']); //注册创始人帐号 $auth = build_auth_key(); $admin = session('admin_info'); register_administrator($db, $dbconfig['DB_PREFIX'], $admin, $auth); //创建配置文件 $conf = write_config($dbconfig, $auth); session('config_file', $conf); if (session('error')) { //show_msg(); } else { session('step', 3); $this->redirect('Index/complete'); } }
/** * 优化表 */ public function optimize() { $tables = $_REQUEST['tables']; if ($tables) { $Db = Db::getInstance(); if (is_array($tables)) { $tables = implode('`,`', $tables); $list = $Db->query("OPTIMIZE TABLE `{$tables}`"); if ($list) { $this->ajaxReturn(1, "数据表优化完成!"); } else { $this->ajaxReturn(0, "数据表优化出错请重试!"); } } else { $list = $Db->query("OPTIMIZE TABLE `{$tables}`"); $tables_ts = substr($tables, 3); if ($list) { $this->ajaxReturn(1, "数据表'{$tables_ts}'优化完成!"); } else { $this->ajaxReturn(0, "数据表'{$tables_ts}'优化出错请重试!"); } } } else { $this->ajaxReturn(0, "请指定要优化的表!"); } }
public function index() { $Db = Db::getInstance(); $list = $Db->query('SHOW TABLE STATUS'); $list = array_map('array_change_key_case', $list); $this->assign('list', $list); $this->display(); }
/** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options = array()) { if (empty($options)) { $options = array('table' => C('DATA_CACHE_TABLE')); } $this->options = $options; $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length']) ? $options['length'] : 0; $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME'); $this->handler = \Think\Db::getInstance(); }
private function getSql($name) { $db_config = array(); $db_config['DB_TYPE'] = C('DB_TYPE'); $db_config['DB_HOST'] = C('DB_HOST'); $db_config['DB_NAME'] = C('DB_NAME'); $db_config['DB_USER'] = C('DB_USER'); $db_config['DB_PWD'] = C('DB_PWD'); $db_config['DB_PORT'] = C('DB_PORT'); $db_config['DB_PREFIX'] = C('DB_PREFIX'); $this->db = Db::getInstance($db_config); //读取插件sql文件 $sqldata = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . '/Addons/' . $this->info['name'] . '/' . $name . '.sql'); $sqlFormat = $this->sql_split($sqldata, $db_config['DB_PREFIX']); foreach ($sqlFormat as $key => $value) { # code... } return $sqlFormat; }
public function uninstall() { $db_config = array(); $db_config['DB_TYPE'] = C('DB_TYPE'); $db_config['DB_HOST'] = C('DB_HOST'); $db_config['DB_NAME'] = C('DB_NAME'); $db_config['DB_USER'] = C('DB_USER'); $db_config['DB_PWD'] = C('DB_PWD'); $db_config['DB_PORT'] = C('DB_PORT'); $db_config['DB_PREFIX'] = C('DB_PREFIX'); $db = Db::getInstance($db_config); //读取插件sql文件 $sqldata = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . '/Addons/' . $this->info['name'] . '/uninstall.sql'); $sqlFormat = $this->sql_split($sqldata, $db_config['DB_PREFIX']); $counts = count($sqlFormat); for ($i = 0; $i < $counts; $i++) { $sql = trim($sqlFormat[$i]); $db->execute($sql); //执行语句 } return true; }
/** * 修复表 * @param String $tables 表名 * @author 麦当苗儿 <*****@*****.**> */ public function repair($tables = null) { if ($tables) { $Db = Db::getInstance(); if (is_array($tables)) { $tables = implode('`,`', $tables); $list = $Db->query("REPAIR TABLE `{$tables}`"); if ($list) { $this->success("数据表修复完成!"); } else { $this->error("数据表修复出错请重试!"); } } else { $list = $Db->query("REPAIR TABLE `{$tables}`"); if ($list) { $this->mtReturn(201, "数据表'{$tables}'修复完成!", '', 'forward', U('index')); } else { $this->mtReturn(300, "数据表'{$tables}'修复出错请重试!"); } } } else { $this->error("请指定要修复的表!"); } }
public function step4() { if (session('step') !== '3') { $this->error('请按顺序安装', U('step3')); } session('step', '4'); session('error', false); $this->assign('meta_title', "step4"); $this->display(); //连接数据库 $db_config = session('db_config'); $db_instance = Db::getInstance($db_config); //创建数据表 create_tables($db_instance, $db_config['DB_PREFIX']); //生成加密字符串 $add_chars .= '`~!@#$%^&*()_+-=[]{};:"|,.<>/?'; $auth = String::randString(64, '', $add_chars); //生成随机数 //创建配置文件 $conf = write_config($db_config, $auth); //根据加密字符串更新admin密码的加密结果 $new_admin_password = user_md5('admin', $auth); $sql = <<<SQL UPDATE `{$db_config["DB_PREFIX"]}admin_config` SET `value`='{$auth}' WHERE `name` = 'AUTH_KEY'; UPDATE `{$db_config["DB_PREFIX"]}admin_user` SET `password`='{$new_admin_password}' WHERE `id` = 1; SQL; $result = $db_instance->execute($sql); if (!$result) { $this->error('写入系统加密KEY或管理员新密码出错!'); } if (session('error')) { $this->error('安装出错', 'index'); } else { $this->redirect('complete'); } }
/** * 修复表 * @param String $tables 表名 * @author 麦当苗儿 <*****@*****.**> */ public function repair($tables = null) { if ($tables) { $Db = Db::getInstance(); if (is_array($tables)) { $tables = implode('`,`', $tables); $list = $Db->query("REPAIR TABLE `{$tables}`"); if ($list) { $this->success(L('_REPAIR_COMPLETE_PARAM_', array('name' => '')) . L('_EXCLAMATION_')); } else { $this->error(L('_REPAIR_ERROR_PARAM_', array('name' => '')) . L('_EXCLAMATION_')); } } else { $list = $Db->query("REPAIR TABLE `{$tables}`"); if ($list) { $this->success(L('_REPAIR_COMPLETE_PARAM_', array('name' => $tables)) . L('_EXCLAMATION_')); } else { $this->error(L('_REPAIR_ERROR_PARAM_', array('name' => $tables)) . L('_EXCLAMATION_')); } } } else { $this->error(L('_REPAIR_ASSIGN_') . L('_EXCLAMATION_')); } }
public function clear() { $Db = Db::getInstance(); $tables = array("wst_users_member", "wst_users_member_apply", "wst_users_member_relation", "wst_users_member_voucher_earn"); if (IS_POST) { $rd = array('status' => -1); foreach ($tables as $k => $v) { $Db->query("TRUNCATE TABLE {$v}"); } $Db->query("update wst_users_member set b_left_user_id=0, b_middle_user_id=0,b_right_user_id=0,parentId=0,recommendId=0,reaches=0,level=0"); $rd = array('status' => 1); $this->ajaxReturn($rd); } else { $this->display("/clear"); } }
/** * 修复表 * @param String $tables 表名 * @author 麦当苗儿 <*****@*****.**> */ public function repair($tables = null) { if ($tables) { $Db = Db::getInstance(); if (is_array($tables)) { $tables = implode('`,`', $tables); $list = $Db->query("REPAIR TABLE `{$tables}`"); if ($list) { $this->mtReturn(200, "数据表修复完成!", $_REQUEST['navTabId'], false); } else { $this->mtReturn(300, "数据表修复出错请重试!", $_REQUEST['navTabId'], false); } } else { $list = $Db->query("REPAIR TABLE `{$tables}`"); if ($list) { $this->mtReturn(200, "数据表'{$tables}'修复完成!", $_REQUEST['navTabId'], false); } else { $this->mtReturn(300, "数据表'{$tables}'修复出错请重试!", $_REQUEST['navTabId'], false); } } } else { $this->error("请指定要修复的表!"); } }
public function step3() { $data = session('install_config'); if (!$data) { $this->error('非法访问'); } $field = array('DB_TYPE', 'DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PWD', 'DB_PORT', 'DB_PREFIX'); $database = array(); foreach ($field as $key) { $database[$key] = $data[$key]; } $db = Db::getInstance($database); //sql字段替换 $sql = file_get_contents(MODULE_PATH . 'Data/sql.sql'); $sql = str_replace('[[DB_PREFIX]]', $data['DB_PREFIX'], $sql); //将sql文件解析成单条语句 $ret = sql_split($sql); //创建管理员账号 $passwordInfo = password($data['password']); $password = $passwordInfo['password']; $encrypt = $passwordInfo['encrypt']; $email = trim($data['email']); array_push($ret, "update {$data['DB_PREFIX']}admin set `username`='{$data['username']}',`password`='{$password}',`roleid`='1',`encrypt`='{$encrypt}',`email`='{$email}' where `userid`='1'"); $tip = array(); //执行情况统计 //安装进度显示 array_push($tip, array('开始安装数据库', '')); foreach ($ret as $value) { $value = trim($value); if (empty($value)) { continue; } if (substr($value, 0, 12) == 'CREATE TABLE') { $name = preg_replace("/^CREATE TABLE `(\\w+)`.*/is", "\\1", $value); $msg = "创建数据表{$name}"; if (false !== $db->execute($value)) { array_push($tip, array($msg, '成功')); } else { array_push($tip, array($msg, '失败')); } } elseif (substr($value, 0, 11) == 'INSERT INTO') { $name = preg_replace("/^INSERT INTO `(\\w+)`.*/is", "\\1", $value); $msg = "写入数据到{$name}"; if (false !== $db->execute($value)) { array_push($tip, array($msg, '成功')); } else { array_push($tip, array($msg, '失败')); } } else { $db->execute($value); } } //同步配置文件 if (APP_MODE != 'sae') { $configFile = CONF_PATH . 'config.php'; $data = file_get_contents($configFile); $data = preg_replace("/('DB_TYPE'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_TYPE']}',", $data); $data = preg_replace("/('DB_HOST'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_HOST']}',", $data); $data = preg_replace("/('DB_NAME'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_NAME']}',", $data); $data = preg_replace("/('DB_USER'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_USER']}',", $data); $data = preg_replace("/('DB_PWD'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_PWD']}',", $data); $data = preg_replace("/('DB_PORT'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_PORT']}',", $data); $data = preg_replace("/('DB_PREFIX'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_PREFIX']}',", $data); $data = preg_replace("/('report'\\s*=>\\s*)'(.*)',/Us", "\\1'{$email}',", $data); file_put_contents($configFile, $data); array_push($tip, array('写入配置文件', '成功')); } session('install_step', 4); array_push($tip, array('安装完成', '')); $this->assign('tip', $tip); $this->display(); }
/** * @param int $type 备份类型,1:table,2:row,3:all */ private function backup_table($table, $type = 1, $start = 0) { $db = Db::getInstance(); switch ($type) { case 1: if (0 == $start) { $result = $db->query("SHOW CREATE TABLE `{$table}`"); $sql = "\n"; $sql .= "-- -----------------------------\n"; $sql .= "-- 表结构 `{$table}`\n"; $sql .= "-- -----------------------------\n"; //$sql .= "DROP TABLE IF EXISTS `{$table}`;\n"; $sql .= str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', trim($result[0]['Create Table']) . ";\n\n"); if (false === $this->write($sql)) { return false; } } //数据总数 $result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`"); $count = $result['0']['count']; break; case 2: //写入数据注释 if (0 == $start) { $sql = "-- -----------------------------\n"; $sql .= "-- 表内记录 `{$table}`\n"; $sql .= "-- -----------------------------\n"; $this->write($sql); } //备份数据记录 $result = $db->query("SELECT * FROM `{$table}` LIMIT {$start}, 1000"); foreach ($result as $row) { $row = array_map('addslashes', $row); $sql = "INSERT INTO `{$table}` VALUES ('" . str_replace(array("\r", "\n"), array('\\r', '\\n'), implode("', '", $row)) . "');\n"; if (false === $this->write($sql)) { return false; } } break; case 3: if (0 == $start) { $result = $db->query("SHOW CREATE TABLE `{$table}`"); $sql = "\n"; $sql .= "-- -----------------------------\n"; $sql .= "-- Table structure for `{$table}`\n"; $sql .= "-- -----------------------------\n"; $sql .= "DROP TABLE IF EXISTS `{$table}`;\n"; $sql .= trim($result[0]['Create Table']) . ";\n\n"; if (false === $this->write($sql)) { return false; } } //数据总数 $result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`"); $count = $result['0']['count']; //备份表数据 if ($count) { //写入数据注释 if (0 == $start) { $sql = "-- -----------------------------\n"; $sql .= "-- Records of `{$table}`\n"; $sql .= "-- -----------------------------\n"; $this->write($sql); } //备份数据记录 $result = $db->query("SELECT * FROM `{$table}` LIMIT {$start}, 1000"); foreach ($result as $row) { $row = array_map('addslashes', $row); $sql = "INSERT INTO `{$table}` VALUES ('" . str_replace(array("\r", "\n"), array('\\r', '\\n'), implode("', '", $row)) . "');\n"; if (false === $this->write($sql)) { return false; } } //还有更多数据 if ($count > $start + 1000) { return array($start + 1000, $count); } } break; } }
public function step3() { /* if(session('step') != 2){ $this->redirect('step2'); }*/ $this->display(); //连接数据库 $dbconfig = cookie('db_config'); $db = Db::getInstance($dbconfig); //创建数据表 create_tables($db, $dbconfig['DB_PREFIX']); //注册创始人帐号 $auth = build_auth_key(); $admin = session('admin_info'); register_administrator($db, $dbconfig['DB_PREFIX'], $admin, $auth); //创建配置文件 $conf = write_config($dbconfig, $auth); session('config_file', $conf); if (session('error')) { //show_msg(); } else { session('step', 3); echo "<script type=\"text/javascript\">setTimeout(function(){location.href='" . U('Index/complete') . "'},5000)</script>"; ob_flush(); flush(); //$this->redirect('Index/complete'); } }
public function step4() { $this->assign('meta_title', "step4"); $this->display(); //连接数据库 $db_config = session('db_config'); $db_instance = Db::getInstance($db_config); //创建数据表 create_tables($db_instance, $db_config['DB_PREFIX']); //生成加密字符串 $add_chars .= '`~!@#$%^&*()_+-=[]{};:"|,.<>/?'; $auth = \Org\Util\String::randString(64, '', $add_chars); //生成随机数 //创建配置文件 $conf = write_config($db_config, $auth); //根据加密字符串更新admin密码的加密结果 $sql = 'UPDATE `' . $db_config["DB_PREFIX"] . 'user` SET `password`="' . user_md5('admin', $auth) . '" WHERE `id` = 1'; $result = $db_instance->execute($sql); if (!$result) { $this->error('写入加密后密码出错!'); } if (session('error')) { $this->error('安装出错', 'step1'); } else { session('step', 4); $this->redirect('complete'); } }
/** * 构造 */ public function __construct() { parent::__construct(); $this->db = \Think\Db::getInstance(C('OAUTH2_DB_DSN')); $this->table = array('auth_codes' => C('OAUTH2_CODES_TABLE'), 'clients' => C('OAUTH2_CLIENTS_TABLE'), 'tokens' => C('OAUTH2_TOKEN_TABLE')); }
public function addDB() { if (!IS_POST) { $id = trim($_GET['id']); $red = D('User')->field("redirect")->where("id = {$id}")->find(); //var_dump($red); if ($red['redirect'] == "Salesman" || $red['redirect'] == "") { $this->error("没有权限创建数据库"); } $dbName = "db_{$id}"; $this->assign("id", $id); $this->assign("dbname", $dbName); $this->display(); } else { /* * 获取数据 */ $user = D("User"); $where['id'] = trim($_POST['id']); $data['dbname'] = trim($_POST['dbname']); /* if ($res) { $this->success("添加用户成功 !!", U('User/index')); } else { $this->error("添加用户失败 ??"); } */ /* * 创建数据库 */ // exit(); $DB = array(); $DB['DB_TYPE'] = C('DB_TYPE'); $DB['DB_PORT'] = C('DB_PORT'); $DB['DB_HOST'] = C('DB_HOST'); $DB['DB_NAME'] = trim($_POST['dbname']); $DB['DB_USER'] = C('DB_USER'); $DB['DB_PWD'] = C('DB_PWD'); $DB['DB_PREFIX'] = C('DB_PREFIX'); $prefix = ""; // $dsn = "$dbType://$dbUser:$dbPwd@$host:$dbPort/$dbName"; $dbname = $DB['DB_NAME']; unset($DB['DB_NAME']); $db = Db::getInstance($DB); //var_dump($DB); $sqld = "CREATE DATABASE IF NOT EXISTS `" . $dbname . "` DEFAULT CHARACTER SET utf8"; //开始安装 // $this->show("<br>... 开始安装数据库: ( " . $dbname . " ) ...<br>"); $result = $db->execute($sqld); if ($result) { // $this->show("<br>... " . $dbname . " ...安装成功<br>"); $DB['DB_NAME'] = trim($_POST['dbname']); $db = Db::getInstance($DB); //print_r($db); $this->create_tables($db, $prefix); $res = $user->where($where)->relation(true)->save($data); } else { // $this->show("创建表失败"); } } }
/** * +---------------------------------------------------------- * 取得当前认证号的所有权限列表 * +---------------------------------------------------------- * @param integer $authId 用户ID * +---------------------------------------------------------- * @access public * +---------------------------------------------------------- */ public static function getAccessList($authId) { // Db方式权限数据 $db = Db::getInstance(C('RBAC_DB_DSN')); $table = array('role' => C('RBAC_ROLE_TABLE'), 'user' => C('RBAC_USER_TABLE'), 'access' => C('RBAC_ACCESS_TABLE'), 'node' => C('RBAC_NODE_TABLE')); $sql = "select node.id,node.name from " . $table['role'] . " as role," . $table['user'] . " as user," . $table['access'] . " as access ," . $table['node'] . " as node " . "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=1 and node.status=1"; $apps = $db->query($sql); $access = array(); foreach ($apps as $key => $app) { $appId = $app['id']; $appName = $app['name']; // 读取项目的模块权限 $access[strtoupper($appName)] = array(); $sql = "select node.id,node.name from " . $table['role'] . " as role," . $table['user'] . " as user," . $table['access'] . " as access ," . $table['node'] . " as node " . "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=2 and node.pid={$appId} and node.status=1"; $modules = $db->query($sql); // 判断是否存在公共模块的权限 $publicAction = array(); foreach ($modules as $key => $module) { $moduleId = $module['id']; $moduleName = $module['name']; if ('PUBLIC' == strtoupper($moduleName)) { $sql = "select node.id,node.name from " . $table['role'] . " as role," . $table['user'] . " as user," . $table['access'] . " as access ," . $table['node'] . " as node " . "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=3 and node.pid={$moduleId} and node.status=1"; $rs = $db->query($sql); foreach ($rs as $a) { $publicAction[$a['name']] = $a['id']; } unset($modules[$key]); break; } } // 依次读取模块的操作权限 foreach ($modules as $key => $module) { $moduleId = $module['id']; $moduleName = $module['name']; $sql = "select node.id,node.name from " . $table['role'] . " as role," . $table['user'] . " as user," . $table['access'] . " as access ," . $table['node'] . " as node " . "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=3 and node.pid={$moduleId} and node.status=1"; $rs = $db->query($sql); $action = array(); foreach ($rs as $a) { $action[$a['name']] = $a['id']; } // 和公共模块的操作权限合并 $action += $publicAction; $access[strtoupper($appName)][strtoupper($moduleName)] = array_change_key_case($action, CASE_UPPER); } } return $access; }
public function testdbpwd() { if (IS_POST) { $dbconfig = I("POST."); $dbconfig['DB_TYPE'] = "mysql"; $db = Db::getInstance($dbconfig); try { $db->query("show databases;"); } catch (\Exception $e) { die(""); } exit("1"); } else { exit("need post!"); } }
/** * 初始化操作 * @access public * @return void */ public function _initialize() { parent::_initialize(); // 获取数据库对象实例 $this->db = \Think\Db::getInstance(); }
public function step4() { session('error', false); //连接数据库 $db_config = session('db_config'); $db_instance = Db::getInstance($db_config); $this->display(); //创建数据表 if (session('tiyang') == 1) { create_tables($db_instance, 'zml_', '1'); } else { create_tables($db_instance, 'zml_'); } if (session('error')) { $this->error('安装出错', 'index'); } else { session('step', 4); $this->redirect('complete'); } }
/** * 取得当前认证号的所有权限列表 * @param integer $authId 用户ID * @param string $map 条件 * @param array $table RBAC表 * @return array */ private function getAccessList($authId, $map = '', $tables = null) { if (is_null($tables)) { $prefix = $this->config['RBAC_DB_DSN'] == '' ? C('DB_PREFIX') : ''; $tables['role'] = $prefix . $this->config['RBAC_ROLE_TABLE']; $tables['user'] = $prefix . $this->config['RBAC_USER_TABLE']; $tables['access'] = $prefix . $this->config['RBAC_ACCESS_TABLE']; $tables['node'] = $prefix . $this->config['RBAC_NODE_TABLE']; } // Db方式权限数据 $db = Db::getInstance($this->config['RBAC_DB_DSN']); $sql = "select node.id,node.name,node.pid,node.level,node.expression from " . $tables['node'] . " as node join " . $tables['access'] . " as access on node.id=access.node_id join " . $tables['user'] . " as user on access.role_id=user.role_id join " . $tables['role'] . " as role on user.role_id=role.id " . "where user.user_id='{$authId}' and role.status=1 and node.status=1" . $map; $list = $db->query($sql); $result = $access = array(); if ($list) { foreach ($list as $v) { if ($v['level'] == 0) { $result[strtoupper($v['name'])] = $v['expression']; } else { unset($v['expression']); $access[$v['level']][$v['id']] = $v; } } foreach ($access[3] as $k => $v) { if (isset($access[2][$v['pid']])) { $pid = $access[2][$v['pid']]['pid']; if (isset($access[1][$pid])) { $array = array(); $array[] = $access[1][$pid]['name']; $array[] = $access[2][$v['pid']]['name']; $array[] = $v['name']; $array = array_map('strtoupper', $array); $result[$array[0]][$array[1]][$array[2]] = $k; } } } } return $result; }
/** * 修复表 * * @param String $tables * 表名 * @author 麦当苗儿 <*****@*****.**> */ public function repair() { $tables = I('post.ids'); $table = I('get.tables'); if ($tables != '') { $tables = str_replace(",", "`,`", $tables); $Db = Db::getInstance(); $list = $Db->query("REPAIR TABLE `{$tables}`"); if ($list) { $this->success("数据表修复完成!"); } else { $this->error("数据表修复出错请重试!"); } } elseif ($table != '') { $Db = Db::getInstance(); $list = $Db->query("REPAIR TABLE `{$table}`"); if ($list) { $this->success("数据表'{$table}'修复完成!"); } else { $this->error("数据表'{$table}'修复出错请重试!"); } } else { $this->error("请指定要修复的表!"); } }
public function import($start) { //还原数据 $db = Db::getInstance(); if ($this->config['compress']) { $gz = gzopen($this->file[1], 'r'); $size = 0; } else { $size = filesize($this->file[1]); $gz = fopen($this->file[1], 'r'); } $sql = ''; if ($start) { $this->config['compress'] ? gzseek($gz, $start) : fseek($gz, $start); } for ($i = 0; $i < 1000; $i++) { $sql .= $this->config['compress'] ? gzgets($gz) : fgets($gz); if (preg_match('/.*;$/', trim($sql))) { if (false !== $db->execute($sql)) { $start += strlen($sql); } else { return false; } $sql = ''; } elseif ($this->config['compress'] ? gzeof($gz) : feof($gz)) { return 0; } } return array($start, $size); }
public function add() { $model = new Model(); $sql = "use " . $_SESSION['dbName']; $model->query($sql); if (!IS_POST) { $this->display(); } else { $company = D("Shop"); /* * 获取信息 */ $data['shop'] = $_POST['shop']; // 唯一性,不可更改 $data['address'] = $_POST['address']; $data['people'] = $_POST['people']; $data['tel'] = $_POST['tel']; /* * 信息入库 */ $res = $company->add($data); /* if ($res) { $this->success("添加店铺成功 !!", U('Shop/index')); } else { $this->error("添加店铺失败 ??"); } /* * 查询 ID shop =》 ID */ $where['shop'] = $_POST['shop']; $id = $company->field("id")->where($where)->find(); $tb_id = $id['id']; // 店铺 ID 建表用 /* * 创建三表 tb_ID_sales tb_ID_storage tb_ID_goods */ $bossDB = $_SESSION['dbName']; $DB = array(); $DB['DB_TYPE'] = C('DB_TYPE'); $DB['DB_PORT'] = C('DB_PORT'); $DB['DB_HOST'] = C('DB_HOST'); $DB['DB_NAME'] = $bossDB; $DB['DB_USER'] = C('DB_USER'); $DB['DB_PWD'] = C('DB_PWD'); $DB['DB_PREFIX'] = C('DB_PREFIX'); $prefix = "tb_"; // $dsn = "$dbType://$dbUser:$dbPwd@$host:$dbPort/$dbName"; $dbname = $DB['DB_NAME']; //unset($DB['DB_NAME']); $db = Db::getInstance($DB); //var_dump($DB); $DB['DB_NAME'] = $bossDB; $db = Db::getInstance($DB); $this->create_tables($db, $prefix, $tb_id); if ($res) { //echo "刷新<script language=JavaScript> self.opener.location.reload();</script>"; $this->success("添加店铺成功 !!"); $this->success("<script language=JavaScript> parent.location.reload();</script>", U('Shop/index'), 2); } else { $this->error("添加店铺失败 ??"); } } }
private function query($sql) { $db = Db::getInstance(); return $db->query($sql); }
/** * 执行安装 */ public function finish_done() { $charset = C('DEFAULT_CHARSET'); header('Content-type:text/html;charset=' . $charset); $temp_info = F('temp_data'); $conn = mysql_connect($temp_info['db_host'] . ':' . $temp_info['db_port'], $temp_info['db_user'], $temp_info['db_pass']); $version = mysql_get_server_info(); $charset = str_replace('-', '', $charset); if ($version > '4.1') { if ($charset != 'latin1') { mysql_query("SET character_set_connection={$charset}, character_set_results={$charset}, character_set_client=binary", $conn); } if ($version > '5.0.1') { mysql_query("SET sql_mode=''", $conn); } } $selected_db = mysql_select_db($temp_info['db_name'], $conn); //开始创建数据表 $this->_show_process(L('create_table_begin')); $sqls = $this->_get_sql(APP_PATH . 'Install/sqldata/create_table.sql'); foreach ($sqls as $sql) { //替换前缀 $sql = str_replace('`cs_', '`' . $temp_info['db_prefix'], $sql); //获得表名 $run = mysql_query($sql, $conn); if (substr($sql, 0, 12) == 'CREATE TABLE') { $table_name = $temp_info['db_prefix'] . preg_replace("/CREATE TABLE IF NOT EXISTS `" . $temp_info['db_prefix'] . "([a-z0-9_]+)` .*/is", "\\1", $sql); $this->_show_process(sprintf(L('create_table_successed'), $table_name)); } } //开始导入数据 $this->_show_process(L('insert_initdate_begin')); $sqls = $this->_get_sql(APP_PATH . 'Install/sqldata/initdata.sql'); $weburl = $_SERVER["HTTP_HOST"]; foreach ($sqls as $sql) { //替换前缀 $sql = str_replace('`cs_', '`' . $temp_info['db_prefix'], $sql); $sql = str_replace('127.0.0.1', $weburl, $sql); $run = mysql_query($sql, $conn); //获得表名 if (substr($sql, 0, 11) == 'INSERT INTO') { $table_name = $temp_info['db_prefix'] . preg_replace("/INSERT INTO `" . $temp_info['db_prefix'] . "([a-z0-9_]+)` .*/is", "\\1", $sql); $this->_show_process(sprintf(L('insert_initdate_successed'), $table_name)); } } $sqls = $this->_get_sql(APP_PATH . 'Install/sqldata/area.sql'); foreach ($sqls as $sql) { //替换前缀 $sql = str_replace('`cs_', '`' . $temp_info['db_prefix'], $sql); $run = mysql_query($sql, $conn); //获得表名 if (substr($sql, 0, 11) == 'INSERT INTO') { $table_name = $temp_info['db_prefix'] . preg_replace("/INSERT INTO `" . $temp_info['db_prefix'] . "([a-z0-9_]+)` .*/is", "\\1", $sql); $this->_show_process(sprintf(L('insert_initdate_successed'), $table_name)); } } $this->_show_process('注册创始人帐号'); //注册创始人帐号 //修改配置文件 $auth = build_auth_key(); $config_data['DB_TYPE'] = $temp_info['db_type']; $config_data['DB_HOST'] = $temp_info['db_host']; $config_data['DB_NAME'] = $temp_info['db_name']; $config_data['DB_USER'] = $temp_info['db_user']; $config_data['DB_PWD'] = $temp_info['db_pass']; $config_data['DB_PORT'] = $temp_info['db_port']; $config_data['DB_PREFIX'] = $temp_info['db_prefix']; $db = Db::getInstance($config_data); $config_data['WEB_MD5'] = $auth; $conf = write_config($config_data); register_administrator($db, $temp_info['db_prefix'], $temp_info, $auth); $this->_show_process('注册创始人帐号成功'); //锁定安装程序 touch('./Data/install.lock'); //$password = hash ( 'md5', $temp_info['admin_pass'] ); //$sqls[] = "INSERT INTO `" . $temp_info['db_prefix'] . "user` VALUES " . // "('1', '" . $temp_info['admin_user'] . "', '管理员', '" . $password . "', '', '". NOW_TIME."', '".get_client_ip(1)."', 1, 8888, '" . $temp_info['admin_email'] . "', '', '". NOW_TIME."', '". NOW_TIME."', 1, 0, '', 0)"; //安装完毕 $this->_show_process(L('install_successed'), 'parent.install_successed();'); //创建配置文件 return false; }
public static function getModuleAccessList($authId, $module) { // Db方式 $db = Db::getInstance(C('RBAC_DB_DSN')); $table = array('role' => C('RBAC_ROLE_TABLE'), 'user' => C('RBAC_USER_TABLE'), 'access' => C('RBAC_ACCESS_TABLE')); $sql = "select access.node_id from " . $table['role'] . " as role," . $table['user'] . " as user," . $table['access'] . " as access " . "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.module='{$module}' and access.status=1"; $rs = $db->query($sql); $access = array(); foreach ($rs as $node) { $access[] = $node['node_id']; } return $access; }
public function step3() { if (session('step') != 2) { $this->redirect('step2'); } $this->display(); if (session('update')) { $db = Db::getInstance(); //更新数据表 update_tables($db, C('DB_PREFIX')); } else { //连接数据库 $dbconfig = session('db_config'); $db = Db::getInstance($dbconfig); //创建数据表 create_tables($db, $dbconfig['DB_PREFIX']); //注册创始人帐号 $admin = session('admin_info'); $admin['password'] = md5($admin['password']); register_administrator($db, $dbconfig['DB_PREFIX'], $admin, $auth); //创建配置文件 $conf = write_config($dbconfig, $auth); session('config_file', $conf); } if (session('error')) { //show_msg(); } else { session('step', 3); Storage::put(APP_PATH . 'Common/Conf/install.lock', 'lock'); $this->redirect('Index/complete'); } }