class U

Some really small, simple, and self-contained utility public static functions

Methods

static 
die_with_error_log($msg, $extra = false, $prefix = "DIE:")

No description

static 
echo_log($msg)

No description

static 
session_safe_id()

No description

static 
print_stack_trace()

No description

static 
get($arr, $key, $default = null)

Produce a Python-style get() to avoid use of ternary operator

static 
htmlpre_utf8($string)

No description

static 
htmlspec_utf8($string)

No description

static 
htmlent_utf8($string)

No description

static 
safe_href($string)

No description

static 
lti_sha256($val)

No description

static 
route_get_local_path($dir)

No description

static 
get_request_document()

Get the last bit of the path

static 
get_base_url($url)

Get the protocol, host, and port from an absolute URL

static 
get_rest_path($uri = false)

Get the path to the current request, w/o trailing slash

static 
get_rest_parent($uri = false)

Get the path to one above the current request, w/o trailing slash

static 
parse_rest_path($uri = false, $SERVER_SCRIPT_NAME = false)

Get the controller for the current request

static 
rest_path($uri = false, $SERVER_SCRIPT_NAME = false)

Return a rest-path

static 
addSession($url, $force = false)

No description

static 
reconstruct_query($baseurl, $newparms = false)

No description

static 
add_url_parm($url, $key, $val)

No description

static 
absolute_url_ref($url)

No description

static 
absolute_url($url)

No description

static 
remove_relative_path($path)

Remove any relative elements from a path

static 
apache_request_headers()

No description

static 
http_response_code($newcode = NULL)

No description

static 
isCli()

No description

static 
lmsDie($message = false)

No description

static 
line_out($output)

No description

static 
error_out($output)

No description

static 
success_out($output)

No description

static 
startsWith($haystack, $needle)

No description

static 
endsWith($haystack, $needle)

No description

static 
goodFolder($folder)

No description

static 
conservativeUrl($url)

No description

static 
curPHPUrl()

Return the URL as seen by PHP (no query string or parameters)

static 
array_Integer_Serialize($arar)

Tightly serialize an integer-only PHP array

static 
array_Integer_Serialize_Map($a, $b)

No description

static 
array_Integer_Deserialize($input)

Deserialize an tightly serialized integer-only PHP array

static 
array_kshift($arr)

Pull off the first element of a key/value array

static 
allow_track()

No description

static 
safe_var_dump($x)

No description

static 
safe_array($inp)

No description

static 
conversion_time($time = "now")

Give the current time in the "conversion format"

static 
iso8601($time = "now")

No description

static 
apcAvailable()

Return if APC cache is available (Deprecated)

static 
apcuAvailable()

Return if APCU cache is available

static 
appCacheGet($key, $default = null)

No description

static 
appCacheSet($key, $value, $ttl = 0)

No description

static 
appCacheDelete($key)

No description

static 
getCaller($count = 1)

No description

static 
displaySize($size)

No description

static 
createGUID()

Create a unique GUID return string

static 
isGUIDValid($guid)

No description

static 
getServerBase($url)

No description

static 
isValidCSSColor($color)

Validate a CSS color value

static 
strlen($string)

Build a PHP 7 strlen for PHP 8.2 and later

static 
isEmpty($string)

Determine if a variable is empty

static 
isNotEmpty($string)

Imitate StringUtils.isNotEmpty()

static 
isKeyNotEmpty(array $collection, string $key)

Check if a key in an array is not empty

static 
escapeHtml($string)

Make an htmlentities() that is more tolerant than the PHP 8.2 version.

static 
json_decode($string)

Build an "always works" json_decode()

Details

at line 10
static die_with_error_log($msg, $extra = false, $prefix = "DIE:")

Parameters

$msg
$extra
$prefix

at line 16
static echo_log($msg)

Parameters

$msg

at line 21
static session_safe_id()

at line 26
static print_stack_trace()

at line 36
static get($arr, $key, $default = null)

Produce a Python-style get() to avoid use of ternary operator

Parameters

$arr
$key
$default

at line 43
static htmlpre_utf8($string)

Parameters

$string

at line 47
static htmlspec_utf8($string)

Parameters

$string

at line 52
static htmlent_utf8($string)

Parameters

$string

at line 57
static safe_href($string)

Parameters

$string

at line 64
static lti_sha256($val)

Parameters

$val

at line 69
static route_get_local_path($dir)

Parameters

$dir

at line 86
static get_request_document()

Get the last bit of the path

input: /py4e/lessons/intro?x=2 output: intro

at line 104
static get_base_url($url)

Get the protocol, host, and port from an absolute URL

input: http://localhost:8888/tsugi output: http://localhost:8888

Parameters

$url

at line 121
static get_rest_path($uri = false)

Get the path to the current request, w/o trailing slash

input: /py4e/lessons/intro?x=2 output: /py4e/lessons/intro

input: /py4e/lessons/intro/?x=2 output: /py4e/lessons/intro

Parameters

$uri

at line 141
static get_rest_parent($uri = false)

Get the path to one above the current request, w/o trailing slash

input: /py4e/lessons/intro?x=2 output: /py4e/lessons

input: /py4e/lessons/intro/?x=2 output: /py4e/lessons/intro

Parameters

$uri

at line 168
static parse_rest_path($uri = false, $SERVER_SCRIPT_NAME = false)

Get the controller for the current request

executing script: /py4e/koseu.php input: /py4e/lessons/intro?x=2 output: (/py4e, lessons, intro)

input: /py4e/lessons/intro/?x=2 output: /py4e/lessons/intro

Parameters

$uri
$SERVER_SCRIPT_NAME

at line 229
static rest_path($uri = false, $SERVER_SCRIPT_NAME = false)

Return a rest-path

This knows the current working folder we are in. The model is that a folder is an "application" with many possible controllers and within each controller there are actions followed by parameters.

for example, assume these files exist in a folder:

../tsugi/mod/zap/index.php
../tsugi/mod/zap/rows.php
../tsugi/mod/zap/store.php

If this URL were called with .htaccess and tsugi.php set up, rows.php would be run and this would be the output:

http://localhost:8888/tsugi/mod/zap/rows/add/1/2
base_url: http://localhost:8888
parent:  /tsugi/mod/zap
current:  /tsugi/mod/zap/rows
controller: rows
action: add
parameters: (1, 2)
full: /tsugi/mod/zap/rows/add/1/2

As a special case, when the index.php runs (not through tsugi.php) The results are as follows:

http://localhost:8888/tsugi/mod/zap/
base_url: http://localhost:8888
parent:  /tsugi/mod/zap
current:  /tsugi/mod/zap
controller: empty string
action: empty string
parameters: empty array
full: /tsugi/mod/zap

Parameters

$uri
$SERVER_SCRIPT_NAME

at line 262
static addSession($url, $force = false)

Parameters

$url
$force

at line 286
static reconstruct_query($baseurl, $newparms = false)

Parameters

$baseurl
$newparms

at line 299
static add_url_parm($url, $key, $val)

Parameters

$url
$key
$val

at line 306
static absolute_url_ref($url)

Parameters

$url

at line 310
static absolute_url($url)

Parameters

$url

at line 329
static remove_relative_path($path)

Remove any relative elements from a path

Before After a/b/c a/b/c a/b/c/ a/b/c/ a/./c/ a/c/ a/../c/ c/

Parameters

$path

at line 346
static apache_request_headers()

at line 361
static http_response_code($newcode = NULL)

Parameters

$newcode

at line 373
static isCli()

at line 381
static lmsDie($message = false)

Parameters

$message

at line 397
static line_out($output)

Parameters

$output

at line 402
static error_out($output)

Parameters

$output

at line 407
static success_out($output)

Parameters

$output

at line 413
static startsWith($haystack, $needle)

Parameters

$haystack
$needle

at line 418
static endsWith($haystack, $needle)

Parameters

$haystack
$needle

at line 423
static goodFolder($folder)

Parameters

$folder

at line 427
static conservativeUrl($url)

Parameters

$url

at line 442
static curPHPUrl()

Return the URL as seen by PHP (no query string or parameters)

Borrowed from from_request on OAuthRequest.php

at line 466
static array_Integer_Serialize($arar)

Tightly serialize an integer-only PHP array

$arar = Array ( 1 => 42 ,2 => 43, 3 => 44 ); $str = U::array_Integer_Serialize($arar); echo($str); // 1=42,2=43,3=44

https://stackoverflow.com/questions/30231476/i-want-to-array-key-and-array-value-comma-separated-string

Parameters

$arar

at line 471
static array_Integer_Serialize_Map($a, $b)

Parameters

$a
$b

at line 488
static array_Integer_Deserialize($input)

Deserialize an tightly serialized integer-only PHP array

$str = '1=42,2=43,3=44'; $arar = U::array_Integer_Deserialize($str); print_r($arar); // Array ( '1' => 42 ,'2' => 43, '3' => 44 );

https://stackoverflow.com/questions/4923951/php-split-string-in-key-value-pairs

Parameters

$input

at line 519
static array_kshift($arr)

Pull off the first element of a key/value array

$arr = array('x'=>'ball','y'=>'hat','z'=>'apple'); print_r($arr); print_r(array_kshift($arr)); // [x] => ball print_r($arr);

http://php.net/manual/en/function.array-shift.php#84179

Parameters

$arr

at line 535
static allow_track()

at line 546
static safe_var_dump($x)

Parameters

$x

at line 557
static safe_array($inp)

Parameters

$inp

at line 573
static conversion_time($time = "now")

Give the current time in the "conversion format"

201711261315

Parameters

$time

at line 579
static iso8601($time = "now")

Parameters

$time

at line 588
static apcAvailable()

Return if APC cache is available (Deprecated)

at line 596
static apcuAvailable()

Return if APCU cache is available

at line 601
static appCacheGet($key, $default = null)

Parameters

$key
$default

at line 609
static appCacheSet($key, $value, $ttl = 0)

Parameters

$key
$value
$ttl

at line 615
static appCacheDelete($key)

Parameters

$key

at line 631
static getCaller($count = 1)

Parameters

$count

at line 652
static displaySize($size)

Parameters

$size

at line 671
static createGUID()

Create a unique GUID return string

https://www.texelate.co.uk/blog/create-a-guid-with-php

at line 693
static isGUIDValid($guid)

Parameters

$guid

at line 698
static getServerBase($url)

Parameters

$url

at line 710
static isValidCSSColor($color)

Validate a CSS color value

Parameters

$color

at line 725
static strlen($string)

Build a PHP 7 strlen for PHP 8.2 and later

This is needed because in PHP 8.2 strlen() demands a string (i.e. can't handle false, etc)

Parameters

$string

at line 746
static isEmpty($string)

Determine if a variable is empty

This is needed because in PHP 8.2 strlen() demands a string (i.e. can't handle false, etc) Sadly, empty('0') becomes falsey and so returns true - What??

The method name is taken from Java since its rules are corect.

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#isEmpty--

Parameters

$string

at line 760
static isNotEmpty($string)

Imitate StringUtils.isNotEmpty()

Sigh, Java 8.0 does not have isNotEmpty - which is super useful so we imitate

https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html

It is the little things - sigh.

Parameters

$string

at line 767
static isKeyNotEmpty(array $collection, string $key)

Check if a key in an array is not empty

Parameters

array $collection
string $key

at line 776
static escapeHtml($string)

Make an htmlentities() that is more tolerant than the PHP 8.2 version.

https://commons.apache.org/proper/commons-text/apidocs/org/apache/commons/text/StringEscapeUtils.html

Parameters

$string

at line 787
static json_decode($string)

Build an "always works" json_decode()

This is needed because in PHP 8.2 json_decode() demands a string (i.e. can't handle false, etc)

Parameters

$string