*', '>=*', '<*', '<=*' - compare $reqValue
* examples:
* - show when $_REQUEST['DBG_SCH'] == '1'
* DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
* - show when $_REQUEST['DBG_SCH'] > '1'
* DBG::_('DBG_SCH', '>1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
* - show when any value: strlen($_REQUEST['DBG_SCH']) > 0
* DBG::_('DBG_SCH', true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
* - always show
* DBG::_(true, true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
*/
public static function _($reqKey, $reqValueExpr, $label, $variable, $className, $functionName, $lineNumber, $borderColor = 'red') {
if (!DBG::isActive()) return;
$showDBG = false;
$reqValue = (true === $reqKey || !$reqKey)? '' : V::get($reqKey, '', $_GET);
if (true === $reqKey) {
$showDBG = true;
}
else if (strlen($reqValue) == 0) {
return;
}
else if ($reqValue == $reqValueExpr) {
$showDBG = true;
}
else if (true === $reqValueExpr) {
$showDBG = true;
}
else {
if ('>=' == substr($reqValueExpr, 0, 2)) {
if ($reqValue >= substr($reqValueExpr, 2)) {
$showDBG = true;
}
}
else if ('>' == substr($reqValueExpr, 0, 1)) {
if ($reqValue > substr($reqValueExpr, 1)) {
$showDBG = true;
}
}
else if ('<=' == substr($reqValueExpr, 0, 2)) {
if ($reqValue <= substr($reqValueExpr, 2)) {
$showDBG = true;
}
}
else if ('<' == substr($reqValueExpr, 0, 1)) {
if ($reqValue < substr($reqValueExpr, 1)) {
$showDBG = true;
}
}
else {
if ($reqValue = $reqValueExpr) {
$showDBG = true;
}
}
}
if ($showDBG) {
?>
({$className}::{$functionName}:{$lineNumber}):"; ?>
| Lp. |
|
$prefixes) : ?>
|
|
|
$prefixes) : ?>
: ""
: ""
|
'#96c178', 'red'=>'#de6b74', 'blue'=>'#55b5c1', 'bg-dark'=>'#282c34', 'white'=>'#abb2bf', 'orange'=>'#d19a66', 'violet'=>'#c476db'];
if (is_scalar($variable)) {
$outLines = [ V::strShort((string)$variable, 20), $variable ];
} else {
$cnt = '';
ob_start();
print_r($variable);
$cnt = ob_get_clean();
$outLines = array();
$lines = explode("\n", $cnt);
$firstLine = $lines[0];
foreach ($lines as $line) {
if ('(' == trim($line)) continue;
if (')' == trim($line)) continue;
if ('' == trim($line)) continue;
if ('Array' == substr($line, -5) || 'stdClass' == substr($line, -5)) {
$line = str_replace('Array', 'Array', $line);
$line = str_replace('stdClass', 'stdClass', $line);
$line .= ':';
}
if (($firstBracket = strpos($line, '[')) > 0) {
$line = str_replace("\t", ' ', $line);
$splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 2 + 4 : 4;
$line = substr($line, $firstBracket - $splitPos);
$line = preg_replace('/\[(\w+)\]/', '[\1]', $line);
$line = preg_replace('/\] \=\> (.+)$/', '] => \1', $line);
}
$outLines[] = $line;
}
}
$maxHeight = V::get('maxHeight', '400px', $params);
return UI::h('details', ['style'=>"max-width:800px"], [
UI::h('summary', ['style'=>"padding:3px 8px;border:1px solid #ccc;background-color:{$col['bg-dark']};color:{$col['white']};font-size:small"],
($varName)
? '' . $varName . " => " . htmlspecialchars(V::strShort($firstLine, 20))
: htmlspecialchars(V::strShort($firstLine, 20))
),
UI::h('pre', [
'style' => implode(";", [
'background-color:'.$col['bg-dark'],
'color:'.$col['white'],
'border:1px solid #ccc;border-radius:0',
'font-size:x-small',
($maxHeight) ? "max-height:{$maxHeight};overflow:scroll" : ''
])
], implode("\n", $outLines))
]) . "\n";
}
public static function niceDump($variable, $varName, $params = []) {
$col = ['green'=>'#96c178', 'red'=>'#de6b74', 'blue'=>'#55b5c1', 'bg-dark'=>'#282c34', 'white'=>'#abb2bf', 'orange'=>'#d19a66', 'violet'=>'#c476db'];
$maxHeight = V::get('maxHeight', '400px', $params);
ob_start();
var_dump($variable);
$cnt = ob_get_clean();
$cnt = preg_replace('/=>\s+/', ' => ', $cnt);
$cnt = preg_replace('/\) "(.*)/', ') "\1', $cnt);
$cnt = preg_replace('/\["(\w+)"\] =>/', '[\1] =>', $cnt);
$cnt = preg_replace('/object\((\w+)\)/', 'object(\1)', $cnt);
$cnt = preg_replace('/string\((\w+)\)/', 'string(\1)', $cnt);
$cnt = preg_replace('/array\((\w+)\)/', 'array(\1)', $cnt);
$cnt = preg_replace('/int\((\w+)\)/', 'int(\1)', $cnt);
$cnt = preg_replace('/bool\((\w+)\)/', 'bool(\1)', $cnt);
echo UI::h('details', ['style'=>"max-width:800px"], [
UI::h('summary', ['style'=>"padding:3px 8px;border:1px solid #ccc;background-color:{$col['bg-dark']};color:{$col['white']};font-size:small"],
'' . $varName . ":"
),
UI::h('pre', [
'style' => implode(";", [
'background-color:'.$col['bg-dark'],
'color:'.$col['white'],
'border:1px solid #ccc;border-radius:0',
'font-size:x-small',
($maxHeight) ? "max-height:{$maxHeight};overflow:scroll" : ''
])
], $cnt)
]) . "\n";
}
/**
* @param $mixedArg string or Exception
*/
public static function log($mixedArg, $type = '', $msg = '') {
if (!self::isActive() && !self::isLogActiveByAdmin()) return;
if ('Debug' == V::get('_route', '', $_REQUEST) && !V::get('DBG', '', $_REQUEST)) return;
if ('UrlAction_Bocian' == V::get('_route', '', $_REQUEST) && 'fetchProgressAjax' == V::get('_task', '', $_REQUEST) && !V::get('DBG', '', $_REQUEST)) return;
static $_firstRun = true;
if ($_firstRun) {
$_firstRun = false;
$firstMsg = V::get('REQUEST_METHOD', '', $_SERVER) . " " . V::get('REQUEST_URI', '', $_SERVER);
DebugLazyLogger::log([ 'POST' => $_POST, 'GET' => $_GET, 'POST_BODY' => Request::getRequestBody() ], 'array', $firstMsg);
}
DebugLazyLogger::log($mixedArg, $type, $msg);
}
public static function logAuth($mixedArg, $msg = '') {
if ('Debug' == V::get('_route', '', $_REQUEST) && !V::get('DBG', '', $_REQUEST)) return;
static $_firstRun = true;
$prefix = ('production' == V::get('P5_ENV', 'production', $_SERVER)) ? "" : "dev-";
$logFileName = "/tmp/{$prefix}se-auth-" . implode('-', [
date("Y-m-d"),
Request::getUserIp(),
V::get('REQUEST_TIME', '', $_SERVER)// [REQUEST_TIME] => 1485770466
]) . '.log';
if ($_firstRun) {
$_firstRun = false;
$firstMsg = V::get('REQUEST_METHOD', '', $_SERVER) . " " . V::get('REQUEST_URI', '', $_SERVER);
self::_log(['POST' => $_POST, 'GET' => $_GET], 'array', $firstMsg, $logFileName);
}
self::_log($mixedArg, $type = 'array', $msg, $logFileName);
}
public static function _log($mixedArg, $type, $msg, $logFileName) {
$logInfo = [
'date' => date("Y-m-d H:i:s") . substr((string)microtime(), 1, 6),
'type' => $type,
'msg' => $msg,
'log' => '',
'trace' => '',
];
if ($mixedArg instanceof Exception) {
$logInfo['type'] = 'Exception';
if (!empty($logInfo['msg'])) $logInfo['msg'] .= ". ";
$logInfo['msg'] .= $mixedArg->getMessage();
$logInfo['log'] = [
'code' => $mixedArg->getCode(),
'line' => $mixedArg->getLine(),
'file' => str_replace(APP_PATH_ROOT, 'SE', $mixedArg->getFile()),
];
$logInfo['trace'] = $mixedArg->getTraceAsString();// getTrace
} else if (is_string($mixedArg)) {
if ('sql' == $type) {
if (!$logInfo['type']) $logInfo['type'] = 'sql';
if (empty($logInfo['msg'])) $logInfo['msg'] = "sql";
$logInfo['log'] = $mixedArg;
} else {
if (empty($logInfo['type'])) $logInfo['type'] = 'string';
if (empty($msg)) {
$logInfo['msg'] = $mixedArg;
$logInfo['log'] = '';
} else {
$logInfo['msg'] = $msg;
$logInfo['log'] = $mixedArg;
}
}
ob_start();
debug_print_backtrace();
$logInfo['trace'] = ob_get_clean();
} else if ('array' == $type || is_array($mixedArg)) {
$mixedArg = (array)$mixedArg;
if (!$logInfo['type']) $logInfo['type'] = 'array';
if (!empty($logInfo['msg']) && !empty($mixedArg['msg'])) $logInfo['msg'] .= ". {$mixedArg['msg']}";
else if (empty($logInfo['msg']) && !empty($mixedArg['msg'])) $logInfo['msg'] = $mixedArg['msg'];
if (!empty($mixedArg['msg'])) unset($mixedArg['msg']);
$logInfo['log'] = $mixedArg;
ob_start();
debug_print_backtrace();
$logInfo['trace'] = ob_get_clean();
}
if (!empty($logInfo['trace']) && 'Exception' !== $logInfo['type']) {// remove #0 and #1 (DBG::log and DBG::_log)
$pos = strpos($logInfo['trace'], "\n#2");
// if (false !== $pos) $logInfo['trace'] = substr($logInfo['trace'], $pos + 1); // TODO
}
$logInfo['trace'] = str_replace(APP_PATH_ROOT, 'SE', $logInfo['trace']);
$logInfo['trace'] .= (("\n" == substr($logInfo['trace'], -1)) ? '' : "\n") . "#URI: " . V::get('REQUEST_URI', '', $_SERVER);
if (!$logInfo['type']) $logInfo['type'] = 'unknown';
if (!empty($logInfo['trace'])) {
$trace = array_map(function ($part) {
if ('URI: ' === substr($part, 0, strlen('URI: '))) return "#{$part}";
if ($pos = strpos($part, '{closure}')) {
// #10 Route_Storage_AclStruct->{closure}(Array ([namespace] => default_db/BI_audit_ENERGA_PRAC ... )
return "#" . substr($part, 0, 200) . ( strlen($part) > 200 ? '...' : '' );
}
if ($pos = strrpos($part, ' called at [')) {
$spacePos = strpos($part, ' ');
$spacePos = (' ' === $part[$spacePos + 1]) ? $spacePos + 1 : $spacePos;
$spacePos = (' ' === $part[$spacePos + 1]) ? $spacePos + 1 : $spacePos;
$spacePos += 1;
$called = substr($part, $pos + strlen(' called at ['), -1);
$nr = substr($part, 0, $spacePos);
$line = substr($part, $spacePos, ($pos > 200) ? 200 : $pos - $spacePos);
if ($pos > 200) $line .= "...";
$line = str_replace("\n", '\\n', $line);
return "#{$nr}{$called}: {$line}";
}
return "#" . substr($part, 0, 200) . ( strlen($part) > 200 ? '...' : '' );
}, explode("\n#", $logInfo['trace']));
array_shift($trace);
$logInfo['trace'] = implode("\n", $trace);
}
error_log(
json_encode($logInfo) . "\n"
, 3
, $logFileName
);
}
public static function simpleLog($type, $msg) {
static $_idRequest = null;
$prefix = ('production' == V::get('P5_ENV', 'production', $_SERVER)) ? "" : "dev-";
$logFileName = "/tmp/{$prefix}se-log-{$type}.tsv.log";
if (null === $_idRequest) {
$_idRequest = substr(md5(date("Y-m-d") . Request::getUserIp() . V::get('REQUEST_TIME', '', $_SERVER)), 0, 6);
$firstMsg = V::get('REQUEST_METHOD', '', $_SERVER) . " " . V::get('REQUEST_URI', '', $_SERVER);
self::_simpleLogFile($firstMsg, $_idRequest, $logFileName);
self::_simpleLogFile("USER IP: " . Request::getUserIp(), $_idRequest, $logFileName);
}
self::_simpleLogFile($msg, $_idRequest, $logFileName);
}
public static function _simpleLogFile($msg, $idRequest, $logFileName) {
error_log(implode("\t", [
date("Y-m-d H:i:s"),
$idRequest,
$msg
]) . "\n", 3, $logFileName);
}
public static function isLogActiveByAdmin() {
static $_isLogActiveByAdmin = null;
if (null !== $_isLogActiveByAdmin) return $_isLogActiveByAdmin;
$_isLogActiveByAdmin = self::_isLogActiveByAdmin();
return $_isLogActiveByAdmin;
}
public static function _isLogActiveByAdmin() {
if (!User::logged()) return false;
if (!User::getID()) return false;
return self::hasUserDebug(User::getID());
}
public static function hasUserDebug($idUser, $idAdmin = null) {
$prefix = ('production' == V::get('P5_ENV', 'production', $_SERVER)) ? "" : "dev-";
if (!$idAdmin) {
$output = (int)@shell_exec("ls -1 /tmp/{$prefix}se-user_debug-{$idUser}-by_admin-*.log 2>/dev/null | wc -l");
return $output > 0;
}
return file_exists("/tmp/{$prefix}se-user_debug-{$idUser}-by_admin-{$idAdmin}.log");
}
public static function startUserDebug($idUser, $idAdmin) {
$prefix = ('production' == V::get('P5_ENV', 'production', $_SERVER)) ? "" : "dev-";
@touch("/tmp/{$prefix}se-user_debug-{$idUser}-by_admin-{$idAdmin}.log");
}
public static function stopUserDebug($idUser, $idAdmin) {
$prefix = ('production' == V::get('P5_ENV', 'production', $_SERVER)) ? "" : "dev-";
@unlink("/tmp/{$prefix}se-user_debug-{$idUser}-by_admin-{$idAdmin}.log");
}
}