diff --git a/src/db.php b/src/db.php index 33fa264..0cc3112 100644 --- a/src/db.php +++ b/src/db.php @@ -217,7 +217,7 @@ public function resetReingest($id) try { - if (DEBUG) debug('Updating HTML cache...'); + if (DEBUG) debug('Resetting reingest flag...'); $st = $this->db->prepare("UPDATE metadata m SET m.reingest=FALSE WHERE m.id = :id"); DB::tryBind($st, ":id", $id); if ($st->execute()) @@ -249,19 +249,19 @@ public function getCachedMSQ($id) if (DISABLE_MSQ_CACHE) { if (DEBUG) debug('Cache disabled.'); - return FALSE; + return null; } if ($this->needReingest($id)) { if (DEBUG) debug('Flagged for reingest.'); $this->resetReingest($id); - return FALSE; + return null; } if (!$this->connect()) return null; - $html = FALSE; + $html = null; try { @@ -276,15 +276,14 @@ public function getCachedMSQ($id) if ($html === NULL) { if (DEBUG) debug('No HTML cache found.'); - return FALSE; + return null; } else if (DEBUG) debug('Cached, returning HTML.'); } else { if (DEBUG) debug("No result for $id"); - echo '
Invalid MSQ
'; - return null; + return FALSE; } } catch (PDOException $e) @@ -295,40 +294,6 @@ public function getCachedMSQ($id) return $html; } - public function getMSQForDownload($id) - { - - if (!$this->connect()) return null; - - $xml = FALSE; - - try - { - $st = $this->db->prepare("SELECT xml FROM msqs INNER JOIN metadata ON metadata.msq = msqs.id WHERE metadata.id = :id LIMIT 1"); - DB::tryBind($st, ":id", $id); - $st->execute(); - if ($st->rowCount() > 0) - { - $result = $st->fetch(PDO::FETCH_ASSOC); - $st->closeCursor(); - $xml = $result['xml']; - if (DEBUG) debug('Cached, returning HTML.'); - } - else - { - echo "
No result for $id
"; - echo '
Invalid MSQ err 2
'; - return null; - } - } - catch (PDOException $e) - { - $this->dbError($e); - } - - return $xml; - } - /** * @brief Get a list of MSQs * @param $bq The BrowseQuery to filter results @@ -648,7 +613,7 @@ public function updateMetadata($id, $metadata) try { - if (DEBUG) debug('Updating HTML cache...'); + if (DEBUG) debug('Updating MSQ metadata...'); $st = $this->db->prepare("UPDATE metadata md SET md.fileFormat = :fileFormat, md.signature = :signature, md.firmware = :firmware, md.author = :author WHERE md.id = :id"); //$xml = mb_convert_encoding($html, "UTF-8"); DB::tryBind($st, ":id", $id); @@ -755,9 +720,6 @@ public function getXML($id) $result = $st->fetch(PDO::FETCH_ASSOC); $st->closeCursor(); $xml = $result['xml']; - } else { - //TODO Send real 404 - echo '
404 MSQ not found.
'; } } catch (PDOException $e) diff --git a/src/download.php b/src/download.php index 6a034e6..47a7fc7 100644 --- a/src/download.php +++ b/src/download.php @@ -18,11 +18,22 @@ require "msqur.php"; if (isset($_GET['msq'])) { - header('Content-Type: application/xml'); - header('Content-Disposition: attachment; filename="' . $_GET['msq'] . '.msq"'); - header('Pragma: no-cache'); + $id = $_GET['msq']; //TODO Sanitize - echo $msqur->getMSQForDownload($_GET['msq']); + $xml = $msqur->getMSQForDownload($id); + + if ($xml) { + header('Content-Type: application/xml'); + header('Content-Disposition: attachment; filename="' . $id . '.msq"'); + header('Pragma: no-cache'); + echo trim($xml); //`trim` is a workaround for #30 + } else { + http_response_code(404); + unset($_GET['msq']); + include "view/header.php"; + echo '
404 MSQ file not found.
'; + include "view/footer.php"; + } } else { include "index.php"; } diff --git a/src/msq.php b/src/msq.php index 3b2b97d..89d465b 100644 --- a/src/msq.php +++ b/src/msq.php @@ -27,7 +27,7 @@ class MSQ /** * @brief Format a constant to HTML * @param $constant The constant name - * @param $value It's value + * @param $value Its value * @returns String HTML \ */ private function msqConstant($constant, $value, $help) @@ -35,7 +35,7 @@ private function msqConstant($constant, $value, $help) //var_export($constant); //var_export($value); //var_export($help); - return "
$constant: $value
"; + return "$constant:$value"; } /** @@ -60,10 +60,9 @@ public function parseMSQ($xml, &$engine, &$metadata) error($error->message); } - $html['header'] = '
Unable to parse MSQ.
'; + throw new MSQ_ParseException("Error parsing XML", '
Unable to parse MSQ.
'); } else if ($msq) { $msqHeader = '
'; - $msqHeader .= '
💾
'; $msqHeader .= "
Format Version: " . $msq->versionInfo['fileFormat'] . "
"; $msqHeader .= "
MS Signature: " . $msq->versionInfo['signature'] . "
"; $msqHeader .= "
Tuning SW: " . $msq->bibliography['author'] . "
"; @@ -101,8 +100,16 @@ public function parseMSQ($xml, &$engine, &$metadata) $engineSchema = getEngineSchema(); - $html["precurves"] = '
2D Tables (Curves)
'; - $html["curves"] = ""; + $html["tabList"] = << + + EOT; + + $html["tabList"] .= '
'; foreach ($curves as $curve) { if (in_array($curve['id'], $this->msq_curve_blacklist)) @@ -129,13 +136,13 @@ public function parseMSQ($xml, &$engine, &$metadata) $yBins = $this->findConstant($msq, $curve['yBinConstant']); $xAxis = preg_split("/\s+/", trim($xBins)); $yAxis = preg_split("/\s+/", trim($yBins)); - $html["curves"] .= $this->msqTable2D($curve, $curve['xMin'], $curve['xMax'], $xAxis, $curve['yMin'], $curve['yMax'], $yAxis, $help); + $html["tabList"] .= $this->msqTable2D($curve, $curve['xMin'], $curve['xMax'], $xAxis, $curve['yMin'], $curve['yMax'], $yAxis, $help); } else if (DEBUG) debug('Missing/unsupported curve information: ' . $curve['id']); } + $html["tabList"] .= '
'; - $html["pretables"] = '
3D Tables
';; - $html["tables"] = ""; + $html["tabList"] .= '
'; foreach ($tables as $table) { if (DEBUG) debug('Table: ' . $table['id']); @@ -154,13 +161,13 @@ public function parseMSQ($xml, &$engine, &$metadata) $xAxis = preg_split("/\s+/", trim($xBins)); $yAxis = preg_split("/\s+/", trim($yBins)); $zData = preg_split("/\s+/", trim($zBins));//, PREG_SPLIT_NO_EMPTY); //, $limit); - $html["tables"] .= $this->msqTable3D($table, $xAxis, $yAxis, $zData, $help); + $html["tabList"] .= $this->msqTable3D($table, $xAxis, $yAxis, $zData, $help); } else if (DEBUG) debug('Missing/unsupported table information: ' . $table['id']); } + $html["tabList"] .= '
'; - $html["preconstants"] = '
Constants
'; - $html["constants"] = ""; + $html["tabList"] .= '
'; foreach ($constants as $key => $config) { if ($config[0] == "array") continue; //TODO Skip arrays until blacklist is done @@ -180,9 +187,12 @@ public function parseMSQ($xml, &$engine, &$metadata) if (array_key_exists($key, $helpTexts)) $help = $helpTexts[$key]; - $html["constants"] .= $this->msqConstant($key, $value, $help); + $html["tabList"] .= $this->msqConstant($key, $value, $help); } } + $html["tabList"] .= '
'; + $html["tabList"] .= '
'; + } return $html; diff --git a/src/msqur.php b/src/msqur.php index 2d2c8fb..7632678 100644 --- a/src/msqur.php +++ b/src/msqur.php @@ -45,8 +45,7 @@ public function getCachedMSQ($id) public function getMSQForDownload($id) { - - return $this->db->getMSQForDownload($id); + return $this->db->getXML($id); } public function addMSQs($files, $engineid) @@ -141,16 +140,15 @@ public function getEngineCodeList($make = null) */ public function view($id) { - $this->header(); if (DEBUG) debug('Load MSQ: ' . $id); //Get cached HTML and display it, or reparse and display (in order) $html = $this->getCachedMSQ($id); - if ($html !== null) + if ($html !== FALSE) { $this->db->updateViews($id); $msq = new MSQ(); //ugh - if ($html == FALSE) + if ($html == null) { $engine = array(); $metadata = array(); @@ -165,22 +163,22 @@ public function view($id) foreach($groupedHtml as $group => $v) { //TODO Group name as fieldset legend or sth - $html .= "
"; + //$html .= "
"; $html .= $v; - $html .= '
'; + //$html .= '
'; } $this->db->updateCache($id, $html); } catch (MSQ_ParseException $e) { $html = $e->getHTMLMessage(); - } + } finally { + return $html; + } } } } - //TODO else show 404 - - echo $html; - $this->footer(); + + return null; } public function addEngine($make, $code, $displacement, $compression, $turbo) diff --git a/src/upload.php b/src/upload.php index 0eb3f24..e79758d 100644 --- a/src/upload.php +++ b/src/upload.php @@ -68,7 +68,7 @@ function checkUploads($files) //Get and check mime types (ignoring provided ones) $finfo = new finfo(FILEINFO_MIME_TYPE); $mimeType = $finfo->file($file['tmp_name']); - if ($mimeType != "application/xml" && $mimeType != "text/xml") + if (!acceptableMimeType($mimeType)) { if (DEBUG) warn('File: ' . $file['tmp_name'] . ': Invalid MIME type ' . $mimeType); unset($files[$index]); @@ -79,6 +79,22 @@ function checkUploads($files) return $files; } +/** + * @brief Check that a mime type matches ones we think are OK. + * @param $mimeType {string} MIME type + * @returns true or false + */ +function acceptableMimeType($mimeType) { + switch ($mimeType) { + case "application/xml": + case "text/xml": + case "text/plain": + return true; + default: + return FALSE; + } +} + //var_export($_POST); //var_export($_FILES); diff --git a/src/view.php b/src/view.php index 58561a9..997c275 100644 --- a/src/view.php +++ b/src/view.php @@ -18,7 +18,20 @@ require "msqur.php"; if (isset($_GET['msq'])) { - echo $msqur->view($_GET['msq']); + $id = $_GET['msq']; //TODO Sanitize + $html = $msqur->view($_GET['msq']); + if ($html !== null) { + include "view/header.php"; + echo $html; + include "view/footer.php"; + } else { + http_response_code(404); + unset($_GET['msq']); + include "view/header.php"; + echo '
404 MSQ file not found.
'; + include "view/footer.php"; + } +} else { + include "index.php"; } -else include "index.php"; ?> diff --git a/src/view/header.php b/src/view/header.php index 82eb8d3..f3c2516 100644 --- a/src/view/header.php +++ b/src/view/header.php @@ -8,11 +8,12 @@ + - + - + @@ -27,7 +28,6 @@ - @@ -97,6 +97,7 @@ + \ No newline at end of file diff --git a/src/view/msqur.css b/src/view/msqur.css index 523df5c..5de327f 100644 --- a/src/view/msqur.css +++ b/src/view/msqur.css @@ -82,9 +82,17 @@ div#splash img.tutorial { div#settings { z-index: 999; background: rgba(224, 224, 224, 32); - position: absolute; + float: right; margin-top: 4px; - right: 8px; +} + +img#settingsIcon { + cursor: pointer; +} + +div#downloadLink { + margin: 5px; + float: right; } #fileDropZone { @@ -110,16 +118,6 @@ table caption { padding: 0.2em; } -div[class^="group-"] { - border: 4px solid #F0F0F0; -} - -div.group-header { - font-size: 3em; - text-align: center; - font-style: bold; -} - table.msq td:hover { background-color: magenta !important; } @@ -157,6 +155,28 @@ div.chart { display: inline; } +/* msqur.css | http://localhost:5050/view/msqur.css */ + +div#tab_constants { + display: flex; + flex-flow: column wrap; + max-height: 900px; + overflow: auto; + max-width: 100%; +} + +span.constant:nth-child(even) { + background-color: #e4e4e4; +} + +span.constant { + margin: 0px 10px; +} + +span.constant span.value { + float: right; +} + #engineForm { display: table; } diff --git a/src/view/msqur.js b/src/view/msqur.js index cdab480..b006cde 100644 --- a/src/view/msqur.js +++ b/src/view/msqur.js @@ -70,10 +70,12 @@ $(function() { heightStyle: "content", activate: doChart }; + + $('div#tabList').tabs(); - $('div.group-curves').accordion(accordionOptions); - $('div.group-tables').accordion(accordionOptions); - $('div.constant').tooltip(); + $('div#tab_tables').accordion(accordionOptions); + $('div#tab_curves').accordion(accordionOptions); + $('div#tab_constants').tooltip(); Chart.defaults.global.animation = false; //Chart.defaults.global.responsive = true;