chiro-canto/public/upload/submitobservation.php

93 lines
3.3 KiB
PHP
Raw Normal View History

2021-03-28 20:40:17 +02:00
<?php
session_start();
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require($root."/database/credentials.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Connect the database
2021-03-30 07:31:22 +02:00
try {
2021-03-28 20:40:17 +02:00
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
2021-03-30 10:52:22 +02:00
} catch (Exception $e) {
2021-03-28 20:40:17 +02:00
die("Error : ".$e->getMessage());
}
2021-03-29 19:35:47 +02:00
// /* Create Table if not exists */
// $sql = file_get_contents($root."/database/create_record.sql");
// $db->exec($sql);
2021-03-30 07:31:22 +02:00
$req = $db->prepare('SELECT id FROM `authors` WHERE username=:username');
$req->execute(array(
"username"=>$_SESSION['username']
));
if ($data = $req->fetch())
{
$id = $data['id'];
}
2021-04-09 14:29:00 +02:00
/** 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);
2021-03-30 07:31:22 +02:00
try {
2021-04-09 14:29:00 +02:00
$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)');
2021-03-30 07:31:22 +02:00
$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']),
2021-04-09 14:29:00 +02:00
"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,
2021-03-30 07:31:22 +02:00
"country"=>$_SESSION['observation']['country'],
"lat"=>$_SESSION['observation']['lat'],
"lng"=>$_SESSION['observation']['lng'],
"date"=>$_SESSION['observation']['date'],
"time"=>$_SESSION['observation']['time'],
2021-04-09 14:29:00 +02:00
"remarks"=>$_SESSION['observation']['remarks']
2021-03-30 07:31:22 +02:00
));
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}
2021-04-16 19:14:52 +02:00
$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: '."/");
}
2021-03-28 20:40:17 +02:00
?>