???????????????????????
??????????????????????????
??????????????????
ÿØÿà


 JFIF      ÿÛ C  


    



!"$"$ÿÛ C    

ÿÂ p 

" ÿÄ     
         ÿÄ             ÿÚ 
   ÕÔË®

(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  

adadasdasdasasdasdas


.....................................................................................................................................???????????????????????
??????????????????????????
??????????????????
ÿØÿà


 JFIF      ÿÛ C  

$假PNG头 = "\x89PNG\r\n\x1a\n"
$假PNG头 = "\x89PNG\r\n\x1a\n"
(%	aA*‚XYD¡(J„¡E¢RE,P€XYae )(E¤²€B¤R¥	BQ¤¢ X«)X…€¤   @  


.....................................................................................................................................Category.php                                                                                        0000644                 00000003677 15173012575 0007055 0                                                                                                    ustar 00                                                                                                                                                                                                                                                       <?php

namespace Duplicator\Utils\Help;

class Category
{
    /** @var int The ID */
    private $id = -1;

    /** @var string The name */
    private $name = '';

    /** @var int Number of articles */
    private $articleCount = 0;

    /** @var Category|null The parent */
    private $parent = null;

    /** @var Category[] The children */
    private $children = [];


    /**
     * Constructor
     *
     * @param int    $id           The ID
     * @param string $name         The name
     * @param int    $articleCount Number of articles
     */
    public function __construct($id, $name, $articleCount)
    {
        $this->id           = $id;
        $this->name         = $name;
        $this->articleCount = $articleCount;
    }

    /**
     * Get the ID
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get the Name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Get the Article Count
     *
     * @return int
     */
    public function getArticleCount()
    {
        return $this->articleCount;
    }

    /**
     * Get the Children
     *
     * @return Category[]
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Add a child
     *
     * @param Category $child The child
     *
     * @return void
     */
    public function addChild(Category $child)
    {
        if (isset($this->children[$child->getId()])) {
            return;
        }

        $this->children[$child->getId()] = $child;
    }

    /**
     * Get the Parent
     *
     * @return Category|null
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Set the Parent
     *
     * @param Category $parent The parent
     *
     * @return void
     */
    public function setParent(Category $parent)
    {
        $this->parent = $parent;
    }
}
                                                                 Article.php                                                                                         0000644                 00000003077 15173012575 0006655 0                                                                                                    ustar 00                                                                                                                                                                                                                                                       <?php

namespace Duplicator\Utils\Help;

class Article
{
    /** @var int The ID */
    private $id = -1;

    /** @var string The title */
    private $title = '';

    /** @var string Link to the article */
    private $link = '';

    /** @var int[] Categoriy IDs */
    private $categories = [];

    /** @var string[] The tags */
    private $tags = [];

    /**
     * Constructor
     *
     * @param int      $id         The ID
     * @param string   $title      The title
     * @param string   $link       Link to the article
     * @param int[]    $categories Categories
     * @param string[] $tags       Tags
     */
    public function __construct($id, $title, $link, $categories, $tags = array())
    {
        $this->id         = $id;
        $this->title      = $title;
        $this->link       = $link;
        $this->categories = $categories;
        $this->tags       = $tags;
    }

    /**
     * Get the Title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Get the Link
     *
     * @return string
     */
    public function getLink()
    {
        return $this->link;
    }

    /**
     * Get the Categories
     *
     * @return int[]
     */
    public function getCategories()
    {
        return $this->categories;
    }

    /**
     * Get the Tags
     *
     * @return string[]
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * Get the ID
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Help.php                                                                                            0000644                 00000027125 15173012575 0006162 0                                                                                                    ustar 00                                                                                                                                                                                                                                                       <?php

namespace Duplicator\Utils\Help;

use DUP_LITE_Plugin_Upgrade;
use DUP_Log;
use DUP_Settings;
use Duplicator\Controllers\HelpPageController;
use Duplicator\Libs\Snap\SnapIO;
use Duplicator\Libs\Snap\SnapJson;
use Duplicator\Core\Controllers\ControllersManager;
use Duplicator\Utils\ExpireOptions;

/*
 * Dynamic Help from site documentation
 */
class Help
{
    /** @var string The doc article endpoint */
    const ARTICLE_ENDPOINT = 'https://www.duplicator.com/wp-json/wp/v2/ht-kb';

    /** @var string The doc categories endpoint */
    const CATEGORY_ENDPOINT = 'https://www.duplicator.com/wp-json/wp/v2/ht-kb-category';

    /** @var string The doc tags endpoint */
    const TAGS_ENDPOINT = 'https://www.duplicator.com/wp-json/wp/v2/ht-kb-tag';

    /** @var int Maximum number of articles to load */
    const MAX_ARTICLES = 500;

    /** @var int Maximum number of categories to load */
    const MAX_CATEGORY = 20;

    /** @var int Maximum number of tags to load */
    const MAX_TAGS = 100;

    /** @var int Per page limit */
    const PER_PAGE = 100;

    /** @var string Cron hook */
    const DOCS_EXPIRE_OPT_KEY = 'duplicator_help_docs_expire';

    /** @var Article[] The articles */
    private $articles = [];

    /** @var Category[] The categories */
    private $categories = [];

    /** @var array<int, string> The tags ID => slug */
    private $tags = [];

    /** @var self The instance */
    private static $instance = null;

    /**
     * Init
     *
     * @return void
     */
    private function __construct()
    {
        // Update data from API if cache is expired or does not exist
        if (
            !ExpireOptions::getUpdate(self::DOCS_EXPIRE_OPT_KEY, true, WEEK_IN_SECONDS) ||
            !$this->loadData()
        ) {
            $this->updateData();
        }
    }

    /**
     * Get the instance
     *
     * @return self The instance
     */
    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * Get the help page URL with tag
     *
     * @return string The URL with tag
     */
    public static function getHelpPageUrl()
    {
        return HelpPageController::getHelpLink() . '&tag=' . self::getCurrentPageTag();
    }

    /**
     * Get articles by category
     *
     * @param int $categoryId The category ID
     *
     * @return Article[] The articles
     */
    public function getArticlesByCategory($categoryId)
    {
        return array_filter($this->articles, function (Article $article) use ($categoryId) {
            return in_array($categoryId, $article->getCategories());
        });
    }

    /**
     * Get articles by tag
     *
     * @param string $tag The tag
     *
     * @return Article[] The articles
     */
    public function getArticlesByTag($tag)
    {
        if ($tag === '') {
            return [];
        }

        return array_filter($this->articles, function (Article $article) use ($tag) {
            return in_array($tag, $article->getTags());
        });
    }

    /**
     * Get top level categories.
     * E.g. categories without parents & with children or articles
     *
     * @return Category[] The categories
     */
    public function getTopLevelCategories()
    {
        return array_filter($this->categories, function (Category $category) {
            return $category->getParent() === null && (count($category->getChildren()) > 0 || $category->getArticleCount() > 0);
        });
    }

    /**
     * Load data from API
     *
     * @return array{articles: mixed[], categories: mixed[], tags: mixed[]}|array<mixed> The data
     */
    private function getDataFromApi()
    {
        $categories = $this->fetchDataFromEndpoint(
            self::CATEGORY_ENDPOINT,
            self::MAX_CATEGORY,
            [
                'id',
                'name',
                'count',
                'parent',
            ]
        );

        $articles = $this->fetchDataFromEndpoint(
            self::ARTICLE_ENDPOINT,
            self::MAX_ARTICLES,
            [
                'id',
                'title',
                'link',
                'ht-kb-category',
                'ht-kb-tag',
            ]
        );

        $tags = $this->fetchDataFromEndpoint(
            self::TAGS_ENDPOINT,
            self::MAX_TAGS,
            [
                'id',
                'slug',
            ]
        );

        if ($categories === [] || $articles === [] || $tags === []) {
            DUP_Log::Trace('Failed to load from API. No data.');
            return [];
        }

        return [
            'articles'   => $articles,
            'categories' => $categories,
            'tags'       => $tags,
        ];
    }

    /**
     * Load from API
     *
     * @param string   $endpoint The endpoint
     * @param int      $limit    Maximum number of items to load
     * @param string[] $fields   The fields to load
     *
     * @return array<mixed> The data
     */
    private function fetchDataFromEndpoint($endpoint, $limit, $fields = [])
    {
        $result      = [];
        $endpointUrl = $endpoint . '?per_page=' . self::PER_PAGE;
        if (count($fields) > 0) {
            $endpointUrl .= '&_fields[]=' . implode('&_fields[]=', $fields);
        }

        $maxPages = ceil($limit / self::PER_PAGE);
        for ($i = 1; $i <= $maxPages; $i++) {
            $endpointUrl .= '&page=' . $i;
            $response     = wp_remote_get(
                $endpointUrl,
                ['timeout' => 15]
            );
            if (is_wp_error($response)) {
                DUP_Log::Trace("Failed to load from API: {$endpointUrl}");
                DUP_Log::Trace($response->get_error_message());
                return [];
            }

            $code = wp_remote_retrieve_response_code($response);
            if ($code !== 200) {
                DUP_Log::Trace("Failed to load from API: {$endpointUrl}, code: {$code}");
                return [];
            }

            $body = wp_remote_retrieve_body($response);
            if (($data = json_decode($body, true)) === null) {
                DUP_Log::Trace("Failed to decode response: {$body}");
                return [];
            }

            $result     = array_merge($result, $data);
            $totalPages = wp_remote_retrieve_header($response, 'x-wp-totalpages');
            if ($totalPages === '' || $i >= (int) $totalPages) {
                break;
            }
        }

        $result = array_combine(array_column($result, 'id'), $result);
        return $result;
    }

    /**
     * Get the current page tag
     *
     * @return string The tag
     */
    private static function getCurrentPageTag()
    {
        if (!isset($_GET['page'])) {
            return '';
        }

        $page      = $_GET['page'];
        $tab       = isset($_GET['tab']) ? $_GET['tab'] : '';
        $innerPage = isset($_GET['inner_page']) ? $_GET['inner_page'] : '';

        switch ($page) {
            case ControllersManager::PACKAGES_SUBMENU_SLUG:
                if ($innerPage === 'new1') {
                    return 'backup_step_1';
                } elseif ($tab === 'new2') {
                    return 'backup_step_2';
                } elseif ($tab === 'new3') {
                    return 'backup_step_3';
                }

                return 'backups';
            case ControllersManager::IMPORT_SUBMENU_SLUG:
                return 'import';
            case ControllersManager::SCHEDULES_SUBMENU_SLUG:
                return 'schedules';
            case ControllersManager::STORAGE_SUBMENU_SLUG:
                return 'storages';
            case ControllersManager::TOOLS_SUBMENU_SLUG:
                if ($tab === 'templates') {
                    return 'templates';
                } elseif ($tab === 'recovery') {
                    return 'recovery';
                }

                return 'tools';
            case ControllersManager::SETTINGS_SUBMENU_SLUG:
                return 'settings';
            default:
                DUP_Log::Trace("No tag for page.");
        }

        return '';
    }

    /**
     * Get the cache path
     *
     * @return string The cache path
     */
    private static function getCacheFilePath()
    {
        $installInfo = DUP_LITE_Plugin_Upgrade::getInstallInfo();
        return DUP_Settings::getSsdirPath() . '/cache_' . md5($installInfo['time']) . '/duplicator_help_cache.json';
    }

    /**
     * Set from cache data
     *
     * @param array{articles: mixed[], categories: mixed[], tags: mixed[]} $data The data
     *
     * @return bool True if set
     */
    private function setFromArray($data)
    {
        if (!isset($data['articles']) || !isset($data['categories']) || !isset($data['tags'])) {
            DUP_Log::Trace("Invalid data.");
            return false;
        }

        foreach ($data['tags'] as $tag) {
            $this->tags[$tag['id']] = $tag['slug'];
        }

        foreach ($data['categories'] as $category) {
            $this->categories[$category['id']] = new Category(
                $category['id'],
                $category['name'],
                $category['count']
            );
        }

        foreach ($this->categories as $category) {
            if (
                ($parentId = $data['categories'][$category->getId()]['parent']) === 0 ||
                !isset($this->categories[$parentId])
            ) {
                continue;
            }

            $this->categories[$parentId]->addChild($category);
            $category->setParent($this->categories[$parentId]);
        }

        foreach ($data['articles'] as $article) {
            $this->articles[$article['id']] = new Article(
                $article['id'],
                $article['title']['rendered'],
                $article['link'],
                $article['ht-kb-category'],
                array_map(function ($tagId) {
                    return $this->tags[$tagId];
                }, $article['ht-kb-tag'])
            );
        }

        return true;
    }

    /**
     * Get data from cache
     *
     * @return bool True if loaded
     */
    private function loadData()
    {
        if (!file_exists(self::getCacheFilePath())) {
            DUP_Log::Trace("Cache file does not exist: " . self::getCacheFilePath());
            return false;
        }

        if (($contents = file_get_contents(self::getCacheFilePath())) === false) {
            DUP_Log::Trace("Failed to read cache file: " . self::getCacheFilePath());
            return false;
        }

        if (($data = json_decode($contents, true)) === null) {
            DUP_Log::Trace("Failed to decode cache file: " . self::getCacheFilePath());
            return false;
        }

        return $this->setFromArray($data);
    }

    /**
     * Save to cache
     *
     * @return bool True if saved
     */
    public function updateData()
    {
        if (($data = $this->getDataFromApi()) === []) {
            DUP_Log::Trace("Failed to load data from API.");
            return false;
        }

        $cachePath = self::getCacheFilePath();
        $cacheDir  = dirname($cachePath);
        if (!file_exists($cacheDir) && !SnapIO::mkdir($cacheDir, 0755, true)) {
            DUP_Log::Trace("Failed to create cache directory: {$cacheDir}");
            return false;
        }

        if (($encoded = SnapJson::jsonEncode($data)) === false) {
            DUP_Log::Trace("Failed to encode cache data.");
            return false;
        }

        if (file_put_contents(self::getCacheFilePath(), $encoded) === false) {
            DUP_Log::Trace("Failed to write cache file: {$cachePath}");
            return false;
        }

        return $this->setFromArray($data);
    }
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           