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

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

前言

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

体验https://oyo.cool/ | https://wangyangyang.vip/ | https://easybe.org/ [亚太地区的服务器暂时关闭了,cnmae转发不行会被拦截]

内容

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

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 } ?>

学无止境,谦卑而行.

目录
相关文章
|
存储 缓存 NoSQL
为你的Typecho使用Redis缓存,优化访问速度-织音博客
前言 Typecho虽然轻量,但终究仍是PHP动态脚本,访问时需要频繁调取数据库的信息,导致并发值一高,CPU就100%占用,无法处理新的请求信息。这时,我们可以用Redis来设置缓存,从而不用频繁调动数据库,来达到加速访问的目的。注意:Redis仅支持 Linux 系统,如果你是Windows系统,可考虑其他软件。 Redis介绍 Redis缓存是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的api。 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。 不仅仅支持简单的key-value类型
219 0
为你的Typecho使用Redis缓存,优化访问速度-织音博客
|
2月前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?
|
19天前
|
canal 缓存 NoSQL
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
根据对一致性的要求程度,提出多种解决方案:同步删除、同步删除+可靠消息、延时双删、异步监听+可靠消息、多重保障方案
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
|
2月前
|
缓存 NoSQL 网络协议
【Azure Redis 缓存】Lettuce 连接到Azure Redis服务,出现15分钟Timeout问题
【Azure Redis 缓存】Lettuce 连接到Azure Redis服务,出现15分钟Timeout问题
【Azure Redis 缓存】Lettuce 连接到Azure Redis服务,出现15分钟Timeout问题
|
2月前
|
缓存 NoSQL Java
Redis深度解析:解锁高性能缓存的终极武器,让你的应用飞起来
【8月更文挑战第29天】本文从基本概念入手,通过实战示例、原理解析和高级使用技巧,全面讲解Redis这一高性能键值对数据库。Redis基于内存存储,支持多种数据结构,如字符串、列表和哈希表等,常用于数据库、缓存及消息队列。文中详细介绍了如何在Spring Boot项目中集成Redis,并展示了其工作原理、缓存实现方法及高级特性,如事务、发布/订阅、Lua脚本和集群等,帮助读者从入门到精通Redis,大幅提升应用性能与可扩展性。
60 0
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】使用StackExchange.Redis,偶发ERROR - Timeout performing HSET (15000ms)
【Azure Redis 缓存】使用StackExchange.Redis,偶发ERROR - Timeout performing HSET (15000ms)
|
2月前
|
缓存 NoSQL Java
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
|
20天前
|
存储 NoSQL Redis
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群
Redis持久化、RDB和AOF方案、Redis主从集群、哨兵、分片集群、散列插槽、自动手动故障转移
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群
|
3天前
|
存储 缓存 NoSQL
解决Redis缓存击穿问题的技术方法
解决Redis缓存击穿问题的技术方法
16 2
下一篇
无影云桌面