<?require ("global.php");?>
<?php

$ID = 0;
if (isset($_POST['ID'])) { $ID = $_POST['ID']; }

list($msec, $sec) = explode(" ", microtime());
$stamp = date("Ymdhis") . substr($msec, 4);

$filename = ""; $uploaddir = ""; $uploadfile = ""; $profile_sql = "";
$inc_pics = 'no'; $inc_video = 'no'; $inc_map = 'no'; $extra_html = '';

if (isset($_POST['inc_pics']) && $_POST['inc_pics'] == 'yes') { $inc_pics = 'yes'; }
if (isset($_POST['inc_video']) && $_POST['inc_video'] == 'yes') { $inc_video = 'yes'; }
if (isset($_POST['inc_map']) && $_POST['inc_map'] == 'yes') { $inc_map = 'yes'; }

if (isset($_FILES['profile']['name']) && $_FILES['profile']['name'] != "")
{
    $uploaddir = "$DOCUMENT_ROOT/tick/images/gallery/";
    $pieces = preg_split('/\./', basename($_FILES['profile']['name']));
    $filename = $stamp . "." . $pieces[1];
    $uploadfile = $uploaddir . $filename;
    move_uploaded_file($_FILES['profile']['tmp_name'], $uploadfile);
    chmod($uploadfile, 0755);

    $s = "/var/home/slowtwitch/slowtwitch.com/www/tick/scripts/pics.pl $filename --- 640";
    exec("$s 2>&1", $updw);

    $s = "/var/home/slowtwitch/slowtwitch.com/www/tick/scripts/pics.pl $filename t_ 60";
    exec("$s 2>&1", $updw);

    $profile_sql = "', profile = '" . $filename;
}

if ($ID != 0)
{
  $query = "UPDATE stages SET name = '" . myaddslashes($_POST['name']) 
    . "', date = '" . $_POST['year'] . "-" . $_POST['month']
    . "-" . $_POST['day']
    . "', status = '" . $_POST['status'] 
    . "', inc_pics = '" . $inc_pics 
    . "', inc_video = '" . $inc_video
    . "', inc_map = '" . $inc_map
    . "', start_location = '" . myaddslashes($_POST['start_location']) 
    . "', end_location = '" . myaddslashes($_POST['end_location'])
    . "', distance = '" . myaddslashes($_POST['distance'])
    . "', refresh = '" . $_POST['refresh']
    . "', tick_sequence = '" . $_POST['tick_sequence']
    . "', refresh_type = '" . $_POST['refresh_type']
    . "', time_offset = '" . $_POST['time_offset']
    . $profile_sql
    . "', mobile_notes = '" . myaddslashes($_POST['mobile_notes'])
    . "', extra_html = '" . myaddslashes($_POST['extra_html'])
    . "', static_url = '" . myaddslashes($_POST['static_url'])
    . "', notes = '" . myaddslashes($_POST['notes'])
    . "' WHERE ID = '$ID'";
  $result = mysql_query($query, $db);
}
else
{
  $query = "INSERT INTO stages (eventID, name, status, inc_pics, inc_video, inc_map, date, start_location, end_location, distance, refresh, tick_sequence, refresh_type, time_offset, profile, mobile_notes, extra_html, static_url, notes) VALUES ('" 
    . $_POST['eventID'] . "','"
    . myaddslashes($_POST['name']) . "','"
    . $_POST['status'] . "','" 
    . $inc_pics . "','" 
    . $inc_video . "','" 
    . $inc_map . "','" 
    . $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'] . "','" 
    . myaddslashes($_POST['start_location']) . "','" 
    . myaddslashes($_POST['end_location']) . "','"
    . myaddslashes($_POST['distance']) . "','"
    . $_POST['refresh'] . "','"
    . $_POST['tick_sequence'] . "','"
    . $_POST['refresh_type'] . "','"
    . $_POST['time_offset'] . "','"
    . myaddslashes($filename) . "','"
    . myaddslashes($_POST['mobile_notes']) . "','"
    . myaddslashes($_POST['extra_html']) . "','"
    . myaddslashes($_POST['static_url']) . "','"
    . myaddslashes($_POST['notes']) . "')";
  $result = mysql_query($query, $db);

  $ID = mysql_insert_id();
  //$query = "UPDATE stages SET static_url = '/tick/html/stage${ID}' WHERE ID = $ID";
  $result = mysql_query($query, $db);
}

header("Location: /tick/stages.php");

?>
