| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class for handling service to get data from file system to frontend |
| 5 |
* Call should already be verified by permissions callback |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPSynchro\API; |
| 10 |
|
| 11 |
use WPSynchro\Files\FileHelperFunctions; |
| 12 |
use WPSynchro\Files\PathData; |
| 13 |
use WPSynchro\Utilities\CommonFunctions; |
| 14 |
use WPSynchro\Transport\RemoteTransport; |
| 15 |
use WPSynchro\Migration\Migration; |
| 16 |
use WPSynchro\Transport\Destination; |
| 17 |
use WPSynchro\Utilities\PluginDirs; |
| 18 |
|
| 19 |
class Filesystem extends WPSynchroService |
| 20 |
{ |
| 21 |
public function service() |
| 22 |
{ |
| 23 |
$body = $this->getRequestBody(); |
| 24 |
$parameters = json_decode($body); |
| 25 |
// Extract parameters |
| 26 |
if (isset($parameters->path)) { |
| 27 |
$path = $parameters->path; |
| 28 |
} else { |
| 29 |
http_response_code(400); |
| 30 |
return; |
| 31 |
} |
| 32 |
if (isset($parameters->migration)) { |
| 33 |
$migration = Migration::map($parameters->migration); |
| 34 |
} else { |
| 35 |
$migration = new Migration(); |
| 36 |
} |
| 37 |
if (isset($parameters->url)) { |
| 38 |
$url = $parameters->url; |
| 39 |
} else { |
| 40 |
$url = ""; |
| 41 |
} |
| 42 |
if (isset($parameters->isLocal)) { |
| 43 |
$is_local = $parameters->isLocal; |
| 44 |
} else { |
| 45 |
$is_local = true; |
| 46 |
} |
| 47 |
|
| 48 |
// If it is not local, call the other site on same service |
| 49 |
if (!$is_local) { |
| 50 |
$remote_request = new \stdClass(); |
| 51 |
$remote_request->path = $path; |
| 52 |
|
| 53 |
$destination = new Destination(Destination::REMOTE); |
| 54 |
$destination->setMigration($migration); |
| 55 |
|
| 56 |
// Get remote transfer object |
| 57 |
$remotetransport = new RemoteTransport(); |
| 58 |
$remotetransport->setDestination($destination); |
| 59 |
$remotetransport->init(); |
| 60 |
$remotetransport->setUrl($url); |
| 61 |
$remotetransport->setDataObject($remote_request); |
| 62 |
$remotetransport->setSendDataAsJSON(); |
| 63 |
$result = $remotetransport->remotePOST(); |
| 64 |
|
| 65 |
if ($result->isSuccess()) { |
| 66 |
$result_body = $result->getBody(); |
| 67 |
echo json_encode($result_body); |
| 68 |
return; |
| 69 |
} |
| 70 |
http_response_code(400); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$common = new CommonFunctions(); |
| 75 |
$plugins_dirs = new PluginDirs(); |
| 76 |
$log_path = $plugins_dirs->getUploadsFilePath(); |
| 77 |
|
| 78 |
// Paths that should NOT be syncable |
| 79 |
$locked_paths = []; |
| 80 |
$locked_paths[] = $common->fixPath(trim($log_path, '/')); |
| 81 |
$locked_paths[] = $common->fixPath(trim(WPSYNCHRO_PLUGIN_DIR, '/')); |
| 82 |
$locked_paths[] = $common->fixPath(ABSPATH . "wp-admin"); |
| 83 |
$locked_paths[] = $common->fixPath(ABSPATH . "wp-includes"); |
| 84 |
$files_in_webroot = FileHelperFunctions::getWPFilesInWebrootToExclude(); |
| 85 |
foreach ($files_in_webroot as $filewebroot) { |
| 86 |
$locked_paths[] = $common->fixPath($filewebroot); |
| 87 |
} |
| 88 |
|
| 89 |
$result = new \stdClass(); |
| 90 |
$pathdata_list = []; |
| 91 |
|
| 92 |
if (file_exists($path)) { |
| 93 |
$files = []; |
| 94 |
$presorteddata = array_diff(scandir($path), ['..', '.']); |
| 95 |
foreach ($presorteddata as $file) { |
| 96 |
if (is_file($file)) { |
| 97 |
array_push($files, $file); |
| 98 |
} else { |
| 99 |
array_unshift($files, $file); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
foreach ($files as $file) { |
| 104 |
$pathdata = new PathData(); |
| 105 |
$pathdata->absolutepath = trailingslashit($path) . $file; |
| 106 |
if (is_file($pathdata->absolutepath)) { |
| 107 |
$pathdata->is_file = true; |
| 108 |
} else { |
| 109 |
// is dir, check for subdirs |
| 110 |
$directories = array_diff(scandir($pathdata->absolutepath), ['..', '.']); |
| 111 |
if ($directories != false && count($directories) > 0) { |
| 112 |
$pathdata->dir_has_content = true; |
| 113 |
$pathdata->is_expanded = false; |
| 114 |
} |
| 115 |
} |
| 116 |
$pathdata->basename = basename($pathdata->absolutepath); |
| 117 |
|
| 118 |
// Check for locked paths |
| 119 |
foreach ($locked_paths as $lpath) { |
| 120 |
if (strpos($pathdata->absolutepath, $lpath) !== false) { |
| 121 |
$pathdata->locked = true; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$pathdata_list[] = $pathdata; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$result->pathdata = $pathdata_list; |
| 131 |
|
| 132 |
echo json_encode($result); |
| 133 |
return; |
| 134 |
} |
| 135 |
} |
| 136 |
|