api.ubrouting.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. /**
  3. * Basic Ubilling GET/POST abstraction and filtering class
  4. */
  5. class ubRouting {
  6. /**
  7. * .- <O> -. .-====-. ,-------. .-=<>=-.
  8. * /_-\'''/-_\ / / '' \ \ |,-----.| /__----__\
  9. * |/ o) (o \| | | ')(' | | /,'-----'.\ |/ (')(') \|
  10. * \ ._. / \ \ / / {_/(') (')\_} \ __ /
  11. * ,>-_,,,_-<. >'=jf='< `. _ .' ,'--__--'.
  12. * / . \ / \ /'-___-'\ / :| \
  13. * (_) . (_) / \ / \ (_) :| (_)
  14. * \_-----'____--/ (_) (_) (_)_______(_) |___:|____|
  15. * \___________/ |________| \_______/ |_________|
  16. */
  17. /**
  18. * Creates new Routing object instance
  19. */
  20. public function __construct() {
  21. //What did you expect to see here?
  22. }
  23. /**
  24. * Checks is all of variables array present in GET scope
  25. *
  26. * @param array/string $params array of variable names to check or single variable name as string
  27. * @param bool $ignoreEmpty ignore or not existing variables with empty values (like wf_Check)
  28. * @param bool $atleastOneExists returns "true" when encounters the very first existent GET param. Respects the $ignoreEmpty parameter.
  29. *
  30. * @return bool
  31. */
  32. public static function checkGet($params, $ignoreEmpty = true, $atleastOneExists = false) {
  33. if (!empty($params)) {
  34. if (!is_array($params)) {
  35. //single param check
  36. $params = array($params);
  37. }
  38. foreach ($params as $eachparam) {
  39. if (!isset($_GET[$eachparam])) {
  40. if ($atleastOneExists) {
  41. // traversing through the array till existent GET param found or the end of the array
  42. continue;
  43. } else {
  44. return (false);
  45. }
  46. } elseif ($atleastOneExists and !$ignoreEmpty) {
  47. return (true);
  48. }
  49. if ($ignoreEmpty) {
  50. if (empty($_GET[$eachparam])) {
  51. if ($atleastOneExists) {
  52. // traversing through the array till existent and non-empty GET param found or the end of the array
  53. continue;
  54. } else {
  55. return (false);
  56. }
  57. } elseif ($atleastOneExists) {
  58. return (true);
  59. }
  60. }
  61. }
  62. if ($atleastOneExists) {
  63. // if "$atleastOneExists = true" and we got here - none of the GET parameters were existent or non-empty then,
  64. // and we failed to find at least one
  65. return (false);
  66. } else {
  67. return (true);
  68. }
  69. } else {
  70. throw new Exception('EX_PARAMS_EMPTY');
  71. }
  72. }
  73. /**
  74. * Checks is all of variables array present in POST scope
  75. *
  76. * @param array/string $params array of variable names to check or single variable name as string
  77. * @param bool $ignoreEmpty ignore or not existing variables with empty values (like wf_Check)
  78. * @param bool $atleastOneExists returns "true" when encounters the very first existent GET param. Respects the $ignoreEmpty parameter.
  79. *
  80. * @return bool
  81. */
  82. public static function checkPost($params, $ignoreEmpty = true, $atleastOneExists = false) {
  83. if (!empty($params)) {
  84. if (!is_array($params)) {
  85. //single param check
  86. $params = array($params);
  87. }
  88. foreach ($params as $eachparam) {
  89. if (!isset($_POST[$eachparam])) {
  90. if ($atleastOneExists) {
  91. // traversing through the array till existent POST param found or the end of the array
  92. continue;
  93. } else {
  94. return (false);
  95. }
  96. } elseif ($atleastOneExists and !$ignoreEmpty) {
  97. return (true);
  98. }
  99. if ($ignoreEmpty) {
  100. if (empty($_POST[$eachparam])) {
  101. if ($atleastOneExists) {
  102. // traversing through the array till existent and non-empty POST param found or the end of the array
  103. continue;
  104. } else {
  105. return (false);
  106. }
  107. } elseif ($atleastOneExists) {
  108. return (true);
  109. }
  110. }
  111. }
  112. if ($atleastOneExists) {
  113. // if "$atleastOneExists = true" and we got here - none of the POST parameters were existent or non-empty then,
  114. // and we failed to find at least one
  115. return (false);
  116. } else {
  117. return (true);
  118. }
  119. } else {
  120. throw new Exception('EX_PARAMS_EMPTY');
  121. }
  122. }
  123. /**
  124. * Returns filtered data
  125. *
  126. * @param mixed $rawData data to be filtered
  127. * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float, login, safe, gigasafe
  128. * @param string|array/filter name $callback callback function name or names array to filter variable value. Or const filter name of php.net/filter
  129. *
  130. * @return mixed|false
  131. *
  132. * @throws Exception
  133. */
  134. public static function filters($rawData, $filtering = 'raw', $callback = '') {
  135. $result = false;
  136. switch ($filtering) {
  137. case 'raw':
  138. return ($rawData);
  139. break;
  140. case 'int':
  141. return (preg_replace("#[^0-9]#Uis", '', $rawData));
  142. break;
  143. case 'mres':
  144. return (mysql_real_escape_string($rawData));
  145. break;
  146. case 'vf':
  147. return (preg_replace("#[~@\+\?\%\/\;=\*\>\<\"\'\-]#Uis", '', $rawData));
  148. break;
  149. case 'nb':
  150. return (preg_replace('/\0/s', '', $rawData));
  151. break;
  152. case 'float':
  153. $filteredResult = preg_replace("#[^0-9.]#Uis", '', $rawData);
  154. if (is_numeric($filteredResult)) {
  155. return ($filteredResult);
  156. } else {
  157. return (false);
  158. }
  159. break;
  160. case 'login':
  161. $filteredResult = str_replace(' ', '_', $rawData);
  162. $loginAllowedChars = 'a-z0-9A-Z_\.' . $callback;
  163. $filteredLogin = preg_replace("#[^" . $loginAllowedChars . "]#Uis", '', $filteredResult);
  164. return ($filteredLogin);
  165. break;
  166. case 'safe':
  167. $rawData = preg_replace('/\0/s', '', $rawData);
  168. if (strpos($callback, 'HTML') !== false) {
  169. $callback = str_replace('HTML', '', $rawData);
  170. } else {
  171. $rawData = self::replaceQuotes($rawData);
  172. $rawData = strip_tags($rawData);
  173. $rawData = str_replace(array("'", '`'), '’', $rawData);
  174. }
  175. $allowedChars = 'a-zA-Z0-9А-Яа-яЁёЇїІіЄєҐґ\w++«»№’=_\ ,\.\-:;!?\(\){}\/\r\n\x{200d}\x{2600}-\x{1FAFF}' . $callback;
  176. $regex = '#[^' . $allowedChars . ']#u';
  177. $filteredData = preg_replace($regex, '', $rawData);
  178. $filteredData = str_replace('--', '', $filteredData);
  179. return ($filteredData);
  180. case 'gigasafe':
  181. $rawData = preg_replace('/\0/s', '', $rawData);
  182. $allowedChars = 'a-zA-Z0-9' . $callback;
  183. $regex = '#[^' . $allowedChars . ']#u';
  184. return (preg_replace($regex, '', $rawData));
  185. case 'fi':
  186. if (!empty($callback)) {
  187. return (filter_var($rawData, $callback));
  188. } else {
  189. throw new Exception('EX_FILTER_EMPTY');
  190. }
  191. break;
  192. case 'callback':
  193. if (!empty($callback)) {
  194. //single callback function
  195. if (!is_array($callback)) {
  196. if (function_exists($callback)) {
  197. return ($callback($rawData));
  198. } else {
  199. throw new Exception('EX_CALLBACK_NOT_DEFINED');
  200. }
  201. } else {
  202. $filteredResult = $rawData;
  203. //multiple callback functions
  204. foreach ($callback as $io => $eachCallbackFunction) {
  205. if (function_exists($eachCallbackFunction)) {
  206. $filteredResult = $eachCallbackFunction($filteredResult);
  207. } else {
  208. throw new Exception('EX_CALLBACK_NOT_DEFINED');
  209. }
  210. }
  211. return ($filteredResult);
  212. }
  213. } else {
  214. throw new Exception('EX_CALLBACK_EMPTY');
  215. }
  216. break;
  217. default:
  218. throw new Exception('EX_WRONG_FILTERING_MODE');
  219. break;
  220. }
  221. return ($result);
  222. }
  223. /**
  224. * Replaces double quotes in a string with special characters.
  225. *
  226. * This method takes a string as input and replaces all occurrences of double quotes with special characters.
  227. *
  228. * @param string $string The input string to be processed.
  229. * @return string The processed string with double quotes replaced by special characters.
  230. */
  231. public static function replaceQuotes($string) {
  232. return (preg_replace('/"([^"]*)"/', '«$1»', $string));
  233. }
  234. /**
  235. * Returns some variable value with optional filtering from GET scope
  236. *
  237. * @param string $name name of variable to extract
  238. * @param string $filtering filtering options. Possible values: raw, int, mres, callback
  239. * @param string|array $callback callback function name or names array to filter variable value
  240. *
  241. * @return mixed|false
  242. */
  243. public static function get($name, $filtering = 'raw', $callback = '') {
  244. $result = false;
  245. if (isset($_GET[$name])) {
  246. return (self::filters($_GET[$name], $filtering, $callback));
  247. }
  248. return ($result);
  249. }
  250. /**
  251. * Returns some variable value with optional filtering from POST scope
  252. *
  253. * @param string $name name of variable to extract
  254. * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float, login, safe, gigasafe
  255. * @param string $callback callback function name to filter variable value
  256. *
  257. * @return mixed|false
  258. */
  259. public static function post($name, $filtering = 'raw', $callback = '') {
  260. $result = false;
  261. if (isset($_POST[$name])) {
  262. return (self::filters($_POST[$name], $filtering, $callback));
  263. }
  264. return ($result);
  265. }
  266. /**
  267. * Short rcms_redirect replacement
  268. *
  269. * @param string $url URL to perform redirect
  270. * @param bool $header Use header redirect instead of JS document.location
  271. *
  272. * @return void
  273. */
  274. public static function nav($url, $header = false) {
  275. if (!empty($url)) {
  276. rcms_redirect($url, $header);
  277. }
  278. }
  279. /**
  280. * Returns complete $_GET array as is
  281. *
  282. * @return array
  283. */
  284. public static function rawGet() {
  285. return ($_GET);
  286. }
  287. /**
  288. * Returns complete $_POST array as is
  289. *
  290. * @return array
  291. */
  292. public static function rawPost() {
  293. return ($_POST);
  294. }
  295. /**
  296. * Checks is all of options array present in CLI command line options as --optionname=
  297. *
  298. * @global array $argv
  299. *
  300. * @param array|string $params array of variable names to check or single variable name as string
  301. * @param bool $ignoreEmpty ignore or not existing variables with empty values
  302. *
  303. * @return bool
  304. */
  305. public static function optionCliCheck($params, $ignoreEmpty = true) {
  306. global $argv;
  307. $result = false;
  308. if (!empty($params)) {
  309. if (!is_array($params)) {
  310. //single param check
  311. $params = array($params);
  312. }
  313. foreach ($params as $eachparam) {
  314. if (!empty($argv)) {
  315. foreach ($argv as $io => $eachArg) {
  316. $result = false; //each new arg drops to false
  317. $fullOptMask = '--' . $eachparam . '='; //yeah, opts like --optioname=value
  318. $shortOptMask = '--' . $eachparam; //but we checks just for --optionname at start
  319. if (ispos($eachArg, $shortOptMask)) {
  320. if ($ignoreEmpty) {
  321. if (ispos($eachArg, $fullOptMask)) {
  322. $optValue = str_replace($fullOptMask, '', $eachArg);
  323. if (!empty($optValue)) {
  324. $result = true;
  325. } else {
  326. $result = false;
  327. }
  328. } else {
  329. $result = false;
  330. }
  331. } else {
  332. $result = true;
  333. return ($result);
  334. }
  335. }
  336. }
  337. }
  338. }
  339. return ($result);
  340. } else {
  341. throw new Exception('EX_PARAMS_EMPTY');
  342. }
  343. }
  344. /**
  345. * Returns some variable value with optional filtering from CLI option
  346. *
  347. * @global array $argv
  348. *
  349. * @param string $name name of variable to extract from CLI options
  350. * @param string $filtering filtering options. Possible values: raw, int, mres, callback
  351. * @param string $callback callback function name to filter variable value
  352. *
  353. * @return mixed|false
  354. */
  355. public static function optionCli($name, $filtering = 'raw', $callback = '') {
  356. global $argv;
  357. $result = false;
  358. if (!empty($argv)) {
  359. foreach ($argv as $io => $eachArg) {
  360. $fullOptMask = '--' . $name . '=';
  361. if (ispos($eachArg, $fullOptMask)) {
  362. $optValue = str_replace($fullOptMask, '', $eachArg);
  363. return (self::filters($optValue, $filtering, $callback));
  364. }
  365. }
  366. }
  367. return ($result);
  368. }
  369. /**
  370. * Returns current CLI application name
  371. *
  372. * @global array $argv
  373. *
  374. * @return string|false
  375. */
  376. public static function optionCliMe() {
  377. global $argv;
  378. $result = false;
  379. if (!empty($argv)) {
  380. if (isset($argv[0])) {
  381. $result = $argv[0];
  382. }
  383. }
  384. return ($result);
  385. }
  386. /**
  387. * Returns count of available CLI options
  388. *
  389. * @global array $argc
  390. *
  391. * @return int
  392. */
  393. public static function optionCliCount() {
  394. global $argc;
  395. return ($argc);
  396. }
  397. }