PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 2.1.7
Backup Migration v2.1.7
2.1.7 2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / uploader / chunks.php
backup-backup / includes / uploader Last commit date
chunks.php 2 weeks ago
chunks.php
135 lines
1 <?php
2
3 // Namespace
4 namespace BMI\Plugin\Uploader;
5
6 // Use
7 use BMI\Plugin\Zipper\BMI_Zipper AS Zipper;
8
9 // Exit on direct access
10 if (!defined('ABSPATH')) exit;
11
12 header('Content-Type: application/json; charset=utf-8');
13 $file = isset($_FILES['file_data']) ? $_FILES['file_data'] : null;
14 $name = isset($this->post['file_name']) ? $this->post['file_name'] : null;
15 $total = isset($this->post['file_total']) ? $this->post['file_total'] : 0;
16 $index = isset($this->post['file_index']) ? $this->post['file_index'] : 0;
17 $size = isset($this->post['file_size']) ? $this->post['file_size'] : null;
18 $taskStart = isset($this->post['taskStart']) ? $this->post['taskStart'] : 123;
19
20 $info = pathinfo($name);
21 $ext = isset($info['extension']) ? $info['extension'] : '';
22
23 $beforename = $name;
24 $name = substr($name, 0, -(strlen($ext) + 1));
25 $noextname = $name;
26 $file_name = $name . '-' . $taskStart . '.' . $ext;
27 $newfile = BMI_BACKUPS . '/' . $file_name . '.part'; // Temporary .part file during upload
28 $afterfile = BMI_BACKUPS . '/' . $beforename;
29 $url = plugin_dir_url(BMI_ROOT_FILE) . '/backups' . '/' . $file_name;
30
31 function jsonMsg($status, $message, $url = '', $afterfile = false) {
32
33 if ($status === 2) {
34
35 require_once BMI_INCLUDES . '/zipper/zipping.php';
36 $zipper = new Zipper();
37 $manifest = $zipper->getZipFileContent($afterfile, 'bmi_backup_manifest.json');
38
39 if ($manifest === false) {
40 @unlink($afterfile);
41 $status = 5;
42 }
43
44 }
45
46 $arr['status'] = $status;
47 $arr['message'] = $message;
48 $arr['url'] = $url;
49
50 echo json_encode($arr);
51 die();
52
53 }
54
55 if (!$file || !$name) {
56
57 jsonMsg(0, 'Missing file?');
58
59 }
60
61 $imgarr = array('zip', 'tar', 'gz');
62 if (!in_array(strtolower($ext), $imgarr)) {
63
64 jsonMsg(0, 'Invalid file type');
65
66 }
67
68 function completed($newfile, $noextname, $ext) {
69
70 chmod($newfile, 0755);
71
72 $i = 1;
73 $curr_fil = BMI_BACKUPS . '/' . $noextname . '.' . $ext;
74 while (file_exists($curr_fil)) {
75 $i++;
76 $curr_fil = BMI_BACKUPS . '/' . $noextname . '-' . $i . '.' . $ext;
77 }
78 rename($newfile, $curr_fil);
79
80 }
81
82 clearstatcache(true, $afterfile);
83
84 if (is_file($afterfile) && ($size == filesize($afterfile))) {
85
86 jsonMsg(3, 'File already exists.', $url);
87
88 }
89
90 if ($file['error'] == 0) {
91
92 if (!file_exists($newfile)) {
93
94 if (!move_uploaded_file($file['tmp_name'], $newfile)) {
95
96 jsonMsg(0, 'Cannot move file');
97
98 }
99
100 if ($index == $total) {
101
102 completed($newfile, $noextname, $ext);
103 jsonMsg(2, 'Upload completed', $url, $afterfile);
104
105 }
106
107 jsonMsg(1, 'Uploading');
108
109 }
110
111 if ($index <= $total) {
112
113 $content = file_get_contents($file['tmp_name']);
114 if (!file_put_contents($newfile, $content, FILE_APPEND)) {
115
116 jsonMsg(0, 'Cannot write file');
117
118 }
119
120 if ($index == $total) {
121
122 completed($newfile, $noextname, $ext);
123 jsonMsg(2, 'Upload completed', $url, $afterfile);
124
125 }
126
127 jsonMsg(1, 'Uploading');
128 }
129
130 } else {
131
132 jsonMsg(0, 'No file uploaded');
133
134 }
135