<?php

	// get the data sent in from the edit form
	$path = initialize("path", "post", "filename");
	$field = initialize("field", "post", "filename");
	$content = initialize("content", "post", "html", 256000);
	$schedule_year = initialize("schedule_year", "post", "text");
	$schedule_month = initialize("schedule_month", "post", "text");
	$schedule_day = initialize("schedule_day", "post", "text");
	$schedule_hours = initialize("schedule_hours", "post", "text");
	$schedule_minutes = initialize("schedule_minutes", "post", "text");
	$schedule_seconds = initialize("schedule_seconds", "post", "text");
	$schedule_ampm = initialize("schedule_ampm", "post", "text");
	$comment = initialize("comment", "post", "text");
	$status = initialize("status", "post", "integer");
	
	// clean up the data if necessary
	$schedule = combine_date_elements($schedule_year, $schedule_month, $schedule_day)." ".combine_time_elements($schedule_hours, $schedule_minutes, $schedule_seconds, $schedule_ampm);
	
	// decode the content from the transmission process
	// actually, this isn't needed, and it changes &nbsp; characters to garbage characters
	//$content = html_entity_decode($content);
	//$comment = html_entity_decode($comment);
	
	// add slashes if not using magic quotes
	// actually, this isn't needed, because initialize() does this for us
	//if (!ini_get("magic_quotes_gpc")) {
	//	$content = addslashes($content);
	//}

	// for all edits, cache the content in our database
	$query = "
		INSERT INTO content (
			path,
			field,
			content,
			user,
			status,
			comment,
			date,
			schedule
		) VALUES (
			'$path',
			'$field',
			'$content',
			 $user,
			 $status,
			'$comment',
			 ".timestamp("SQL", 1).",
			 COALESCE(DATE_SUB('$schedule', INTERVAL $timezone HOUR), '0000-00-00 00:00:00')
		)
	";
	$result = db_do_query($query);
	
	// for content not yet published, add a Dreamweaver lock file; for published content, see if we can remove it
	if ($result) {
		if ($status < 4) {
			set_dreamweaver_lock($path);
		} else {
			clear_dreamweaver_lock($path);
		}
	}
	
	// for final edits, also save the content in the site format
	if (($result)&&($status == 4)) {
		$content = stripslashes($content);
		$result = save_content($path, $field, $content);
	}
	
	// for deletions, remove the content from the site format
	if (($result)&&($status == 6)) {
		$result = delete_content($path, $field, $content);
	}
	
	// return the result to the edit form
	print $result;
	
?>
