cdn
7 months ago
data_structure
7 months ago
activation.cls.php
7 months ago
admin-display.cls.php
7 months ago
admin-settings.cls.php
7 months ago
admin.cls.php
7 months ago
api.cls.php
7 months ago
avatar.cls.php
7 months ago
base.cls.php
7 months ago
cdn.cls.php
7 months ago
cloud.cls.php
7 months ago
conf.cls.php
7 months ago
control.cls.php
7 months ago
core.cls.php
7 months ago
crawler-map.cls.php
7 months ago
crawler.cls.php
7 months ago
css.cls.php
7 months ago
data.cls.php
7 months ago
data.upgrade.func.php
7 months ago
db-optm.cls.php
7 months ago
debug2.cls.php
7 months ago
doc.cls.php
7 months ago
error.cls.php
7 months ago
esi.cls.php
7 months ago
file.cls.php
7 months ago
gui.cls.php
7 months ago
health.cls.php
7 months ago
htaccess.cls.php
7 months ago
img-optm.cls.php
7 months ago
import.cls.php
7 months ago
import.preset.cls.php
7 months ago
lang.cls.php
7 months ago
localization.cls.php
7 months ago
media.cls.php
7 months ago
metabox.cls.php
7 months ago
object-cache-wp.cls.php
7 months ago
object-cache.cls.php
7 months ago
object.lib.php
7 months ago
optimize.cls.php
7 months ago
optimizer.cls.php
7 months ago
placeholder.cls.php
7 months ago
purge.cls.php
7 months ago
report.cls.php
7 months ago
rest.cls.php
7 months ago
root.cls.php
7 months ago
router.cls.php
7 months ago
str.cls.php
7 months ago
tag.cls.php
7 months ago
task.cls.php
7 months ago
tool.cls.php
7 months ago
ucss.cls.php
7 months ago
utility.cls.php
7 months ago
vary.cls.php
7 months ago
vpi.cls.php
7 months ago
import.cls.php
214 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | |
| 4 | /** |
| 5 | * The import/export class. |
| 6 | * |
| 7 | * @since 1.8.2 |
| 8 | */ |
| 9 | |
| 10 | namespace LiteSpeed; |
| 11 | |
| 12 | defined('WPINC') || exit(); |
| 13 | |
| 14 | class Import extends Base { |
| 15 | |
| 16 | protected $_summary; |
| 17 | |
| 18 | const TYPE_IMPORT = 'import'; |
| 19 | const TYPE_EXPORT = 'export'; |
| 20 | const TYPE_RESET = 'reset'; |
| 21 | |
| 22 | /** |
| 23 | * Init |
| 24 | * |
| 25 | * @since 1.8.2 |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | Debug2::debug('Import init'); |
| 29 | |
| 30 | $this->_summary = self::get_summary(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Export settings to file |
| 35 | * |
| 36 | * @since 1.8.2 |
| 37 | * @since 7.3 added download content type |
| 38 | * @access public |
| 39 | */ |
| 40 | public function export( $only_data_return = false ) { |
| 41 | $raw_data = $this->get_options(true); |
| 42 | |
| 43 | $data = array(); |
| 44 | foreach ($raw_data as $k => $v) { |
| 45 | $data[] = \json_encode(array( $k, $v )); |
| 46 | } |
| 47 | |
| 48 | $data = implode("\n\n", $data); |
| 49 | |
| 50 | if ($only_data_return) { |
| 51 | return $data; |
| 52 | } |
| 53 | |
| 54 | $filename = $this->_generate_filename(); |
| 55 | |
| 56 | // Update log |
| 57 | $this->_summary['export_file'] = $filename; |
| 58 | $this->_summary['export_time'] = time(); |
| 59 | self::save_summary(); |
| 60 | |
| 61 | Debug2::debug('Import: Saved to ' . $filename); |
| 62 | |
| 63 | @header('Content-Type: application/octet-stream'); |
| 64 | @header('Content-Disposition: attachment; filename=' . $filename); |
| 65 | echo $data; |
| 66 | |
| 67 | exit(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Import settings from file |
| 72 | * |
| 73 | * @since 1.8.2 |
| 74 | * @access public |
| 75 | */ |
| 76 | public function import( $file = false ) { |
| 77 | if (!$file) { |
| 78 | if (empty($_FILES['ls_file']['name']) || substr($_FILES['ls_file']['name'], -5) != '.data' || empty($_FILES['ls_file']['tmp_name'])) { |
| 79 | Debug2::debug('Import: Failed to import, wrong ls_file'); |
| 80 | |
| 81 | $msg = __('Import failed due to file error.', 'litespeed-cache'); |
| 82 | Admin_Display::error($msg); |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $this->_summary['import_file'] = $_FILES['ls_file']['name']; |
| 88 | |
| 89 | $data = file_get_contents($_FILES['ls_file']['tmp_name']); |
| 90 | } else { |
| 91 | $this->_summary['import_file'] = $file; |
| 92 | |
| 93 | $data = file_get_contents($file); |
| 94 | } |
| 95 | |
| 96 | // Update log |
| 97 | $this->_summary['import_time'] = time(); |
| 98 | self::save_summary(); |
| 99 | |
| 100 | $ori_data = array(); |
| 101 | try { |
| 102 | // Check if the data is v4+ or not |
| 103 | if (strpos($data, '["_version",') === 0) { |
| 104 | Debug2::debug('[Import] Data version: v4+'); |
| 105 | $data = explode("\n", $data); |
| 106 | foreach ($data as $v) { |
| 107 | $v = trim($v); |
| 108 | if (!$v) { |
| 109 | continue; |
| 110 | } |
| 111 | list($k, $v) = \json_decode($v, true); |
| 112 | $ori_data[$k] = $v; |
| 113 | } |
| 114 | } else { |
| 115 | $ori_data = \json_decode(base64_decode($data), true); |
| 116 | } |
| 117 | } catch (\Exception $ex) { |
| 118 | Debug2::debug('[Import] ❌ Failed to parse serialized data'); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | if (!$ori_data) { |
| 123 | Debug2::debug('[Import] ❌ Failed to import, no data'); |
| 124 | return false; |
| 125 | } else { |
| 126 | Debug2::debug('[Import] Importing data', $ori_data); |
| 127 | } |
| 128 | |
| 129 | $this->cls('Conf')->update_confs($ori_data); |
| 130 | |
| 131 | if (!$file) { |
| 132 | Debug2::debug('Import: Imported ' . $_FILES['ls_file']['name']); |
| 133 | |
| 134 | $msg = sprintf(__('Imported setting file %s successfully.', 'litespeed-cache'), $_FILES['ls_file']['name']); |
| 135 | Admin_Display::success($msg); |
| 136 | } else { |
| 137 | Debug2::debug('Import: Imported ' . $file); |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Reset all configs to default values. |
| 145 | * |
| 146 | * @since 2.6.3 |
| 147 | * @access public |
| 148 | */ |
| 149 | public function reset() { |
| 150 | $options = $this->cls('Conf')->load_default_vals(); |
| 151 | |
| 152 | $this->cls('Conf')->update_confs($options); |
| 153 | |
| 154 | Debug2::debug('[Import] Reset successfully.'); |
| 155 | |
| 156 | $msg = __('Reset successfully.', 'litespeed-cache'); |
| 157 | Admin_Display::success($msg); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Generate the filename to export |
| 162 | * |
| 163 | * @since 1.8.2 |
| 164 | * @access private |
| 165 | */ |
| 166 | private function _generate_filename() { |
| 167 | // Generate filename |
| 168 | $parsed_home = parse_url(get_home_url()); |
| 169 | $filename = 'LSCWP_cfg-'; |
| 170 | if (!empty($parsed_home['host'])) { |
| 171 | $filename .= $parsed_home['host'] . '_'; |
| 172 | } |
| 173 | |
| 174 | if (!empty($parsed_home['path'])) { |
| 175 | $filename .= $parsed_home['path'] . '_'; |
| 176 | } |
| 177 | |
| 178 | $filename = str_replace('/', '_', $filename); |
| 179 | |
| 180 | $filename .= '-' . date('Ymd_His') . '.data'; |
| 181 | |
| 182 | return $filename; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Handle all request actions from main cls |
| 187 | * |
| 188 | * @since 1.8.2 |
| 189 | * @access public |
| 190 | */ |
| 191 | public function handler() { |
| 192 | $type = Router::verify_type(); |
| 193 | |
| 194 | switch ($type) { |
| 195 | case self::TYPE_IMPORT: |
| 196 | $this->import(); |
| 197 | break; |
| 198 | |
| 199 | case self::TYPE_EXPORT: |
| 200 | $this->export(); |
| 201 | break; |
| 202 | |
| 203 | case self::TYPE_RESET: |
| 204 | $this->reset(); |
| 205 | break; |
| 206 | |
| 207 | default: |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | Admin::redirect(); |
| 212 | } |
| 213 | } |
| 214 |