/** * Send notification to admin if need * @param $fromCheckPointTime * @return bool */ private function _sendNotification($fromCheckPointTime) { if (!Yii::app()->params['csb']['notifyAdmin'] || empty(Yii::app()->params['csb']['adminEmail'])) { return; } // send log of blocked users for last period $lockedUsers = CsbLog::model()->findAll('create_time > :createTime AND type = :type', array(':createTime' => $fromCheckPointTime, ':type' => CsbLog::TYPE_LOCK)); if (!$lockedUsers) { $this->_verbose("There's no new locked IPs"); return false; } $notification = 'Some users were blocked. Check Time ' . date('Y-m-d H:i:s') . '<br /><br /> <table cellpadding="5px" cellspacing="5px"> <tr> <th>IP</th> <th>Block Time</th> <th>Block Till Time</th> <th>User</th> <th>Details</th> </tr>'; foreach ($lockedUsers as $row) { $notification .= '<tr> <td>' . long2ip($row->ip) . '</td> <td>' . $row->create_time . '</td> <td>' . $row->till_time . '</td> <td>' . $row->user_id . '</td> <td>' . $row->details . '</td> </tr>'; } $notification .= '</table>'; $mail = new Mail(); $mail->setSubject('Some users were blocked')->setBodyHtml($notification)->addTo(Yii::app()->params['csb']['adminEmail'])->send(); $this->_verbose("Email was sent to admin", null, self::VERBOSE_INFO); }
public function loadModel($id) { $model = CsbLog::model()->findByPk((int) $id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function testLongRequest() { $ip = '2.2.2.2'; // test 100 requests per 600 last seconds for ($i = 1; $i <= 100; $i++) { $request = new CsbRequest(); $request->ip = ip2long($ip); $request->time = date('Y-m-d H:i:s', strtotime("-" . $i * 5 . " second")); $request->save(); } CsbRequest::model()->checkLongIntervals(); // check logs $this->assertEquals(1, CsbIpInfo::model()->count('ip = :ip', array(':ip' => ip2long($ip)))); $this->assertEquals(1, CsbLog::model()->count('ip = :ip', array(':ip' => ip2long($ip)))); }
/** * @param $ip * @param $type * @param $tillTime * @param $details * @return mixed */ public function log($ip, $type, $tillTime, $details) { $ipInfo = CsbHttpBl::getInstance()->getHttpBlInfo($ip); $model = new CsbLog(); $model->setAttributes(array('ip' => ip2long($ip), 'type' => $type, 'create_time' => date('Y-m-d H:i:s'), 'till_time' => $tillTime, 'account_id' => Yii::app()->user->getId(), 'request_info' => CsbYaml::dump($this->_filterServerInfo()), 'ip_info' => CsbYaml::dump($ipInfo), 'details' => CsbYaml::dump($details))); $model->save(); return $model; }
public function isBlockIp($ip, $details = array()) { $visitorType = CsbHttpBl::getInstance()->getVisitorType($ip); // for now only check is it search engine if ($visitorType === CsbHttpBl::VISITOR_SEARCH_ENGINE) { $tillTime = date('Y-m-d H:i:s', strtotime("+ " . self::SEARCH_ENGINE_CACHE_TIME_DAYS . " days")); $this->addIpInfo($ip, self::TYPE_SEARCH_ENGINE, $tillTime); CsbLog::model()->log($ip, CsbLog::TYPE_SEARCH_ENGINE, $tillTime, $details); return false; } else { if (empty($details['block time'])) { throw new App_Exception_System("Param 'blockTime' is not set"); } $blockTillTime = date('Y-m-d H:i:s', strtotime("+ " . $details['block time'] . " second")); $this->addIpInfo($ip, self::TYPE_BLOCKED, $blockTillTime); CsbLog::model()->log($ip, CsbLog::TYPE_LOCK, $blockTillTime, $details); return true; } }