api.ubrouting.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. *
  29. * @return bool
  30. */
  31. public static function checkGet($params, $ignoreEmpty = true) {
  32. if (!empty($params)) {
  33. if (!is_array($params)) {
  34. //single param check
  35. $params = array($params);
  36. }
  37. foreach ($params as $eachparam) {
  38. if (!isset($_GET[$eachparam])) {
  39. return (false);
  40. }
  41. if ($ignoreEmpty) {
  42. if (empty($_GET[$eachparam])) {
  43. return (false);
  44. }
  45. }
  46. }
  47. return(true);
  48. } else {
  49. throw new Exception('EX_PARAMS_EMPTY');
  50. }
  51. }
  52. /**
  53. * Checks is all of variables array present in POST scope
  54. *
  55. * @param array/string $params array of variable names to check or single variable name as string
  56. * @param bool $ignoreEmpty ignore or not existing variables with empty values (like wf_Check)
  57. *
  58. * @return bool
  59. */
  60. public static function checkPost($params, $ignoreEmpty = true) {
  61. if (!empty($params)) {
  62. if (!is_array($params)) {
  63. //single param check
  64. $params = array($params);
  65. }
  66. foreach ($params as $eachparam) {
  67. if (!isset($_POST[$eachparam])) {
  68. return (false);
  69. }
  70. if ($ignoreEmpty) {
  71. if (empty($_POST[$eachparam])) {
  72. return (false);
  73. }
  74. }
  75. }
  76. return (true);
  77. } else {
  78. throw new Exception('EX_PARAMS_EMPTY');
  79. }
  80. }
  81. /**
  82. * Returns filtered data
  83. *
  84. * @param type $rawData data to be filtered
  85. * @param string $filtering filtering options. Possible values: raw, int, mres, callback, fi, vf, nb, float
  86. * @param string/array/filter name $callback callback function name or names array to filter variable value. Or const filter name of php.net/filter
  87. *
  88. * @return mixed/false
  89. *
  90. * @throws Exception
  91. */
  92. public static function filters($rawData, $filtering = 'raw', $callback = '') {
  93. $result = false;
  94. switch ($filtering) {
  95. case 'raw':
  96. return($rawData);
  97. break;
  98. case 'int':
  99. return(preg_replace("#[^0-9]#Uis", '', $rawData));
  100. break;
  101. case 'mres':
  102. return(mysql_real_escape_string($rawData));
  103. break;
  104. case 'vf':
  105. return(preg_replace("#[~@\+\?\%\/\;=\*\>\<\"\'\-]#Uis", '', $rawData));
  106. break;
  107. case 'nb':
  108. return(preg_replace('/\0/s', '', $rawData));
  109. break;
  110. case 'float':
  111. $filteredResult = preg_replace("#[^0-9.]#Uis", '', $rawData);
  112. if (is_numeric($filteredResult)) {
  113. return($filteredResult);
  114. } else {
  115. return(false);
  116. }
  117. break;
  118. case 'fi':
  119. if (!empty($callback)) {
  120. return(filter_var($rawData, $callback));
  121. } else {
  122. throw new Exception('EX_FILTER_EMPTY');
  123. }
  124. break;
  125. case 'callback':
  126. if (!empty($callback)) {
  127. //single callback function
  128. if (!is_array($callback)) {
  129. if (function_exists($callback)) {
  130. return($callback($rawData));
  131. } else {
  132. throw new Exception('EX_CALLBACK_NOT_DEFINED');
  133. }
  134. } else {
  135. $filteredResult = $rawData;
  136. //multiple callback functions
  137. foreach ($callback as $io => $eachCallbackFunction) {
  138. if (function_exists($eachCallbackFunction)) {
  139. $filteredResult = $eachCallbackFunction($filteredResult);
  140. } else {
  141. throw new Exception('EX_CALLBACK_NOT_DEFINED');
  142. }
  143. }
  144. return($filteredResult);
  145. }
  146. } else {
  147. throw new Exception('EX_CALLBACK_EMPTY');
  148. }
  149. break;
  150. default :
  151. throw new Exception('EX_WRONG_FILTERING_MODE');
  152. break;
  153. }
  154. return($result);
  155. }
  156. /**
  157. * Returns some variable value with optional filtering from GET scope
  158. *
  159. * @param string $name name of variable to extract
  160. * @param string $filtering filtering options. Possible values: raw, int, mres, callback
  161. * @param string/array $callback callback function name or names array to filter variable value
  162. *
  163. * @return mixed/false
  164. */
  165. public static function get($name, $filtering = 'raw', $callback = '') {
  166. $result = false;
  167. if (isset($_GET[$name])) {
  168. return(self::filters($_GET[$name], $filtering, $callback));
  169. }
  170. return($result);
  171. }
  172. /**
  173. * Returns some variable value with optional filtering from POST scope
  174. *
  175. * @param string $name name of variable to extract
  176. * @param string $filtering filtering options. Possible values: raw, int, mres, callback
  177. * @param string $callback callback function name to filter variable value
  178. *
  179. * @return mixed/false
  180. */
  181. public static function post($name, $filtering = 'raw', $callback = '') {
  182. $result = false;
  183. if (isset($_POST[$name])) {
  184. return(self::filters($_POST[$name], $filtering, $callback));
  185. }
  186. return($result);
  187. }
  188. /**
  189. * Short rcms_redirect replacement
  190. *
  191. * @param string $url URL to perform redirect
  192. *
  193. * @return void
  194. */
  195. public static function nav($url) {
  196. if (!empty($url)) {
  197. rcms_redirect($url);
  198. }
  199. }
  200. /**
  201. * Returns complete $_GET array as is
  202. *
  203. * @return array
  204. */
  205. public static function rawGet() {
  206. return($_GET);
  207. }
  208. /**
  209. * Returns complete $_POST array as is
  210. *
  211. * @return array
  212. */
  213. public static function rawPost() {
  214. return($_POST);
  215. }
  216. /**
  217. * Checks is all of options array present in CLI command line options as --optionname=
  218. *
  219. * @global array $argv
  220. *
  221. * @param array/string $params array of variable names to check or single variable name as string
  222. * @param bool $ignoreEmpty ignore or not existing variables with empty values
  223. *
  224. * @return bool
  225. */
  226. public static function optionCliCheck($params, $ignoreEmpty = true) {
  227. global $argv;
  228. $result = false;
  229. if (!empty($params)) {
  230. if (!is_array($params)) {
  231. //single param check
  232. $params = array($params);
  233. }
  234. foreach ($params as $eachparam) {
  235. if (!empty($argv)) {
  236. foreach ($argv as $io => $eachArg) {
  237. $result = false; //each new arg drops to false
  238. $fullOptMask = '--' . $eachparam . '='; //yeah, opts like --optioname=value
  239. $shortOptMask = '--' . $eachparam; //but we checks just for --optionname at start
  240. if (ispos($eachArg, $shortOptMask)) {
  241. if ($ignoreEmpty) {
  242. if (ispos($eachArg, $fullOptMask)) {
  243. $optValue = str_replace($fullOptMask, '', $eachArg);
  244. if (!empty($optValue)) {
  245. $result = true;
  246. } else {
  247. $result = false;
  248. }
  249. } else {
  250. $result = false;
  251. }
  252. } else {
  253. $result = true;
  254. return($result);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. return ($result);
  261. } else {
  262. throw new Exception('EX_PARAMS_EMPTY');
  263. }
  264. }
  265. /**
  266. * Returns some variable value with optional filtering from CLI option
  267. *
  268. * @global array $argv
  269. *
  270. * @param string $name name of variable to extract from CLI options
  271. * @param string $filtering filtering options. Possible values: raw, int, mres, callback
  272. * @param string $callback callback function name to filter variable value
  273. *
  274. * @return mixed/false
  275. */
  276. public static function optionCli($name, $filtering = 'raw', $callback = '') {
  277. global $argv;
  278. $result = false;
  279. if (!empty($argv)) {
  280. foreach ($argv as $io => $eachArg) {
  281. $fullOptMask = '--' . $name . '=';
  282. if (ispos($eachArg, $fullOptMask)) {
  283. $optValue = str_replace($fullOptMask, '', $eachArg);
  284. return(self::filters($optValue, $filtering, $callback));
  285. }
  286. }
  287. }
  288. return($result);
  289. }
  290. /**
  291. * Returns current CLI application name
  292. *
  293. * @global array $argv
  294. *
  295. * @return string/false
  296. */
  297. public static function optionCliMe() {
  298. global $argv;
  299. $result = false;
  300. if (!empty($argv)) {
  301. if (isset($argv[0])) {
  302. $result = $argv[0];
  303. }
  304. }
  305. return($result);
  306. }
  307. /**
  308. * Returns count of available CLI options
  309. *
  310. * @global array $argc
  311. *
  312. * @return int
  313. */
  314. public static function optionCliCount() {
  315. global $argc;
  316. return($argc);
  317. }
  318. }