2011-04-01 14:28:50 +02:00
< ? php
2023-07-03 17:20:25 +02:00
// Copyright (c) 2011-2023 Pandora FMS
2023-06-23 23:44:33 +02:00
// http://www.pandorafms.com <info@pandorafms.com>
2011-04-01 14:28:50 +02:00
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2019-01-30 16:18:44 +01:00
function serialize_in_temp ( $array = [], $serial_id = null , $ttl = 1 )
{
$json = json_encode ( $array );
if ( $serial_id === null ) {
$serial_id = uniqid ();
}
$file_path = sys_get_temp_dir () . '/pandora_serialize_' . $serial_id . '__1__' . $ttl ;
if ( file_put_contents ( $file_path , $json ) === false ) {
return false ;
}
2011-04-01 14:28:50 +02:00
2019-01-30 16:18:44 +01:00
return $serial_id ;
2011-04-01 14:28:50 +02:00
}
2019-01-30 16:18:44 +01:00
function unserialize_in_temp ( $serial_id = null , $delete = true , $ttl = 1 )
{
if ( $serial_id === null ) {
return false ;
}
$volume = - 1 ;
for ( $i = 1 ; $i <= $ttl ; $i ++ ) {
$file_path = sys_get_temp_dir () . '/pandora_serialize_' . $serial_id . '__' . $i . '__' . $ttl ;
if ( file_exists ( $file_path )) {
$volume = $i ;
break ;
}
}
2024-02-09 13:51:56 +01:00
if ( file_exists ( $file_path ) === false ) {
return false ;
} else {
$content = file_get_contents ( $file_path );
}
2019-01-30 16:18:44 +01:00
if ( $content === false ) {
return false ;
}
$array = json_decode ( $content , true );
if ( $delete ) {
if ( $volume == $ttl ) {
unlink ( $file_path );
} else {
$next_volume = ( $volume + 1 );
rename ( $file_path , sys_get_temp_dir () . '/pandora_serialize_' . $serial_id . '__' . $next_volume . '__' . $ttl );
}
}
return $array ;
2011-04-01 14:28:50 +02:00
}
2019-01-30 16:18:44 +01:00
function delete_unserialize_in_temp ( $serial_id = null )
{
if ( $serial_id === null ) {
return false ;
}
$file_path = sys_get_temp_dir () . '/pandora_serialize_' . $serial_id ;
return unlink ( $file_path );
2011-04-01 14:28:50 +02:00
}
2011-04-08 10:56:43 +02:00
2019-01-30 16:18:44 +01:00
function reverse_data ( $array )
{
$array2 = [];
foreach ( $array as $index => $values ) {
foreach ( $values as $index2 => $value ) {
$array2 [ $index2 ][ $index ] = $value ;
}
}
return $array2 ;
2011-04-08 10:56:43 +02:00
}
2011-04-11 18:45:03 +02:00
2019-01-30 16:18:44 +01:00
function stack_data ( & $chart_data , & $legend = null , & $color = null )
{
foreach ( $chart_data as $val_x => $graphs ) {
$prev_val = 0 ;
$key = 1000 ;
foreach ( $graphs as $graph => $val_y ) {
$chart_data [ $val_x ][ $graph ] += $prev_val ;
$prev_val = $chart_data [ $val_x ][ $graph ];
$temp_data [ $val_x ][ $key ] = $chart_data [ $val_x ][ $graph ];
if ( isset ( $color [ $graph ])) {
$temp_color [ $key ] = $color [ $graph ];
}
if ( isset ( $legend [ $graph ])) {
$temp_legend [ $key ] = $legend [ $graph ];
}
$key -- ;
}
ksort ( $temp_data [ $val_x ]);
}
$chart_data = $temp_data ;
if ( isset ( $legend )) {
$legend = $temp_legend ;
ksort ( $legend );
}
if ( isset ( $color )) {
$color = $temp_color ;
ksort ( $color );
}
2011-04-11 18:45:03 +02:00
}
2011-12-22 19:42:50 +01:00
2019-01-30 16:18:44 +01:00
function graph_get_max_index ( $legend_values )
{
$max_chars = 0 ;
foreach ( $legend_values as $string_legend ) {
if ( empty ( $string_legend )) {
continue ;
}
$string_legend = explode ( " \n " , $string_legend );
foreach ( $string_legend as $st_lg ) {
$len = strlen ( $st_lg );
if ( $len > $max_chars ) {
$max_chars = $len ;
}
}
}
return $max_chars ;
2011-12-22 19:42:50 +01:00
}
2012-03-01 17:41:02 +01:00
2019-01-30 16:18:44 +01:00
function setup_watermark ( $water_mark , & $water_mark_file , & $water_mark_url )
{
if ( ! is_array ( $water_mark )) {
$water_mark_file = $water_mark ;
$water_mark_url = '' ;
return ;
}
if ( isset ( $water_mark [ 'file' ])) {
$water_mark_file = $water_mark [ 'file' ];
} else {
$water_mark_file = '' ;
}
if ( isset ( $water_mark [ 'url' ])) {
$water_mark_url = $water_mark [ 'url' ];
} else {
$water_mark_url = '' ;
}
2012-03-01 17:41:02 +01:00
}
2013-02-15 13:37:07 +01:00
2019-01-30 16:18:44 +01:00
2013-02-15 13:37:07 +01:00
// Function to convert hue to RGB
2019-01-30 16:18:44 +01:00
function hue_2_rgb ( $v1 , $v2 , $vh )
{
if ( $vh < 0 ) {
$vh += 1 ;
};
if ( $vh > 1 ) {
$vh -= 1 ;
};
if (( 6 * $vh ) < 1 ) {
return ( $v1 + ( $v2 - $v1 ) * 6 * $vh );
};
if (( 2 * $vh ) < 1 ) {
return ( $v2 );
};
2013-02-15 13:37:07 +01:00
2019-01-30 16:18:44 +01:00
if (( 3 * $vh ) < 2 ) {
return ( $v1 + ( $v2 - $v1 ) * (( 2 / 3 - $vh ) * 6 ));
};
return ( $v1 );
2013-02-15 13:37:07 +01:00
};
2019-01-30 16:18:44 +01:00
function hex_2_rgb ( $hexcode )
{
$hexcode = str_replace ( '#' , '' , $hexcode );
// $hexcode is the six digit hex colour code we want to convert
$redhex = substr ( $hexcode , 0 , 2 );
$greenhex = substr ( $hexcode , 2 , 2 );
$bluehex = substr ( $hexcode , 4 , 2 );
// $var_r, $var_g and $var_b are the three decimal fractions to be input to our RGB-to-HSL conversion routine
$var_r = hexdec ( $redhex );
$var_g = hexdec ( $greenhex );
$var_b = hexdec ( $bluehex );
return [
'R' => $var_r ,
'G' => $var_g ,
'B' => $var_b ,
];
2013-02-20 18:15:38 +01:00
}
2019-01-30 16:18:44 +01:00
function get_complementary_rgb ( $hexcode )
{
$rgb = hex_2_rgb ( $hexcode );
$var_r = ( $rgb [ 'R' ] / 255 );
$var_g = ( $rgb [ 'G' ] / 255 );
$var_b = ( $rgb [ 'B' ] / 255 );
// Now plug these values into the rgb2hsl routine. Below is my PHP version of EasyRGB.com's generic code for that conversion:
// Input is $var_r, $var_g and $var_b from above
// Output is HSL equivalent as $h, $s and $l — these are again expressed as fractions of 1, like the input values
$var_min = min ( $var_r , $var_g , $var_b );
$var_max = max ( $var_r , $var_g , $var_b );
$del_max = ( $var_max - $var_min );
$l = (( $var_max + $var_min ) / 2 );
if ( $del_max == 0 ) {
$h = 0 ;
$s = 0 ;
} else {
if ( $l < 0.5 ) {
$s = ( $del_max / ( $var_max + $var_min ));
} else {
$s = ( $del_max / ( 2 - $var_max - $var_min ));
};
$del_r = (((( $var_max - $var_r ) / 6 ) + ( $del_max / 2 )) / $del_max );
$del_g = (((( $var_max - $var_g ) / 6 ) + ( $del_max / 2 )) / $del_max );
$del_b = (((( $var_max - $var_b ) / 6 ) + ( $del_max / 2 )) / $del_max );
if ( $var_r == $var_max ) {
$h = ( $del_b - $del_g );
} else if ( $var_g == $var_max ) {
$h = (( 1 / 3 ) + $del_r - $del_b );
} else if ( $var_b == $var_max ) {
$h = (( 2 / 3 ) + $del_g - $del_r );
};
if ( $h < 0 ) {
$h += 1 ;
};
if ( $h > 1 ) {
$h -= 1 ;
};
};
// So now we have the colour as an HSL value, in the variables $h, $s and $l. These three output variables are again held as fractions of 1 at this stage, rather than as degrees and percentages. So e.g., cyan (180° 100% 50%) would come out as $h = 0.5, $s = 1, and $l = 0.5.
// Next find the value of the opposite Hue, i.e., the one that's 180°, or 0.5, away (I'm sure the mathematicians have a more elegant way of doing this, but):
// Calculate the opposite hue, $h2
$h2 = ( $h + 0.5 );
if ( $h2 > 1 ) {
$h2 -= 1 ;
};
// The HSL value of the complementary colour is now in $h2, $s, $l. So we're ready to convert this back to RGB (again, my PHP version of the EasyRGB.com formula). Note the input and output formats are different this time, see my comments at the top of the code:
// Input is HSL value of complementary colour, held in $h2, $s, $l as fractions of 1
// Output is RGB in normal 255 255 255 format, held in $r, $g, $b
// Hue is converted using function hue_2_rgb, shown at the end of this code
if ( $s == 0 ) {
$r = ( $l * 255 );
$g = ( $l * 255 );
$b = ( $l * 255 );
} else {
if ( $l < 0.5 ) {
$var_2 = ( $l * ( 1 + $s ));
} else {
$var_2 = (( $l + $s ) - ( $s * $l ));
};
$var_1 = ( 2 * $l - $var_2 );
$r = ( 255 * hue_2_rgb ( $var_1 , $var_2 , ( $h2 + ( 1 / 3 ))));
$g = ( 255 * hue_2_rgb ( $var_1 , $var_2 , $h2 ));
$b = ( 255 * hue_2_rgb ( $var_1 , $var_2 , ( $h2 - ( 1 / 3 ))));
};
// And after that routine, we finally have $r, $g and $b in 255 255 255 (RGB) format, which we can convert to six digits of hex:
$rhex = sprintf ( '%02X' , round ( $r ));
$ghex = sprintf ( '%02X' , round ( $g ));
$bhex = sprintf ( '%02X' , round ( $b ));
$rgbhex = $rhex . $ghex . $bhex ;
return $rgbhex ;
2013-02-15 13:37:07 +01:00
}
2018-07-23 08:59:08 +02:00
2019-01-30 16:18:44 +01:00
2018-07-23 08:59:08 +02:00
/**
2019-01-30 16:18:44 +01:00
* Returns convert array multidimensional to string whit gluer .
*
* @ param array $array to convert
* @ param string glue to implode
*/
function convert_array_multi ( $array , $glue )
{
2018-07-23 08:59:08 +02:00
$result = '' ;
foreach ( $array as $item ) {
if ( is_array ( $item )) {
2019-01-30 16:18:44 +01:00
$result .= convert_array_multi ( $item , $glue ) . $glue ;
2018-07-23 08:59:08 +02:00
} else {
2019-01-30 16:18:44 +01:00
$result .= $item . $glue ;
2018-07-23 08:59:08 +02:00
}
}
2019-01-30 16:18:44 +01:00
$result = substr ( $result , 0 , ( 0 - strlen ( $glue )));
2018-07-23 08:59:08 +02:00
return $result ;
}
2022-03-24 13:53:22 +01:00
/**
* Evaluate if the chars of coming variable has in the range stablished .
*
* @ param string $string String for be evaluated .
* @ param array $ranges Ranges for valid chars . Min : [ x <= Y ] Max : [ Y > x ] .
* Example of valid ranges : [ '32:126' , '150:188' ] .
*
* @ return boolean .
*/
function evaluate_ascii_valid_string ( string $string = '' , array $ranges = [ '33:38' , '40:126' ])
{
if ( empty ( $string ) === true ) {
return false ;
}
$countChars = strlen ( $string );
// Let's explore all the chars.
for ( $i = 0 ; $i < $countChars ; $i ++ ) {
// Get ascii number of the char.
$asciiNumber = ord ( $string [ $i ]);
// Check in all ranges.
$rangeValidation = false ;
foreach ( $ranges as $range ) {
list ( $minRangeValue , $maxRangeValue ) = explode ( ':' , $range , 2 );
// Check if is in range.
if ( $asciiNumber > ( int ) $minRangeValue && $asciiNumber < ( int ) $maxRangeValue ) {
$rangeValidation = true ;
}
}
// None of the ranges was validated.
if ( $rangeValidation === false ) {
return false ;
}
}
return true ;
}