function add($filePath, $fileName, $fileType, &$defRow, $store = true)
 {
     global $my, $mainframe, $database, $option, $priTask, $subTask;
     global $WBG_CONFIG, $wbGalleryDB_cat;
     $time = time() . rand(0, 99999);
     $date = date('Y_m');
     $fileName = preg_replace('/[\\s|\\\\|\\"|\\&|\\?|\']+/', '_', $fileName);
     $fileName = preg_replace('/\\_+/', '_', $fileName);
     $fileExt = preg_replace('/^\\w+\\//', '', $fileType);
     $newFileName = $date . '/' . $time . '.' . $fileExt;
     // Initial File Should be the Largest
     $origInfo = getimagesize($filePath);
     // Debug
     echo "Adding File: " . $fileName . ' -> ' . $newFileName . '<br/>';
     for ($iType = 1; $iType <= 5; $iType++) {
         $active = 0;
         // Process from Largest to Smallest
         switch ($iType) {
             case 1:
                 // ORIGINAL
                 $active = $WBG_CONFIG->save_original;
                 $width = 0;
                 $height = 0;
                 $quality = 0;
                 $path = $mainframe->getCfg('absolute_path') . $WBG_CONFIG->path_original;
                 break;
             case 2:
                 // LARGE
                 $active = $WBG_CONFIG->save_large;
                 $width = $WBG_CONFIG->width_large;
                 $height = $WBG_CONFIG->height_large;
                 $quality = $WBG_CONFIG->quality_large;
                 $path = $mainframe->getCfg('absolute_path') . $WBG_CONFIG->path_large;
                 break;
             case 3:
                 // MEDIUM
                 $active = $WBG_CONFIG->save_medium;
                 $width = $WBG_CONFIG->width_medium;
                 $height = $WBG_CONFIG->height_medium;
                 $quality = $WBG_CONFIG->quality_medium;
                 $path = $mainframe->getCfg('absolute_path') . $WBG_CONFIG->path_medium;
                 break;
             case 4:
                 // THUMB
                 $active = $WBG_CONFIG->save_thumb;
                 $width = $WBG_CONFIG->width_thumb;
                 $height = $WBG_CONFIG->height_thumb;
                 $quality = $WBG_CONFIG->quality_thumb;
                 $path = $mainframe->getCfg('absolute_path') . $WBG_CONFIG->path_thumb;
                 break;
             case 5:
                 // TACK
                 $active = $WBG_CONFIG->save_tack;
                 $width = $WBG_CONFIG->width_tack;
                 $height = $WBG_CONFIG->height_tack;
                 $quality = $WBG_CONFIG->quality_tack;
                 $path = $mainframe->getCfg('absolute_path') . $WBG_CONFIG->path_tack;
                 break;
         }
         // Process the Image for Step
         // Check / Create Destination
         // Copy Image
         // Resize Copied
         if ($active) {
             $fullPath = $path . $newFileName;
             if (!is_writable($path)) {
                 echo "<script> alert('Permission Denied for {$path}'); window.history.go(-1); </script>\n";
                 exit;
             }
             if (!file_exists($path . $date)) {
                 if (!mkdir($path . $date)) {
                     echo "<script> alert('Failed to Create Category Folder'); window.history.go(-1); </script>\n";
                     exit;
                 }
                 mosChmod($path . $date, 0777);
             }
             if (!copy($filePath, $fullPath)) {
                 echo "<script> alert('Failed to Save Image'); window.history.go(-1); </script>\n";
                 exit;
             }
             if ($width && $height) {
                 if (!$this->resize($fullPath, $fileType, $width, $height)) {
                     echo "<script> alert('Error Resizing Image {$fileName}'); window.history.go(-1); </script>\n";
                     exit;
                 } else {
                     $imgInfo = getimagesize($fullPath);
                 }
             }
         }
     }
     // Debug
     echo "Creating Database Record: " . $fileName . '<br/>';
     // Store Record
     $row = new wbGalleryDB_img($database);
     $row->file = $newFileName;
     $row->cat_id = $defRow->cat_id;
     $row->name = strlen($defRow->name) ? $defRow->name : preg_replace('/\\.\\w+$/', '', $fileName);
     $row->description = $defRow->description;
     $row->photographer = $defRow->photographer;
     $row->price = $defRow->price;
     $row->sku = $defRow->sku;
     $row->publised = $defRow->publised;
     $row->created = date('Y-m-d H:i:s');
     $row->modified = $row->created;
     $row->ordering = 0;
     if (is_array($origInfo)) {
         $row->width = $origInfo[0];
         $row->height = $origInfo[1];
         $row->size = $origInfo['bits'];
     } elseif (is_array($imgInfo)) {
         $row->width = $imgInfo[0];
         $row->height = $imgInfo[1];
         $row->size = $imgInfo['bits'];
     }
     if ($store) {
         // Check
         if (!$row->check()) {
             echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
             exit;
         }
         // Store
         if (!$row->store()) {
             echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
             exit;
         }
         // Update Ordering
         $row->updateOrder('cat_id = ' . (int) $row->cat_id);
         echo "Image Stored Successfully <br/><br/>";
         return true;
     } else {
         return $row;
     }
 }
 function save()
 {
     global $my, $mainframe, $database, $option, $priTask, $subTask;
     global $WBG_CONFIG, $wbGalleryDB_cat;
     foreach ($_POST as $k => $v) {
         $_POST[$k] = stripslashes($v);
     }
     $row = new wbGalleryDB_img($database);
     $row->load(mosGetParam($_POST, 'id', 0));
     $row->bind($_POST);
     if (!$row->id) {
         $row->ordering = 0;
         $row->created = date('Y-m-d H:i:s');
     } else {
         $row->modified = date('Y-m-d H:i:s');
     }
     // Is this an Ajax Call?
     if (in_array($subTask, array('rename'))) {
         if (!$row->check() || !$row->store()) {
             echo 'Update Failed...';
         } else {
             echo 'Update Success...';
         }
         exit;
     }
     if (!$row->check()) {
         echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
         exit;
     }
     if (!$row->store()) {
         echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
         exit;
     }
     $row->updateOrder('cat_id = ' . (int) $row->cat_id);
     switch ($subTask) {
         case 'save':
             mosRedirect('index2.php?option=' . $option . '&task=image', 'Changes Saved');
             break;
         case 'apply':
             mosRedirect('index2.php?option=' . $option . '&task=image.edit&id=' . $row->id . '&hidemainmenu=1', 'Changes Saved');
             break;
     }
 }