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