Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
XHiddenProjects authored Apr 10, 2023
1 parent 7cee0db commit fc70cc9
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 35 deletions.
19 changes: 4 additions & 15 deletions SSQL/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,10 @@
<body>
<?php
if($ssql->setCredential('localhost', 'root', '')){
if(!$ssql->resetDB('myDB'))
$ssql->makeDB('myDB');
$db = $ssql->selectDB('myDB');
($db->checkTable('myTable') ? $db->dropTable('myTable') : '');
$db->makeTable('myTable', ['id', 'firstname', 'lastname', 'email', 'reg_date'], ['int(6)', '', '', 'VARCHAR(50)', 'TIMESTAMP'], ['UNSIGNED', '', '', '', 'CURRENT_TIMESTAMP'], ['AUTO_INCREMENT PRIMARY KEY', 'NOT NULL', 'NOT NULL', '', 'ON UPDATE CURRENT_TIMESTAMP']);
$db->addData('myTable', ['firstname','lastname','email'],[['John','Doe','john@example.com'],['Fred','Bang','fred@example.com'],['Greg','barns','greg@example.com']]);
$db->selectData('myTable',['*']);
$db->dropData('myTable', 'id=2');
$db->updateData('myTable', 'lastname="Doe"', 'id=3');
echo $db->makeView('myView','myTable',['firstname','lastname','email']);
$db->dropView('myView');
$ssql->makeUser('test', $ssql->genPsw('psw'));
$ssql->givePerm(['ALL'], '*', ['test']);
$ssql->dropPerm(['ALL'], '*', ['test']);
$ssql->dropUser('test');
if(!$ssql->resetDB('StoreData'))
$ssql->makeDB('StoreData');
$db = $ssql->selectDB('StoreData');
$db->makeTable('customers', ['id', 'first_name','last_name', 'email', 'phone'],['int(6)', '', '', 'varchar(50)','bigint'],['UNSIGNED','','','',''],['AUTO_INCREMENT PRIMARY KEY','NOT NULL','NOT NULL', '','']);
$ssql->close();
}
?>
Expand Down
162 changes: 142 additions & 20 deletions SSQL/lib/ssql.lib.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php
/*
--------info--------
@Name: SSQL
@Author: SurveyBuilderTeams
@Version:0.0.3
@SQL: mySQL(phpMyAdmin) v5.2
@Server type: MariaDB v10.4
@Language: PHP v8.2
--------info--------
*/
mysqli_report(MYSQLI_REPORT_OFF);
class SSQL{
protected $server;
Expand Down Expand Up @@ -60,6 +70,21 @@ function style($darkmode=true){
$out.='</style>';
return $out;
}
# others
function genPsw($salt='') {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-_+=';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return $salt.implode($pass); //turn the array into a string
}
function isNoQuote($input){
return (gettype($input)==='integer'||gettype($input)==='double'||gettype($input)==='NULL' ? ($input===NULL ? 'NULL' : $input) : '"'.$input.'"');
}

function setCredential(string $s, string $u, string $p) : bool{
$this->server = filter_var($s,FILTER_SANITIZE_STRING);
$this->name = filter_var($u, FILTER_SANITIZE_STRING);
Expand Down Expand Up @@ -167,17 +192,17 @@ function addData(string $tbname, array $data, array $values) : bool{
return false;
}
}
function selectData(string $tbname, array $sel, string $condition='') : array{
function selectData(string $tbname, array $sel, string $condition='', array $args=[], $returnArr=true) : array|string{
$row=[];
$sql = 'SELECT '.implode(',',$sel).' FROM '.strtolower($tbname).($condition!=='' ? ' '.$condition : '');
$sql = 'SELECT '.implode(',',$sel).' FROM '.strtolower($tbname).($condition!=='' ? ' '.$condition : '').' '.implode(' ',$args);
$results = $this->conn->query($sql);
if($results->num_rows > 0){
if($results !== false&&$results->num_rows > 0){
while($rows = $results->fetch_assoc()){
$row[] = $rows;
}
return $row;
return $returnArr ? $row : $sql;
}else{
die('Error: '.$this->conn->error);
die(($this->conn->error ? 'Error: '.$this->conn->error : 'No Results'));
return $row;
}
}
Expand All @@ -201,7 +226,7 @@ function updateData(string $tbname, string $replacement, string $condition='') :
}
# permissions
function givePerm(array $perm, string $tbname, array $username) : bool{
$sql = 'GRANT '.implode(',',$perm).' ON '.$this->db.'.'.strtolower($tbname).' TO '.implode(',',array_map(function($i){return $i.'@'.$this->server;},$username));
$sql = 'GRANT '.implode(',',$perm).' ON '.strtolower($tbname).' TO '.implode(',',array_map(function($i){return $i.'@'.$this->server;},$username));
if($this->conn->query($sql)){
return true;
}else{
Expand All @@ -210,7 +235,7 @@ function givePerm(array $perm, string $tbname, array $username) : bool{
}
}
function dropPerm(array $perm,string $tbname, array $username) : bool{
$sql = 'REVOKE '.implode(',',$perm).' ON '.$this->db.'.'.strtolower($tbname).' FROM '.implode(',',array_map(function($i){return $i.'@'.$this->server;},$username));
$sql = 'REVOKE '.implode(',',$perm).' ON '.strtolower($tbname).' FROM '.implode(',',array_map(function($i){return $i.'@'.$this->server;},$username));
if($this->conn->query($sql)){
return true;
}else{
Expand Down Expand Up @@ -238,12 +263,12 @@ function dropUser(string $username){
}
}
# views
function makeView(string $viewName, string $tbname, array $data, string $condition='', array $options=[]) : string{
$sql = 'CREATE OR REPLACE VIEW '.strtolower($viewName).' AS SELECT '.implode(',',$data).' FROM '.$this->db.'.'.strtolower($tbname).($condition!=='' ? 'WHERE '.$condition : '').' '.implode(' ',$options);
function makeView(string $viewName, array $data, string $selector) : string{
$sql = 'CREATE OR REPLACE VIEW '.strtolower($viewName).' AS '.$selector;
if($this->conn->query($sql)){
$out='<table class="ssql-table '.$this->dm.'">
<tbody>';
$getView = 'SELECT * FROM '.$viewName;
$getView = $selector;
$r = $this->conn->query($getView);
$out.='<tr>';
foreach($data as $d){
Expand Down Expand Up @@ -274,17 +299,114 @@ function dropView(string $viewName) : bool{
return false;
}
}

# others
function genPsw($salt='') {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-_+=';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
#indexs
function createIndex(string $idx_name, string $tbname, array $cols) : bool{
$sql = 'CREATE INDEX '.strtolower($idx_name).' ON '.strtolower($tbname).' ('.implode(',', array_map(function($i){return strtolower($i);},$cols)).')';
if($this->conn->query($sql)){
return true;
}else{
die('Error: '.$this->conn->error);
return false;
}
return $salt.implode($pass); //turn the array into a string
}
function dropIndex(string $tbname, string $idx_name) : bool{
$sql='ALTER TABLE '.strtolower($tbname).' DROP INDEX '.strtolower($idx_name).';';
if($this->conn->query($sql)){
return true;
}else{
die('Error: '.$this->conn->error);
return false;
}
}
# functions/operators
function min(string $col, string $as) : string{
return 'MIN('.strtolower($col).') AS '.strtolower($as);
}
function max(string $col, string $as) : string{
return 'MAX('.strtolower($col).') AS '.$as;
}
function count(string $col) : string{
return 'COUNT('.strtolower($col).')';
}
function avg(string $col) : string{
return 'AVG('.strtolower($col).')';
}
function sum(string $col) : string{
return 'SUM('.strtolower($col).')';
}
function distinct(string $col) : string{
return 'DISTINCT '.strtolower($col);
}
function order(array $col, array $list=['']) : string{
$arg = [];
for($i=0;$i<count($col);$i++){
$args[] = strtolower($col[$i]).($list[$i]!=='DESC'||$list[$i]!=='ASC' ? ' '.strtoupper($list[$i]) : '');
}
return 'ORDER BY '.implode(',', $args);
}
function group(string $col) : string{
return 'GROUP BY '.strtolower($col);
}
function having(string $condition) : string{
return 'HAVING '.$condition;
}
function range(int $min, int $max=0) : string{
return 'LIMIT '.$min.($max==0 ? '' : ' OFFSET '.$max);
}
function where(string $colVal, array $extras=[]) : string{
return 'WHERE '.$colVal.' '.implode(' ',$extras);
}
function in(array $args, bool $not=false) : string{
return ($not ? 'NOT ' : '').'IN ('.implode(',',array_map(function($i){return "'".$i."'";},$args)).')';
}
function like(string $pattern) : string{
return 'LIKE '.$pattern;
}
function and(array $content) : string{
return implode(' AND ',$content);
}
function or(array $content) : string{
return implode(' OR ',$content);
}
function not(string $content) : string{
return 'NOT '.$content;
}
function exists(string $selector) : string{
return 'EXISTS ('.$selector.')';
}
function all(string $selector) : string{
return 'ALL ('.$selector.')';
}
function any(string $selector) : string{
return 'ANY ('.$selector.')';
}
function comment(string $str, bool $block=false) : string{
return ($block ? '/*'.$str.'*/' : '--'.$str);
}
function between(string $com1, string $com2) : string{
return 'BETWEEN '.$com1.' AND '.$com2;
}
function ifNull($exp, $alt) : string{
return 'IFNULL('.$exp.', '.$this->isNoQuote($alt).')';
}
function coalesce(array $list) : string{
return 'COALESCE ('.implode(',',array_map(function($i){return $this->isNoQuote($i);},$list)).')';
}

# joins
function innerJoin(string $table1, string $table2, string $col1, string $col2) : string{
return 'INNER JOIN '.strtolower($table2).' ON '.strtolower($table1).'.'.$col1.' = '.strtolower($table2).'.'.$col2;
}
function leftJoin(string $table1, string $table2, string $col1, string $col2) : string{
return 'LEFT JOIN '.strtolower($table2).' ON '.strtolower($table1).'.'.$col1.' = '.strtolower($table2).'.'.$col2;
}
function rightJoin(string $table1, string $table2, string $col1, string $col2) : string{
return 'RIGHT JOIN '.strtolower($table2).' ON '.strtolower($table1).'.'.$col1.' = '.strtolower($table2).'.'.$col2;
}
function fullJoin(string $table1, string $table2, string $col1, string $col2, bool $outer=false) : string{
return 'FULL '.($outer ? 'OUTER ' : '').'JOIN '.strtolower($table2).' ON '.strtolower($table1).'.'.$col1.' = '.strtolower($table2).'.'.$col2;
}


}
?>

0 comments on commit fc70cc9

Please # to comment.