Config.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Transferito\Models\Core; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | class Config { |
| 8 | |
| 9 | public static function getBasePath() |
| 10 | { |
| 11 | return TRANSFERITO_UPLOAD_PATH; |
| 12 | } |
| 13 | |
| 14 | public static function getChunkSize() |
| 15 | { |
| 16 | $settings = get_option('transferito_settings_option'); |
| 17 | $uploadChunkSize = isset($settings['transferito_upload_chunk_size']) ? $settings['transferito_upload_chunk_size'] : false; |
| 18 | |
| 19 | if (!$uploadChunkSize) { |
| 20 | return TRANSFERITO_CHUNK_SIZE; |
| 21 | } |
| 22 | |
| 23 | $newUploadChunkSize = intval($uploadChunkSize); |
| 24 | |
| 25 | return ($newUploadChunkSize * 1024 * 1024); |
| 26 | } |
| 27 | |
| 28 | public static function getUploadPath() |
| 29 | { |
| 30 | return wp_upload_dir()["path"]; |
| 31 | } |
| 32 | |
| 33 | public static function getBaseApiUrl() |
| 34 | { |
| 35 | return 'https://api.transferito.com/wp'; |
| 36 | } |
| 37 | |
| 38 | public static function getEndpoint($endpoint) |
| 39 | { |
| 40 | return self::getBaseApiUrl() . '/' . $endpoint; |
| 41 | } |
| 42 | |
| 43 | public static function getCorrectPath() |
| 44 | { |
| 45 | $transferitoDir = self::getBasePath(); |
| 46 | |
| 47 | /** |
| 48 | * PHP File to create |
| 49 | */ |
| 50 | $json = "<?php" . PHP_EOL; |
| 51 | $json .= 'header("Content-type: application/json;charset=utf-8");' .PHP_EOL; |
| 52 | $json .= 'http_response_code(200);' .PHP_EOL; |
| 53 | $json .= "echo json_encode([ 'canAccess' => true ]);" .PHP_EOL; |
| 54 | $json .= "die();" .PHP_EOL; |
| 55 | |
| 56 | /** |
| 57 | * |
| 58 | */ |
| 59 | if (!file_exists($transferitoDir)) { |
| 60 | if (!wp_mkdir_p($transferitoDir, 0755)) { |
| 61 | $transferitoDir = self::getUploadPath(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | file_put_contents($transferitoDir . DIRECTORY_SEPARATOR . 'index.html', ''); |
| 66 | file_put_contents($transferitoDir . DIRECTORY_SEPARATOR . 'check-public-access.php', $json); |
| 67 | |
| 68 | return $transferitoDir; |
| 69 | } |
| 70 | |
| 71 | public static function createTestFile() |
| 72 | { |
| 73 | $transferitoDir = self::getBasePath(); |
| 74 | $transferitoHash = bin2hex(openssl_random_pseudo_bytes(32)); |
| 75 | $filename = 'test.txt'; |
| 76 | |
| 77 | /** |
| 78 | * As long as the directory exists |
| 79 | * Create the test file |
| 80 | */ |
| 81 | if (file_exists($transferitoDir)) { |
| 82 | file_put_contents($transferitoDir . DIRECTORY_SEPARATOR . $filename, 'Transferito Hash: ' . $transferitoHash); |
| 83 | } |
| 84 | |
| 85 | return [ |
| 86 | 'url' => site_url() . '/transferito/' . $filename, |
| 87 | 'hash' => $transferitoHash, |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | public static function getWPContentPaths() |
| 92 | { |
| 93 | $paths = []; |
| 94 | $iterator = new \RecursiveIteratorIterator( |
| 95 | new \RecursiveDirectoryIterator(WP_CONTENT_DIR, \RecursiveDirectoryIterator::SKIP_DOTS), |
| 96 | \RecursiveIteratorIterator::SELF_FIRST); |
| 97 | $iterator->setMaxDepth(1); |
| 98 | foreach($iterator as $file) { |
| 99 | if($file->isDir()) { |
| 100 | $depth = $iterator->getDepth(); |
| 101 | if ($depth === 0) { |
| 102 | $paths[$file->getFilename()] = []; |
| 103 | } else if ($depth === 1) { |
| 104 | $fullPath = $file->getRealpath(); |
| 105 | $splitPaths = array_filter(explode(DIRECTORY_SEPARATOR, substr($fullPath, strlen(WP_CONTENT_DIR)))); |
| 106 | |
| 107 | /** |
| 108 | * Check if index 1 & 2 exist |
| 109 | */ |
| 110 | $directoryLevel1Path = isset($splitPaths[1]) ? $splitPaths[1] : false; |
| 111 | $directoryLevel2Path = isset($splitPaths[2]) ? $splitPaths[2] : false; |
| 112 | |
| 113 | /** |
| 114 | * Only push to array if directory paths exist |
| 115 | */ |
| 116 | if ($directoryLevel1Path && $directoryLevel2Path) { |
| 117 | array_push($paths[$directoryLevel1Path], $directoryLevel2Path); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return $paths; |
| 123 | } |
| 124 | } |
| 125 |