shop.balmet.com

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

installer.php (5490B)


      1 <?php
      2 class ControllerMarketplaceInstaller extends Controller {
      3 	public function index() {
      4 		$this->load->language('marketplace/installer');
      5 
      6 		$this->document->setTitle($this->language->get('heading_title'));
      7 		
      8 		$data['breadcrumbs'] = array();
      9 
     10 		$data['breadcrumbs'][] = array(
     11 			'text' => $this->language->get('text_home'),
     12 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
     13 		);
     14 
     15 		$data['breadcrumbs'][] = array(
     16 			'text' => $this->language->get('heading_title'),
     17 			'href' => $this->url->link('marketplace/installer', 'user_token=' . $this->session->data['user_token'], true)
     18 		);
     19 
     20 		$data['user_token'] = $this->session->data['user_token'];
     21 		
     22 		$data['header'] = $this->load->controller('common/header');
     23 		$data['column_left'] = $this->load->controller('common/column_left');
     24 		$data['footer'] = $this->load->controller('common/footer');
     25 		
     26 		$this->response->setOutput($this->load->view('marketplace/installer', $data));
     27 	}
     28 
     29 	public function history() {
     30 		$this->load->language('marketplace/installer');
     31 		
     32 		if (isset($this->request->get['page'])) {
     33 			$page = $this->request->get['page'];
     34 		} else {
     35 			$page = 1;
     36 		}
     37 					
     38 		$data['histories'] = array();
     39 		
     40 		$this->load->model('setting/extension');
     41 		
     42 		$results = $this->model_setting_extension->getExtensionInstalls(($page - 1) * 10, 10);
     43 		
     44 		foreach ($results as $result) {
     45 			$data['histories'][] = array(
     46 				'extension_install_id' => $result['extension_install_id'],
     47 				'filename'             => $result['filename'],
     48 				'date_added'           => date($this->language->get('date_format_short'), strtotime($result['date_added']))
     49 			);
     50 		}
     51 		
     52 		$history_total = $this->model_setting_extension->getTotalExtensionInstalls();
     53 
     54 		$pagination = new Pagination();
     55 		$pagination->total = $history_total;
     56 		$pagination->page = $page;
     57 		$pagination->limit = 10;
     58 		$pagination->url = $this->url->link('marketplace/installer/history', 'user_token=' . $this->session->data['user_token'] . '&page={page}', true);
     59 
     60 		$data['pagination'] = $pagination->render();
     61 
     62 		$data['results'] = sprintf($this->language->get('text_pagination'), ($history_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($history_total - 10)) ? $history_total : ((($page - 1) * 10) + 10), $history_total, ceil($history_total / 10));
     63 				
     64 		$this->response->setOutput($this->load->view('marketplace/installer_history', $data));
     65 	}	
     66 		
     67 	public function upload() {
     68 		$this->load->language('marketplace/installer');
     69 
     70 		$json = array();
     71 
     72 		// Check user has permission
     73 		if (!$this->user->hasPermission('modify', 'marketplace/installer')) {
     74 			$json['error'] = $this->language->get('error_permission');
     75 		}
     76 
     77 		// Check if there is a install zip already there
     78 		$files = glob(DIR_UPLOAD . '*.tmp');
     79 
     80 		foreach ($files as $file) {
     81 			if (is_file($file) && (filectime($file) < (time() - 5))) {
     82 				unlink($file);
     83 			}
     84 			
     85 			if (is_file($file)) {
     86 				$json['error'] = $this->language->get('error_install');
     87 				
     88 				break;
     89 			}
     90 		}
     91 
     92 		// Check for any install directories
     93 		$directories = glob(DIR_UPLOAD . 'tmp-*');
     94 		
     95 		foreach ($directories as $directory) {
     96 			if (is_dir($directory) && (filectime($directory) < (time() - 5))) {
     97 				// Get a list of files ready to upload
     98 				$files = array();
     99 	
    100 				$path = array($directory);
    101 	
    102 				while (count($path) != 0) {
    103 					$next = array_shift($path);
    104 	
    105 					// We have to use scandir function because glob will not pick up dot files.
    106 					foreach (array_diff(scandir($next), array('.', '..')) as $file) {
    107 						$file = $next . '/' . $file;
    108 	
    109 						if (is_dir($file)) {
    110 							$path[] = $file;
    111 						}
    112 	
    113 						$files[] = $file;
    114 					}
    115 				}
    116 	
    117 				rsort($files);
    118 	
    119 				foreach ($files as $file) {
    120 					if (is_file($file)) {
    121 						unlink($file);
    122 					} elseif (is_dir($file)) {
    123 						rmdir($file);
    124 					}
    125 				}
    126 	
    127 				rmdir($directory);
    128 			}
    129 			
    130 			if (is_dir($directory)) {
    131 				$json['error'] = $this->language->get('error_install');
    132 				
    133 				break;
    134 			}		
    135 		}
    136 		
    137 		if (isset($this->request->files['file']['name'])) {
    138 			if (substr($this->request->files['file']['name'], -10) != '.ocmod.zip') {
    139 				$json['error'] = $this->language->get('error_filetype');
    140 			}
    141 
    142 			if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
    143 				$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
    144 			}
    145 		} else {
    146 			$json['error'] = $this->language->get('error_upload');
    147 		}
    148 
    149 		if (!$json) {
    150 			$this->session->data['install'] = token(10);
    151 			
    152 			$file = DIR_UPLOAD . $this->session->data['install'] . '.tmp';
    153 			
    154 			move_uploaded_file($this->request->files['file']['tmp_name'], $file);
    155 
    156 			if (is_file($file)) {
    157 				$this->load->model('setting/extension');
    158 				
    159 				$extension_install_id = $this->model_setting_extension->addExtensionInstall($this->request->files['file']['name']);
    160 				
    161 				$json['text'] = $this->language->get('text_install');
    162 
    163 				$json['next'] = str_replace('&amp;', '&', $this->url->link('marketplace/install/install', 'user_token=' . $this->session->data['user_token'] . '&extension_install_id=' . $extension_install_id, true));		
    164 			} else {
    165 				$json['error'] = $this->language->get('error_file');
    166 			}
    167 		}
    168 
    169 		$this->response->addHeader('Content-Type: application/json');
    170 		$this->response->setOutput(json_encode($json));
    171 	}
    172 }