2021-01-26 13:41:01 +00:00
< ? php
/* 获取主题当前版本号 */
function _getVersion ()
{
2021-03-07 13:55:28 +00:00
return " 5.6.6 " ;
2021-01-26 13:41:01 +00:00
};
/* 判断是否是手机 */
function _isMobile ()
{
2021-02-21 09:45:35 +00:00
if ( isset ( $_SERVER [ 'HTTP_X_WAP_PROFILE' ]))
return true ;
if ( isset ( $_SERVER [ 'HTTP_VIA' ])) {
return stristr ( $_SERVER [ 'HTTP_VIA' ], " wap " ) ? true : false ;
}
if ( isset ( $_SERVER [ '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' , 'openwave' , 'nexusone' , 'cldc' , 'midp' , 'wap' , 'mobile' );
if ( preg_match ( " /( " . implode ( '|' , $clientkeywords ) . " )/i " , strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ])))
return true ;
}
if ( isset ( $_SERVER [ 'HTTP_ACCEPT' ])) {
if (( strpos ( $_SERVER [ 'HTTP_ACCEPT' ], 'vnd.wap.wml' ) !== false ) && ( strpos ( $_SERVER [ 'HTTP_ACCEPT' ], 'text/html' ) === false || ( strpos ( $_SERVER [ 'HTTP_ACCEPT' ], 'vnd.wap.wml' ) < strpos ( $_SERVER [ 'HTTP_ACCEPT' ], 'text/html' )))) {
return true ;
}
}
return false ;
2021-01-26 13:41:01 +00:00
}
/* 根据评论agent获取浏览器类型 */
function _getAgentBrowser ( $agent )
{
2021-02-21 09:45:35 +00:00
if ( preg_match ( '/MSIE\s([^\s|;]+)/i' , $agent , $regs )) {
$outputer = 'Internet Explore' ;
} else if ( preg_match ( '/FireFox\/([^\s]+)/i' , $agent , $regs )) {
$outputer = 'FireFox' ;
} else if ( preg_match ( '/Maxthon([\d]*)\/([^\s]+)/i' , $agent , $regs )) {
$outputer = 'MicroSoft Edge' ;
} else if ( preg_match ( '#360([a-zA-Z0-9.]+)#i' , $agent , $regs )) {
$outputer = '360 Fast Browser' ;
} else if ( preg_match ( '/Edge([\d]*)\/([^\s]+)/i' , $agent , $regs )) {
$outputer = 'MicroSoft Edge' ;
} else if ( preg_match ( '/UC/i' , $agent )) {
$outputer = 'UC Browser' ;
} else if ( preg_match ( '/QQ/i' , $agent , $regs ) || preg_match ( '/QQ Browser\/([^\s]+)/i' , $agent , $regs )) {
$outputer = 'QQ Browser' ;
} else if ( preg_match ( '/UBrowser/i' , $agent , $regs )) {
$outputer = 'UC Browser' ;
} else if ( preg_match ( '/Opera[\s|\/]([^\s]+)/i' , $agent , $regs )) {
$outputer = 'Opera' ;
} else if ( preg_match ( '/Chrome([\d]*)\/([^\s]+)/i' , $agent , $regs )) {
$outputer = 'Google Chrome' ;
} else if ( preg_match ( '/safari\/([^\s]+)/i' , $agent , $regs )) {
$outputer = 'Safari' ;
} else {
$outputer = 'Google Chrome' ;
}
echo $outputer ;
2021-01-26 13:41:01 +00:00
}
/* 根据评论agent获取设备类型 */
function _getAgentOS ( $agent )
{
2021-02-21 09:45:35 +00:00
$os = " Linux " ;
if ( preg_match ( '/win/i' , $agent )) {
if ( preg_match ( '/nt 6.0/i' , $agent )) {
$os = 'Windows Vista' ;
} else if ( preg_match ( '/nt 6.1/i' , $agent )) {
$os = 'Windows 7' ;
} else if ( preg_match ( '/nt 6.2/i' , $agent )) {
$os = 'Windows 8' ;
} else if ( preg_match ( '/nt 6.3/i' , $agent )) {
$os = 'Windows 8.1' ;
} else if ( preg_match ( '/nt 5.1/i' , $agent )) {
$os = 'Windows XP' ;
} else if ( preg_match ( '/nt 10.0/i' , $agent )) {
$os = 'Windows 10' ;
} else {
$os = 'Windows X64' ;
}
} else if ( preg_match ( '/android/i' , $agent )) {
if ( preg_match ( '/android 9/i' , $agent )) {
$os = 'Android Pie' ;
} else if ( preg_match ( '/android 8/i' , $agent )) {
$os = 'Android Oreo' ;
} else {
$os = 'Android' ;
}
} else if ( preg_match ( '/ubuntu/i' , $agent )) {
$os = 'Ubuntu' ;
} else if ( preg_match ( '/linux/i' , $agent )) {
$os = 'Linux' ;
} else if ( preg_match ( '/iPhone/i' , $agent )) {
$os = 'iPhone' ;
} else if ( preg_match ( '/mac/i' , $agent )) {
$os = 'MacOS' ;
} else if ( preg_match ( '/fusion/i' , $agent )) {
$os = 'Android' ;
} else {
$os = 'Linux' ;
}
echo $os ;
2021-01-26 13:41:01 +00:00
}
/* 获取全局懒加载图 */
function _getLazyload ( $type = true )
{
2021-02-21 09:45:35 +00:00
if ( $type ) echo Helper :: options () -> JLazyload ;
else return Helper :: options () -> JLazyload ;
2021-01-26 13:41:01 +00:00
}
/* 获取头像懒加载图 */
function _getAvatarLazyload ( $type = true )
{
2021-02-21 09:45:35 +00:00
if ( $type ) echo " https://cdn.jsdelivr.net/gh/HaoOuBa/Joe@master/assets/img/lazyload_avatar.gif " ;
else return " https://cdn.jsdelivr.net/gh/HaoOuBa/Joe@master/assets/img/lazyload_avatar.gif " ;
2021-01-26 13:41:01 +00:00
}
/* 查询文章浏览量 */
function _getViews ( $item , $type = true )
{
2021-02-21 09:45:35 +00:00
$db = Typecho_Db :: get ();
$result = $db -> fetchRow ( $db -> select ( 'views' ) -> from ( 'table.contents' ) -> where ( 'cid = ?' , $item -> cid ))[ 'views' ];
if ( $type ) echo number_format ( $result );
else return number_format ( $result );
2021-01-26 13:41:01 +00:00
}
/* 查询文章点赞量 */
function _getAgree ( $item , $type = true )
{
2021-02-21 09:45:35 +00:00
$db = Typecho_Db :: get ();
$result = $db -> fetchRow ( $db -> select ( 'agree' ) -> from ( 'table.contents' ) -> where ( 'cid = ?' , $item -> cid ))[ 'agree' ];
if ( $type ) echo number_format ( $result );
else return number_format ( $result );
2021-01-26 13:41:01 +00:00
}
/* 页面开始计时 */
function _startCountTime ()
{
2021-02-21 09:45:35 +00:00
global $timeStart ;
$mTime = explode ( ' ' , microtime ());
$timeStart = $mTime [ 1 ] + $mTime [ 0 ];
return true ;
2021-01-26 13:41:01 +00:00
}
/* 页面结束计时 */
function _endCountTime ( $precision = 3 )
{
2021-02-21 09:45:35 +00:00
global $timeStart , $timeEnd ;
$mTime = explode ( ' ' , microtime ());
$timeEnd = $mTime [ 1 ] + $mTime [ 0 ];
$timeTotal = number_format ( $timeEnd - $timeStart , $precision );
echo $timeTotal < 1 ? $timeTotal * 1000 . 'ms' : $timeTotal . 's' ;
2021-01-26 13:41:01 +00:00
}
/* 通过邮箱生成头像地址 */
function _getAvatarByMail ( $mail )
{
2021-02-21 09:45:35 +00:00
$gravatarsUrl = 'https://gravatar.helingqi.com/wavatar/' ;
$mailLower = strtolower ( $mail );
$md5MailLower = md5 ( $mailLower );
$qqMail = str_replace ( '@qq.com' , '' , $mailLower );
if ( strstr ( $mailLower , " qq.com " ) && is_numeric ( $qqMail ) && strlen ( $qqMail ) < 11 && strlen ( $qqMail ) > 4 ) {
echo 'https://thirdqq.qlogo.cn/g?b=qq&nk=' . $qqMail . '&s=100' ;
} else {
echo $gravatarsUrl . $md5MailLower . '?d=mm' ;
}
2021-01-26 13:41:01 +00:00
};
/* 获取侧边栏随机一言 */
function _getAsideAuthorMotto ()
{
2021-02-21 09:45:35 +00:00
$JMottoRandom = explode ( " \r \n " , Helper :: options () -> JAside_Author_Motto );
echo $JMottoRandom [ array_rand ( $JMottoRandom , 1 )];
2021-01-26 13:41:01 +00:00
}
/* 获取文章摘要 */
function _getAbstract ( $item , $type = true )
{
2021-02-21 09:45:35 +00:00
$abstract = " " ;
if ( $item -> password ) {
$abstract = " 加密文章,请前往内页查看详情 " ;
} else {
if ( $item -> fields -> abstract ) {
$abstract = $item -> fields -> abstract ;
} else {
2021-02-22 04:57:50 +00:00
$abstract = strip_tags ( $item -> excerpt );
2021-02-21 09:45:35 +00:00
}
}
if ( $abstract === '' ) $abstract = " 暂无简介 " ;
if ( $type ) echo $abstract ;
else return $abstract ;
2021-01-26 13:41:01 +00:00
}
2021-02-11 04:46:01 +00:00
/* 获取列表缩略图 */
function _getThumbnails ( $item )
2021-01-26 13:41:01 +00:00
{
2021-02-21 09:45:35 +00:00
$result = [];
$pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i' ;
$patternMD = '/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|jpeg|gif|png|webp))/i' ;
$patternMDfoot = '/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|jpeg|gif|png|webp))/i' ;
/* 如果填写了自定义缩略图,则优先显示填写的缩略图 */
if ( $item -> fields -> thumb ) {
$fields_thumb_arr = explode ( " \r \n " , $item -> fields -> thumb );
foreach ( $fields_thumb_arr as $list ) $result [] = $list ;
}
/* 如果匹配到正则,则继续补充匹配到的图片 */
if ( preg_match_all ( $pattern , $item -> content , $thumbUrl )) {
foreach ( $thumbUrl [ 1 ] as $list ) $result [] = $list ;
}
if ( preg_match_all ( $patternMD , $item -> content , $thumbUrl )) {
foreach ( $thumbUrl [ 1 ] as $list ) $result [] = $list ;
}
if ( preg_match_all ( $patternMDfoot , $item -> content , $thumbUrl )) {
foreach ( $thumbUrl [ 1 ] as $list ) $result [] = $list ;
}
/* 如果上面的数量不足3个, 则直接补充3个随即图进去 */
if ( sizeof ( $result ) < 3 ) {
$custom_thumbnail = Helper :: options () -> JThumbnail ;
/* 将for循环放里面, 减少一次if判断 */
if ( $custom_thumbnail ) {
$custom_thumbnail_arr = explode ( " \r \n " , $custom_thumbnail );
for ( $i = 0 ; $i < 3 ; $i ++ ) {
$result [] = $custom_thumbnail_arr [ array_rand ( $custom_thumbnail_arr , 1 )] . " ?key= " . mt_rand ( 0 , 1000000 );
}
} else {
for ( $i = 0 ; $i < 3 ; $i ++ ) {
$result [] = 'https://cdn.jsdelivr.net/gh/HaoOuBa/Joe@master/assets/thumb/' . rand ( 1 , 42 ) . '.jpg' ;
}
}
}
return $result ;
2021-01-26 13:41:01 +00:00
}
2021-02-11 04:46:01 +00:00
2021-01-26 13:41:01 +00:00
/* 获取父级评论 */
function _getParentReply ( $parent )
{
2021-02-21 09:45:35 +00:00
if ( $parent !== " 0 " ) {
$db = Typecho_Db :: get ();
$commentInfo = $db -> fetchRow ( $db -> select ( 'author' ) -> from ( 'table.comments' ) -> where ( 'coid = ?' , $parent ));
echo '<div class="parent"><span style="vertical-align: 1px;">@</span> ' . $commentInfo [ 'author' ] . '</div>' ;
}
2021-01-26 13:41:01 +00:00
}
/* 获取侧边栏作者随机文章 */
function _getAsideAuthorNav ()
{
2021-03-07 11:49:03 +00:00
if ( Helper :: options () -> JAside_Author_Nav && Helper :: options () -> JAside_Author_Nav !== " off " ) {
2021-03-07 12:55:20 +00:00
$limit = Helper :: options () -> JAside_Author_Nav ;
2021-02-21 09:45:35 +00:00
$db = Typecho_Db :: get ();
2021-03-07 12:55:20 +00:00
$prefix = $db -> getPrefix ();
$sql = " SELECT * FROM ` { $prefix } contents` WHERE cid >= (SELECT floor( RAND() * ((SELECT MAX(cid) FROM ` { $prefix } contents`)-(SELECT MIN(cid) FROM ` { $prefix } contents`)) + (SELECT MIN(cid) FROM ` { $prefix } contents`))) and type='post' and status='publish' and (password is NULL or password='') ORDER BY cid LIMIT $limit " ;
$result = $db -> query ( $sql );
2021-03-07 13:55:28 +00:00
if ( $result instanceof Traversable ) {
2021-03-07 13:31:08 +00:00
foreach ( $result as $item ) {
$item = Typecho_Widget :: widget ( 'Widget_Abstract_Contents' ) -> push ( $item );
$title = htmlspecialchars ( $item [ 'title' ]);
$permalink = $item [ 'permalink' ];
echo "
< li class = 'item' >
< a class = 'link' href = '{$permalink}' title = '{$title}' > { $title } </ a >
< svg class = 'icon' viewBox = '0 0 1024 1024' xmlns = 'http://www.w3.org/2000/svg' width = '16' height = '16' >< path d = 'M448.12 320.331a30.118 30.118 0 0 1-42.616-42.586L552.568 130.68a213.685 213.685 0 0 1 302.2 0l38.552 38.551a213.685 213.685 0 0 1 0 302.2L746.255 618.497a30.118 30.118 0 0 1-42.586-42.616l147.034-147.035a153.45 153.45 0 0 0 0-217.028l-38.55-38.55a153.45 153.45 0 0 0-216.998 0L448.12 320.33zM575.88 703.67a30.118 30.118 0 0 1 42.616 42.586L471.432 893.32a213.685 213.685 0 0 1-302.2 0l-38.552-38.551a213.685 213.685 0 0 1 0-302.2l147.065-147.065a30.118 30.118 0 0 1 42.586 42.616L173.297 595.125a153.45 153.45 0 0 0 0 217.027l38.55 38.551a153.45 153.45 0 0 0 216.998 0L575.88 703.64zm-234.256-63.88L639.79 341.624a30.118 30.118 0 0 1 42.587 42.587L384.21 682.376a30.118 30.118 0 0 1-42.587-42.587z' /></ svg >
</ li >
" ;
}
2021-02-21 09:45:35 +00:00
}
}
2021-02-01 10:11:49 +00:00
}
function _curl ( $url )
{
2021-02-21 09:45:35 +00:00
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $url );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
if ( strpos ( $url , 'https' ) !== false ) {
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , false );
}
2021-03-07 11:49:03 +00:00
curl_setopt ( $ch , CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36' );
2021-02-21 09:45:35 +00:00
curl_setopt ( $ch , CURLOPT_TIMEOUT , 5 );
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result ;
2021-02-01 10:11:49 +00:00
}
2021-02-16 12:26:00 +00:00
/* 判断敏感词是否在字符串内 */
function _checkSensitiveWords ( $words_str , $str )
{
2021-02-21 09:45:35 +00:00
$words = explode ( " || " , $words_str );
if ( empty ( $words )) {
return false ;
}
foreach ( $words as $word ) {
if ( false !== strpos ( $str , trim ( $word ))) {
return true ;
}
}
return false ;
2021-02-20 01:46:06 +00:00
}