api.ubrouting.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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, fi, vf, nb, float, login, safe, gigasafe
  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. * Redirects user to some specified URL
  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. if ($header) {
  277. @header('Location: ' . $url);
  278. } else {
  279. print('<script language="javascript">document.location.href="' . $url . '";</script>');
  280. }
  281. }
  282. }
  283. /**
  284. * Returns complete $_GET array as is
  285. *
  286. * @return array
  287. */
  288. public static function rawGet() {
  289. return ($_GET);
  290. }
  291. /**
  292. * Returns complete $_POST array as is
  293. *
  294. * @return array
  295. */
  296. public static function rawPost() {
  297. return ($_POST);
  298. }
  299. /**
  300. * Checks is all of options array present in CLI command line options as --optionname=
  301. *
  302. * @global array $argv
  303. *
  304. * @param array|string $params array of variable names to check or single variable name as string
  305. * @param bool $ignoreEmpty ignore or not existing variables with empty values
  306. *
  307. * @return bool
  308. */
  309. public static function optionCliCheck($params, $ignoreEmpty = true) {
  310. global $argv;
  311. $result = false;
  312. if (!empty($params)) {
  313. if (!is_array($params)) {
  314. //single param check
  315. $params = array($params);
  316. }
  317. foreach ($params as $eachparam) {
  318. if (!empty($argv)) {
  319. foreach ($argv as $io => $eachArg) {
  320. $result = false; //each new arg drops to false
  321. $fullOptMask = '--' . $eachparam . '='; //yeah, opts like --optioname=value
  322. $shortOptMask = '--' . $eachparam; //but we checks just for --optionname at start
  323. if (ispos($eachArg, $shortOptMask)) {
  324. if ($ignoreEmpty) {
  325. if (ispos($eachArg, $fullOptMask)) {
  326. $optValue = str_replace($fullOptMask, '', $eachArg);
  327. if (!empty($optValue)) {
  328. $result = true;
  329. } else {
  330. $result = false;
  331. }
  332. } else {
  333. $result = false;
  334. }
  335. } else {
  336. $result = true;
  337. return ($result);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. return ($result);
  344. } else {
  345. throw new Exception('EX_PARAMS_EMPTY');
  346. }
  347. }
  348. /**
  349. * Returns some variable value with optional filtering from CLI option
  350. *
  351. * @global array $argv
  352. *
  353. * @param string $name name of variable to extract from CLI options
  354. * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float, login, safe, gigasafe
  355. * @param string $callback callback function name to filter variable value
  356. *
  357. * @return mixed|false
  358. */
  359. public static function optionCli($name, $filtering = 'raw', $callback = '') {
  360. global $argv;
  361. $result = false;
  362. if (!empty($argv)) {
  363. foreach ($argv as $io => $eachArg) {
  364. $fullOptMask = '--' . $name . '=';
  365. if (ispos($eachArg, $fullOptMask)) {
  366. $optValue = str_replace($fullOptMask, '', $eachArg);
  367. return (self::filters($optValue, $filtering, $callback));
  368. }
  369. }
  370. }
  371. return ($result);
  372. }
  373. /**
  374. * Returns current CLI application name
  375. *
  376. * @global array $argv
  377. *
  378. * @return string|false
  379. */
  380. public static function optionCliMe() {
  381. global $argv;
  382. $result = false;
  383. if (!empty($argv)) {
  384. if (isset($argv[0])) {
  385. $result = $argv[0];
  386. }
  387. }
  388. return ($result);
  389. }
  390. /**
  391. * Returns count of available CLI options
  392. *
  393. * @global array $argc
  394. *
  395. * @return int
  396. */
  397. public static function optionCliCount() {
  398. global $argc;
  399. return ($argc);
  400. }
  401. }