420 lines
12 KiB
PHP
420 lines
12 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2020-10-30
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\components;
|
||
|
||
use Yii;
|
||
use yii\helpers\Html;
|
||
use yii\helpers\HtmlPurifier;
|
||
use app\models\Option;
|
||
use app\components\Utils;
|
||
|
||
class SiteHelper
|
||
{
|
||
/*
|
||
* 过滤html标签,防止xss的问题
|
||
* $content为原始内容
|
||
*/
|
||
public static function htmlTagFilter($content)
|
||
{
|
||
return HtmlPurifier::process($content);
|
||
}
|
||
|
||
/**
|
||
* 获得站点域名
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteDomain(){
|
||
$value = Option::getOption("SITE_DOMAIN");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteDomain"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点域名
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteWapDomain(){
|
||
$value = Option::getOption("SITE_WAP_DOMAIN");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteWapDomain"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点名称
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteName(){
|
||
$value = Option::getOption("SITE_NAME");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteName"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点简称
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteShortName(){
|
||
$value = Option::getOption("SITE_SHORT_NAME");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteShortName"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点标题
|
||
* 优选使用站点简称,其次使用站点名称
|
||
*/
|
||
public static function getSiteTitle(){
|
||
$value = Option::getOption("SITE_SHORT_NAME");
|
||
if($value == NULL){
|
||
$value = self::getSiteName();
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点一句话介绍
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteBriefIntro(){
|
||
$value = Option::getOption("SITE_BRIEF_INTRO");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteBriefIntro"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点描述
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteDescription(){
|
||
$value = Option::getOption("SITE_DESCRIPTION");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteDescription"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点关键词
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteKeywords(){
|
||
$value = Option::getOption("SITE_KEYWORDS");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteKeywords"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点logo
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteLogo(){
|
||
$value = Option::getOption("SITE_LOGO");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteLogo"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点mini logo
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteMiniLogo(){
|
||
$value = Option::getOption("SITE_MINI_LOGO");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteMiniLogo"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点favicon.ico
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteICO(){
|
||
$value = Option::getOption("SITE_ICO");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteIco"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点ICP
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteICP(){
|
||
$value = Option::getOption("SITE_ICP");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteIcp"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点联系电话
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteTel(){
|
||
$value = Option::getOption("SITE_TEL");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteTel"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点联系地址
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteAddress(){
|
||
$value = Option::getOption("SITE_ADDRESS");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteAddress"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
|
||
/**
|
||
* 获得站点邮箱
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSiteEmail(){
|
||
$value = Option::getOption("SITE_EMAIL");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["siteEmail"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得站点邮编
|
||
* 优选使用数据库配置,其次是使用app\config\params.php配置(Yii::$app->params[])
|
||
*/
|
||
public static function getSitePostcode(){
|
||
$value = Option::getOption("SITE_POSTCODE");
|
||
if($value == NULL){
|
||
$value = Yii::$app->params["sitePostcode"];
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 获得网站自定义配置
|
||
* @param string $key 配置项,默认使用下划线大写SITE_NAME,否则是驼峰式siteName
|
||
*/
|
||
public static function getCustomiseOptionByKey($key,$type="underline"){
|
||
if($type == "hump"){
|
||
$_key = Utils::hump2underline($key);
|
||
$_key = strtoupper($_key);
|
||
$value = Option::getOption($_key);
|
||
if($value == NULL){
|
||
$value = Yii::$app->params[$key];
|
||
}
|
||
} else {
|
||
$value = Option::getOption($key);
|
||
if($value == NULL){
|
||
$_key = strtolower($key);
|
||
$_key = Utils::underline2hump($_key);
|
||
$value = Yii::$app->params[$_key];
|
||
}
|
||
}
|
||
return $value;
|
||
}
|
||
|
||
/**
|
||
* 根据w值获取中文星期几
|
||
* @param int $w date("w",time())
|
||
*/
|
||
public static function weekday2Cn($w, $short=false)
|
||
{
|
||
if($short){
|
||
switch ($w){
|
||
case 0:
|
||
return '周日';
|
||
case 1:
|
||
return '周一';
|
||
case 2:
|
||
return '周二';
|
||
case 3:
|
||
return '周三';
|
||
case 4:
|
||
return '周四';
|
||
case 5:
|
||
return '周五';
|
||
case 6:
|
||
return '周六';
|
||
default :
|
||
return '未知';
|
||
}
|
||
} else {
|
||
switch ($w){
|
||
case 0:
|
||
return '星期日';
|
||
case 1:
|
||
return '星期一';
|
||
case 2:
|
||
return '星期二';
|
||
case 3:
|
||
return '星期三';
|
||
case 4:
|
||
return '星期四';
|
||
case 5:
|
||
return '星期五';
|
||
case 6:
|
||
return '星期六';
|
||
default :
|
||
return '未知';
|
||
}
|
||
}
|
||
}
|
||
|
||
public static function getModelError($model)
|
||
{
|
||
foreach ($model->errors as $errors)
|
||
return [
|
||
'code' => 1,
|
||
'msg' => $errors[0],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 获取资源绝对地址
|
||
* @param string $url 资源绝对地址或相对地址
|
||
* @param string $hostname 主机,默认当前
|
||
* return string
|
||
*/
|
||
public static function getFullUrl($url, $hostname = null)
|
||
{
|
||
$url = trim($url);
|
||
if(strlen($url) == 0){
|
||
return $url;
|
||
}
|
||
if(stripos($url,'http') === 0){
|
||
return $url;
|
||
}
|
||
$url = ltrim($url,'/');
|
||
try{
|
||
$hostname = $hostname != null ? $hostname : \Yii::$app->request->getHostInfo();
|
||
}catch (\Exception $e){
|
||
$hostname = "";
|
||
}
|
||
return $hostname .'/' . $url;
|
||
}
|
||
|
||
/**
|
||
* 获取url相对路径
|
||
* @param string $url 资源绝对地址或相对地址
|
||
* @param string $hostname 主机,默认当前
|
||
* return string
|
||
*/
|
||
public static function getRelativeUrl($url, $hostname = null)
|
||
{
|
||
$url = trim($url);
|
||
$hostname = $hostname != null ? $hostname : \Yii::$app->request->getHostInfo();
|
||
if(stripos($url, $hostname) === 0){
|
||
$start = strlen($hostname);
|
||
return substr($url, $start);
|
||
}
|
||
return $url;
|
||
}
|
||
|
||
/**
|
||
* 修复html内容资源文件地址
|
||
* @param string $content 内容
|
||
* @param string $hostname 主机,默认当前
|
||
* return string
|
||
*/
|
||
public static function repairContent($content, $hostname = null)
|
||
{
|
||
//图片
|
||
preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $content, $imgs);
|
||
foreach ($imgs[1] as $img){
|
||
$ori_img_url = self::getFullUrl($img, $hostname);
|
||
//防止相同被多次替换
|
||
if(strpos($content, $ori_img_url)){
|
||
continue;
|
||
}
|
||
$content = str_replace($img,$ori_img_url,$content);
|
||
}
|
||
//视频
|
||
preg_match_all('#<video.*?src="([^"]*)"[^>]*>#i', $content, $videos);
|
||
foreach ($videos[1] as $video){
|
||
$ori_video_url = self::getFullUrl($video, $hostname);
|
||
//防止相同被多次替换
|
||
if(strpos($content, $ori_video_url)){
|
||
continue;
|
||
}
|
||
$content = str_replace($video,$ori_video_url,$content);
|
||
}
|
||
return $content;
|
||
}
|
||
|
||
|
||
/**
|
||
* UNIX时间戳格式化
|
||
* @param integer $time uinx时间戳
|
||
*/
|
||
public static function unixTimeFormat($time)
|
||
{
|
||
$timestamp = time();
|
||
$dur = $timestamp - $time;
|
||
if($dur < 60){
|
||
return '刚刚';
|
||
} else if($dur < 3600){
|
||
$minute = $dur / 60;
|
||
$minute = floor($minute);
|
||
return Utils::stringFormat('{{}}分钟前', [$minute]);
|
||
} else if($dur < 3600 * 24){
|
||
$hour = $dur / 3600;
|
||
$hour = floor($hour);
|
||
return Utils::stringFormat('{{}}小时前', [$hour]);
|
||
} else if($dur < 86400 * 30){
|
||
$day = $dur / 86400;
|
||
$day = floor($day);
|
||
return Utils::stringFormat('{{}}天前', [$day]);
|
||
} else {
|
||
return date('Y年m月d日',$time);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 目录创建(递归创建)
|
||
*/
|
||
public static function mkDir($dir, $mode = 0777)
|
||
{
|
||
return is_dir($dir) or self::mkDir(dirname($dir), $mode) and mkdir($dir, $mode);
|
||
}
|
||
|
||
public static function isBase64Encode($content)
|
||
{
|
||
try{
|
||
if($content == base64_encode(base64_decode($content)))
|
||
return true;
|
||
} catch (\Exception $ex){
|
||
}
|
||
return false;
|
||
}
|
||
} |