PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-dxlog/admin/readlog.php

https://github.com/clarkema/wp-dxlog
PHP | 575 lines | 398 code | 88 blank | 89 comment | 58 complexity | baf35086a73928e6126da9690a3378dc MD5 | raw file
  1. <?php
  2. require_once('../wp-content/plugins/wp-dxlog/dbinc.php');
  3. require_once('../wp-content/plugins/wp-dxlog/error.inc');
  4. // Function to read one line of QSO data from the Cabrillo file and parse according to the contest type
  5. // Parameters:
  6. // $contest_type - Cabrillo contest type (determines the number of columns in the QSO data)
  7. // $s - the QSO: line from the Cabrillo file
  8. // &$qso_data - array to put parsed data into
  9. function readCabrilloQSO ($contest_type, $s, &$qso_data)
  10. {
  11. switch ($contest_type)
  12. {
  13. case "ARRL-VHF-SEP":
  14. // This Cabrillo format has 9 columns (including QSO: column)
  15. sscanf ($s, "%s %s %s %s %s %s %s %s %s",
  16. &$dummy,
  17. &$qso_data['freq'],
  18. &$qso_data['mode'],
  19. &$dummy,
  20. &$dummy,
  21. &$dummy,
  22. &$dummy,
  23. &$qso_data['call'],
  24. &$dummy);
  25. break;
  26. case "IARU":
  27. case "AP-SPRINT":
  28. case "ARRL-10":
  29. case "ARRL-160":
  30. case "ARRL-DX":
  31. case "CQ-WPX":
  32. // This Cabrillo format has 11 columns (including QSO: column)
  33. sscanf ($s, "%s %s %s %s %s %s %s %s %s %s %s",
  34. &$dummy,
  35. &$qso_data['freq'],
  36. &$qso_data['mode'],
  37. &$dummy,
  38. &$dummy,
  39. &$dummy,
  40. &$dummy,
  41. &$dummy,
  42. &$qso_data['call'],
  43. &$dummy,
  44. &$dummy);
  45. break;
  46. case "RSGB-IOTA":
  47. case "CQ-WW-RTTY":
  48. // This Cabrillo format has 13 columns (including QSO: column)
  49. sscanf ($s, "%s %s %s %s %s %s %s %s %s %s %s %s %s",
  50. &$dummy,
  51. &$qso_data['freq'],
  52. &$qso_data['mode'],
  53. &$dummy,
  54. &$dummy,
  55. &$dummy,
  56. &$dummy,
  57. &$dummy,
  58. &$dummy,
  59. &$qso_data['call'],
  60. &$dummy,
  61. &$dummy,
  62. &$dummy);
  63. break;
  64. case "RSGB 21":
  65. // This Cabrillo format has 13 columns (including QSO: column)
  66. sscanf ($s, "%s %s %s %s %s %s %s %s %s %s %s %s %s",
  67. &$dummy,
  68. &$qso_data['freq'],
  69. &$qso_data['mode'],
  70. &$dummy,
  71. &$dummy,
  72. &$dummy,
  73. &$dummy,
  74. &$dummy,
  75. &$qso_data['call'],
  76. &$dummy,
  77. &$dummy,
  78. &$dummy,
  79. &$dummy);
  80. break;
  81. case "ARRL-SS-CW":
  82. // This Cabrillo format has 15 columns (including QSO: column)
  83. sscanf ($s, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
  84. &$dummy,
  85. &$qso_data['freq'],
  86. &$qso_data['mode'],
  87. &$dummy,
  88. &$dummy,
  89. &$dummy,
  90. &$dummy,
  91. &$dummy,
  92. &$dummy,
  93. &$dummy,
  94. &$qso_data['call'],
  95. &$dummy,
  96. &$dummy,
  97. &$dummy,
  98. &$dummy);
  99. break;
  100. case "DXPEDITION":
  101. // This Cabrillo format has 9 columns (including QSO: column)
  102. sscanf ($s, "%s %s %s %s %s %s %s %s %s",
  103. &$dummy,
  104. &$qso_data['freq'],
  105. &$qso_data['mode'],
  106. &$dummy,
  107. &$dummy,
  108. &$dummy,
  109. &$dummy,
  110. &$qso_data['call'],
  111. &$dummy);
  112. break;
  113. default:
  114. // The default Cabrillo format has 11 columns (inluding QSO: column)
  115. sscanf ($s, "%s %s %s %s %s %s %s %s %s %s %s",
  116. &$dummy,
  117. &$qso_data['freq'],
  118. &$qso_data['mode'],
  119. &$dummy,
  120. &$dummy,
  121. &$dummy,
  122. &$dummy,
  123. &$dummy,
  124. &$qso_data['call'],
  125. &$dummy,
  126. &$dummy);
  127. break;
  128. }
  129. }
  130. // Function to convert frequency (211234) to band (15)
  131. // Only 160 to 6m
  132. // Parameters:
  133. // frequency to convert to band
  134. function convertFrequencyBand ($frequency)
  135. {
  136. // 1.8 or 18 MHz
  137. if (ereg('^18',$frequency))
  138. {
  139. if (strlen($frequency) > 4)
  140. $band = 17;
  141. else
  142. $band = 160;
  143. }
  144. // 3.5 or 3.8 MHz
  145. elseif (ereg('^3',$frequency))
  146. {
  147. $band = 80;
  148. }
  149. // 7.0 or 7.1 MHz
  150. elseif (ereg('^7',$frequency))
  151. {
  152. $band = 40;
  153. }
  154. // 10.1 MHz
  155. elseif (ereg('^10',$frequency))
  156. {
  157. $band = 30;
  158. }
  159. // 14 MHz
  160. elseif (ereg('^14',$frequency))
  161. {
  162. $band = 20;
  163. }
  164. // 21 MHz
  165. elseif (ereg('^21',$frequency))
  166. {
  167. $band = 15;
  168. }
  169. // 24 MHz
  170. elseif (ereg('^24',$frequency))
  171. {
  172. $band = 12;
  173. }
  174. // 28 MHz
  175. elseif (ereg('^28',$frequency))
  176. {
  177. $band = 10;
  178. }
  179. // 29 MHz FM
  180. elseif (ereg('^29',$frequency))
  181. {
  182. $band = 10;
  183. }
  184. // 50 MHz
  185. elseif (ereg('^50',$frequency))
  186. {
  187. $band = 6;
  188. }
  189. else
  190. $band = 0;
  191. return $band;
  192. }
  193. // Function to read a Cabrillo file and insert each QSO into the database
  194. // Parameters:
  195. // $file - file pointer of Cabrillo file being read
  196. // $qso_count - (global) number of QSOs read
  197. // $connection - Database connection
  198. // $dxcallsign - FK to logbook
  199. function processCabrilloFile ($file, &$qso_count, $connection, $dxcallsign)
  200. {
  201. // Initialise the array
  202. $qso_data = array ('freq' => '',
  203. 'mode' => '',
  204. 'call' => '');
  205. // Read the Cabrillo file until we reach the "CONTEST:" tag
  206. while (fscanf ($file,"%s %s",&$tag, &$value))
  207. {
  208. if (!strcasecmp($tag, "CONTEST:"))
  209. {
  210. // Read the contest type so that we can parse the file
  211. $contest_type = $value;
  212. echo "<p>Cabrillo Contest type is $contest_type <P>\n";
  213. break;
  214. }
  215. else
  216. // Continue until the CONTEST: tag is reached
  217. continue;
  218. }
  219. // Keep a count of the number of QSOs added to the database
  220. $qso_count = 0;
  221. // Read each line of the log file
  222. while ($s = fgets ($file,1024))
  223. {
  224. $line = explode (' ', $s);
  225. // Skip Cabrillo header lines
  226. if (!strcasecmp($line[0], "QSO:"))
  227. {
  228. // Read one line of QSO data from the Cabrillo file
  229. readCabrilloQSO ($contest_type, $s, &$qso_data);
  230. }
  231. else
  232. // Continue reading until the "QSO" tag
  233. continue;
  234. // Convert frequency to band
  235. $band = convertFrequencyBand ($qso_data['freq']);
  236. // Trap unknown bands error
  237. if ($band == 0)
  238. {
  239. $freq = $qso_data['freq'];
  240. echo "<P><EM>Error - Frequency to Band conversion failed - frequency: $freq</EM></P>\n";
  241. echo "<P>No QSOs loaded\n";
  242. echo "<p><A HREF=\"uploadlog.php\">Return to Log Upload Page</A>\n";
  243. die();
  244. }
  245. // Cabrillo logs contain mode as "CW/PH/RY"
  246. // Convert PH to SSB
  247. if (!strcasecmp($qso_data['mode'],"PH"))
  248. $qso_data['mode'] = 'SSB';
  249. // Convert RY to DIG
  250. if (!strcasecmp($qso_data['mode'],"RY"))
  251. $qso_data['mode'] = 'DIG';
  252. // Insert QSO into the database
  253. $query = "INSERT INTO qsos SET id = 0, " .
  254. "callsign = \"" . $qso_data['call'] . "\" , " .
  255. "op_mode = \"" . $qso_data['mode'] . "\" , " .
  256. "band = \"" . $band . "\" , " .
  257. "fk_dxstn = \"" . $dxcallsign . "\" ";
  258. if (!(@ mysql_query ($query, $connection)))
  259. showerror();
  260. $qso_count++;
  261. }
  262. }
  263. // Function to read one line of QSO data from the AIF file
  264. // Parameters:
  265. // $string - first valid line from file (<EOH> or <CALL>
  266. // $qso_data - array to put parsed data into
  267. // $band_found - boolean flag for <BAND> tag
  268. // $freq_found - boolean flag for <FREQ> tag
  269. function readADIFQSO ($string, &$qso_data, &$band_found, &$freq_found)
  270. {
  271. $string = strtoupper ($string);
  272. // Read Callsign
  273. if ($s = stristr($string,"<CALL"))
  274. {
  275. $values = sscanf ($s, "<CALL:%d>%s ", $length,$qso_data['call']);
  276. if ($values != 2)
  277. sscanf ($s, "<CALL:%d:%c>%s ", $length,$dummy,$qso_data['call']);
  278. }
  279. // Read Band
  280. if ($s = stristr($string,"<BAND"))
  281. {
  282. $band_found = 1;
  283. $values = sscanf ($s, "<BAND:%d>%s ", $length,$qso_data['band']);
  284. if ($values != 2)
  285. sscanf ($s, "<BAND:%d:%c>%s ", $length,$dummy,$qso_data['band']);
  286. // Strip the 'M off e.g. 40M
  287. if (($pos = strpos ($qso_data['band'], 'M')) != NULL)
  288. $qso_data['band'][$pos] = ' ';
  289. }
  290. // Read Mode
  291. if ($s = stristr($string,"<MODE"))
  292. {
  293. $values = sscanf ($s, "<MODE:%d>%s ", $length,$qso_data['mode']);
  294. if ($values != 2)
  295. sscanf ($s, "<MODE:%d:%c>%s ", $length,$dummy,$qso_data['mode']);
  296. switch ($qso_data['mode'])
  297. {
  298. // Convert all Digital modes to 'DIG'
  299. case "PSK31":
  300. case "PSK63":
  301. case "BPSK31":
  302. case "BPSK63":
  303. case "RTTY":
  304. case "MFSK16":
  305. case "WSJT":
  306. case "FSK441":
  307. case "JT6M":
  308. $qso_data['mode'] = "DIG";
  309. break;
  310. // Convert all Phone modes to SSB
  311. case "USB":
  312. case "LSB":
  313. case "FM":
  314. case "AM":
  315. $qso_data['mode'] = "SSB";
  316. break;
  317. }
  318. }
  319. // Read Frequency (e.g. if Band is not present in the record)
  320. if (($s = stristr($string,"<FREQ")) && $band_found == 0)
  321. {
  322. $freq_found = 1;
  323. $values = sscanf ($s, "<FREQ:%d>%s ", $length,$qso_data['freq']);
  324. if ($values != 2)
  325. sscanf ($s, "<FREQ:%d:%c>%s ", $length,$dummy,$qso_data['freq']);
  326. }
  327. }
  328. // Function to read an ADIF file and insert each QSO into the database
  329. function processADIFFile ($file, &$qso_count, $connection, $string, $dxcallsign)
  330. {
  331. $EOR = 0;
  332. $band_found = 0;
  333. $freq_found = 0;
  334. $blank_line = 1;
  335. $qso_data = array ('band' => '',
  336. 'mode' => '',
  337. 'freq' => '',
  338. 'call' => '');
  339. // Can enter this procedure with $string set to either <CALL:x> which is a valid QSO
  340. // which needs to be processed or set to <EOH> in which case we need to read any blank
  341. // lines until the first QSO
  342. if (stristr($string,"<EOH>"))
  343. // Skip any blank lines
  344. while ($string == "\n" || $string == "\r\n")
  345. $string = fgets ($file, 1024);
  346. // process the first valid ADIF line
  347. readADIFQSO ($string, &$qso_data, &$band_found, &$freq_found);
  348. while (($string = fgets ($file,1024)))
  349. {
  350. // Skip any blank lines
  351. if ($string == "\n" || $string == "\r\n")
  352. continue;
  353. while (!$EOR)
  354. {
  355. readADIFQSO ($string, &$qso_data, &$band_found, &$freq_found);
  356. // Check for End of Record
  357. if (stristr($string,"<EOR>"))
  358. $EOR = 1;
  359. else
  360. $string = fgets($file,1024);
  361. }
  362. // End of Record. If no <BAND> data has been found then
  363. // convert the frequency to band
  364. if ($band_found == 0)
  365. // Convert the frequency to band
  366. $qso_data['band'] = convertFrequencyBand ($qso_data['freq']);
  367. // Insert QSO into the database
  368. $query = "INSERT INTO qsos SET id = 0, " .
  369. "callsign = \"" . $qso_data['call'] . "\" , " .
  370. "op_mode = \"" . $qso_data['mode'] . "\" , " .
  371. "band = \"" . $qso_data['band'] . "\" , " .
  372. "fk_dxstn = \"" . $dxcallsign . "\" ";
  373. $band_found = 0;
  374. $freq_found = 0;
  375. $EOR = 0;
  376. $qso_data[]='';
  377. if (!(@ mysql_query ($query, $connection)))
  378. showerror();
  379. $qso_count++;
  380. }
  381. }
  382. // Start of Main Program
  383. $valid_file = 0;
  384. $file_type = "unknown";
  385. // Record the start time of the script
  386. $start = microtime();
  387. sscanf ($start,"%s %s",&$microseconds,&$seconds);
  388. $start_time = $seconds + $microseconds;
  389. // Read data posted from form
  390. $browser_name = $_FILES['userfile']['name'];
  391. $temp_name = $_FILES['userfile']['tmp_name'];
  392. $dxcallsign = $_POST['callsign'];
  393. // Connect to the database
  394. if (!($connection = @ mysql_connect ($hostName,
  395. $username,
  396. $password)))
  397. die ("Could not connect to database");
  398. if (!mysql_select_db ($databaseName, $connection))
  399. showerror();
  400. echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\">";
  401. echo "<html>";
  402. echo "<head>";
  403. echo "<title>Upload Log</title>";
  404. echo "</head>";
  405. echo "<body>";
  406. // Was a log file uploaded?
  407. if (is_uploaded_file ($temp_name))
  408. {
  409. echo "<h1>File Upload $browser_name</h1>";
  410. echo "<p>Filename - $browser_name\n";
  411. echo "<br>DX Callsign - $dxcallsign\n";
  412. // Open the log file
  413. if (!($file = fopen ($temp_name, "r")))
  414. die ("Could not open the log file $file\n");
  415. // Read the first line
  416. $string = fgets ($file, 1024);
  417. // Check that it is a Cabrillo File
  418. if (stristr($string, "START-OF-LOG:"))
  419. {
  420. // Process Cabrillo file
  421. processCabrilloFile ($file,&$qso_count, $connection, $dxcallsign);
  422. $file_type = "CABRILLO";
  423. }
  424. // Check if it is an ADIF file
  425. elseif (stristr($string, "<EOH>") || stristr($string,"<CALL"))
  426. {
  427. // Process ADIF file
  428. processADIFFile ($file,&$qso_count, $connection,$string, $dxcallsign);
  429. $file_type = "ADIF";
  430. }
  431. else
  432. {
  433. while (($string = fgets ($file, 1024)) && !$valid_file)
  434. {
  435. if (stristr($string, "<EOH>") || stristr($string,"<CALL"))
  436. {
  437. $valid_file = 1;
  438. processADIFFile ($file,&$qso_count, $connection,$string, $dxcallsign);
  439. $file_type = "ADIF";
  440. }
  441. }
  442. // No Cabrillo or ADFI file found - exit with an error
  443. if (!$valid_file)
  444. {
  445. echo "<P>Error - Unable to upload file: $browser_name\n";
  446. echo "<P>Invalid Cabrillo or ADIF file\n";
  447. echo "<P>No QSOs loaded\n";
  448. echo "<p><A HREF=\"uploadlog.php\">Return to Log Upload Page</A>\n";
  449. die();
  450. }
  451. }
  452. // Record the end time of the script
  453. $end = microtime();
  454. sscanf ($end,"%s %s",&$microseconds,&$seconds);
  455. $end_time = $seconds + $microseconds;
  456. // Calculate elapsed time for the script
  457. $elapsed = $end_time - $start_time;
  458. sscanf ($elapsed,"%5f", &$elapsed_time);
  459. // Determine the callsign for these logs
  460. $query = "SELECT dxcallsign from dxstation where id=$dxcallsign";
  461. if (!($result = @ mysql_query ($query, $connection)))
  462. showerror();
  463. $row = @ mysql_fetch_array ($result);
  464. $callsign = $row['dxcallsign'];
  465. echo "<P>File type loaded: $file_type";
  466. echo "<p>A total of $qso_count QSOs were added to the database ";
  467. echo "for callsign $callsign<P>";
  468. echo "Elapsed time = $elapsed_time seconds";
  469. // Count the total number of QSOs in the database
  470. $query = "SELECT count(*) from qsos";
  471. if (!($result = @ mysql_query ($query, $connection)))
  472. showerror();
  473. $row = @ mysql_fetch_array ($result);
  474. $total_qso_count = $row['count(*)'];
  475. echo "<P>There are now $total_qso_count QSOs in the database<P>";
  476. }
  477. else
  478. {
  479. // No file uploaded
  480. echo "<h1>No file Uploaded</h1>";
  481. }
  482. echo "<p><A HREF=\"index.php\">Return to Log Upload Page</A>\n";
  483. if (!mysql_close ($connection))
  484. showerror();
  485. echo "</body>";
  486. echo "</html>";
  487. ?>