/** * @param \Magento\Framework\HTTP\Adapter\Curl $subject * @param $result * @return mixed */ public function afterRead(\Magento\Framework\HTTP\Adapter\Curl $subject, $result) { try { /* @var $curlLog \Foggyline\Sentinel\Model\CurlLog */ $curlLog = $this->curlLog->create(); $curlLog->setRequestId($this->helper->getHttpRequestUniqueId()); $curlLog->setResult($result); $curlLog->setMethod($this->cUrlMethod); $curlLog->setUrl($this->cUrlUrl); $curlLog->setHttpVer($this->cUrlHttpVer); $curlLog->setHeaders(serialize($this->cUrlHeaders)); $curlLog->setBody($this->cUrlBody); $curlLog->setHttpCode($subject->getInfo(CURLINFO_HTTP_CODE)); $curlLog->setTotalTime($subject->getInfo(CURLINFO_TOTAL_TIME)); $curlLog->setNameLookupTime($subject->getInfo(CURLINFO_NAMELOOKUP_TIME)); $curlLog->setPrimaryIp($subject->getInfo(CURLINFO_PRIMARY_IP)); $curlLog->setPrimaryPort($subject->getInfo(CURLINFO_PRIMARY_PORT)); $curlLog->setLocalIp($subject->getInfo(CURLINFO_LOCAL_IP)); $curlLog->setLocalPort($subject->getInfo(CURLINFO_LOCAL_PORT)); $curlLog->setSizeUpload($subject->getInfo(CURLINFO_SIZE_UPLOAD)); $curlLog->setSizeDownload($subject->getInfo(CURLINFO_SIZE_DOWNLOAD)); $curlLog->setSpeedUpload($subject->getInfo(CURLINFO_SPEED_UPLOAD)); $curlLog->setSpeedDownload($subject->getInfo(CURLINFO_SPEED_DOWNLOAD)); $curlLog->setContentType($subject->getInfo(CURLINFO_CONTENT_TYPE)); $curlLog->save(); } catch (\Exception $e) { $this->logger->critical($e); } return $result; }
private function logFailedCustomerAuthentication($username, $password) { try { /* @var $loginLog \Foggyline\Sentinel\Model\LoginLog */ $loginLog = $this->loginLogFactory->create(); $loginLog->setIdentifier($username); $loginLog->setRequestId($this->helper->getHttpRequestUniqueId()); $loginLog->setType(\Foggyline\Sentinel\Model\LoginLog::TYPE_CUSTOMER); $loginLog->setLoginStatus(\Foggyline\Sentinel\Model\LoginLog::LOGIN_STATUS_FAIL); $loginLog->save(); } catch (Exception $e) { $this->logger->critical($e); } }
/** * Cleanup old logs so that the database table foggyline_sentinel_log does not get cluttered. * * @return $this */ public function cleanup() { try { /* Here we do a fast cleanup, directly on database, no individual objects */ $connection = $this->resource->getConnection(); $connection->beginTransaction(); $condition = ['created_at < (NOW() - INTERVAL ? HOUR)' => $this->helper->getCleanAfterHours()]; $connection->delete($this->resource->getTableName('foggyline_sentinel_log'), $condition); $connection->commit(); $this->logger->info('Cron job foggyline_sentinel_cleanup executed'); } catch (\Exception $e) { $connection->rollBack(); $this->logger->critical($e); } return $this; }
/** * As per http://php.net/manual/en/language.oop5.decon.php: * The destructor method will be called as soon as there are no other references to a particular object, * or in any order during the shutdown sequence. * * As randomly tested accross the code, triggering exist and exceptions, the __destruct is called in cases of: * - successful execution * - exit() being called anywhere later on * - exception being thrown anywhere later on * * This is a nice example of catching all query logs, which we populated first within the logStats method. * * Within the __destruct we initiate the new Database connection, from DB parameters read from config. * This new DB connection is using the DB\Logger\Quiet so we don't fall into a loop of logging the * SQL queries for SQL query logger :) * * This way, we agregated all of the individual query logs on request, and did one insertMultiple. */ public function __destruct() { try { /** * Note, logStats still executes. It is not possible to include DB read config in there as it executes * right after first DB connection, and Magento does not have config value just yet. * Thus we check here. Hopefully the performance impact is not too big within the logStats. */ if (!$this->helper->isQueryLogActive()) { return; } $config = $this->deploymentConfig->get(\Magento\Framework\Config\ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT); $connection = new \Magento\Framework\Model\ResourceModel\Type\Db\Pdo\Mysql(new \Magento\Framework\Stdlib\StringUtils(), new \Magento\Framework\Stdlib\DateTime(), $config); /* * Here we init the new DB connection with * Magento config and directly set the dummy Quiet logger to it. */ $connection = $connection->getConnection(new \Magento\Framework\DB\Logger\Quiet()); $uniqueId = $this->helper->getHttpRequestUniqueId(); $logCallStack = $this->helper->getQueryLogCallStack(); $logQueryTime = $this->helper->getQueryLogQueryTime(); $logAllQueries = $this->helper->getQueryLogAllQueries(); $logQueryChunks = $this->helper->getQueryLogQueryChunks(); $this->queryLogs = array_map(create_function('$arr', '$arr["request_id"] = "' . $uniqueId . '"; return $arr;'), $this->queryLogs); $queryLogChunks = array_chunk($this->queryLogs, $logQueryChunks); /* Breaks in between 80 & 90 */ foreach ($queryLogChunks as $queryLogChunk) { if (!$logCallStack) { $queryLogChunk = array_map(create_function('$arr', 'unset($arr["backtrace"]); return $arr;'), $queryLogChunk); } $queryLogChunk = array_map(create_function('$arr', 'unset($arr["backtrace"]); return $arr;'), $queryLogChunk); if ($logQueryTime && !$logAllQueries) { $queryLogChunk = array_map(create_function('$arr', 'if($arr["time"] >= ' . $logQueryTime . ') { return $arr; }'), $queryLogChunk); } /* @todo Exclude the foggyline_sentinel_query_log table from logging itself */ /* $queryLogChunk = array_map(create_function('$arr', 'if (!strstr($arr["sql"], "foggyline_sentinel_query_log")) { return $arr; }'), $queryLogChunk); */ if ($queryLogChunk = array_filter($queryLogChunk)) { /* @todo There is a bug here, DB name is without possible prefix/suffix, needs proper handling */ $connection->insertMultiple('foggyline_sentinel_query_log', $queryLogChunk); } } /** CREATE TABLE `foggyline_sentinel_query_log` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `type` text, `time` decimal(12,4) DEFAULT NULL, `sql` text, `bind` text, `row_count` int(11) DEFAULT NULL, `request_id` text, `backtrace` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8; */ } catch (\Exception $e) { //Silently do nothing :) //var_dump($e->getMessage()); } }
/** * Observer for controller_front_send_response_before event fired in lib/internal/Magento/Framework/App/Http.php. * * $eventParams = ['request' => $this->_request, 'response' => $this->_response]; * * request => Magento\Framework\App\Request\Http * response => Magento\Framework\Webapi\Rest\Response\Proxy * * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { /* @var $request \Magento\Framework\App\Request\Http */ $request = $observer->getEvent()->getRequest(); $areaCode = $this->areaList->getCodeByFrontName($request->getFrontName()); /** * Stop execution if module is not active * or if the current request falls within excluded areas * or if the current request falls within excluded actions */ if (!$this->helper->isFoggylineSentinelActive() or in_array($areaCode, $this->helper->getExcludeAreas()) or in_array($request->getFullActionName(), $this->helper->getExcludeActions())) { //return $this; } /* @var $log \Foggyline\Sentinel\Model\AccessLog */ $log = $this->log->create(); $log->setRequestId($this->helper->getHttpRequestUniqueId()); if ($this->authSession && $this->authSession->getUser()) { $user = $this->authSession->getUser(); $log->setUserId($user->getId()); $log->setUserUsername($user->getUserName()); $log->setUserEmail($user->getEmail()); $log->setUserName($user->getName()); } $log->setActionName($request->getActionName()); $log->setFullActionName($request->getFullActionName()); $log->setClientIp($request->getClientIp()); $log->setRequestString($request->getRequestString()); $log->setRequestMethod($request->getMethod()); $log->setHttpGetParams($_GET); $log->setHttpPostParams($_POST); $log->setHttpFilesParams($_FILES); $log->setModuleName($request->getModuleName()); $log->setControllerModule($request->getControllerModule()); $log->setArea($areaCode); try { $log->save(); } catch (\Exception $e) { $this->logger->critical($e); } return $this; }