Request.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. /**
  10. * Request Manager - for controlled access to the global state of the world.
  11. *
  12. * @package Pimf
  13. * @author Gjero Krsteski <[email protected]>
  14. */
  15. class Request
  16. {
  17. /**
  18. * @var Param
  19. */
  20. public static $postData;
  21. /**
  22. * @var Param
  23. */
  24. public static $getData;
  25. /**
  26. * @var Param
  27. */
  28. public static $cookieData;
  29. /**
  30. * @var Param
  31. */
  32. public static $cliData;
  33. /**
  34. * @param array $getData
  35. * @param array $postData
  36. * @param array $cookieData
  37. * @param array $cliData
  38. */
  39. public function __construct(array $getData, array $postData = array(), array $cookieData = array(), array $cliData = array())
  40. {
  41. static::$getData = new Param((array)self::stripSlashesIfMagicQuotes($getData));
  42. static::$postData = new Param((array)self::stripSlashesIfMagicQuotes($postData));
  43. static::$cookieData = new Param($cookieData);
  44. static::$cliData = new Param((array)self::stripSlashesIfMagicQuotes($cliData));
  45. }
  46. /**
  47. * HTTP GET variables.
  48. *
  49. * @return Param
  50. */
  51. public function fromGet()
  52. {
  53. return static::$getData;
  54. }
  55. /**
  56. * CLI arguments passed to script.
  57. *
  58. * @return Param
  59. */
  60. public function fromCli()
  61. {
  62. return static::$cliData;
  63. }
  64. /**
  65. * HTTP POST variables.
  66. *
  67. * @return Param
  68. */
  69. public function fromPost()
  70. {
  71. return static::$postData;
  72. }
  73. /**
  74. * HTTP Cookies.
  75. *
  76. * @return Param
  77. */
  78. public function fromCookie()
  79. {
  80. return static::$cookieData;
  81. }
  82. /**
  83. * Strip slashes from string or array
  84. *
  85. * @param $rawData
  86. * @param null $overrideStripSlashes
  87. *
  88. * @return array|string
  89. */
  90. public static function stripSlashesIfMagicQuotes($rawData, $overrideStripSlashes = null)
  91. {
  92. $hasMagicQuotes = function_exists('get_magic_quotes_gpc') ? get_magic_quotes_gpc() : false;
  93. $strip = !$overrideStripSlashes ? $hasMagicQuotes : $overrideStripSlashes;
  94. if ($strip) {
  95. return self::stripSlashes($rawData);
  96. }
  97. return $rawData;
  98. }
  99. /**
  100. * Strip slashes from string or array
  101. *
  102. * @param $rawData
  103. *
  104. * @return array|string
  105. */
  106. public static function stripSlashes($rawData)
  107. {
  108. return is_array($rawData)
  109. ? array_map(
  110. function ($value) {
  111. return \Pimf\Request::stripSlashes($value);
  112. }, $rawData
  113. )
  114. : stripslashes($rawData);
  115. }
  116. }