PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/calibration/form-process.php

https://github.com/ngacharts/opencpn.info
PHP | 204 lines | 162 code | 13 blank | 29 comment | 84 complexity | ddad9e71c2458d3b2d7b358e2059f3a1 MD5 | raw file
  1. <?php
  2. session_start();
  3. $sessID = session_id();
  4. $ret = '';
  5. $db_errors = '';
  6. $cid = strip_tags($_POST['chartID']);
  7. $sid = strip_tags($_POST['sessionID']);
  8. if($sessID != $sid) $ret = "Session ID does not match!";
  9. else {
  10. foreach($_POST as $k => $v) {
  11. $params[$k] = trim(strip_tags($v));
  12. $ret .= $params[$k]."<br />";
  13. }
  14. }
  15. // ### Connect to the DB
  16. $link = mysql_connect('localhost', 'gunther', 'tatJana2603');
  17. if (!$link) {
  18. die('Connection failed: ' . mysql_error());
  19. }
  20. $db_selected = mysql_select_db('usr_web12_1', $link);
  21. if (!$db_selected) {
  22. die ('Cannot use DB: ' . mysql_error());
  23. }
  24. // The saving starts here =====================================================================================================
  25. if (isset($_POST['chartID'])) //A very stupid check whether we actually should save something
  26. {
  27. //load all data, so we can fill in what the form does not send back.
  28. $result = mysql_query('SELECT * FROM ocpn_nga_charts_with_params WHERE number = "'.$cid.'"');
  29. $chart = mysql_fetch_array($result);
  30. mysql_free_result($result);
  31. if ($chart['prerotate'] == 90 || $chart['prerotate'] == 270) {
  32. $hlp = $chart['width'];
  33. $chart['width'] = $chart['height'];
  34. $chart['height'] = $hlp;
  35. }
  36. //timestamp
  37. $timestamp = time();
  38. //find the current KAP
  39. $result = mysql_query('SELECT kap_id, scale, title FROM ocpn_nga_kap WHERE active = 1 AND is_main = 1 AND number = '.$cid.' LIMIT 0,1;');
  40. $kap = mysql_fetch_array($result);
  41. mysql_free_result($result);
  42. //Update ocpn_nga_charts
  43. mysql_query('UPDATE ocpn_nga_charts SET status_id = '.undef2null($params['status']).',bsb_chf = '.str2db($params['chart_type'], true).', bsb_chf_other = '.str2db($params['chart_type_other'], true).', status_other = '.str2db($params['status_other'], true).' WHERE number = '.$cid);
  44. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  45. //Invalidate the existing KAP info
  46. mysql_query('UPDATE ocpn_nga_kap SET active = 0 WHERE kap_id = '.$kap['kap_id']);
  47. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  48. //Insert the new data
  49. $query_fmt = 'INSERT INTO ocpn_nga_kap (number, is_main, status_id, locked, scale, title, NU, GD, PR, PP, UN, SD, DTMx, DTMy, DTMdat, changed, changed_by, active, bsb_type, GD_other, PR_other, UN_other, SD_other, DTMdat_other, locked_by, comments, noPP, noDTM, gpx)
  50. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, FROM_UNIXTIME(%s), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)';
  51. if ($_FILES['gpx']['size'] > 0)
  52. {
  53. // get contents of a file into a string
  54. $handle = fopen( $_FILES['gpx']['tmp_name'], "r");
  55. $gpx_str = '\''.mysql_real_escape_string(fread($handle, $_FILES['gpx']['size'])).'\'';
  56. fclose($handle);
  57. }
  58. else
  59. {
  60. $gpx_str = 'NULL';
  61. }
  62. //: Some values are not clear, so we will "invent" them for now
  63. //PP is not clear from the data sent from the form
  64. $pp_fake_hemi = 'N'; //TODO: will the form be extended or should we "invent" it here from the chart data - not 100% safe to do, but would almost be
  65. if ($params['pp_deg'] == -9999 || $params['pp_deg'] === '') //if not even degree is set, let's assume PP is totally incorrect or not entered at all
  66. $pp = 'NULL';
  67. else {
  68. if ($params['pp_min'] == -9999 || $params['pp_min'] === '')
  69. $ppmin = 0;
  70. else
  71. $ppmin = $params['pp_min'];
  72. $pp = deg2dbl($params['pp_deg'], $ppmin, 0, $pp_fake_hemi);
  73. }
  74. if (isset($params['noPP']))
  75. {
  76. $params['noPP'] = 1;
  77. $pp = 'NULL';
  78. }
  79. else
  80. $params['noPP'] = 'NULL';
  81. if (isset($params['noDTM']))
  82. {
  83. $params['noDTM'] = 1;
  84. $params['datum_adj_x'] = '';
  85. $params['datum_adj_y'] = '';
  86. $params['datum_adj_we'] = '';
  87. $params['datum_adj_ns'] = '';
  88. $params['datum_correction'] = '';
  89. $params['datum_correction_other'] = '';
  90. }
  91. else
  92. $params['noDTM'] = 'NULL';
  93. $query = sprintf($query_fmt, $cid, 1, undef2null($params['status']), 'NULL', $chart['scale'], str2db($chart['title']), str2db($cid), str2db(empty2null($params['datum'])), str2db(empty2null($params['projection'])),$pp , str2db(empty2null($params['soundings'])), str2db(empty2null($params['soundings_datum'])), min2dbl($params['datum_adj_x'], 0, $params['datum_adj_we']), min2dbl($params['datum_adj_y'], 0, $params['datum_adj_ns']), str2db($params['datum_correction'], true), $timestamp, $_SESSION['wp-user']['id'], 1, str2db('BASE'), str2db($params['datum_other'], true), str2db($params['projection_other'], true), str2db($params['soundings_other'], true), str2db($params['soundings_datum_other'], true), str2db($params['datum_correction_other'], true), 'NULL', str2db($params['comment'], true), $params['noPP'], $params['noDTM'], $gpx_str);
  94. mysql_query($query);
  95. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  96. $new_kap_id = mysql_insert_id($link);
  97. //Invalidate REF points
  98. mysql_query('UPDATE ocpn_nga_kap_point SET active = 0 WHERE point_type=\'REF\' AND kap_id = '.$kap['kap_id']);
  99. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  100. //Insert new points
  101. $query_fmt = 'INSERT INTO ocpn_nga_kap_point (kap_id, latitude, longitude, x, y, point_type, created_by, created, sequence, active) VALUES (%s, %s, %s, %s, %s, %s, %s, FROM_UNIXTIME(%s), %s, %s)';
  102. //SW and NE corners are a special case - we save them even in case we have just LAT/LON and no coordinates
  103. if (($params['xcoordh_sw'] !== '' && $params['ycoordh_sw'] !== '' && $params['xcoordh_sw'] != 25 && $params['ycoordh_sw'] != $chart['height'] - 25) || deg2dbl($params['lat_deg_sw'], $params['lat_min_sw'], $params['lat_sec_sw'], $params['lat_ns_sw']) != 'NULL' || deg2dbl($params['lng_deg_sw'], $params['lng_min_sw'], $params['lng_sec_sw'], $params['lng_we_sw']) != 'NULL')
  104. {
  105. $latitude = deg2dbl($params['lat_deg_sw'], $params['lat_min_sw'], $params['lat_sec_sw'], $params['lat_ns_sw']);
  106. $longitude = deg2dbl($params['lng_deg_sw'], $params['lng_min_sw'], $params['lng_sec_sw'], $params['lng_we_sw']);
  107. $query = sprintf($query_fmt, $new_kap_id, $latitude, $longitude, empty2null($params['xcoordh_sw']), empty2null($params['ycoordh_sw']), str2db('REF'), $_SESSION['wp-user']['id'], $timestamp, 1, 1);
  108. mysql_query($query);
  109. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  110. }
  111. if ($params['xcoordh_nw'] !== '' && $params['ycoordh_nw'] !== '' && $params['xcoordh_nw'] != 25 && $params['ycoordh_sw'] != 25)
  112. {
  113. $latitude = deg2dbl($params['lat_deg_ne'], $params['lat_min_ne'], $params['lat_sec_ne'], $params['lat_ns_ne']);
  114. $longitude = deg2dbl($params['lng_deg_sw'], $params['lng_min_sw'], $params['lng_sec_sw'], $params['lng_we_sw']);
  115. $query = sprintf($query_fmt, $new_kap_id, $latitude, $longitude, $params['xcoordh_nw'], $params['ycoordh_nw'], str2db('REF'), $_SESSION['wp-user']['id'], $timestamp, 2, 1);
  116. mysql_query($query);
  117. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  118. }
  119. if (($params['xcoordh_ne'] !== '' && $params['ycoordh_ne'] !== '' && $params['xcoordh_ne'] != $chart['width'] - 25 && $params['ycoordh_ne'] != 25) || deg2dbl($params['lat_deg_ne'], $params['lat_min_ne'], $params['lat_sec_ne'], $params['lat_ns_ne']) != 'NULL' || deg2dbl($params['lng_deg_ne'], $params['lng_min_ne'], $params['lng_sec_ne'], $params['lng_we_ne']) != 'NULL')
  120. {
  121. $latitude = deg2dbl($params['lat_deg_ne'], $params['lat_min_ne'], $params['lat_sec_ne'], $params['lat_ns_ne']);
  122. $longitude = deg2dbl($params['lng_deg_ne'], $params['lng_min_ne'], $params['lng_sec_ne'], $params['lng_we_ne']);
  123. $query = sprintf($query_fmt, $new_kap_id, $latitude, $longitude, empty2null($params['xcoordh_ne']), empty2null($params['ycoordh_ne']), str2db('REF'), $_SESSION['wp-user']['id'], $timestamp, 3, 1);
  124. mysql_query($query);
  125. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  126. }
  127. if ($params['xcoordh_se'] !== '' && $params['ycoordh_se'] !== '' && $params['xcoordh_se'] != $chart['width'] - 25 && $params['ycoordh_se'] != $chart['height'] - 25)
  128. {
  129. $latitude = deg2dbl($params['lat_deg_sw'], $params['lat_min_sw'], $params['lat_sec_sw'], $params['lat_ns_sw']);
  130. $longitude = deg2dbl($params['lng_deg_ne'], $params['lng_min_ne'], $params['lng_sec_ne'], $params['lng_we_ne']);
  131. $query = sprintf($query_fmt, $new_kap_id, $latitude, $longitude, $params['xcoordh_se'], $params['ycoordh_se'], str2db('REF'), $_SESSION['wp-user']['id'], $timestamp, 4, 1);
  132. mysql_query($query);
  133. if(mysql_errno() !== 0) $db_errors .= mysql_errno() . ": " . mysql_error() . "\n";
  134. }
  135. }
  136. // The saving ends here =====================================================================================================
  137. // Helper functions declarations ============================================================================================
  138. // LAT or LON in human format to double
  139. function deg2dbl($deg, $min, $sec, $hemi)
  140. {
  141. if ($hemi == '-9999' || $hemi === '' || $deg === '' || $min === '' || $sec === '')
  142. return 'NULL'; //If something is not set, we consider the value invalid
  143. if (strtoupper($hemi) == 'S' || strtoupper($hemi) == 'W')
  144. $hemi = -1;
  145. else
  146. $hemi = 1;
  147. return $hemi * ($deg + $min / 60 + $sec / 3600);
  148. }
  149. // Datum shift in human format to double
  150. function min2dbl($min, $sec, $dir)
  151. {
  152. if ($min === '' || $sec === '')
  153. return 'NULL';
  154. if(!is_numeric($dir))
  155. if (strtoupper($dir) == 'S' || strtoupper($dir) == 'W')
  156. $dir = -1;
  157. else
  158. $dir = 1;
  159. return $dir * ($min + $sec / 60);
  160. }
  161. // undefined value (-9999) to null
  162. function undef2null($value)
  163. {
  164. if ($value == -9999)
  165. return 'NULL';
  166. return $value;
  167. }
  168. // empty value to null
  169. function empty2null($value)
  170. {
  171. if ($value === '')
  172. return 'NULL';
  173. return $value;
  174. }
  175. // string value to database string, with the possibility to replace empty with NULL
  176. function str2db($value, $empty2null = false)
  177. {
  178. if (($empty2null && $value === '') || $value === 'NULL' || $value === '-9999')
  179. return 'NULL';
  180. return '\''.mysql_real_escape_string($value).'\'';
  181. }
  182. /*
  183. #print_r($params);
  184. $response = "
  185. <script type=\"text/javascript\">
  186. alert('Response sent!');
  187. </script>
  188. ";
  189. */
  190. echo"$db_errors";
  191. ?>