<?php

// Pixelpost Uploader v1.0 Script (Admin Folder)


/**
 * This is the "master password" that allows the uploader access to pixelpost.
 * Make sure the key is LONG and hard to guess.  You can always copy-paste the
 * key into the Uploader program if it is too long to remember.
 * 
 * For a good post key, check out this site: https://www.grc.com/passwords.htm
 **/
define("POSTKEY", "ChangeMe");


/**
 * When you enter categories, you can have the Updater automatically create new
 * categories if the one you entered does not exist.  To enable this feature,
 * change the text from false to true.
 **/
define("CREATECAT", false);








////////// DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING! ////////// 




// Cheep hack to allow loading of vars on addons page
if(!isset($_GET['view'])){



// Passkey Check
if(!isset($_POST['postkey']) || $_POST['postkey'] != POSTKEY || $_POST['headline'] == ""){
//	exit();
	die ("Try another day!!");
}

$_POST['headline'] = urldecode($_POST['headline']);
$_POST['categories'] = urldecode($_POST['categories']);
$_POST['autodate'] = urldecode($_POST['autodate']);
$_POST['body'] = urldecode($_POST['body']);


if($_POST['autodate'] == "Post one day after last post"){
	$_POST['autodate'] = 1; // Post one day after last post
}else if($_POST['autodate'] == "Use exif date"){
	$_POST['autodate'] = 3; // Use exif date
}else{
	$_POST['autodate'] = 2; // Post Now
}

// Hack to get adons to work
$_GET['x'] = "save";

// Hack to get post slug to auto-generate titles
$_POST['postslug'] = "";

// variable saying we are inside admin panel (i.e. to use in addons)
$admin_panel = 1;

session_start();

require("../includes/pixelpost.php");
require("../includes/functions.php");

$pixelpost_prefix_used = $pixelpost_db_prefix;
start_mysql();

// Changed to allow upgrades
if($cfgquery = mysql_query("select * from ".$pixelpost_db_prefix."config")) {
	$cfgrow = mysql_fetch_assoc($cfgquery);
	$upload_dir = $cfgrow['imagepath'];
	
} else {
    // header("Location: install.php");
	die('Error getting MYSQL Data!');
}


// refresh the addons table
$dir = "../addons/";
refresh_addons_table($dir);


//------------- addons in admin panel begins
$addon_admin_functions = array(0 => array('function_name' => '','workspace' => '','menu_name' => '','submenu_name' => ''));
create_admin_addon_array();



//------------- addons in admin panel ends
// check whether the language-files for the public part exist

// Force LOGIN
   $cfgrow_password = $cfgrow['password'];
	$_POST['user'] = $cfgrow['admin'];


      // login is valid, set session
      	$_SESSION["pixelpost_admin"]  = $cfgrow_password;
 $_GET["_SESSION"]["pixelpost_admin"] = '';
$_POST["_SESSION"]["pixelpost_admin"] = '';
    
 // if (login = yes) end

eval_addon_admin_workspace_menu('admin_html_head');

eval_addon_admin_workspace_menu('admin_main_menu'); 

// new image
//include('new_image.php');

/**************************
*  CVS file version: $Id: new_image.php,v 1.46 2006/06/27 11:20:49 jspurling Exp $
**************************/

if(!isset($_SESSION["pixelpost_admin"]) || $cfgrow['password'] != $_SESSION["pixelpost_admin"] || $_GET["_SESSION"]["pixelpost_admin"] == $_SESSION["pixelpost_admin"] || $_POST["_SESSION"]["pixelpost_admin"] == $_SESSION["pixelpost_admin"]) {
	die ("Try another day, again!!");
}

require("../includes/exifer1_5/exif.php");

   // save new post
      //if($_GET['x'] == "save") {
	if($_POST['postkey'] == POSTKEY) {
         $headline = clean($_POST['headline']);
  	     $body =  clean($_POST['body']);
	       $datetime =
             $_POST['post_year']."-".
             $_POST['post_month']."-".
             $_POST['post_day']." ".
             $_POST['post_hour'].":".
             $_POST['post_minute'].":".date('s');

	if( $_POST['autodate'] == 1 ) {
		$query = mysql_query("select datetime + INTERVAL 1 DAY from ".$pixelpost_db_prefix."pixelpost order by datetime desc limit 1");
		$row = mysql_fetch_row($query);
		if( $row ) $datetime = $row[0];	// If there is none, will default to the other value
		} else
			if( $_POST['autodate'] == 2 ) {
				$tz = $cfgrow['timezone'];
			 $datetime = gmdate("Y-m-d H:i:s",time()+(3600 * $tz));} else if ($_POST['autodate'] == 3)// exifdate
			 { // New, JFK: post date from EXIF
			 // delay action to later point. We don't know the filename yet...
			 // just set a flag so we know what to do later on
			 $postdatefromexif = TRUE;};
	

  if($headline == "") {
	die(" No title specfied. ");
      }

  // prepare the file
	if($_FILES['userfile'] != "") {
		$userfile = strtolower($_FILES['userfile']['name']);
		$tz = $cfgrow['timezone'];
		if ($cfgrow['timestamp']=='yes')
			$time_stamp_r = gmdate("YmdHis",time()+(3600 * $tz)) .'_';
		$uploadfile = $upload_dir .$time_stamp_r .$userfile;
		if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
			chmod($uploadfile, 0644);
			$result = check_upload($_FILES['userfile']['error']);
			$filnamn =strtolower($_FILES['userfile']['name']);
			$filnamn = $time_stamp_r .$filnamn;
			$filtyp = $_FILES['userfile']['type'];
			$filstorlek = $_FILES['userfile']['size'];
			$status = "ok";
			if($postdatefromexif == TRUE) {
				 $exif_result = read_exif_data_raw($uploadfile,"0");
				 $exposuredatetime = $exif_result['SubIFD']['DateTimeOriginal'];
				 if ($exposuredatetime!=''){
				 list($exifyear,$exifmonth,$exifday,$exifhour,$exifmin, $exifsec) = split('[: ]', $exposuredatetime);
			    $datetime = date("Y-m-d H:i:s", mktime($exifhour, $exifmin, $exifsec, $exifmonth, $exifday, $exifyear));
			    }else $datetime = gmdate("Y-m-d H:i:s",time()+(3600 * $tz));
         }
		} else {
			// something went wrong, try to describe what
			if ($_FILES['userfile']['error']!='0'){
			$result = check_upload($_FILES['userfile']['error']);
		}else{
			
      			$status = "no";
				die ("Error uploading image file!  $result");
			}
    } // end move
  } // end prepare of file ($_FILES['userfile'] != "")
// var_dump ($_POST['category']); // Debug
  // insert post in mysql
	$image = $filnamn;
	if($status == "ok") {
		$query = "insert into ".$pixelpost_db_prefix."pixelpost(datetime,headline,body,image)
		VALUES('$datetime','$headline','$body','$image')";
		$result = mysql_query($query) || die("Error: ".mysql_error().$admin_lang_ni_db_error);

    $theid = mysql_insert_id(); //Gets the id of the last added image to use in the next "insert"

		if (isset($_POST['category'])){
			foreach($_POST['category'] as $val) {
				$query  ="INSERT INTO ".$pixelpost_db_prefix."catassoc(id,cat_id,image_id) VALUES(NULL,'$val','$theid')";
				$result = mysql_query($query) || die("Error: ".mysql_error());
			}
    }

    // done

		// workspace: image_uploaded
		eval_addon_admin_workspace_menu('image_uploaded');
		$headline = pullout($_POST['headline']);
		$body = pullout($_POST['body']);
		$headline = htmlspecialchars($headline,ENT_QUOTES);
		$body = htmlspecialchars($body,ENT_QUOTES);
			

			 //create thumbnail
		 if(function_exists('gd_info')) {
			 $gd_info = gd_info();
				 if($gd_info != "") {
					 $thumbnail = $filnamn;
					 $thumbnail = createthumbnail($thumbnail);
					 eval_addon_admin_workspace_menu('thumb_created');
				
			
					} // gd info
			} // function_exists
		} // end status ok
	} // end save
//} // end view=null


//////// END OF IMAGE UPLOAD SCRIPT ////////

eval_addon_admin_workspace_menu('admin_main_menu_contents');

// LOGOUT AT END
unset($_SESSION["pixelpost_admin"]);
setcookie( "pp_user", "", time()-36000);
setcookie( "pp_password", "", time()-36000);
}// End if Get View
?>