Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
bruneo32 authored Mar 15, 2021
1 parent d3e18f0 commit bab3301
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 24 deletions.
1 change: 1 addition & 0 deletions doc/connect_remote.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ol{
text-align: left;
width: fit-content;
width: -moz-fit-content;
margin:0 auto;
}
ul li{list-style-position: inside;}
Expand Down
1 change: 1 addition & 0 deletions doc/localhost.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ol,ul{
text-align: left;
width: fit-content;
width: -moz-fit-content;
margin:0 auto;
}
ul li{list-style-position: inside;}
Expand Down
2 changes: 1 addition & 1 deletion licenses.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</head>
<body>
<img style="margin-top:-16px;" src="res/banner.jpg" alt=""><br>
<a target="_blank" href="https://github.com/bruneo32/phpmiami"><img src="https://img.shields.io/badge/version-0.9-tomato.svg" alt=""></a>
<a target="_blank" href="https://github.com/bruneo32/phpmiami"><img src="https://img.shields.io/badge/version-1.0-cornflowerblue.svg" alt=""></a>
<br>
<br>

Expand Down
4 changes: 2 additions & 2 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<script src="codemirror/sql-hint.js"></script>
<script src="codemirror/matchbrackets.js"></script>
</head>
<body onload="InstantiateCard()">
<body onload="InstantiateCard();">
<div id="topbar">
<img src="res/icon.jpg" alt="" onclick="ScrollBottom()" title="Scroll bottom">
<img src="res/banner.jpg" alt="" onclick="ScrollBottom()" title="Scroll bottom">
<span style="color:var(--aux2);">DB: <?php echo $_SESSION["db"]; ?></span>

<hr>
Expand Down
9 changes: 4 additions & 5 deletions php/100.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
* Login
*/

$LOCALHOST=(($_SERVER["SERVER_ADDR"]=="::1" || $_SERVER["SERVER_ADDR"]=="127.0.0.1") ? true:false); // Turn true to allow localhost connections.
//$LOCALHOST=true; // Enable this line and comment the previous one to allow localhost access.

error_reporting(E_ALL ^ E_WARNING); // Supress warnings, comment to debug.
require("config.php");
if(!$DEBUG_MODE){error_reporting(E_ALL ^ E_WARNING);} // Supress warnings
session_start();
require("conn.php");

$h=$_REQUEST["h"];
$hn=$h;

if(!$LOCALHOST){
if(!$_CONFIG["allow_localhost"]){
if($h == "localhost"){die("For security reasons, conections to 'localhost' are bloqued. Learn more about localhost <a href=\"\">here</a>.<br><br><a href='../login'>Return</a>");}
}
$u=$_REQUEST["u"];
Expand All @@ -31,6 +30,6 @@
die("<b>[System]</b> Conection failed - ".mysqli_connect_errno()."<br>".mysqli_connect_error()."<br><br><a href='../login'>Return</a>");
}

//header('Location: ../main');
//header('Location: ../main'); // This won't work on some servers
die('<meta http-equiv="refresh" content="0; URL=../main">');
?>
4 changes: 3 additions & 1 deletion php/101.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* Used by AJAX JavaScript to execute an SQL query and return its result.
*/

error_reporting(E_ALL ^ E_WARNING); // Supress warnings, comment to debug.

require("config.php");
if(!$DEBUG_MODE){error_reporting(E_ALL ^ E_WARNING);} // Supress warnings
session_start();
require("conn.php");

Expand Down
3 changes: 2 additions & 1 deletion php/102.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* Log out
*/


session_start();

session_unset();

//header('Location: ../login');
//header('Location: ../login'); // This won't work on some servers
die('<meta http-equiv="refresh" content="0; URL=../login">');
?>
4 changes: 3 additions & 1 deletion php/103.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* Returns the hintOptions[table] for the code editor using AJAX.
*/

error_reporting(E_ALL ^ E_WARNING); // Supress warnings, comment to debug.

require("config.php");
if(!$DEBUG_MODE){error_reporting(E_ALL ^ E_WARNING);} // Supress warnings
session_start();
require("conn.php");

Expand Down
14 changes: 14 additions & 0 deletions php/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/*
* config.php
* Basic configuration of phpmiami server side
*/


$DEBUG_MODE=false;

$_CONFIG["allow_localhost"]=(($_SERVER["SERVER_ADDR"]=="::1" || $_SERVER["SERVER_ADDR"]=="127.0.0.1") ? true:false);
/* Enable the following line to allow localhost access. */
//$_CONFIG["allow_localhost"]=true;

?>
111 changes: 101 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var cardHTML = '<div class="card"><table><tr><td>SQL</td><td><textarea></textarea><br><input type="button" style="float:right;" title="CTRL+ENTER" class="button" value="Run query" onclick="RunQuery()"/></td></tr><tr class="result_query"><td>Result</td><td></td></tr><tr class="result_query"><td onclick="SelectNext(this,1)">Raw result</td><td></td></tr></table></div>';
var cardHTML = '<div class="card"><table><tr><td>SQL</td><td><textarea></textarea><br><select class="button" title="Copy a snippet to clipboard" onchange="CopySnippet(this)"></select><input type="button" style="float:right;" title="CTRL+ENTER" class="button" value="Run query" onclick="RunQuery()"/></td></tr><tr class="result_query"><td>Result</td><td></td></tr><tr class="result_query"><td onclick="SelectNext(this,1)">Raw result</td><td></td></tr></table></div>';
var defaults = {
lineNumbers: true,
matchBrackets: true,
Expand All @@ -12,11 +12,16 @@ var defaults = {
lineWrapping: true,
hintOptions: {}
};
var snippets=[];
var snippetsHTML="";
var G_TEXTAREA=null;

function CopyBtn(btn_elem){
var textarea = btn_elem.parentElement.children[0];
navigator.clipboard.writeText(textarea.value);
copyTextToClipboard(textarea.value);
}
function CopySnippet(this_elm){
copyTextToClipboard(snippets[this_elm.value][1]);
}
function SelectNext(btn_elem,ind){
var elm = btn_elem.parentElement.children[ind];
Expand All @@ -30,6 +35,7 @@ function RunQuery(){
G_TEXTAREA.setAttribute("readonly","true");
table.parentElement.style.backgroundColor="var(--color2)";
G_TEXTAREA.parentElement.lastChild.setAttribute("onclick","CopyBtn(this)");
G_TEXTAREA.parentElement.lastChild.previousSibling.setAttribute("disabled","true");
G_TEXTAREA.parentElement.lastChild.value="Copy";

ExecQuery(val,table.children[1].children[1],table.children[2].children[1]);
Expand All @@ -44,14 +50,29 @@ function InstantiateCard(){

G_TEXTAREA = document.querySelector(".card:last-child textarea");
window.editor = CodeMirror.fromTextArea(G_TEXTAREA, defaults);
ScrollBottom();

if(snippetsHTML==""){ // Never loaded snippets.txt
G_TEXTAREA.parentElement.lastChild.previousSibling.style.display="none";
GetSnippets();

// Wait 100 milliseconds.
setTimeout(function () {
if(snippetsHTML!=""){
G_TEXTAREA.parentElement.lastChild.previousSibling.innerHTML = snippetsHTML;
G_TEXTAREA.parentElement.lastChild.previousSibling.style.display="inline";
}
}, 100);
}else{
G_TEXTAREA.parentElement.lastChild.previousSibling.innerHTML = snippetsHTML;
}

AutocompletChanged();
GetTableHint();
ScrollBottom();
}

function ExecQuery(sqlstr,resulte,rawr){
console.log(sqlstr);
console.debug("SQL: "+sqlstr);
if (sqlstr.length == 0) {
InstantiateCard();
} else {
Expand All @@ -62,7 +83,7 @@ function ExecQuery(sqlstr,resulte,rawr){
if (this.readyState == 4 && this.status == 200) {

var res=this.responseText;
console.log(res);
console.debug("RAW: "+res);
rawr.innerHTML=escapeHtml(res);

resulte.parentElement.style.display="table-row";
Expand All @@ -75,11 +96,10 @@ function ExecQuery(sqlstr,resulte,rawr){
InstantiateCard();
return;
}
console.log(resarray);
console.debug("Parsed: "+resarray);

var pres="";
for (var i = 0; i < resarray.length; i++) {
console.log(typeof resarray[i]);
if(typeof resarray[i]==="boolean" || resarray[i].length==0){
pres+="<p><i>No result.</i></p>";
}else if(typeof resarray[i] !== "object"){
Expand Down Expand Up @@ -125,9 +145,8 @@ function AutocompletChanged(){
if(document.getElementById("totalCompletion").checked){
window.editor.on("keyup", function (cm, event) {
var cr=cm.getCursor();
var pr_char=cm.getLine(cr["line"]).charCodeAt([cr["ch"]]-1); // Character before cursor.

if(pr_char===undefined || pr_char<48 || pr_char===59){return false;} // Not autocomplete if its <"0", OR ; " '
var pr_char=cm.getLine(cr["line"]).charAt([cr["ch"]]-1); // Character before cursor.
if(pr_char===undefined || (pr_char!="." && !(/^[a-zA-Z0-9]+$/).test(pr_char))){return false;} // Not autocomplete if its not alfanumeric.

if (!cm.state.completionActive && event.keyCode != 13) {
CodeMirror.commands.autocomplete(cm, null, {completeSingle: false});
Expand Down Expand Up @@ -157,6 +176,51 @@ function GetTableHint(){
}
}
}
function GetSnippets(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var res=this.responseText;
res=res.split("\n");

for (var i = 0; i < res.length; i++) {
if(res[i].toString().trim()==="---"){continue;}

res[i]=res[i].split("::");
if(res[i].length<2){
res.splice(i, 1);
continue;
}

res[i][1]=res[i][1].replace(/\\n/g,"\n").replace(/\\t/g,"\t");
}

snippets=res;

snippetsHTML='<option selected disabled value="1000">Copy snippet to clipboard</option>';
var groupclosing=false;
for (var i = 0; i < snippets.length; i++) {
if(snippets[i].toString().trim()==="---"){
snippetsHTML+='<option class="separator" disabled="true"></option>';
continue;
}else if(snippets[i][0]===""){
if(groupclosing){snippetsHTML+="<br/></optgroup>";}

snippetsHTML+='<option class="separator" disabled="true"></option>';
snippetsHTML+='<optgroup label="'+snippets[i][1]+'">'
groupclosing=true;
continue;
}

snippetsHTML+='<option value="'+i.toString()+'">'+snippets[i][0]+"</option>";
}
if(groupclosing){snippetsHTML+="<br/></optgroup><br/>";}
snippetsHTML+='<option class="separator" disabled="true"></option>';
}
};
xmlhttp.open("GET", "snippets.txt", true);
xmlhttp.send();
}


/* EXTRAS */
Expand Down Expand Up @@ -187,3 +251,30 @@ function escapeHtml(text) {

return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
var textArea = document.createElement("textarea");
textArea.value = text;

// Avoid scrolling to bottom
textArea.style.position = "fixed";
textArea.style.top = "0";
textArea.style.left = "0";

document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
document.execCommand('copy');
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}

document.body.removeChild(textArea);
return;
}
navigator.clipboard.writeText(text).then(function() {}, function(err) {
console.error('Async: Could not copy text. ', err);
});
}
28 changes: 28 additions & 0 deletions snippets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
::General
Select *::SELECT * FROM table_name;
Select::SELECT column1, column2, columnN\nFROM table_name\nWHERE condition;
Update::UPDATE table_name\nSET column1=value, column2=value, columnN=value\nWHERE condition;
Insert into::INSERT INTO table_name (column1,column2,columnN)\nVALUES (value1,value2,valueN);
Delete::DELETE FROM table_name\nWHERE 0; -- Change this
::Database
Show databases::SHOW DATABASES;
Create database::CREATE DATABASE database_name;
Drop database::DROP DATABASE database_name;
::Table
Show tables::SHOW TABLES;
Create table::CREATE TABLE table_name (\n\tid int PRIMARY KEY,\n\tcolumn2 datatype,\n\tcolumn3 datatype,\n\tcolumnN datatype\n);
Drop table::DROP TABLE table_name;
Truncate table::TRUNCATE TABLE table_name;
---
Show columns::SELECT ORDINAL_POSITION AS "N", COLUMN_NAME AS "Column Name", COLUMN_TYPE AS "Datatype", COLUMN_KEY AS "Key", EXTRA\nFROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'table_name';
Add column::ALTER TABLE table_name\nADD column_name datatype;
Drop column::ALTER TABLE table_name\nDROP COLUMN column_name;
Modify column::ALTER TABLE table_name\nMODIFY COLUMN column_name datatype;
::Users
Show users::SELECT User, Host, CASE\n WHEN Password="" THEN "NO" ELSE "YES"\nEND AS "Using password" FROM mysql.user;
Create user::CREATE USER 'username'@'localhost' IDENTIFIED BY 'user_password'; -- This user will not have any privileges. Use GRANT to assign them.
Grant privileges::GRANT INSERT, UPDATE, DELETE, ... /* For all privileges use: ALL PRIVILEGES */\nON table_name\nTO username@localhost;
Drop user::DROP USER 'username'@'localhost';
::Misc
Set autoincrement::ALTER TABLE table_name AUTO_INCREMENT=100;
CASE Statement::CASE\n\tWHEN condition1 THEN result1\n\tWHEN condition2 THEN result2\n\tWHEN conditionN THEN resultN\n\tELSE result\nEND
20 changes: 17 additions & 3 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ tt{
background-color:rgba(0,0,0,0.05);
border-radius: 3px;
}
optgroup{font-weight: bold;font-style: normal;}
select option[disabled]{
font-weight: bold;
color:red;
}
select option.separator{
font-size: 0.25em;
}

.center{text-align: center;}
.mono{font-family: 'Roboto',monospace;}
Expand All @@ -77,12 +85,16 @@ input[type="text"],input[type="password"]{
}
.button{
font-family: sans-serif;
padding:6px 12px;
padding:0.5em 1em;
cursor:pointer;
}
.button.little{
font-size:10px;
}

.tip{
border-bottom: 1px dotted var(--aux2);
font-family: monospace;
font-family: 'Roboto',monospace;
}

#top{height:32px;}
Expand All @@ -102,9 +114,10 @@ input[type="text"],input[type="password"]{
margin-right: 6px;
vertical-align: middle;
}
#topbar hr{cursor:default;}
#topbar span{
cursor: text;
font-family: monospace;
font-family: 'Roboto',monospace;
margin-right: 12px;
user-select: text;
}
Expand All @@ -131,6 +144,7 @@ input[type="text"],input[type="password"]{
background-color: var(--color44);
box-shadow: 0 9px 20px 0px #000;
margin:16px;
overflow-x: auto;
}
.card table{
width:100%;
Expand Down

0 comments on commit bab3301

Please sign in to comment.