openlogin.fun.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. if (!function_exists("json_decode")) {
  3. function json_decode($json, $assoc=FALSE, /*emu_args*/$n=0,$state=0,$waitfor=0) {
  4. #-- result var
  5. $val = NULL;
  6. static $lang_eq = array("true" => TRUE, "false" => FALSE, "null" => NULL);
  7. static $str_eq = array("n"=>"\012", "r"=>"\015", "\\"=>"\\", '"'=>'"', "f"=>"\f", "b"=>"\b", "t"=>"\t", "/"=>"/");
  8. #-- flat char-wise parsing
  9. for (/*n*/; $n<strlen($json); /*n*/) {
  10. $c = $json[$n];
  11. #-= in-string
  12. if ($state==='"') {
  13. if ($c == '\\') {
  14. $c = $json[++$n];
  15. // simple C escapes
  16. if (isset($str_eq[$c])) {
  17. $val .= $str_eq[$c];
  18. }
  19. // here we transform \uXXXX Unicode (always 4 nibbles) references to UTF-8
  20. elseif ($c == "u") {
  21. // read just 16bit (therefore value can't be negative)
  22. $hex = hexdec( substr($json, $n+1, 4) );
  23. $n += 4;
  24. // Unicode ranges
  25. if ($hex < 0x80) { // plain ASCII character
  26. $val .= chr($hex);
  27. }
  28. elseif ($hex < 0x800) { // 110xxxxx 10xxxxxx
  29. $val .= chr(0xC0 + $hex>>6) . chr(0x80 + $hex&63);
  30. }
  31. elseif ($hex <= 0xFFFF) { // 1110xxxx 10xxxxxx 10xxxxxx
  32. $val .= chr(0xE0 + $hex>>12) . chr(0x80 + ($hex>>6)&63) . chr(0x80 + $hex&63);
  33. }
  34. // other ranges, like 0x1FFFFF=0xF0, 0x3FFFFFF=0xF8 and 0x7FFFFFFF=0xFC do not apply
  35. }
  36. // no escape, just a redundant backslash
  37. //@COMPAT: we could throw an exception here
  38. else {
  39. $val .= "\\" . $c;
  40. }
  41. }
  42. // end of string
  43. elseif ($c == '"') {
  44. $state = 0;
  45. }
  46. // yeeha! a single character found!!!!1!
  47. else/*if (ord($c) >= 32)*/ { //@COMPAT: specialchars check - but native json doesn't do it?
  48. $val .= $c;
  49. }
  50. }
  51. #-> end of sub-call (array/object)
  52. elseif ($waitfor && (strpos($waitfor, $c) !== false)) {
  53. return array($val, $n); // return current value and state
  54. }
  55. #-= in-array
  56. elseif ($state===']') {
  57. list($v, $n) = json_decode($json, 0, $n, 0, ",]");
  58. $val[] = $v;
  59. if ($json[$n] == "]") { return array($val, $n); }
  60. }
  61. #-= in-object
  62. elseif ($state==='}') {
  63. list($i, $n) = json_decode($json, 0, $n, 0, ":"); // this allowed non-string indicies
  64. list($v, $n) = json_decode($json, 0, $n+1, 0, ",}");
  65. $val[$i] = $v;
  66. if ($json[$n] == "}") { return array($val, $n); }
  67. }
  68. #-- looking for next item (0)
  69. else {
  70. #-> whitespace
  71. if (preg_match("/\s/", $c)) {
  72. // skip
  73. }
  74. #-> string begin
  75. elseif ($c == '"') {
  76. $state = '"';
  77. }
  78. #-> object
  79. elseif ($c == "{") {
  80. list($val, $n) = json_decode($json, $assoc, $n+1, '}', "}");
  81. if ($val && $n && !$assoc) {
  82. $obj = new stdClass();
  83. foreach ($val as $i=>$v) {
  84. $obj->{$i} = $v;
  85. }
  86. $val = $obj;
  87. unset($obj);
  88. }
  89. }
  90. #-> array
  91. elseif ($c == "[") {
  92. list($val, $n) = json_decode($json, $assoc, $n+1, ']', "]");
  93. }
  94. #-> comment
  95. elseif (($c == "/") && ($json[$n+1]=="*")) {
  96. // just find end, skip over
  97. ($n = strpos($json, "*/", $n+1)) or ($n = strlen($json));
  98. }
  99. #-> numbers
  100. elseif (preg_match("#^(-?\d+(?:\.\d+)?)(?:[eE]([-+]?\d+))?#", substr($json, $n), $uu)) {
  101. $val = $uu[1];
  102. $n += strlen($uu[0]) - 1;
  103. if (strpos($val, ".")) { // float
  104. $val = (float)$val;
  105. }
  106. elseif ($val[0] == "0") { // oct
  107. $val = octdec($val);
  108. }
  109. else {
  110. $val = (int)$val;
  111. }
  112. // exponent?
  113. if (isset($uu[2])) {
  114. $val *= pow(10, (int)$uu[2]);
  115. }
  116. }
  117. #-> boolean or null
  118. elseif (preg_match("#^(true|false|null)\b#", substr($json, $n), $uu)) {
  119. $val = $lang_eq[$uu[1]];
  120. $n += strlen($uu[1]) - 1;
  121. }
  122. #-- parsing error
  123. else {
  124. // PHPs native json_decode() breaks here usually and QUIETLY
  125. trigger_error("json_decode: error parsing '$c' at position $n", E_USER_WARNING);
  126. return $waitfor ? array(NULL, 1<<30) : NULL;
  127. }
  128. }//state
  129. #-- next char
  130. if ($n === NULL) { return NULL; }
  131. $n++;
  132. }//for
  133. #-- final result
  134. return ($val);
  135. }
  136. }
  137. function get_url_contents($url)
  138. {
  139. $ch = curl_init();
  140. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  141. curl_setopt($ch, CURLOPT_URL, $url);
  142. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  143. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  144. $result = curl_exec($ch);
  145. curl_close($ch);
  146. return $result;
  147. }
  148. ?>