<?php
// story_submit.php
// add / modify story record 

include_once('include_files.php');
if (!check_auth_user()) {
include('form_login.php');
}else{	
if (isset($HTTP_POST_VARS['front_page_use']) && $HTTP_POST_VARS['front_page_use']!='')
    {
	$front_page_use = 'yes';
	}else{
	$front_page_use = '';
	} 
if (isset($HTTP_POST_VARS['newsletter']))    {
	$newsletter = 'Yes';
	$specific_newsLTR = "Audio";
	}else{
	$newsletter = 'NULL';
	}  	
$headline = $HTTP_POST_VARS['headline'];
$story_description =  $HTTP_POST_VARS['story_description'];
$mls_no = $HTTP_POST_VARS['mls_no'];
$page = $HTTP_POST_VARS['page'];
$rank = $HTTP_POST_VARS['rank'];
$outside_URL = $HTTP_POST_VARS['outside_URL']; 
$school_email = $HTTP_POST_VARS['school_email']; 
$story_text = $HTTP_POST_VARS['story_text'];
$html = $HTTP_POST_VARS['html'];
$captiona = $HTTP_POST_VARS['captiona'];
$captionb = $HTTP_POST_VARS['captionb'];
$captionc = $HTTP_POST_VARS['captionc'];
$captiond = $HTTP_POST_VARS['captiond'];
$captione = $HTTP_POST_VARS['captione'];
$captionf = $HTTP_POST_VARS['captionf'];
$captiong = $HTTP_POST_VARS['captiong'];
$captionh = $HTTP_POST_VARS['captionh'];
$audio = $HTTP_POST_VARS['audio'];
$audio_error = ""; 
$video = $HTTP_POST_VARS['video'];
$video_error = "";
$time = time();
$nwsltr_date = "0000-00-00 00:00:00";



$conn = MY_db_connect();

if ( (isset($HTTP_POST_FILES['html']['name']) && 
 (dirname($HTTP_POST_FILES['html']['type']) == 'text') 
      && is_uploaded_file($HTTP_POST_FILES['html']['tmp_name']))) 
{
  $fp = fopen($HTTP_POST_FILES['html']['tmp_name'], 'r');
  $story_text = addslashes(fread($fp, filesize($HTTP_POST_FILES['html']['tmp_name'])));
  fclose($fp);
}
else
  $story_text = $HTTP_POST_VARS['story_text'];
	
if (isset($HTTP_POST_VARS['story']) && $HTTP_POST_VARS['story']!='') 
{   // It's an update
  $story = $HTTP_POST_VARS['story'];
  $sql = "update stories
          set headline = '$headline', 
		  		mls_no = '$mls_no',
		 		page = '$page',
		  	  rank = '$rank',
			  story_description = '$story_description',
			  staff_email = '$staff_email',
              outside_URL = '$outside_URL',
			  story_text = '$story_text',
              captiona = '$captiona',
			  captionb = '$captionb',
			  captionc = '$captionc',
			  captiond = '$captiond',
			  captione = '$captione',
			  captionf = '$captionf',
			  captiong = '$captiong',
			  captionh = '$captionh',
			  captioni = '$captioni',
			  captioni = '$captioni',
			  captionj = '$captionj',
			  captionj = '$captionj',
			  modified = '$time',
			  newsletter = '$newsletter',
			  front_page_use = '$front_page_use',
			  specific_newsLTR = '$specific_newsLTR',
			  nwsltr_date = '$nwsltr_date'
			  
          where id = $story";
}else {         // It's a new story
  $sql = "insert into stories 
            (headline, mls_no, page, rank, story_description, staff_email, outside_URL, story_text, captiona, captionb,captionc, captiond, captione, captionf, captiong, captionh, captioni, captionj, writer, created, modified, front_page_use, newsletter, specific_newsLTR, nwsltr_date)
          values 
            ('$headline', '$mls_no', '$page', '$rank', '$story_description', '$staff_email', '$outside_URL', '$story_text', '$captiona', '$captionb', '$captionc', '$captiond', '$captione', '$captionf', '$captiong', '$captionh', '$captioni', '$captionj', '".$HTTP_SESSION_VARS['auth_user']."', '$time', '$time', '$front_page_use', '$newsletter', '$specific_newsLTR', '$nwsltr_date')";
}

$result = mysql_query($sql, $conn);

if (!$result) {
  print "There was a database error when executing <pre>$sql</pre>";
  print mysql_error();
  exit;
} 
// Handling Old and New pictures. 	 
// 			
// Array for looping through pics
$Pic_Array = array("","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p");
for ($z=1; $z <=10; ++$z){
	if ( (isset($HTTP_POST_FILES['picture'.$z]['name']) && is_uploaded_file($HTTP_POST_FILES['picture'.$z]['tmp_name']))){	
	// Determine Picture Array Values
	${PicInfo.$z} = @GetImageSize(${picture.$z});
		if ( (${PicInfo.$z}[2] == 1) || (${PicInfo.$z}[2] == 2) || (${PicInfo.$z}[2] == 3) ){
		// is image outside limits  if so reset
		${total_size.$z} = ${PicInfo.$z}[0] * ${PicInfo.$z}[1];
			if ( ((${PicInfo.$z}[0] > 600) || (${PicInfo.$z}[1] > 600)) &&  (${total_size.$z} > 360000) ) {
				if (${PicInfo.$z}[0] > ${PicInfo.$z}[1]){
					${new_height_ratio.$z} = ${PicInfo.$z}[0] / 600;
					${new_height.$z} = ${PicInfo.$z}[1] / ${new_height_ratio.$z};
					${new_width.$z} = 600;
				}else{
					${new_width_ratio.$z} = ${PicInfo.$z}[1] / 600;
					${new_width.$z} = ${PicInfo.$z}[0] / ${new_width_ratio.$z};
					${new_height.$z} = 600;
				}
			}else{
				${new_width.$z} = ${PicInfo.$z}[0];
				${new_height.$z} = ${PicInfo.$z}[1];
			}	
			
			${NewPic.$z} = imagecreatetruecolor(${new_width.$z}, ${new_height.$z}); // Create New Image platform
			if (${PicInfo.$z}[2] == 1){
				${New_Image.$z} = "pictures/$story$Pic_Array[$z].gif";
				${image.$z} = imagecreatefromgif(${picture.$z}); // Creating picture
			} 
			if (${PicInfo.$z}[2] == 2){
				${New_Image.$z} = "pictures/$story$Pic_Array[$z].jpg";
				${image.$z} = imagecreatefromjpeg(${picture.$z}); // Creating picture
			}
			if (${PicInfo.$z}[2] == 3){
				${New_Image.$z} = "pictures/$story$Pic_Array[$z].png";
				${image.$z} = imagecreatefrompng(${picture.$z}); // Creating picture
			}
			imagecopyresized(${NewPic.$z}, ${image.$z}, 0, 0, 0, 0, ${new_width.$z}, ${new_height.$z}, ${PicInfo.$z}[0], ${PicInfo.$z}[1]); // Resample / Resize
			imagejpeg(${NewPic.$z}, ${New_Image.$z},80); // Write file to directory
		}else{		
			$New_ImageA= 'Invalid picture format. You tried to upload: '.$HTTP_POST_FILES['picture'.$z]['type'];	
		}	
		// Save to Database
		 
		$sql = "update stories
        set picture".$Pic_Array[$z]." = '${New_Image.$z}'
        where id = '$story'
		";
        $result = mysql_query($sql, $conn);
		if(isset($result)){
		$saved_picture = "yes";
		}
	}
}
// Audio file being uploaded and labeled within DB 
if ( (isset($HTTP_POST_FILES['audio1']['name']) && 
      is_uploaded_file($HTTP_POST_FILES['audio1']['tmp_name'])))
{
  if (!isset($story))
  $story = mysql_insert_id();	
//  $Atype = basename($HTTP_POST_FILES['audio1']['type']);
  $Aname = $_FILES['audio1']['name'];
  $Asize = $_FILES['audio1']['size'];
  $Atype = $_FILES['audio1']['type'];

   	 $str =  "$Aname";
	 $pat = "\.";
	 $arr = split($pat, $str);
	 $Audio_type = ("$arr[1]");
  switch ($Audio_type) {		 
    case 'wma':
	case 'WMA':
	case 'wav':
	case 'WAV':
	case 'mp3':
	case 'MP3':
	case 'avi':
	case 'AVI':
	case 'rm':
	case 'RM':
	
	$Afilename = "audios/$story.$Audio_type";
	move_uploaded_file($HTTP_POST_FILES['audio1']['tmp_name'], $Afilename);
                    $sql = "update stories
                            set audio = '$Afilename'
                            where id = $story";
                    $result = mysql_query($sql, $conn);
		if(!isset($result)){
		$state = "broke early";
		break;
		}else{
		
				
		$Aisfilename = "/home/tracyp/public_html/content_publisher/audios/$story.rpm";
		$Adisplay_block = "http://www.peoplesdelawarehomes.com/content_publisher/$Afilename";		
		if (file_exists($Aisfilename)) {
			$fileexists = "yes";
			} else {  
			$newfile = fopen ($Aisfilename, "w+") or die ("Couldn't create file!");
			fclose($newfile);
			$fileexists = "no";
			$write = "yes";
			}
			
		$myAfile = fopen($Aisfilename, "w+") or die ("Couldn't open file!");
		fwrite($myAfile, $Adisplay_block) or die ("Couldn't write to file!");
		fclose($myAfile);
		$rtname = "$story";
}
	break;		
    default:  
	$audio_error = 'Invalid audio format. You tried to upload: '.$HTTP_POST_FILES['audio1']['type'].'- with the extension: '.$Audio_type;
	$sql = "update stories
    set audio = '$audio_error'
    where id = $story";
    $result = mysql_query($sql, $conn);
	if(!isset($result)){
	}
  }
}
/// Creating XML files for MP3 Player


	$basedir="/home/tracyp/public_html/";
	/// Who are we writing this file for
	$filename="playlist.xml"; 
	
	/// Starting XML file data information
	$display_block = '<?xml version="1.0" encoding="UTF-8"?> ';
	$display_block .= "	\n";
	$display_block .= "<Total_songs> \n";

	/// We are going to pull photos and then set the names of the images and thumbnails 
	$i =1;
	$sql="select headline, story_description, audio  from stories
	WHERE    page  	= 	'Audio Interview' and
	published		!= ''
	order by published asc";
	$result = mysql_query($sql, $conn);
		while ($s = mysql_fetch_array($result)) {	/// Variable for Flash XML File
			$line = 	'	<song path="/content_publisher/'.$s['audio'];
			$line .= 	'" art="/home/tracyp/www/images/personal_img.jpg"';
			$line .= 	' bands="Tracy Peoples"';
			$line .= 	' titles="'.$s['headline'].'"';
			$line .= 	" /> \n";
			$display_block .= $line;
		}
	/// Writing variable information for Flash XML File
	$display_block .= "</Total_songs>";
	$filename = "$basedir$filename";
		if (file_exists($filename)) {
			$fileexists = "yes";
		} else {  
			$newfile = fopen ($filename, "w+") or die ("Couldn't create file!");
			fclose($newfile);
			$fileexists = "no";
			$write = "yes";
		}
	$myfile = fopen($filename, "w+") or die ("Couldn't open file!");
	fwrite($myfile, $display_block) or die ("Couldn't write to file!");
		if ($fileexists == "yes") {
			$state = "<p>Previous file was opened, new text replaced old information !</p>";
		} else {
			$state = "<p>New file created and information has been saved!</p>";
			fclose($myfile);
		}
// Video file being uploaded and labeled within DB
if ( (isset($HTTP_POST_FILES['video1']['name']) && 
      is_uploaded_file($HTTP_POST_FILES['video1']['tmp_name']))) 
{
  if (!isset($story))
  $story = mysql_insert_id();	
	
//  $Vtype = basename($HTTP_POST_FILES['video1']['type']); 
  $Vname = $_FILES['video1']['name'];
  $Vsize = $_FILES['video1']['size'];
  $Vtype = $_FILES['video1']['type'];

   	 $str =  "$Vname";
	 $pat = "\.";
	 $arr = split($pat, $str);
	 $Video_type = ("$arr[1]");
  switch ($Video_type) {
   	case 'mpg':
	case 'mpv':
	case 'mp4':
	case 'mpe':
	case 'mpeg':
	case 'MPEG':
	case 'wmv':
	case 'wm':
	
	$Vfilename = "videos/$story.$Video_type";
	move_uploaded_file($HTTP_POST_FILES['video1']['tmp_name'], $Vfilename);
                    $sql = "update stories
                            set video = '$Vfilename'
                            where id = $story";
                    $result = mysql_query($sql, $conn);
		if(!isset($result)){
		$state = "broke early";
		break;
		}else{		
		$Visfilename = "/home/shoh/public_html/content_publisher/videos/$story.rpm";
		$Vdisplay_block = "http://www.shilohhouseofhope.org/content_publisher/$Vfilename";		
		if (file_exists($Visfilename)) {
			$fileexists = "yes";
			} else {  
			$newVfile = fopen ($Visfilename, "w+") or die ("Couldn't create file!");
			fclose($newVfile);
			$fileexists = "no";
			$write = "yes";
			}
			
		$myVfile = fopen($Visfilename, "w+") or die ("Couldn't open file!");
		fwrite($myVfile, $Vdisplay_block) or die ("Couldn't write to file!");
		fclose($myVfile);
	}
	break;		
    default:  
	$video_error = 'Invalid video format. You tried to upload: '.$HTTP_POST_FILES['video1']['type'].' - With the extension: '.$Video_type;
	$sql = "update stories
                            set video = '$video_error'
                            where id = $story";
    $result = mysql_query($sql, $conn);
	if(!isset($result)){
	}
  }
}
// document file being uploaded and labeled within DB
if ( (isset($HTTP_POST_FILES['doc1']['name']) && 
      is_uploaded_file($HTTP_POST_FILES['doc1']['tmp_name']))) 
{
  if (!isset($story))
  $story = mysql_insert_id();	
	
//  $Vtype = basename($HTTP_POST_FILES['video1']['type']); 
  $Dname = $_FILES['doc1']['name'];
  $Dsize = $_FILES['doc1']['size'];
  $Dtype = $_FILES['doc1']['type'];

   	 $str =  "$Dname";
	 $pat = "\.";
	 $arr = split($pat, $str);
	 $Doc_type = ("$arr[1]");
  switch ($Doc_type) {
   	case 'txt':
	case 'pdf':
	
	
	$docfilename = "docs/$story.$Doc_type";  
	move_uploaded_file($HTTP_POST_FILES['doc1']['tmp_name'],$docfilename);
    $sql = "update stories
           set doc = '$docfilename'
           where id = $story";
           $result = mysql_query($sql, $conn);
    break;		
    default:  
	$doc_error = 'Invalid document format. You tried to upload: '.$HTTP_POST_FILES['doc1']['type'].' - With the extension: '.$Doc_type;
	$sql = "update stories
                            set doc = '$doc_error'
                            where id = $story";
    $result = mysql_query($sql, $conn);
	if(!isset($result)){
	}
  }
}
	
	

header('Location: '.$HTTP_POST_VARS['destination']);
}
?>

