uploadhistory.php 8.62 KB
Newer Older
1
<?php
2 3 4 5
// uploadhistory.php
// Validates form data from pages/history.html and writes a file for the Matlab 
// script file_scanner_mcmc_starter.m to read as input for the MCMC inversion.

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// reCAPTCHA setup
require_once('recaptchalib.php');

// your secret key
$secret = "6LeMrRATAAAAAOdcvVGi6PfR__XGOVoUP7lCqHp1";
 
// empty response
$response = null;
 
// check secret key
$reCaptcha = new ReCaptcha($secret);

// if submitted check response
if ($_POST["g-recaptcha-response"]) {
    $response = $reCaptcha->verifyResponse(
        $_SERVER["REMOTE_ADDR"],
        $_POST["g-recaptcha-response"]
    );
}


27 28
//$missing_fields = ''; // string of missing field names
$missing_fields = array(); // array of missing field names
29
//die('"' . $_POST['sample_id'] . '", ' . isset($_POST['sample_id']));
30

31
// Check required fields if not set or blank, one by one
32
if (!isset($_POST['sample_id']) || $_POST['sample_id'] == '') {
33 34
    array_push($missing_fields, 'Sample ID');
}
35
if (!isset($_POST['name']) || $_POST['name'] == '') {
36 37
    array_push($missing_fields, 'Your Name');
}
38
if (!isset($_POST['email']) || $_POST['email'] == '') {
39 40
    array_push($missing_fields, 'Email');
}
41
if (!isset($_POST['lat']) || $_POST['lat'] == '') {
42 43
    array_push($missing_fields, 'Latitude');
}
44
if (!isset($_POST['long']) || $_POST['long'] == '') {
45 46
    array_push($missing_fields, 'Longitude');
}
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
if (!isset($_POST['rock_density']) || $_POST['rock_density'] == '') {
    array_push($missing_fields, 'Rock density');
}
if (!isset($_POST['epsilon_gla_min']) || $_POST['epsilon_gla_min'] == '') {
    array_push($missing_fields, 'Min. glacial erosion rate');
}
if (!isset($_POST['epsilon_gla_max']) || $_POST['epsilon_gla_max'] == '') {
    array_push($missing_fields, 'Max. glacial erosion rate');
}
if (!isset($_POST['epsilon_int_min']) || $_POST['epsilon_int_min'] == '') {
    array_push($missing_fields, 'Min. inter-glacial erosion rate');
}
if (!isset($_POST['epsilon_int_max']) || $_POST['epsilon_int_max'] == '') {
    array_push($missing_fields, 'Max. inter-glacial erosion rate');
}
62 63
if (!isset($_POST['t_degla_min']) || $_POST['t_degla_min'] == '') {
    array_push($missing_fields, 'Minimum time since deglaciation');
64
}
65 66
if (!isset($_POST['t_degla_max']) || $_POST['t_degla_max'] == '') {
    array_push($missing_fields, 'Maximum time since deglaciation');
67
}
68 69
if (!isset($_POST['record']) || $_POST['record'] == ''){
    array_push($missing_fields, 'Climate record');
70
}
71 72 73 74 75
if (!isset($_POST['record_threshold_min']) || $_POST['record_threshold_min'] == ''){
    array_push($missing_fields, 'Min. climate record threshold value');
}
if (!isset($_POST['record_threshold_max']) || $_POST['record_threshold_max'] == ''){
    array_push($missing_fields, 'Max. climate record threshold value');
76
}
77 78 79
if (!isset($_POST['nwalkers']) || $_POST['nwalkers'] == ''){
    array_push($missing_fields, 'Number of MCMC walkers');
}
80

81 82 83 84
if (!isset($_POST['zobs']) || $_POST['zobs'] == ''){
    array_push($missing_fields, 'One or more sample depths');
}

85
// Check TCN concentrations, at least one value is needed
86 87 88 89
if ((!isset($_POST['be_conc']) || $_POST['be_conc'] == '') &&
    (!isset($_POST['al_conc']) || $_POST['al_conc'] == '') &&
    (!isset($_POST['c_conc'])  || $_POST['c_conc'] == '') &&
    (!isset($_POST['ne_conc']) || $_POST['ne_conc']) == '') {
90
        array_push($missing_fields, 'At least 1 TCN concentration');
91 92 93 94
}

// For each isotope concentration there should be uncertainty and production 
// rate
95
if ((isset($_POST['be_conc']) && $_POST['be_conc'] != '') &&
96 97
    (!isset($_POST['be_prod_muons']) || $_POST['be_prod_muons'] == '' ||
    !isset($_POST['be_prod_spall']) || $_POST['be_prod_spall'] == '' ||
98
    !isset($_POST['be_uncer']) || $_POST['be_uncer'] == '')) {
99 100
    array_push($missing_fields,
        'Production rate, sample depth, and/or uncertainty for ' .
101 102
        '<sup>10</sup>Be');
}
103
if ((isset($_POST['al_conc']) && $_POST['al_conc'] != '') &&
104 105
    (!isset($_POST['al_prod_muons']) || $_POST['al_prod_muons'] == '' ||
    !isset($_POST['al_prod_spall']) || $_POST['al_prod_spall'] == '' ||
106
    !isset($_POST['al_uncer']) || $_POST['al_uncer'] == '')) {
107 108
        array_push($missing_fields,
        'Production rate, sample depth, and/or uncertainty for ' .
109 110
        '<sup>26</sup>Al');
}
111
if ((isset($_POST['c_conc']) && $_POST['c_conc'] != '') &&
112 113
    (!isset($_POST['c_prod_muons']) || $_POST['c_prod_muons'] == '' ||
    !isset($_POST['c_prod_spall']) || $_POST['c_prod_spall'] == '' ||
114
    !isset($_POST['c_uncer']) || $_POST['c_uncer'] == '')) {
115 116
        array_push($missing_fields,
        'Production rate, sample depth, and/or uncertainty for ' .
117 118
        '<sup>14</sup>C');
}
119
if ((isset($_POST['ne_conc']) && $_POST['ne_conc'] != '') &&
120 121
    (!isset($_POST['ne_prod_muons']) || $_POST['ne_prod_muons'] == '' ||
    !isset($_POST['ne_prod_spall']) || $_POST['ne_prod_spall'] == '' ||
122
    !isset($_POST['ne_uncer']) || $_POST['ne_uncer'] == '')) {
123 124
        array_push($missing_fields,
        'Production rate, sample depth, and/or uncertainty for ' .
125 126
        '<sup>21</sup>Ne');
}
127 128 129 130
if (!$response->success) {
        array_push($missing_fields,
        'reCAPTCHA challenge');
}
131

132

133
// If something is missing, send error to user and make him/her go back
134
if (count($missing_fields) > 0) {
135 136
    //$error_msg = '<html><body>' .
        //'<h2>Invalid input</h2>';
137 138 139 140 141 142 143
    $error_msg = '
<main>
<div class="section no-pad-bot" id="index-banner">
    <div class="container">
        <br><br>
        <!-- page header -->
        <h1 class="header center orange-text">Invalid input</h1>';
144

145 146
    // generate comma-separated list of missing field names
    for ($i = 0; $i < count($missing_fields); $i++) {
147 148

        // text before list of field names
149
        if ($i == 0 && count($missing_fields) == 1) {
150
            $error_msg .= '<p><b>The following value is missing:</b> <ul class="collection">';
151
        } elseif ($i == 0) {
152
            $error_msg .= '<p><b>The following values are missing:</b> <ul class="collection">';
153 154
        }

155
        $error_msg .= '<li class="collection-item">' . $missing_fields[$i] . '</li>';
156
    }
157 158
    $error_msg .= '</ul></p><p><b>Please <a href="javascript:history.back()">go' .
       ' back</a> and fill in the missing fields.</b></p></div></div></main>';
159
       //' back</a> and fill in the missing fields.</p></main></html>';
160 161
    //die($error_msg); // end this script, print error
    include('head.html');
162 163
    include('navigation.html');
    echo($error_msg);
164 165
    include('foot.html');
    die();
166 167 168 169
}


// If this is reached, input is ok and it is time to write the file for matlab
170 171 172 173 174 175

// generate string containing all user input.
// addslashes adds backslashes before characters that need to be escaped.

$fieldnames = array(
    'sample_id',
176
    'name',
177 178 179
    'email',
    'lat',
    'long',
180 181 182 183 184 185 186 187
    'be_conc',
    'al_conc',
    'c_conc',
    'ne_conc',
    'be_uncer',
    'al_uncer',
    'c_uncer',
    'ne_uncer',
188
    'zobs',
189 190 191 192
    'be_prod_spall',
    'al_prod_spall',
    'c_prod_spall',
    'ne_prod_spall',
193 194 195 196
    'be_prod_muons',
    'al_prod_muons',
    'c_prod_muons',
    'ne_prod_muons',
197 198 199 200 201
    'rock_density',
    'epsilon_gla_min',
    'epsilon_gla_max',
    'epsilon_int_min',
    'epsilon_int_max',
202 203
    't_degla_min',
    't_degla_max',
204 205
    'record', // check if set missing
    'record_threshold_min',
206 207
    'record_threshold_max',
    'nwalkers');
208 209

// Generate unique output file name
210 211
//$outputfile = tempnam('/tmp', 'cosmo_');
$outputfile = tempnam('/home/adc/cosmo/input', 'cosmo_');
Anders Damsgaard's avatar
Anders Damsgaard committed
212
$id = explode('_', $outputfile)[1];
213 214 215 216
if (is_writable($outputfile)) {

    if (!$handle = fopen($outputfile, 'w')) {
        die("The php server could not open $outputfile.");
217 218
    }

219 220
    // write values to file
    foreach ($fieldnames as $fieldname) {
Anders Damsgaard's avatar
Anders Damsgaard committed
221
        if (fwrite($handle, addslashes(trim($_POST[$fieldname])) . "\t") === false) {
222 223 224
            die("The php server could not write $fieldname to $outputfile.");
        }
    }
225 226

} else {
227
    die("The php server output file $outputfile does not exist or is not writable");
228
}
229

230 231 232 233
// change permissions of output file
if (!chmod($outputfile, 0777)) {
    die("The php server could not set proper permissions for $outputfile.");
}
234

235
// Create inversion status output file
236 237 238 239
$statusfile = ("/home/adc/cosmo/input/status_" . $id);
if (!$handle = fopen($statusfile, 'w')) {
    die("The php server could not open $statusfile.");
}
240

241 242 243
// write status to file
if (fwrite($handle, "Queued") === false) {
    die("The php server could not write the status to $statusfile.");
244
}
245

246 247 248 249
// change permissions of status file
if (!chmod($statusfile, 0777)) {
    die("The php server could not set proper permissions for $statusfile.");
}
250

Anders Damsgaard's avatar
Anders Damsgaard committed
251

252 253
// Finally redirect user after processing uploaded data. This header function 
// call must be before any output!
254
header("Location: /index.php?wait_id=" . $id);
255

256
?>