diff --git a/composer.json b/composer.json index f5fbe68..2df1341 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,13 @@ { "name" : "windwork/util", "description" : "windwork util component,Windwork 工具组件", - "version" : "0.3.1", + "version" : "0.4.0", "require" : { "php" : ">=5.5.0", - "windwork/core" : "*", - "windwork/cache" : "*", - "windwork/db" : "*", - "windwork/logger" : "*", - "windwork/storage" : "*", - "windwork/template" : "*", - "windwork/mailer" : "*", - "windwork/web" : "*" + "windwork/core" : "*" }, "license" : "MIT", "autoload" : { - "files" : [ "lib/helper.php" ], "psr-4" : { "wf\\util\\" : "lib" } diff --git a/lib/JSOutput.php b/lib/JSOutput.php deleted file mode 100644 index fb021fa..0000000 --- a/lib/JSOutput.php +++ /dev/null @@ -1,94 +0,0 @@ - - * @link http://docs.windwork.org/manual/wf.web.helper.html - * @since 0.1.0 - */ -class JSOutput { - - /** - * 输出js内容 - * - * @param string $js js程序代码 - * @return string - */ - public static function jsScript($js) { - return "\n"; - } - - /** - * js跳转 - * - * @param string $url - * @param bool $waitTime = 0 等待多时秒 - * @return string - */ - public static function jsLocation($url, $waitTime = 0) { - $url = urldecode(urldecode($url)); - $url = str_replace("'", "\\'", $url); - - if($waitTime) { - $waitTime = $waitTime * 1000; - return static::jsScript("setTimeout(function(){window.location.href='{$url}';}, {$waitTime});"); - } else { - return static::jsScript("window.location.href='{$url}'"); - } - } - - /** - * 把内容转换成提供js的document.write()使用的字符串 - * - * @param string $content - */ - public static function jsWrite($content) { - $search = array("\r\n", "\n", "\r", "\"", ""; - } else { - $ajaxCallback = $req->getRequest('ajax_callback'); - if(!$ajaxCallback) { - $ajaxCallback = $req->getRequest('callback'); - } - - if($ajaxCallback && $ajaxCallback != '?') { - $callback = preg_replace("/[^0-9a-z_\\.]/i", '', $ajaxCallback); - $json = "try{{$callback}({$json});}catch(e){}"; - } - } - - return $json; - } -} \ No newline at end of file diff --git a/lib/Str.php b/lib/Str.php index 5f36253..df7a518 100644 --- a/lib/Str.php +++ b/lib/Str.php @@ -438,4 +438,89 @@ public static function safeString($str) { public static function isEqual($str, $expect) { return strtolower((string)$str) === strtolower((string)$expect); } + + /** + * 转换编码 + * @param string $str + * @param string $from + * @param string $to + */ + public static function convertEncoding($str, $from, $to) + { + if(function_exists('iconv')) { + return iconv($from, $to . '//ignore', $str); + } elseif(function_exists('mb_convert_encoding')) { + return mb_convert_encoding($str, $to, $from); + } + + throw new \Exception('请先安装PHP iconv或mb_string扩展'); + } + + /** + * 获取字符串长度 + * @param string $str + * @param string $encoding = 'UTF-8' + * @throws \Exception + */ + public static function strlen($str, $encoding = 'UTF-8') { + if(function_exists('iconv_strlen')) { + return iconv_strlen($str, $encoding); + } elseif(function_exists('mb_strlen')) { + return mb_strlen($str, $encoding); + } + throw new \Exception('请先安装PHP iconv或mb_string扩展'); + } + + /** + * 截取字符串 + * @param string $str + * @param int $len + * @param int $start + * @param string $encoding = 'UTF-8' + * @throws \Exception + */ + public static function substr($str, $len, $start = 0, $encoding = 'UTF-8') { + if(function_exists('iconv_substr')) { + return iconv_substr($str, $start, $len, $encoding); + } elseif(function_exists('mb_substr')) { + return mb_substr($str, $start, $len, $encoding); + } + throw new \Exception('请先安装PHP iconv或mb_string扩展'); + } + + /** + * 查找字符串第一次出现的位置 + * @param string $haystack + * @param string $needle + * @param int $start = 0 开始查找的位置 + * @param string $encoding = 'UTF-8' + * @throws \Exception + * @return string + */ + public static function strpos($haystack, $needle, $start = 0, $encoding = 'UTF-8') { + if(function_exists('iconv_substr')) { + return iconv_strpos($haystack, $needle, $start, $encoding); + } elseif(function_exists('mb_substr')) { + return mb_strpos($haystack, $needle, $start, $encoding); + } + throw new \Exception('请先安装PHP iconv或mb_string扩展'); + } + + /** + * 查找字符串最后一次出现的位置 + * @param string $haystack + * @param string $needle + * @param int $start = 0 开始查找的位置 + * @param string $encoding = 'UTF-8' + * @throws \Exception + * @return string + */ + public static function strrpos($haystack, $needle, $start = 0, $encoding = 'UTF-8') { + if(function_exists('iconv_substr')) { + return iconv_strrpos($haystack, $needle, $start, $encoding); + } elseif(function_exists('mb_substr')) { + return mb_strrpos($haystack, $needle, $start, $encoding); + } + throw new \Exception('请先安装PHP iconv或mb_string扩展'); + } } \ No newline at end of file diff --git a/lib/Utf8.php b/lib/Utf8.php deleted file mode 100644 index caeee54..0000000 --- a/lib/Utf8.php +++ /dev/null @@ -1,187 +0,0 @@ - 0x80) { - $thisW = substr($gbstr, 0, 2); - $gbstr = substr($gbstr, 2, strlen($gbstr)); - $utf8 = ""; - @$utf8 = self::u2Utf8(hexdec($GBK2UTF8_CODETABLE[hexdec(bin2hex($thisW)) - 0x8080])); - if($utf8 != "") { - for ($i = 0;$i < strlen($utf8);$i += 3) { - $ret .= chr(substr($utf8, $i, 3)); - } - } - } else { - $ret .= substr($gbstr, 0, 1); - $gbstr = substr($gbstr, 1, strlen($gbstr)); - } - } - - return $ret; - } - - - /** - * Unicode字符集转utf8 - * - * @param string $c - * @return string - */ - public static function u2Utf8($c) { - for ($i = 0;$i < count($c);$i++) { - $str = ""; - } - - if ($c < 0x80) { - $str .= $c; - } elseif ($c < 0x800) { - $str .= (0xC0 | $c >> 6); - $str .= (0x80 | $c & 0x3F); - } elseif ($c < 0x10000) { - $str .= (0xE0 | $c >> 12); - $str .= (0x80 | $c >> 6 & 0x3F); - $str .= (0x80 | $c & 0x3F); - } elseif ($c < 0x200000) { - $str .= (0xF0 | $c >> 18); - $str .= (0x80 | $c >> 12 & 0x3F); - $str .= (0x80 | $c >> 6 & 0x3F); - $str .= (0x80 | $c & 0x3F); - } - - return $str; - } - - - /** - * utf8字符集转Unicode - * - * @param string $c - * @return string - */ - public static function utf82U($c) { - switch(strlen($c)) { - case 1: - return ord($c); - case 2: - $n = (ord($c[0]) & 0x3f) << 6; - $n += ord($c[1]) & 0x3f; - return $n; - case 3: - $n = (ord($c[0]) & 0x1f) << 12; - $n += (ord($c[1]) & 0x3f) << 6; - $n += ord($c[2]) & 0x3f; - return $n; - case 4: - $n = (ord($c[0]) & 0x0f) << 18; - $n += (ord($c[1]) & 0x3f) << 12; - $n += (ord($c[2]) & 0x3f) << 6; - $n += ord($c[3]) & 0x3f; - return $n; - } - } - -} - diff --git a/lib/helper.php b/lib/helper.php deleted file mode 100644 index 70aca46..0000000 --- a/lib/helper.php +++ /dev/null @@ -1,298 +0,0 @@ - - * @since 0.1.0 - */ - -/** - * 获取应用实例 - * - * @return \wf\web\Application - */ -function app() { - return \wf\web\Application::app(); -} - -/** - * 应用注入容器 - * @return \wf\core\DIContainer - */ -function di() { - return app()->getDi(); -} - -/** - * 调度器实例 - * - * @return \wf\web\Dispatcher - */ -function dsp() { - return app()->getDispatcher(); -} - -/** - * 读取配置变量,该函数第n个参数分别对应数组第n维下标,最多支持直接访问到第5维 - *
- * // 访问 $this->configs['url']
- * $conf->get('url');
- *
- * // 访问 $this->configs['url']['rewrite']
- * $conf->get('url', 'rewrite');
- *
- * // 访问 $this->configs['url']['rewrite']['login']
- * $conf->get('url', 'alias', 'login');
- * 
- * - * @param string $index0 = null - * @param string $index1 = null - * @param string $index2 = null - * @param string $index3 = null - * @param string $index4 = null - * @return NULL|mixed - */ -function cfg($name = null) { - $cfgObj = app()->getConfig(); - - if ($name === null) { - return $cfgObj->getAll(); - } - - return $cfgObj->get($name); -} - -/** - * 获取语言包中的字符串 - * @param string $key - * @return string - */ -function lang($key) { - return \wf\core\Lang::get($key); -} - -/** - * 获取缓存实例 - * - * @return \wf\cache\ACache - */ -function cache() { - return di()->cache(); -} - -/** - * 写入日志 - * - * 可以在config/config.php中启用日志,所有日志按类别保存 - * @param string $level 日志级别 emergency|alert|critical|error|warning|notice|info|debug - * @param string $message 日志内容,如果是非标量则使用var_export成字符串保存 - */ -function logging($level, $message) { - return di()->logger()->log($level, $message); -} - -/** - * 获取数据库操作实例 - * - * @param string $id = 'default' - * @return \wf\db\IDB - */ -function db($id = 'default') { - return di()->db($id); -} - -/** - * 获取存贮组件实例 - * - * @return \wf\storage\AStorage - */ -function storage() { - return di()->storage(); -} - -/** - * 获取缩略图的URL,一般在模板中使用 - * @param string|ing $path 图片路径或图片附件id - * @param int $width = 100 为0时按高比例缩放 - * @param int $height = 0 为0时按宽比例缩放 - * @return string - */ -function thumb($path, $width = 100, $height = 0) { - return storage()->getThumbUrl($path, $width, $height); -} - -/** - * 根据上传文件的Path获取完整URL - * @param string $path - * @return string - */ -function storageUrl($path) { - return storage()->getFullUrl($path); -} - -/** - * 根据上传文件的Path获取完整URL - * @param string $path - * @return string - */ -function storagePath($url) { - return storage()->getPathFromUrl($url); -} - -/** - * 生成URL - * - * @param string $uri - * @param bool $fullUrl = false 是否获取完整URL - * @return string - */ -function url($uri, $fullUrl = false) { - return dsp()->getRouter()->createUrl($uri, [], $fullUrl); -} - -/** - * 创建记录查询分页导航对象 - * @param int $totals 总记录数 - * @param int $rows = 10 每页显示记录数 - * @param string $tpl = 'simple' 分页 导航模板,mobile)手机分页, simple)简单分页, complex)复杂分页 - */ -function pager($totals, $rows = 10, $tpl = 'simple') { - $pager = new \wf\pager\Pager($totals, $rows, '', ['argSeparator' => '/', 'valSeparator' => ':', 'tpl' => $tpl]); - return $pager; -} - -/** - * 获取记录列表 - * @param array $cdt = [] - * @param int $rows = 10 每页记录数 - * @param string $countField = '*' 统计字段 - * @return array ( - * 'list' => $list, - * 'total' => $total, - * 'pages' => $pager->lastPage, // 总页数 - * 'pager' => instance of \wf\pager\Pager(), - * ) - */ -function modelPager(\wf\model\Model $m, $cdt = [], $rows = 10, $countField = '*') { - $total = $m->find($cdt)->count($countField); - $pager = new \wf\pager\Pager($total, $rows); - - $list = $m->find($cdt)->all($pager->offset, $pager->rows); - - return [ - 'list' => $list, - 'total' => $total, - 'pages' => $pager->lastPage, // 总页数 - 'pager' => $pager, - ]; -} - -/** - * 获取会员头像的url,一般在模板中使用 - * - * @param int $uid - * @param string $type big|medium|small|tiny - * @param bool $reload 浏览时是否重新加载头像 - * @return string - */ -function avatar($uid, $type = 'small', $reload = false) { - $urlExt = cfg('url.rewriteExt'); - // => "storage/avatar/{$type}/{$uid}.jpg" - $avatar = url("system.uploader.load/avatar/{$type}/{$uid}.jpg"); - - $urlExt && $urlExt != '.jpg' && $avatar = preg_replace("/{$urlExt}$/", '', $avatar); - - if($reload) { - static $rand = null; - $rand or $rand = sprintf("%X", mt_rand(0x100000, 0xFFFFFF)); - - $avatar .= "?".$rand; - } - - return $avatar; -} - -/** - * 对请求URL进行解码 - * @param string $str - * @return string||array - */ -function paramDecode($arg) { - if (is_array($arg)) { - foreach ($arg as $key => $val) { - $arg[$key] = paramDecode($val); - } - } else { - $arg = urldecode(urldecode($arg)); - } - return $arg; -} - -/** - * 对请求URL进行编码 - * @param string $arg - * @return string - */ -function paramEncode($arg) { - if (is_array($arg)) { - foreach ($arg as $key => $val) { - $arg[$key] = paramEncode($val); - } - } else { - $arg = urlencode(urlencode(paramDecode($arg))); - } - return $arg; -} - -/** - * 默认异常处理 - * - * @param Exception $e 异常对象 - */ -function exceptionHandler($e) { - $message = $e->getMessage(); - $message = "{$message}"; - - $file = $e->getFile(); - $file = str_replace(ROOT_DIR, '', $file); - $file = ltrim(str_replace('\\', '/', $file), '/'); - - $line = $e->getLine(); - - $trace = $e->getTraceAsString(); - $trace = str_replace(ROOT_DIR, '', $trace); - $trace = "
{$trace}
\n"; - - $message = "
" - . " Exception: ".get_class($e) . "\n
" - . " Message: {$message}\n
" - . " File: {$file}\n
" - . " Line: {$line}" - . " {$trace}\n" - . "
"; - - //@ob_end_clean(); - header('Content-Type: text/html; Charset=utf-8'); - print "
{$message}
\n"; -} - -/** - * 检查页面请求令牌 - * @return bool - */ -function checkToken() { - $hash = dsp()->getRequest()->getRequest('hash'); - - return \wf\util\Csrf::checkToken($hash); -}