Files
selima-perl/lib/php/monica/actlog.inc.php
2026-03-10 21:31:43 +08:00

88 lines
2.3 KiB
PHP

<?php
// File name: actlog.inc.php
// Description: PHP subroutine to handle activity log
// Date: 2002-04-17
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2002-2013 Pristine Communications
// Set the include path
if (!defined("INCPATH_SET")) {
require_once dirname(__FILE__) . "/incpath.inc.php";
}
// Referenced subroutines
require_once "monica/cgiemu.inc.php";
require_once "monica/login.inc.php";
require_once "monica/requri.inc.php";
// Settings
if (!defined("_ACTLOG_DONE_FINDING")) {
_actlog_find_actlog_txt();
define("_ACTLOG_DONE_FINDING", true);
}
// actlog: Log an activity
function actlog($message, $user = null)
{
// ACTLOG must be defined first
if (!defined("ACTLOG")) {
trigger_error("Activity log actlog.txt not found", E_USER_ERROR);
}
$date = date("Y-m-d H:i:s O");
$script = REQUEST_PATH;
$remote = $_SERVER["REMOTE_ADDR"];
if (is_null($user)) {
$user = get_login_id();
if (is_null($user)) {
$user = "anonymous";
}
}
// Escape control characters for safety
$message = str_replace("\t", "\\t", $message);
$message = str_replace("\r", "\\r", $message);
$message = str_replace("\n", "\\n", $message);
$message = preg_replace_callback("/([\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F])/",
function ($matches) {
return sprintf("\\x%02x", ord($matches[1]));
}, $message);
$fp = fopen(ACTLOG, "a");
flock($fp, LOCK_EX);
fwrite($fp, "[$date] $script: ($user from $remote) $message\n");
flock($fp, LOCK_UN);
fclose($fp);
return;
}
// _actlog_find_actlog_txt: Find the activity log file
function _actlog_find_actlog_txt()
{
// It is set
if (defined("ACTLOG")) {
return;
}
// The file location candidates
$cands = array();
if (defined("PACKAGE")) {
$cands[] = "/var/log/apache2/" . PACKAGE . "/actlog.txt";
}
$cands[] = "/var/log/apache2/actlog.txt";
// Find the first available one
$actlog = null;
foreach ($cands as $file) {
if (file_exists($file) && is_file($file) && is_writable($file)) {
$actlog = $file;
break;
}
}
if (!is_null($actlog)) {
define("ACTLOG", $actlog);
}
return;
}
?>