shop.balmet.com

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

filemanager.php (12708B)


      1 <?php
      2 class ControllerCommonFileManager extends Controller {
      3 	public function index() {
      4 		$this->load->language('common/filemanager');
      5 
      6 		// Find which protocol to use to pass the full image link back
      7 		if ($this->request->server['HTTPS']) {
      8 			$server = HTTPS_CATALOG;
      9 		} else {
     10 			$server = HTTP_CATALOG;
     11 		}
     12 
     13 		if (isset($this->request->get['filter_name'])) {
     14 			$filter_name = rtrim(str_replace(array('*', '/', '\\'), '', $this->request->get['filter_name']), '/');
     15 		} else {
     16 			$filter_name = '';
     17 		}
     18 
     19 		// Make sure we have the correct directory
     20 		if (isset($this->request->get['directory'])) {
     21 			$directory = rtrim(DIR_IMAGE . 'catalog/' . str_replace('*', '', $this->request->get['directory']), '/');
     22 		} else {
     23 			$directory = DIR_IMAGE . 'catalog';
     24 		}
     25 
     26 		if (isset($this->request->get['page'])) {
     27 			$page = $this->request->get['page'];
     28 		} else {
     29 			$page = 1;
     30 		}
     31 
     32 		$directories = array();
     33 		$files = array();
     34 
     35 		$data['images'] = array();
     36 
     37 		$this->load->model('tool/image');
     38 
     39 		if (substr(str_replace('\\', '/', realpath($directory) . '/' . $filter_name), 0, strlen(DIR_IMAGE . 'catalog')) == str_replace('\\', '/', DIR_IMAGE . 'catalog')) {
     40 			// Get directories
     41 			$directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR);
     42 
     43 			if (!$directories) {
     44 				$directories = array();
     45 			}
     46 
     47 			// Get files
     48 			$files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
     49 
     50 			if (!$files) {
     51 				$files = array();
     52 			}
     53 		}
     54 
     55 		// Merge directories and files
     56 		$images = array_merge($directories, $files);
     57 
     58 		// Get total number of files and directories
     59 		$image_total = count($images);
     60 
     61 		// Split the array based on current page number and max number of items per page of 10
     62 		$images = array_splice($images, ($page - 1) * 16, 16);
     63 
     64 		foreach ($images as $image) {
     65 			$name = str_split(basename($image), 14);
     66 
     67 			if (is_dir($image)) {
     68 				$url = '';
     69 
     70 				if (isset($this->request->get['target'])) {
     71 					$url .= '&target=' . $this->request->get['target'];
     72 				}
     73 
     74 				if (isset($this->request->get['thumb'])) {
     75 					$url .= '&thumb=' . $this->request->get['thumb'];
     76 				}
     77 
     78 				$data['images'][] = array(
     79 					'thumb' => '',
     80 					'name'  => implode(' ', $name),
     81 					'type'  => 'directory',
     82 					'path'  => utf8_substr($image, utf8_strlen(DIR_IMAGE)),
     83 					'href'  => $this->url->link('common/filemanager', 'user_token=' . $this->session->data['user_token'] . '&directory=' . urlencode(utf8_substr($image, utf8_strlen(DIR_IMAGE . 'catalog/'))) . $url, true)
     84 				);
     85 			} elseif (is_file($image)) {
     86 				$data['images'][] = array(
     87 					'thumb' => $this->model_tool_image->resize(utf8_substr($image, utf8_strlen(DIR_IMAGE)), 100, 100),
     88 					'name'  => implode(' ', $name),
     89 					'type'  => 'image',
     90 					'path'  => utf8_substr($image, utf8_strlen(DIR_IMAGE)),
     91 					'href'  => $server . 'image/' . utf8_substr($image, utf8_strlen(DIR_IMAGE))
     92 				);
     93 			}
     94 		}
     95 
     96 		$data['user_token'] = $this->session->data['user_token'];
     97 
     98 		if (isset($this->request->get['directory'])) {
     99 			$data['directory'] = urlencode($this->request->get['directory']);
    100 		} else {
    101 			$data['directory'] = '';
    102 		}
    103 
    104 		if (isset($this->request->get['filter_name'])) {
    105 			$data['filter_name'] = $this->request->get['filter_name'];
    106 		} else {
    107 			$data['filter_name'] = '';
    108 		}
    109 
    110 		// Return the target ID for the file manager to set the value
    111 		if (isset($this->request->get['target'])) {
    112 			$data['target'] = $this->request->get['target'];
    113 		} else {
    114 			$data['target'] = '';
    115 		}
    116 
    117 		// Return the thumbnail for the file manager to show a thumbnail
    118 		if (isset($this->request->get['thumb'])) {
    119 			$data['thumb'] = $this->request->get['thumb'];
    120 		} else {
    121 			$data['thumb'] = '';
    122 		}
    123 
    124 		// Parent
    125 		$url = '';
    126 
    127 		if (isset($this->request->get['directory'])) {
    128 			$pos = strrpos($this->request->get['directory'], '/');
    129 
    130 			if ($pos) {
    131 				$url .= '&directory=' . urlencode(substr($this->request->get['directory'], 0, $pos));
    132 			}
    133 		}
    134 
    135 		if (isset($this->request->get['target'])) {
    136 			$url .= '&target=' . $this->request->get['target'];
    137 		}
    138 
    139 		if (isset($this->request->get['thumb'])) {
    140 			$url .= '&thumb=' . $this->request->get['thumb'];
    141 		}
    142 
    143 		$data['parent'] = $this->url->link('common/filemanager', 'user_token=' . $this->session->data['user_token'] . $url, true);
    144 
    145 		// Refresh
    146 		$url = '';
    147 
    148 		if (isset($this->request->get['directory'])) {
    149 			$url .= '&directory=' . urlencode($this->request->get['directory']);
    150 		}
    151 
    152 		if (isset($this->request->get['target'])) {
    153 			$url .= '&target=' . $this->request->get['target'];
    154 		}
    155 
    156 		if (isset($this->request->get['thumb'])) {
    157 			$url .= '&thumb=' . $this->request->get['thumb'];
    158 		}
    159 
    160 		$data['refresh'] = $this->url->link('common/filemanager', 'user_token=' . $this->session->data['user_token'] . $url, true);
    161 
    162 		$url = '';
    163 
    164 		if (isset($this->request->get['directory'])) {
    165 			$url .= '&directory=' . urlencode(html_entity_decode($this->request->get['directory'], ENT_QUOTES, 'UTF-8'));
    166 		}
    167 
    168 		if (isset($this->request->get['filter_name'])) {
    169 			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
    170 		}
    171 
    172 		if (isset($this->request->get['target'])) {
    173 			$url .= '&target=' . $this->request->get['target'];
    174 		}
    175 
    176 		if (isset($this->request->get['thumb'])) {
    177 			$url .= '&thumb=' . $this->request->get['thumb'];
    178 		}
    179 
    180 		$pagination = new Pagination();
    181 		$pagination->total = $image_total;
    182 		$pagination->page = $page;
    183 		$pagination->limit = 16;
    184 		$pagination->url = $this->url->link('common/filemanager', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
    185 
    186 		$data['pagination'] = $pagination->render();
    187 
    188 		$this->response->setOutput($this->load->view('common/filemanager', $data));
    189 	}
    190 
    191 	public function upload() {
    192 		$this->load->language('common/filemanager');
    193 
    194 		$json = array();
    195 
    196 		// Check user has permission
    197 		if (!$this->user->hasPermission('modify', 'common/filemanager')) {
    198 			$json['error'] = $this->language->get('error_permission');
    199 		}
    200 
    201 		// Make sure we have the correct directory
    202 		if (isset($this->request->get['directory'])) {
    203 			$directory = rtrim(DIR_IMAGE . 'catalog/' . $this->request->get['directory'], '/');
    204 		} else {
    205 			$directory = DIR_IMAGE . 'catalog';
    206 		}
    207 
    208 		// Check its a directory
    209 		if (!is_dir($directory) || substr(str_replace('\\', '/', realpath($directory)), 0, strlen(DIR_IMAGE . 'catalog')) != str_replace('\\', '/', DIR_IMAGE . 'catalog')) {
    210 			$json['error'] = $this->language->get('error_directory');
    211 		}
    212 
    213 		if (!$json) {
    214 			// Check if multiple files are uploaded or just one
    215 			$files = array();
    216 
    217 			if (!empty($this->request->files['file']['name']) && is_array($this->request->files['file']['name'])) {
    218 				foreach (array_keys($this->request->files['file']['name']) as $key) {
    219 					$files[] = array(
    220 						'name'     => $this->request->files['file']['name'][$key],
    221 						'type'     => $this->request->files['file']['type'][$key],
    222 						'tmp_name' => $this->request->files['file']['tmp_name'][$key],
    223 						'error'    => $this->request->files['file']['error'][$key],
    224 						'size'     => $this->request->files['file']['size'][$key]
    225 					);
    226 				}
    227 			}
    228 
    229 			foreach ($files as $file) {
    230 				if (is_file($file['tmp_name'])) {
    231 					// Sanitize the filename
    232 					$filename = basename(html_entity_decode($file['name'], ENT_QUOTES, 'UTF-8'));
    233 
    234 					// Validate the filename length
    235 					if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 255)) {
    236 						$json['error'] = $this->language->get('error_filename');
    237 					}
    238 
    239 					// Allowed file extension types
    240 					$allowed = array(
    241 						'jpg',
    242 						'jpeg',
    243 						'gif',
    244 						'png'
    245 					);
    246 
    247 					if (!in_array(utf8_strtolower(utf8_substr(strrchr($filename, '.'), 1)), $allowed)) {
    248 						$json['error'] = $this->language->get('error_filetype');
    249 					}
    250 
    251 					// Allowed file mime types
    252 					$allowed = array(
    253 						'image/jpeg',
    254 						'image/pjpeg',
    255 						'image/png',
    256 						'image/x-png',
    257 						'image/gif'
    258 					);
    259 
    260 					if (!in_array($file['type'], $allowed)) {
    261 						$json['error'] = $this->language->get('error_filetype');
    262 					}
    263 
    264 					// Return any upload error
    265 					if ($file['error'] != UPLOAD_ERR_OK) {
    266 						$json['error'] = $this->language->get('error_upload_' . $file['error']);
    267 					}
    268 				} else {
    269 					$json['error'] = $this->language->get('error_upload');
    270 				}
    271 
    272 				if (!$json) {
    273 					move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
    274 				}
    275 			}
    276 		}
    277 
    278 		if (!$json) {
    279 			$json['success'] = $this->language->get('text_uploaded');
    280 		}
    281 
    282 		$this->response->addHeader('Content-Type: application/json');
    283 		$this->response->setOutput(json_encode($json));
    284 	}
    285 
    286 	public function folder() {
    287 		$this->load->language('common/filemanager');
    288 
    289 		$json = array();
    290 
    291 		// Check user has permission
    292 		if (!$this->user->hasPermission('modify', 'common/filemanager')) {
    293 			$json['error'] = $this->language->get('error_permission');
    294 		}
    295 
    296 		// Make sure we have the correct directory
    297 		if (isset($this->request->get['directory'])) {
    298 			$directory = rtrim(DIR_IMAGE . 'catalog/' . $this->request->get['directory'], '/');
    299 		} else {
    300 			$directory = DIR_IMAGE . 'catalog';
    301 		}
    302 
    303 		// Check its a directory
    304 		if (!is_dir($directory) || substr(str_replace('\\', '/', realpath($directory)), 0, strlen(DIR_IMAGE . 'catalog')) != str_replace('\\', '/', DIR_IMAGE . 'catalog')) {
    305 			$json['error'] = $this->language->get('error_directory');
    306 		}
    307 
    308 		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
    309 			// Sanitize the folder name
    310 			$folder = basename(html_entity_decode($this->request->post['folder'], ENT_QUOTES, 'UTF-8'));
    311 
    312 			// Validate the filename length
    313 			if ((utf8_strlen($folder) < 3) || (utf8_strlen($folder) > 128)) {
    314 				$json['error'] = $this->language->get('error_folder');
    315 			}
    316 
    317 			// Check if directory already exists or not
    318 			if (is_dir($directory . '/' . $folder)) {
    319 				$json['error'] = $this->language->get('error_exists');
    320 			}
    321 		}
    322 
    323 		if (!isset($json['error'])) {
    324 			mkdir($directory . '/' . $folder, 0777);
    325 			chmod($directory . '/' . $folder, 0777);
    326 
    327 			@touch($directory . '/' . $folder . '/' . 'index.html');
    328 
    329 			$json['success'] = $this->language->get('text_directory');
    330 		}
    331 
    332 		$this->response->addHeader('Content-Type: application/json');
    333 		$this->response->setOutput(json_encode($json));
    334 	}
    335 
    336 	public function delete() {
    337 		$this->load->language('common/filemanager');
    338 
    339 		$json = array();
    340 
    341 		// Check user has permission
    342 		if (!$this->user->hasPermission('modify', 'common/filemanager')) {
    343 			$json['error'] = $this->language->get('error_permission');
    344 		}
    345 
    346 		if (isset($this->request->post['path'])) {
    347 			$paths = $this->request->post['path'];
    348 		} else {
    349 			$paths = array();
    350 		}
    351 
    352 		// Loop through each path to run validations
    353 		foreach ($paths as $path) {
    354 			// Check path exsists
    355 			if ($path == DIR_IMAGE . 'catalog' || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $path)), 0, strlen(DIR_IMAGE . 'catalog')) != str_replace('\\', '/', DIR_IMAGE . 'catalog')) {
    356 				$json['error'] = $this->language->get('error_delete');
    357 
    358 				break;
    359 			}
    360 		}
    361 
    362 		if (!$json) {
    363 			// Loop through each path
    364 			foreach ($paths as $path) {
    365 				$path = rtrim(DIR_IMAGE . $path, '/');
    366 
    367 				// If path is just a file delete it
    368 				if (is_file($path)) {
    369 					unlink($path);
    370 
    371 				// If path is a directory beging deleting each file and sub folder
    372 				} elseif (is_dir($path)) {
    373 					$files = array();
    374 
    375 					// Make path into an array
    376 					$path = array($path);
    377 
    378 					// While the path array is still populated keep looping through
    379 					while (count($path) != 0) {
    380 						$next = array_shift($path);
    381 
    382 						foreach (glob($next) as $file) {
    383 							// If directory add to path array
    384 							if (is_dir($file)) {
    385 								$path[] = $file . '/*';
    386 							}
    387 
    388 							// Add the file to the files to be deleted array
    389 							$files[] = $file;
    390 						}
    391 					}
    392 
    393 					// Reverse sort the file array
    394 					rsort($files);
    395 
    396 					foreach ($files as $file) {
    397 						// If file just delete
    398 						if (is_file($file)) {
    399 							unlink($file);
    400 
    401 						// If directory use the remove directory function
    402 						} elseif (is_dir($file)) {
    403 							rmdir($file);
    404 						}
    405 					}
    406 				}
    407 			}
    408 
    409 			$json['success'] = $this->language->get('text_delete');
    410 		}
    411 
    412 		$this->response->addHeader('Content-Type: application/json');
    413 		$this->response->setOutput(json_encode($json));
    414 	}
    415 }