Skip to content

Commit 65ab16f

Browse files
cigamitTheWitness
andauthored
Start converting to newer PHP Syntax and Functions (#6103)
* 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 <thewitness@cacti.net>
1 parent 2adf598 commit 65ab16f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1313
-1187
lines changed

.github/workflows/syntax.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ jobs:
161161
sudo chmod 777 ${{ github.workspace }}/include/config.php
162162
cat ${{ github.workspace}}/include/config.php
163163
164+
- name: Validate composer.json and composer.lock
165+
run: composer validate --strict
166+
167+
- name: Cache Composer packages
168+
id: composer-cache
169+
uses: actions/cache@v3
170+
with:
171+
path: include/vendor
172+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
173+
restore-keys: |
174+
${{ runner.os }}-php-
175+
176+
- name: Install dependencies
177+
run: composer install --prefer-dist --no-progress
178+
164179
- name: Check install code for validity
165180
run: |
166181
cd ${{ github.workspace }}
@@ -190,21 +205,6 @@ jobs:
190205
- name: View contents of logs
191206
run: sudo cat ${{ github.workspace }}/log/cacti.log
192207

193-
# - name: Validate composer.json and composer.lock
194-
# run: composer validate --strict
195-
196-
# - name: Cache Composer packages
197-
# id: composer-cache
198-
# uses: actions/cache@v3
199-
# with:
200-
# path: vendor
201-
# key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
202-
# restore-keys: |
203-
# ${{ runner.os }}-php-
204-
#
205-
# - name: Install dependencies
206-
# run: composer install --prefer-dist --no-progress
207-
208208
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
209209
# Docs: https://getcomposer.org/doc/articles/scripts.md
210210

composer.json

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

composer.lock

Lines changed: 123 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
*/
7575
if ($auth_method != AUTH_METHOD_BASIC) {
7676
if (isset($_SESSION[SESS_CHANGE_PASSWORD])) {
77-
header('Location: ' . CACTI_PATH_URL . 'auth_changepassword.php?ref=' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'index.php'));
77+
header('Location: ' . CACTI_PATH_URL . 'auth_changepassword.php?ref=' . ($_SERVER['HTTP_REFERER'] ?? 'index.php'));
7878

7979
exit;
8080
}

include/global.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@
165165
foreach ($db_var_defaults as $db_var_name => $db_var_default) {
166166
$db_var_full = $db_var_prefix . $db_var_name;
167167

168-
if (!isset($$db_var_full)) {
168+
if (!isset(${$db_var_full})) {
169169
if ($db_var_default !== null) {
170-
$$db_var_full = $db_var_default;
170+
${$db_var_full} = $db_var_default;
171171
} else {
172172
$db_missing_vars .= (($db_missing_vars == '') ? 'missing ' : ', ') . $db_var_full;
173173
}
@@ -225,7 +225,7 @@
225225

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

@@ -596,7 +596,7 @@
596596

597597
foreach ($input as $var => $val) {
598598
if (!in_array($var, $not_unset, true)) {
599-
unset($$var);
599+
unset(${$var});
600600
}
601601
}
602602

include/global_languages.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ function get_src_language_files($i18n_handler) {
299299
);
300300
}
301301

302-
$i18n_handler_text = ($i18n_handler === null) ? 'null' : $i18n_handler;
302+
$i18n_handler_text = $i18n_handler ?? 'null';
303303

304304
foreach ($i18n_providers as $i18n_provider) {
305305
$found = true;
@@ -566,7 +566,7 @@ function set_language_constants(array $constants) {
566566
function __gettext(?string $text, string $domain = 'cacti') {
567567
global $i18n;
568568

569-
$text = $text ?? '';
569+
$text ??= '';
570570

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

597-
$singular = $singular ?? '';
598-
$plural = $plural ?? '';
597+
$singular ??= '';
598+
$plural ??= '';
599599

600600
if (isset($i18n[$domain])) {
601601
return __uf($i18n[$domain]->ngettext($singular, $plural, $number));
@@ -931,21 +931,21 @@ function number_format_i18n($number, $decimals = null, $baseu = 1024) {
931931
$number = 0;
932932
} elseif ($decimals == -1 || $decimals == null) {
933933
$number = number_format($number, 0, $locale['decimal_point'], $locale['thousands_sep']);
934-
} elseif ($number >= pow($baseu, 4)) {
935-
$number = number_format($number / pow($baseu, 4), $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' T');
936-
} elseif ($number >= pow($baseu, 3)) {
937-
$number = number_format($number / pow($baseu, 3), $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' G');
938-
} elseif ($number >= pow($baseu, 2)) {
939-
$number = number_format($number / pow($baseu, 2), $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' M');
934+
} elseif ($number >= $baseu ** 4) {
935+
$number = number_format($number / $baseu ** 4, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' T');
936+
} elseif ($number >= $baseu ** 3) {
937+
$number = number_format($number / $baseu ** 3, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' G');
938+
} elseif ($number >= $baseu ** 2) {
939+
$number = number_format($number / $baseu ** 2, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' M');
940940
} elseif ($number >= $baseu) {
941941
$number = number_format($number / $baseu, $decimals, $locale['decimal_point'], $locale['thousands_sep']) . __(' K');
942942
} else {
943943
$number = number_format($number, $decimals, $locale['decimal_point'], $locale['thousands_sep']);
944944
}
945945

946946
foreach ($origlocales as $locale_setting) {
947-
if (strpos($locale_setting, '=') !== false) {
948-
list($category, $locale) = explode('=', $locale_setting);
947+
if (str_contains($locale_setting, '=')) {
948+
[$category, $locale] = explode('=', $locale_setting);
949949
} else {
950950
$category = LC_ALL;
951951
$locale = $locale_setting;
@@ -980,15 +980,15 @@ function get_new_user_default_language() {
980980
function i18n_debug($text, $mode = FILE_APPEND, $eol = PHP_EOL) {
981981
global $config;
982982

983-
if (!empty($config['i18n_log']) && is_writeable($config['i18n_log'])) {
983+
if (!empty($config['i18n_log']) && is_writable($config['i18n_log'])) {
984984
file_put_contents($config['i18n_log'], $text . $eol, $mode);
985985
}
986986
}
987987

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

991-
if (!empty($config['i18n_text_log']) && is_writeable($config['i18n_log'])) {
991+
if (!empty($config['i18n_text_log']) && is_writable($config['i18n_log'])) {
992992
file_put_contents($config['i18n_text_log'], $text . $eol, $mode);
993993
}
994994
}

include/global_session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
$myrefresh['seconds'] = 99999999;
9090
$myrefresh['page'] = 'index.php';
9191
$refreshIsLogout = 'false';
92-
} elseif (!isset($_SESSION[SESS_USER_ID]) && isset($_SERVER['REQUEST_URL']) && strpos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
92+
} elseif (!isset($_SESSION[SESS_USER_ID]) && isset($_SERVER['REQUEST_URL']) && str_contains($_SERVER['REQUEST_URI'], 'index.php')) {
9393
$myrefresh['seconds'] = 99999999;
9494
$myrefresh['page'] = sanitize_uri($_SERVER['REQUEST_URI']);
9595
$refreshIsLogout = 'false';

include/session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function cacti_db_session_write($id, $data) {
9393
}
9494

9595
$client_addr = get_client_addr();
96-
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT']:'Unknown';
96+
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
9797

9898
if ($user_id > 0) {
9999
db_execute_prepared('INSERT INTO sessions
@@ -105,7 +105,7 @@ function cacti_db_session_write($id, $data) {
105105
user_agent = VALUES(user_agent),
106106
transactions = transactions + 1',
107107
array($id, $client_addr, $access, $data, $user_id, $user_agent));
108-
} elseif (strpos($data, 'ses_user_id') !== false) {
108+
} elseif (str_contains($data, 'ses_user_id')) {
109109
db_execute_prepared('INSERT INTO sessions
110110
(id, remote_addr, access, data, user_agent)
111111
VALUES (?, ?, ?, ?, ?)

0 commit comments

Comments
 (0)