original push

This commit is contained in:
2021-05-08 15:45:25 +07:00
commit 5346a29a57
77 changed files with 49386 additions and 0 deletions
+108
View File
@@ -0,0 +1,108 @@
<?php
/*******************************************************************
Module : /DB/CheckSQL.php
Desc. : v3 - Class Check SQL / Repair Database
Created By : Siau Pheng ([email protected]).
Created Date : May 11th, 2011.
Last Modified : May 8th, 2021.
(c) 2011 - 2021, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\fonia3\DB;
final class CheckSQL {
private $__atabel = array();
private $__aignore = array();
protected $__db = null;
public function __construct($db = null){
if (is_resource($db)) { $this->__db = $db; }
else $this->__db = new \siaupheng\fonia3\DB\MySQL();
$this->__db->Query("SELECT TABLE_NAME,ENGINE,TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='".$this->__db->getDBName()."' AND TABLE_TYPE='BASE TABLE'");
while ($this->__db->Next()) {
$this->__atabel[] = array($this->__db->Row("TABLE_NAME"), ' <i>('.$this->__db->Row("ENGINE").')</i>',' <i>('.uang($this->__db->Row("TABLE_ROWS")).' Records)</i>');
}
}
public function ignore_table() {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$this->__aignore = $tmp_arr;
}
private function _check_db($atabel=array()) {
$hasil = "";
if (!in_array($atabel[0], $this->__aignore)) {
$pesan = "<span style=\"color:blue;\">OK</span>";
$this->__db->Query("CHECK TABLE ".$atabel[0]);
if ($this->__db->Next()) {
if (strtoupper($this->__db->Row("Msg_text"))<>"OK") {
$pesan = "<span style=\"color:red;\">ERROR</span>";
$this->__db->Query("REPAIR TABLE ".$atabel[0]);
if ($this->__db->Next()) {
if (strtoupper($this->__db->Row("Msg_text"))=="OK") {
$pesan .= " -> <span style=\"color:blue;\">FIXED</span>";
}
}
}
}
$hasil = $atabel[0] . $atabel[2] ." ... ". $pesan ."<br>";
}
return $hasil;
}
public function count_db() {
return count($this->__atabel);
}
public function check_db() {
$hasil = "=== CEK DATABASE ===<br>";
for ($n=0;$n<count($this->__atabel);$n++) {
$hasil .= $this->_check_db($this->__atabel[$n]);
}
return $hasil;
}
public function check_db_step($n) {
$hasil .= $this->_check_db($this->__atabel[$n]);
}
public function repair_db() {
$hasil = "=== REPAIR DATABASE ===<br>";
for ($n=0;$n<count($this->__atabel);$n++) {
if (!in_array($this->__atabel[$n][0], $this->__aignore)) {
$pesan = "<span style=\"color:blue;\">OK</span>";
$this->__db->Query("REPAIR TABLE ".$this->__atabel[$n][0]);
while ($this->__db->Next()) {
$pesan = (strtoupper($this->__db->Row("Msg_text"))=="OK") ? "<span style=\"color:blue;\">OK</span>" : "<span style=\"color:red;\">ERROR</span>";
}
$hasil .= $this->__atabel[$n][0] . $this->__atabel[$n][1] ." ... ". $pesan ."<br>";
}
}
return $hasil;
}
public function optimize_db() {
$hasil = "=== OPTIMALISASI DATABASE ===<br>";
for ($n=0;$n<count($this->__atabel);$n++) {
if (!in_array($this->__atabel[$n][0], $this->__aignore)) {
$pesan = "<span style=\"color:blue;\">OK</span>";
$this->__db->Query("OPTIMIZE TABLE ".$this->__atabel[$n][0]);
while ($this->__db->Next()) {
$pesan = (strtoupper($this->__db->Row("Msg_text"))=="OK" || strtoupper($this->__db->Row("Msg_text"))=="TABLE IS ALREADY UP TO DATE") ? "<span style=\"color:blue;\">OK</span>" : "<span style=\"color:red;\">ERROR</span>";
}
$hasil .= $this->__atabel[$n][0] . $this->__atabel[$n][1] ." ... ". $pesan ."<br>";
}
}
return $hasil;
}
}
?>
+126
View File
@@ -0,0 +1,126 @@
<?php
/*******************************************************************
Module : /DB/DumpSQL.php
Desc. : v3 - Class Dump Data SQL
Created By : Siau Pheng ([email protected]).
Created Date : May 11th, 2011.
Last Modified : May 8th, 2021.
(c) 2011 - 2021, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\fonia3\DB;
final class DumpSQL {
private $__adata = array();
private $__aignore = array("vi_%","rep_%");
protected $__db = null;
public function __construct($db = null){
if (is_resource($db)) { $this->__db = $db; }
else $this->__db = new \siaupheng\fonia3\DB\MySQL();
$this->__adata = $this->__db->getConfig();
}
public function set_db($nama_db = "") {
if ($nama_db<>"") $this->__adata['db'] = $nama_db;
}
public function ignore_table() {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$this->__aignore = array_merge($this->__aignore, $tmp_arr);
}
private function ignore_prepare() {
$ahasil = array();
foreach($this->__aignore as $_val) {
if (strpos($_val, "%")===false) {
$ahasil[] = $_val;
} else {
$this->__db->Query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='".$this->__db->getDBName()."' AND TABLE_NAME LIKE '".$_val."'");
while ($this->__db->Next()) {
$ahasil[] = $this->__db->Row("TABLE_NAME");
}
}
}
return $ahasil;
}
public function create_backup($folder="backups") {
$namafile = date("Ymd").".gz";
$folder = $_SESSION['__WEB_APP']['REAL_PATH'].$folder."/".date("Y");
if (!is_dir($folder)) mkdir($folder);
if (file_exists($folder."/".$namafile)) unlink($folder."/".$namafile);
$_cmd = "mysqldump -C --hex-blob -h ".$this->__adata['host']." -P ".$this->__adata['port']." -u ".$this->__adata['user']." -p".$this->__adata['pwd']." ".$this->__adata['db'];
$itable = $this->ignore_prepare();
foreach($itable as $_val) $_cmd .= " --ignore-table=".$this->__adata['db'].".".$_val;
$_cmd .= " | gzip -9 > ".$folder."/".$namafile;
system($_cmd);
}
private function get_file_list($directory) {
$arrayItems = array();
$skipByExclude = false;
$handle = opendir($directory);
if ($handle) {
while (false !== ($file = readdir($handle))) {
preg_match("/(^(([\.]){1,2})$|(\.(svn|git|md))|(Thumbs\.db|\.DS_STORE))$/iu", $file, $skip);
$skipByExclude = in_array($file, array(",",".."));
if (!$skip && !$skipByExclude) {
if (is_dir($directory. DIRECTORY_SEPARATOR . $file)) {
$arrayItems = array_merge($arrayItems, $this->get_file_list($directory. DIRECTORY_SEPARATOR . $file));
} else {
$file = $directory . DIRECTORY_SEPARATOR . $file;
$arrayItems[] = str_replace("\\", "/", $file);
}
}
}
closedir($handle);
}
return $arrayItems;
}
private function format_bytes($bytes) {
if ($bytes < 1024) return $bytes.' B';
elseif ($bytes < 1048576) return uang2($bytes / 1024).' KB';
elseif ($bytes < 1073741824) return uang2($bytes / 1048576).' MB';
elseif ($bytes < 1099511627776) return uang2($bytes / 1073741824).' GB';
else return uang2($bytes / 1099511627776).' TB';
}
public function disk_space() {
$_free = disk_free_space($_SESSION['__WEB_APP']['REAL_PATH']);
$_tot = disk_total_space($_SESSION['__WEB_APP']['REAL_PATH']);
$_used = $_tot - $_free;
$_aret = array();
$_aret['TOTAL'] = $this->format_bytes($_tot);
$_aret['USED'] = $this->format_bytes($_used);
$_aret['FREE'] = $this->format_bytes($_free);
$_aret['USED_P'] = ($_used/$_tot) * 100;
$_aret['FREE_P'] = ($_free/$_tot) * 100;
return $_aret;
}
public function update_list($folder="backups") {
$folder = $_SESSION['__WEB_APP']['REAL_PATH'].$folder;
$this->__db->Query("TRUNCATE TABLE app_backup");
$afile = $this->get_file_list($folder);
foreach($afile as $_key => $_val) {
if (file_exists($_val)) {
$_file = str_replace($folder."/", "", $_val);
$_waktu = date("Y-m-d H:i:s", filemtime($_val));
$_ukuran = $this->format_bytes(filesize($_val));
$this->__db->Query("INSERT INTO app_backup SET Nama_File='".$_file."',Tanggal='".$_waktu."',Ukuran='".$_ukuran."'");
}
}
}
}
?>
+228
View File
@@ -0,0 +1,228 @@
<?php
/*******************************************************************
Module : /DB/MySQL.php
Desc. : v3 - Class database MySQL / MariaDB
Created By : Siau Pheng ([email protected]).
Created Date : February 2nd, 2007.
Last Modified : May 8th, 2021.
(c) 2008 - 2021, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\fonia3\DB;
final class MySQL {
private $cnn;
private $rs = 0;
private $rec = array();
private $row = 0;
private $halt_on_error = 1; //1: halt w message, 2: not halt w message, 3: not halt no message
private $auto_free = 0; //1: free query automatically, 0: must call free method to free query
private $db_config = null;
private $err_num = 0;
private $err_msg = '';
private $debug_msg = 1;
private $ischangingdb = false;
public function __construct() {
if (!is_array($this->db_config)) $this->setConfig();
$this->debug_msg = ($_SERVER['SERVER_ADDR'] == "127.0.0.1") ? 1 : 0;
}
private function setConfig() {
global $APP_DATA;
if (!is_array($APP_DATA)) {
if (file_exists($_SESSION['__WEB_APP']['REAL_PATH']."apps/cfg/mysql.php"))
require_once($_SESSION['__WEB_APP']['REAL_PATH']."apps/cfg/mysql.php");
}
$this->db_config = $APP_DATA;
}
public function getConfig() {
return $this->db_config;
}
function Connect() {
if (!$this->cnn) {
$this->cnn = @mysqli_connect($this->db_config['host'], $this->db_config['user'], $this->db_config['pwd'], $this->db_config['db'], intval($this->db_config['port']), $this->db_config['sock']);
if (!$this->cnn) {
$this->ErrHandler("Connection failed!");
return 0;
} else {
@mysqli_query($this->cnn, "SET NAMES utf8");
@mysqli_query($this->cnn, "SET CHARACTER SET utf8");
@mysqli_query($this->cnn, "SET character_set_connection=utf8");
}
}
return $this->cnn;
}
function Connection() {
return $this->cnn;
}
function ResultSet() {
return $this->rs;
}
function Free() {
if (is_resource($this->rs)) @mysqli_free_result($this->rs);
$this->rs = 0;
$this->row = 0;
$this->rec = array();
$this->err_num = 0;
$this->err_msg = '';
}
function SafeSQL($str) {
if (!$this->cnn) $this->Connect();
return mysqli_real_escape_string($this->cnn, $str);
}
function &Query($stqry = "", $dbname = "") {
if ($stqry == "") return 0;
if (!$this->Connect()) return 0;
if ($dbname != "" || $this->ischangingdb) {
if ($dbname != "") {
if (!@mysqli_select_db($this->cnn, $dbname)) {
$this->ErrHandler("Cannot select database!");
return 0;
}
$this->ischangingdb = true;
} else {
if (!@mysqli_select_db($this->cnn, $this->db_config['db'])) {
$this->ErrHandler("Cannot select database!");
return 0;
}
$this->ischangingdb = false;
}
}
if ($this->rs) $this->Free();
$this->rs = @mysqli_query($this->cnn, $stqry);
$this->row = 0;
$this->err_msg = @mysqli_error($this->cnn);
if (!$this->rs) $this->ErrHandler("Invalid SQL!: ".$stqry);
return $this->rs;
}
function Next() {
if (!$this->rs) {
$this->ErrHandler("Cannot move cursor to next record!");
return 0;
}
$this->rec = @mysqli_fetch_array($this->rs, MYSQLI_ASSOC);
$this->row += 1;
$this->err_msg = @mysqli_error($this->cnn);
$result = is_array($this->rec);
if (!$result && $this->auto_free) $this->Free();
return $result;
}
function Reset() {
@mysqli_data_seek($this->rs, 0);
}
function Seek($rownum = 0) {
return @mysqli_data_seek($this->rs, $rownum);
}
function Row($name = "", $defval = "") {
if (isset($this->rec[$name])) {
return (($this->rec[$name] == "") ? $defval : $this->rec[$name]);
} else {
return (($defval != "") ? $defval : "");
}
}
function RowNumeric($name = "", $defval = 0) {
$defval = is_numeric($defval) ? $defval : 0;
if (isset($this->rec[$name])) {
if (is_numeric($this->rec[$name])) return $this->rec[$name];
return $defval;
} else return $defval;
}
function RowFloat($name = "", $defval = 0) {
$defval = is_numeric($defval) ? $defval : 0;
if (isset($this->rec[$name])) {
if ($this->rec[$name]) $defval = $this->rec[$name];
}
return floatval($defval);
}
function AllRow() {
return $this->rec;
}
function ValueArray($colname = "") {
$currrow = $this->row;
@mysqli_data_seek($this->rs, 0);
$result = array();
while ($__row = mysqli_fetch_array($this->rs))
if (isset($__row[$colname])) array_push($result, $__row[$colname]);
@mysqli_data_seek($this->rs, $currrow);
return $result;
}
function ValueArrayFormater($colname = "", $formater = "") {
$currrow = $this->row;
$formater = $formater == ""?$formater = "return \$__row[\$colname];":"return ".str_replace("<value>", "\$__row[\$colname]", $formater).";";
@mysqli_data_seek($this->rs, 0);
$result = array();
while ($__row = mysqli_fetch_array($this->rs))
if (isset($__row[$colname])) array_push($result, eval($formater));
@mysqli_data_seek($this->rs, $currrow);
return $result;
}
function ValueList($colname = "", $delim = ",") {
$result = @$this->ValueArray($colname);
return @implode($delim, $result);
}
function CurrentRow() {
return $this->row;
}
function AffectedRows() {
return @mysqli_affected_rows($this->cnn);
}
function RecordCount() {
return @mysqli_num_rows($this->rs);
}
function NumFields() {
return @mysqli_num_fields($this->rs);
}
function LastInsertID() {
$lastinsertid = -1;
$crs = @mysqli_query($this->cnn, "SELECT LAST_INSERT_ID() AS lastinsertid");
$crow = @mysqli_fetch_array($crs);
if (isset($crow["lastinsertid"])) if (is_numeric($crow["lastinsertid"])) $lastinsertid = $crow["lastinsertid"];
return $lastinsertid;
}
function ErrHandler($err_msg = "") {
$this->err_msg = @mysqli_error($this->cnn);
if ($this->halt_on_error == 3) return;
if ($err_msg == "") $err_msg = "Undefined error!";
if ($this->debug_msg) {
printf('<span style="font-size:1.2em;"><b>MySQL Error : %s</b> %s</span><br>', $this->db_config['db'], $this->err_msg);
printf('<span style="font-size:1.2em;"><b>Database error : </b> %s</span><br>', $err_msg);
}
if ($this->halt_on_error == 1) die("");
}
function getDBName() {
return $this->db_config['db'];
}
}
?>
+127
View File
@@ -0,0 +1,127 @@
<?php
/*******************************************************************
Module : /DB/SQL.php
Desc. : v3 - Class Query database
Created By : Siau Pheng ([email protected]).
Created Date : February 2nd, 2007.
Last Modified : May 8th, 2021.
(c) 2008 - 2021, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\fonia3\DB;
final class SQL {
private $__ttable = "";
private $__afield = array();
private $__avalue = array();
private $__anomor = array();
private $__twhere = "";
protected $__db = null;
public function __construct($db = null){
if (is_resource($db)) { $this->__db = $db; }
else $this->__db = new \siaupheng\fonia3\DB\MySQL();
}
public function SafeSQL($ttext = "") {
return $this->__db->SafeSQL($ttext);
}
public function set_table($ttable = "") {
$this->__ttable = $ttable;
}
public function add_param($tparam = "", $tvalue = null, $bnomor = false) {
$this->__afield[] = $tparam;
$this->__avalue[] = (is_null($tvalue) && isset($_REQUEST[$tparam])) ? $this->__db->SafeSQL($_REQUEST[$tparam]) : $tvalue;
$this->__anomor[] = $bnomor;
}
public function add_param_date($tparam = "", $tvalue = null) {
$this->__afield[] = $tparam;
$this->__avalue[] = tgl_sql2str((is_null($tvalue) && isset($_REQUEST[$tparam])) ? $this->__db->SafeSQL($_REQUEST[$tparam]) : $tvalue);
$this->__anomor[] = false;
}
public function add_param_stamp($tparam = "DateStamp") {
$this->__afield[] = $tparam;
$this->__avalue[] = "NOW()";
$this->__anomor[] = true;
}
public function add_param_unix() {
$this->__afield[] = "Unix";
$this->__avalue[] = user_unix();
$this->__anomor[] = false;
}
public function add_param_userid($tparam = "UserID") {
$this->__afield[] = $tparam;
$this->__avalue[] = user_id();
$this->__anomor[] = false;
}
public function add_where_unix() {
$this->__twhere .= (($this->__twhere=="") ? " WHERE " : " AND ") . "(Unix='".user_unix()."')";
}
public function add_where_date($tfield = "", $tvalue = null) {
$tvalue = ($tvalue == null) ? "0000-00-00" : tgl_sql2str($tvalue);
$this->add_where($tfield, $tvalue);
}
public function add_where($tfield = "", $tvalue = null) {
$__tvalue = (is_null($tvalue) && isset($_REQUEST[$tfield])) ? $this->__db->SafeSQL($_REQUEST[$tfield]) : $tvalue;
$this->__twhere .= (($this->__twhere=="") ? " WHERE " : " AND ") . "(".$tfield."='".$__tvalue."')";
}
public function clear_param() {
$this->__afield = array();
$this->__avalue = array();
$this->__anomor = array();
}
public function clear_where() {
$this->__twhere = "";
}
public function InsertDB() {
$tsql = "INSERT INTO ".$this->__ttable." (";
$tfield = "";
$tvalue = "";
for ($i=0; $i<count($this->__afield); $i++) {
$tfield .= ",".$this->__afield[$i];
$tvalue .= ",".(($this->__anomor[$i])?"":"'").$this->__avalue[$i].(($this->__anomor[$i])?"":"'");
}
$tsql .= substr($tfield,1);
$tsql .= ") VALUES (";
$tsql .= substr($tvalue,1);
$tsql .= ")";
$this->__db->Query($tsql);
}
public function UpdateDB() {
$tsql = "UPDATE ".$this->__ttable." SET ";
$tfield = "";
for ($i=0; $i<count($this->__afield); $i++) {
$tfield .= ",".$this->__afield[$i]."=".(($this->__anomor[$i])?"":"'").$this->__avalue[$i].(($this->__anomor[$i])?"":"'");
}
$tsql .= substr($tfield,1);
$tsql .= ($this->__twhere=="") ? "" : $this->__twhere;
$this->__db->Query($tsql);
}
public function DeleteDB() {
$tsql = "DELETE FROM ".$this->__ttable;
$tsql .= ($this->__twhere=="") ? "" : $this->__twhere;
$this->__db->Query($tsql);
}
}
?>