diff --git a/LibreNMS/OS/Symbol.php b/LibreNMS/OS/Symbol.php index 071fd11612..dd386c7a7c 100644 --- a/LibreNMS/OS/Symbol.php +++ b/LibreNMS/OS/Symbol.php @@ -41,7 +41,7 @@ class Symbol extends OS implements WirelessClientsDiscovery { $device = $this->getDevice(); - if (str_contains($device['hardware'], 'AP', true)) { + if (str_i_contains($device['hardware'], 'AP')) { $oid = '.1.3.6.1.4.1.388.11.2.4.2.100.10.1.18.1'; return array( new WirelessSensor('clients', $device['device_id'], $oid, 'symbol', 1, 'Clients') diff --git a/html/ajax_rulesuggest.php b/html/ajax_rulesuggest.php index 094d957d52..f6ec6e3c9b 100644 --- a/html/ajax_rulesuggest.php +++ b/html/ajax_rulesuggest.php @@ -123,7 +123,7 @@ if (isset($_GET['term'], $_GET['device_id'])) { } elseif ($vars['type'] === 'alert_rule_collection') { $x=0; foreach (get_rules_from_json() as $rule) { - if (str_contains($rule['name'], $vars['term'], true)) { + if (str_i_contains($rule['name'], $vars['term'])) { $rule['id'] = $x; $tmp[] = $rule; } diff --git a/includes/common.php b/includes/common.php index cb7ed8cd92..4d58f60aa4 100644 --- a/includes/common.php +++ b/includes/common.php @@ -1385,83 +1385,78 @@ function ResolveGlues($tables, $target, $x = 0, $hist = array(), $last = array() return false; } +if (!function_exists('str_contains')) { + /** + * Determine if a given string contains a given substring. + * + * @param string $haystack + * @param string|array $needles + * @return bool + */ + function str_contains($haystack, $needles) + { + foreach ((array)$needles as $needle) { + if ($needle != '' && strpos($haystack, $needle) !== false) { + return true; + } + } + return false; + } +} + /** * Determine if a given string contains a given substring. * * @param string $haystack * @param string|array $needles - * @param bool $case_insensitive * @return bool */ -function str_contains($haystack, $needles, $case_insensitive = false) +function str_i_contains($haystack, $needles) { - if ($case_insensitive) { - foreach ((array) $needles as $needle) { - if ($needle != '' && stripos($haystack, $needle) !== false) { - return true; - } - } - } else { - foreach ((array) $needles as $needle) { - if ($needle != '' && strpos($haystack, $needle) !== false) { - return true; - } + foreach ((array)$needles as $needle) { + if ($needle != '' && stripos($haystack, $needle) !== false) { + return true; } } return false; } -/** - * Determine if a given string ends with a given substring. - * - * @param string $haystack - * @param string|array $needles - * @param bool $case_insensitive - * @return bool - */ -function ends_with($haystack, $needles, $case_insensitive = false) -{ - if ($case_insensitive) { - $lower_haystack = strtolower($haystack); - foreach ((array)$needles as $needle) { - if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) { - return true; - } - } - } else { +if (!function_exists('ends_with')) { + /** + * Determine if a given string ends with a given substring. + * + * @param string $haystack + * @param string|array $needles + * @return bool + */ + function ends_with($haystack, $needles) + { foreach ((array)$needles as $needle) { if ((string)$needle === substr($haystack, -strlen($needle))) { return true; } } + return false; } - return false; } -/** - * Determine if a given string starts with a given substring. - * - * @param string $haystack - * @param string|array $needles - * @param bool $case_insensitive - * @return bool - */ -function starts_with($haystack, $needles, $case_insensitive = false) -{ - if ($case_insensitive) { - foreach ((array)$needles as $needle) { - if ($needle != '' && stripos($haystack, $needle) === 0) { - return true; - } - } - } else { +if (!function_exists('starts_with')) { + /** + * Determine if a given string starts with a given substring. + * + * @param string $haystack + * @param string|array $needles + * @return bool + */ + function starts_with($haystack, $needles) + { foreach ((array)$needles as $needle) { if ($needle != '' && strpos($haystack, $needle) === 0) { return true; } } + return false; } - return false; } function get_auth_ad_user_filter($username) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index a8fd43e1f5..025863232c 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1005,7 +1005,7 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) if (!is_numeric($tmp_value)) { if ($sensor_type === 'temperature') { // For temp sensors, try and detect fahrenheit values - if (ends_with($tmp_value, 'f', true)) { + if (ends_with($tmp_value, array('f', 'F'))) { $user_function = 'fahrenheit_to_celsius'; } } diff --git a/includes/functions.php b/includes/functions.php index b411167cc0..126b764c64 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1069,12 +1069,12 @@ function is_port_valid($port, $device) $ifAlias = $port['ifAlias']; $ifType = $port['ifType']; - if (str_contains($ifDescr, Config::getOsSetting($device['os'], 'good_if'), true)) { + if (str_i_contains($ifDescr, Config::getOsSetting($device['os'], 'good_if'))) { return true; } foreach (Config::getCombined($device['os'], 'bad_if') as $bi) { - if (str_contains($ifDescr, $bi, true)) { + if (str_i_contains($ifDescr, $bi)) { d_echo("ignored by ifDescr: $ifDescr (matched: $bi)\n"); return false; } diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 10ccbd1647..8931ba001a 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -420,7 +420,7 @@ function poll_mib_def($device, $mib_name_table, $mib_subdir, $mib_oids, $mib_gra list($mib, $file) = explode(':', $mib_name_table, 2); if (is_null($rrd_name)) { - if (str_contains($mib_name_table, 'UBNT', true)) { + if (str_i_contains($mib_name_table, 'UBNT')) { $rrd_name = strtolower($mib); } else { $rrd_name = strtolower($file); diff --git a/scripts/new-os.php b/scripts/new-os.php index e37e0f7dfd..73f9f19990 100755 --- a/scripts/new-os.php +++ b/scripts/new-os.php @@ -44,7 +44,7 @@ sysObjectID: $full_sysObjectID $continue = get_user_input("We already detect this device as OS $os type, do you want to continue to add sensors? (Y/n)"); } - if (!str_contains($continue, 'y', true)) { + if (!str_i_contains($continue, 'y')) { $descr = get_user_input('Enter the description for this OS, i.e Cisco IOS:'); $icon = get_user_input('Enter the logo to use, this can be the name of an existing one (i.e: cisco) or the url to retrieve one:'); diff --git a/tests/CommonFunctionsTest.php b/tests/CommonFunctionsTest.php index 8acf11849a..f065fb00d9 100644 --- a/tests/CommonFunctionsTest.php +++ b/tests/CommonFunctionsTest.php @@ -34,14 +34,14 @@ class CommonFunctionsTest extends TestCase $this->assertTrue(str_contains($data, 'Just')); $this->assertFalse(str_contains($data, 'just')); - $this->assertTrue(str_contains($data, 'juSt', true)); - $this->assertFalse(str_contains($data, 'nope', true)); + $this->assertTrue(str_i_contains($data, 'juSt')); + $this->assertFalse(str_i_contains($data, 'nope')); $this->assertTrue(str_contains($data, array('not', 'this', 'This'))); $this->assertFalse(str_contains($data, array('not', 'this'))); - $this->assertTrue(str_contains($data, array('not', 'thIs'), true)); - $this->assertFalse(str_contains($data, array('not', 'anything'), true)); + $this->assertTrue(str_i_contains($data, array('not', 'thIs'))); + $this->assertFalse(str_i_contains($data, array('not', 'anything'))); } public function testStartsWith() @@ -51,14 +51,8 @@ class CommonFunctionsTest extends TestCase $this->assertTrue(starts_with($data, 'This')); $this->assertFalse(starts_with($data, 'this')); - $this->assertTrue(starts_with($data, 'thIs', true)); - $this->assertFalse(starts_with($data, 'test', true)); - $this->assertTrue(starts_with($data, array('this', 'Test', 'This'))); $this->assertFalse(starts_with($data, array('this', 'Test'))); - - $this->assertTrue(starts_with($data, array('Test', 'no', 'thiS'), true)); - $this->assertFalse(starts_with($data, array('just', 'Test'), true)); } public function testEndsWith() @@ -68,14 +62,8 @@ class CommonFunctionsTest extends TestCase $this->assertTrue(ends_with($data, 'Testing')); $this->assertFalse(ends_with($data, 'testing')); - $this->assertTrue(ends_with($data, 'testIng', true)); - $this->assertFalse(ends_with($data, 'test', true)); - $this->assertTrue(ends_with($data, array('this', 'Testing', 'This'))); $this->assertFalse(ends_with($data, array('this', 'Test'))); - - $this->assertTrue(ends_with($data, array('this', 'tesTing', 'no'), true)); - $this->assertFalse(ends_with($data, array('this', 'Test'), true)); } public function testRrdDescriptions()