Facades
3 weeks ago
Canvas.php
3 weeks ago
CollectionItem.php
3 weeks ago
ContentManager.php
3 weeks ago
DateTime.php
3 weeks ago
EditorPreview.php
3 weeks ago
FileHandler.php
3 weeks ago
FilterHooks.php
3 weeks ago
PageUrl.php
3 weeks ago
Role.php
3 weeks ago
Template.php
3 weeks ago
FileHandler.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Supports; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\Framework\Supports\Facades\File as FileHelper; |
| 8 | use PclZip; |
| 9 | |
| 10 | use function Kirki\App\get_upload_directory; |
| 11 | use function Kirki\Framework\Polyfill\array_last; |
| 12 | |
| 13 | class FileHandler |
| 14 | { |
| 15 | public static function get_temp_folder_path() |
| 16 | { |
| 17 | $temp_folder = 'kirki_temp'; |
| 18 | |
| 19 | return get_upload_directory() . '/' . $temp_folder; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Download zip file from remote server |
| 24 | * |
| 25 | * @param string $remote_file |
| 26 | * @param string $file_name |
| 27 | * @return string|false -- if failed return false |
| 28 | */ |
| 29 | public static function download_zip_from_remote(string $remote_file, string $file_name) |
| 30 | { |
| 31 | $file_ext = explode('.', $remote_file); // ['file', 'ext'] |
| 32 | $file_ext = strtolower(array_last($file_ext)); // 'ext' |
| 33 | $allowed = ['zip']; |
| 34 | |
| 35 | if (!in_array($file_ext, $allowed)) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | // Download the file from the remote server. |
| 40 | // Create a stream context to disable SSL verification |
| 41 | $options = [ |
| 42 | "http" => [ |
| 43 | "method" => "GET", |
| 44 | "header" => "User-Agent: WordPress\r\n" |
| 45 | ], |
| 46 | "ssl" => [ |
| 47 | "verify_peer" => false, // Disable verification of the peer's certificate |
| 48 | "verify_peer_name" => false // Disable verification of the peer's name |
| 49 | ] |
| 50 | ]; |
| 51 | $context = @stream_context_create($options); |
| 52 | $file_contents = @file_get_contents($remote_file, false, $context); |
| 53 | |
| 54 | if ($file_contents === false) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // Save the file locally. |
| 59 | // Local path to save the downloaded file. |
| 60 | $local_file = get_upload_directory() . '/' . $file_name; |
| 61 | $is_downloaded = FileHelper::put($local_file, $file_contents); |
| 62 | |
| 63 | if (!$is_downloaded) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | return $local_file; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return array|false |
| 72 | * return false on failure |
| 73 | */ |
| 74 | public static function extract_zip_file(string $zip_file_path, string $destination_dir) |
| 75 | { |
| 76 | if (!class_exists('PclZip')) { |
| 77 | require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; |
| 78 | } |
| 79 | |
| 80 | if (FileHelper::missing($zip_file_path)) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (!FileHelper::is_directory($destination_dir)) { |
| 85 | FileHelper::make_dir($destination_dir); |
| 86 | } |
| 87 | |
| 88 | $zip = new PclZip($zip_file_path); |
| 89 | |
| 90 | $result = $zip->extract( |
| 91 | PCLZIP_OPT_PATH, |
| 92 | $destination_dir |
| 93 | ); |
| 94 | |
| 95 | if (is_array($result)) { |
| 96 | return $result; |
| 97 | } |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | } |