diff --git a/pages/run.php b/pages/run.php index 6f0e832..79d3842 100644 --- a/pages/run.php +++ b/pages/run.php @@ -43,17 +43,52 @@ catch (Exception $x) {} } + // WARNING: Can be abused. + // Translate occurrences of {{ SQL: select 'test' }} into the string value of the first cell of the first row: + // Can be used in string literals or default values in comments! + preg_match_all( + "/\{\{\s*SQL\:\s*(.*)\s+?\}\}/i", + $strFileData, + $varSQLReplacements); + + for ($i = 0; $i < count($varSQLReplacements[0]); $i++) + { + try + { + $strMatch = $varSQLReplacements[0][$i]; + $strMatchSafe = preg_quote($strMatch); + $strSQLString = $varSQLReplacements[1][$i]; + $varTempRows = $c->query($strSQLString); + + //Respond::json($varTempRows); + + $strFirstCell = ""; + if (count($varTempRows) > 0) + foreach ($varTempRows[0] as $k => $v) + { + $strFirstCell = $varTempRows[0][$k]; + break; + } + + $strFileData = preg_replace( + "/{$strMatchSafe}/i", + $strFirstCell, + $strFileData); + } + catch (Exception $x) {} + } + + //Respond::json($strFileData); + //foreach ($varReplacements as $k => $v) // $strFileData = preg_replace("/\{\{{$k}\}\}/i", $v, $strFileData); // Get the inputs: preg_match_all( - "/declare\s+\@([A-Za-z0-9]{1,})\s+(.+)\s+=\s+\?;(\s+-- Default\:\s(.*))?/i", + "/declare\s+\@([A-Za-z0-9]{1,})\s+(.+)\s+=\s+\?;(\s+-- ([A-Za-z0-9]{1,})\:\s(.*))?/i", $strFileData, $varInputs); - //Respond::json($varInputs); - // Get the options defined in comments: preg_match_all( "/--\s+([A-Za-z0-9]{1,})\:\s+(.+)/i", @@ -95,19 +130,28 @@ $intAllowed = 1; } - $intRun = Request::getParam("run"); + $intRun = Request::getParam("run"); + $strFormat = Request::getParam("format"); //if (Request::posts("run_query")) if ($intRun !== null && $intRun == 1 && $intAllowed == 1) { $varPosted = Request::getParams(); - unset($varPosted["run"]); + + // Remove these keys from submitted fields: + foreach (["run", "format"] as $k) + if (array_key_exists($k, $varPosted)) + unset($varPosted[$k]); try { $varRows = $c->query("{$strQueryDir}/{$strSelection}", $varPosted); $strView = "files/views/{$strSelection}.php"; + // Allow returning the data as JSON for APIs, maybe: + if (strtolower($strFormat) == "json") + Respond::json($varRows); + // Load a custom view if there is one: if (file_exists($strView)) { @@ -249,8 +293,16 @@ $strInputTypeHTML = "date"; $strInputDefaultValue = ""; - if (strlen($varInputs[4][$i]) > 0) - $strInputDefaultValue = $varInputs[4][$i]; + if (strtolower($varInputs[4][$i]) == "default") + if (strlen($varInputs[5][$i]) > 0) + $strInputDefaultValue = $varInputs[5][$i]; + + $varOptions = []; + if (strtolower($varInputs[4][$i]) == "options") + { + $strOptionsList = $varInputs[5][$i]; + $varOptions = explode(",", $strOptionsList); + } ?>