toropyga / base
Base libraries from Toropyga
Requires
- php: >=7.0.0
- ext-iconv: *
- ext-json: *
- ext-mbstring: *
- ext-openssl: *
- ext-simplexml: *
README
Base PHP functions for "every day" use.
Contents
- General information
- Installation
- Usage
- Functions
- Convert an array to an object
- Convert an object to an array
- Determine the IP address
- Debug function
- Password check
- Date check
- Convert text to a given encoding
- Detect text encoding
- Escape data
- Unescape data
- Calculate a string hash
- Set HEADERS
- Calculate pagination parameters
- Build XML from an array
- Get Json or an array from XML
- Set a Cookie
- Reversible string encryption
General information
The Base class is a set of base functions for "every day" use. Requires PHP version 5 or higher.
Installation
The recommended way to install the NetContent library is using Composer:
composer require toropyga/base
or simply download and save the library to the desired directory.
Usage
require_once("vendor/autoload.php");
after that add
use Toropyga\Base;
Functions
Convert an array to an object
(for unification and convenience)
/** * @param array $data - array * @return object * * function ArrayToObj ($data); */
Example:
$data = Base::ArrayToObj($data);
Convert an object to an array
(only for unification and convenience)
/** * @param object $data - object * @return array * * function ObjToArray ($data); */
Example:
$data = Base::ObjToArray($data);
Determine the IP address
/** * Returns an associative array with the keys: * 'ip' - the current IP address * 'proxy' - the IP address of the proxy server used, if it can be determined * @return array * * function getIP (); */
Example:
$IP = Base::getIP();
Debug function
for displaying on screen or returning formatted data contained in the passed $array variable
/** * @param mixed $array * @param bool $print - output the data to the screen (true/false) * @return string * * function dump ($array = array(), $print = true); */
Example:
Base::dump($array, true); or $dump = Base::dump($array, false);
Password check
against security requirements
/** * @param string $password - the string being checked * @param int $len - the string must be at least this many characters long * @param int $type - string type * 0 => string contains at least one digit, at least one special character, at least one lowercase Latin letter, at least one uppercase Latin letter; * 1 => string contains at least one digit, at least one lowercase Latin letter, at least one uppercase Latin letter; * 2 => string contains at least one digit, at least one special character, at least one Latin letter; * 3 => string contains at least one digit, at least one Latin letter; * 4 => string contains at least one Latin letter; * 5 => string contains at least one digit; * @return bool * * function checkPassword ($password, $len = 6, $type = 0); */
Example:
if (Base::checkPassword ($password, 8, 0)) { echo "Valid password"; } else { echo "Password is not valid"; }
Date check
for validity
/** * @param string $date - the date being checked * @param string $format - format of the checked date, 'd/m/Y H:i:s' by default * @return bool * * function validateDate($date, $format = 'd/m/Y H:i:s'); */
Example:
$date = '09.08.2020'; $format = 'd.m.Y'; if (Base::validateDate($date, $format)) { echo "Valid date"; } else { echo "Wrong date" }
Convert text to a given encoding
/** * @param string $line - string containing the text * @param string $enc - target encoding, utf-8 by default ('utf-8', 'ascii', 'cp1251', 'KOI8-R', 'CP866', 'KOI8-U', etc.) * * function convertLine ($line, $enc = 'utf-8'); */
Example:
$text = Base::convertLine($line, 'cp1251');
Detect text encoding
Works even if the mb_detect_encoding function fails.
/** * @param string $string - string containing the text * @param int $pattern_size - maximum string length to parse * * function detect_encoding ($string, $pattern_size = 50); */
Example:
$code = Base::detect_encoding($string);
or
$code = Base::detect_encoding($string, 100);
Escape data
protects against malicious requests
/** * @param array|string $array - string or array of data * @param string $code - text encoding, 'utf-8' by default * @return array|string * * function screeningData ($array, $code = 'utf-8'); */
Example:
$array = Base::screeningData($array, 'cp1251');
Unescape data
/** * Unescape data * @param array $array * @return array|string * * function unscreeningData($array); */
Example:
$array = Base::unscreeningData($array);
Calculate a string hash
You can optionally set two global variables beforehand:
- CRYPT_TYPE - encryption algorithm (see $alg)
- CRYPT_KEY - encryption key (see $key)
/** * @param string $string - the string being hashed * @param string $alg - hashing algorithm (type of function used), md5 by default * 'password' => password_hash($key, PASSWORD_DEFAULT); * 'password_bcrypt' => password_hash($key, PASSWORD_BCRYPT); * 'crypt' => crypt($key); * 'crypt_site': => crypt($key, CRYPT_KEY); * 'sha1' => sha1($key); * 'hash' => hash('sha256', $key); * 'md5' => md5($key); * @param string $key - encryption key for the 'crypt_site' algorithm * @return bool|string * * function getKeyHash ($string, $alg = '', $key = ''); */
Example:
$hash = Base::getKeyHash($sfring, 'sha1');
or
$hash = Base::getKeyHash($sfring, 'crypt_site', 'your_code_word');
Set HEADERS
/** * @param string $type - content type ('text/html', 'application/json', 'text/xml') * @param string $CODE - text encoding ('utf-8', 'ascii', 'cp1251', 'KOI8-R', 'CP866', 'KOI8-U', 'ISO-8859-1', etc.) * @param array $methods - allowed interaction methods ('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE', 'PATCH') * @param string $servers - servers allowed to interact ('*' - any, <origin> - a single source [https://google.com], null - undesirable parameter) * @param string $protocol - data transfer protocol (http, https, ftp) * @param integer $lifetime - page "lifetime" for a secure connection (https) in seconds * @return bool * * function setHeaders ($type = 'text/html', $CODE = 'utf-8', $methods = array('GET', 'POST', 'OPTIONS'), $servers = '*', $protocol = 'https', $lifetime = 2400); */
Example:
Base::setHeaders('text/json', 'utf-8', array('GET', 'POST', 'OPTIONS'), '*', 'https', 1200);
Calculate pagination parameters
/** * @param int $sum - total number of records * @param int $page - current page * @param int $res_on_page - number of records per page (LIMIT) * @param int $pages_show - number of displayed page bookmarks * @return array * keys (response keys): * sum - total number of records * pages - total number of pages * page - the displayed page number * size - maximum number of records per page * size_now - number of records on the current page * begin - page at which the current range starts * end - page at which the current range ends * forward - whether to show a quick jump to the beginning (true/false) * back - whether to show a quick jump to the end (true/false) * * function getPagination ($sum, $page = 1, $res_on_page = 20, $pages_show = 5); */
Example:
$pagination = Base::getPagination(187, 1, 25, 5);
Build XML from an array
/** * @param array $array - the passed array * @param string|null $title - name of the primary tag, 'root' by default * @param bool $first - first occurrence; supports internal recursion — when processing an array of arrays, headers are not written on subsequent calls * @return string * * function xml_encode ($array, $title = null, $first = true); */
Example:
$xml = Base::xml_encode ($array, $title, $first);
Get Json or an array from XML
/** * @param string $xml - data in XML format * @param boolean $array - return as an array (true) or return as Json (false) * @return mixed * * function xml_decode ($xml, $array = true) */
Example:
$json = Base::xml_decode($xml, false);
Set a Cookie
/** * @param string $text - Cookie value * @param string $name - Cookie name * @param int $live_time - Cookie lifetime in seconds from the current moment * @param string $domain - domain determines which domain the cookie is available on * @param boolean $secure - transmit the Cookie only over the HTTPS protocol * @param boolean $http_only - forbid any access to the Cookie from JavaScript * @param string $samesite - sets cross-site request availability for the Cookie('', 'Strict', 'Lax') * @param string $path - URL path prefix for the Cookie * * function setCookie ($text, $name, $live_time = 86400, $domain = 'localhost', $secure = true, $http_only = false, $samesite = 'lax', $path = '/') */
Example:
Base::setCookie ($text, $name, 3600, $domain = 'localhost', true, true);
Reversible string encryption
Encryption
/** * Encrypt a string * The encryption key is specified in the settings * @param string $string - the string being encrypted * * function MEncrypt ($string) */
Example:
$encoded_text = Base::MEncrypt ('text');
Decryption
/** * Decrypt a string encrypted by the MEncrypt function * @param string $ciphertext - the string being decrypted * * public function MDecrypt ($ciphertext) */
Example:
$decoded_text = Base::MEncrypt ($encoded_text);