PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.22.4
JetBackup – Backup, Restore & Migrate v3.1.22.4
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Ajax / Calls / FileManager.php
backup / src / JetBackup / Ajax / Calls Last commit date
.htaccess 1 year ago AbortQueueItem.php 1 year ago AddToQueue.php 3 months ago ClearAlerts.php 1 year ago ClearCompletedQueueItems.php 1 year ago CreateSupportUser.php 1 year ago DeleteBackupJob.php 1 year ago DeleteDestination.php 1 year ago DeleteDownload.php 1 year ago DeleteSchedule.php 1 year ago DeleteSnapshot.php 1 year ago DestinationSetExportConfig.php 1 year ago DuplicateBackupJob.php 1 year ago EditBackupNotes.php 1 year ago EnableBackupJob.php 1 year ago EnableDestination.php 1 year ago ExecuteCron.php 1 year ago FileManager.php 10 months ago GetBackup.php 1 year ago GetBackupJob.php 1 year ago GetDashboard.php 1 year ago GetDatabaseTables.php 1 year ago GetDestination.php 1 year ago GetLog.php 1 year ago GetQRCode.php 1 year ago GetQueueItem.php 1 year ago GetSchedule.php 1 year ago GetSettingsAutomation.php 1 year ago GetSettingsGeneral.php 1 year ago GetSettingsIntegrations.php 1 year ago GetSettingsLogging.php 1 year ago GetSettingsMaintenance.php 1 year ago GetSettingsNotifications.php 1 year ago GetSettingsPerformance.php 1 year ago GetSettingsRestore.php 1 year ago GetSettingsSecurity.php 1 year ago GetSettingsUpdates.php 1 year ago GetSystemInfo.php 11 months ago ListAlerts.php 1 year ago ListBackupJobs.php 1 year ago ListBackups.php 1 year ago ListDatabaseTables.php 1 year ago ListDestinations.php 1 year ago ListDownloads.php 1 year ago ListQueueItems.php 4 months ago ListSchedules.php 1 year ago LockSnapshot.php 1 year ago ManageBackupJob.php 1 year ago ManageDestination.php 1 year ago ManageSchedule.php 3 months ago ManageSettingsAutomation.php 10 months ago ManageSettingsGeneral.php 4 months ago ManageSettingsIntegrations.php 1 year ago ManageSettingsLogging.php 1 year ago ManageSettingsMaintenance.php 1 year ago ManageSettingsNotifications.php 1 year ago ManageSettingsPerformance.php 1 year ago ManageSettingsRestore.php 1 year ago ManageSettingsSecurity.php 1 week ago ManageSettingsUpdates.php 1 year ago ManageShowcase.php 1 year ago PanelPreload.php 1 year ago SendTestEmail.php 1 year ago StartOverQueueItem.php 1 year ago ValidateDestination.php 1 year ago ValidateMFA.php 1 year ago index.html 1 year ago web.config 1 year ago
FileManager.php
187 lines
1 <?php
2 /*
3 *
4 * JetBackup @ package
5 * Created By Idan Ben-Ezra
6 *
7 * Copyrights @ JetApps
8 * http://www.jetapps.com
9 *
10 **/
11 namespace JetBackup\Ajax\Calls;
12
13 use JetBackup\Ajax\ListRecord;
14 use JetBackup\BackupJob\BackupJob;
15 use JetBackup\CLI\CLI;
16 use JetBackup\Data\Engine;
17 use JetBackup\Destination\Destination;
18 use JetBackup\Destination\Integration\DestinationFile as iDestinationFile;
19 use JetBackup\Entities\Util;
20 use JetBackup\Exception\AjaxException;
21 use JetBackup\Exception\DBException;
22 use JetBackup\Exception\DestinationException;
23 use JetBackup\Exception\JetBackupLinuxException;
24 use JetBackup\Factory;
25 use JetBackup\JetBackup;
26 use JetBackup\JetBackupLinux\JetBackupLinux;
27 use JetBackup\Snapshot\Snapshot;
28 use JetBackup\SocketAPI\Exception\SocketAPIException;
29 use JetBackup\UserInput\UserInput;
30 use JetBackup\Wordpress\Helper;
31 use SleekDB\Exceptions\InvalidArgumentException;
32 use SleekDB\Exceptions\IOException;
33
34 defined("__JETBACKUP__") or die("Restricted Access.");
35
36
37 class FileManager extends ListRecord {
38
39
40 /**
41 * @return int
42 * @throws AjaxException
43 */
44 public function getDestinationId():int { return $this->getUserInput('destination_id', 0, UserInput::UINT); }
45
46 /**
47 /**
48 * @return int
49 * @throws AjaxException
50 */
51 public function getSnapshotId():int { return $this->getUserInput(JetBackup::ID_FIELD, 0, UserInput::UINT); }
52
53 /**
54 * @return string
55 * @throws AjaxException
56 */
57 public function getPath():string { return $this->getUserInput('location', '', UserInput::STRING); }
58
59 /**
60 * @return void
61 * @throws AjaxException
62 * @throws DBException
63 * @throws IOException
64 * @throws InvalidArgumentException
65 * @throws SocketAPIException
66 * @throws DestinationException
67 */
68 public function execute():void {
69 if ($this->getDestinationId()) {
70 $response = $this->getDestinationsFiles();
71 $this->setResponseData($response);
72 return;
73 }
74 $snap = new Snapshot($this->getSnapshotId());
75 if(!$snap->getId()) throw new AjaxException("Invalid snapshot id provided");
76 if($snap->getEngine() != Engine::ENGINE_JB) throw new AjaxException("This feature is only supported for JetBackup Linux snapshots");
77 if($snap->getBackupType() != BackupJob::TYPE_ACCOUNT) throw new AjaxException("This feature is only supported for account backups");
78 if ($snap->getContains() != BackupJob::BACKUP_ACCOUNT_CONTAINS_HOMEDIR &&
79 $snap->getContains() != BackupJob::BACKUP_ACCOUNT_CONTAINS_FULL) {
80 throw new AjaxException("This feature is only supported for backups containing the home directory");
81 }
82
83 $item = null;
84 $items = $snap->getItems();
85
86 foreach($items as $item_details) {
87 if($item_details->getBackupContains() != BackupJob::BACKUP_ACCOUNT_CONTAINS_HOMEDIR) continue;
88 $item = $item_details;
89 break;
90 }
91
92 if(!$item) throw new AjaxException("This backup doesn't contain homedir item");
93 if(!$userHomedir = Helper::getUserHomedir())throw new AjaxException("Unable to fetch linux user homedir");
94
95 $wordpressHomedir = Factory::getWPHelper()->getWordPressHomedir();
96
97 if(!str_starts_with($wordpressHomedir, $userHomedir))
98 throw new AjaxException("The WP public dir doesn't start with the account homedir");
99
100 $public_dir = substr($wordpressHomedir, strlen($userHomedir)+1);
101
102 $path = preg_replace("#/+#", "/", $public_dir . JetBackup::SEP . $this->getPath());
103
104 $sort = $this->getSort();
105 if(isset($sort['_id'])) {
106 $sort['name'] = $sort['_id'];
107 unset($sort['_id']);
108 }
109
110
111 try {
112 $response = JetBackupLinux::fileManager($item->getUniqueId(), $path, $this->getLimit(), $this->getSkip(), $sort);
113 } catch(JetBackupLinuxException $e) {
114 throw new AjaxException($e->getMessage());
115 }
116
117 if(!$this->isCLI()) {
118 $this->setResponseData($response);
119 return;
120 }
121
122 $output = [];
123
124 foreach ($response['files'] as $file) {
125 $output[] = [
126 //'ID' => $file['id'],
127 'Name' => $file['name'] . ($file['link'] ? ' -> ' . $file['link'] : ''),
128 'Type' => $file['icon'] == 'dir' ? 'Directory' : 'File',
129 'Created' => CLI::date(strtotime($file['created'])),
130 'Size' => Util::bytesToHumanReadable($file['size']),
131 ];
132 }
133
134 $this->setResponseData($output);
135 }
136
137 /**
138 * @return array
139 * @throws DestinationException
140 * @throws IOException
141 * @throws DBException
142 * @throws AjaxException
143 * @throws InvalidArgumentException
144 * @throws \Exception
145 */
146 private function getDestinationsFiles(): array
147 {
148 $destination = new Destination($this->getDestinationId());
149 if (!$destination->getId()) {
150 throw new AjaxException("Invalid destination id provided");
151 }
152
153 $iterator = $destination->listDir($this->getPath());
154
155 $limit = $this->getLimit(); // e.g., 10
156 $skip = $this->getSkip(); // e.g., 20
157
158 $allFiles = [];
159
160 while ($iterator->hasNext()) {
161 $file = $iterator->getNext();
162 $name = $file->getName();
163 $type = ($file->getType() == iDestinationFile::TYPE_DIRECTORY) ? 'Directory' : 'File';
164 $path = $file->getPath();
165 $size = $file->getSize();
166 $mtime = Util::date('Y-m-d H:i:s', $file->getModifyTime());
167
168 $allFiles[] = [
169 'name' => $name,
170 'type' => $type,
171 'created' => $mtime,
172 'size' => Util::bytesToHumanReadable($size),
173 'path' => $path,
174 'icon' => $type == 'Directory' ? 'dir' : 'file',
175 ];
176 }
177
178 $totalFiles = count($allFiles);
179 $pagedFiles = array_slice($allFiles, $skip, $limit);
180
181 return [
182 'total' => $totalFiles,
183 'files' => $pagedFiles,
184 ];
185 }
186
187 }