侧边栏壁纸
博主头像
小宇

不待山花烂漫时,只争朝夕共星辰

  • 累计撰写 22 篇文章
  • 累计收到 47 条评论
    love love love

手动给Emlog友链添加icon

2022-3-3 / 0 评论 / 857 阅读 / 正在检测是否收录...

先在数据库添加icon字段

1

在任意字段后面添加即可

名字 类型 长度 默认 排序规则 属性 索引 AUTO_I 注释 虚拟 移动字段
icon VARCHAR 255 自行决定 --- 不勾选 友链icon

修改admin/view/links.php

<?php if (!defined('EMLOG_ROOT')) {
    exit('error!');
} ?>
<?php if (isset($_GET['active_taxis'])): ?>
    <div class="alert alert-success">排序更新成功</div><?php endif ?>
<?php if (isset($_GET['active_del'])): ?>
    <div class="alert alert-success">删除成功</div><?php endif ?>
<?php if (isset($_GET['active_edit'])): ?>
    <div class="alert alert-success">修改成功</div><?php endif ?>
<?php if (isset($_GET['active_add'])): ?>
    <div class="alert alert-success">添加成功</div><?php endif ?>
<?php if (isset($_GET['error_a'])): ?>
    <div class="alert alert-danger">名称和地址不能为空</div><?php endif ?>
<?php if (isset($_GET['error_b'])): ?>
    <div class="alert alert-danger">没有可排序的链接</div><?php endif ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">友情链接</h1>
    <a href="#" class="btn btn-sm btn-success shadow-sm mt-4" data-toggle="modal" data-target="#addModal"><i class="icofont-plus"></i> 添加链接</a>
</div>
<form action="link.php?action=link_taxis" method="post">
    <div class="card shadow mb-4">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-striped table-hover" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                    <tr>
                        <th>排序</th>
                        <th>链接</th>
                        <th>描述</th>
                        <th>ICON</th>
                        <th>查看</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    foreach ($links as $key => $value):
                        doAction('adm_link_display');
                        ?>
                        <tr>
                            <td><input class="form-control em-small" name="link[<?= $value['id'] ?>]" value="<?= $value['taxis'] ?>" maxlength="4"/></td>
                            <td>
                                <a href="#" data-toggle="modal" data-target="#editModal"
                                   data-linkid="<?= $value['id'] ?>"
                                   data-sitename="<?= $value['sitename'] ?>"
                                   data-siteurl="<?= $value['siteurl'] ?>"
                                   data-description="<?= $value['description'] ?>"
                                   data-icon="<?= $value['icon'] ?>"><?= $value['sitename'] ?></a>
                            </td>
                            <td><?= $value['description'] ?></td>
                            <td><?= $value['icon'] ?></td>
                            <td>
                                <a href="<?= $value['siteurl'] ?>" target="_blank"><img src="./views/images/vlog.gif" align="absbottom" border="0"/></a>
                            </td>
                            <td>
                                <?php if ($value['hide'] == 'n'): ?>
                                    <a href="link.php?action=hide&amp;linkid=<?= $value['id'] ?>" class="badge badge-primary">显示</a>
                                <?php else: ?>
                                    <a href="link.php?action=show&amp;linkid=<?= $value['id'] ?>" class="badge badge-warning">隐藏</a>
                                <?php endif ?>
                                <a href="javascript: em_confirm(<?= $value['id'] ?>, 'link', '<?= LoginAuth::genToken() ?>');" class="badge badge-danger">删除</a>
                            </td>
                        </tr>
                    <?php endforeach ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="list_footer">
        <input type="submit" value="改变排序" class="btn btn-sm btn-success shadow-sm"/>
    </div>
</form>
<!--添加链接弹窗-->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">新建链接</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <form action="link.php?action=addlink" method="post" name="link" id="link">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="alias">名称</label>
                        <input class="form-control" id="sitename" maxlength="255" name="sitename" required>
                    </div>
                    <div class="form-group">
                        <label for="template">地址</label>
                        <input class="form-control" id="siteurl" name="siteurl" maxlength="255" required>
                    </div>
                    <div class="form-group">
                        <label for="alias">描述</label>
                        <textarea name="description" type="text" maxlength="512" class="form-control"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="template">ICON</label>
                        <input class="form-control" id="icon" name="icon" maxlength="255">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-sm btn-success">保存</button>
                    <span id="alias_msg_hook"></span>
                </div>
            </form>
        </div>
    </div>
</div>

<!--编辑链接弹窗-->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">编辑链接</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <form action="link.php?action=update_link" method="post" name="link" id="link">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="alias">名称</label>
                        <input class="form-control" id="sitename" maxlength="255" name="sitename" required>
                    </div>
                    <div class="form-group">
                        <label for="template">地址</label>
                        <input class="form-control" id="siteurl" maxlength="255" name="siteurl" required>
                    </div>
                    <div class="form-group">
                        <label for="alias">描述</label>
                        <textarea name="description" id="description" maxlength="512" type="text" class="form-control"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="template">ICON</label>
                        <input class="form-control" id="icon" maxlength="255" name="icon">
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" value="" name="linkid" id="linkid"/>
                    <button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-sm btn-success">保存</button>
                    <span id="alias_msg_hook"></span>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    $("#menu_category_view").addClass('active');
    $("#menu_view").addClass('show');
    $("#menu_link").addClass('active');
    setTimeout(hideActived, 3600);
    $('#editModal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget)
        var linkid = button.data('linkid')
        var sitename = button.data('sitename')
        var siteurl = button.data('siteurl')
        var description = button.data('description')
        var icon = button.data('icon')
        var modal = $(this)
        modal.find('.modal-body #sitename').val(sitename)
        modal.find('.modal-body #siteurl').val(siteurl)
        modal.find('.modal-body #description').val(description)
        modal.find('.modal-body #icon').val(icon)
        modal.find('.modal-footer #linkid').val(linkid)
    })
</script>

修改 include/model/link_model.php

//在$row['siteurl'] = $row['siteurl'];下面添加以下代码

$row['icon'] = $row['icon'];

//修改addLink

function addLink($name, $url, $des , $ico) {
        $sql = "insert into " . DB_PREFIX . "link (sitename,siteurl,description,icon) values('$name','$url','$des','$ico')";
        $this->db->query($sql);
}

完整代码

<?php
/**
 * links model
 * @package EMLOG (www.emlog.net)
 */

class Link_Model {

    private $db;

    function __construct() {
        $this->db = Database::getInstance();
    }

    function getLinks() {
        $res = $this->db->query("SELECT * FROM " . DB_PREFIX . "link ORDER BY taxis ASC");
        $links = [];
        while ($row = $this->db->fetch_array($res)) {
            $row['sitename'] = htmlspecialchars($row['sitename']);
            $row['description'] = htmlClean($row['description'], false);
            $row['siteurl'] = $row['siteurl'];
            $row['icon'] = $row['icon'];
            $links[] = $row;
        }
        return $links;
    }

    function updateLink($linkData, $linkId) {
        $Item = [];
        foreach ($linkData as $key => $data) {
            $Item[] = "$key='$data'";
        }
        $upStr = implode(',', $Item);
        $this->db->query("update " . DB_PREFIX . "link set $upStr where id=$linkId");
    }

    function addLink($name, $url, $des , $ico) {
        $sql = "insert into " . DB_PREFIX . "link (sitename,siteurl,description,icon) values('$name','$url','$des','$ico')";
        $this->db->query($sql);
    }

    function deleteLink($linkId) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "link where id=$linkId");
    }

}

修改admin/link.php

//在$description = isset($_POST['description']) ? addslashes(trim($_POST['description'])) : '';下面添加以下代码

$icon = isset($_POST['icon']) ? addslashes(trim($_POST['icon'])) : '';

//修改$Link_Model->updateLink(array('sitename' => $sitename, 'siteurl' => $siteurl, 'description' => $description), $linkId);为以下代码

$Link_Model->updateLink(array('sitename' => $sitename, 'siteurl' => $siteurl, 'description' => $description, 'icon' => $icon), $linkId);

//第52行$Link_Model->addLink($sitename, $siteurl, $description);
改为以下代码
$Link_Model->addLink($sitename, $siteurl, $description,$icon);

完整代码

<?php
/**
 * links
 * @package EMLOG (www.emlog.net)
 */

/**
 * @var string $action
 * @var object $CACHE
 */

require_once 'globals.php';

$Link_Model = new Link_Model();

if (empty($action)) {
    $links = $Link_Model->getLinks();
    include View::getAdmView('header');
    require_once(View::getAdmView('links'));
    include View::getAdmView('footer');
    View::output();
}

if ($action == 'link_taxis') {
    $link = $_POST['link'] ?? '';

    if (empty($link)) {
        emDirect("./link.php?error_b=1");
    }

    foreach ($link as $key => $value) {
        $value = (int)$value;
        $key = (int)$key;
        $Link_Model->updateLink(array('taxis' => $value), $key);
    }
    $CACHE->updateCache('link');
    emDirect("./link.php?active_taxis=1");
}

if ($action == 'addlink') {
    $sitename = isset($_POST['sitename']) ? addslashes(trim($_POST['sitename'])) : '';
    $siteurl = isset($_POST['siteurl']) ? addslashes(trim($_POST['siteurl'])) : '';
    $description = isset($_POST['description']) ? addslashes(trim($_POST['description'])) : '';
    $icon = isset($_POST['icon']) ? addslashes(trim($_POST['icon'])) : '';

    if ($sitename == '' || $siteurl == '') {
        emDirect("./link.php?error_a=1");
    }
    if (!preg_match("/^http|ftp.+$/i", $siteurl)) {
        $siteurl = 'http://' . $siteurl;
    }
    $Link_Model->addLink($sitename, $siteurl, $description,$icon);
    $CACHE->updateCache('link');
    emDirect("./link.php?active_add=1");
}

if ($action == 'update_link') {
    $sitename = isset($_POST['sitename']) ? addslashes(trim($_POST['sitename'])) : '';
    $siteurl = isset($_POST['siteurl']) ? addslashes(trim($_POST['siteurl'])) : '';
    $description = isset($_POST['description']) ? addslashes(trim($_POST['description'])) : '';
    $icon = isset($_POST['icon']) ? addslashes(trim($_POST['icon'])) : '';
    $linkId = isset($_POST['linkid']) ? (int)$_POST['linkid'] : '';

    if (!preg_match("/^http|ftp.+$/i", $siteurl)) {
        $siteurl = 'http://' . $siteurl;
    }

    $Link_Model->updateLink(array('sitename' => $sitename, 'siteurl' => $siteurl, 'description' => $description, 'icon' => $icon), $linkId);

    $CACHE->updateCache('link');
    emDirect("./link.php?active_edit=1");
}

if ($action == 'dellink') {
    LoginAuth::checkToken();
    $linkid = isset($_GET['linkid']) ? (int)$_GET['linkid'] : '';
    $Link_Model->deleteLink($linkid);
    $CACHE->updateCache('link');
    emDirect("./link.php?active_del=1");
}

if ($action == 'hide') {
    $linkId = isset($_GET['linkid']) ? (int)$_GET['linkid'] : '';

    $Link_Model->updateLink(array('hide' => 'y'), $linkId);

    $CACHE->updateCache('link');
    emDirect('./link.php');
}

if ($action == 'show') {
    $linkId = isset($_GET['linkid']) ? (int)$_GET['linkid'] : '';

    $Link_Model->updateLink(array('hide' => 'n'), $linkId);

    $CACHE->updateCache('link');
    emDirect('./link.php');
}

修改 include/lib/cache.php

找到 mc_link()

//修改
//$query = $this->db->query("SELECT siteurl,sitename,description FROM " . DB_PREFIX . "link WHERE hide='n' ORDER BY taxis ASC");
//$link_cache[] = array(
//              'link' => htmlspecialchars($show_link['sitename']),
//              'url'  => htmlspecialchars($show_link['siteurl']),
//              'des'  => htmlspecialchars($show_link['description'])
//          );

修改后:
$query = $this->db->query("SELECT siteurl,sitename,description,icon FROM " . DB_PREFIX . "link WHERE hide='n' ORDER BY taxis ASC");
$link_cache[] = array(
                'link' => htmlspecialchars($show_link['sitename']),
                'url'  => htmlspecialchars($show_link['siteurl']),
                'des'  => htmlspecialchars($show_link['description']),
                'icon' => htmlspecialchars($show_link['icon'])
);

完整代码

<?php
/**
 * 生成文本缓存类
 *
 * @package EMLOG (www.emlog.net)
 */

class Cache {

    private $db;
    private static $instance = null;

    private $options_cache;
    private $user_cache;
    private $sta_cache;
    private $comment_cache;
    private $tags_cache;
    private $sort_cache;
    private $link_cache;
    private $navi_cache;
    private $newlog_cache;
    private $record_cache;
    private $logtags_cache;
    private $logsort_cache;
    private $logalias_cache;

    protected function __construct() {
        $this->db = Database::getInstance();
    }

    /**
     * 静态方法,返回数据库连接实例
     *
     * @return Cache
     */
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new Cache();
        }
        return self::$instance;
    }

    /**
     * 更新缓存
     *
     * @param array/string $cacheMethodName 需要更新的缓存,更新多个采用数组方式:array('options', 'user'),单个采用字符串方式:'options',全部则留空
     * @return unknown_type
     */
    function updateCache($cacheMethodName = null) {
        // 更新单个缓存
        if (is_string($cacheMethodName)) {
            if (method_exists($this, 'mc_' . $cacheMethodName)) {
                call_user_func(array($this, 'mc_' . $cacheMethodName));
            }
            return;
        }
        // 更新多个缓存
        if (is_array($cacheMethodName)) {
            foreach ($cacheMethodName as $name) {
                if (method_exists($this, 'mc_' . $name)) {
                    call_user_func(array($this, 'mc_' . $name));
                }
            }
            return;
        }
        // 更新全部缓存
        if ($cacheMethodName == null) {
            // 自动运行本类所有更新缓存的方法(此类方法的名称必须由mc_开头)
            $cacheMethodNames = get_class_methods($this);
            foreach ($cacheMethodNames as $method) {
                if (preg_match('/^mc_/', $method)) {
                    call_user_func(array($this, $method));
                }
            }
        }
    }

    /**
     * 站点配置缓存
     * 注意更新缓存的方法必须为mc开头
     */
    private function mc_options() {
        $options_cache = [];
        $res = $this->db->query("SELECT * FROM " . DB_PREFIX . "options");
        while ($row = $this->db->fetch_array($res)) {
            if (in_array($row['option_name'], array('site_key', 'blogname', 'bloginfo', 'blogurl', 'icp'))) {
                $row['option_value'] = htmlspecialchars($row['option_value']);
            }
            $options_cache[$row['option_name']] = $row['option_value'];
        }
        $cacheData = serialize($options_cache);
        $this->cacheWrite($cacheData, 'options');
    }

    /**
     * 用户信息缓存
     */
    private function mc_user() {
        $user_cache = [];
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "user");
        while ($row = $this->db->fetch_array($query)) {
            $photo = [];
            $avatar = '';
            if (!empty($row['photo'])) {
                $photosrc = str_replace("../", '', $row['photo']);
                $imgsize = chImageSize($row['photo'], Option::ICON_MAX_W, Option::ICON_MAX_H);
                $photo['src'] = htmlspecialchars($photosrc);
                $photo['width'] = $imgsize['w'];
                $photo['height'] = $imgsize['h'];
                $avatar = strstr($photosrc, 'thum') ? str_replace('thum', 'thum52', $photosrc) : preg_replace("/^(.*)\/(.*)$/", "\$1/thum52-\$2", $photosrc);
                $avatar = file_exists('../' . $avatar) ? $avatar : $photosrc;
            }
            $row['nickname'] = empty($row['nickname']) ? $row['username'] : $row['nickname'];
            $user_cache[$row['uid']] = [
                'uid'       => $row['uid'],
                'photo'     => $photo,
                'avatar'    => $avatar,
                'name_orig' => $row['nickname'],
                'name'      => htmlspecialchars($row['nickname']),
                'mail'      => htmlspecialchars($row['email']),
                'des'       => htmlClean($row['description']),
                'ischeck'   => htmlspecialchars($row['ischeck']),
                'role'      => $row['role'],
            ];
        }
        $cacheData = serialize($user_cache);
        $this->cacheWrite($cacheData, 'user');
    }

    /**
     * 站点统计缓存
     */
    private function mc_sta() {
        $sta_cache = [];
        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE type='blog' AND hide='n' AND checked='y' ");
        $lognum = $data['total'];

        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE type='blog' AND hide='y'");
        $draftnum = $data['total'];

        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE type='blog' AND hide='n' AND checked='n' ");
        $checknum = $data['total'];

        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "comment WHERE hide='n' ");
        $comnum = $data['total'];

        $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "comment WHERE hide='y' ");
        $hidecom = $data['total'];

        $sta_cache = array(
            'lognum'     => $lognum,
            'draftnum'   => $draftnum,
            'comnum'     => $comnum,
            'comnum_all' => $comnum + $hidecom,
            'hidecomnum' => $hidecom,
            'checknum'   => $checknum,
        );

        $query = $this->db->query("SELECT uid FROM " . DB_PREFIX . "user");
        while ($row = $this->db->fetch_array($query)) {
            $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE author={$row['uid']} AND hide='n' and type='blog'");
            $logNum = $data['total'];

            $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE author={$row['uid']} AND hide='y' AND type='blog'");
            $draftNum = $data['total'];

            $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "comment AS a, " . DB_PREFIX . "blog AS b WHERE a.gid = b.gid AND b.author={$row['uid']}");
            $commentNum = $data['total'];

            $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "comment AS a, " . DB_PREFIX . "blog AS b WHERE a.gid=b.gid and a.hide='y' AND b.author={$row['uid']}");
            $hidecommentNum = $data['total'];

            $sta_cache[$row['uid']] = array(
                'lognum'         => $logNum,
                'draftnum'       => $draftNum,
                'commentnum'     => $commentNum,
                'hidecommentnum' => $hidecommentNum,
            );
        }

        $cacheData = serialize($sta_cache);
        $this->cacheWrite($cacheData, 'sta');
    }

    /**
     * 最新评论缓存
     */
    private function mc_comment() {
        $query = $this->db->query("SELECT option_value,option_name FROM " . DB_PREFIX . "options WHERE option_name IN('index_comnum','comment_subnum','comment_paging','comment_pnum','comment_order')");
        while ($row = $this->db->fetch_array($query)) {
            ${$row['option_name']} = $row['option_value'];
        }
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "comment WHERE hide='n' ORDER BY date DESC LIMIT 0, $index_comnum");
        $com_cache = [];
        $com_cids = [];
        while ($show_com = $this->db->fetch_array($query)) {
            $com_page = '';
            if ($comment_paging == 'y') {
                $pid = $show_com['pid'];
                $cid = $show_com['cid'];
                $order = $comment_order == 'newer' ? 'DESC' : '';
                while ($pid != 0) {
                    $show_pid = $this->db->once_fetch_array("SELECT cid,pid FROM " . DB_PREFIX . "comment WHERE cid=$pid");
                    $pid = $show_pid['pid'];
                    $cid = $show_pid['cid'];
                }
                if (!isset($com_cids[$show_com['gid']])) {
                    $com_cids[$show_com['gid']] = [];
                    $query2 = $this->db->query("SELECT cid FROM " . DB_PREFIX . "comment WHERE gid=" . $show_com['gid'] . " AND pid=0 AND hide='n' ORDER BY date $order");
                    while ($show_cid = $this->db->fetch_array($query2)) {
                        $com_cids[$show_com['gid']][] = $show_cid['cid'];
                    }
                }
                $com_page = (int)floor(array_search($cid, $com_cids[$show_com['gid']]) / $comment_pnum) + 1;
            }
            $com_cache[] = array(
                'cid'     => $show_com['cid'],
                'gid'     => $show_com['gid'],
                'name'    => htmlspecialchars($show_com['poster']),
                'date'    => $show_com['date'],
                'page'    => $com_page,
                'mail'    => $show_com['mail'],
                'uid'     => $show_com['uid'],
                'content' => htmlClean(subString($show_com['comment'], 0, $comment_subnum), false),
            );
        }
        $cacheData = serialize($com_cache);
        $this->cacheWrite($cacheData, 'comment');
    }

    /**
     * 侧边栏标签缓存
     */
    private function mc_tags() {
        $tag_cache = [];
        $tagnum = 100;
        $maxuse = 20;
        $minuse = 0;
        $spread = (min($tagnum, 12));
        $rank = $maxuse - $minuse;
        $rank = ($rank == 0 ? 1 : $rank);
        $rank = $spread / $rank;
        $query = $this->db->query("SELECT tagname,gid FROM " . DB_PREFIX . "tag order by tid desc limit 100");
        while ($row = $this->db->fetch_array($query)) {
            if ($row['gid'] == ',') {
                continue;
            }
            $usenum = empty($row['gid']) ? 0 : substr_count($row['gid'], ',') + 1;
            $fontsize = 10 + round(($usenum - $minuse) * $rank); //maxfont:22pt,minfont:10pt
            $tag_cache[] = [
                'tagurl'   => urlencode($row['tagname']),
                'tagname'  => htmlspecialchars($row['tagname']),
                'fontsize' => $fontsize,
                'usenum'   => $usenum
            ];
        }
        $cacheData = serialize($tag_cache);
        $this->cacheWrite($cacheData, 'tags');
    }

    /**
     * 侧边栏分类缓存
     */
    private function mc_sort() {
        $sort_cache = [];
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sort ORDER BY pid ASC,taxis ASC");
        while ($row = $this->db->fetch_array($query)) {
            $data = $this->db->once_fetch_array("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog WHERE sortid=" . $row['sid'] . " AND hide='n' AND checked='y' AND type='blog'");
            $logNum = $data['total'];
            $sortData = array(
                'lognum'      => $logNum,
                'sortname'    => htmlspecialchars($row['sortname']),
                'description' => htmlspecialchars($row['description']),
                'alias'       => $row['alias'],
                'sid'         => (int)$row['sid'],
                'taxis'       => (int)$row['taxis'],
                'pid'         => (int)$row['pid'],
                'template'    => htmlspecialchars($row['template']),
            );
            if ($sortData['pid'] == 0) {
                $sortData['children'] = [];
            } elseif (isset($sort_cache[$row['pid']])) {
                $sort_cache[$row['pid']]['children'][] = $row['sid'];
            }
            $sort_cache[$row['sid']] = $sortData;
        }
        $cacheData = serialize($sort_cache);
        $this->cacheWrite($cacheData, 'sort');
    }

    /**
     * 友情链接缓存
     */
    private function mc_link() {
        $link_cache = [];
        $query = $this->db->query("SELECT siteurl,sitename,description,icon FROM " . DB_PREFIX . "link WHERE hide='n' ORDER BY taxis ASC");
        while ($show_link = $this->db->fetch_array($query)) {
            $link_cache[] = array(
                'link' => htmlspecialchars($show_link['sitename']),
                'url'  => htmlspecialchars($show_link['siteurl']),
                'des'  => htmlspecialchars($show_link['description']),
                'icon' => htmlspecialchars($show_link['icon'])
            );
        }
        $cacheData = serialize($link_cache);
        $this->cacheWrite($cacheData, 'link');
    }

    /**
     * 导航缓存
     */
    private function mc_navi() {
        $navi_cache = [];
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "navi WHERE hide='n' ORDER BY pid ASC, taxis ASC");
        $sorts = $this->readCache('sort');
        while ($row = $this->db->fetch_array($query)) {
            $children = [];
            $url = Url::navi($row['type'], $row['type_id'], $row['url']);

            if ($row['type'] == Navi_Model::navitype_sort && !empty($sorts[$row['type_id']]['children'])) {
                foreach ($sorts[$row['type_id']]['children'] as $sortid) {
                    $children[] = $sorts[$sortid];
                }
            }
            $naviData = array(
                'id'        => (int)$row['id'],
                'naviname'  => htmlspecialchars(trim($row['naviname'])),
                'url'       => htmlspecialchars(trim($url)),
                'newtab'    => $row['newtab'],
                'isdefault' => $row['isdefault'],
                'type'      => (int)$row['type'],
                'typeId'    => (int)$row['type_id'],
                'taxis'     => (int)$row['taxis'],
                'hide'      => $row['hide'],
                'pid'       => (int)$row['pid'],
                'children'  => $children,
            );
            if ($row['type'] == Navi_Model::navitype_custom) {
                if ($naviData['pid'] == 0) {
                    $naviData['childnavi'] = [];
                } elseif (isset($navi_cache[$row['pid']])) {
                    $navi_cache[$row['pid']]['childnavi'][] = $naviData;
                }
            }
            $navi_cache[$row['id']] = $naviData;
        }
        $cacheData = serialize($navi_cache);
        $this->cacheWrite($cacheData, 'navi');
    }

    /**
     * 最新文章
     */
    private function mc_newlog() {
        $row = $this->db->fetch_array($this->db->query("SELECT option_value FROM " . DB_PREFIX . "options where option_name='index_newlognum'"));
        $index_newlognum = $row['option_value'];
        $sql = "SELECT gid,title FROM " . DB_PREFIX . "blog WHERE hide='n' and checked='y' and type='blog' ORDER BY date DESC LIMIT 0, $index_newlognum";
        $res = $this->db->query($sql);
        $logs = [];
        while ($row = $this->db->fetch_array($res)) {
            $row['gid'] = (int)$row['gid'];
            $row['title'] = htmlspecialchars($row['title']);
            $logs[] = $row;
        }
        $cacheData = serialize($logs);
        $this->cacheWrite($cacheData, 'newlog');
    }

    /**
     * 文章归档缓存
     */
    private function mc_record() {
        $query = $this->db->query('select date from ' . DB_PREFIX . "blog WHERE hide='n' and checked='y' and type='blog' ORDER BY date DESC");
        $record = 'xxxx_x';
        $p = 0;
        $lognum = 1;
        $record_cache = [];
        while ($show_record = $this->db->fetch_array($query)) {
            $f_record = gmdate('Y_n', $show_record['date']);
            if ($record != $f_record) {
                $h = $p - 1;
                if ($h != -1) {
                    $record_cache[$h]['lognum'] = $lognum;
                }
                $record_cache[$p] = array(
                    'record' => gmdate('Y年n月', $show_record['date']),
                    'date'   => gmdate('Ym', $show_record['date'])
                );
                $p++;
                $lognum = 1;
            } else {
                $lognum++;
                continue;
            }
            $record = $f_record;
        }
        $j = $p - 1;
        if ($j >= 0) {
            $record_cache[$j]['lognum'] = $lognum;
        }

        $cacheData = serialize($record_cache);
        $this->cacheWrite($cacheData, 'record');
    }

    /**
     * 文章标签缓存
     */
    private function mc_logtags() {
        $tag_model = new Tag_Model();
        $newlog = $this->readCache("newlog");

        $log_cache_tags = [];
        foreach ($newlog as $each) {
            $gid = $each['gid'];
            $tag_ids = $tag_model->getTagIdsFromBlogId($gid);
            $tag_names = $tag_model->getNamesFromIds($tag_ids);

            $tags = [];
            foreach ($tag_names as $key => $value) {
                $tag = [];
                $tag['tagurl'] = rawurlencode($value);
                $tag['tagname'] = htmlspecialchars($value);
                $tag['tid'] = (int)$key;
                $tags[] = $tag;
            }

            $log_cache_tags[$gid] = $tags;
        }

        $cacheData = serialize($log_cache_tags);
        $this->cacheWrite($cacheData, 'logtags');
    }

    /**
     * 文章分类缓存
     */
    private function mc_logsort() {
        $sql = "SELECT gid,sortid FROM " . DB_PREFIX . "blog where type='blog'";
        $query = $this->db->query($sql);
        $log_cache_sort = [];
        while ($row = $this->db->fetch_array($query)) {
            if ($row['sortid'] > 0) {
                $res = $this->db->query("SELECT sid,sortname,alias FROM " . DB_PREFIX . "sort where sid=" . $row['sortid']);
                $srow = $this->db->fetch_array($res);
                $log_cache_sort[$row['gid']] = array(
                    'name'  => htmlspecialchars($srow['sortname']),
                    'id'    => htmlspecialchars($srow['sid']),
                    'alias' => htmlspecialchars($srow['alias']),
                );
            }
        }
        $cacheData = serialize($log_cache_sort);
        $this->cacheWrite($cacheData, 'logsort');
    }

    /**
     * 文章别名缓存
     */
    private function mc_logalias() {
        $sql = "SELECT gid,alias FROM " . DB_PREFIX . "blog where alias!=''";
        $query = $this->db->query($sql);
        $log_cache_alias = [];
        while ($row = $this->db->fetch_array($query)) {
            $log_cache_alias[$row['gid']] = $row['alias'];
        }
        $cacheData = serialize($log_cache_alias);
        $this->cacheWrite($cacheData, 'logalias');
    }

    /**
     * 写入缓存
     */
    public function cacheWrite($cacheData, $cacheName) {
        $cachefile = EMLOG_ROOT . '/content/cache/' . $cacheName . '.php';
        $cacheData = "<?php exit;//" . $cacheData;
        @ $fp = fopen($cachefile, 'wb') or emMsg('读取缓存失败');
        @ fwrite($fp, $cacheData) or emMsg('写入缓存失败,缓存目录 (content/cache) 不可写');
        $this->{$cacheName . '_cache'} = null;
        fclose($fp);
    }

    /**
     * 读取缓存文件
     */
    public function readCache($cacheName) {
        if ($this->{$cacheName . '_cache'} != null) {
            return $this->{$cacheName . '_cache'};
        } else {
            $cachefile = EMLOG_ROOT . '/content/cache/' . $cacheName . '.php';
            // 如果缓存文件不存在则自动生成缓存文件
            if (!is_file($cachefile) || filesize($cachefile) <= 0) {
                if (method_exists($this, 'mc_' . $cacheName)) {
                    call_user_func(array($this, 'mc_' . $cacheName));
                }
            }
            if ($fp = fopen($cachefile, 'r')) {
                $data = fread($fp, filesize($cachefile));
                fclose($fp);
                clearstatcache();
                $this->{$cacheName . '_cache'} = unserialize(str_replace("<?php exit;//", '', $data));
                return $this->{$cacheName . '_cache'};
            }
        }
    }
}
最终效果

2

3

评论一下?

OωO
取消