Skip to content

Commit

Permalink
Split functions file
Browse files Browse the repository at this point in the history
Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
  • Loading branch information
rdwebdesign committed Jul 19, 2022
1 parent ca8a35f commit 22dea99
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 64 deletions.
64 changes: 0 additions & 64 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,27 +596,6 @@ function convertUnicodeToIDNA($IDNA) {
return $IDNA;
}

// Return memory usage to show on status block
function getMemUsage() {
$data = explode("\n", file_get_contents("/proc/meminfo"));
$meminfo = array();
if (count($data) > 0) {
foreach ($data as $line) {
$expl = explode(":", $line);
if (count($expl) == 2) {
// remove " kB" from the end of the string and make it an integer
$meminfo[$expl[0]] = intval(trim(substr($expl[1],0, -3)));
}
}
$memused = $meminfo["MemTotal"] - $meminfo["MemFree"] - $meminfo["Buffers"] - $meminfo["Cached"];
$memusage = $memused / $meminfo["MemTotal"];
} else {
$memusage = -1;
}

return $memusage;
}

// Return PID of FTL (used in settings.php)
function pidofFTL() {
return shell_exec("pidof pihole-FTL");
Expand All @@ -640,47 +619,4 @@ function convertseconds($argument) {
return sprintf('%dd %dh %dm %ds', ($seconds / 86400), ($seconds / 3600 % 24), ($seconds / 60 % 60), ($seconds % 60));
}
}

// Try to get temperature value from different places (OS dependent)
// - return an array, containing the temperature and limit.
function getTemperature() {
global $setupVars;

if (file_exists("/sys/class/thermal/thermal_zone0/temp")) {
$output = rtrim(file_get_contents("/sys/class/thermal/thermal_zone0/temp"));
} elseif (file_exists("/sys/class/hwmon/hwmon0/temp1_input")) {
$output = rtrim(file_get_contents("/sys/class/hwmon/hwmon0/temp1_input"));
} else {
$output = "";
}

// Test if we succeeded in getting the temperature
if (is_numeric($output)) {
// $output could be either 4-5 digits or 2-3, and we only divide by 1000 if it's 4-5
// ex. 39007 vs 39
$celsius = intval($output);

// If celsius is greater than 1 degree and is in the 4-5 digit format
if ($celsius > 1000) {
// Use multiplication to get around the division-by-zero error
$celsius *= 1e-3;
}

// Get user-defined temperature limit if set
if (isset($setupVars['TEMPERATURE_LIMIT'])) {
$limit = intval($setupVars['TEMPERATURE_LIMIT']);
} else {
$limit = 60;
}

} else {
// Nothing can be colder than -273.15 degree Celsius (= 0 Kelvin)
// This is the minimum temperature possible (AKA absolute zero)
$celsius = -273.16;
// Set templimit to null if no tempsensor was found
$limit = null;
}

return [$celsius, $limit];
}
?>
1 change: 1 addition & 0 deletions scripts/pi-hole/php/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "scripts/pi-hole/php/password.php";
require_once "scripts/pi-hole/php/FTL.php";
require_once "scripts/pi-hole/php/func.php";
require_once "scripts/pi-hole/php/header_func.php";
require "scripts/pi-hole/php/theme.php";
$scriptname = basename($_SERVER['SCRIPT_FILENAME']);
$hostname = gethostname() ? gethostname() : "";
Expand Down
65 changes: 65 additions & 0 deletions scripts/pi-hole/php/header_func.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
// Return memory usage to show on status block
function getMemUsage() {
$data = explode("\n", file_get_contents("/proc/meminfo"));
$meminfo = array();
if (count($data) > 0) {
foreach ($data as $line) {
$expl = explode(":", $line);
if (count($expl) == 2) {
// remove " kB" from the end of the string and make it an integer
$meminfo[$expl[0]] = intval(trim(substr($expl[1],0, -3)));
}
}
$memused = $meminfo["MemTotal"] - $meminfo["MemFree"] - $meminfo["Buffers"] - $meminfo["Cached"];
$memusage = $memused / $meminfo["MemTotal"];
} else {
$memusage = -1;
}

return $memusage;
}

// Try to get temperature value from different places (OS dependent)
// - return an array, containing the temperature and limit.
function getTemperature() {
global $setupVars;

if (file_exists("/sys/class/thermal/thermal_zone0/temp")) {
$output = rtrim(file_get_contents("/sys/class/thermal/thermal_zone0/temp"));
} elseif (file_exists("/sys/class/hwmon/hwmon0/temp1_input")) {
$output = rtrim(file_get_contents("/sys/class/hwmon/hwmon0/temp1_input"));
} else {
$output = "";
}

// Test if we succeeded in getting the temperature
if (is_numeric($output)) {
// $output could be either 4-5 digits or 2-3, and we only divide by 1000 if it's 4-5
// ex. 39007 vs 39
$celsius = intval($output);

// If celsius is greater than 1 degree and is in the 4-5 digit format
if ($celsius > 1000) {
// Use multiplication to get around the division-by-zero error
$celsius *= 1e-3;
}

// Get user-defined temperature limit if set
if (isset($setupVars['TEMPERATURE_LIMIT'])) {
$limit = intval($setupVars['TEMPERATURE_LIMIT']);
} else {
$limit = 60;
}

} else {
// Nothing can be colder than -273.15 degree Celsius (= 0 Kelvin)
// This is the minimum temperature possible (AKA absolute zero)
$celsius = -273.16;
// Set templimit to null if no tempsensor was found
$limit = null;
}

return [$celsius, $limit];
}
?>

0 comments on commit 22dea99

Please sign in to comment.