// +---------------------------------------------------------------------- namespace app\controller; use app\Request; class InstallController { public function index(Request $request) { [$step, $testdbpwd, $install, $n] = $request->getMore([ ['step', 1], ['testdbpwd', 0], ['install', 0], ['n', 0] ], true); $path = app()->getRootPath(); $step = app()->request->param('step') ?? 1; if (file_exists($path . 'public/install/install.lock')) { return '你已经安装过该系统,如果想重新安装,请先删除install目录下的 install.lock 文件,然后再安装。'; } @set_time_limit(1000); if (phpversion() < '7.1.0' && '7.4.0' > phpversion()) { return '您的php版本过低,不能安装本软件,php版本7.4,谢谢!'; } if (phpversion() > '8.0') { return '您的php版本太高,不能安装本软件,php版本7.4,谢谢!'; } date_default_timezone_set('PRC'); error_reporting(E_ALL & ~E_NOTICE); //数据库 $sqlFile = 'crmeb.sql'; $configFile = '.env'; if (!file_exists($path . 'public/install/' . $sqlFile) || !file_exists($path . 'public/install/' . $configFile)) { return '缺少必要的安装文件!'; } $Title = "CRMEB安装向导"; $Powered = "Powered by CRMEB"; $steps = array( '1' => '安装许可协议', '2' => '运行环境检测', '3' => '安装参数设置', '4' => '安装详细过程', '5' => '安装完成', ); switch ($step) { case '1': return view('/install/view/step1', [ 'title' => $Title, 'powered' => $Powered ]); case '2': $phpv = @ phpversion(); $os = PHP_OS; $tmp = function_exists('gd_info') ? gd_info() : array(); $server = $_SERVER["SERVER_SOFTWARE"]; $host = (empty($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_HOST"] : $_SERVER["SERVER_ADDR"]); $name = $_SERVER["SERVER_NAME"]; $max_execution_time = ini_get('max_execution_time'); $allow_reference = (ini_get('allow_call_time_pass_reference') ? '[√]On' : '[×]Off'); $allow_url_fopen = (ini_get('allow_url_fopen') ? '[√]On' : '[×]Off'); $safe_mode = (ini_get('safe_mode') ? '[×]On' : '[√]Off'); $err = 0; if (empty($tmp['GD Version'])) { $gd = '[×]Off'; $err++; } else { $gd = '[√]On ' . $tmp['GD Version']; } if (extension_loaded('redis')) { $redis = ' 已安装'; } else { $redis = ' 点击查看帮助'; $err++; } if (extension_loaded('swoole_loader')) { $swoole = ' 已安装'; } else { $swoole = ' 点击查看帮助'; $err++; } if (function_exists('mysqli_connect')) { $mysql = ' 已安装'; } else { $mysql = ' 请安装mysqli扩展'; $err++; } if (ini_get('file_uploads')) { $uploadSize = ' ' . ini_get('upload_max_filesize'); } else { $uploadSize = '禁止上传'; } if (function_exists('session_start')) { $session = ' 支持'; } else { $session = ' 不支持'; $err++; } if (function_exists('curl_init')) { $curl = '[√]支持 '; } else { $curl = '[×]不支持'; $err++; } if (function_exists('bcadd')) { $bcmath = '[√]支持 '; } else { $bcmath = '[×]不支持'; $err++; } if (function_exists('openssl_encrypt')) { $openssl = '[√]支持 '; } else { $openssl = '[×]不支持'; $err++; } if (function_exists('finfo_open')) { $finfo_open = '[√]支持 '; } else { $finfo_open = '点击查看帮助'; $err++; } $folder = array( 'public/uploads', 'runtime', ); $dirStr = ''; foreach ($folder as $dir) { $Testdir = $path . $dir; if (!is_file($Testdir)) { if (!is_dir($Testdir)) { $this->dir_create($Testdir); } } if ($this->testwrite($Testdir)) { $w = '可写 '; } else { $w = '不可写 '; $err++; } if (is_readable($Testdir)) { $r = '可读'; } else { $r = '不可读'; $err++; } $dirStr .= " $dir 读写 $w $r "; } //必须开启函数 if (function_exists('file_put_contents')) { $file_put_contents = '[√]开启 '; } else { $file_put_contents = '[×]关闭'; $err++; } if (function_exists('imagettftext')) { $imagettftext = '[√]开启 '; } else { $imagettftext = '[×]关闭'; $err++; } if ($err > 0) { $next = '下一步'; } else { $next = '下一步'; } return view('/install/view/step2', [ 'title' => $Title, 'powered' => $Powered, 'os' => $os, 'server' => $server, 'phpv' => $phpv, 'uploadSize' => $uploadSize, 'session' => $session, 'safe_mode' => $safe_mode, 'swoole' => $swoole, 'redis' => $redis, 'mysql' => $mysql, 'curl' => $curl, 'bcmath' => $bcmath, 'openssl' => $openssl, 'finfo_open' => $finfo_open, 'gd' => $gd, 'file_put_contents' => $file_put_contents, 'imagettftext' => $imagettftext, 'dirStr' => $dirStr, 'next' => $next, ]); case '3': if ($testdbpwd) { $post = $request->postMore([ ['dbHost', ''], ['dbport', ''], ['dbUser', ''], ['dbPwd', ''], ['dbName', ''], ['rbhost', ''], ['rbport', ''], ['rbselect', ''], ['rbpw', ''], ]); $conn = @mysqli_connect($post['dbHost'], $post['dbUser'], $post['dbPwd'], NULL, $post['dbport']); if (mysqli_connect_errno($conn)) { return 0; } else { $result = mysqli_query($conn, "SELECT @@global.sql_mode"); $result = $result->fetch_array(); $version = mysqli_get_server_info($conn); if ($version >= 5.7) { if (strstr($result[0], 'STRICT_TRANS_TABLES') || strstr($result[0], 'STRICT_ALL_TABLES') || strstr($result[0], 'TRADITIONAL') || strstr($result[0], 'ANSI')) return -1; } $result = mysqli_query($conn, "select count(table_name) as c from information_schema.`TABLES` where table_schema='$post[dbName]'"); $result = $result->fetch_array(); if ($result['c'] > 0) return -2; } //redis数据库信息 $rbhost = $post['rbhost'] ?? '127.0.0.1'; $rbport = $post['rbport'] ?? 6379; $rbpw = $post['rbpw'] ?? ''; $rbselect = $post['rbselect'] ?? 0; try { $redis = new \Redis(); $redis->connect($rbhost, $rbport); if ($rbpw) { $redis->auth($rbpw); } if ($rbselect) { $redis->select($rbselect); } $res = $redis->set('install', 1, 10); if ($res) { return 1; } else { return -3; } } catch (\Throwable $e) { return -3; } } return view('/install/view/step3', [ 'title' => $Title, 'powered' => $Powered, ]); case '4': $post = $request->postMore([ ['dbhost', ''], ['dbport', ''], ['dbname', ''], ['dbuser', ''], ['dbpw', ''], ['dbprefix', ''], ['rbhost', ''], ['rbport', ''], ['rbselect', ''], ['rbpw', ''], ['manager', ''], ['manager_pwd', ''], ['manager_email', ''], ['demo', ''], ['cache_prefix', ''], ['cache_tag_prefix', ''], ]); if (intval($install)) { $i = 0; if ($i == 999999) return false; $arr = array(); $dbHost = trim($post['dbhost']); $post['dbport'] = $post['dbport'] ? $post['dbport'] : '3306'; $dbName = strtolower(trim($post['dbname'])); $dbUser = trim($post['dbuser']); $dbPwd = trim($post['dbpw']); $dbPrefix = empty($post['dbprefix']) ? 'eb_' : trim($post['dbprefix']); $username = trim($post['manager']); $password = trim($post['manager_pwd']); $email = trim($post['manager_email']); if (!function_exists('mysqli_connect')) { $arr['msg'] = "请安装 mysqli 扩展!"; return $arr; } $conn = @mysqli_connect($dbHost, $dbUser, $dbPwd, NULL, $post['dbport']); if (mysqli_connect_errno($conn)) { $arr['msg'] = "连接数据库失败!" . mysqli_connect_error($conn); return $arr; } mysqli_set_charset($conn, "utf8"); //,character_set_client=binary,sql_mode=''; $version = mysqli_get_server_info($conn); if ($version < 5.1) { $arr['msg'] = '数据库版本太低! 必须5.1以上'; return $arr; } if (!mysqli_select_db($conn, $dbName)) { //创建数据时同时设置编码 if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;")) { $arr['msg'] = '数据库 ' . $dbName . ' 不存在,也没权限创建新的数据库!'; return $arr; } if ($n == -1) { $arr['n'] = 0; $arr['msg'] = "成功创建数据库:{$dbName}
"; return $arr; } mysqli_select_db($conn, $dbName); } //读取数据文件 $sqldata = file_get_contents($path . 'public/install/' . $sqlFile); $sqlFormat = $this->sql_split($sqldata, $dbPrefix); //创建写入sql数据库文件到库中 结束 /** * 执行SQL语句 */ $counts = count($sqlFormat); for ($i = $n; $i < $counts; $i++) { $sql = trim($sqlFormat[$i]); if (strstr($sql, 'CREATE TABLE')) { preg_match('/CREATE TABLE (IF NOT EXISTS)? `eb_([^ ]*)`/is', $sql, $matches); mysqli_query($conn, "DROP TABLE IF EXISTS `$matches[2]`"); $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀 $ret = mysqli_query($conn, $sql); if ($ret) { $message = '
  • 创建数据表[' . $dbPrefix . $matches[2] . ']完成!' . date('Y-m-d H:i:s') . '
  • '; } else { $err = mysqli_error($conn); $message = '
  • 创建数据表[' . $dbPrefix . $matches[2] . ']失败!失败原因:' . $err . '' . date('Y-m-d H:i:s') . '
  • '; } $i++; $arr = array('n' => $i, 'msg' => $message); return $arr; } else { if (trim($sql) == '') continue; $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀 $ret = mysqli_query($conn, $sql); $message = ''; $i++; $arr = array('n' => $i, 'msg' => $message); return $arr; } } // 清空测试数据 if (!$post['demo']) { $result = mysqli_query($conn, "show tables"); $tables = mysqli_fetch_all($result);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型 $bl_table = array('eb_system_admin' , 'eb_system_role' , 'eb_system_config' , 'eb_system_config_tab' , 'eb_system_menus' , 'eb_system_file' , 'eb_express' , 'eb_system_group' , 'eb_system_group_data' , 'eb_template_message' , 'eb_shipping_templates' , "eb_shipping_templates_region" , "eb_shipping_templates_free" , 'eb_system_city' , 'eb_diy' , 'eb_member_ship' , 'eb_member_right' , 'eb_agreement' , 'eb_store_service_speechcraft' , 'eb_system_user_level' , 'eb_city_area' , 'eb_system_notification' , 'eb_system_attachment_category' , 'eb_system_attachment' , 'eb_page_category' , 'eb_page_link' , 'eb_category' , 'eb_store_product_label' , 'eb_store_product_ensure' , 'eb_cache'); foreach ($bl_table as $k => $v) { $bl_table[$k] = str_replace('eb_', $dbPrefix, $v); } foreach ($tables as $key => $val) { if (!in_array($val[0], $bl_table)) { mysqli_query($conn, "truncate table " . $val[0]); } } // $this->delFile($path . 'public/uploads'); // 清空测试图片 } //读取配置文件,并替换真实配置数据1 $strConfig = file_get_contents($path . 'public/install/' . $configFile); $app_key = md5(request()->ip() . uniqid() . time()); $strConfig = str_replace('#APP_KEY#', $app_key, $strConfig); $strConfig = str_replace('#DB_HOST#', $dbHost, $strConfig); $strConfig = str_replace('#DB_NAME#', $dbName, $strConfig); $strConfig = str_replace('#DB_USER#', $dbUser, $strConfig); $strConfig = str_replace('#DB_PWD#', $dbPwd, $strConfig); $strConfig = str_replace('#DB_PORT#', $post['dbport'], $strConfig); $strConfig = str_replace('#DB_PREFIX#', $dbPrefix, $strConfig); $strConfig = str_replace('#DB_CHARSET#', 'utf8', $strConfig); // $strConfig = str_replace('#DB_DEBUG#', false, $strConfig); //redis数据库信息 $rbhost = $post['rbhost'] ?? '127.0.0.1'; $rbport = $post['rbport'] ?? '6379'; $rbpw = $post['rbpw'] ?? ''; $rbselect = $post['rbselect'] ?? 0; $strConfig = str_replace('#RB_HOST#', $rbhost, $strConfig); $strConfig = str_replace('#RB_PORT#', $rbport, $strConfig); $strConfig = str_replace('#RB_PWD#', $rbpw, $strConfig); $strConfig = str_replace('#RB_SELECT#', $rbselect, $strConfig); //多项目部署配置 $cache_prefix = md5($app_key . uniqid() . time()); $cache_tag_prefix = 'CRMEB_MULTI-STORE_' . $cache_prefix; $strConfig = str_replace('#CACHE_PREFIX#', $cache_prefix, $strConfig); $strConfig = str_replace('#CACHE_TAG_PREFIX#', $cache_tag_prefix, $strConfig); @chmod($path . '/.env', 0777); //数据库配置文件的地址 @file_put_contents($path . '/.env', $strConfig); //数据库配置文件的地址 //读取配置文件,并替换换配置 // $strConfig = file_get_contents(SITE_DIR . '/application/config.php'); // $strConfig = str_replace('CRMEB_cache_prefix', $uniqid_str, $strConfig); // @chmod(SITE_DIR . '/application/config.php',0777); //配置文件的地址 // @file_put_contents(SITE_DIR . '/application/config.php', $strConfig); //配置文件的地址 //更新网站配置信息2 //插入管理员表字段tp_admin表 $time = time(); $ip = request()->ip(); $ip = empty($ip) ? "0.0.0.0" : $ip; $password = password_hash($post['manager_pwd'], PASSWORD_BCRYPT); mysqli_query($conn, "truncate table {$dbPrefix}system_admin"); $addadminsql = "INSERT INTO `{$dbPrefix}system_admin` (`id`, `account`, `pwd`, `real_name`, `roles`, `last_ip`, `last_time`, `add_time`, `login_count`, `level`, `status`, `is_del`) VALUES (1, '" . $username . "', '" . $password . "', 'admin', '1', '" . $ip . "',$time , $time, 0, 0, 1, 0)"; $res = mysqli_query($conn, $addadminsql); $res2 = true; if (app()->request->host(true)) { $site_url = '\'"http' . (app()->request->isSsl() ? 's' : '') . '://' . app()->request->host(true) . '"\''; $res2 = mysqli_query($conn, 'UPDATE `' . $dbPrefix . 'system_config` SET `value`=' . $site_url . ' WHERE `menu_name`="site_url"'); } if ($res) { $message = '成功添加管理员
    成功写入配置文件
    安装完成.'; $arr = array('n' => 999999, 'msg' => $message); return $arr; } else { $message = '添加管理员失败
    成功写入配置文件
    安装完成.'; $arr = array('n' => 999999, 'msg' => $message); return $arr; } } return view('/install/view/step4', [ 'title' => $Title, 'powered' => $Powered, 'data' => json_encode($post) ]); case '5': $this->installlog(); @touch($path . 'public/install/install.lock'); return view('/install/view/step5', [ 'title' => $Title, 'powered' => $Powered, 'ip' => request()->ip(), 'host' => request()->host(), 'version' => get_crmeb_version() ]); } } public function swooleCompiler() { // Set constants // Language items $languages['zh-cn'] = [ 'title' => 'Swoole Compiler Loader 安装助手', ]; $languages['en'] = [ 'title' => 'Swoole Compiler Loader Wizard', ]; // Set env variable for current environment $env = []; // Check os type $env['os'] = []; if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $env['os']['name'] = "windows"; $env['os']['raw_name'] = php_uname(); } else { $env['os']['name'] = "unix"; $env['os']['raw_name'] = php_uname(); } // Check php $env['php'] = []; $env['php']['version'] = phpversion(); // Check run mode $sapi_type = php_sapi_name(); if ("cli" == $sapi_type) { $env['php']['run_mode'] = "cli"; } else { $env['php']['run_mode'] = "web"; } // Check php bit if (PHP_INT_SIZE == 4) { $env['php']['bit'] = 32; } else { $env['php']['bit'] = 64; } $env['php']['sapi'] = $sapi_type; $env['php']['ini_loaded_file'] = php_ini_loaded_file(); $env['php']['ini_scanned_files'] = php_ini_scanned_files(); $env['php']['loaded_extensions'] = get_loaded_extensions(); $env['php']['incompatible_extensions'] = ['xdebug', 'ionCube', 'zend_loader']; $env['php']['loaded_incompatible_extensions'] = []; $env['php']['extension_dir'] = ini_get('extension_dir'); // Check incompatible extensions if (is_array($env['php']['loaded_extensions'])) { foreach ($env['php']['loaded_extensions'] as $loaded_extension) { foreach ($env['php']['incompatible_extensions'] as $incompatible_extension) { if (strpos(strtolower($loaded_extension), strtolower($incompatible_extension)) !== false) { $env['php']['loaded_incompatible_extensions'][] = $loaded_extension; } } } } $env['php']['loaded_incompatible_extensions'] = array_unique($env['php']['loaded_incompatible_extensions']); // Parse System Environment Info $sysInfo = $this->w_getSysInfo($env); // Check php thread safety $env['php']['raw_thread_safety'] = isset($sysInfo['thread_safety']) ? $sysInfo['thread_safety'] : false; if (isset($sysInfo['thread_safety'])) { $env['php']['thread_safety'] = $sysInfo['thread_safety'] ? '线程安全' : '非线程安全'; } else { $env['php']['thread_safety'] = '未知'; } // Check swoole loader installation if (isset($sysInfo['swoole_loader']) and isset($sysInfo['swoole_loader_version'])) { $env['php']['swoole_loader']['status'] = $sysInfo['swoole_loader'] ? "已安装" : '未安装'; if ($sysInfo['swoole_loader_version'] !== false) { $env['php']['swoole_loader']['version'] = "" . $sysInfo['swoole_loader_version'] . ""; } else { $env['php']['swoole_loader']['version'] = '未知'; } } else { $env['php']['swoole_loader']['status'] = '未安装'; $env['php']['swoole_loader']['version'] = '未知'; } /** * Web mode */ if (true) { $language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'zh-c', 0, 4); if (preg_match("/zh-c/i", $language)) { $env['lang'] = "zh-cn"; $wizard_lang = $env['lang']; } else { $env['lang'] = "en"; $wizard_lang = $env['lang']; } $html = ''; // Header $html_header = ' %s '; $html_header = sprintf($html_header, $languages[$wizard_lang]['title']); $html_body = '
    '; $html_body_nav = '
    '; $html_body_nav .= '

    CRMEB Swoole Compiler 安装向导

    '; $html_body_nav .= '

    Version:2.0.2 Date:2019-01-09

    '; $html_body_nav .= '

    '; // Environment information $html_body_environment = '
    检查当前环境
    '; // Error infomation $html_error = ""; if (!empty($env['php']['loaded_incompatible_extensions'])) { $html_error = '
    错误信息

    %s

    '; $err_msg = "当前PHP包含与swoole_compiler_loader扩展不兼容的扩展" . implode(',', $env['php']['loaded_incompatible_extensions']) . ",请移除不兼容的扩展。"; $html_error = sprintf($html_error, $err_msg); } // Check Loader Status $html_body_loader = '
    '; if (empty($html_error)) { $html_body_loader .= '
    '; $html_body_loader .= '
    安装和配置Swoole Loader
    '; $phpversion = substr($env['php']['version'], 0, 3); $phpversion = str_replace('.', '', $phpversion); $loaderFileName = ''; if ($env['os']['name'] == "windows") { $loaderFileName = 'php_swoole_loader_php' . $phpversion; if ($env['php']['thread_safety'] == '非线程安全') { $loaderFileName .= '_nzts_x64.dll'; } else { $loaderFileName .= '_zts_x64.dll'; } } else { if ($env['php']['thread_safety'] != '非线程安全') { $loaderFileName = 'swoole_loader' . $phpversion . '_zts.so'; } else { $loaderFileName = 'swoole_loader' . $phpversion . '.so'; } } $html_body_loader .= '

    1 - 安装Swoole Loader

    前往根目录 /help/swoole-loader/' . $loaderFileName . '扩展文件上传到当前PHP的扩展安装目录中:

    ' . $env['php']['extension_dir'] . '

    '; $html_body_loader .= '

    2 - 修改php.ini配置(如已修改配置,请忽略此步骤,不必重复添加)

    '; $html_body_loader .= '编辑此PHP配置文件:' . $env['php']['ini_loaded_file'] . ',在此文件底部结尾处加入如下配置
    '; if ($env['os']['name'] == "windows") { $html_body_loader .= '

    extension=' . $env['php']['extension_dir'] . DIRECTORY_SEPARATOR . $loaderFileName . '
    注意:需要名称和刚才上传到当前PHP的扩展安装目录中的文件名一致'; } else { $html_body_loader .= '
    extension=' . $env['php']['extension_dir'] . DIRECTORY_SEPARATOR . $loaderFileName . '
    注意:需要名称和刚才上传到当前PHP的扩展安装目录中的文件名一致'; } $html_body_loader .= '

    '; $html_body_loader .= '

    3 - 重启服务

    重启或重载PHP配置

    '; $html_body_loader .= '
    '; } // Body footer $html_body_footer = ''; $html_body .= $html_body_nav . '
    ' . $html_body_environment . $html_error . $html_body_loader . '
    ' . $html_body_footer; $html_body .= '
    '; // Footer $html_footer = ' '; // Make full html $html = $html_header . $html_body . $html_footer; echo $html; } } function w_getSysInfo($env) { $sysEnv = []; // Get content of phpinfo ob_start(); phpinfo(); $sysInfo = ob_get_contents(); ob_end_clean(); // Explode phpinfo content if ($env['php']['run_mode'] == 'cli') { $sysInfoList = explode('\n', $sysInfo); } else { $sysInfoList = explode('', $sysInfo); } foreach ($sysInfoList as $sysInfoItem) { if (preg_match('/thread safety/i', $sysInfoItem)) { $sysEnv['thread_safety'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0); } if (preg_match('/swoole_loader support/i', $sysInfoItem)) { $sysEnv['swoole_loader'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0); } if (preg_match('/swoole_loader version/i', $sysInfoItem)) { preg_match('/\d+.\d+.\d+/s', $sysInfoItem, $match); $sysEnv['swoole_loader_version'] = isset($match[0]) ? $match[0] : false; } } return $sysEnv; } //读取版本号 function getversion() { try { $curent_version = @file(app()->getRootPath() . '.version'); } catch (\Exception $e) { $curent_version = []; } if ($curent_version && is_array($curent_version)) { foreach ($curent_version as $val) { [$k, $v] = explode('=', $val); $version_arr[$k] = $v; } } return $version_arr; } //写入安装信息 function installlog() { $mt_rand_str = $this->sp_random_string(6); $str_constant = "getRootPath() . '.constant', $str_constant); } function sp_random_string($len = 8) { $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); $charsLen = count($chars) - 1; shuffle($chars); // 将数组打乱 $output = ""; mt_srand(); for ($i = 0; $i < $len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } //判断权限 function testwrite($d) { if (is_file($d)) { if (is_writeable($d)) { return true; } return false; } else { $tfile = "_test.txt"; $fp = @fopen($d . "/" . $tfile, "w"); if (!$fp) { return false; } fclose($fp); $rs = @unlink($d . "/" . $tfile); if ($rs) { return true; } return false; } } // 获取客户端IP地址 function get_client_ip() { static $ip = NULL; if ($ip !== NULL) return $ip; if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown', $arr); if (false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } // IP地址合法验证 $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0'; return $ip; } //创建目录 function dir_create($path, $mode = 0777) { if (is_dir($path)) return TRUE; $ftp_enable = 0; $path = $this->dir_path($path); $temp = explode('/', $path); $cur_dir = ''; $max = count($temp) - 1; for ($i = 0; $i < $max; $i++) { $cur_dir .= $temp[$i] . '/'; if (@is_dir($cur_dir)) continue; @mkdir($cur_dir, 0777, true); @chmod($cur_dir, 0777); } return is_dir($path); } function dir_path($path) { $path = str_replace('\\', '/', $path); if (substr($path, -1) != '/') $path = $path . '/'; return $path; } function sql_split($sql, $tablepre) { if ($tablepre != "tp_") $sql = str_replace("tp_", $tablepre, $sql); $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql); $sql = str_replace("\r", "\n", $sql); $ret = array(); $num = 0; $queriesarray = explode(";\n", trim($sql)); unset($sql); foreach ($queriesarray as $query) { $ret[$num] = ''; $queries = explode("\n", trim($query)); $queries = array_filter($queries); foreach ($queries as $query) { $str1 = substr($query, 0, 1); if ($str1 != '#' && $str1 != '-') $ret[$num] .= $query; } $num++; } return $ret; } // 递归删除文件夹 function delFile($dir, $file_type = '') { if (is_dir($dir)) { $files = scandir($dir); //打开目录 //列出目录中的所有文件并去掉 . 和 .. foreach ($files as $filename) { if ($filename != '.' && $filename != '..') { if (!is_dir($dir . '/' . $filename)) { if (empty($file_type)) { unlink($dir . '/' . $filename); } else { if (is_array($file_type)) { //正则匹配指定文件 if (preg_match($file_type[0], $filename)) { unlink($dir . '/' . $filename); } } else { //指定包含某些字符串的文件 if (false != stristr($filename, $file_type)) { unlink($dir . '/' . $filename); } } } } else { $this->delFile($dir . '/' . $filename); rmdir($dir . '/' . $filename); } } } } else { if (file_exists($dir)) unlink($dir); } } }