123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?php
- /**
- * Basic Ubilling GET/POST abstraction and filtering class
- */
- class ubRouting {
- /**
- * .- <O> -. .-====-. ,-------. .-=<>=-.
- * /_-\'''/-_\ / / '' \ \ |,-----.| /__----__\
- * |/ o) (o \| | | ')(' | | /,'-----'.\ |/ (')(') \|
- * \ ._. / \ \ / / {_/(') (')\_} \ __ /
- * ,>-_,,,_-<. >'=jf='< `. _ .' ,'--__--'.
- * / . \ / \ /'-___-'\ / :| \
- * (_) . (_) / \ / \ (_) :| (_)
- * \_-----'____--/ (_) (_) (_)_______(_) |___:|____|
- * \___________/ |________| \_______/ |_________|
- */
- /**
- * Creates new Routing object instance
- */
- public function __construct() {
- //What did you expect to see here?
- }
- /**
- * Checks is all of variables array present in GET scope
- *
- * @param array/string $params array of variable names to check or single variable name as string
- * @param bool $ignoreEmpty ignore or not existing variables with empty values (like wf_Check)
- * @param bool $atleastOneExists returns "true" when encounters the very first existent GET param. Respects the $ignoreEmpty parameter.
- *
- * @return bool
- */
- public static function checkGet($params, $ignoreEmpty = true, $atleastOneExists = false) {
- if (!empty($params)) {
- if (!is_array($params)) {
- //single param check
- $params = array($params);
- }
- foreach ($params as $eachparam) {
- if (!isset($_GET[$eachparam])) {
- if ($atleastOneExists) {
- // traversing through the array till existent GET param found or the end of the array
- continue;
- } else {
- return (false);
- }
- } elseif ($atleastOneExists and !$ignoreEmpty) {
- return (true);
- }
- if ($ignoreEmpty) {
- if (empty($_GET[$eachparam])) {
- if ($atleastOneExists) {
- // traversing through the array till existent and non-empty GET param found or the end of the array
- continue;
- } else {
- return (false);
- }
- } elseif ($atleastOneExists) {
- return (true);
- }
- }
- }
- if ($atleastOneExists) {
- // if "$atleastOneExists = true" and we got here - none of the GET parameters were existent or non-empty then,
- // and we failed to find at least one
- return (false);
- } else {
- return (true);
- }
- } else {
- throw new Exception('EX_PARAMS_EMPTY');
- }
- }
- /**
- * Checks is all of variables array present in POST scope
- *
- * @param array/string $params array of variable names to check or single variable name as string
- * @param bool $ignoreEmpty ignore or not existing variables with empty values (like wf_Check)
- * @param bool $atleastOneExists returns "true" when encounters the very first existent GET param. Respects the $ignoreEmpty parameter.
- *
- * @return bool
- */
- public static function checkPost($params, $ignoreEmpty = true, $atleastOneExists = false) {
- if (!empty($params)) {
- if (!is_array($params)) {
- //single param check
- $params = array($params);
- }
- foreach ($params as $eachparam) {
- if (!isset($_POST[$eachparam])) {
- if ($atleastOneExists) {
- // traversing through the array till existent POST param found or the end of the array
- continue;
- } else {
- return (false);
- }
- } elseif ($atleastOneExists and !$ignoreEmpty) {
- return (true);
- }
- if ($ignoreEmpty) {
- if (empty($_POST[$eachparam])) {
- if ($atleastOneExists) {
- // traversing through the array till existent and non-empty POST param found or the end of the array
- continue;
- } else {
- return (false);
- }
- } elseif ($atleastOneExists) {
- return (true);
- }
- }
- }
- if ($atleastOneExists) {
- // if "$atleastOneExists = true" and we got here - none of the POST parameters were existent or non-empty then,
- // and we failed to find at least one
- return (false);
- } else {
- return (true);
- }
- } else {
- throw new Exception('EX_PARAMS_EMPTY');
- }
- }
- /**
- * Returns filtered data
- *
- * @param mixed $rawData data to be filtered
- * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float, login, safe, gigasafe
- * @param string|array/filter name $callback callback function name or names array to filter variable value. Or const filter name of php.net/filter
- *
- * @return mixed|false
- *
- * @throws Exception
- */
- public static function filters($rawData, $filtering = 'raw', $callback = '') {
- $result = false;
- switch ($filtering) {
- case 'raw':
- return ($rawData);
- break;
- case 'int':
- return (preg_replace("#[^0-9]#Uis", '', $rawData));
- break;
- case 'mres':
- return (mysql_real_escape_string($rawData));
- break;
- case 'vf':
- return (preg_replace("#[~@\+\?\%\/\;=\*\>\<\"\'\-]#Uis", '', $rawData));
- break;
- case 'nb':
- return (preg_replace('/\0/s', '', $rawData));
- break;
- case 'float':
- $filteredResult = preg_replace("#[^0-9.]#Uis", '', $rawData);
- if (is_numeric($filteredResult)) {
- return ($filteredResult);
- } else {
- return (false);
- }
- break;
- case 'login':
- $filteredResult = str_replace(' ', '_', $rawData);
- $loginAllowedChars = 'a-z0-9A-Z_\.' . $callback;
- $filteredLogin = preg_replace("#[^" . $loginAllowedChars . "]#Uis", '', $filteredResult);
- return ($filteredLogin);
- break;
- case 'safe':
- $rawData = preg_replace('/\0/s', '', $rawData);
- if (strpos($callback, 'HTML') !== false) {
- $callback = str_replace('HTML', '', $rawData);
- } else {
- $rawData = self::replaceQuotes($rawData);
- $rawData = strip_tags($rawData);
- $rawData = str_replace(array("'", '`'), '’', $rawData);
- }
- $allowedChars = 'a-zA-Z0-9А-Яа-яЁёЇїІіЄєҐґ\w++«»№’=_\ ,\.\-:;!?\(\){}\/\r\n\x{200d}\x{2600}-\x{1FAFF}' . $callback;
- $regex = '#[^' . $allowedChars . ']#u';
- $filteredData = preg_replace($regex, '', $rawData);
- $filteredData = str_replace('--', '', $filteredData);
- return ($filteredData);
- case 'gigasafe':
- $rawData = preg_replace('/\0/s', '', $rawData);
- $allowedChars = 'a-zA-Z0-9' . $callback;
- $regex = '#[^' . $allowedChars . ']#u';
- return (preg_replace($regex, '', $rawData));
- case 'fi':
- if (!empty($callback)) {
- return (filter_var($rawData, $callback));
- } else {
- throw new Exception('EX_FILTER_EMPTY');
- }
- break;
- case 'callback':
- if (!empty($callback)) {
- //single callback function
- if (!is_array($callback)) {
- if (function_exists($callback)) {
- return ($callback($rawData));
- } else {
- throw new Exception('EX_CALLBACK_NOT_DEFINED');
- }
- } else {
- $filteredResult = $rawData;
- //multiple callback functions
- foreach ($callback as $io => $eachCallbackFunction) {
- if (function_exists($eachCallbackFunction)) {
- $filteredResult = $eachCallbackFunction($filteredResult);
- } else {
- throw new Exception('EX_CALLBACK_NOT_DEFINED');
- }
- }
- return ($filteredResult);
- }
- } else {
- throw new Exception('EX_CALLBACK_EMPTY');
- }
- break;
- default:
- throw new Exception('EX_WRONG_FILTERING_MODE');
- break;
- }
- return ($result);
- }
- /**
- * Replaces double quotes in a string with special characters.
- *
- * This method takes a string as input and replaces all occurrences of double quotes with special characters.
- *
- * @param string $string The input string to be processed.
- * @return string The processed string with double quotes replaced by special characters.
- */
- public static function replaceQuotes($string) {
- return (preg_replace('/"([^"]*)"/', '«$1»', $string));
- }
- /**
- * Returns some variable value with optional filtering from GET scope
- *
- * @param string $name name of variable to extract
- * @param string $filtering filtering options. Possible values: raw, int, mres, callback
- * @param string|array $callback callback function name or names array to filter variable value
- *
- * @return mixed|false
- */
- public static function get($name, $filtering = 'raw', $callback = '') {
- $result = false;
- if (isset($_GET[$name])) {
- return (self::filters($_GET[$name], $filtering, $callback));
- }
- return ($result);
- }
- /**
- * Returns some variable value with optional filtering from POST scope
- *
- * @param string $name name of variable to extract
- * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float, login, safe, gigasafe
- * @param string $callback callback function name to filter variable value
- *
- * @return mixed|false
- */
- public static function post($name, $filtering = 'raw', $callback = '') {
- $result = false;
- if (isset($_POST[$name])) {
- return (self::filters($_POST[$name], $filtering, $callback));
- }
- return ($result);
- }
- /**
- * Short rcms_redirect replacement
- *
- * @param string $url URL to perform redirect
- * @param bool $header Use header redirect instead of JS document.location
- *
- * @return void
- */
- public static function nav($url, $header = false) {
- if (!empty($url)) {
- rcms_redirect($url, $header);
- }
- }
- /**
- * Returns complete $_GET array as is
- *
- * @return array
- */
- public static function rawGet() {
- return ($_GET);
- }
- /**
- * Returns complete $_POST array as is
- *
- * @return array
- */
- public static function rawPost() {
- return ($_POST);
- }
- /**
- * Checks is all of options array present in CLI command line options as --optionname=
- *
- * @global array $argv
- *
- * @param array|string $params array of variable names to check or single variable name as string
- * @param bool $ignoreEmpty ignore or not existing variables with empty values
- *
- * @return bool
- */
- public static function optionCliCheck($params, $ignoreEmpty = true) {
- global $argv;
- $result = false;
- if (!empty($params)) {
- if (!is_array($params)) {
- //single param check
- $params = array($params);
- }
- foreach ($params as $eachparam) {
- if (!empty($argv)) {
- foreach ($argv as $io => $eachArg) {
- $result = false; //each new arg drops to false
- $fullOptMask = '--' . $eachparam . '='; //yeah, opts like --optioname=value
- $shortOptMask = '--' . $eachparam; //but we checks just for --optionname at start
- if (ispos($eachArg, $shortOptMask)) {
- if ($ignoreEmpty) {
- if (ispos($eachArg, $fullOptMask)) {
- $optValue = str_replace($fullOptMask, '', $eachArg);
- if (!empty($optValue)) {
- $result = true;
- } else {
- $result = false;
- }
- } else {
- $result = false;
- }
- } else {
- $result = true;
- return ($result);
- }
- }
- }
- }
- }
- return ($result);
- } else {
- throw new Exception('EX_PARAMS_EMPTY');
- }
- }
- /**
- * Returns some variable value with optional filtering from CLI option
- *
- * @global array $argv
- *
- * @param string $name name of variable to extract from CLI options
- * @param string $filtering filtering options. Possible values: raw, int, mres, callback
- * @param string $callback callback function name to filter variable value
- *
- * @return mixed|false
- */
- public static function optionCli($name, $filtering = 'raw', $callback = '') {
- global $argv;
- $result = false;
- if (!empty($argv)) {
- foreach ($argv as $io => $eachArg) {
- $fullOptMask = '--' . $name . '=';
- if (ispos($eachArg, $fullOptMask)) {
- $optValue = str_replace($fullOptMask, '', $eachArg);
- return (self::filters($optValue, $filtering, $callback));
- }
- }
- }
- return ($result);
- }
- /**
- * Returns current CLI application name
- *
- * @global array $argv
- *
- * @return string|false
- */
- public static function optionCliMe() {
- global $argv;
- $result = false;
- if (!empty($argv)) {
- if (isset($argv[0])) {
- $result = $argv[0];
- }
- }
- return ($result);
- }
- /**
- * Returns count of available CLI options
- *
- * @global array $argc
- *
- * @return int
- */
- public static function optionCliCount() {
- global $argc;
- return ($argc);
- }
- }
|