chunks.php
134 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($afterfile); |
| 83 | if (is_file($afterfile) && ($size == filesize($afterfile))) { |
| 84 | |
| 85 | jsonMsg(3, 'File already exists.', $url); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | if ($file['error'] == 0) { |
| 90 | |
| 91 | if (!file_exists($newfile)) { |
| 92 | |
| 93 | if (!move_uploaded_file($file['tmp_name'], $newfile)) { |
| 94 | |
| 95 | jsonMsg(0, 'Cannot move file'); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | if ($index == $total) { |
| 100 | |
| 101 | completed($newfile, $noextname, $ext); |
| 102 | jsonMsg(2, 'Upload completed', $url, $afterfile); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | jsonMsg(1, 'Uploading'); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | if ($index <= $total) { |
| 111 | |
| 112 | $content = file_get_contents($file['tmp_name']); |
| 113 | if (!file_put_contents($newfile, $content, FILE_APPEND)) { |
| 114 | |
| 115 | jsonMsg(0, 'Cannot write file'); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | if ($index == $total) { |
| 120 | |
| 121 | completed($newfile, $noextname, $ext); |
| 122 | jsonMsg(2, 'Upload completed', $url, $afterfile); |
| 123 | |
| 124 | } |
| 125 | |
| 126 | jsonMsg(1, 'Uploading'); |
| 127 | } |
| 128 | |
| 129 | } else { |
| 130 | |
| 131 | jsonMsg(0, 'No file uploaded'); |
| 132 | |
| 133 | } |
| 134 |