/**
  * commit the upload process
  */
 function commit($method = '', $skip_cleanup = false)
 {
     global $USER, $CONF, $memcache;
     if ($this->validUploadId($this->upload_id)) {
         $uploadfile = $this->_pendingJPEG($this->upload_id);
         if (!file_exists($uploadfile)) {
             return "Upload image not found";
         }
     } else {
         return "Must assign upload id";
     }
     if (!is_object($this->square)) {
         return "Must assign square";
     }
     $viewpoint = new GridSquare();
     if ($this->viewpoint_gridreference) {
         $ok = $viewpoint->setByFullGridRef($this->viewpoint_gridreference, true, true, true);
     }
     //get sequence number
     $mkey = $this->square->gridsquare_id;
     $seq_no =& $memcache->name_get('sid', $mkey);
     if (empty($seq_no) && !empty($CONF['use_insertionqueue'])) {
         $seq_no = $this->db->GetOne("select max(seq_no) from gridimage_queue where gridsquare_id={$this->square->gridsquare_id}");
     }
     if (empty($seq_no)) {
         $seq_no = $this->db->GetOne("select max(seq_no) from gridimage where gridsquare_id={$this->square->gridsquare_id}");
     }
     $seq_no = max($seq_no + 1, 0);
     $memcache->name_set('sid', $mkey, $seq_no, false, $memcache->period_long);
     //ftf is zero under image is moderated
     $ftf = 0;
     //get the exif data and set orientation
     $this->reReadExifFile();
     if (!empty($CONF['use_insertionqueue'])) {
         $table = "gridimage_queue";
     } else {
         $table = "gridimage";
     }
     //create record
     // nateasting/natnorthings will only have values if getNatEastings has been called (in this case because setByFullGridRef has been called IF an exact location is specifed)
     $sql = sprintf("insert into {$table} (" . "gridsquare_id, seq_no, user_id, ftf," . "moderation_status,title,comment,title2,comment2,nateastings,natnorthings,natgrlen,imageclass,imagetaken," . "submitted,viewpoint_eastings,viewpoint_northings,viewpoint_grlen,view_direction,use6fig,user_status,realname,reference_index,viewpoint_refindex) values " . "(%d,%d,%d,%d," . "'pending',%s,%s,%s,%s,%d,%d,'%d',%s,%s," . "now(),%d,%d,'%d',%d,%d,%s,%s,'%d','%d')", $this->square->gridsquare_id, $seq_no, $USER->user_id, $ftf, $this->db->Quote($this->title), $this->db->Quote($this->comment), $this->db->Quote($this->title2), $this->db->Quote($this->comment2), $this->square->nateastings, $this->square->natnorthings, $this->square->natgrlen, $this->db->Quote($this->imageclass), $this->db->Quote($this->imagetaken), $viewpoint->nateastings, $viewpoint->natnorthings, $viewpoint->natgrlen, $this->view_direction, $this->use6fig, $this->db->Quote($this->user_status), $this->db->Quote($this->realname), $this->square->reference_index, $viewpoint->reference_index);
     $this->db->Query($sql);
     //get the id
     $gridimage_id = $this->db->Insert_ID();
     //save the exif
     $sql = sprintf("insert into gridimage_exif (" . "gridimage_id,exif) values " . "(%d,%s)", $gridimage_id, $this->db->Quote($exif));
     $this->db->Query($sql);
     //copy image to correct area
     $src = $this->_pendingJPEG($this->upload_id);
     $image = new GridImage();
     $image->gridimage_id = $gridimage_id;
     $image->user_id = $USER->user_id;
     if ($this->clearexif && $CONF['exiftooldir'] !== '') {
         $cmd = sprintf("\"%sexiftool\" -overwrite_original -all= \"%s\" > /dev/null 2>&1", $CONF['exiftooldir'], $src);
         passthru($cmd);
     }
     $storedoriginal = false;
     if ($ok = $image->storeImage($src)) {
         $orginalfile = $this->_originalJPEG($this->upload_id);
         if (file_exists($orginalfile) && $this->largestsize && $this->largestsize > $CONF['img_max_size']) {
             if ($this->switchxy) {
                 list($oheight, $owidth, $otype, $oattr) = getimagesize($orginalfile);
             } else {
                 list($owidth, $oheight, $otype, $oattr) = getimagesize($orginalfile);
             }
             list($destwidth, $destheight, $destdim, $changedim) = $this->_new_size($owidth, $oheight, $this->largestsize);
             $this->_downsizeFile($orginalfile, $destdim);
             if ($this->clearexif && $CONF['exiftooldir'] !== '') {
                 $cmd = sprintf("\"%sexiftool\" -overwrite_original -all= \"%s\" > /dev/null 2>&1", $CONF['exiftooldir'], $orginalfile);
                 passthru($cmd);
             }
             $storedoriginal = $image->storeOriginal($orginalfile);
         }
         if (!$skip_cleanup) {
             $this->cleanUp();
         }
     }
     //fire an event
     require_once 'geograph/event.class.php';
     new Event(EVENT_NEWPHOTO, $gridimage_id . ',' . $USER->user_id . ',' . $storedoriginal);
     #//assign the snippets now we know the real id.
     #$gid = crc32($this->upload_id)+4294967296;
     #$gid += $USER->user_id * 4294967296;
     #
     #$this->db->Execute($sql = "UPDATE gridimage_snippet SET gridimage_id = $gridimage_id WHERE gridimage_id = ".$gid);
     $this->gridimage_id = $gridimage_id;
     #if (!empty($method)) {
     #	if (!empty($GLOBALS['STARTTIME'])) {
     #
     #		list($usec, $sec) = explode(' ',microtime());
     #		$endtime = ((float)$usec + (float)$sec);
     #		$timetaken = $endtime - $GLOBALS['STARTTIME'];
     #
     #		$this->db->Execute("INSERT INTO submission_method SET gridimage_id = $gridimage_id,method='$method',timetaken=$timetaken");
     #	} else {
     #		$this->db->Execute("INSERT INTO submission_method SET gridimage_id = $gridimage_id,method='$method'");
     #	}
     #}
 }
     $envfrom = is_null($CONF['mail_envelopefrom']) ? null : "-f {$CONF['mail_envelopefrom']}";
     mail($CONF['admin_email'], '[Geograph] Resubmission failure!', $con, $geofrom, $envfrom);
     //unclog the queue!
     $db->Execute("UPDATE gridimage_pending gp SET status = 'rejected' WHERE gridimage_id = {$gridimage_id} ");
 } elseif (!empty($_POST['confirm']) || !empty($_POST['similar'])) {
     $image->originalUrl = $image->_getOriginalpath(true, false, '_original');
     $image->previewUrl = $image->_getOriginalpath(true, false, '_preview');
     $image->pendingUrl = $image->_getOriginalpath(true, false, '_pending');
     //we actually hav a file to move!
     if ($image->pendingUrl != "/photos/error.jpg") {
         //delete the current original file if any
         if ($image->originalUrl != "/photos/error.jpg") {
             unlink($_SERVER['DOCUMENT_ROOT'] . $image->originalUrl);
         }
         //save the pending as original
         $image->storeOriginal($_SERVER['DOCUMENT_ROOT'] . $image->pendingUrl, true);
         if (!empty($CONF['awsAccessKey'])) {
             $image->originalUrl = $image->_getOriginalpath(true, false, '_original');
             require_once "3rdparty/S3.php";
             $s3 = new S3($CONF['awsAccessKey'], $CONF['awsSecretKey']);
             $ok = $s3->putObjectFile($_SERVER['DOCUMENT_ROOT'] . $image->originalUrl, $CONF['awsS3Bucket'], preg_replace("/^\\//", '', $image->originalUrl), S3::ACL_PRIVATE);
         }
         if ($image->previewUrl != "/photos/error.jpg") {
             if (!empty($_POST['confirm'])) {
                 //delete the preview - we dont need it
                 unlink($_SERVER['DOCUMENT_ROOT'] . $image->previewUrl);
             } else {
                 //store the preview as an alterantive fullsize
                 $image->storeImage($_SERVER['DOCUMENT_ROOT'] . $image->previewUrl, true, '_640x640');
             }
         }