diff --git a/appliction/Common/Config/etc.php b/appliction/Common/Config/etc.php new file mode 100644 index 0000000..be88d4d --- /dev/null +++ b/appliction/Common/Config/etc.php @@ -0,0 +1,21 @@ + '', + 'dbEngine' => 'mysql', + 'dbName' => 'leguan', + 'dbHost' => '127.0.0.1', + 'dbPort' => '', + 'dbUser'=>'root', + 'dbPwd' =>'', + 'dbPrefix' => 'lg_' + ); \ No newline at end of file diff --git a/appliction/Index/Config/etc.php b/appliction/Index/Config/etc.php new file mode 100644 index 0000000..16bd58d --- /dev/null +++ b/appliction/Index/Config/etc.php @@ -0,0 +1,4 @@ + 'test'); \ No newline at end of file diff --git a/appliction/Index/Controller/IndexController.php b/appliction/Index/Controller/IndexController.php new file mode 100644 index 0000000..ebda117 --- /dev/null +++ b/appliction/Index/Controller/IndexController.php @@ -0,0 +1,87 @@ + array('>',5,'or'),'id'=>array('in',array(1,3,5))); + $article = $this->Db->table($table)->where($condition)->field('id,title')->select(); + + $view = $this->view; + $view->assign('author', 'ushe'); + $view->assign('script', ''); + $view->assign('article', $article); + $view->assign('age',20); + $view->list = array('apple','pear','orange'); + $view->display(); + echo "
"; + echo $this->Debug->execTime(); + } + + public function dbAction() + { + $values = array("title"=>"new 'title",'description' => 'test'); + $table = 'article'; + //$this->Debug->dump($this->Db->query('insert into `lg_article` set title = ?,description = ?;')); + //echo $this->Db->table($table)->values($values)->add(); + + $this->Debug->dump( + $this->Sql->table($table)->where( + array('click' => array('>',5,'or'),'id'=>array('in',array(1,3,5))))->field('id,title')->select() + ); + } + + public function chapterAction() + { + $chapter = new Chapter(); + $chapter->show(); + } + + public function cacheAction() + { + $arr = array('a',123); + $this->cache->write('test_arr', $arr); + $this->debug->dump($this->cache->read('test_arr')); + } + + public function readcacheAction() + { + $arr = $this->cache->read('test_arr'); + $this->debug->dump($arr); + } + + public function defaultAction() + { + echo 'defaultAction'; + } + + public function uploadAction() + { + $this->view->assign('upload', new \Usheweb\Upload\Upload()); + $this->view->display(); + } + + public function doUploadAction() + { + if(!$this->request->isPost()){ + return; + } + + $upload = new \Usheweb\Upload\Upload(); + $this->debug->dump($upload->run()); + } +} \ No newline at end of file diff --git a/data/cache/test_arr.php b/data/cache/test_arr.php new file mode 100644 index 0000000..a13360a --- /dev/null +++ b/data/cache/test_arr.php @@ -0,0 +1,2 @@ +_init(); + } + + /** + * 初始化路径信息 + */ + private function _init() + { + $this->_path['root'] = realpath(''); + $this->_path['ds'] = DIRECTORY_SEPARATOR; + $this->_path['ps'] = PATH_SEPARATOR; + $this->_path['lib'] = $this->_path['root'] . $this->_path['ds'] . 'library'; + $this->_path['app'] = $this->_path['root'] . $this->_path['ds'] . APP_NAME; + $this->_path['data'] = $this->_path['root'] . $this->_path['ds'] . 'data'; + $this->_path['view'] = $this->_path['root'] . $this->_path['ds'] . 'themes'; + $this->_path['upload'] = $this->_path['root'] . $this->_path['ds'] . 'uploads'; + + $this->_path['cache'] = $this->_path['data'] . $this->_path['ds'] . 'cache'; + $this->_path['log'] = $this->_path['data'] . $this->_path['ds'] . 'log'; + + $this->_path['html'] = $this->_path['cache'] . $this->_path['ds'] . 'html'; + $this->_path['data'] = $this->_path['cache'] . $this->_path['ds'] . 'data'; + + $this->_path['appCommon'] = $this->_path['app'] . $this->_path['ds'] . 'Common'; + $this->_path['appConfig'] = $this->_path['appCommon'] . $this->_path['ds'] . 'Config'; + } + + public function __get($name) + { + if (isset($this->_path[$name])) { + return $this->_path[$name]; + } + + return null; + } + + public function __set($name, $value) + { + $this->add($name, $value); + } + + /** + * 添加路径信息 + * + * @exception $name路径信息存在 + * @param $name string + * @param $value string + * @return void + */ + public function add($name, $value) + { + if (isset($this->_path[$name])) { + throw new \Exception("$name".'已在Path::$_path中', 1); + } + + $this->_path[$name] = $value; + } + + /** + * 删除路径信息 + * + * @exception $name路径信息不存在 + * @param $name + * @return string + */ + public function del($name) + { + if(!isset($this->_path[$name])){ + throw new \Exception("$name".'不在Path::$_path中', 1); + } + + unset($this->_path[$name]); + } + + /** + * 清除路径信息 + */ + public function clear() + { + $this->_path = array(); + } + + /** + * 获取路径中文件扩展名 + */ + public function getExtension($path) + { + return pathinfo($path, PATHINFO_EXTENSION); + } +} \ No newline at end of file diff --git a/library/Leguan/Bootstrap/Start.php b/library/Leguan/Bootstrap/Start.php new file mode 100644 index 0000000..02ad186 --- /dev/null +++ b/library/Leguan/Bootstrap/Start.php @@ -0,0 +1,89 @@ + 5.3.0 !'); + } + + //加载引导文件 + $coreFiles = array('Path','Leguan'); + foreach ($coreFiles as $value) { + require "{$value}.php"; + } + $path = new Path(); + Leguan::set('path',$path); + + //设置include的路径 + $new_include_path = array('.', $path->lib, $path->app); + $new_include_path = implode($path->ps, $new_include_path); + set_include_path($new_include_path); + + //注册 类加载器 + spl_autoload_register('self::autoLoader'); + + $config = Leguan::get('config'); + defined('APP_NAME') && $config->app = APP_NAME; + defined('IS_DEBUG') && $config->isDebug = IS_DEBUG; + + //加载应用公共配置 + $appConfig = $path->appConfig . $path->ds . 'etc.php'; + if(file_exists($appConfig)){ + $config->load(require $appConfig); + } + + $config->isDebug ? error_reporting(E_ALL) : error_reporting(0); + date_default_timezone_set($config->timezone); + header("Content-Type:text/html;charset={$config->charset}"); + + Leguan::get('url')->setCleanKey($config->urlCleanKey); + } + + /** + * 框架入口 + */ + public static function run() + { + self::_init(); + Leguan::get('routing')->run(); + } + + /** + * 类自动加载器 + */ + public static function autoLoader($className) + { + $path = leguan::get('path'); + $className = ltrim($className, '\\'); + $className = str_replace('\\', $path->ds, $className); + + require "{$className}.php"; + } +} \ No newline at end of file diff --git a/library/Leguan/Cache/Adapter/File.php b/library/Leguan/Cache/Adapter/File.php new file mode 100644 index 0000000..8cf0606 --- /dev/null +++ b/library/Leguan/Cache/Adapter/File.php @@ -0,0 +1,60 @@ +cache . $path->ds . "{$name}.php"; + + if(file_exists($fileName)){ + $content = require $fileName; + //http://www.cnblogs.com/A-Song/archive/2011/12/13/2285619.html + if(get_magic_quotes_gpc()){ + $content = stripslashes($content); + } + return unserialize($content); + } + + return null; + } + + /** + * 写入緩存內容 + * + * @param $name + * @param $value + * @return void + */ + public function write($name, $value) + { + $path = Leguan::get('path'); + $fileName = $path->cache . $path->ds . "{$name}.php"; + + $data = "cacheType; + $cacheType = ucwords($cacheType); + + $class = "\\Leguan\\Cache\\Adapter\\{$cacheType}"; + $this->_obj = new $class(); + } + + /** + * 讀取緩存內容 + * + * @param $name + * @return array + */ + public function read($name) + { + if(!isset($this->_data[$name])){ + $this->_data[$name] = $this->_obj->read($name); + } + + return $this->_data[$name]; + } + + /** + * 写入緩存內容 + * + * @param $name + * @param $value + * @return void + */ + public function write($name, $value) + { + $this->_data[$name] = $value; + $this->_obj->write($name, $value); + } + } \ No newline at end of file diff --git a/library/Leguan/Cache/ICache.php b/library/Leguan/Cache/ICache.php new file mode 100644 index 0000000..3e662d8 --- /dev/null +++ b/library/Leguan/Cache/ICache.php @@ -0,0 +1,19 @@ +_init(); + } + + /** + * 配置信息初始化 + */ + private function _init() + { + $this->_config = require 'etc.php'; + } + + public function __get($name) + { + return $this->get($name); + } + + public function __set($name, $value) + { + $this->set($name, $value); + } + + /** + * 加载一组配置 + * + * @param array $config 新的配置数组 + * @return void + */ + public function load($config) + { + $this->_config = array_merge($this->_config,$config); + } + + /** + * 获取系统配置 + * + * @param string $name 配置的name + * @return mixed + */ + public function get($name) + { + if(!isset($this->_config[$name])){ + return null; + } + + return $this->_config[$name]; + } + + /** + * 设置系统配置 + * + * @param string $name 配置的name + * @param mixed $value 配置的值 + * @return void + */ + public function set($name,$value) + { + $this->_config[$name] = $value; + } + + /** + * 删除一个配置 + * + * @param $name string + * @return void + */ + public function del($name) + { + if(!isset($this->_config[$name])){ + return null; + } + + unset($this->_config[$name]); + } + + /** + * 清除配置 + */ + public function clear() + { + $this->_config = array(); + } + } \ No newline at end of file diff --git a/library/Leguan/Config/etc.php b/library/Leguan/Config/etc.php new file mode 100644 index 0000000..d8facd5 --- /dev/null +++ b/library/Leguan/Config/etc.php @@ -0,0 +1,53 @@ + 'Asia/Shanghai', + //默认字符集 + 'charset' => 'utf-8', + //开发者模式 + 'isDebug' => false, + //路由类型 0 普通、1 patInfo、2 url重写、3 clean + 'urlType' => 1, + //url clean模式下的key + 'urlCleanKey' => 'l', + //表、字段转义 - mysql + 'dbEscapeMysql' => array('`', '`'), + //表、字段转义 - mssql + 'dbEscapeMssql' => array('[', ']'), + //表、字段转义 - sqllite + 'dbEscapeSqllite' => array('"', '"'), + //是否开启session + 'sessionStart' => true, + //默认模块 + 'defaultModule' => 'Index', + //默认控制器 + 'defaultController' => 'Index', + //默认方法 + 'defaultAction' => 'Index', + //默认模板主题 + 'theme' => 'Default', + //模板扩展名 + 'viewExtension' => '.php', + //模板分割符 + 'viewLimiter' => array(''), + //模板输出转义函数 + 'viewEscape' => 'htmlspecialchars', + //url扩展 + 'urlExtension' => '.html', + //url分割符 + 'urlLimiter' => '/', + //缓存类型 + 'cacheType' => 'file', + //上传文件扩展名 + 'fileExtension' => array('txt', 'html', 'htm', 'jpg', 'png', 'gif', 'bmp', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'zip', 'rar'), + //上传文件最大尺寸 1024 * 1024 * 10 = 10M + 'fileMaxSize' => 1024 * 1024 * 10 + ); \ No newline at end of file diff --git a/library/Leguan/Controller/Controller.php b/library/Leguan/Controller/Controller.php new file mode 100644 index 0000000..df18fc4 --- /dev/null +++ b/library/Leguan/Controller/Controller.php @@ -0,0 +1,17 @@ +get($name); + } + + public function __set($name, $value) + { + $this->set($name, $value); + } +} \ No newline at end of file diff --git a/library/Leguan/Db/Db.php b/library/Leguan/Db/Db.php new file mode 100644 index 0000000..eff401a --- /dev/null +++ b/library/Leguan/Db/Db.php @@ -0,0 +1,102 @@ +_init(); + } + + /** + * 初始化数据库连接信息 + */ + protected function _init() + { + $config = Leguan::get('config'); + + if(empty(self::$_db)){ + + self::$_dbCharset = $config->charset != null ? + $config->charset : self::$_dbCharset; + self::$_dbCharset = str_replace('-', '', self::$_dbCharset); + self::$_dbPrefix = $config->dbPrefix; + + if(empty($config->dbDsn)){ + $dsn = new Dsn($config->dbEngine, + $config->dbHost, + $config->dbName, + $config->dbPort); + $config->dbDsn = $dsn->getDsn(); + } + + try { + self::$_db = new \PDO($config->dbDsn, $config->dbUser, $config->dbPwd); + //禁用prepared statements的仿真效果 + self::$_db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); + self::$_db->exec("set names '".self::$_dbCharset."'"); + } catch (PDOException $e) { + echo 'Connection failed: ' . $e->getMessage(); + } + } + } + + /** + * 执行一条sql查询语句 + * + * @param $statement string 带预处理的sql语句 + * @param $input array 预处理数据 + * @return mixed + */ + public function query($statement,$input = array()) + { + $statement = str_replace("@_", self::$_dbPrefix, $statement); + $this->_stmt = self::$_db->prepare($statement); + $result = $this->_stmt->execute($input); + + if($result === false){ + return false; + } + + return $this->_fetchAll(); + } + + /** + * 获取最后插入记录的自增长id + */ + public function getLastInsertId() + { + return self::$_db->lastInsertId(); + } + + /** + * 获取结果集 + * + * @param $style + */ + private function _fetchAll($style = \PDO::FETCH_ASSOC) + { + return $this->_stmt->fetchAll($style); + } + +} \ No newline at end of file diff --git a/library/Leguan/Db/Dsn.php b/library/Leguan/Db/Dsn.php new file mode 100644 index 0000000..ff7d97e --- /dev/null +++ b/library/Leguan/Db/Dsn.php @@ -0,0 +1,63 @@ +_dbEngine = $engine; + $this->_dbPort = $port; + $this->_dbName = $dbName; + $this->_host = $host; + } + + /** + * 获取当前数据库引擎对应的dsn + * + * @return string + */ + public function getDsn() + { + $engine = $this->_dbEngine; + + if (empty($this->_dbPort)) { + return $this->$engine(); + } else { + return $this->$engine($this->_dbPort); + } + } + + public function mysql($port = 3306) + { + return $this->_common($port); + } + + public function cubrid($port = 33000) + { + return $this->_common($port); + } + + /** + * 通用dsn构造器 + */ + private function _common($port) + { + return "{$this->_dbEngine}:dbname={$this->_dbName};host={$this->_host};port={$port}"; + } +} \ No newline at end of file diff --git a/library/Leguan/Debug/Debug.php b/library/Leguan/Debug/Debug.php new file mode 100644 index 0000000..6ff289e --- /dev/null +++ b/library/Leguan/Debug/Debug.php @@ -0,0 +1,70 @@ +"; + var_dump($expression); + echo ""; + } + + /** + * 字符串输出到 textarea 中 + */ + public function dumpText($string, $col = 150, $row = 50) + { + echo ""; + } + + /** + * 开始记录代码执行时间 + * + * @param $name string + * @return void + */ + public function start($name) + { + $this->_time[$name] = microtime(true); + } + + /** + * 获取代码执行时间 + * + * @param $name string + * @return float + */ + public function stop($name) + { + $this->_time[$name] = microtime(true) - $this->_time[$name]; + + return $this->_time[$name]; + } + + /** + * 获取app执行时间 + * + * @return float + */ + public function execTime() + { + return microtime(true) - APP_START_TIME; + } +} \ No newline at end of file diff --git a/library/Leguan/Helper/Helper.php b/library/Leguan/Helper/Helper.php new file mode 100644 index 0000000..11cdeea --- /dev/null +++ b/library/Leguan/Helper/Helper.php @@ -0,0 +1,60 @@ +_data['HTTP_X_REQUESTED_WITH']) + && strtolower($this->_data['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ? true : false; + } + + /** + * 是否为手机客户端 + */ + public function isMobile() + { + //判断手机发送的客户端标志 + if(isset($this->_data['HTTP_USER_AGENT'])) { + $userAgent = strtolower($this->_data['HTTP_USER_AGENT']); + $clientkeywords = array( + 'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-' + ,'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', + 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', + 'operamobi', 'opera mobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile' + ); + // 从HTTP_USER_AGENT中查找手机浏览器的关键字 + if(preg_match("/(".implode('|',$clientkeywords).")/i",$userAgent) + && strpos($userAgent,'ipad') === false){ + return true; + } + } + return false; + } + + /** + * 获取客户端IP地址 + * + * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 + * @param boolean $adv 是否进行高级模式获取(有可能被伪装) + * @return mixed + */ + public function getClientIp($type = 0,$adv=false) + { + $type = $type ? 1 : 0; + static $ip = NULL; + if ($ip !== NULL) return $ip[$type]; + if($adv){ + if (isset($this->_data['HTTP_X_FORWARDED_FOR'])) { + $arr = explode(',', $this->_data['HTTP_X_FORWARDED_FOR']); + $pos = array_search('unknown',$arr); + if(false !== $pos) unset($arr[$pos]); + $ip = trim($arr[0]); + }elseif (isset($this->_data['HTTP_CLIENT_IP'])) { + $ip = $this->_data['HTTP_CLIENT_IP']; + }elseif (isset($this->_data['REMOTE_ADDR'])) { + $ip = $this->_data['REMOTE_ADDR']; + } + }elseif (isset($this->_data['REMOTE_ADDR'])) { + $ip = $this->_data['REMOTE_ADDR']; + } + // IP地址合法验证 + $long = sprintf("%u",ip2long($ip)); + $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); + return $ip[$type]; + } + + /** + * 获取当前域名 + */ + public function getDomain($hasBaseUrl = true) + { + $httpType = $this->getHttpType(); + $domain = "{$httpType}{$_SERVER['SERVER_NAME']}"; + + if ($_SERVER["SERVER_PORT"] != 80) { + $domain = "{$domain}:{$_SERVER["SERVER_PORT"]}"; + } + + if ($hasBaseUrl) { + $baseUrl = dirname($_SERVER['SCRIPT_NAME']); + $baseUrl = str_replace('\\','/', $baseUrl); + $baseUrl = ltrim($baseUrl, '/'); + $domain = "{$domain}/{$baseUrl}"; + } + + return $domain; + } + + /** + * 获取http类型 + */ + public function getHttpType() + { + return $this->isSsl() ? "https://" : "http://"; + } + + /** + * 判断是否SSL协议 + * @return boolean + */ + function isSsl() + { + if (isset($_SERVER['HTTPS']) && + ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) { + return true; + } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) { + return true; + } + return false; + } +} \ No newline at end of file diff --git a/library/Leguan/Routing/Routing.php b/library/Leguan/Routing/Routing.php new file mode 100644 index 0000000..74d58e6 --- /dev/null +++ b/library/Leguan/Routing/Routing.php @@ -0,0 +1,146 @@ +_init(); + } + + /** + * 初始化路由配置 + */ + private function _init() + { + $this->_routing = require 'etc.php'; + } + + public function run() + { + $config = Leguan::get('config'); + $urlType = $config->urlType; + $url = Leguan::get('url'); + $urlType = $url->getUrlType($urlType); + + if($urlType === null){ + throw new \Exception('配置中的urlType不在Url::urlType中', 1); + } + + //重写路由 + if($urlType != 'normal'){ + $this->$urlType(); + } + //解析url + $url->$urlType(); + + $path = Leguan::get('path'); + //加载模块配置 + $moduleConfig = array($path->appPath, $url->m, 'Config', 'etc.php'); + $moduleConfig = implode($path->ds, $moduleConfig); + if(file_exists($moduleConfig)){ + $config->load(require $moduleConfig); + } + + //开启session + if($config->sessionStart){ + $session = new \Leguan\Session\Session(); + Leguan::set('session',$session); + $session->start(); + } + + //加载控制器 + $controllerName = "\\{$url->m}\\Controller\\{$url->c}Controller"; + + $controller = new $controllerName(); + $actionName = "{$url->a}Action"; + + $defaultAction = 'defaultAction'; + if (is_callable(array($controller, $actionName))) { + $controller->$actionName(); + } elseif (is_callable(array($controller, $defaultAction))) { + $controller->$defaultAction(); + } else { + $this->defaultAction($controllerName, $actionName); + } + + } + + /** + * 未知路由处理函数 + */ + public function defaultAction($controllerName, $actionName){ + exit("类{$controllerName} {$actionName}方法不存在或不是public"); + } + + /** + * 重写路由 - pathInfo + */ + public function pathInfo() + { + if(!isset($_SERVER['PATH_INFO'])){ + $_SERVER['PATH_INFO'] = ''; + } + + $_SERVER['PATH_INFO'] = $this->_getUrl($_SERVER['PATH_INFO']); + } + + /** + * 重写路由 - rewrite + */ + public function rewrite() + { + $this->clean(); + } + + /** + * 重写路由 - clean + */ + public function clean() + { + $cleanKey = Leguan::get('url')->getCleanKey(); + + if(isset($_GET[$cleanKey])){ + $_GET[$cleanKey] = $this->_getUrl($_GET[$cleanKey]); + } + } + + /** + * 获取重写后的url + * + * @param $url string + * @return string + */ + private function _getUrl($url = '') + { + if(empty($url)){ + return ''; + } + + foreach ($this->_routing as $value) { + $from = preg_replace("/(\\$\d)/", '([a-zA-Z_0-9\/])', $value['from']); + if(preg_match("|{$from}|i", $url)){ + return preg_replace("|{$from}|i", "{$value['to']}", $url); + } + } + + return $url; + } + +} \ No newline at end of file diff --git a/library/Leguan/Routing/etc.php b/library/Leguan/Routing/etc.php new file mode 100644 index 0000000..356116e --- /dev/null +++ b/library/Leguan/Routing/etc.php @@ -0,0 +1,13 @@ +'test/$1','to'=>'Index/Index/Index/$1'), + array('from'=>'demo/$1','to'=>'Index/Index/Index/$1') + ); \ No newline at end of file diff --git a/library/Leguan/Session/Session.php b/library/Leguan/Session/Session.php new file mode 100644 index 0000000..1675bca --- /dev/null +++ b/library/Leguan/Session/Session.php @@ -0,0 +1,141 @@ +get($name); + } + + public function __set($name, $value) + { + $this->set($name, $value); + } + } \ No newline at end of file diff --git a/library/Leguan/Sql/Adapter/Mysql.php b/library/Leguan/Sql/Adapter/Mysql.php new file mode 100644 index 0000000..2adeb83 --- /dev/null +++ b/library/Leguan/Sql/Adapter/Mysql.php @@ -0,0 +1,290 @@ +dbEngine); + $escape = "dbEscape{$dbEngine}"; + + if($config->$escape !== null){ + //设置表、字段转义字符 + $this->_escape = $config->$escape; + } + + $this->_db = Leguan::get('db'); + } + + protected function _init() + { + $this->_where = array(); + $this->_field = array(); + $this->_values = array(); + $this->_input = array(); + $this->_table = ''; + } + + /** + * 加添where条件数组 + * + * @param $condition array + * @return void + */ + public function where($condition) + { + $this->_where = array_merge($this->_where, $condition); + + return $this; + } + + /** + * 添加查询的字段信息 + * + * @param $fields + * @return void + */ + public function field($fields) + { + $this->_field = $fields; + + return $this; + } + + /** + * 添加新增的数据 + * + * @param $values + * @return void + */ + public function values($values) + { + $this->_values = array_merge($this->_values, $values); + + return $this; + } + + /** + * 设置操作的表名 + * + * @param $table + * @return void + */ + public function table($table) + { + $this->_table = $table; + + return $this; + } + + /** + * 获取添加数据的sql + */ + public function add($isQuery = true) + { + $sql = "insert into ". $this->_save() . ";"; + + if ($isQuery) { + return $this->_db->query($sql, $this->_input); + } + + return $sql; + } + + /** + * 限制操作数 + */ + public function limit($limit) + { + $this->_limit = $limit; + } + + /** + * 获取新增和修改 公共部分sql + */ + private function _save() + { + if(empty($this->_values)){ + return ''; + } + + $values = ''; + foreach ($this->_values as $key => $value) { + array_push($this->_input, $value); + $values .= "{$this->_escape[0]}". $this->filter($key) ."{$this->_escape[1]} = ?,"; + } + $values = rtrim($values,','); + + return "{$this->_escape[0]}@_{$this->_table}{$this->_escape[1]} set {$values}"; + } + + /** + * 获取删除数据的sql + */ + public function del($isQuery = true) + { + //防止意外删除全部数据 + if(empty($this->_where)){ + return ''; + } + + return $this->delAll($isQuery); + } + + /** + * 删除全部数据 + */ + public function delAll($isQuery = true) + { + $sql = "delete @_{$this->_table} where ".$this->_where().";"; + + if ($isQuery) { + return $this->_db->query($sql, $this->_input); + } + + return $sql; + } + + /** + * 获取修改数据的sql + */ + public function update($isQuery = true) + { + //防止意外修改全部数据 + if(empty($this->_where)){ + return ''; + } + + return $this->updateAll($isQuery); + } + + /** + * 修改全部数据 + */ + public function updateAll($isQuery = true) + { + $sql = "update " . $this->_save() . " where " . $this->_where() .";"; + + if ($isQuery) { + return $this->_db->query($sql, $this->_input); + } + + return $sql; + } + + /** + * 获取查询数据的sql + */ + public function select($isQuery = true) + { + $fields = ''; + if (empty($this->_field)) { + $fields = '*'; + } elseif (is_array($this->_field)) { + foreach ($this->_field as $value) { + $fields .= "{$this->_escape[0]}{$value}{$this->_escape[1]},"; + } + + $fields = rtrim($fields, ','); + } else { + $fields = $this->_field; + } + + $sql = "select {$fields} from @_{$this->_table} where ".$this->_where(); + if (!empty($this->_limit)) { + $sql .= " limit {$this->_limit}"; + } + $sql = "{$sql};"; + + if ($isQuery) { + return $this->_db->query($sql, $this->_input); + } + + return $sql; + } + + /** + * 获取where条件字符串 + * + * @return string + */ + private function _where() + { + if(empty($this->_where)){ + return '1=1'; + } + + $where = ''; + //array('age' => array('>','20','or')) == or age > 20 + foreach ($this->_where as $key => $value) { + $condition = isset($value[2]) ? strtolower($value[2]) : 'and'; + $operator = is_array($value) ? $value[0] : '='; + $placeholder = '?'; + + //in (?,?,?) + if ($operator == 'in') { + $placeholder = '('; + foreach ($value[1] as $in) { + $placeholder .= "?,"; + array_push($this->_input, $in); + } + $placeholder = rtrim($placeholder,','); + $placeholder .= ')'; + } else { + if (is_array($value)) { + array_push($this->_input, $value[1]); + } else { + array_push($this->_input, $value); + } + } + + $where .= " {$condition} {$this->_escape[0]}". $this->filter($key) ."{$this->_escape[1]} {$operator} {$placeholder}"; + } + + $where = ltrim($where, ' and '); + $where = ltrim($where, ' or '); + + return $where; + } + + /** + * key过滤 + */ + public function filter($key) + { + if(preg_match("|^[0-9a-zA-Z_]*$|", $key)){ + return $key; + } + + return ''; + } + } \ No newline at end of file diff --git a/library/Leguan/Sql/Sql.php b/library/Leguan/Sql/Sql.php new file mode 100644 index 0000000..cf983a1 --- /dev/null +++ b/library/Leguan/Sql/Sql.php @@ -0,0 +1,35 @@ +dbEngine; + $dbEngine = ucwords($dbEngine); + + $class = "\\Leguan\\Sql\\Adapter\\{$dbEngine}"; + $this->_obj = new $class(); + } + + public function __call($name, $arguments) + { + return $this->_obj->$name(implode(',', $arguments)); + } +} \ No newline at end of file diff --git a/library/Leguan/Url/Url.php b/library/Leguan/Url/Url.php new file mode 100644 index 0000000..6dc4ef7 --- /dev/null +++ b/library/Leguan/Url/Url.php @@ -0,0 +1,317 @@ +get($name); + } + + + public function __set($name, $value) + { + $this->set($name, $value); + } + + /** + * 修改clean key + * + * @param $key string + * @return void + */ + public function setCleanKey($key) + { + $this->_cleanKey = $key; + } + + /** + * 获取url clean key + * + * @return string + */ + public function getCleanKey() + { + return $this->_cleanKey; + } + + /** + * 获取url + * + * @return array + */ + public function getUrlArr() + { + return $this->_url; + } + + /** + * 获取url类型 + * + * @param $type int + * @return mixed + */ + public function getUrlType($type) + { + if(isset($this->_urlType[$type])){ + return $this->_urlType[$type]; + } + + return null; + } + + /** + * 设置路径键值信息 + * + * @param $name string + * @param $value string + * @return void + */ + public function set($name, $value) + { + $this->_url[$name] = $value; + } + + /** + * 获取路径中的键值信息 + * + * @param $name string + * @return string + */ + public function get($name) + { + if(isset($this->_url[$name])){ + return $this->_url[$name]; + } + + return null; + } + + /** + * url解析 - normal + */ + public function normal() + { + $config = Leguan::get('config'); + !isset($_GET['m']) && $_GET['m'] = $config->defaultModule; + !isset($_GET['c']) && $_GET['c'] = $config->defaultController; + !isset($_GET['a']) && $_GET['a'] = $config->defaultAction; + + foreach ($_GET as $key => $value) { + $this->_url[$key] = $value; + } + + $this->_ucwords(); + } + + /** + * url解析 - pathinfo + */ + public function pathInfo() + { + if(!isset($_SERVER['PATH_INFO'])){ + $_SERVER['PATH_INFO'] = ''; + } + + $this->parseUrl($_SERVER['PATH_INFO']); + } + + /** + * url解析 - rewrite + */ + public function rewrite() + { + $this->clean(); + } + + /** + * url解析 - clean + */ + public function clean() + { + if(!isset($_GET[$this->_cleanKey])){ + $_GET[$this->_cleanKey] = ''; + } + + $this->parseUrl($_GET[$this->_cleanKey]); + } + + /** + * url解析 + * + * @param $path string + */ + public function parseUrl($path = '') + { + $pathArr = $this->_getPathArr($path); + + $this->_url['m'] = $pathArr[0]; + $this->_url['c'] = $pathArr[1]; + $this->_url['a'] = $pathArr[2]; + $this->_ucwords(); + //去除$pathArr前3个成员 + array_splice($pathArr,0,3); + + $pathKeys = array(); + $pathValues = array(); + foreach ($pathArr as $key => $value) { + if($key % 2 == 0){ + array_push($pathKeys, $value); + }else{ + array_push($pathValues, $value); + } + } + + if($pathKeys || $pathValues){ + $pathCombine = array_combine($pathKeys, $pathValues); + //把url转换成数组保存 + $this->_url = array_merge($this->_url,$pathCombine); + } + } + + /** + * 获取path补全m a c后的默认值后的array + */ + private function _getPathArr($path) + { + $config = Leguan::get('config'); + $urlExtension = $config->urlExtension; + $urlLimiter = $config->urlLimiter; + + $path = trim($path); + $path = trim($path,'/'); + $path = trim($path,$urlLimiter); + $path = rtrim($path,$urlExtension); + if (empty($path)) { + $pathArr = array(); + } else { + $pathArr = explode($urlLimiter, $path); + } + + //给url补全缺省m c a + empty($pathArr[0]) && array_push($pathArr, $config->defaultModule); + empty($pathArr[1]) && array_push($pathArr, $config->defaultController); + empty($pathArr[2]) && array_push($pathArr, $config->defaultAction); + + return $pathArr; + } + + /** + * m c a 大小写处理 + */ + private function _ucwords() + { + $this->_url['m'] = ucwords(strtolower($this->_url['m'])); + $this->_url['c'] = ucwords(strtolower($this->_url['c'])); + $this->_url['a'] = strtolower($this->_url['a']); + } + + /** + * 获取url + * + * @param $path string + * @return string + */ + public function getUrl($path, $hasDomain = false) + { + $config = Leguan::get('config'); + $urlType = $this->getUrlType($config->urlType); + $urlType = ucwords($urlType); + $action = "_get{$urlType}Url"; + $url = ''; + + if ($hasDomain) { + $request = Leguan::get('request'); + $url = $request->getDomain(); + } + + $url = $url . $this->$action($path); + + return $url; + } + + private function _getNormalUrl($path) + { + $pathArr = $this->_getPathArr($path); + $urlArr['m'] = $pathArr[0]; + $urlArr['c'] = $pathArr[1]; + $urlArr['a'] = $pathArr[2]; + array_splice($pathArr,0,3); + + $pathKeys = array(); + $pathValues = array(); + foreach ($pathArr as $key => $value) { + if($key % 2 == 0){ + array_push($pathKeys, $value); + }else{ + array_push($pathValues, $value); + } + } + + if($pathKeys || $pathValues){ + $pathCombine = array_combine($pathKeys, $pathValues); + $urlArr = array_merge($urlArr,$pathCombine); + } + + $url = '?'; + foreach ($urlArr as $key => $value) { + $url .= "{$key}={$value}&"; + } + + return "/index.php".rtrim($url,'&'); + } + + private function _getPathInfoUrl($path) + { + return "/index.php".$this->_getPathUrl($path); + } + + private function _getRewriteUrl($path) + { + return $this->_getPathUrl($path); + } + + private function _getCleanUrl($path) + { + return "/index.php?{$this->_cleanKey}=".$this->_getPathUrl($path); + } + + private function _getPathUrl($path) + { + $pathArr = $this->_getPathArr($path); + $urlArr = array(); + array_unshift($urlArr, array_pop($pathArr)); + array_unshift($urlArr, array_pop($pathArr)); + array_unshift($urlArr, array_pop($pathArr)); + + foreach ($pathArr as $key => $value) { + array_push($urlArr, $key); + array_push($urlArr, $value); + } + + $config = Leguan::get('config'); + + return "/".implode('/', $urlArr).$config->urlExtension; + } + +} \ No newline at end of file diff --git a/library/Leguan/View/View.php b/library/Leguan/View/View.php new file mode 100644 index 0000000..fb28b79 --- /dev/null +++ b/library/Leguan/View/View.php @@ -0,0 +1,334 @@ +config; + $viewLimiter = $config->viewLimiter; + + $this->_lLimiter = $viewLimiter[0]; + $this->_rLimiter = $viewLimiter[1]; + $this->_escape = $config->viewEscape; + } + + /** + * 加载模板 + */ + public function display($path = '') + { + $config = $this->config; + $cache = $this->_cache($path); + // $this->debug->dumpText($this->getContent($path)); + // exit; + + if($config->isDebug || !file_exists($cache)){ + $cacheDir = dirname($cache); + + if(!file_exists($cacheDir) && + !mkdir($cacheDir, 0, true)){ + die('创建目录失败 {$cacheDir}'); + } + + file_put_contents($cache, $this->getContent($path)); + } + + extract($this->_data); + require $cache; + } + + /** + * 模板渲染输出 + * + * @param $path string + */ + public function getContent($path = '') + { + $view = $this->_view($path); + + if(!file_exists($view)){ + return "{$view}不存在"; + } + + $content = file_get_contents($view); + return $this->_compile($content); + } + + /** + * 获取view路径 + */ + private function _view($path) + { + return $this->_filename($path); + } + + /** + * 获取view 缓存路径 + */ + private function _cache($path) + { + return $this->_filename($path, true); + } + + /** + * 获取模板路径 + * + * @param $path string + * @param $isCache bool + * @return string + */ + private function _filename($path = '',$isCache = false) + { + $path = ltrim($path,'/'); + + if (empty($path)) { + $path = array(); + } elseif(strpos($path, '/') === false) { + $path = array($path); + } else { + $path = explode('/', $path); + } + + $url = $this->url; + count($path) == 0 && array_unshift($path, $url->a); + count($path) == 1 && array_unshift($path, $url->c); + count($path) == 2 && array_unshift($path, $url->m); + + $a = ucwords(array_pop($path)); + $c = ucwords(array_pop($path)); + $m = ucwords(array_pop($path)); + + $path = $this->path; + $config = $this->config; + + if ($isCache) { + $filename = array($path->view, $config->theme, 'Cache', $m, $c, $a . $config->viewExtension); + } else { + $filename = array($path->view, $config->theme, $m, $c, $a . $config->viewExtension); + } + + $filename = implode($path->ds, $filename); + + return $filename; + } + + /** + * 获取js css 文件目录 + */ + public function skin() + { + $path = $this->path; + $config = $this->config; + + return $path->view. $path->ds . $config->theme . $path->ds . 'Skin'; + } + + /** + * 给模板分配变量 + */ + public function assign($name, $value) + { + $this->_data[$name] = $value; + } + + public function __set($name, $value) + { + $this->assign($name, $value); + } + + /** + * 解析模板内容 + */ + private function _compile($content) + { + $content = $this->_extends($content); + $content = $this->_removeArea($content); + + $content = $this->_securityOutput($content); + $content = $this->_normalOutput($content); + $content = $this->_include($content); + $content = $this->_foreach($content); + $content = $this->_while($content); + $content = $this->_if($content); + + $content = $this->_dump($content); + + $content = $this->_php($content); + return $content; + } + + /** + * 模板解析 - 模板继承 + */ + private function _extends($content) + { + //获取继承的目标path + $matches = array(); + $pattern = "/{$this->_lLimiter}extends (.*?){$this->_rLimiter}/"; + preg_match($pattern, $content, $matches); + if(!isset($matches[1])){ + return $content; + } + + $parentView = $this->_view($matches[1]); + if(!file_exists($parentView)){ + return "{$parentView}不存在"; + } + + $parentContent = file_get_contents($parentView); + //找出子模板中的 area + $pattern = "|{$this->_lLimiter}area (.*?){$this->_rLimiter}(.*?){$this->_lLimiter}/area{$this->_rLimiter}|s"; + preg_match_all($pattern, $content, $matches); + if(!isset($matches[1])){ + return $parentContent; + } + + //把子模板中的area替换掉在父模板中对应的area + foreach ($matches[1] as $key => $value) { + $pattern = "|{$this->_lLimiter}area {$value}{$this->_rLimiter}(.*?){$this->_lLimiter}/area{$this->_rLimiter}|s"; + $replacement = "{$this->_lLimiter}area {$value}{$this->_rLimiter} + {$matches[2][$key]} + {$this->_lLimiter}/area{$this->_rLimiter}"; + $parentContent = preg_replace($pattern, $replacement, $parentContent); + } + + //如果还有上级继承 就递归 + $pattern = "/{$this->_lLimiter}extends (.*?){$this->_rLimiter}/"; + if(preg_match($pattern, $parentContent)){ + return $this->_extends($parentContent); + } + return $parentContent; + } + + /** + * 模板解析 - 移除area包裹 + */ + private function _removeArea($content) + { + $pattern = "|{$this->_lLimiter}area (.*?){$this->_rLimiter}(.*?){$this->_lLimiter}/area{$this->_rLimiter}|s"; + return preg_replace_callback($pattern, function($matches){ + return $matches[2]; + }, $content); + } + + /** + * 模板解析 - 安全输出 + * {=$script} == + */ + private function _securityOutput($content) + { + $pattern = "/{$this->_lLimiter}=(.*?){$this->_rLimiter}/"; + $replacement = "_escape}($1); ?>"; + return preg_replace($pattern, $replacement, $content); + } + + /** + * 模板解析 - 正常输出 + * {!$script} == + */ + private function _normalOutput($content) + { + $pattern = "/{$this->_lLimiter}!(.*?){$this->_rLimiter}/"; + $replacement = ""; + return preg_replace($pattern, $replacement, $content); + } + + /** + * 模板解析 - 加载模板 + */ + private function _include($content) + { + $pattern = "/{$this->_lLimiter}include (.*?){$this->_rLimiter}/"; + $_this = $this; + $content = preg_replace_callback($pattern, function($matches) use($_this){ + return $_this->getContent($matches[1]); + }, $content); + + return $content; + } + + /** + * 模板解析 - foreach + */ + private function _foreach($content) + { + $pattern = "/{$this->_lLimiter}(foreach.*?){$this->_rLimiter}/"; + $replacement = ""; + $content = preg_replace($pattern, $replacement, $content); + $pattern = "|{$this->_lLimiter}/foreach{$this->_rLimiter}|"; + $replacement = ""; + return preg_replace($pattern, $replacement, $content); + } + + /** + * 模板解析 - while + */ + private function _while($content) + { + $pattern = "/{$this->_lLimiter}(while.*?){$this->_rLimiter}/"; + $replacement = ""; + $content = preg_replace($pattern, $replacement, $content); + $pattern = "|{$this->_lLimiter}/while{$this->_rLimiter}|"; + $replacement = ""; + return preg_replace($pattern, $replacement, $content); + } + + /** + * 模板解析 - if + */ + private function _if($content) + { + $pattern = "/{$this->_lLimiter}(if.*?){$this->_rLimiter}/"; + $replacement = ""; + $content = preg_replace($pattern, $replacement, $content); + $pattern = "|{$this->_lLimiter}else{$this->_rLimiter}|"; + $replacement = ""; + $content = preg_replace($pattern, $replacement, $content); + $pattern = "/{$this->_lLimiter}(elseif|else if)(.*?){$this->_rLimiter}/"; + $replacement = ""; + $content = preg_replace($pattern, $replacement, $content); + $pattern = "|{$this->_lLimiter}/if{$this->_rLimiter}|"; + $replacement = ""; + return preg_replace($pattern, $replacement, $content); + } + + /** + * 模板解析 - php + */ + private function _php($content) + { + $pattern = "/{$this->_lLimiter}(.*?){$this->_rLimiter}/"; + $replacement = ""; + return preg_replace($pattern, $replacement, $content); + } + + /** + * 变量调试 + */ + private function _dump($content) + { + $pattern = "/{$this->_lLimiter}dump\((.*?)\){$this->_rLimiter}/"; + $replacement = "debug->dump($1); ?>"; + return preg_replace($pattern, $replacement, $content); + } + +} diff --git a/library/Usheweb/Chapter/Chapter.php b/library/Usheweb/Chapter/Chapter.php new file mode 100644 index 0000000..85658ea --- /dev/null +++ b/library/Usheweb/Chapter/Chapter.php @@ -0,0 +1,172 @@ +_width = $width; + $this->_height = $height; + $this->_len = $len; + $this->_fontSize = $fontSize; + + $this->setFont('elephant.ttf'); + } + + /** + * 设置图片宽度 + * + * @param $width int + * @return void + */ + public function setWidth($width) + { + $this->_width = $width; + } + + /** + * 设置图片高度 + * + * @param $height int + * @return void + */ + public function setHeight($height) + { + $this->_height = $height; + } + + /** + * 设置验证码字符数 + * + * @param $len int + * @return void + */ + public function setLen($len) + { + $this->_len = $len; + } + + /** + * 设置字体尺寸 + * + * @param $fontSize int + * @return void + */ + public function setFontSize($fontSize) + { + $this->_fontSize = $fontSize; + } + + /** + * 设置字体 + * + * @param $font string + * @return void + */ + public function setFont($font) + { + $this->_font = $font; + } + + /** + * 显示验证码 + */ + public function show() + { + $this->_createBg(); + $this->_createCode(); + $this->_createLine(); + $this->_createFont(); + $this->_outPut(); + } + + /** + * 获取验证码 + */ + public function getChapter() + { + return strtolower($this->_code); + } + + //生成随机码 + private function _createCode() + { + $_len = strlen($this->_charset)-1; + for ($i=0;$i<$this->_len;$i++) { + $this->_code .= $this->_charset[mt_rand(0,$_len)]; + } + } + + //生成背景 + private function _createBg() + { + $this->_img = imagecreatetruecolor($this->_width, $this->_height); + $color = imagecolorallocate($this->_img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); + imagefilledrectangle($this->_img,0,$this->_height,$this->_width,0,$color); + } + + //生成文字 + private function _createFont() + { + $_x = $this->_width / $this->_len; + for ($i=0;$i<$this->_len;$i++) { + $this->_fontcolor = imagecolorallocate($this->_img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); + + //判断是否支持 FreeType + $gdInfo = gd_info(); + if(isset($gdInfo['FreeType Support']) + && $gdInfo['FreeType Support'] === true){ + $font = array(dirname(__FILE__), 'font', $this->_font); + $font = implode(DIRECTORY_SEPARATOR, $font); + + imagettftext($this->_img,$this->_fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->_height / 1.4,$this->_fontcolor,$font,$this->_code[$i]); + }else{ + imagechar($this->_img, mt_rand(3,30), $_x*$i+mt_rand(5,15), mt_rand(0,$this->_height/2), $this->_code[$i], $this->_fontcolor); + } + } + } + + //生成线条、雪花 + private function _createLine() + { + for ($i=0;$i<6;$i++) { + $color = imagecolorallocate($this->_img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); + imageline($this->_img,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color); + } + for ($i=0;$i<100;$i++) { + $color = imagecolorallocate($this->_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); + imagestring($this->_img,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$color); + } + } + + //输出 + private function _outPut() + { + header('Content-type:image/png'); + imagepng($this->_img); + imagedestroy($this->_img); + } +} \ No newline at end of file diff --git a/library/Usheweb/Chapter/font/elephant.ttf b/library/Usheweb/Chapter/font/elephant.ttf new file mode 100644 index 0000000..2763d31 Binary files /dev/null and b/library/Usheweb/Chapter/font/elephant.ttf differ diff --git a/library/Usheweb/Upload/Mime.php b/library/Usheweb/Upload/Mime.php new file mode 100644 index 0000000..9ec055f --- /dev/null +++ b/library/Usheweb/Upload/Mime.php @@ -0,0 +1,75 @@ + 'application/zip', + 'rar' => 'application/x-rar-compressed', + 'tar' => 'application/x-tar', + 'gz' => 'application/x-gzip', + 'jpg' => 'image/jpeg', + 'jpeg' => 'image/jpeg', + 'jpe' => 'image/jpeg', + 'jpz' => 'image/jpeg', + 'gif' => 'image/gif', + 'ifm' => 'image/gif', + 'bmp' => 'application/x-MS-bmp', + 'png' => 'image/png', + 'mp3' => 'audio/x-mpeg', + 'mp4' => 'video/mp4', + 'mpe' => 'video/mpeg', + 'mpeg' => 'video/mpeg', + 'mpg' => 'video/mpeg', + 'mpg4' => 'video/mp4', + 'rmvb' => 'audio/x-pn-realaudio', + 'doc' => 'application/msword', + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'xls' => 'application/vnd.ms-excel', + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'ppt' => 'application/vnd.ms-powerpoint', + 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', + 'htm' => 'text/html', + 'html' => 'text/html', + 'txt' => 'text/plain', + ); + /** + * 获取文件扩展 + */ + public static function getExtension($mime) + { + $data = array_flip(self::$_data); + if (isset($data[$mime])) { + return $data[$mime]; + } + + return null; + } + + /** + * 获取文件Mime + */ + public static function getMime($extension) + { + $data = self::$_data; + if (isset($data[$extension])) { + return $data[$extension]; + } + + return null; + } + } \ No newline at end of file diff --git a/library/Usheweb/Upload/Upload.php b/library/Usheweb/Upload/Upload.php new file mode 100644 index 0000000..70b11b8 --- /dev/null +++ b/library/Usheweb/Upload/Upload.php @@ -0,0 +1,169 @@ +_fileSize = $config->fileMaxSize; + $this->_fileExtension = $config->fileExtension; + + $path = Leguan::get('path'); + $this->_fileDir = $path->upload; + } + + /** + * 设置上传文件表单name + * + * @param $name string + * @return void + */ + public function setFormName($name) + { + $this->_formName = $name; + } + + /** + * 设置上传文件大小 + * + * @param $size int + * @return void + */ + public function setFileSize($size) + { + $this->_fileSize = $size; + } + + /** + * 设置上传文件所在目录 + * + * @param $fileDir string + * @return void + */ + public function setFileDir($fileDir) + { + $this->_fileDir = $fileDir; + } + + /** + * 设置上传文件允许扩展名 + * + * @param $fileExtention array + * @return void + */ + public function setFileExtention($fileExtention) + { + $this->_fileExtention = $fileExtention; + } + + public function run() + { + //新上传文件所在路径 + $filePath = array(); + + if(!isset($_FILES[$this->_formName])){ + return "上传文件失败,1.表单上传name是否为{$this->_formName} 2.文件不能大于". $this->_fileSize / 1024 / 1024 .'MB'; + } + + //多文件上传 + if (is_array($_FILES[$this->_formName]['error'])) { + foreach ($_FILES[$this->_formName]['error'] as $key => $error) { + if (empty($_FILES[$this->_formName]['name'][$key])) { + continue; + } + + if ($_FILES[$this->_formName]['error'][$key] == UPLOAD_ERR_OK) { + + $file = $_FILES[$this->_formName]; + $result = $this->_exec($file['name'][$key], $file['type'][$key], $file['tmp_name'][$key], $file['error'][$key], $file['size'][$key]); + if (!is_array($result)) { + return $result; + } + + array_push($filePath, array_pop($result)); + } else { + return '文件上传失败'; + } + } + } else { + //单文件上传 + $file = $_FILES[$this->_formName]; + $result = $this->_exec($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); + if(is_array($result)){ + array_push($filePath, array_pop($result)); + } else { + return $result; + } + } + + return $filePath; + } + + /** + * 表单处理 + */ + private function _exec($name, $type, $tmp_name, $error, $size) + { + if ($size > $this->_fileSize) { + return '上传文件失败,文件不能大于' . $this->_fileSize / 1024 / 1024 .'MB'; + } + + $extension = Leguan::get('path')->getExtension($name); + + if (empty($extension)) { + return '文件扩展名不能为空'; + } + + if (!in_array($extension, $this->_fileExtension)) { + return "上传文件失败,文件类型不允许为扩展{$extension}"; + } + + $path = Leguan::get('path'); + $dirName = date('Ym') . $path->ds . date('d'); + $realPath = array($this->_fileDir, $dirName); + $realPath = implode($path->ds, $realPath); + //判断目录是否存在 + if (!file_exists($realPath)) { + mkdir($realPath, 0, true); + } + + $fileName = md5_file($tmp_name).".{$extension}"; + $filePath = array(); + array_push($filePath, $dirName . $path->ds . $fileName); + move_uploaded_file($tmp_name, "{$realPath}{$path->ds}{$fileName}"); + + return $filePath; + } + + /** + * 获取表单最大文件上传尺寸隐藏字段 + */ + public function getFormMaxSize() + { + return ""; + } + + } \ No newline at end of file diff --git a/themes/Default/Cache/Index/Index/Index.php b/themes/Default/Cache/Index/Index/Index.php new file mode 100644 index 0000000..3352b5b --- /dev/null +++ b/themes/Default/Cache/Index/Index/Index.php @@ -0,0 +1,33 @@ +

TOP

+ + + +

+ Hi , Leguan for +

+ +
    + +
  1. ()
  2. + +
+

+ + + + + +

+ +

+ + + \ No newline at end of file diff --git a/themes/Default/Cache/Index/Index/Upload.php b/themes/Default/Cache/Index/Index/Upload.php new file mode 100644 index 0000000..bc3829c --- /dev/null +++ b/themes/Default/Cache/Index/Index/Upload.php @@ -0,0 +1,19 @@ + + + + + 文件上传测试 + + +
+ getFormMaxSize(); ?> +
+ + +
+ + \ No newline at end of file diff --git a/themes/Default/Index/Index/Index.php b/themes/Default/Index/Index/Index.php new file mode 100644 index 0000000..00e48db --- /dev/null +++ b/themes/Default/Index/Index/Index.php @@ -0,0 +1,36 @@ + + + + +

+ Hi , Leguan for +

+ +
    + +
  1. ()
  2. + +
+

+ + + + + +

+ +

+ + +test + + \ No newline at end of file diff --git a/themes/Default/Index/Index/Parent.php b/themes/Default/Index/Index/Parent.php new file mode 100644 index 0000000..91152b5 --- /dev/null +++ b/themes/Default/Index/Index/Parent.php @@ -0,0 +1,13 @@ + +

title

+ +

body

+ + + + +

footer

\ No newline at end of file diff --git a/themes/Default/Index/Index/Test.php b/themes/Default/Index/Index/Test.php new file mode 100644 index 0000000..db03c10 --- /dev/null +++ b/themes/Default/Index/Index/Test.php @@ -0,0 +1 @@ +

include test for

\ No newline at end of file diff --git a/themes/Default/Index/Index/Top.php b/themes/Default/Index/Index/Top.php new file mode 100644 index 0000000..e8513fd --- /dev/null +++ b/themes/Default/Index/Index/Top.php @@ -0,0 +1,4 @@ +

TOP

+ +

body

+ \ No newline at end of file diff --git a/themes/Default/Index/Index/upload.php b/themes/Default/Index/Index/upload.php new file mode 100644 index 0000000..4ebc1c3 --- /dev/null +++ b/themes/Default/Index/Index/upload.php @@ -0,0 +1,19 @@ + + + + + 文件上传测试 + + +
+ +
+ + +
+ + \ No newline at end of file