From c907878fd0f3309c685c131c915d075fb0a10fc6 Mon Sep 17 00:00:00 2001 From: Kevin Date: Fri, 5 Apr 2024 21:47:25 +0200 Subject: [PATCH] v 2.75.0 Base Toolbox v 0.14.0 - New helpers : - Export array to CSV. - Export array to JSON. - Get user IP. - Anonymize an IP. --- inc/WPUBaseToolbox/WPUBaseToolbox.php | 113 +++++++++++++++++++++++++- wpubaseplugin.php | 6 +- 2 files changed, 113 insertions(+), 6 deletions(-) diff --git a/inc/WPUBaseToolbox/WPUBaseToolbox.php b/inc/WPUBaseToolbox/WPUBaseToolbox.php index c518b82..d32d433 100644 --- a/inc/WPUBaseToolbox/WPUBaseToolbox.php +++ b/inc/WPUBaseToolbox/WPUBaseToolbox.php @@ -1,10 +1,10 @@ true @@ -461,4 +461,111 @@ function array_to_html_table($array, $args = array()) { return $html; } + /* ---------------------------------------------------------- + Export + ---------------------------------------------------------- */ + + /* Ensure all lines have the same keys + -------------------------- */ + + function export_array_clean_for_csv($data) { + + /* Extract all available keys */ + $all_keys = array(); + foreach ($data as $item) { + $all_keys = array_merge($all_keys, array_keys($item)); + } + $all_keys = array_unique($all_keys); + + foreach ($data as $item_key => $item) { + /* Ensure all rows have the same keys */ + foreach ($all_keys as $k) { + if (!isset($item[$k])) { + $data[$item_key][$k] = ''; + } + } + /* Ensure same sorting of all keys */ + ksort($data[$item_key]); + } + + return $data; + } + + /* Array to JSON + -------------------------- */ + + public function export_array_to_json($data, $name) { + if (!isset($data[0])) { + return; + } + /* Correct headers */ + header('Content-type: application/json'); + header('Content-Disposition: attachment; filename=' . $name . '.json'); + header('Pragma: no-cache'); + + echo json_encode($data); + } + + /* Array to CSV + -------------------------- */ + + public function export_array_to_csv($data, $name) { + if (!isset($data[0])) { + return; + } + + $data = $this->export_array_clean_for_csv($data); + + /* Correct headers */ + header('Content-Type: application/csv'); + header('Content-Disposition: attachment; filename=' . $name . '.csv'); + header('Pragma: no-cache'); + + $all_keys = array_keys($data[0]); + + /* Build and send CSV */ + $output = fopen("php://output", 'w'); + fputcsv($output, $all_keys); + foreach ($data as $item) { + fputcsv($output, $item); + } + fclose($output); + die; + } + + /* ---------------------------------------------------------- + IPs + ---------------------------------------------------------- */ + + /* Thanks to https://stackoverflow.com/a/13646735/975337 */ + function get_user_ip($anonymized = true) { + if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) { + $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; + $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; + } + $client = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : ''; + $forward = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : ''; + $remote = $_SERVER['REMOTE_ADDR']; + + if (filter_var($client, FILTER_VALIDATE_IP)) { + $ip = $client; + } elseif (filter_var($forward, FILTER_VALIDATE_IP)) { + $ip = $forward; + } else { + $ip = $remote; + } + if (!$anonymized) { + return $ip; + } + return $this->anonymize_ip($ip); + } + + /* Thanks to https://gist.github.com/svrnm/3a124d2af18a6726f66e */ + function anonymize_ip($ip) { + if ($ip = @inet_pton($ip)) { + return inet_ntop(substr($ip, 0, strlen($ip) / 2) . str_repeat(chr(0), strlen($ip) / 2)); + } + return '0.0.0.0'; + } + } diff --git a/wpubaseplugin.php b/wpubaseplugin.php index 7c9153e..afdd8f5 100644 --- a/wpubaseplugin.php +++ b/wpubaseplugin.php @@ -5,7 +5,7 @@ Plugin URI: https://github.com/WordPressUtilities/wpubaseplugin Update URI: https://github.com/WordPressUtilities/wpubaseplugin Description: A framework for a WordPress plugin -Version: 2.74.0 +Version: 2.75.0 Author: Darklg Author URI: https://darklg.me/ Text Domain: wpubaseplugin @@ -20,7 +20,7 @@ class WPUBasePlugin { - public $version = '2.74.0'; + public $version = '2.75.0'; private $utilities_classes = array( 'messages' => array( @@ -56,7 +56,7 @@ class WPUBasePlugin { 'name' => 'WPUBaseEmail' ), 'toolbox' => array( - 'namespace' => 'wpubasetoolbox_0_13_0', + 'namespace' => 'wpubasetoolbox_0_14_0', 'name' => 'WPUBaseToolbox' ), 'filecache' => array(