Skip to content

Commit

Permalink
v1.61 (#47)
Browse files Browse the repository at this point in the history
* rm angular-resource

* Fix some opengraph tags

* Fix uploading in FF 71

* Cleanup downloads

* Fix debug messages

* Fix download link

* Move sections to jQuery tabs

* Style constants slightly better

* Fix header download link

* Add real 404
  • Loading branch information
nearwood authored Nov 8, 2019
1 parent 83bc28b commit 05e95c3
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 95 deletions.
52 changes: 7 additions & 45 deletions src/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
{
Expand All @@ -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 '<div class="error">Invalid MSQ</div>';
return null;
return FALSE;
}
}
catch (PDOException $e)
Expand All @@ -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 "<div class=\"debug\">No result for $id</div>";
echo '<div class="error">Invalid MSQ err 2</div>';
return null;
}
}
catch (PDOException $e)
{
$this->dbError($e);
}

return $xml;
}

/**
* @brief Get a list of MSQs
* @param $bq The BrowseQuery to filter results
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 '<div class="error">404 MSQ not found.</div>';
}
}
catch (PDOException $e)
Expand Down
19 changes: 15 additions & 4 deletions src/download.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<div class="error">404 MSQ file not found. </div>';
include "view/footer.php";
}
} else {
include "index.php";
}
Expand Down
36 changes: 23 additions & 13 deletions src/msq.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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 \<div\>
*/
private function msqConstant($constant, $value, $help)
{
//var_export($constant);
//var_export($value);
//var_export($help);
return "<div class=\"constant\" title=\"$help\"><b>$constant</b>: $value</div>";
return "<span class=\"constant\" title=\"$help\"><b>$constant:</b><span class=\"value\">$value</span></span>";
}

/**
Expand All @@ -60,10 +60,9 @@ public function parseMSQ($xml, &$engine, &$metadata)
error($error->message);
}

$html['header'] = '<div class="error">Unable to parse MSQ.</div>';
throw new MSQ_ParseException("Error parsing XML", '<div class="error">Unable to parse MSQ.</div>');
} else if ($msq) {
$msqHeader = '<div class="info">';
$msqHeader .= '<div style="float: right;"><a title="Download MSQ File" href="download.php?msq=' . $_GET['msq'] . '">💾</a></div>';
$msqHeader .= "<div>Format Version: " . $msq->versionInfo['fileFormat'] . "</div>";
$msqHeader .= "<div>MS Signature: " . $msq->versionInfo['signature'] . "</div>";
$msqHeader .= "<div>Tuning SW: " . $msq->bibliography['author'] . "</div>";
Expand Down Expand Up @@ -101,8 +100,16 @@ public function parseMSQ($xml, &$engine, &$metadata)

$engineSchema = getEngineSchema();

$html["precurves"] = '<div class="group-header">2D Tables (Curves)</div>';
$html["curves"] = "";
$html["tabList"] = <<<EOT
<div id="tabList">
<ul>
<li><a href="#tab_tables">3D Tables</a></li>
<li><a href="#tab_curves">2D Tables (Curves)</a></li>
<li><a href="#tab_constants">Constants</a></li>
</ul>
EOT;

$html["tabList"] .= '<div id="tab_curves">';
foreach ($curves as $curve)
{
if (in_array($curve['id'], $this->msq_curve_blacklist))
Expand All @@ -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"] .= '</div>';

$html["pretables"] = '<div class="group-header">3D Tables</div>';;
$html["tables"] = "";
$html["tabList"] .= '<div id="tab_tables">';
foreach ($tables as $table)
{
if (DEBUG) debug('Table: ' . $table['id']);
Expand All @@ -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"] .= '</div>';

$html["preconstants"] = '<div class="group-header">Constants</div>';
$html["constants"] = "";
$html["tabList"] .= '<div id="tab_constants">';
foreach ($constants as $key => $config)
{
if ($config[0] == "array") continue; //TODO Skip arrays until blacklist is done
Expand All @@ -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"] .= '</div>';
$html["tabList"] .= '</div>';

}

return $html;
Expand Down
22 changes: 10 additions & 12 deletions src/msqur.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -165,22 +163,22 @@ public function view($id)
foreach($groupedHtml as $group => $v)
{
//TODO Group name as fieldset legend or sth
$html .= "<div class=\"group-$group\">";
//$html .= "<div class=\"group-$group\">";
$html .= $v;
$html .= '</div>';
//$html .= '</div>';
}

$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)
Expand Down
18 changes: 17 additions & 1 deletion src/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);

Expand Down
17 changes: 15 additions & 2 deletions src/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<div class="error">404 MSQ file not found.</div>';
include "view/footer.php";
}
} else {
include "index.php";
}
else include "index.php";
?>
7 changes: 4 additions & 3 deletions src/view/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
<link rel="icon" type="image/x-icon" href="view/img/favicon.ico">
<link rel="stylesheet" href="view/msqur.css" />
<!-- Open Graph data -->
<meta property="fb:admins" content="127238424281477"/>
<meta property="og:title" content="msqur" />
<meta property="og:type" content="page" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://msqur.com/" />
<meta property="og:image" content="https://msqur.com/view/img/tutorial5.png" />
<meta property="og:description" content="Your Description Here" />
<meta property="og:description" content="Megasquirt tune (msq) sharing site" />
<meta property="og:site_name" content="msqur" />
<!-- Twitter Card data -->
<meta name="twitter:card" content="summary">
Expand All @@ -27,7 +28,6 @@
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" integrity="sha384-5L1Zwk1YapN1l4l4rYc+1fr3Z0g23LbCBztpq0LQcbDCelzqgFb96BMCFtDwjq/b" crossorigin="anonymous">
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js" integrity="sha384-ovZOciNc/R4uUo2fCVS1oDT0vIBuaou1d39yqL4a9xFdZAYDswCgrJ6tF8ShkqzF" crossorigin="anonymous"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js" integrity="sha384-neqWoCEBO5KsP6TEzfMryfZUeh7+qMQEODngh2KGzau+pMU9csLE2azsvQFa8Oel" crossorigin="anonymous"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular-resource.min.js" integrity="sha384-fQqYlHIz3plgWo88gnyC2Z5EKOTk53w3z102UGn+OnE17oQeCa4h+u0PPijIpklj" crossorigin="anonymous"></script>
<!-- Hosted scripts -->
<script src="view/lib/tablesorter/jquery.tablesorter.min.js"></script>
<script src="view/lib/Chart.js/Chart.min.js"></script>
Expand Down Expand Up @@ -97,6 +97,7 @@
<label><input id="normalizeAxis" type="checkbox" disabled />Normalize Axis</label>
</div>
</div>
<div id="downloadLink"><a title="Download MSQ File" href="download.php?msq=<?php echo $_GET['msq']; ?>">💾 Download MSQ</a></div>
<?php
}
?>
Loading

0 comments on commit 05e95c3

Please sign in to comment.