tuploadhistory.php - cosmo - front and backend for Markov-Chain Monte Carlo inversion of cosmogenic nuclide concentrations
 (HTM) git clone git://src.adamsgaard.dk/cosmo
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tuploadhistory.php (8822B)
       ---
            1 <?php
            2 // uploadhistory.php
            3 // Validates form data from pages/history.html and writes a file for the Matlab 
            4 // script file_scanner_mcmc_starter.m to read as input for the MCMC inversion.
            5 
            6 // reCAPTCHA setup
            7 require_once('recaptchalib.php');
            8 
            9 // your secret key
           10 $secret = "6LeMrRATAAAAAOdcvVGi6PfR__XGOVoUP7lCqHp1";
           11  
           12 // empty response
           13 $response = null;
           14  
           15 // check secret key
           16 $reCaptcha = new ReCaptcha($secret);
           17 
           18 // if submitted check response
           19 if ($_POST["g-recaptcha-response"]) {
           20     $response = $reCaptcha->verifyResponse(
           21         $_SERVER["REMOTE_ADDR"],
           22         $_POST["g-recaptcha-response"]
           23     );
           24 }
           25 
           26 
           27 //$missing_fields = ''; // string of missing field names
           28 $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     array_push($missing_fields, 'Sample ID');
           34 }
           35 if (!isset($_POST['name']) || $_POST['name'] == '') {
           36     array_push($missing_fields, 'Your Name');
           37 }
           38 if (!isset($_POST['email']) || $_POST['email'] == '') {
           39     array_push($missing_fields, 'Email');
           40 }
           41 if (!isset($_POST['lat']) || $_POST['lat'] == '') {
           42     array_push($missing_fields, 'Latitude');
           43 }
           44 if (!isset($_POST['long']) || $_POST['long'] == '') {
           45     array_push($missing_fields, 'Longitude');
           46 }
           47 if (!isset($_POST['rock_density']) || $_POST['rock_density'] == '') {
           48     array_push($missing_fields, 'Rock density');
           49 }
           50 if (!isset($_POST['epsilon_gla_min']) || $_POST['epsilon_gla_min'] == '') {
           51     array_push($missing_fields, 'Min. glacial erosion rate');
           52 }
           53 if (!isset($_POST['epsilon_gla_max']) || $_POST['epsilon_gla_max'] == '') {
           54     array_push($missing_fields, 'Max. glacial erosion rate');
           55 }
           56 if (!isset($_POST['epsilon_int_min']) || $_POST['epsilon_int_min'] == '') {
           57     array_push($missing_fields, 'Min. inter-glacial erosion rate');
           58 }
           59 if (!isset($_POST['epsilon_int_max']) || $_POST['epsilon_int_max'] == '') {
           60     array_push($missing_fields, 'Max. inter-glacial erosion rate');
           61 }
           62 if (!isset($_POST['t_degla_min']) || $_POST['t_degla_min'] == '') {
           63     array_push($missing_fields, 'Minimum time since deglaciation');
           64 }
           65 if (!isset($_POST['t_degla_max']) || $_POST['t_degla_max'] == '') {
           66     array_push($missing_fields, 'Maximum time since deglaciation');
           67 }
           68 if (!isset($_POST['record']) || $_POST['record'] == ''){
           69     array_push($missing_fields, 'Climate record');
           70 }
           71 if (!isset($_POST['record_threshold_min']) || $_POST['record_threshold_min'] == ''){
           72     array_push($missing_fields, 'Min. climate record threshold value');
           73 }
           74 if (!isset($_POST['record_threshold_max']) || $_POST['record_threshold_max'] == ''){
           75     array_push($missing_fields, 'Max. climate record threshold value');
           76 }
           77 if (!isset($_POST['nwalkers']) || $_POST['nwalkers'] == ''){
           78     array_push($missing_fields, 'Number of MCMC walkers');
           79 }
           80 
           81 if (!isset($_POST['zobs']) || $_POST['zobs'] == ''){
           82     array_push($missing_fields, 'One or more sample depths');
           83 }
           84 
           85 // Check TCN concentrations, at least one value is needed
           86 if ((!isset($_POST['be_conc']) || $_POST['be_conc'] == '') &&
           87     (!isset($_POST['al_conc']) || $_POST['al_conc'] == '') &&
           88     (!isset($_POST['c_conc'])  || $_POST['c_conc'] == '') &&
           89     (!isset($_POST['ne_conc']) || $_POST['ne_conc']) == '') {
           90         array_push($missing_fields, 'At least 1 TCN concentration');
           91 }
           92 
           93 // For each isotope concentration there should be uncertainty and production 
           94 // rate
           95 if ((isset($_POST['be_conc']) && $_POST['be_conc'] != '') &&
           96     (!isset($_POST['be_prod_muons']) || $_POST['be_prod_muons'] == '' ||
           97     !isset($_POST['be_prod_spall']) || $_POST['be_prod_spall'] == '' ||
           98     !isset($_POST['be_uncer']) || $_POST['be_uncer'] == '')) {
           99     array_push($missing_fields,
          100         'Production rate, sample depth, and/or uncertainty for ' .
          101         '<sup>10</sup>Be');
          102 }
          103 if ((isset($_POST['al_conc']) && $_POST['al_conc'] != '') &&
          104     (!isset($_POST['al_prod_muons']) || $_POST['al_prod_muons'] == '' ||
          105     !isset($_POST['al_prod_spall']) || $_POST['al_prod_spall'] == '' ||
          106     !isset($_POST['al_uncer']) || $_POST['al_uncer'] == '')) {
          107         array_push($missing_fields,
          108         'Production rate, sample depth, and/or uncertainty for ' .
          109         '<sup>26</sup>Al');
          110 }
          111 if ((isset($_POST['c_conc']) && $_POST['c_conc'] != '') &&
          112     (!isset($_POST['c_prod_muons']) || $_POST['c_prod_muons'] == '' ||
          113     !isset($_POST['c_prod_spall']) || $_POST['c_prod_spall'] == '' ||
          114     !isset($_POST['c_uncer']) || $_POST['c_uncer'] == '')) {
          115         array_push($missing_fields,
          116         'Production rate, sample depth, and/or uncertainty for ' .
          117         '<sup>14</sup>C');
          118 }
          119 if ((isset($_POST['ne_conc']) && $_POST['ne_conc'] != '') &&
          120     (!isset($_POST['ne_prod_muons']) || $_POST['ne_prod_muons'] == '' ||
          121     !isset($_POST['ne_prod_spall']) || $_POST['ne_prod_spall'] == '' ||
          122     !isset($_POST['ne_uncer']) || $_POST['ne_uncer'] == '')) {
          123         array_push($missing_fields,
          124         'Production rate, sample depth, and/or uncertainty for ' .
          125         '<sup>21</sup>Ne');
          126 }
          127 if (!$response->success) {
          128         array_push($missing_fields,
          129         'reCAPTCHA challenge');
          130 }
          131 
          132 
          133 // If something is missing, send error to user and make him/her go back
          134 if (count($missing_fields) > 0) {
          135     //$error_msg = '<html><body>' .
          136         //'<h2>Invalid input</h2>';
          137     $error_msg = '
          138 <main>
          139 <div class="section no-pad-bot" id="index-banner">
          140     <div class="container">
          141         <br><br>
          142         <!-- page header -->
          143         <h1 class="header center orange-text">Invalid input</h1>';
          144 
          145     // generate comma-separated list of missing field names
          146     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     $error_msg .= '</ul></p><p><b>Please <a href="javascript:history.back()">go' .
          158        ' 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     //die($error_msg); // end this script, print error
          161     include('head.html');
          162     include('navigation.html');
          163     echo($error_msg);
          164     include('foot.html');
          165     die();
          166 }
          167 
          168 
          169 // If this is reached, input is ok and it is time to write the file for matlab
          170 
          171 // generate string containing all user input.
          172 // addslashes adds backslashes before characters that need to be escaped.
          173 
          174 $fieldnames = array(
          175     'sample_id',
          176     'name',
          177     'email',
          178     'lat',
          179     'long',
          180     'be_conc',
          181     'al_conc',
          182     'c_conc',
          183     'ne_conc',
          184     'be_uncer',
          185     'al_uncer',
          186     'c_uncer',
          187     'ne_uncer',
          188     'zobs',
          189     'be_prod_spall',
          190     'al_prod_spall',
          191     'c_prod_spall',
          192     'ne_prod_spall',
          193     'be_prod_muons',
          194     'al_prod_muons',
          195     'c_prod_muons',
          196     'ne_prod_muons',
          197     'rock_density',
          198     'epsilon_gla_min',
          199     'epsilon_gla_max',
          200     'epsilon_int_min',
          201     'epsilon_int_max',
          202     't_degla_min',
          203     't_degla_max',
          204     'record', // check if set missing
          205     'record_threshold_min',
          206     'record_threshold_max',
          207     'nwalkers');
          208 
          209 // Generate unique output file name
          210 //$outputfile = tempnam('/tmp', 'cosmo_');
          211 $outputfile = tempnam('/home/adc/cosmo/input', 'cosmo_');
          212 $id = explode('_', $outputfile)[1];
          213 if (is_writable($outputfile)) {
          214 
          215     if (!$handle = fopen($outputfile, 'w')) {
          216         die("The php server could not open $outputfile.");
          217     }
          218 
          219     // write values to file
          220     foreach ($fieldnames as $fieldname) {
          221         if (fwrite($handle, addslashes(trim($_POST[$fieldname])) . "\t") === false) {
          222             die("The php server could not write $fieldname to $outputfile.");
          223         }
          224     }
          225 
          226 } else {
          227     die("The php server output file $outputfile does not exist or is not writable");
          228 }
          229 
          230 // change permissions of output file
          231 if (!chmod($outputfile, 0777)) {
          232     die("The php server could not set proper permissions for $outputfile.");
          233 }
          234 
          235 // Create inversion status output file
          236 $statusfile = ("/home/adc/cosmo/input/status_" . $id);
          237 if (!$handle = fopen($statusfile, 'w')) {
          238     die("The php server could not open $statusfile.");
          239 }
          240 
          241 // write status to file
          242 if (fwrite($handle, "Queued") === false) {
          243     die("The php server could not write the status to $statusfile.");
          244 }
          245 
          246 // change permissions of status file
          247 if (!chmod($statusfile, 0777)) {
          248     die("The php server could not set proper permissions for $statusfile.");
          249 }
          250 
          251 
          252 // Finally redirect user after processing uploaded data. This header function 
          253 // call must be before any output!
          254 header("Location: /index.php?wait_id=" . $id);
          255 
          256 ?>