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

<?php

namespace WpDownloader;

use WpDownloader\Log;

class Utility
{
    public static function deleteDirectory($path)
    {
        if (!file_exists($path)) {
            Log::write("deleteDirectory: {$path} does not exist, so it cannot be deleted.");
            return;
        }

        if (!is_dir($path)) {
            Log::throwException("deleteDirectory: {$path} is not a directory.");
        }

        self::emptyDirectory($path);

        return rmdir($path);
    }

    public static function emptyDirectory($path)
    {
        if ($path == '/') {
            throw new Exception("Attempt to delete {$path} was prevented.");
        }

        $it = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($files as $file) {
            if ($file->isDir()) {
                rmdir($file->getRealPath());
            } else {
                unlink($file->getRealPath());
            }
        }
    }

    public static function copyPath($copyFrom, $copyTo)
    {
        $dirPerms = 0755;

        if (!file_exists($copyFrom)) {
            Log::write("Directory Copy: Source {$copyFrom} does not exist.", Log::WARN);
            return;
        }

        $toDir = dirname($copyTo);
        if (!file_exists($toDir)) {
            mkdir($toDir, $dirPerms, true);
        }

        if (is_file($copyFrom)) {
            return copy($copyFrom, $copyTo);
        }

        if (file_exists($copyTo) && !is_dir($copyTo)) {
            Log::throwException("Directory Copy: Destination {$copyTo} exists and is not a directory.");
        }

        if (is_dir($copyFrom)) {
            $copyFromBase = basename($copyFrom);
            $copyTo = "$copyTo/$copyFromBase";
        }

        if (!file_exists($copyTo)) {
            mkdir($copyTo, $dirPerms, true);
        }


        $iterator = new \RecursiveIteratorIterator(
          new \RecursiveDirectoryIterator($copyFrom, \RecursiveDirectoryIterator::SKIP_DOTS),
          \RecursiveIteratorIterator::SELF_FIRST
        );
        foreach ($iterator as $item) {
            $dest = "$copyTo/{$iterator->getSubPathName()}";

            if ($item->isDir()) {
                if (file_exists($dest)) {
                    continue;
                }

                if (!mkdir($dest, $dirPerms, true)) {
                    Log::write("Failed to create directory $copyTo/{$iterator->getSubPathName()}", Log::WARN);
                }
            } else {
                if (!copy($item, $dest)) {
                    Log::write("Failed to copy $item to $copyTo/{$iterator->getSubPathName()}", Log::WARN);
                }
            }
        }
    }

    public static function loadJsonFile($jsonFile, $asArray=true)
    {
        $json = file_get_contents($jsonFile);
        $decodedJson = json_decode($json, $asArray);
        if (json_last_error()) {
            Log::throwException("Error decoding JSON file $jsonFile: ".json_last_error_msg().'json was: '.substr($responseText, 0, 600));
        }

        return $decodedJson;
    }
}
Back to Directory File Manager