<?php
// set a variable to determine which design templates to use
$templates="1";
//set page specific variables
$title="hunting and fishing stories and jokes on PrairieOutdoors.com The Campfire";
$keywords='"hunting, fishing, camping, Canada, Saskatchewan, Alberta, Manitoba, hunting stories, fishing stories, camping stories, hunting jokes, fishing jokes, camping jokes"';
$description='"hunting stories fishing stories and camping stories from Saskatchewan, Alberta, and Manitoba"';
//include the top html page code
include 'top.php';
//select the design template to use in the body based on the $templates variable
$header='header'.$templates.'.htm';
$layouttopandleft='layouttopandleft'.$templates.'.htm';
$layoutrightandbottom='layoutrightandbottom'.$templates.'.htm';
//set page header
include $header;
//set top and left layout (usually includes navigation, google ads, and affiliate program
include $layouttopandleft;
//page specific content goes here, php scripts, text, images
?>
<?php
$today=date("Y").'-'.date("m").'-'.date("d");
function escape_data($data){
	include "../mysql_connect.php";
	global $dbc;
	if(ini_get('magic_quotes_gpc')){
		$data=stripslashes($data);
	}
	return mysql_real_escape_string($data,$dbc);
}

//form handling 
if (isset($_POST['upload'])){

// create main image ***
// This is the temporary file created by PHP 
$uploadedfile = $_FILES['uploadfile']['tmp_name'];
if (!($uploadedfile=='')){
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to a max of
// 800 pixels wide, and maintain the original aspect 
// ratio. This prevents the image from being "stretched"
// or "squashed". 
$newwidth=min($width,800);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "outdoors/upload_files/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);

// create thumbnail image ***

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to a max of
// 800 pixels wide, and maintain the original aspect 
// ratio. This prevents the image from being "stretched"
// or "squashed". 
$newwidth=min($width,150);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$tfilename = "outdoors/upload_files/".'t'.$_FILES['uploadfile']['name'];
imagejpeg($tmp,$tfilename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request has completed.
}

    $nospam=$_POST['pass'];
    $pss=$_POST['pss'];
    if ($nospam==$pss){
        
        $category=$_POST['category'];
        $species=$_POST['species'];
        if ($category=="jokes"){$species="";}
        $text=$_POST['text'];
        
//******************
        //$text=escape_data($text);
//******************
        $alttext=$_POST['alttext'];
        $date=$_POST['date'];
        $location=$_POST['location'];

        //update database with image information
	    if ($filename==''){
		  $image='noimage.jpg';
		  $thumb='noimage.jpg';
        }else{
         	$image=$filename;
         	$thumb=$tfilename;
        }
        include "../mysql_connect.php";
        $query = "INSERT INTO campfire SET date='$date', text='$text', location='$location', alttext='$alttext', image='$image', timage='$thumb', category='$category', species='$species', submitted_date=CURDATE()";
        $result=@mysql_query($query);
        if ($result) {
		  echo('<center><font face="Arial" size="2"></br></br> Thanks for your item.</font></center>');
		  mail('gsonntag@prairieoutdoors.com',$text,$category,"From: campfire@prairieoutdoors.com");
		  echo '</br></br></br></br></br></br></br></br></br><font face="Arial" size="3"><center><a href="campfire-fish-pictures-hunting-pictures.php?cat='.$category.'-'.$species.'"> Return to The Campfire</a></center></font></br></br>
		  <center><a href="'.$_SERVER['PHP_SELF'].'"><font face="Arial" size="3">Submit another picture/story</font></a></center>';
        } else {
		  echo '<p> database update failed</p>'.
            mysql_error() . '</p>';
        }
    }else{
        echo '<center><font face="Arial" size="3" color="red">You did not pass the test.
		<p> <a href="campfire_submit.php">Try again</a>
		</a></center></font>';
    }
}else{


	// random check: generate question section - start
    require_once ('../mysql_connect.php');
    $query="SELECT count(id) as qty FROM randomcheck";
    $result=@mysql_query($query);
	if($result){
        while ($row=mysql_fetch_array($result)){
            $qty=$row['qty'];
        }
    }
    $key=rand (1, $qty);
    require_once ('../mysql_connect.php');
    $query="SELECT * FROM randomcheck WHERE id='$key'";
    $result=@mysql_query($query);
	if($result){
        while ($row=mysql_fetch_array($result)){
            $q=$row['q'];
            $a=$row['a'];
            
        }
    }
    
	// random check generate question section - end    

?>
</br>
<font face="Arial" size="1"><center>PrairieOutdoors.com reserves the right to remove any material which is not deemed suitable for this site.</center></font></br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post" action="<?=$_SERVER['PHP_SELF']?>">
  <table border="1" width="600" align="center">
      <tr>
      <td colspan="2" bordercolor="#CCCCCC" bgcolor="#D9D8D8">
      <font face="Arial" size="3"><center>Submit your Campfire Story, Immediate Posting</center></font>
      </td>
      </tr>
	  <tr>
        <td width="200" bgcolor="#D9D8D8"><font face="Arial" size="-1">Date:</font></td>
        <td width="400"><input type="text" name="date" size="20" value="<?php echo $today; ?>"></td>
      </tr>
      <tr>
        <td width="200" bgcolor="#D9D8D8"><font face="Arial" size="-1">Story:</font></td>
        <td width="400"><textarea rows="5" name="text" cols="50"></textarea></td>
      </tr><tr>
        <td width="200" bgcolor="#D9D8D8"><font face="Arial" size="-1"></font><font face="Arial" size="-1">Category:</font></td>
        <td width="400">
      <select name="category">
        <option>--choose your category--</option>
        <option>fishing</option>
        <option>hunting</option>
        <option>jokes</option>
      </select>
      <select name="species">
        <option>--choose the species--</option>
        <option>Walleye</option>
        <option>Pike</option>
        <option>Rainbow trout</option>
        <option>Brook trout</option>
        <option>Brown trout</option>
        <option>Lake Trout</option>
        <option>Perch</option>
        <option>----</option>
        <option>Black Bear</option>
        <option>Moose</option>
        <option>Elk</option>
        <option>Whitetail Deer</option>
        <option>Mule Deer</option>
        <option>Black Bear</option>
        <option>----</option>
        <option>Pheasant</option>
        <option>Goose</option>
        <option>Duck</option>
        <option>Sharptail Grouse</option>
        <option>Hungarian Partridge</option>
      </select>
      </td>
      </tr>
      <tr>
        <td width="200" bgcolor="#D9D8D8"><font face="Arial" size="-1">Location:</font></td>
        <td width="400"><input type="text" name="location" size="20"></textarea></td>
      </tr>
      <tr>
  		  <td width="200" bgcolor="#D9D8D8"><font face="Arial" size="-1">Upload image (<b>jpeg only Max size 2MB</b>): </font> </td>
		  <td width="400"><input type="file" id="uploadfile" name="uploadfile" size="50"></td>
  		</tr>
      <tr>
        <td width="200" bgcolor="#D9D8D8"><font face="Arial" size="-1">Image caption:</font></td>
        <td width="400"><input type="text" name="alttext" size="40"></textarea></td>
      </tr>
  		<tr><td width="200" bgcolor="#D9D8D8">
            <?php echo $q; ?>
        </td><td>
        <input type="text" name="pass">
        <input type="hidden" name="pss" value="<?php echo $a; ?>">
        </td></tr>
  		<tr>
		  <td width="200" bgcolor="#D9D8D8"></td>
		  <td>  <input type="submit" name="upload" value="Submit">
		  </td>
  		</tr>
</form>
</table>

<?php
}


//set right and bottom layout, includes bottom links and page closing body and html tags 

include $layoutrightandbottom;

?>