public function __construct($mode = false, $sub_dir = false)
 {
     // Define some constants
     if (!defined('FM_DL_ERROR_EXPIRED')) {
         define('FM_DL_ERROR_EXPIRED', 1);
     }
     if (!defined('FM_DL_ERROR_MAXDL')) {
         define('FM_DL_ERROR_MAXDL', 2);
     }
     if (!defined('FM_DL_ERROR_NOFILE')) {
         define('FM_DL_ERROR_NOFILE', 3);
     }
     if (!defined('FM_DL_ERROR_NOPRODUCT')) {
         define('FM_DL_ERROR_NOPRODUCT', 4);
     }
     if (!defined('FM_DL_ERROR_NORECORD')) {
         define('FM_DL_ERROR_NORECORD', 5);
     }
     if (!defined('FM_DL_ERROR_PAYMENT')) {
         define('FM_DL_ERROR_PAYMENT', 6);
     }
     switch ($mode) {
         case self::FM_FILETYPE_DL:
             $this->_manage_root = CC_ROOT_DIR . '/files';
             break;
         case self::FM_FILETYPE_IMG:
         default:
             $mode = 1;
             $this->_manage_root = CC_ROOT_DIR . '/images/source';
             $this->_manage_cache = CC_ROOT_DIR . '/images/cache';
     }
     $this->_mode = (int) $mode;
     $this->_manage_dir = str_replace(CC_ROOT_DIR . '/', '', $this->_manage_root);
     $this->_sub_dir = $sub_dir ? $this->formatPath($sub_dir) : null;
     //Auto-handler: Create Directory
     if (isset($_POST['fm']['create-dir']) && !empty($_POST['fm']['create-dir'])) {
         $create = $this->createDirectory($_POST['fm']['create-dir']);
     }
     // Auto-handler: image details & cropping
     if (isset($_POST['file_id']) && is_numeric($_POST['file_id'])) {
         if (($file = $GLOBALS['db']->select('CubeCart_filemanager', false, array('file_id' => (int) $_POST['file_id']))) !== false) {
             if (isset($_POST['details'])) {
                 if (!$this->filename_is_illegal($_POST['details']['filename'])) {
                     // Update details
                     $new_location = $current_location = $this->_manage_root . '/' . urldecode($this->_sub_dir);
                     $new_filename = $current_filename = $file[0]['filename'];
                     $new_subdir = $this->_sub_dir;
                     if ($file[0]['filename'] != $_POST['details']['filename']) {
                         $new_filename = $this->formatName($_POST['details']['filename']);
                     }
                     if (isset($_POST['details']['move']) && !empty($_POST['details']['move'])) {
                         $move_to = $this->_manage_root . '/' . $this->formatPath($_POST['details']['move']);
                         if (is_dir($move_to)) {
                             $new_location = $move_to;
                             $new_subdir = $this->formatPath(str_replace($this->_manage_root, '', $new_location), false);
                         }
                     }
                     // Does it need moving?
                     if ($new_location != $current_location || $new_filename != $current_filename) {
                         if (file_exists($current_location . $current_filename) && rename($current_location . $current_filename, $new_location . $new_filename)) {
                             $this->_sub_dir = $new_subdir;
                             $current_location = $new_location;
                             $current_filename = $new_filename;
                             // Database record
                             $record['filename'] = $new_filename;
                             $record['filepath'] = $this->formatPath($this->_sub_dir);
                             $record['filepath'] = $this->_sub_dir == null ? 'NULL' : $this->formatPath($this->_sub_dir);
                         } else {
                             $GLOBALS['gui']->setError($GLOBALS['language']->filemanager['error_file_moved']);
                         }
                     }
                     $record['description'] = strip_tags($_POST['details']['description']);
                     $update = false;
                     foreach ($record as $k => $v) {
                         if (!isset($file[0][$k]) || $file[0][$k] != $v) {
                             $update = true;
                         }
                     }
                     if ($update) {
                         if (!$GLOBALS['db']->update('CubeCart_filemanager', $record, array('file_id' => (int) $_POST['file_id']))) {
                             $GLOBALS['gui']->setError($GLOBALS['language']->filemanager['error_file_update']);
                         }
                     }
                 } else {
                     $GLOBALS['gui']->setError($GLOBALS['language']->filemanager['error_file_update']);
                 }
             }
             if (isset($_POST['resize']) && !empty($_POST['resize']['w']) && !empty($_POST['resize']['h'])) {
                 $resize = $_POST['resize'];
                 if (file_exists($this->_manage_root . '/' . $this->_sub_dir . $current_filename)) {
                     // Use Hi-res image
                     $source = $this->_manage_root . '/' . $this->_sub_dir . $current_filename;
                     $size = getimagesize($source);
                     $gd = new GD(dirname($source), false, 100);
                     $gd->gdLoadFile($source);
                     # TO DO: ROTATION
                     $gd->gdCrop((int) $resize['x'], (int) $resize['y'], (int) $resize['w'], (int) $resize['h']);
                     if ($gd->gdSave(basename($source))) {
                         // Delete previously generated images
                         preg_match('#(\\w+)(\\.\\w+)$#', $current_filename, $match);
                         if (($files = glob($current_location . $match[1] . '*', GLOB_NOSORT)) !== false) {
                             foreach ($files as $file) {
                                 if ($file != $source) {
                                     unlink($file);
                                 }
                             }
                         }
                         $GLOBALS['gui']->setNotify($GLOBALS['language']->filemanager['notify_image_update']);
                     } else {
                         $GLOBALS['gui']->setError($GLOBALS['language']->filemanager['error_image_update']);
                     }
                 }
             }
             httpredir(currentPage(null, array('subdir' => $this->formatPath($this->_sub_dir, false))));
         }
     }
     // Create a directory list
     $this->findDirectories($this->_manage_root);
 }