File manager - Edit - /usr/local/cpanel/base/frontend/jupiter/cpguard/includes/functions.php
Back
<?php if (!function_exists('str_contains')) { function str_contains(string $haystack, string $needle) { return empty($needle) || strpos($haystack, $needle) !== false; } } function number_format_short($n, $precision = 1) { if ($n < 900) { // 0 - 900 $n_format = number_format($n, $precision); $suffix = ''; } else if ($n < 900000) { // 0.9k-850k $n_format = number_format($n / 1000, $precision); $suffix = 'K'; } else if ($n < 900000000) { // 0.9m-850m $n_format = number_format($n / 1000000, $precision); $suffix = 'M'; } else if ($n < 900000000000) { // 0.9b-850b $n_format = number_format($n / 1000000000, $precision); $suffix = 'B'; } else { // 0.9t+ $n_format = number_format($n / 1000000000000, $precision); $suffix = 'T'; } // Remove unecessary zeroes after decimal. "1.0" -> "1"; "1.00" -> "1" // Intentionally does not affect partials, eg "1.50" -> "1.50" if ($precision > 0) { $dotzero = '.' . str_repeat('0', $precision); $n_format = str_replace($dotzero, '', $n_format); } return $n_format . $suffix; } function is_dir_empty($dir) { $handle = opendir($dir); while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { closedir($handle); return FALSE; } } closedir($handle); return TRUE; } function file_strlen($filename) { $count = 0; $fp = fopen($filename, 'r'); if (!$fp) { app_log('Could not open file ' . $filename); return false; } while (false !== ($char = fgetc($fp))) { $count++; } return $count; } function is_file_identical($fileOne, $fileTwo) { if (filesize($fileOne) !== filesize($fileTwo)) return false; //if (filetype($fileOne) !== filetype($fileTwo)) return false; if (!$fp1 = fopen($fileOne, 'rb')) return false; if (!$fp2 = fopen($fileTwo, 'rb')) { fclose($fp1); return false; } $same = true; while (!feof($fp1) and !feof($fp2)) if (fread($fp1, 4096) !== fread($fp2, 4096)) { $same = false; break; } if (feof($fp1) !== feof($fp2)) $same = false; fclose($fp1); fclose($fp2); return $same; } function update_ini_file($data, $filepath) { if (!is_array($data)) { return false; } $content = ""; $parsed_ini = parse_ini_file($filepath, true); foreach ($data as $ini_key => $ini_value) { if (is_array($ini_value)) { //if it is a section $content .= "[" . $ini_key . "]" . PHP_EOL; foreach ($ini_value as $sec_key => $sec_value) { $content .= $sec_key . "=" . $sec_value . PHP_EOL; } } else { $content .= $ini_key . "=" . $ini_value . PHP_EOL; } } file_put_contents($filepath, $content); return true; } function array_keys_exists(array $keys, array $arr) { return !array_diff_key(array_flip($keys), $arr); } function remove_subdirectories($directory_list) { asort($directory_list); $prev = NULL; $new_list = array(); foreach ($directory_list as $dirpath) { if (!str_contains($dirpath, $prev)) { $new_list[] = $dirpath; $prev = $dirpath; } } return $new_list; } function time_interval_string($seconds) { $seconds = (int) max(0, $seconds); $interval = [ 'Days' => intdiv($seconds, 86400), 'Hours' => intdiv($seconds % 86400, 3600), 'Minutes' => intdiv($seconds % 3600, 60), 'Seconds' => $seconds % 60 ]; $output = ''; foreach ($interval as $key => $value) { if ($value > 0) { $output .= $value . ' ' . $key . ' '; } } return trim($output) !== '' ? trim($output) : '0 Seconds'; } function time_elapsed_string($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second', ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); } else { unset($string[$k]); } } if (!$full) $string = array_slice($string, 0, 1); return $string ? implode(', ', $string) . ' ago' : 'just now'; } function convert_date($str = false) { $dtime = DateTime::createFromFormat("D M d H:i:s Y", $str); return $dtime->getTimestamp(); } function read_json_file($file_path, $create_if_not_exist = false) { if (file_exists($file_path)) { $ret = json_decode(file_get_contents($file_path), true); if (!is_array($ret) || empty($ret)) { app_log("Invalid JSON syntax [file: $file_path]"); return array(); } else { return $ret; } } else if ($create_if_not_exist) { if (file_put_contents($file_path, '') === false) { app_log("File cannot be created : " . $file_path); return false; } return array(); } else { app_log("File not found : " . $file_path); return false; } } function save_json_file($data_array, $file) { file_put_contents($file, json_encode($data_array), JSON_PRETTY_PRINT); } $app_log_connection = NULL; function app_log($data) { global $app_log_connection, $home_dir; if ($app_log_connection === NULL) { if (!file_exists($home_dir . '/.cpguard')) { shell_exec("/bin/mkdir $home_dir/.cpguard; /bin/chmod 755 $home_dir/.cpguard;"); } $app_log_connection = fopen($home_dir . '/.cpguard/application.log', "a+"); } fwrite($app_log_connection, PHP_EOL . "[" . date('d-m-Y H:i:s') . "]: " . trim(preg_replace('/\s+/', ' ', $data))); } function api_error($message) { echo '<div style="font-family: \'Segoe UI\', Tahoma, Geneva, Verdana, sans-serif; background-color: #ffffff; border-left: 5px solid #d32f2f; box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 15px 20px; border-radius: 4px; display: flex; justify-content: space-between; align-items: center; max-width: 600px; margin: 0 auto;"> <div style="display: flex; align-items: center;"> <svg xmlns="http://www.w3.org/2000/svg" style="width: 20px; height: 20px; fill: #d32f2f; margin-right: 15px;" viewBox="0 0 20 20" fill="currentColor"> <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" /> </svg> <div> <h4 style="margin: 0; font-size: 16px; color: #333;">Error Encountered</h4> <p style="margin: 4px 0 0; font-size: 13px; color: #666;">' . htmlspecialchars($message) . '</p> </div> </div> </div>'; } function string_between($string, $start, $end) { $string = " " . $string; $ini = strpos($string, $start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string, $end, $ini) - $ini; $ret = substr($string, $ini, $len); return empty($ret) ? false : $ret; } function ip_is_private($ip) { $pri_addrs = array( '10.0.0.0|10.255.255.255', '172.16.0.0|172.31.255.255', '192.168.0.0|192.168.255.255', '169.254.0.0|169.254.255.255', '127.0.0.0|127.255.255.255' ); $long_ip = ip2long($ip); if ($long_ip != -1) { foreach ($pri_addrs as $pri_addr) { list($start, $end) = explode('|', $pri_addr); // IF IS PRIVATE if ($long_ip >= ip2long($start) && $long_ip <= ip2long($end)) { return true; } } } return false; } function get_http_response_code($url) { $headers = @get_headers($url); return (int) substr($headers[0], 9, 3); }
| ver. 1.4 |
Github
|
.
| PHP 8.1.34 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings