Application->ConfigValue('UseChangeLog')) { // don't use session log when change log is disabled return ; } $object = $this->Application->recallObject($event->Prefix, null, Array ('skip_autoload' => 1)); /* @var $object kDBItem */ $fields_hash = Array ( 'SessionStart' => time(), 'IP' => $this->Application->getClientIp(), 'PortalUserId' => $this->Application->RecallVar('user_id'), 'SessionId' => $this->Application->GetSID(), 'Status' => SESSION_LOG_ACTIVE, ); $object->SetDBFieldsFromHash($fields_hash); $object->UpdateFormattersSubFields(); if ($object->Create()) { $this->Application->StoreVar('_SessionLogId_', $object->GetID()); } } /** * Closes log for current session * * @param kEvent $event */ function OnEndSession($event) { $object = $this->Application->recallObject($event->Prefix, null, Array ('skip_autoload' => 1)); /* @var $object kDBItem */ $object->Load($this->Application->RecallVar('_SessionLogId_')); if (!$object->isLoaded()) { return ; } $fields_hash = Array ( 'SessionEnd' => time(), 'Status' => SESSION_LOG_LOGGED_OUT, ); $object->SetDBFieldsFromHash($fields_hash); $object->UpdateFormattersSubFields(); $object->Update(); } /** * Apply custom processing to item * * @param kEvent $event * @param string $type * @return void * @access protected */ protected function customProcessing(kEvent $event, $type) { if ( $event->Name == 'OnMassDelete' && $type == 'before' ) { $ids = $event->getEventParam('ids'); if ( $ids ) { $config = $event->getUnitConfig(); $id_field = $config->getIDField(); $sql = 'SELECT ' . $id_field . ' FROM ' . $config->getTableName() . ' WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ') AND Status <> ' . SESSION_LOG_ACTIVE; $allowed_ids = $this->Conn->GetCol($sql); $event->setEventParam('ids', $allowed_ids); } } } /** * Delete changes, related to deleted session * * @param kEvent $event * @return void * @access protected */ protected function OnAfterItemDelete(kEvent $event) { parent::OnAfterItemDelete($event); $object = $event->getObject(); /* @var $object kDBItem */ $change_logs_config = $this->Application->getUnitConfig('change-log'); $sql = 'SELECT ' . $change_logs_config->getIDField() . ' FROM ' . $change_logs_config->getTableName() . ' WHERE SessionLogId = ' . $object->GetID(); $related_ids = $this->Conn->GetCol($sql); if ( $related_ids ) { $temp_handler = $this->Application->recallObject('change-log_TempHandler', 'kTempTablesHandler', Array ('parent_event' => $event->MasterEvent)); /* @var $temp_handler kTempTablesHandler */ $temp_handler->DeleteItems('change-log', '', $related_ids); } } /** * [SCHEDULED TASK] Will remove old session logs * * @param kEvent $event Event. * * @return void */ protected function OnRotate(kEvent $event) { $rotation_interval = (int)$this->Application->ConfigValue('SessionLogRotationInterval'); if ( $rotation_interval === -1 ) { // Forever. return; } $session_log_temp_handler = $this->getTempTablesHandler($event->getPrefixSpecial(), $event); $change_log_temp_handler = $this->getTempTablesHandler('change-log', $event); $limit = 100; $session_log_config = $event->getUnitConfig(); $session_log_select_sql = ' SELECT ' . $session_log_config->getIDField() . ' FROM ' . $session_log_config->getTableName() . ' WHERE ' . TIMENOW . ' - SessionEnd > ' . $rotation_interval . ' LIMIT 0,' . $limit; $change_log_config = $this->Application->getUnitConfig('change-log'); do { $session_log_ids = $this->Conn->GetCol($session_log_select_sql); if ( !$session_log_ids ) { break; } $change_log_select_sql = ' SELECT ' . $change_log_config->getIDField() . ' FROM ' . $change_log_config->getTableName() . ' WHERE SessionLogId IN (' . implode(',', $session_log_ids) . ') LIMIT 0,' . $limit; do { $change_log_ids = $this->Conn->GetCol($change_log_select_sql); if ( $change_log_ids ) { $change_log_temp_handler->DeleteItems('change-log', '', $change_log_ids); } } while ( count($change_log_ids) == $limit ); $session_log_temp_handler->DeleteItems($event->Prefix, $event->Special, $session_log_ids); } while ( count($session_log_ids) == $limit ); } /** * Get temp tables handler instance. * * @param string $prefix_special Prefix, special. * @param kEvent $event Event. * * @return kTempTablesHandler */ public function getTempTablesHandler($prefix_special, kEvent $event) { return $this->Application->recallObject( $prefix_special . '_TempHandler', 'kTempTablesHandler', array('parent_event' => $event) ); } }