Skip to content

Commit

Permalink
Merge pull request #127 from matomo-org/spice-psr
Browse files Browse the repository at this point in the history
Making PHPCS changes
  • Loading branch information
snake14 authored Oct 20, 2024
2 parents ccb3209 + a799903 commit fa18674
Show file tree
Hide file tree
Showing 38 changed files with 204 additions and 80 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: PHPCS check

on: pull_request

permissions:
actions: read
checks: read
contents: read
deployments: none
issues: read
packages: none
pull-requests: read
repository-projects: none
security-events: none
statuses: read

jobs:
phpcs:
name: PHPCS
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: false
persist-credentials: false
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: cs2pr
- name: Install dependencies
run:
composer init --name=matomo/trackingspamprevention --quiet;
composer --no-plugins config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -n;
composer config repositories.matomo-coding-standards vcs https://github.com/matomo-org/matomo-coding-standards -n;
composer require matomo-org/matomo-coding-standards:dev-master;
composer install --dev --prefer-dist --no-progress --no-suggest
- name: Check PHP code styles
id: phpcs
run: ./vendor/bin/phpcs --report-full --standard=phpcs.xml --report-checkstyle=./phpcs-report.xml
- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs.outcome == 'failure' }}
run: cs2pr ./phpcs-report.xml --prepend-filename
2 changes: 1 addition & 1 deletion AllowListIpRange.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -33,5 +34,4 @@ public function isAllowed($ip)

return false;
}

}
19 changes: 10 additions & 9 deletions BanIpNotificationEmail.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -9,7 +10,6 @@
namespace Piwik\Plugins\TrackingSpamPrevention;

use Piwik\Common;
use Piwik\Config;
use Piwik\Log;
use Piwik\Mail;
use Piwik\Piwik;
Expand All @@ -27,15 +27,17 @@ public function send($ipRange, $ip, $email, $maxActionsAllowed, $locationData, $
$mail->addTo($email);
$mail->setSubject('An IP was banned as too many actions were tracked.');
$mail->setDefaultFromPiwik();
if (empty($mail->getFromName()) || in_array($mail->getFromName(), [
if (
empty($mail->getFromName()) || in_array($mail->getFromName(), [
'CoreHome_WebAnalyticsReports',
'TagManager_MatomoTagName'
])) {
])
) {
$mail->setFrom($mail->getFrom(), 'Web Analytics Reports');
}

$mailBody = 'This is for your information. The following IP was banned because visit tried to track more than ' . Common::sanitizeInputValue($maxActionsAllowed) . ' actions:';
$mailBody .= PHP_EOL.PHP_EOL.'"' . Common::sanitizeInputValue($ipRange) . '"'.PHP_EOL;
$mailBody .= PHP_EOL . PHP_EOL . '"' . Common::sanitizeInputValue($ipRange) . '"' . PHP_EOL;
$instanceId = SettingsPiwik::getPiwikInstanceId();


Expand All @@ -58,12 +60,12 @@ public function send($ipRange, $ip, $email, $maxActionsAllowed, $locationData, $
}

if (!empty($instanceId)) {
$mailBody .= PHP_EOL.'Instance ID: ' . Common::sanitizeInputValue($instanceId);
$mailBody .= PHP_EOL . 'Instance ID: ' . Common::sanitizeInputValue($instanceId);
}
$mailBody .= PHP_EOL.'Current date (UTC): ' . Common::sanitizeInputValue($nowDateTime) . '
$mailBody .= PHP_EOL . 'Current date (UTC): ' . Common::sanitizeInputValue($nowDateTime) . '
IP as detected in header: ' . Common::sanitizeInputValue($ip) . '
GET request info: ' . json_encode($get) . '
POST request info: ' . json_encode($post). PHP_EOL;
POST request info: ' . json_encode($post) . PHP_EOL;

if (!empty($locationData)) {
$mailBody .= 'Geo IP info: ' . json_encode($locationData) . PHP_EOL;
Expand All @@ -82,9 +84,8 @@ public function send($ipRange, $ip, $email, $maxActionsAllowed, $locationData, $
$mail->send();
}

$a=$mail->getBodyText();
$a = $mail->getBodyText();

return $mail->getBodyText();
}

}
2 changes: 1 addition & 1 deletion BlockedGeoIp.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -75,5 +76,4 @@ public function isExcludedProvider($ip, $language)

return false;
}

}
7 changes: 4 additions & 3 deletions BlockedIpRanges.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -19,7 +20,7 @@

class BlockedIpRanges
{
const OPTION_KEY = 'TrackingSpamBlockedIpRanges';
public const OPTION_KEY = 'TrackingSpamBlockedIpRanges';

/**
* @var IpRangeProviderInterface[]
Expand Down Expand Up @@ -155,7 +156,8 @@ public function banIp($ip)
return $ranges;
}

public function unsetAllIpRanges() {
public function unsetAllIpRanges()
{
$this->setBlockedRanges([]);
}

Expand Down Expand Up @@ -190,5 +192,4 @@ public function updateBlockedIpRanges()
}
$this->setBlockedRanges($indexedRange);
}

}
18 changes: 15 additions & 3 deletions BlockedIpRanges/Aws.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -16,8 +17,20 @@ public function getRanges(): array
{
$ranges = [];

$aws = Http::sendHttpRequest('https://ip-ranges.amazonaws.com/ip-ranges.json', 120, null, null, 0,
false, false, false, 'GET', null, null, false);
$aws = Http::sendHttpRequest(
'https://ip-ranges.amazonaws.com/ip-ranges.json',
120,
null,
null,
0,
false,
false,
false,
'GET',
null,
null,
false
);

if (empty($aws)) {
throw new \Exception('Failed to retrieve AWS IP ranges');
Expand Down Expand Up @@ -52,5 +65,4 @@ public function getRanges(): array

return $ranges;
}

}
12 changes: 8 additions & 4 deletions BlockedIpRanges/Azure.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -41,7 +42,7 @@ public function getRanges(): array
$ranges[] = $ip;
}
}

return $ranges;
}

Expand All @@ -61,8 +62,12 @@ public function getDownloadUrl()
$prefixStrLen = mb_strlen($prefixUrl, 'UTF-8');
$posStart = mb_strpos($contentDownloadPage, $prefixUrl . 'https://download.microsoft.com/download/', 0, 'UTF-8');
$posEnd = mb_strpos($contentDownloadPage, '.json"', $posStart + $prefixStrLen, 'UTF-8'); // we don't want to match the " in href="
$contentDownloadPage = mb_substr($contentDownloadPage, $posStart + $prefixStrLen,
$posEnd - $posStart - $prefixStrLen, 'UTF-8');
$contentDownloadPage = mb_substr(
$contentDownloadPage,
$posStart + $prefixStrLen,
$posEnd - $posStart - $prefixStrLen,
'UTF-8'
);
$downloadUrl = trim($contentDownloadPage, '="' . "'") . '.json';
$downloadUrl = trim($downloadUrl);

Expand All @@ -76,5 +81,4 @@ public function getDownloadUrl()

return $downloadUrl;
}

}
2 changes: 1 addition & 1 deletion BlockedIpRanges/DigitalOcean.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -42,5 +43,4 @@ public function getRanges(): array

return $ranges;
}

}
3 changes: 1 addition & 2 deletions BlockedIpRanges/ExceptionRange.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -13,10 +14,8 @@
*/
class ExceptionRange implements IpRangeProviderInterface
{

public function getRanges(): array
{
throw new \Exception('Failed to get any range');
}

}
2 changes: 1 addition & 1 deletion BlockedIpRanges/Gcloud.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -47,5 +48,4 @@ public function getRanges(): array

return $ranges;
}

}
2 changes: 1 addition & 1 deletion BlockedIpRanges/IpRangeProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand All @@ -11,5 +12,4 @@
interface IpRangeProviderInterface
{
public function getRanges(): array;

}
2 changes: 1 addition & 1 deletion BlockedIpRanges/Oracle.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -47,5 +48,4 @@ public function getRanges(): array

return $ranges;
}

}
2 changes: 1 addition & 1 deletion BlockedIpRanges/VariableRange.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -27,5 +28,4 @@ public function getRanges(): array
{
return $this->ranges;
}

}
2 changes: 1 addition & 1 deletion BrowserDetection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -47,5 +48,4 @@ public function isLibrary($userAgent)

return $staticContainer->isLibrary();
}

}
3 changes: 2 additions & 1 deletion Commands/BlockGeoIpOrganisation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Matomo - free/libre analytics platform
*
Expand Down Expand Up @@ -39,7 +40,7 @@ protected function doExecute(): int

$name = $input->getOption('organisation-name');
$pluginConfig[Configuration::KEY_GEOIP_MATCH_PROVIDERS][] = mb_strtolower(trim($name));

$pluginConfig[Configuration::KEY_GEOIP_MATCH_PROVIDERS] = array_values(array_unique($pluginConfig[Configuration::KEY_GEOIP_MATCH_PROVIDERS]));

$config->TrackingSpamPrevention = $pluginConfig;
Expand Down
14 changes: 8 additions & 6 deletions Configuration.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\TrackingSpamPrevention;

use Piwik\Config;

class Configuration
{
const DEFAULT_RANGE_THROW_EXCEPTION = 0;
const DEFAULT_RANGE_ALLOW_LIST = [''];
const DEFAULT_GEOIP_MATCH_PROVIDERS = ['alicloud', 'alibaba cloud', 'digitalocean', 'digital ocean'];
public const DEFAULT_RANGE_THROW_EXCEPTION = 0;
public const DEFAULT_RANGE_ALLOW_LIST = [''];
public const DEFAULT_GEOIP_MATCH_PROVIDERS = ['alicloud', 'alibaba cloud', 'digitalocean', 'digital ocean'];

const KEY_RANGE_THROW_EXCEPTION = 'block_cloud_sync_throw_exception_on_error';
const KEY_RANGE_ALLOW_LIST = 'iprange_allowlist';
const KEY_GEOIP_MATCH_PROVIDERS = 'block_geoip_organisations';
public const KEY_RANGE_THROW_EXCEPTION = 'block_cloud_sync_throw_exception_on_error';
public const KEY_RANGE_ALLOW_LIST = 'iprange_allowlist';
public const KEY_GEOIP_MATCH_PROVIDERS = 'block_geoip_organisations';

public function install()
{
Expand Down
2 changes: 2 additions & 0 deletions Settings/BlockCloudsSetting.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/

namespace Piwik\Plugins\TrackingSpamPrevention\Settings;

use Piwik\Settings\Plugin\SystemSetting;
Expand Down
Loading

0 comments on commit fa18674

Please sign in to comment.