shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

backup.php (5136B)


      1 <?php
      2 class ControllerToolBackup extends Controller {
      3 	public function index() {
      4 		$this->load->language('tool/backup');
      5 
      6 		$this->document->setTitle($this->language->get('heading_title'));
      7 
      8 		if (isset($this->session->data['error'])) {
      9 			$data['error_warning'] = $this->session->data['error'];
     10 
     11 			unset($this->session->data['error']);
     12 		} else {
     13 			$data['error_warning'] = '';
     14 		}
     15 
     16 		$data['breadcrumbs'] = array();
     17 
     18 		$data['breadcrumbs'][] = array(
     19 			'text' => $this->language->get('text_home'),
     20 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
     21 		);
     22 
     23 		$data['breadcrumbs'][] = array(
     24 			'text' => $this->language->get('heading_title'),
     25 			'href' => $this->url->link('tool/backup', 'user_token=' . $this->session->data['user_token'], true)
     26 		);
     27 
     28 		$data['user_token'] = $this->session->data['user_token'];
     29 
     30 		$data['export'] = $this->url->link('tool/backup/export', 'user_token=' . $this->session->data['user_token'], true);
     31 		
     32 		$this->load->model('tool/backup');
     33 
     34 		$data['tables'] = $this->model_tool_backup->getTables();
     35 
     36 		$data['header'] = $this->load->controller('common/header');
     37 		$data['column_left'] = $this->load->controller('common/column_left');
     38 		$data['footer'] = $this->load->controller('common/footer');
     39 
     40 		$this->response->setOutput($this->load->view('tool/backup', $data));
     41 	}
     42 	
     43 	public function import() {
     44 		$this->load->language('tool/backup');
     45 		
     46 		$json = array();
     47 		
     48 		if (!$this->user->hasPermission('modify', 'tool/backup')) {
     49 			$json['error'] = $this->language->get('error_permission');
     50 		}
     51 		
     52 		if (isset($this->request->files['import']['tmp_name']) && is_uploaded_file($this->request->files['import']['tmp_name'])) {
     53 			$filename = tempnam(DIR_UPLOAD, 'bac');
     54 			
     55 			move_uploaded_file($this->request->files['import']['tmp_name'], $filename);
     56 		} elseif (isset($this->request->get['import'])) {
     57 			$filename = html_entity_decode($this->request->get['import'], ENT_QUOTES, 'UTF-8');
     58 		} else {
     59 			$filename = '';
     60 		}
     61 		
     62 		if (!is_file($filename)) {
     63 			$json['error'] = $this->language->get('error_file');
     64 		}	
     65 		
     66 		if (isset($this->request->get['position'])) {
     67 			$position = $this->request->get['position'];
     68 		} else {
     69 			$position = 0; 	
     70 		}
     71 				
     72 		if (!$json) {
     73 			// We set $i so we can batch execute the queries rather than do them all at once.
     74 			$i = 0;
     75 			$start = false;
     76 			
     77 			$handle = fopen($filename, 'r');
     78 
     79 			fseek($handle, $position, SEEK_SET);
     80 			
     81 			while (!feof($handle) && ($i < 100)) {
     82 				$position = ftell($handle);
     83 
     84 				$line = fgets($handle, 1000000);
     85 				
     86 				if (substr($line, 0, 14) == 'TRUNCATE TABLE' || substr($line, 0, 11) == 'INSERT INTO') {
     87 					$sql = '';
     88 					
     89 					$start = true;
     90 				}
     91 
     92 				if ($i > 0 && (substr($line, 0, 24) == 'TRUNCATE TABLE `oc_user`' || substr($line, 0, 30) == 'TRUNCATE TABLE `oc_user_group`')) {
     93 					fseek($handle, $position, SEEK_SET);
     94 
     95 					break;
     96 				}
     97 
     98 				if ($start) {
     99 					$sql .= $line;
    100 				}
    101 				
    102 				if ($start && substr($line, -2) == ";\n") {
    103 					$this->db->query(substr($sql, 0, strlen($sql) -2));
    104 					
    105 					$start = false;
    106 				}
    107 					
    108 				$i++;
    109 			}
    110 
    111 			$position = ftell($handle);
    112 
    113 			$size = filesize($filename);
    114 
    115 			$json['total'] = round(($position / $size) * 100);
    116 
    117 			if ($position && !feof($handle)) {
    118 				$json['next'] = str_replace('&amp;', '&', $this->url->link('tool/backup/import', 'user_token=' . $this->session->data['user_token'] . '&import=' . $filename . '&position=' . $position, true));
    119 			
    120 				fclose($handle);
    121 			} else {
    122 				fclose($handle);
    123 				
    124 				unlink($filename);
    125 
    126 				$json['success'] = $this->language->get('text_success');
    127 
    128 				$this->cache->delete('*');
    129 			}
    130 		}
    131 
    132 		$this->response->addHeader('Content-Type: application/json');
    133 		$this->response->setOutput(json_encode($json));
    134 	}
    135 
    136 	public function export() {
    137 		$this->load->language('tool/backup');
    138 
    139 		if (!isset($this->request->post['backup'])) {
    140 			$this->session->data['error'] = $this->language->get('error_export');
    141 
    142 			$this->response->redirect($this->url->link('tool/backup', 'user_token=' . $this->session->data['user_token'], true));
    143 		} elseif (!$this->user->hasPermission('modify', 'tool/backup')) {
    144 			$this->session->data['error'] = $this->language->get('error_permission');
    145 
    146 			$this->response->redirect($this->url->link('tool/backup', 'user_token=' . $this->session->data['user_token'], true));
    147 		} else {
    148 			$this->response->addheader('Pragma: public');
    149 			$this->response->addheader('Expires: 0');
    150 			$this->response->addheader('Content-Description: File Transfer');
    151 			$this->response->addheader('Content-Type: application/octet-stream');
    152 			$this->response->addheader('Content-Disposition: attachment; filename="' . DB_DATABASE . '_' . date('Y-m-d_H-i-s', time()) . '_backup.sql"');
    153 			$this->response->addheader('Content-Transfer-Encoding: binary');
    154 
    155 			$this->load->model('tool/backup');
    156 
    157 			$this->response->setOutput($this->model_tool_backup->backup($this->request->post['backup']));		
    158 		}
    159 	}	
    160 }