Viewing File: /usr/local/cpanel/3rdparty/wpinstaller/src/WpDownloader/Component/Core.php

<?php
namespace WpDownloader\Component;

use WpDownloader\WpApi;
use WpDownloader\Log;
use WpDownloader\Utility;

class Core extends Base
{
    public $slug = 'core';

    public function getSourcePath()
    {
        return "{$this->localPath}/wordpress";
    }
    public function getDestinationPath()
    {
        return '/';
    }

    public function downloadLatestVersion()
    {
        $latestVersion = WpApi::getLatestVersion();
        $downloadUrl = $latestVersion['download'];
        $version = $latestVersion['version'];

        $fileName = basename($downloadUrl);
        $localFile = $this->downloadFile($downloadUrl, "{$this->temporaryDirectory}/{$fileName}");

        if (!$localFile || !file_exists($localFile)) {
            Log::throwException("Downloaded failed or local file {$localFile} does not exist.");
        }

        $this->extractArchive($localFile);

        Log::write("Verifying checksums");
        $filesWithBadChecksum = $this->findChecksumMismatches($version, "{$this->tempPath}/wordpress");
        $numMismatches = count($filesWithBadChecksum);
        if ($numMismatches > 0) {
            Log::write("Checksum mismatch files:". print_r($filesWithBadChecksum, 1), Log::ERR);
            Log::throwException("Found {$numMismatches} checksums that don't match the local files.");
        }

        
        $this->commitTempToPersistent();

        Log::write("Saving version number to metadata file");
        $this->appendLocalData(['version' => $version]);

        return $version;
    }

    public function getLatestVersion()
    {
        return WpApi::getLatestVersionNumber();
    }

    /**
     * Gets a list of files and their checksums from the WP API, and checks each one against the local version of that file.
     *
     * @param string $version  The version of wordpress to compare against (be the version that is in $filePath). Example: "4.8.3"
     * @param string $filePath The root of a wordpress directory
     */
    public function findChecksumMismatches($version, $filePath)
    {
        $checksumList = WpApi::getFileChecksumsForVersion($version);
        $fileList = array_keys($checksumList);

        return array_reduce($fileList, function ($mismatches, $file) use ($filePath, $checksumList) {
            $remoteChecksum = $checksumList[$file];

            $fullPath = "$filePath/$file";

            $localChecksum = md5_file($fullPath);

            if ($remoteChecksum !== $localChecksum) {
                $mismatches[$file] = [
                    'local'  => $localChecksum,
                    'remote' => $remoteChecksum
                ];
            }

            return $mismatches;
        }, []);
    }
}
Back to Directory File Manager