PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/classes/class.libraries.php

https://github.com/Tania-Zaman/jQuery.pidCrypt
PHP | 138 lines | 95 code | 8 blank | 35 comment | 25 complexity | 3be98ddad8f6e7c8c04e87448776a050 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class libraries
  3. {
  4. /**
  5. * @function _serialize
  6. * @abstract Perform serialization of sent POST data. This is required for the
  7. * jQuery.AJAX plug-in checksum verification as the current PHP
  8. * serialize() function will not create an accurate hash
  9. */
  10. function _serialize($array)
  11. {
  12. $x = '';
  13. if ((is_array($array))&&(count($array)>0)){
  14. foreach($array as $key => $value){
  15. $x .= $key.'='.$value.'&';
  16. }
  17. $x = substr($x, 0, -1);
  18. }
  19. return (strlen($x)>0) ? $x : false;
  20. }
  21. /**
  22. * @function JSONencode
  23. * @abstract Primary interface for creating JSON objects
  24. */
  25. function JSONencode($array){
  26. if (!function_exists('json_encode')) {
  27. return arr2json($array);
  28. } else {
  29. return json_encode($array);
  30. }
  31. }
  32. /**
  33. * @function arr2json
  34. * @abstract Creates JSON object when json_encode is missing
  35. */
  36. function arr2json($array)
  37. {
  38. if (is_array($array)) {
  39. foreach($array as $key => $value) $json[]=$key.':'.php2js($value);
  40. if(count($json)>0) return '{'.implode(',',$json).'}';
  41. else return '';
  42. }
  43. }
  44. /**
  45. * @function php2js
  46. * @abstract Helper for arr2json. Perofrms typecasting
  47. */
  48. function php2js($value)
  49. {
  50. if(is_array($value)) return arr2json($val);
  51. if(is_string($value)) return '"'.$value.'"';
  52. if(is_bool($value)) return 'Boolean('.(int) $value.')';
  53. if(is_null($value)) return '""';
  54. return $value;
  55. }
  56. /**
  57. * @function _uuid()
  58. * @abstract Generate a unique GUID (per RFC4122)
  59. */
  60. function _uuid()
  61. {
  62. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff),
  63. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000,
  64. mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff),
  65. mt_rand(0, 0xffff), mt_rand(0, 0xffff));
  66. }
  67. /**
  68. * @function _getRealIPv4
  69. * @abstract Try all methods of obtaining 'real' IP address
  70. */
  71. function _getRealIPv4()
  72. {
  73. return (getenv('HTTP_CLIENT_IP') && $this->_ip(getenv('HTTP_CLIENT_IP'))) ?
  74. getenv('HTTP_CLIENT_IP') :
  75. (getenv('HTTP_X_FORWARDED_FOR') && $this->_forwarded(getenv('HTTP_X_FORWARDED_FOR'))) ?
  76. $this->_forwarded(getenv('HTTP_X_FORWARDED_FOR')) :
  77. (getenv('HTTP_X_FORWARDED') && $this->_ip(getenv('HTTP_X_FORWARDED'))) ?
  78. getenv('HTTP_X_FORWARDED') :
  79. (getenv('HTTP_X_FORWARDED_HOST') && $this->_ip(getenv('HTTP_FORWARDED_HOST'))) ?
  80. getenv('HTTP_X_FORWARDED_HOST') :
  81. (getenv('HTTP_X_FORWARDED_SERVER') && $this->_ip(getenv('HTTP_X_FORWARDED_SERVER'))) ?
  82. getenv('HTTP_X_FORWARDED_SERVER') :
  83. (getenv('HTTP_X_CLUSTER_CLIENT_IP') && $this->_ip(getenv('HTTP_X_CLIUSTER_CLIENT_IP'))) ?
  84. getenv('HTTP_X_CLUSTER_CLIENT_IP') :
  85. getenv('REMOTE_ADDR');
  86. }
  87. /**
  88. * @function _ip
  89. * @abstract Attempts to determine if IP is non-routeable
  90. */
  91. function _ip($ip)
  92. {
  93. if (!empty($ip) && ip2long($ip)!=-1 && ip2long($ip)!=false){
  94. $nr = array(array('0.0.0.0','2.255.255.255'),
  95. array('10.0.0.0','10.255.255.255'),
  96. array('127.0.0.0','127.255.255.255'),
  97. array('169.254.0.0','169.254.255.255'),
  98. array('172.16.0.0','172.31.255.255'),
  99. array('192.0.2.0','192.0.2.255'),
  100. array('192.168.0.0','192.168.255.255'),
  101. array('255.255.255.0','255.255.255.255'));
  102. foreach($nr as $r){
  103. $min = ip2long($r[0]);
  104. $max = ip2long($r[1]);
  105. if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
  106. }
  107. return true;
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * @function _forwarded
  114. * @abstract A helper for HTTP_X_FORWARDED_FOR, loops over comma
  115. * separated list of proxies associated with request
  116. */
  117. function _forwarded($l)
  118. {
  119. if (!empty($l)){
  120. foreach (explode(',', $l) as $i){
  121. if ($this->_ip(trim($i))) {
  122. return (!$this->_ip(trim($i))) ? false : $i;
  123. }
  124. }
  125. } else {
  126. return false;
  127. }
  128. }
  129. }
  130. ?>