User.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Luracast\Restler;
  3. /**
  4. * Information gathered about the api user is kept here using static methods
  5. * and properties for other classes to make use of them.
  6. * Typically Authentication classes populate them
  7. *
  8. * @category Framework
  9. * @package restler
  10. * @author R.Arul Kumaran <arul@luracast.com>
  11. * @copyright 2010 Luracast
  12. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  13. * @link http://luracast.com/products/restler/
  14. *
  15. */
  16. class User implements iIdentifyUser
  17. {
  18. private static $initialized = false;
  19. public static $id = null;
  20. public static $cacheId = null;
  21. public static $ip;
  22. public static $browser = '';
  23. public static $platform = '';
  24. public static function init()
  25. {
  26. static::$initialized = true;
  27. static::$ip = static::getIpAddress();
  28. }
  29. public static function getUniqueIdentifier($includePlatform = false)
  30. {
  31. if (!static::$initialized) static::init();
  32. return static::$id ? : base64_encode('ip:' . ($includePlatform
  33. ? static::$ip . '-' . static::$platform
  34. : static::$ip
  35. ));
  36. }
  37. public static function getIpAddress($ignoreProxies = false)
  38. {
  39. foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR',
  40. 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP',
  41. 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
  42. 'REMOTE_ADDR') as $key) {
  43. if (array_key_exists($key, $_SERVER) === true) {
  44. foreach (explode(',', $_SERVER[$key]) as $ip) {
  45. $ip = trim($ip); // just to be safe
  46. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4
  47. | FILTER_FLAG_NO_PRIV_RANGE
  48. | FILTER_FLAG_NO_RES_RANGE) !== false
  49. ) {
  50. return $ip;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * Authentication classes should call this method
  58. *
  59. * @param string $id user id as identified by the authentication classes
  60. *
  61. * @return void
  62. */
  63. public static function setUniqueIdentifier($id)
  64. {
  65. static::$id = $id;
  66. }
  67. /**
  68. * User identity to be used for caching purpose
  69. *
  70. * When the dynamic cache service places an object in the cache, it needs to
  71. * label it with a unique identifying string known as a cache ID. This
  72. * method gives that identifier
  73. *
  74. * @return string
  75. */
  76. public static function getCacheIdentifier()
  77. {
  78. return static::$cacheId ?: static::$id;
  79. }
  80. /**
  81. * User identity for caching purpose
  82. *
  83. * In a role based access control system this will be based on role
  84. *
  85. * @param $id
  86. *
  87. * @return void
  88. */
  89. public static function setCacheIdentifier($id)
  90. {
  91. static::$cacheId = $id;
  92. }
  93. }