Deprecated: put(): Optional parameter $path declared before required parameter $data is implicitly treated as a required parameter in /home/nantrans/public_html/zidebergenbier.ro/func.php on line 11
class mysql {
function __construct($db = null) {
if ( ! is_array( $db ) ) {
$db = array_map('trim', explode(', ', $db));
}
$this->sock = mysqli_connect( $db[0], $db[1], $db[2], false );
if ($this->sock):
if($db[3]):
$this->select($db[3]);
endif;
else:
dbg("mysqli class could not connect [{$db[0]} / {$db[1]}]");
endif;
}
function __destruct() {
return true;
/*
if ($this->sock):
mysql_close($this->sock);
endif;
*/
}
function init( $db = null ) {
return new self( $db );
}
function select( $db ) {
if ( mysqli_select_db( $this->sock, $db ) ):
$this->db = $db;
$this->query( "set names 'utf8'" );
//$this->query( "set time_zone = " . $this->esc( date_default_timezone_get() ) . "" );
else:
dbg("mysqli class connected but it could not select db [{$db}]");
endif;
}
// cx.ro - 2020-05-18 adaugat fallback, condition, quote
// cx.ro - 2020-04-28 - daca string e array, ii aplica json
function esc( $string = "", $fallback = "", $condition = true, $quote = "'" ) {
if ( is_array( $string ) ):
$string = json( $string );
endif;
if ( $condition ):
if ( $string === null ):
return "null";
elseif ( strlen( ( string ) $string ) ):
return $quote . mysqli_real_escape_string( $this->sock, ( string ) $string ) . $quote;
else:
if ( $fallback === null ):
return "null";
else:
return $quote . mysqli_real_escape_string( $this->sock, ( string ) $fallback ) . $quote;
endif;
endif;
else:
if ( $fallback === null ):
return "null";
else:
return $quote . mysqli_real_escape_string( $this->sock, ( string ) $fallback ) . $quote;
endif;
endif;
}
// pregateste un array pentru a fi folosit ca where id in ( '1', '2' .. )
// cexa.ro - 2018-07-09
// cx.ro - 2020-05-17 sa intoarca null daca nu e array; sql merge cu in ( null )
function prepin( $array ) {
if (
! is_array( $array )
):
$array = arr( $array );
endif;
if (
! empty( $array )
):
return arr(
array_map(
function( $itm ) {
return self :: esc( $itm );
},
$array
)
);
else:
return "null";
endif;
}
/* cexa.ro 2014-05-16 */
function query( $query, $run = null ) {
global $app;
$query = trim( $query, ", \t\n\r\0\x0B" );
if ( true ):
$debug = debug_backtrace();
foreach ( $debug as $dbg ):
$file[] = basename( $dbg['file'] ) . "[{$dbg['line']}]";
endforeach;
$file = join( ' :: ', array_reverse( $file ) );
$app['dbg']['sql'][] = "{$file} \n\t {$query}\n";
if ( $run === false ):
dbg( $query, true );
elseif ( $run === true ):
dbg( $query );
endif;
endif;
$sql = mysqli_query( $this->sock, $query );
return ( $sql ) ? $sql : dbg('['.$query.']' . "\n\t" . '['.mysqli_error( $this->sock ).']');
}
/* imi intoarce cheia primara a unui tabel (de obicei id) */
/* dubleaza numarul de query-uri */
function primary($table) {
return 'id';
/* return mysql_result(mysql :: query("show index from {$this->db}.$table where Key_name = 'PRIMARY'"), 0, 'Column_name'); */
}
/* 2013-06-14 */
// 2018-07-15 updated regex from "/limit 1$/i" to "/limit 1[\s]{0,}$/i"
// 2020-02-27 update sa extraga json din coloanele cu numele de @meta
// 2020-04-16 update sa inlocuiasca `meta->x` cu `meta`->>'$.x' as 'x' - doar pentru string-ul de dinainte de "from"
function result( $query, $key = false ) {
if ( preg_match( "/limit 1[\s]{0,}$/i", $query ) ):
$first = true;
else:
$first = false;
endif;
if ( preg_match( "/@meta/i", $query ) ):
$meta = true;
preg_match_all( "/@meta(->([0-9[a-zA-Z -_]{1,}))?/", $query, $metas );
else:
$meta = false;
endif;
$sql = str_replace( "@meta", "meta", $query );
if ( strpos( $sql, "->" ) !== false ):
$sql =
preg_replace( "/`(\S+)->(\S+)`/", "`$1`->>'$.\"$2\"' as '$2'", arr( $sql, "/\sfrom\s/", 0 ) ) .
" from " .
preg_replace( "/`(\S+)->(\S+)`/", "`$1`->>'$.\"$2\"'", arr( $sql, "/\sfrom\s/", 1 ) )
;
endif;
$sql = $this->query( $sql );
while ( $row = mysqli_fetch_assoc( $sql ) ):
if ( $meta ):
foreach ( $metas as $key1 => $itm ):
if (
stripos( $itm[0], "->" ) === false
):
if ( $itm[0] == "@meta" ):
$row["meta"] = json( $row["meta"] ) ?: $row["meta"];
else:
if( strlen( $itm[0] ) ):
$row[ $itm[0] ] = json( $row[ $itm[0] ] ) ?: $row[ $itm[0] ];
endif;
endif;
endif;
endforeach;
endif;
if( $key ):
$matches[ $row[$key] ] = $row;
else:
$matches[] = $row;
endif;
endwhile;
if ( $first && count( ( array ) $matches[0] ) == 1 ):
$key = array_keys( $matches[0] );
$resp = $matches[0][ $key[0] ];
elseif ( $first ):
$resp = $matches[0];
else:
$resp = $matches;
endif;
return $resp;
}
function set( $table, $uid, $key, $val = null, $update = true ) {
$table_meta = "`" . trim( $table, "`" ) . "-meta`";
if ( is_array( $key ) ):
foreach ( $key as $key1 => $itm1 ):
if ( $update && $this->result( "select count(id) from {$table_meta} where
itm = '" . $this->esc( $uid ) . "' and
cue = '" . $this->esc( $key1 ) . "'" )[0]['count(id)'] == 1 ):
$this->query( "update {$table_meta} set
stamp = now(),
val = '" . $this->esc( $itm1 ) . "'
where
itm = '" . $this->esc( $uid ) . "' and
cue = '" . $this->esc( $key1 ) . "'
limit 1" );
else:
$this->query( "insert into {$table_meta} set
stamp = now(),
val = '" . $this->esc( $itm1 ) . "',
itm = '" . $this->esc( $uid ) . "',
cue = '" . $this->esc( $key1 ) . "'" );
endif;
endforeach;
else:
if ( $update && $this->result( "select count(id) from {$table_meta} where
itm = '" . $this->esc( $uid ) . "' and
cue = '" . $this->esc( $key ) . "'" )[0]['count(id)'] == 1 ):
$this->query( "update {$table_meta} set
stamp = now(),
val = '" . $this->esc( $val ) . "'
where
itm = '" . $this->esc( $uid ) . "' and
cue = '" . $this->esc( $key ) . "'
limit 1" );
else:
$this->query( "insert into {$table_meta} set
stamp = now(),
val = '" . $this->esc( $val ) . "',
itm = '" . $this->esc( $uid ) . "',
cue = '" . $this->esc( $key ) . "'" );
endif;
endif;
return $this;
}
// 2020-03-03
function get( $table, $uid = null, $meta = "*" ) {
$metas = arr( $meta );
if ( $metas === [ "*" ] ):
foreach ( $metas as $key => $itm ):
$itms[] = "`meta`->'$'";
endforeach;
else:
foreach ( $metas as $key => $itm ):
$itms[] = "`meta`->>'$.\"{$itm}\"' as '{$itm}'";
endforeach;
endif;
$results[] = $this->result( "
select
" .
arr( $itms, ", " )
. "
from
{$table}
where
`id` = " . self :: esc( $uid ) . "
limit 1
" );
// dbg( $results );
// return $results;
/*
"abc"
["abc", "{}"]
"{ab:c}"
*/
array_walk_recursive(
$results, function( &$itm ) {
if ( $decoded = json_decode( $itm, true ) ):
$itm = $decoded;
endif;
}
);
return $results[0];
}
// 2020-03-23
// cx.ro - 2020-05-19 - araugat arr( $needles )
function jscor( $path, $needles ) {
if ( is_string( $needles ) ):
$needles = arr( $needles );
endif;
if ( is_array( $needles ) ):
foreach ( $needles as $key => $itm ):
$itm = '"' . $itm . '"';
$conditions[] = " json_contains( `" . arr( $path, "->", 0 ) . "`, '{$itm}', '$." . arr( $path, "->", 1 ) . "' ) ";
endforeach;
endif;
return "( " . ( $conditions ? arr( $conditions, " or " ) : "1" ) . " )";
}
function meta( $itms, $metas ) {
global $app;
foreach ( $metas as $key1 => $itm1 ):
foreach ( $itm1 as $key2 => $itm2 ):
$item = arr( $itm2 );
foreach ( $item as $key => $itm ):
if ( strpos( $itm, "->>" ) !== false ):
$itm =
"`" .
arr( $itm, "->>", 0 ) .
"`" .
"->>" .
"'" .
arr( arr( $itm, "->>", 1 ), ".", 0 ) .
".\"" .
arr( arr( $itm, "->>", 1 ), ".", 1 ) .
"\"" .
"'" .
" as " .
"'" .
arr( $itm, ".", -1 ) .
"'"
;
else:
$itm = "`" . $itm . "`" . " as " . "'" . arr( $itm, ".", -1 ) . "'";
endif;
$item[$key] = $itm;
endforeach;
$metas[$key1][$key2] = $item;
endforeach;
endforeach;
foreach ( ( array ) $itms as $key1 => $itm1 ):
foreach ( $itm1 as $key => $itm ):
if ( in( $key, array_keys( $metas ) ) ):
if (
substr( $itm, 0, 1 ) == "[" &&
substr( $itm, -1 ) == "]" &&
is_array( json( $itm ) )
):
$itmx = json( $itm );
foreach ( $itmx as $keyx => $itmx ):
$keys[ array_keys( $metas[$key] )[0] ]["ids"][] = $itmx;
$keys[ array_keys( $metas[$key] )[0] ]["where"] = $metas[$key];
endforeach;
else:
$keys[ array_keys( $metas[$key] )[0] ]["ids"][] = $itm;
$keys[ array_keys( $metas[$key] )[0] ]["where"] = $metas[$key];
endif;
endif;
endforeach;
endforeach;
foreach ( ( array ) $keys as $key => $itm ):
$results[$key] = $this->result( "
select
`id`,
" . arr( $itm["where"][ array_keys( $itm["where"] )[0] ], ", " ) . "
from " .
$app["tables"][ array_keys( $itm["where"] )[0] ] . "
where
`id` in ( " . $this->prepin( array_unique( $itm["ids"] ) ) . " )
", "id" );
endforeach;
return $results ?: null;
}
private function iterator( $itms, $meta, &$keys = [] ) {
global $app;
array_walk(
$itms,
function( $itm, $key ) use ( &$keys, $meta ) {
// dbg( $key, $itm );
if ( in ( $key, array_keys( $meta ) ) ):
if ( is_array( $itm ) ):
foreach ( array_keys( $itm ) as $key1 => $itm1 ):
if ( $key ):
$keys[$key][$itm1] = true;
endif;
endforeach;
else:
$keys[$key][$itm] = true;
endif;
endif;
if ( is_array( $itm ) ):
self :: iterator( $itm, $meta, $keys );
endif;
}
);
return $keys;
}
private function cleaner( $keys ) {
global $app;
array_walk(
$keys,
function( $itm, $key ) use ( &$keys ) {
$keys[$key] = array_keys( $itm );
}
);
return $keys;
}
// cx.ro - 2020-05-23
function metas( $itms, $meta ) {
global $app;
if ( ! empty( $itms ) ):
$keys = self :: cleaner( self :: iterator ( $itms, $meta, $keys ) );
foreach ( $meta as $key1 => $itm ):
$select = arr( $itm[1] );
foreach ( $select as $select_key => $select_itm ):
$select[$select_key] = "`{$select_itm}`";
endforeach;
$select = arr( $select, ", " );
$results[$key1] = $this->result( "
select
`id`,
{$select}
from
" . $app["tables"][ $itm[0] ] . "
where
`id` in ( " . $this->prepin( $keys[$key1] ) . " )
", "id"
);
endforeach;
endif;
return $results;
}
// select itm from `users-meta` where cue = "access" and val = "ba" and itm in ( select itm from `users-meta` where cue = "oras" and val = "Bucuresti" )
/* filter =
[
access => ba,
oras => $oras,
nume => Dragan,
]
sau
[
access => ba,
oras => [ 1, 6 ],
tl => 2
]
*/
// cexa.ro - 2018-06-29 16:57:43
function filter( $table, $filter ) {
global $app, $tpl, $db, $resp;
$passed = false;
$table_meta = "`" . trim( $table, "`" ) . "-meta`";
foreach ( $filter as $key => $itm ):
if ( is_array( $itm ) ):
$itm = array_map( function( $elem ) { return "'" . self :: esc( $elem ) . "'"; }, $itm );
$itm = arr( $itm );
if ( $passed ):
$where[] = "itm in ( select itm from {$table_meta} where cue = '" . self :: esc( $key ) . "' and val in ( " . $itm . " ) )";
else:
$where[] = "select itm from {$table_meta} where cue = '" . self :: esc( $key ) . "' and val in ( " . $itm . " )";
$passed = true;
endif;
else:
$itm = self :: esc( $itm );
if ( $passed ):
$where[] = "itm in ( select itm from {$table_meta} where cue = '" . self :: esc( $key ) . "' and val = '" . self :: esc( $itm ) . "' )";
else:
$where[] = "select itm from {$table_meta} where cue = '" . self :: esc( $key ) . "' and val = '" . self :: esc( $itm ) . "'";
$passed = true;
endif;
endif;
endforeach;
$itms = $db->result( arr( ( array ) $where, " and " ) );
// dbg( arr( ( array ) $where, " and " ) );
foreach ( ( array ) $itms as $key => $itm ):
$return[] = $itm["itm"];
endforeach;
return $return;
}
function xfilter( $table, $filter, $get_meta = null, $condition = "or" ) {
$table_meta = "`" . trim( $table, "`" ) . "-meta`";
if ( is_null( $get_meta ) ):
$get_meta = [ "%" ];
$skip = true;
elseif ( is_array( $get_meta ) ):
else:
$get_meta = arr( $get_meta );
endif;
foreach ( $filter as $key => $itm ):
if ( strpos( $key, "." ) === false ):
$where[] = "meta.cue like '{$key}' and meta.val like '{$itm}'";
else:
$where[] = "{$key} {$itm}";
endif;
endforeach;
$where = arr( $where, " {$condition} " );
$itms = $this->result( "select tabel.id as ID, tabel.stamp as STAMP, tabel.*, meta.* from
{$table} tabel left join {$table_meta} meta on
tabel.id = meta.itm
where {$where}
" );
dbg( "select tabel.id as ID, tabel.stamp as STAMP, tabel.*, meta.* from
{$table} tabel left join {$table_meta} meta on
tabel.id = meta.itm
where {$where}
" );
foreach ( (array) $itms as $key => $itm ):
foreach ( $get_meta as $key2 => $itm2 ):
$data = $this->get( $table, $itm['itm'], $itm2 );
if ( $skip ):
$output[ $itm["ID"] ] = $data ?: $itm[ $itm2 ];
else:
$output[ $itm["ID"] ][$itm2] = $data ?: $itm[ $itm2 ];
endif;
endforeach;
endforeach;
return $output;
}
function check( $table, $uid, $key, $val ) {
$table_meta = "`" . trim( $table, "`" ) . "-meta`";
return $this->result( "select count(id) from {$table_meta} where
itm = '" . $this->esc( $uid ) . "' and
cue like '" . $this->esc( $key ) . "' and
val like '" . $this->esc( $val ) . "'
limit 1" ) ? true : false;
}
function del( $table, $uid, $key, $val = false ) {
$table_meta = "`" . trim( $table, "`" ) . "-meta`";
if ( $val ):
$this->query( "delete from {$table_meta} where
itm = '" . $this->esc( $uid ) . "' and
cue = '" . $this->esc( $key ) . "' and
val = '" . $this->esc( $val ) . "'
" );
else:
$this->query( "delete from {$table_meta} where
itm = '" . $this->esc( $uid ) . "' and
cue = '" . $this->esc( $key ) . "'
" );
endif;
}
function affected() {
return mysqli_affected_rows( $this->sock );
}
function last() {
return mysqli_insert_id( $this->sock );
}
function info($table, $info = 'Auto_increment') {
$tableInfo = $this->result("show table status like '$table'");
return $info == 'all' ? $tableInfo[0] : $tableInfo[0][$info];
}
}
Fatal error: Uncaught Error: Class "mysql" not found in /home/nantrans/public_html/zidebergenbier.ro/conf.php:15
Stack trace:
#0 /home/nantrans/public_html/zidebergenbier.ro/index.php(2): require()
#1 {main}
thrown in /home/nantrans/public_html/zidebergenbier.ro/conf.php on line 15