-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/** | ||
* SystemAccessLogService | ||
* | ||
* @version 7.6 | ||
* @package service | ||
* @subpackage log | ||
* @author Pablo Dall'Oglio | ||
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br) | ||
* @license https://adiantiframework.com.br/license-template | ||
*/ | ||
class SystemAccessLogService | ||
{ | ||
/** | ||
* Register login | ||
*/ | ||
public static function registerLogin( $impersonated = false, $impersonated_by = null ) | ||
{ | ||
$cur_conn = serialize(TTransaction::getDatabaseInfo()); | ||
$new_conn = serialize(TConnection::getDatabaseInfo('log')); | ||
|
||
$open_transaction = ($cur_conn !== $new_conn); | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::open('log'); | ||
} | ||
|
||
$object = new SystemAccessLog; | ||
$object->login = TSession::getValue('login'); | ||
$object->sessionid = session_id(); | ||
$object->login_time = date("Y-m-d H:i:s"); | ||
$object->login_year = date("Y"); | ||
$object->login_month = date("m"); | ||
$object->login_day = date("d"); | ||
$object->impersonated = ($impersonated ? 'Y' : 'N'); | ||
$object->impersonated_by = $impersonated_by; | ||
$object->access_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; | ||
$object->store(); | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::close(); | ||
} | ||
} | ||
|
||
/** | ||
* Register logout | ||
*/ | ||
public static function registerLogout() | ||
{ | ||
$cur_conn = serialize(TTransaction::getDatabaseInfo()); | ||
$new_conn = serialize(TConnection::getDatabaseInfo('log')); | ||
|
||
$open_transaction = ($cur_conn !== $new_conn); | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::open('log'); | ||
} | ||
|
||
// get logs by session id | ||
$logs = SystemAccessLog::where('sessionid', '=', session_id())->load(); | ||
if (count($logs)>0) | ||
{ | ||
$log = $logs[0]; | ||
if ($log instanceof SystemAccessLog); | ||
{ | ||
$log->logout_time = date("Y-m-d H:i:s"); | ||
} | ||
$log->store(); | ||
} | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::close(); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public static function getStatsByDay() | ||
{ | ||
TTransaction::open('log'); | ||
// get logs by session id | ||
$logs = SystemAccessLog::where('login_time', '>=', date('Y-m-01'))->where('login_time', '<=', date('Y-m-t'))->load(); | ||
$accesses = array(); | ||
|
||
if (count($logs)>0) | ||
{ | ||
$accesses = array(); | ||
foreach ($logs as $log) | ||
{ | ||
$day = substr($log->login_time,8,2); | ||
if (isset($accesses[$day])) | ||
{ | ||
$accesses[$day] ++; | ||
} | ||
else | ||
{ | ||
$accesses[$day] = 1; | ||
} | ||
} | ||
} | ||
|
||
TTransaction::close(); | ||
return $accesses; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
app/service/log/SystemAccessNotificationLogService.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
/** | ||
* SystemAccessNotificationLogService | ||
* | ||
* @version 7.6 | ||
* @package service | ||
* @subpackage log | ||
* @author Pablo Dall'Oglio | ||
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br) | ||
* @license https://adiantiframework.com.br/license-template | ||
*/ | ||
class SystemAccessNotificationLogService | ||
{ | ||
/** | ||
* Register login notification | ||
*/ | ||
public static function registerLogin() | ||
{ | ||
$ini = AdiantiApplicationConfig::get(); | ||
|
||
if (empty($ini['general']['notification_login'])) | ||
{ | ||
return; | ||
} | ||
|
||
$cur_conn = serialize(TTransaction::getDatabaseInfo()); | ||
$new_conn = serialize(TConnection::getDatabaseInfo('log')); | ||
|
||
$open_transaction = ($cur_conn !== $new_conn); | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::open('log'); | ||
} | ||
|
||
$object = new SystemAccessNotificationLog; | ||
$object->email = TSession::getValue('usermail'); | ||
$object->login = TSession::getValue('login'); | ||
$object->login_time = date("Y-m-d H:i:s"); | ||
$object->ip_address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; | ||
$object->store(); | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::close(); | ||
} | ||
} | ||
|
||
/** | ||
* Send email login notification | ||
* php cmd.php "class=SystemAccessNotificationLogService&method=sendNotificationLogin&static=1" | ||
*/ | ||
public static function sendNotificationLogin() | ||
{ | ||
try | ||
{ | ||
$ini = AdiantiApplicationConfig::get(); | ||
|
||
if (empty($ini['general']['notification_login'])) | ||
{ | ||
return; | ||
} | ||
|
||
TTransaction::open('log'); | ||
$objects = SystemAccessNotificationLog::getObjects(); | ||
TTransaction::close(); | ||
|
||
if (empty($objects)) | ||
{ | ||
return; | ||
} | ||
|
||
foreach($objects as $notification) | ||
{ | ||
try | ||
{ | ||
TTransaction::open('log'); | ||
|
||
(new TEmailValidator())->validate("E-mail {$notification->email} FROM {$notification->login}", $notification->email); | ||
|
||
$html = new THtmlRenderer('app/resources/system_access_notification.html'); | ||
$html->enableTranslation(); | ||
|
||
$title = $ini['general']['title']??'System'; | ||
|
||
$subject = _t('Login to your account'); | ||
$content = _t('You have just successfully logged in to ^1. If you do not recognize this login, contact technical support', $title); | ||
|
||
$html->enableSection( | ||
'main', | ||
[ | ||
'login' => $notification->login, | ||
'login_time' => $notification->login_time, | ||
'ip_address' => $notification->ip_address, | ||
'subject' => $subject, | ||
'content' => $content, | ||
] | ||
); | ||
|
||
MailService::send($notification->email, $subject, $html->getContents(), 'html'); | ||
|
||
$notification->delete(); | ||
TTransaction::close(); | ||
} | ||
catch (Exception $e) | ||
{ | ||
TTransaction::rollback(); | ||
echo $e->getMessage(); | ||
} | ||
} | ||
} | ||
catch(Exception $e) | ||
{ | ||
TTransaction::rollback(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
/** | ||
* SystemChangeLogService | ||
* | ||
* @version 7.6 | ||
* @package service | ||
* @subpackage log | ||
* @author Pablo Dall'Oglio | ||
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br) | ||
* @license https://adiantiframework.com.br/license-template | ||
*/ | ||
class SystemChangeLogService | ||
{ | ||
/** | ||
* Register a change log | ||
*/ | ||
public static function register($activeRecord, $lastState, $currentState, $operation = null) | ||
{ | ||
$table = $activeRecord->getEntity(); | ||
$pk = $activeRecord->getPrimaryKey(); | ||
|
||
$created_col = $activeRecord->getCreatedAtColumn(); | ||
$updated_col = $activeRecord->getUpdatedAtColumn(); | ||
$deleted_col = $activeRecord->getDeletedAtColumn(); | ||
|
||
$e = new Exception; | ||
$uniqid = TTransaction::getUniqId(); | ||
$cur_conn = serialize(TTransaction::getDatabaseInfo()); | ||
$new_conn = serialize(TConnection::getDatabaseInfo('log')); | ||
|
||
$open_transaction = ($cur_conn !== $new_conn); | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::open('log'); | ||
} | ||
|
||
foreach ($lastState as $key => $value) | ||
{ | ||
if (!in_array($key, array_keys($currentState)) && !in_array($key, [$created_col, $updated_col, $deleted_col]) && ( (string) $value !== '') && ($operation == 'delete')) | ||
{ | ||
// deleted | ||
$log = new SystemChangeLog; | ||
$log->tablename = $table; | ||
$log->logdate = date('Y-m-d H:i:s'); | ||
$log->log_year = date('Y'); | ||
$log->log_month = date('m'); | ||
$log->log_day = date('d'); | ||
$log->login = TSession::getValue('login'); | ||
$log->primarykey = $pk; | ||
$log->pkvalue = $activeRecord->$pk; | ||
$log->operation = 'deleted'; | ||
$log->columnname = $key; | ||
$log->oldvalue = (string) $value; | ||
$log->newvalue = ''; | ||
$log->access_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; | ||
$log->transaction_id = $uniqid; | ||
$log->log_trace = $e->getTraceAsString(); | ||
$log->session_id = session_id(); | ||
$log->class_name = isset($_REQUEST['class']) ? $_REQUEST['class'] : ''; | ||
$log->php_sapi = php_sapi_name(); | ||
$log->store(); | ||
} | ||
} | ||
|
||
foreach ($currentState as $key => $value) | ||
{ | ||
if (isset($lastState[$key]) && ($value != $lastState[$key]) && !in_array($key, [$created_col, $updated_col, $deleted_col])) | ||
{ | ||
// changed | ||
$log = new SystemChangeLog; | ||
$log->tablename = $table; | ||
$log->logdate = date('Y-m-d H:i:s'); | ||
$log->log_year = date('Y'); | ||
$log->log_month = date('m'); | ||
$log->log_day = date('d'); | ||
$log->login = TSession::getValue('login'); | ||
$log->primarykey = $pk; | ||
$log->pkvalue = $activeRecord->$pk; | ||
$log->operation = 'changed'; | ||
$log->columnname = $key; | ||
$log->oldvalue = (string) $lastState[$key]; | ||
$log->newvalue = (string) is_scalar($value) ? $value : ( (is_null($value) ? NULL : serialize($value)) ); | ||
$log->access_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; | ||
$log->transaction_id = $uniqid; | ||
$log->log_trace = $e->getTraceAsString(); | ||
$log->session_id = session_id(); | ||
$log->class_name = isset($_REQUEST['class']) ? $_REQUEST['class'] : ''; | ||
$log->php_sapi = php_sapi_name(); | ||
$log->store(); | ||
} | ||
|
||
if (!isset($lastState[$key]) && !empty($value) && !in_array($key, [$created_col, $updated_col, $deleted_col])) | ||
{ | ||
// created | ||
$log = new SystemChangeLog; | ||
$log->tablename = $table; | ||
$log->logdate = date('Y-m-d H:i:s'); | ||
$log->log_year = date('Y'); | ||
$log->log_month = date('m'); | ||
$log->log_day = date('d'); | ||
$log->login = TSession::getValue('login'); | ||
$log->primarykey = $pk; | ||
$log->pkvalue = $activeRecord->$pk; | ||
$log->operation = 'created'; | ||
$log->columnname = $key; | ||
$log->oldvalue = ''; | ||
$log->newvalue = (string) is_scalar($value) ? $value : serialize($value); | ||
$log->access_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; | ||
$log->transaction_id = $uniqid; | ||
$log->log_trace = $e->getTraceAsString(); | ||
$log->session_id = session_id(); | ||
$log->class_name = isset($_REQUEST['class']) ? $_REQUEST['class'] : ''; | ||
$log->php_sapi = php_sapi_name(); | ||
$log->store(); | ||
} | ||
} | ||
|
||
if ($open_transaction) | ||
{ | ||
TTransaction::close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.