<?php
require_once dirname(__FILE__) . '/../config.inc.php';
$db = \Typecho\Db::get();
try {
    $query = $db->select('title', 'url', 'logo', 'description')
                ->from('table.friends')
                ->where('status = ?', 1);
    $rows = $db->fetchAll($query);
    $link_list = [];
    foreach ($rows as $row) {
        $name = $row['title'];
        $link = $row['url'];
        $avatar = $row['logo'] ?? '';
        $descr = $row['description'];
        $siteshot = "https://v2.xxapi.cn/api/screenshot?url=" . urlencode($link) . "&return=302";

        $link_list[] = [
            "name" => $name,
            "link" => $link,
            "avatar" => $avatar,
            "descr" => $descr,
            "siteshot" => $siteshot
        ];
    }
    $length = count($link_list);
    $output = [
        "link_list" => $link_list,
        "length" => $length
    ];
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} catch (\Typecho\Db\Exception $e) {
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode(['error' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
}