Skip to content

PHP Functions: Library

Use functions described below by including /_framework/functions/x_library.php.

Dependencies

  • PHP 7.1-7.4
  • PHP 8.0-8.4

PHP-Modules

  • sockets: Required for the function x_connection_check().
  • mbstring: Required for preg_match functions.
  • exif: For Thumbnail Functions.
  • gd: The function uses GD functions like imagecreatetruecolor(), imagecolorallocate(), imagefilledrectangle(), imageellipse(), imagefttext(), and imagejpeg().

Various Functions

Library Function Name Description Parameter
x_copy_directory($src, $dst) Copy content of a folder recursively to another folder. - src: Folder Path (FULL) to Copy.
- dst: Destination Folder Name to Copy to.
x_htaccess_secure($path) Secure a folder by placing an .htaccess file to deny folder content. - path: Path where the .htaccess should be placed.
x_getRelativeFolderFromURL($url) Extract relative path from full URL string. - url: The URL the relative folder should be extracted from.
x_firstimagetext Get first image out of "a" tag from text string. - text: Input text containing HTML.
- all: Whether to return all image tags or just the first one.
x_connection_check Check a connection with fsockopen. - host: Hostname or IP address to check the connection to.
- port: Port number to check.
- timeout: Timeout duration in seconds.
x_inCLI Check if the current script execution is handled via CGI and not in a web browser. Checks if the script is running in a Command Line Interface (CLI) environment and returns true if it is, false otherwise.
x_rmdir Recursively delete a folder. - dir: Directory path to be deleted recursively.
x_html_redirect Spawn HTML redirect meta tag for simple HTML redirects. - url: URL to redirect to.
- seconds: Number of seconds before the redirection occurs.

Validation Functions

Function Description Explanation
x_isset($val) Checks if a value is not null and has a length greater than 0 after trimming. The function returns true if $val is set, is not empty after trimming, and has a length greater than 0. The @ operator suppresses errors in case $val is undefined, and the null coalescing operator (??) ensures a fallback to an empty string if $val is null.
x_imgValid($url) Validates if a string is a valid image URL by checking its existence and if it is an image. The function checks if $url is set and is a string with a length greater than 3. It then attempts to fetch the image size using getimagesize(), which confirms if the URL points to a valid image. If these conditions aren't met, it returns false.
x_hsc($string) Safely encodes special characters in a string to HTML entities. The function uses htmlspecialchars() to convert special characters to HTML entities, helping to prevent XSS attacks. If $string is null or undefined, it defaults to an empty string.
x_het($string) Converts all applicable characters in a string to HTML entities. The function uses htmlentities() to convert all applicable characters to HTML entities, ensuring that the string is safe for output in an HTML context. Similar to x_hsc(), it handles null or undefined values gracefully.
x_trim($string) Trims whitespace from both sides of a string. The function uses trim() to remove whitespace from the beginning and end of the string. The @ operator handles cases where $string might be undefined.
x_contains_cyrillic($val) Checks if a string contains Cyrillic characters. The function uses a regular expression to check if $val contains any Cyrillic characters. If it does, it returns true; otherwise, it returns false. The regular expression pattern [\p{Cyrillic}] matches any Cyrillic character.
x_contains_bad_word($val) Checks if a string contains the word "porn" or "Porn". The function checks if the string contains " porn " or " Porn " (with spaces around). If either is found, the function returns false (indicating it's a "bad" word). If neither is found, it returns true.
x_contains_url($val) Checks if a string contains an HTTP or HTTPS URL. The function looks for the substrings "http://" or "https://" within $val. If found, it returns false (indicating that a URL is present). If neither is found, it returns true.
x_getint($val) Retrieves a value from the $_GET array if it's numeric. The function checks if the value associated with the key $val in the $_GET array is numeric. If it is, it returns the value; otherwise, it returns false. The @ operator handles cases where the key does not exist.
x_postint($val) Retrieves a value from the $_POST array if it's numeric. Similar to x_getint(), this function checks if the value associated with the key $val in the $_POST array is numeric. If so, it returns the value; otherwise, it returns false. The @ operator handles cases where the key does not exist.
x_get($val) Retrieves a value from the $_GET array if it exists. The function checks if the key $val exists in the $_GET array. If it does, it returns the corresponding value; otherwise, it returns false. The @ operator handles undefined keys.
x_post($val) Retrieves a value from the $_POST array if it exists. Similar to x_get(), this function checks if the key $val exists in the $_POST array. If it does, it returns the corresponding value; otherwise, it returns false. The @ operator handles undefined keys.
x_datediff_before($d1, $d2, $length) Checks if the difference between two dates is greater than a specified length (in days). The function calculates the difference between two dates ($d1 and $d2) and returns true if the difference (in days) is greater than $length. If either date is false, it returns false. The date_diff() function is used to calculate the interval, and format('%a') returns the difference in days.