Typecho——非插件方式实现QQ和Gravatar缓存到本地

前言

EasyBe终于差不多可以收尾了,EasyBe希望大家多多star和issue;

体验https://oyo.cool/ | https://wangyangyang.vip/

内容

其实还可以稍微优化下,比如将缓存时间也作为配置来;

functions.php

/**
*主题配置
*/
function themeConfig() {
   // 设置gravatar的镜像服务器
   $gravatarPrefix = new Typecho_Widget_Helper_Form_Element_Radio('gravatarPrefix', array(
        'https://gravatar.loli.net/avatar/' => 'LOLI镜像 <small>https://gravatar.loli.net</small>',
        'https://gravatar.cat.net/avatar/' => 'CAT镜像 <small>https://gravatar.cat.net</small>',
        'https://sdn.geekzu.org/avatar/' => '极客镜像 <small>https://sdn.geekzu.org</small>',
        'https://dn-qiniu-avatar.qbox.me/avatar/' => '七牛镜像 <small>https://dn-qiniu-avatar.qbox.me</small>',
        ),
        'https://gravatar.loli.net/avatar/', _t('Gravatar镜像'), _t('Gravatar镜像服务器'));
    $form->addInput($gravatarPrefix->multiMode());
    // 设置加盐参数
    $salt = new Typecho_Widget_Helper_Form_Element_Text('salt', NULL, 'easybe', _t('加密参数'), _t('头像加密参数建议修改'));
    $form->addInput($salt);

}


/**
 * 获取本地头像
 * @param string $email 邮箱地址
 * @return string 返回头像链接
 */
function getAvatar(string $email) {
        $options = Helper::options();
        // 默认缓存15天
        $time = 1296000;
        $gravatarUrl = $options->gravatarPrefix.md5(strtolower(trim($email))).'?s=100&r=G';
        $qqNumber = preg_match('/@qq.com/', $email) ?  explode('@', $email)[0] : '';
        $fileUrl = $qqNumber ? "https://q.qlogo.cn/headimg_dl?dst_uin={$qqNumber}&spec=100&img_type=jpg" : $gravatarUrl;
        $filePath = '/usr/uploads/avatar/'.md5(strtolower(trim($email.$options->salt))).'.jpg';
        $fileInfo = __TYPECHO_ROOT_DIR__.$filePath;
        $dirPath = dirname($fileInfo);
        if (!is_dir($dirPath)) mkdir($dirPath, 0755);
        if (file_exists($fileInfo) && (time() - filemtime($fileInfo)) < $time) {
            echo $options->siteUrl.$filePath;
        } else if (file_put_contents($fileInfo,file_get_contents($fileUrl))) { 
            echo $options->siteUrl.$filePath;
        }

}

comments.php

<?php
function threadedComments($comments, $options){
    $commentClass = '';
    if ($comments->authorId) {
        if ($comments->authorId == $comments->ownerId) {
            $commentClass .= ' comment-by-author';  //如果是文章作者的评论添加 .comment-by-author 样式
        } else {
            $commentClass .= ' comment-by-user';  //如果是评论作者的添加 .comment-by-user 样式
        }
    }
    $commentLevelClass = $comments->_levels > 0 ? ' comment-child' : ' comment-parent';  //评论层数大于0为子级,否则是父级
    ?>

    <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php
    if ($comments->levels > 0) {
        echo ' comment-child';
        $comments->levelsAlt(' comment-level-odd', ' comment-level-even');
    } else {
        echo ' comment-parent';
    }
    $comments->alt(' comment-odd', ' comment-even');
    echo $commentClass;
    ?>">
        <div class="feedbackItem" id="<?php $comments->theId(); ?>">
            <div class="feedbackListSubtitle">
                <div class="feedbackManage">
                <span class="comment_actions">
                    <?php $comments->reply(); ?>
                </span>
                </div>
                <a href="javascript:void(0);" class="layer">#<?php $comments->sequence(); ?>楼</a>
                <?php if ($comments->authorId) {
                    if ($comments->authorId == $comments->ownerId) {
                        echo "<span class='louzhu'>[楼主]</span>";
                    } ?>
                <?php } ?>
                <span class="comment_date"><?php $comments->date('Y-m-d H:i'); ?></span>
                <a id="a_comment_author_<?php $comments->sequence(); ?>" href="<?php $comments->permalink(); ?>"><?php $comments->author(); ?></a>
                <span id="device_comment_<?php $comments->sequence(); ?>" >
                    <?php $data = getBrowsersInfo($comments->agent); ?>
                    <span><?php echo $data['system']; ?> <?php echo $data['systemVersion']; ?></span>
                    <span><?php echo $data['browser']; ?> <?php echo $data['version']; ?> </span>
                </span>
                <span id="ip_comment_<?php $comments->sequence(); ?>" ><?php getIPInfo($comments->ip) ?></span>
            </div>
            <div class="feedbackCon">
                <div id="comment_body_<?php $comments->sequence(); ?>" class="blog_comment_body cnblogs-markdown">
                    <p>
                        <a><?php echo getPermalinkFromCoId($comments->parent); ?></a>
                        <br><?php $comments->content(); ?>
                    </p>
                </div>
                <div class="comment_vote">
                    <span class="comment_error" style="color: red"></span>
                </div>
                <span id="comment_<?php $comments->sequence(); ?>_avatar" style="display:none">
                    <img  src="<?php getAvatar($comments->mail) ?>" alt='avatar' >
                </span>
            </div>
        </div>

        <?php if ($comments->children) { ?>
            <?php $comments->threadedComments($options); ?>
        <?php } ?>
    </li>
<?php } ?>

posted @ 2023-03-08 01:25:00 王洋 阅读(1283) 评论(0)
发表评论
昵称
邮箱
网址