Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F24328200
core.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
core.php
View Options
<?php
namespace
Keruald
;
/**
* Keruald, core libraries for Pluton and Xen engines.
*
* Global functions
*/
///
/// Strings
///
/**
* Pads a multibytes string to a certain length with another string
*
* @param string $str the input string
* @param int $pad_length the target string size
* @param string $pad_string the padding characters (optional, default is space)
* @param int $pad_type STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH (optional, default is STR_PAD_RIGHT)
* @param string the character encoding (optional)
*
* @return string the padded string
*
*/
function
mb_str_pad
(
$input
,
$pad_length
,
$pad_string
=
' '
,
$pad_type
=
STR_PAD_RIGHT
,
$encoding
=
null
)
{
// Inspired by Ronald Ulysses Swanson method
// http://stackoverflow.com/a/27194169/1930997
// who followed the str_pad PHP implementation.
if
(
$encoding
===
null
)
{
$encoding
=
mb_internal_encoding
();
}
$padBefore
=
$pad_type
===
STR_PAD_BOTH
||
$pad_type
===
STR_PAD_LEFT
;
$padAfter
=
$pad_type
===
STR_PAD_BOTH
||
$pad_type
===
STR_PAD_RIGHT
;
$pad_length
-=
mb_strlen
(
$input
,
$encoding
);
if
(
$padBefore
&&
$padAfter
)
{
$targetLength
=
$pad_length
/
2
;
}
else
{
$targetLength
=
$pad_length
;
}
$strToRepeatLength
=
mb_strlen
(
$pad_string
,
$encoding
);
$repeatTimes
=
ceil
(
$targetLength
/
$strToRepeatLength
);
$repeatedString
=
str_repeat
(
$pad_string
,
max
(
0
,
$repeatTimes
));
// safe if used with valid Unicode sequences (any charset)
$paddedString
=
''
;
if
(
$padBefore
)
{
$paddedString
=
mb_substr
(
$repeatedString
,
0
,
floor
(
$targetLength
),
$encoding
);
}
$paddedString
.=
$input
;
if
(
$padAfter
)
{
$paddedString
.=
mb_substr
(
$repeatedString
,
0
,
ceil
(
$targetLength
),
$encoding
);
}
return
$paddedString
;
}
///
/// Identifiers
///
/**
* Generates a RFC 4211 compliant v4 UUID (random-based)
*
* @return string The UUID
*/
function
uuid
()
{
//Code by Andrew Moore
//See http://php.net/manual/en/function.uniqid.php#94959
// https://www.ietf.org/rfc/rfc4122.txt
return
sprintf
(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x'
,
// 32 bits for "time_low"
mt_rand
(
0
,
0xffff
),
mt_rand
(
0
,
0xffff
),
// 16 bits for "time_mid"
mt_rand
(
0
,
0xffff
),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand
(
0
,
0x0fff
)
|
0x4000
,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand
(
0
,
0x3fff
)
|
0x8000
,
// 48 bits for "node"
mt_rand
(
0
,
0xffff
),
mt_rand
(
0
,
0xffff
),
mt_rand
(
0
,
0xffff
)
);
}
///
/// Error and debug
///
/**
* Prints human-readable information about a variable, wrapped in a <pre> block
*
* @param mixed $variable the variable to dump
*/
function
dprint_r
(
$variable
)
{
echo
'<pre>'
;
print_r
(
$variable
);
echo
'</pre>'
;
}
/**
* Prints human-readable information about a variable, wrapped in a <pre> block
* then dies
*
* @param mixed $variable the variable to dump
*/
function
dieprint_r
(
$variable
)
{
dprint_r
(
$variable
);
die
;
};
///
/// Client information
///
/**
* Returns the full header or the IP part of it
*
* @param string $value The header value
* @return string the IP part
*/
function
extract_client_ip_from_header
(
$value
)
{
if
(
strpos
(
$value
,
','
)
!==
false
)
{
//Header contains 'clientIP, proxyIP, anotherProxyIP'
//The first value is so the one to return.
//See draft-ietf-appsawg-http-forwarded-10.
$ips
=
explode
(
','
,
$value
,
2
);
return
trim
(
$ips
[
0
]);
}
return
$value
;
}
/**
* Gets remote IP address.
*
* This is intended as a drop-in replacement for $_SERVER['REMOTE_ADDR'],
* which takes in consideration proxy values.
*/
function
get_remote_addr
()
{
$candidates
=
[
//Standard header provided by draft-ietf-appsawg-http-forwarded-10
'HTTP_X_FORWARDED_FOR'
,
//Legacy headers
'HTTP_CLIENT_IP'
,
'HTTP_FORWARDED'
,
'HTTP_FORWARDED_FOR'
,
'HTTP_X_CLUSTER_CLIENT_IP'
,
'HTTP_X_FORWARDED'
,
//Default header if no proxy information could be detected
'REMOTE_ADDR'
,
];
foreach
(
$candidates
as
$candidate
)
{
if
(
array_key_exists
(
$candidate
,
$_SERVER
))
{
return
extract_client_ip_from_header
(
$_SERVER
[
$candidate
]);
}
}
return
''
;
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Mon, Feb 16, 10:13 (2 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3392120
Default Alt Text
core.php (4 KB)
Attached To
Mode
rKGF Keruald global functions
Attached
Detach File
Event Timeline
Log In to Comment