222 lines
5.7 KiB
PHP
Executable File
222 lines
5.7 KiB
PHP
Executable File
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
|
// +----------------------------------------------------------------------
|
|
// 应用公共文件
|
|
use think\facade\Session;
|
|
use app\model\Member;
|
|
|
|
define("SENTCMS_VERSION", '4.x');
|
|
|
|
/**
|
|
*
|
|
*/
|
|
function is_login() {
|
|
$user = Session::get('userInfo');
|
|
return $user['uid'];
|
|
}
|
|
|
|
function is_administrator() {
|
|
$user = Session::get('userInfo');
|
|
return (int) $user['uid'] === (int) env('rootuid') ? true : false;
|
|
}
|
|
|
|
function form($field = [], $data = []) {
|
|
return \app\http\form\Form::render($field, $data);
|
|
}
|
|
|
|
/**
|
|
* 广告位广告
|
|
* @param string $name 广告位名称
|
|
* @param array $param 参数
|
|
* @return mixed
|
|
*/
|
|
function ad($name, $param = []){
|
|
return '';
|
|
}
|
|
|
|
function parse_field_bind(){
|
|
|
|
}
|
|
|
|
function time_format($value){
|
|
return date('Y-m-d H:i:s', $value);
|
|
}
|
|
|
|
/**
|
|
* 获取客户端IP地址
|
|
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
|
|
* @param boolean $adv 是否进行高级模式获取(有可能被伪装)
|
|
* @return mixed
|
|
*/
|
|
function get_client_ip($type = 0, $adv = false) {
|
|
$type = $type ? 1 : 0;
|
|
static $ip = NULL;
|
|
if ($ip !== NULL) {
|
|
return $ip[$type];
|
|
}
|
|
|
|
if ($adv) {
|
|
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'];
|
|
}
|
|
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
// IP地址合法验证
|
|
$long = sprintf("%u", ip2long($ip));
|
|
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
|
|
return $ip[$type];
|
|
}
|
|
|
|
/**
|
|
* 根据用户ID获取用户名
|
|
* @param integer $uid 用户ID
|
|
* @return string 用户名
|
|
*/
|
|
function get_username($uid = 0) {
|
|
static $list;
|
|
if (!($uid && is_numeric($uid))) {
|
|
//获取当前登录用户名
|
|
return session('userInfo.username');
|
|
}
|
|
$name = Member::where('uid', $uid)->value('username');
|
|
return $name;
|
|
}
|
|
|
|
function avatar($uid, $size = 'middle') {
|
|
return request()->domain() . '/static/common/images/default_avatar_' . $size . '.jpg';
|
|
}
|
|
|
|
// 分析枚举类型配置值 格式 a:名称1,b:名称2
|
|
function parse_config_attr($string) {
|
|
$array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
|
|
if (strpos($string, ':')) {
|
|
$value = array();
|
|
foreach ($array as $val) {
|
|
list($k, $v) = explode(':', $val);
|
|
$value[$k] = $v;
|
|
}
|
|
} else {
|
|
$value = $array;
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
function mk_dir($dir, $mode = 0755) {
|
|
if (is_dir($dir) || @mkdir($dir, $mode, true)) {
|
|
return true;
|
|
}
|
|
|
|
if (!mk_dir(dirname($dir), $mode, true)) {
|
|
return false;
|
|
}
|
|
|
|
return @mkdir($dir, $mode, true);
|
|
}
|
|
|
|
/**
|
|
* 字符串转换为数组,主要用于把分隔符调整到第二个参数
|
|
* @param string $str 要分割的字符串
|
|
* @param string $glue 分割符
|
|
* @return array
|
|
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
|
|
*/
|
|
function str2arr($str = '', $glue = ',') {
|
|
if ($str) {
|
|
return explode($glue, $str);
|
|
} else {
|
|
return array();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 数组转换为字符串,主要用于把分隔符调整到第二个参数
|
|
* @param array $arr 要连接的数组
|
|
* @param string $glue 分割符
|
|
* @return string
|
|
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
|
|
*/
|
|
function arr2str($arr = array(), $glue = ',') {
|
|
if (empty($arr)) {
|
|
return '';
|
|
} else {
|
|
return implode($glue, $arr);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 格式化字节大小
|
|
* @param number $size 字节数
|
|
* @param string $delimiter 数字和单位分隔符
|
|
* @return string 格式化后的带单位的大小
|
|
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
|
|
*/
|
|
function format_bytes($size, $delimiter = '') {
|
|
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
|
|
for ($i = 0; $size >= 1024 && $i < 5; $i++) {
|
|
$size /= 1024;
|
|
}
|
|
|
|
return round($size, 2) . $delimiter . $units[$i];
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取附件信息
|
|
* @param int $cover_id
|
|
* @param string $field
|
|
* @return 完整的数据 或者 指定的$field字段值
|
|
*/
|
|
function get_attach($id, $field = null) {
|
|
$basePath = request()->domain();
|
|
if (empty($id)) {
|
|
return $basePath . '/static/images/default.png';
|
|
}
|
|
$picture = \think\facade\Db::name('Attach')->where(array('id' => $id))->find();
|
|
if ($field == 'path') {
|
|
if (!empty($picture['url'])) {
|
|
$picture['path'] = $picture['url'] ? $$basePath . $picture['url'] : $$basePath . '/static/images/default.png';
|
|
} else {
|
|
$picture['path'] = $picture['path'] ? $$basePath . $picture['path'] : $$basePath . '/static/images/default.png';
|
|
}
|
|
}
|
|
return empty($field) ? $picture : $picture[$field];
|
|
}
|
|
|
|
/**
|
|
* 获取文档封面图片
|
|
* @param int $cover_id
|
|
* @param string $field
|
|
* @return 完整的数据 或者 指定的$field字段值
|
|
* @author huajie <banhuajie@163.com>
|
|
*/
|
|
function get_cover($cover_id, $field = null) {
|
|
if (empty($cover_id)) {
|
|
return BASE_PATH . '/static/images/default.png';
|
|
}
|
|
$base_path = "";
|
|
$picture = \think\facade\Db::name('Picture')->where(array('status' => 1, 'id' => $cover_id))->find();
|
|
if ($field == 'path') {
|
|
if (!empty($picture['url'])) {
|
|
$picture['path'] = $picture['url'] ? $base_path . $picture['url'] : $base_path . '/static/images/default.png';
|
|
} else {
|
|
$picture['path'] = $picture['path'] ? $base_path . $picture['path'] : $base_path . '/static/images/default.png';
|
|
}
|
|
}
|
|
return empty($field) ? $picture : $picture[$field];
|
|
} |