<?php
require_once($INC . "/util.inc.php");
require_once($INC . "/cache.inc.php");
if (!isset($rg_log_dir))
$rg_log_dir = "/var/log/rocketgit";
if (!isset($rg_log_file))
$rg_log_file = $rg_log_dir . "/err-fallback.log";
$rg_log_fd = FALSE;
if (empty($rg_log_sid))
$rg_log_sid = rg_id(6);
$rg_log_last_date = "";
$rg_log_level = 0;
/*
* Allow forcing sid, so we can spread the logs to multiple file
* or to supress the output of it.
*/
function rg_log_set_sid($v)
{
global $rg_log_sid;
$rg_log_sid = $v;
}
function rg_log_set_file($file)
{
global $rg_log_file;
global $rg_log_fd;
$rg_log_file = $file;
if ($rg_log_fd !== FALSE) {
fclose($rg_log_fd);
$rg_log_fd = FALSE;
}
}
function rg_log($str)
{
global $rg_log_level;
global $rg_log_file;
global $rg_log_fd;
global $rg_log_sid;
global $rg_log_last_date;
$date = gmdate("Ymd");
if ($rg_log_fd === FALSE) {
$_pi = pathinfo($rg_log_file);
$lf = $_pi['dirname'] . "/" . $_pi['filename']
. "-" . $date . "." . $_pi['extension'];
$rg_log_fd = fopen($lf, "a");
if ($rg_log_fd === FALSE)
return;
// Set correct rights
chmod($lf, 0600);
// write an empty line
fwrite($rg_log_fd, "\n");
$rg_log_last_date = $date;
$rg_log_last_sid = $rg_log_sid;
} else if (strcmp($rg_log_sid, "000000") == 0) { // we reopen log only if sid=000000 (long running tasks)
if ((strcmp($date, $rg_log_last_date) != 0)) {
// date changed, we need to reopen the log file
fclose($rg_log_fd);
$rg_log_fd = FALSE;
return rg_log($str);
}
}
// We insert some spaces based on backtrace len
$spaces = "";
for ($i = 0; $i < $rg_log_level; $i++)
$spaces .= ' ';
$t = gettimeofday();
$buf0 = gmdate('H:i:s', $t['sec']) . '.'
. sprintf('%03u', $t['usec'] / 1000);
if (strcmp($rg_log_sid, '000000') != 0)
$buf0 .= ' ' . $rg_log_sid;
$buf = '';
$str2 = preg_replace_callback('/[^\pL\pN\pP\pS \t]/uU',
'rg_callback_hexa', $str);
if ($str2 === NULL) // try without utf-8
$str2 = preg_replace_callback('/[^\x20-\x7e]/U',
'rg_callback_hexa', $str);
if ($str2 === NULL)
$str2 = 'could not log data';
$buf .= $buf0 . ' ' . $spaces . $str2 . "\n";
fwrite($rg_log_fd, $buf);
}
/*
* Log a multiline string
*/
function rg_log_ml($str)
{
if (empty($str))
return;
$off = 0;
while (1) {
$pos = strpos($str, "\n", $off);
if ($pos === FALSE) {
rg_log(substr($str, $off));
break;
}
rg_log(substr($str, $off, $pos - $off));
$off = $pos + 1;
}
}
/*
* This function will log fatal errors.
*/
$rg_error_core_seen = array();
function rg_error_core($msg)
{
global $rg_log_dir;
global $rg_log_sid;
global $rg_admin_email;
global $rg_error_core_seen;
rg_log($msg);
$key = md5($msg);
if (isset($rg_error_core_seen[$key]))
return;
$me = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : "?";
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '?';
$dir = $rg_log_dir;
// First, log small backtrace, then full backtrace
$_a = array(DEBUG_BACKTRACE_IGNORE_ARGS, 0);
foreach ($_a as $_v) {
$bt = print_r(debug_backtrace($_v), TRUE);
$r = @file_put_contents($dir . "/err-" . $key,
'Script: ' . $me . "\n"
. 'log_sid: ' . $rg_log_sid . "\n"
. 'Date: ' . gmdate('Y-m-d H:i:s') . "\n"
. 'IP: ' . $ip . "\n"
. $bt . "\n",
FILE_APPEND);
if ($r === FALSE)
return;
chmod($dir . "/err-" . $key, 0600);
}
$rg_error_core_seen[$key] = 1;
}
/*
* This function is run from cron and will signal pending fatal errors.
*/
function rg_log_cron()
{
global $rg_log_dir;
global $rg_admin_email;
global $rg_state_dir;
$nd = rg_dir_load_pattern($rg_log_dir, "err-.*");
if ($nd === FALSE)
$ncount = "?";
else
$ncount = count($nd);
if ($ncount == 0)
return;
// Do not send less than 1 per 4 hours
$last_ts = 0;
if (file_exists($rg_state_dir . '/tmp/log_cron_last'))
$last_ts = @filemtime($rg_state_dir . '/tmp/log_cron_last');
if ($last_ts + 4 * 3600 > time())
return 0;
mail($rg_admin_email,
$ncount . ' errors are waiting...',
'Please forward the content of /var/log/rocketgit/err-*'
. ' to RocketGit developers at rg-bugs@embedromix.ro.\n\n'
. 'Thank you!\n\n',
'Content-Type: text/plain;charset=UTF-8',
"-f $rg_admin_email");
file_put_contents($rg_state_dir . '/tmp/log_cron_last', time());
}
function rg_fatal($msg)
{
rg_error_core("FATAL: $msg");
exit(1);
}
$rg_internal_error_last = '';
function rg_internal_error($msg)
{
global $rg_internal_error_last;
$rg_internal_error_last = $msg;
rg_error_core("Internal error: $msg");
}
function rg_security_violation_no_exit($msg)
{
// We do not report function tests violations
$ra = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$sa = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '';
if (!empty($ra) && (strcmp($ra, $sa) == 0))
return;
rg_error_core("Security violation: $msg");
}
function rg_security_violation($msg)
{
rg_security_violation_no_exit($msg);
exit(1);
}
function rg_log_enter($a)
{
global $rg_log_level;
rg_log($a);
$rg_log_level++;
}
function rg_log_ml_enter($a)
{
global $rg_log_level;
rg_log_ml($a);
$rg_log_level++;
}
function rg_log_exit()
{
global $rg_log_level;
if ($rg_log_level == 0)
rg_internal_error('rg_log_exit called with level 0');
$rg_log_level--;
}
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master