| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\manager;
- use \MGModule\DNSManager2\models\custom\globalsetting;
- class BlockerHelper {
-
- public function __construct() {
-
- }
-
- protected function startsWith($haystack, $needle) {
- return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
- }
- protected function endsWith($haystack, $needle) {
- return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
- }
-
- protected function preparePattern($pattern) {
- $pattern = str_replace('*', '', $pattern);
- $pattern = str_replace('.', '\.', $pattern);
- return $pattern;
- }
-
- public function isLoggedUserExcluded() {
- $excludedClients = globalsetting\GlobalSetting::byKey('blocked_strings_excluded_clients');
-
- if(empty($excludedClients)) {
- return false;
- }
-
- $excludedClients = explode(',', $excludedClients);
-
- return in_array($_SESSION['uid'], $excludedClients);
- }
-
- public function isRecordBlocked($pattern, $subject) {
- if($pattern === "")
- {
- return;
- }
- $isBlocked = false;
- if(strpos($pattern, '*') !== false) {
- if($this->endsWith($pattern, '*')) {
- $isBlocked = preg_match(sprintf('/^%s/', $this->preparePattern($pattern)), $subject);
- }
- else if($this->startsWith($pattern, '*')) {
- $isBlocked = preg_match(sprintf('/%s$/', $this->preparePattern($pattern)), $subject);
- }
- else
- {
- list($exp1, $exp2) = explode('*', $pattern);
- $isBlocked = preg_match(sprintf('/\b%s(.*)%s\b/', $exp1, $exp2), $subject);
- }
- }
- else if(strpos($pattern, '.') !== false) {
- if($this->endsWith($pattern, '.') || $this->startsWith($pattern, '.')) {
- if(strpos($subject, $pattern) !== false) {
- $isBlocked = true;
- }
- }
- else
- {
- $isBlocked = preg_match(sprintf('/\b%s\b/', $pattern), $subject);
- }
- }
- else {
- $isBlocked = preg_match(sprintf('/\b%s\b/', $pattern), $subject);
- }
- return $isBlocked;
- }
-
- public function getRules($rDNS = false){
- $useRdnsSameAsDns = globalsetting\GlobalSetting::byKey('use_rdns_blocked_same_as_for_dns');
- if($useRdnsSameAsDns != 'on' && $rDNS === true) {
- $settings = globalsetting\GlobalSetting::byKey('rdns_blocked_strings');
- }
- else {
- $settings = globalsetting\GlobalSetting::byKey('dns_blocked_strings');
- }
- return $this->multipleExplode([PHP_EOL, ', ', ',', ' '], $settings->value);
- }
-
- protected function multipleExplode($delimiters = [], $strings){
- if(in_array(PHP_EOL, $delimiters)){
- unset($delimiters[array_search(PHP_EOL, $delimiters)]);
- }
-
- $readyString = str_replace($delimiters, PHP_EOL, $strings);
- if($readyString == ''){
- return;
- }
- return explode(PHP_EOL, $readyString);
- }
- }
|