|
| 1 | +<?php |
| 2 | +defined('BASEPATH') OR exit('No direct script access allowed'); |
| 3 | + |
| 4 | +class FilemanagerController extends CI_Controller { |
| 5 | + public $DIR_IMAGE = ''; |
| 6 | + public $IMAGE_LIMIT = ''; |
| 7 | + |
| 8 | + public function __construct(){ |
| 9 | + parent::__construct(); |
| 10 | + $this->DIR_IMAGE = strtr(rtrim(FCPATH."assets/images/", '/\\'),'/\\',DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
| 11 | + $this->IMAGE_LIMIT = 30; |
| 12 | + } |
| 13 | + |
| 14 | + public function index($page=1){ |
| 15 | + $get = $this->input->get(); |
| 16 | + |
| 17 | + $server = base_url('/'); |
| 18 | + if (isset($get['filter_name'])) { |
| 19 | + $filter_name = rtrim(str_replace(array('*', '/', '\\'), '', $get['filter_name']), '/'); |
| 20 | + } else { |
| 21 | + $filter_name = ''; |
| 22 | + } |
| 23 | + |
| 24 | + if (isset($get['type']) && $get['type'] != 'undefined') { |
| 25 | + $type = $get['type']; |
| 26 | + } else { |
| 27 | + $type = ''; |
| 28 | + } |
| 29 | + |
| 30 | + // Make sure we have the correct directory |
| 31 | + if (isset($get['directory'])) { |
| 32 | + $directory = rtrim($this->DIR_IMAGE . str_replace('*', '', $get['directory']), '/'); |
| 33 | + } else { |
| 34 | + $directory = $this->DIR_IMAGE; |
| 35 | + } |
| 36 | + |
| 37 | + $directories = array(); |
| 38 | + $files = array(); |
| 39 | + |
| 40 | + $data['images'] = array(); |
| 41 | + if (substr(str_replace('\\', '/', realpath($directory) . '/' . $filter_name), 0, strlen($this->DIR_IMAGE)) == str_replace('\\', '/', $this->DIR_IMAGE )) { |
| 42 | + $directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR); |
| 43 | + |
| 44 | + if (!$directories) { $directories = array(); } |
| 45 | + |
| 46 | + $directories = array_map(function($v){ |
| 47 | + return !endsWith($v,"cache") ? $v : ''; |
| 48 | + }, $directories); |
| 49 | + $directories = array_filter($directories); |
| 50 | + |
| 51 | + $files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE); |
| 52 | + |
| 53 | + if (!$files) { |
| 54 | + $files = array(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $images = array_merge($directories, $files); |
| 59 | + $image_total = $images; |
| 60 | + $perPage = $this->IMAGE_LIMIT; |
| 61 | + $offset = ($page * $perPage) - $perPage; |
| 62 | + |
| 63 | + $images = new \Illuminate\Pagination\LengthAwarePaginator( |
| 64 | + array_slice($image_total, $offset, $perPage, true), |
| 65 | + count($image_total), |
| 66 | + $perPage, |
| 67 | + $page, |
| 68 | + ['path' => route('admin.filemanager.index'), 'query' => $get] |
| 69 | + ); |
| 70 | + |
| 71 | + $config['base_url'] = route('admin.filemanager.index'); |
| 72 | + $config['use_page_numbers'] = TRUE; |
| 73 | + $config['reuse_query_string'] = TRUE; |
| 74 | + $config['total_rows'] = count($image_total); |
| 75 | + $config['per_page'] = $this->IMAGE_LIMIT; |
| 76 | + $config['full_tag_open'] = '<ul class="pagination m-0 ml-auto">'; |
| 77 | + $config['full_tag_close'] = '</ul>'; |
| 78 | + $config['num_tag_open'] = '<li class="page-item">'; |
| 79 | + $config['num_tag_close'] = '</li>'; |
| 80 | + $config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">'; |
| 81 | + $config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>'; |
| 82 | + $config['next_tag_open'] = '<li class="page-item">'; |
| 83 | + $config['next_tagl_close'] = '<span aria-hidden="true">»</span></li>'; |
| 84 | + $config['prev_tag_open'] = '<li class="page-item">'; |
| 85 | + $config['prev_tagl_close'] = '</li>'; |
| 86 | + $config['first_tag_open'] = '<li class="page-item">'; |
| 87 | + $config['first_tagl_close'] = '</li>'; |
| 88 | + $config['last_tag_open'] = '<li class="page-item">'; |
| 89 | + $config['last_tagl_close'] = '</li>'; |
| 90 | + $config['next_link'] = 'Next <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon"><polyline points="9 18 15 12 9 6"></polyline></svg>'; |
| 91 | + $config['prev_link'] = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon"><polyline points="15 18 9 12 15 6"></polyline></svg> Prev'; |
| 92 | + $config['attributes'] = array('class' => 'page-link'); |
| 93 | + |
| 94 | + $this->pagination->initialize($config); |
| 95 | + $data['pagination'] = $this->pagination->create_links(); |
| 96 | + |
| 97 | + $url = ''; |
| 98 | + if (isset($get['target'])) { $url .= '&target=' . $get['target']; } |
| 99 | + if (isset($get['thumb'])) { $url .= '&thumb=' . $get['thumb']; } |
| 100 | + |
| 101 | + foreach ($images->items() as $image) { |
| 102 | + $name = str_split(basename($image), 14); |
| 103 | + if (is_dir($image)) { |
| 104 | + $data['images'][] = array( |
| 105 | + 'thumb' => '', |
| 106 | + 'name' => implode(' ', $name), |
| 107 | + 'type' => 'directory', |
| 108 | + 'path' => substr($image, strlen($this->DIR_IMAGE)), |
| 109 | + 'href' => route('admin.filemanager.index'). '?directory=' . urlencode(substr($image, strlen($this->DIR_IMAGE))) .$url, |
| 110 | + ); |
| 111 | + } elseif (is_file($image)) { |
| 112 | + $data['images'][] = array( |
| 113 | + 'thumb' => RImage::resize(substr($image, strlen($this->DIR_IMAGE)), 100, 100), |
| 114 | + 'name' => implode(' ', $name), |
| 115 | + 'type' => 'image', |
| 116 | + 'path' => substr($image, strlen($this->DIR_IMAGE)), |
| 117 | + 'href' => $server . 'image/' . substr($image, strlen($this->DIR_IMAGE)) |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + if (isset($get['directory'])) { |
| 124 | + $data['directory'] = urlencode($get['directory']); |
| 125 | + } else { |
| 126 | + $data['directory'] = ''; |
| 127 | + } |
| 128 | + |
| 129 | + if (isset($get['filter_name'])) { |
| 130 | + $data['filter_name'] = $get['filter_name']; |
| 131 | + } else { |
| 132 | + $data['filter_name'] = ''; |
| 133 | + } |
| 134 | + |
| 135 | + if (isset($get['target'])) { |
| 136 | + $data['target'] = $get['target']; |
| 137 | + } else { |
| 138 | + $data['target'] = ''; |
| 139 | + } |
| 140 | + |
| 141 | + if (isset($get['thumb'])) { |
| 142 | + $data['thumb'] = $get['thumb']; |
| 143 | + } else { |
| 144 | + $data['thumb'] = ''; |
| 145 | + } |
| 146 | + |
| 147 | + $url = ''; |
| 148 | + |
| 149 | + if (isset($get['directory'])) { |
| 150 | + $pos = strrpos($get['directory'], '/'); |
| 151 | + |
| 152 | + if ($pos) { |
| 153 | + $url .= '&directory=' . urlencode(substr($get['directory'], 0, $pos)); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (isset($get['target'])) { |
| 158 | + $url .= '&target=' . $get['target']; |
| 159 | + } |
| 160 | + |
| 161 | + if (isset($get['thumb'])) { |
| 162 | + $url .= '&thumb=' . $get['thumb']; |
| 163 | + } |
| 164 | + |
| 165 | + $data['parent'] = route('admin.filemanager.index'). '?user_token=' . $url; |
| 166 | + |
| 167 | + // Refresh |
| 168 | + $url = ''; |
| 169 | + |
| 170 | + if (isset($get['directory'])) { |
| 171 | + $url .= '&directory=' . urlencode($get['directory']); |
| 172 | + } |
| 173 | + |
| 174 | + if (isset($get['target'])) { |
| 175 | + $url .= '&target=' . $get['target']; |
| 176 | + } |
| 177 | + |
| 178 | + if (isset($get['thumb'])) { |
| 179 | + $url .= '&thumb=' . $get['thumb']; |
| 180 | + } |
| 181 | + |
| 182 | + if (isset($get['filter_name'])) { |
| 183 | + $url .= '&filter_name=' . $get['filter_name']; |
| 184 | + } |
| 185 | + |
| 186 | + $data['refresh'] = route('admin.filemanager.index'). '?user_token=' . $url; |
| 187 | + |
| 188 | + $url = ''; |
| 189 | + |
| 190 | + if (isset($get['directory'])) { |
| 191 | + $url .= '&directory=' . urlencode(html_entity_decode($get['directory'], ENT_QUOTES, 'UTF-8')); |
| 192 | + } |
| 193 | + |
| 194 | + if (isset($get['filter_name'])) { |
| 195 | + $url .= '&filter_name=' . urlencode(html_entity_decode($get['filter_name'], ENT_QUOTES, 'UTF-8')); |
| 196 | + } |
| 197 | + |
| 198 | + if (isset($get['target'])) { |
| 199 | + $url .= '&target=' . $get['target']; |
| 200 | + } |
| 201 | + |
| 202 | + if (isset($get['thumb'])) { |
| 203 | + $url .= '&thumb=' . $get['thumb']; |
| 204 | + } |
| 205 | + |
| 206 | + $data['token'] = ''; |
| 207 | + $this->load->view('admin/filemanager/modal', $data); |
| 208 | + } |
| 209 | + |
| 210 | + public function delete_photos(){ |
| 211 | + $get = $this->input->post(); |
| 212 | + $json = array(); |
| 213 | + |
| 214 | + if (isset($get['path'])) { |
| 215 | + $paths = $get['path']; |
| 216 | + } else { |
| 217 | + $paths = array(); |
| 218 | + } |
| 219 | + |
| 220 | + |
| 221 | + foreach ($paths as $path) { |
| 222 | + if ($path == $this->DIR_IMAGE || substr(str_replace('\\', '/', realpath($this->DIR_IMAGE . $path)), 0, strlen($this->DIR_IMAGE)) != str_replace('\\', '/', $this->DIR_IMAGE)) { |
| 223 | + $json['error'] = 'error_delete'; |
| 224 | + break; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + if (!$json) { |
| 229 | + foreach ($paths as $path) { |
| 230 | + $path = rtrim($this->DIR_IMAGE . $path, '/'); |
| 231 | + if (is_file($path)) { unlink($path); } |
| 232 | + elseif (is_dir($path)) { |
| 233 | + $files = array(); |
| 234 | + $path = array($path); |
| 235 | + |
| 236 | + while (count($path) != 0) { |
| 237 | + $next = array_shift($path); |
| 238 | + foreach (glob($next) as $file) { |
| 239 | + if (is_dir($file)) { |
| 240 | + $path[] = $file . '/*'; |
| 241 | + } |
| 242 | + $files[] = $file; |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + rsort($files); |
| 247 | + |
| 248 | + foreach ($files as $file) { |
| 249 | + if (is_file($file)) { |
| 250 | + unlink($file); |
| 251 | + } elseif (is_dir($file)) { |
| 252 | + rmdir($file); |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + $json['success'] = 'Photo Deleted Successfully'; |
| 259 | + } |
| 260 | + |
| 261 | + echo json_encode($json); |
| 262 | + } |
| 263 | + |
| 264 | + public function upload_photos() { |
| 265 | + $get = $this->input->get(); |
| 266 | + $json = array(); |
| 267 | + |
| 268 | + if (isset($get['directory'])) { |
| 269 | + $directory = rtrim($this->DIR_IMAGE . $get['directory'], '/'); |
| 270 | + } else { |
| 271 | + $directory = $this->DIR_IMAGE; |
| 272 | + } |
| 273 | + |
| 274 | + // Check its a directory |
| 275 | + if (!is_dir($directory)) { |
| 276 | + $json['error'] = 'error_directory'; |
| 277 | + } |
| 278 | + |
| 279 | + |
| 280 | + if (!$json) { |
| 281 | + $files = array(); |
| 282 | + |
| 283 | + if (!empty($_FILES['file']['name']) && is_array($_FILES['file']['name'])) { |
| 284 | + foreach (array_keys($_FILES['file']['name']) as $key) { |
| 285 | + $files[] = array( |
| 286 | + 'name' => $_FILES['file']['name'][$key], |
| 287 | + 'type' => $_FILES['file']['type'][$key], |
| 288 | + 'tmp_name' => $_FILES['file']['tmp_name'][$key], |
| 289 | + 'error' => $_FILES['file']['error'][$key], |
| 290 | + 'size' => $_FILES['file']['size'][$key] |
| 291 | + ); |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + foreach ($files as $file) { |
| 296 | + if (is_file($file['tmp_name'])) { |
| 297 | + // Sanitize the filename |
| 298 | + $filename = basename(html_entity_decode($file['name'], ENT_QUOTES, 'UTF-8')); |
| 299 | + |
| 300 | + // Validate the filename length |
| 301 | + if ((strlen($filename) < 3) || (strlen($filename) > 255)) { |
| 302 | + $json['error'] = 'error_filename'; |
| 303 | + } |
| 304 | + |
| 305 | + $allowed = array('jpg','jpeg','gif','png'); |
| 306 | + |
| 307 | + if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) { |
| 308 | + $json['error'] = 'error_filetype'; |
| 309 | + } |
| 310 | + |
| 311 | + |
| 312 | + $allowed = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif'); |
| 313 | + |
| 314 | + if (!in_array($file['type'], $allowed)) { |
| 315 | + $json['error'] = 'error_filetype'; |
| 316 | + } |
| 317 | + |
| 318 | + // Return any upload error |
| 319 | + if ($file['error'] != UPLOAD_ERR_OK) { |
| 320 | + $json['error'] = 'error_upload_' . $file['error']; |
| 321 | + } |
| 322 | + } else { |
| 323 | + $json['error'] = 'error_upload'; |
| 324 | + } |
| 325 | + |
| 326 | + if (!$json) { |
| 327 | + move_uploaded_file($file['tmp_name'], $directory . '/' . $filename); |
| 328 | + } |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + if (!$json) { |
| 333 | + $json['success'] = 'text_uploaded'; |
| 334 | + } |
| 335 | + |
| 336 | + echo json_encode($json); |
| 337 | + } |
| 338 | + |
| 339 | + public function folder_create() { |
| 340 | + $get = $this->input->post(); |
| 341 | + $json = array(); |
| 342 | + |
| 343 | + if (isset($get['directory'])) { |
| 344 | + $directory = rtrim($this->DIR_IMAGE . $get['directory'], '/'); |
| 345 | + } else { |
| 346 | + $directory = $this->DIR_IMAGE; |
| 347 | + } |
| 348 | + |
| 349 | + if (!is_dir($directory)) { |
| 350 | + $json['error'] = 'error_directory'; |
| 351 | + } |
| 352 | + |
| 353 | + |
| 354 | + if ($this->input->server('REQUEST_METHOD') == 'POST') { |
| 355 | + $folder = basename(html_entity_decode($get['folder'], ENT_QUOTES, 'UTF-8')); |
| 356 | + |
| 357 | + if ((strlen($folder) < 3) || (strlen($folder) > 128)) { |
| 358 | + $json['error'] = 'Folder name must be between 3 and 128'; |
| 359 | + } |
| 360 | + |
| 361 | + // Check if directory already exists or not |
| 362 | + if (is_dir($directory . '/' . $folder)) { |
| 363 | + $json['error'] = 'error_exists'; |
| 364 | + } |
| 365 | + } |
| 366 | + |
| 367 | + if (!isset($json['error'])) { |
| 368 | + mkdir($directory . '/' . $folder, 0777); |
| 369 | + chmod($directory . '/' . $folder, 0777); |
| 370 | + @touch($directory . '/' . $folder . '/' . 'index.html'); |
| 371 | + |
| 372 | + $json['success'] = 'text_directory'; |
| 373 | + } |
| 374 | + |
| 375 | + echo json_encode($json); |
| 376 | + } |
| 377 | +} |
0 commit comments