Skip to content

Commit

Permalink
Start converting to newer PHP Syntax and Functions (#6103)
Browse files Browse the repository at this point in the history
* Add Rector

* Add Rector

* Fix "Cashed" misspelling

Also harmonize the phrase with the one below it

* Fix implode error in db_table_create

Only relevant when multiple primary keys

* Fix auth issue, parentheses in the wrong location

Was causing too many arguments to be sent to cacti_sizeof which only take 1, instead of get_allowed_devices.

* Fix issue with session_var not properly using class variable

* Fix pass by reference error in get_allowed_devices

get_allowed_devices has a pass by reference on the "total_rows" field.  It is used in several places to pass the total_rows back to a counter.  Here though, we are passing a number directly to the function, which errors out with "Uncaught Error: get_allowed_devices(): Argument #4 ($total_rows) cannot be passed by reference".  So we will just place a dummy variable in front to catch the pass.

This wasn't caught before because the previous parentheses issue.

* Fix undefined variable "field_array" in html_filter

* Fix undefined function 'get_nfilter_filter_request_var'

* Fix undefined variable 'resizable'

I corrected the function parameter, as that was technically the incorrect spelling

* Fix invalid variable $user_id

* Fix undefined vars in spikekill class

* Fix copy and paste error

Oops

* Forgot to use the dummy variables

* Add missing $

Oops

* Start converting to newer PHP Syntax and Functions

* Test fixing the workflow by install composer

* Lets not valid the composer file right now
Seems we have a few things to fix it in

* Validate composer

* Lite formating changes

* lite formatting

* lite formatting

* lite format changes

* lite format changes

* lite format changes

* lite format changes

* lite format changes

* lite format changes

* lite format changes

* lite format changes

---------

Co-authored-by: TheWitness <[email protected]>
  • Loading branch information
cigamit and TheWitness authored Feb 23, 2025
1 parent 2adf598 commit 65ab16f
Show file tree
Hide file tree
Showing 68 changed files with 1,313 additions and 1,187 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/syntax.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ jobs:
sudo chmod 777 ${{ github.workspace }}/include/config.php
cat ${{ github.workspace}}/include/config.php
- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: include/vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Check install code for validity
run: |
cd ${{ github.workspace }}
Expand Down Expand Up @@ -190,21 +205,6 @@ jobs:
- name: View contents of logs
run: sudo cat ${{ github.workspace }}/log/cacti.log

# - name: Validate composer.json and composer.lock
# run: composer validate --strict

# - name: Cache Composer packages
# id: composer-cache
# uses: actions/cache@v3
# with:
# path: vendor
# key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
# restore-keys: |
# ${{ runner.os }}-php-
#
# - name: Install dependencies
# run: composer install --prefer-dist --no-progress

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

Expand Down
49 changes: 26 additions & 23 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"_copyright": {
"01": "+-------------------------------------------------------------------------+",
"02": "| Copyright (C) 2004-2023 The Cacti Group |",
"03": "| |",
"04": "| This program is free software; you can redistribute it and/or |",
"05": "| modify it under the terms of the GNU General Public License |",
"06": "| as published by the Free Software Foundation; either version 2 |",
"07": "| of the License, or (at your option) any later version. |",
"08": "| |",
"09": "| This program is distributed in the hope that it will be useful, |",
"10": "| but WITHOUT ANY WARRANTY; without even the implied warranty of |",
"11": "| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |",
"12": "| GNU General Public License for more details. |",
"13": "+-------------------------------------------------------------------------+",
"14": "| Cacti: The Complete RRDtool-based Graphing Solution |",
"15": "+-------------------------------------------------------------------------+",
"16": "| This code is designed, written, and maintained by the Cacti Group. See |",
"17": "| about.php and/or the AUTHORS file for specific developer information. |",
"18": "+-------------------------------------------------------------------------+",
"19": "| http://www.cacti.net/ |",
"20": "+-------------------------------------------------------------------------+"
},
"_comment": [
"+-------------------------------------------------------------------------+",
"| Copyright (C) 2004-2025 The Cacti Group |",
"| |",
"| This program is free software; you can redistribute it and/or |",
"| modify it under the terms of the GNU General Public License |",
"| as published by the Free Software Foundation; either version 2 |",
"| of the License, or (at your option) any later version. |",
"| |",
"| This program is distributed in the hope that it will be useful, |",
"| but WITHOUT ANY WARRANTY; without even the implied warranty of |",
"| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |",
"| GNU General Public License for more details. |",
"+-------------------------------------------------------------------------+",
"| Cacti: The Complete RRDtool-based Graphing Solution |",
"+-------------------------------------------------------------------------+",
"| This code is designed, written, and maintained by the Cacti Group. See |",
"| about.php and/or the AUTHORS file for specific developer information. |",
"+-------------------------------------------------------------------------+",
"| http://www.cacti.net/ |",
"+-------------------------------------------------------------------------+"
],
"name": "cacti/cacti",
"description": "Cacti Network Monitoring Framework",
"type": "composer-plugin",
"license": "GPL2",
"license": "GPL-2.0-only",
"require": {
"php": ">=8.0",
"ext-PDO": "*",
Expand Down Expand Up @@ -79,5 +79,8 @@
"lint": "phplint --no-cache src/",
"phpcsfixer": "php-cs-fixer fix --diff --dry-run --ansi",
"phpcsfixit": "php-cs-fixer fix --ansi"
},
"require-dev": {
"rector/rector": "^2.0"
}
}
128 changes: 123 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion include/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
*/
if ($auth_method != AUTH_METHOD_BASIC) {
if (isset($_SESSION[SESS_CHANGE_PASSWORD])) {
header('Location: ' . CACTI_PATH_URL . 'auth_changepassword.php?ref=' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'index.php'));
header('Location: ' . CACTI_PATH_URL . 'auth_changepassword.php?ref=' . ($_SERVER['HTTP_REFERER'] ?? 'index.php'));

exit;
}
Expand Down
8 changes: 4 additions & 4 deletions include/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@
foreach ($db_var_defaults as $db_var_name => $db_var_default) {
$db_var_full = $db_var_prefix . $db_var_name;

if (!isset($$db_var_full)) {
if (!isset(${$db_var_full})) {
if ($db_var_default !== null) {
$$db_var_full = $db_var_default;
${$db_var_full} = $db_var_default;
} else {
$db_missing_vars .= (($db_missing_vars == '') ? 'missing ' : ', ') . $db_var_full;
}
Expand Down Expand Up @@ -225,7 +225,7 @@

/* define any additional paths as constants */
foreach ($config as $key => $value) {
if (substr($key, -5) == '_path') {
if (str_ends_with($key, '_path')) {
$path_name = substr($key, 0, -5);
$constant_name = 'CACTI_PATH_' . strtoupper($path_name);

Expand Down Expand Up @@ -596,7 +596,7 @@

foreach ($input as $var => $val) {
if (!in_array($var, $not_unset, true)) {
unset($$var);
unset(${$var});
}
}

Expand Down
28 changes: 14 additions & 14 deletions include/global_languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function get_src_language_files($i18n_handler) {
);
}

$i18n_handler_text = ($i18n_handler === null) ? 'null' : $i18n_handler;
$i18n_handler_text = $i18n_handler ?? 'null';

foreach ($i18n_providers as $i18n_provider) {
$found = true;
Expand Down Expand Up @@ -566,7 +566,7 @@ function set_language_constants(array $constants) {
function __gettext(?string $text, string $domain = 'cacti') {
global $i18n;

$text = $text ?? '';
$text ??= '';

// Assume translation fails or is not defined
if (isset($i18n[$domain])) {
Expand Down Expand Up @@ -594,8 +594,8 @@ function __gettext(?string $text, string $domain = 'cacti') {
function __n(?string $singular, ?string $plural, $number, string $domain = 'cacti') {
global $i18n;

$singular = $singular ?? '';
$plural = $plural ?? '';
$singular ??= '';
$plural ??= '';

if (isset($i18n[$domain])) {
return __uf($i18n[$domain]->ngettext($singular, $plural, $number));
Expand Down Expand Up @@ -931,21 +931,21 @@ function number_format_i18n($number, $decimals = null, $baseu = 1024) {
$number = 0;
} elseif ($decimals == -1 || $decimals == null) {
$number = number_format($number, 0, $locale['decimal_point'], $locale['thousands_sep']);
} elseif ($number >= pow($baseu, 4)) {
$number = number_format($number / pow($baseu, 4), $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' T');
} elseif ($number >= pow($baseu, 3)) {
$number = number_format($number / pow($baseu, 3), $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' G');
} elseif ($number >= pow($baseu, 2)) {
$number = number_format($number / pow($baseu, 2), $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' M');
} elseif ($number >= $baseu ** 4) {
$number = number_format($number / $baseu ** 4, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' T');
} elseif ($number >= $baseu ** 3) {
$number = number_format($number / $baseu ** 3, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' G');
} elseif ($number >= $baseu ** 2) {
$number = number_format($number / $baseu ** 2, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' M');
} elseif ($number >= $baseu) {
$number = number_format($number / $baseu, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' K');
} else {
$number = number_format($number, $decimals, $locale['decimal_point'], $locale['thousands_sep']);
}

foreach ($origlocales as $locale_setting) {
if (strpos($locale_setting, '=') !== false) {
list($category, $locale) = explode('=', $locale_setting);
if (str_contains($locale_setting, '=')) {
[$category, $locale] = explode('=', $locale_setting);
} else {
$category = LC_ALL;
$locale = $locale_setting;
Expand Down Expand Up @@ -980,15 +980,15 @@ function get_new_user_default_language() {
function i18n_debug($text, $mode = FILE_APPEND, $eol = PHP_EOL) {
global $config;

if (!empty($config['i18n_log']) && is_writeable($config['i18n_log'])) {
if (!empty($config['i18n_log']) && is_writable($config['i18n_log'])) {
file_put_contents($config['i18n_log'], $text . $eol, $mode);
}
}

function i18n_text_debug($text, $mode = FILE_APPEND, $eol = PHP_EOL) {
global $config;

if (!empty($config['i18n_text_log']) && is_writeable($config['i18n_log'])) {
if (!empty($config['i18n_text_log']) && is_writable($config['i18n_log'])) {
file_put_contents($config['i18n_text_log'], $text . $eol, $mode);
}
}
2 changes: 1 addition & 1 deletion include/global_session.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
$myrefresh['seconds'] = 99999999;
$myrefresh['page'] = 'index.php';
$refreshIsLogout = 'false';
} elseif (!isset($_SESSION[SESS_USER_ID]) && isset($_SERVER['REQUEST_URL']) && strpos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
} elseif (!isset($_SESSION[SESS_USER_ID]) && isset($_SERVER['REQUEST_URL']) && str_contains($_SERVER['REQUEST_URI'], 'index.php')) {
$myrefresh['seconds'] = 99999999;
$myrefresh['page'] = sanitize_uri($_SERVER['REQUEST_URI']);
$refreshIsLogout = 'false';
Expand Down
4 changes: 2 additions & 2 deletions include/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function cacti_db_session_write($id, $data) {
}

$client_addr = get_client_addr();
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT']:'Unknown';
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';

if ($user_id > 0) {
db_execute_prepared('INSERT INTO sessions
Expand All @@ -105,7 +105,7 @@ function cacti_db_session_write($id, $data) {
user_agent = VALUES(user_agent),
transactions = transactions + 1',
array($id, $client_addr, $access, $data, $user_id, $user_agent));
} elseif (strpos($data, 'ses_user_id') !== false) {
} elseif (str_contains($data, 'ses_user_id')) {
db_execute_prepared('INSERT INTO sessions
(id, remote_addr, access, data, user_agent)
VALUES (?, ?, ?, ?, ?)
Expand Down
Loading

0 comments on commit 65ab16f

Please sign in to comment.