api.ubrouting.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. }
  174. $allowedChars = 'a-zA-Z0-9А-Яа-яЁёЇїІіЄєҐґ\w++«»№=_\ ,\.\-:;!?\(\){}\/\r\n\x{200d}\x{2600}-\x{1FAFF}' . $callback;
  175. $regex = '#[^' . $allowedChars . ']#u';
  176. $filteredData = preg_replace($regex, '', $rawData);
  177. $filteredData = str_replace('--', '', $filteredData);
  178. return ($filteredData);
  179. case 'gigasafe':
  180. $rawData = preg_replace('/\0/s', '', $rawData);
  181. $allowedChars = 'a-zA-Z0-9' . $callback;
  182. $regex = '#[^' . $allowedChars . ']#u';
  183. return (preg_replace($regex, '', $rawData));
  184. case 'fi':
  185. if (!empty($callback)) {
  186. return (filter_var($rawData, $callback));
  187. } else {
  188. throw new Exception('EX_FILTER_EMPTY');
  189. }
  190. break;
  191. case 'callback':
  192. if (!empty($callback)) {
  193. //single callback function
  194. if (!is_array($callback)) {
  195. if (function_exists($callback)) {
  196. return ($callback($rawData));
  197. } else {
  198. throw new Exception('EX_CALLBACK_NOT_DEFINED');
  199. }
  200. } else {
  201. $filteredResult = $rawData;
  202. //multiple callback functions
  203. foreach ($callback as $io => $eachCallbackFunction) {
  204. if (function_exists($eachCallbackFunction)) {
  205. $filteredResult = $eachCallbackFunction($filteredResult);
  206. } else {
  207. throw new Exception('EX_CALLBACK_NOT_DEFINED');
  208. }
  209. }
  210. return ($filteredResult);
  211. }
  212. } else {
  213. throw new Exception('EX_CALLBACK_EMPTY');
  214. }
  215. break;
  216. default:
  217. throw new Exception('EX_WRONG_FILTERING_MODE');
  218. break;
  219. }
  220. return ($result);
  221. }
  222. /**
  223. * Replaces double quotes in a string with special characters.
  224. *
  225. * This method takes a string as input and replaces all occurrences of double quotes with special characters.
  226. *
  227. * @param string $string The input string to be processed.
  228. * @return string The processed string with double quotes replaced by special characters.
  229. */
  230. public static function replaceQuotes($string) {
  231. return (preg_replace('/"([^"]*)"/', '«$1»', $string));
  232. }
  233. /**
  234. * Returns some variable value with optional filtering from GET scope
  235. *
  236. * @param string $name name of variable to extract
  237. * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float, login, safe, gigasafe
  238. * @param string|array $callback callback function name or names array to filter variable value
  239. *
  240. * @return mixed|false
  241. */
  242. public static function get($name, $filtering = 'raw', $callback = '') {
  243. $result = false;
  244. if (isset($_GET[$name])) {
  245. return (self::filters($_GET[$name], $filtering, $callback));
  246. }
  247. return ($result);
  248. }
  249. /**
  250. * Returns some variable value with optional filtering from POST scope
  251. *
  252. * @param string $name name of variable to extract
  253. * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float, login, safe, gigasafe
  254. * @param string $callback callback function name to filter variable value
  255. *
  256. * @return mixed|false
  257. */
  258. public static function post($name, $filtering = 'raw', $callback = '') {
  259. $result = false;
  260. if (isset($_POST[$name])) {
  261. return (self::filters($_POST[$name], $filtering, $callback));
  262. }
  263. return ($result);
  264. }
  265. /**
  266. * Redirects user to some specified URL
  267. *
  268. * @param string $url URL to perform redirect
  269. * @param bool $header Use header redirect instead of JS document.location
  270. *
  271. * @return void
  272. */
  273. public static function nav($url, $header = false) {
  274. if (!empty($url)) {
  275. if ($header) {
  276. @header('Location: ' . $url);
  277. } else {
  278. print('<script language="javascript">document.location.href="' . $url . '";</script>');
  279. }
  280. }
  281. }
  282. /**
  283. * Returns complete $_GET array as is
  284. *
  285. * @return array
  286. */
  287. public static function rawGet() {
  288. return ($_GET);
  289. }
  290. /**
  291. * Returns complete $_POST array as is
  292. *
  293. * @return array
  294. */
  295. public static function rawPost() {
  296. return ($_POST);
  297. }
  298. /**
  299. * Checks is all of options array present in CLI command line options as --optionname=
  300. *
  301. * @global array $argv
  302. *
  303. * @param array|string $params array of variable names to check or single variable name as string
  304. * @param bool $ignoreEmpty ignore or not existing variables with empty values
  305. *
  306. * @return bool
  307. */
  308. public static function optionCliCheck($params, $ignoreEmpty = true) {
  309. global $argv;
  310. $result = false;
  311. if (!empty($params)) {
  312. if (!is_array($params)) {
  313. //single param check
  314. $params = array($params);
  315. }
  316. foreach ($params as $eachparam) {
  317. if (!empty($argv)) {
  318. foreach ($argv as $io => $eachArg) {
  319. $result = false; //each new arg drops to false
  320. $fullOptMask = '--' . $eachparam . '='; //yeah, opts like --optioname=value
  321. $shortOptMask = '--' . $eachparam; //but we checks just for --optionname at start
  322. if (ispos($eachArg, $shortOptMask)) {
  323. if ($ignoreEmpty) {
  324. if (ispos($eachArg, $fullOptMask)) {
  325. $optValue = str_replace($fullOptMask, '', $eachArg);
  326. if (!empty($optValue)) {
  327. $result = true;
  328. } else {
  329. $result = false;
  330. }
  331. } else {
  332. $result = false;
  333. }
  334. } else {
  335. $result = true;
  336. return ($result);
  337. }
  338. }
  339. }
  340. }
  341. }
  342. return ($result);
  343. } else {
  344. throw new Exception('EX_PARAMS_EMPTY');
  345. }
  346. }
  347. /**
  348. * Returns some variable value with optional filtering from CLI option
  349. *
  350. * @global array $argv
  351. *
  352. * @param string $name name of variable to extract from CLI options
  353. * @param string $filtering filtering options. Possible values: raw, int, mres, callback
  354. * @param string $callback callback function name to filter variable value
  355. *
  356. * @return mixed|false
  357. */
  358. public static function optionCli($name, $filtering = 'raw', $callback = '') {
  359. global $argv;
  360. $result = false;
  361. if (!empty($argv)) {
  362. foreach ($argv as $io => $eachArg) {
  363. $fullOptMask = '--' . $name . '=';
  364. if (ispos($eachArg, $fullOptMask)) {
  365. $optValue = str_replace($fullOptMask, '', $eachArg);
  366. return (self::filters($optValue, $filtering, $callback));
  367. }
  368. }
  369. }
  370. return ($result);
  371. }
  372. /**
  373. * Returns current CLI application name
  374. *
  375. * @global array $argv
  376. *
  377. * @return string|false
  378. */
  379. public static function optionCliMe() {
  380. global $argv;
  381. $result = false;
  382. if (!empty($argv)) {
  383. if (isset($argv[0])) {
  384. $result = $argv[0];
  385. }
  386. }
  387. return ($result);
  388. }
  389. /**
  390. * Returns count of available CLI options
  391. *
  392. * @global array $argc
  393. *
  394. * @return int
  395. */
  396. public static function optionCliCount() {
  397. global $argc;
  398. return ($argc);
  399. }
  400. }