chiro-canto/public/upload/submit.php

243 lines
8.8 KiB
PHP

<?php
function db_entry() {
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require($root."/database/credentials.php");
// Connect the database
try {
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}
// /* Create Table if not exists */
// $sql = file_get_contents($root."/database/create_record.sql");
// $db->exec($sql);
$req = $db->prepare('SELECT id FROM `authors` WHERE username=:username');
$req->execute(array(
"username"=>$_SESSION['username']
));
if ($data = $req->fetch())
{
$id = $data['id'];
}
/** Retrieve .wav duration*/
$filepath = "$root/storage/records/".$_SESSION['observation']['file'];
function wavDur($file) {
$fp = fopen($file, 'r');
if (fread($fp,4) == "RIFF") {
fseek($fp, 20);
$rawheader = fread($fp, 16);
$header = unpack('vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits',$rawheader);
$pos = ftell($fp);
while (fread($fp,4) != "data" && !feof($fp)) {
$pos++;
fseek($fp,$pos);
}
$rawheader = fread($fp, 4);
$data = unpack('Vdatasize',$rawheader);
$sec = $data['datasize']/$header['bytespersec'];
$minutes = intval(($sec / 60) % 60);
$seconds = intval($sec % 60);
return str_pad($minutes,2,"0", STR_PAD_LEFT).":".str_pad($seconds,2,"0", STR_PAD_LEFT);
}
}
$t = wavDur($filepath);
print_r($t);
try {
$req = $db->prepare('INSERT INTO `records` (author_id, recordist_name, file_name, license, species, subspecies, sound_type, duration, make, model, serial, sample_rate, time_expansion, country, lat, lng, date, time, remarks) VALUES ( :id, :recordist_name, :file_name, :license, :species, :subspecies, :sound_type, :duration, :make, :model, :serial, :sample_rate, :time_expansion, :country, :lat, :lng, :date, :time, :remarks)');
$req->execute(array(
"id"=>$id,
"recordist_name"=>$_SESSION['observation']['recordist-name'],
"file_name"=>$_SESSION['observation']['file'],
"license"=>$_SESSION['observation']['license'],
"species" => $_SESSION['observation']['species'],
"subspecies"=>$_SESSION['observation']['subspecies'],
"sound_type"=>implode(',', $_SESSION['observation']['type']),
"duration"=>$t,
"make"=>isset($_SESSION['observation']['make']) ? $_SESSION['observation']['make'] : "",
"model"=>isset($_SESSION['osbservation']['model']) ? $_SESSION['osbservation']['model'] : "",
"serial"=>isset($_SESSION['observation']['serial']) ? $_SESSION['observation']['serial'] : "",
"sample_rate"=>isset($_SESSION['observation']['sample_rate']) ? $_SESSION['observation']['sample_rate'] : 384,
"time_expansion"=>isset($_SESSION['observation']['time_expansion']) ? $_SESSION['observation']['time_expansion'] : 10,
"country"=>$_SESSION['observation']['country'],
"lat"=>$_SESSION['observation']['lat'],
"lng"=>$_SESSION['observation']['lng'],
"date"=>$_SESSION['observation']['date'],
"time"=>$_SESSION['observation']['time'],
"remarks"=>$_SESSION['observation']['remarks']
));
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}
$req = $db->prepare('SELECT LAST_INSERT_ID() AS id;');
$req->execute();
if ($data = $req->fetch()) {
header('Location: '."/explore/record/?id=".$data['id']);
} else {
header('Location: '."/");
}
}
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$target_dir = $root."/storage/records/";
$fileName = $_FILES['file']['name'];
$target_file = $target_dir.basename($_FILES['file']['name']);
$uploadOk = 1;
$fileSize = $_FILES['file']['size'];
$temp = explode('.', $fileName);
$fileExtension = strtolower(end($temp));
$_SESSION['error_msg'] = "";
if (isset($_POST['submit']))
{
// /!\ Need to check if it is .wav.
$file_extension_allowed = ['wav'];
if (! in_array($fileExtension, $file_extension_allowed))
{
$_SESSION['error_msg'] .= "This file extension is not allowed; please upload a WAV file.\n";
}
if ($fileSize > 10*1024*1024)
{
$_SESSION['error_msg'] .= "The file size must be below 10MB. \n";
}
print_r($target_file);
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file))
{
echo "The file ". htmlspecialchars(basename($_FILES['file']['name']))." has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file";
}
$_SESSION['observation']['file'] = basename($_FILES['file']['name']);
if (isset($_POST['latlng'])) {
$latlng = $_POST['latlng'];
$_SESSION['observation']['latlng'] = $latlng;
$_SESSION['observation']['lat'] = explode(',', $latlng)[0];
$_SESSION['observation']['lng'] = explode(',', $latlng)[1];
} else {
$_SESSION['error_msg'] .= "Incorrect coordinates, please try again.";
}
if (isset($_POST['spchoice']))
{
if ($_POST['spchoice'] == "unknown") {
$_SESSION['observation']['species'] = "unknown";
$_SESSION['observation']['subspecies'] = "";
setcookie('species', $_SESSION['observation']['species'], time() + 86400 * 365.2, "/");
} else {
if (isset($_POST['species']))
{
$_SESSION['observation']['species'] = $_POST['species'];
setcookie('species', $_SESSION['observation']['species'], time() + 86400 * 365.2, "/");
} else
{
$_SESSION['error_msg'].= "Error, incorrect species name.\n";
}
if (isset($_POST['subspecies']))
{
$_SESSION['observation']['subspecies'] = $_POST['subspecies'];
setcookie('subspecies', $_SESSION['observation']['subspecies'], time() + 86400 * 365.2, "/");
} else
{
$_SESSION['observation']['subspecies'] = "";
}
}
}
if (isset($_POST['recording-license']))
{
$_SESSION['observation']['license'] = $_POST['recording-license'];
setcookie('license', $_SESSION['observation']['license'], time() + 86400 * 365.2, "/");
} else
{
$_SESSION['error_msg'] .= "Incorrect license\n";
}
if (isset($_POST['name']))
{
$_SESSION['observation']['recordist-name'] = $_POST['name'];
setcookie('name', $_SESSION['observation']['recordist-name'], time() + 86400 * 365.2, "/");
} else {
$_SESSION['error_msg'] .= "Incorrect Recordist Name.\n";
}
if (isset($_POST['loc-country']))
{
$_SESSION['observation']['country'] = $_POST['loc-country'];
} else
{
$_SESSION['error_msg'] .= "Incorrect Country. \n";
}
if (isset($_POST['recording-date']))
{
$_SESSION['observation']['date'] = $_POST['recording-date'];
} else
{
$_SESSION['error_msg'] .= "Incorrect date. \n";
}
if (isset($_POST['recording-time']))
{
$_SESSION['observation']['time'] = $_POST['recording-time'];
} else
{
$_SESSION['error_msg'] .= "Incorrect time. \n";
}
if (isset($_POST['sound-type-option']))
{
$_SESSION['observation']['type'] = $_POST['sound-type-option'];
} else
{
$_SESSION['error_msg'] .= "Incorrect sound type. \n";
}
if (isset($_POST['quality']))
{
$_SESSION['observation']['quality'] = $_POST['quality'];
} else
{
$_SESSION['error_msg'] .= "Incorrect quality value.\n";
}
if (isset($_POST['remarks']))
{
$_SESSION['observation']['remarks'] = $_POST['remarks'];
}
/* Optional entries */
if (isset($_POST['make']))
{
$_SESSION['observation']['make'] = $_POST['make'];
}
if (isset($_POST['model']))
{
$_SESSION['observation']['model'] = $_POST['model'];
}
if (isset($_POST['serial']))
{
$_SESSION['observation']['serial'] = $_POST['serial'];
}
if (isset($_POST['sample_rate']))
{
$_SESSION['observation']['sample_rate'] = $_POST['sample_rate'];
}
if (isset($_POST['time_expansion']))
{
$_SESSION['observation']['time_expansion'] = $_POST['time_expansion'];
}
} else {
$_SESSION['error_msg'] = "Form not submitted.\n";
}
if ($_SESSION['error_msg'] == "") {
db_entry();
} else {
header('Location: /upload');
}