public function getToken($module = "") { if ($module == "") { return false; } $domain = $this->getCurrentDomain(); $domain = base64_encode($domain); $params = array('t' => 'token', 'd' => $domain, 'm' => $module, 'time' => time(), 'tt' => 'phpfox'); $urlget = $this->_config['url']; $token = $this->doPost($params, $urlget); $token = json_decode2($token, true); $token['time'] = $params['time']; $token['m'] = $module; $token_data = array('token' => $token['tk'], 'params' => $params['time']); return $token; }
function json_encode2($data) { $json = new Services_JSON(); return $json->encode($data); } } // Future-friendly json_decode if (!function_exists('json_decode2')) { function json_decode2($data) { $json = new Services_JSON(); return $json->decode($data); } } //var_dump($_POST); $idValue = $_POST["data"]; $data = json_decode2($_POST["data"]); print "<p>"; echo $idValue . "\n"; // . $data.; print "<p>"; foreach ($data->captcha as $value) { print $value->image; print "<br>"; foreach ($value->data as $xy) { print " "; print "x=" . $xy->x . ", y=" . $xy->y; print "<br>"; } print "<p>"; }
/** * decodes a JSON string into appropriate variable * * @param string $str JSON-formatted string * * @return mixed number, boolean, string, array, or object * corresponding to given JSON input string. * See argument 1 to JSON() above for object-output behavior. * Note that decode() always returns strings * in ASCII or UTF-8 format! * @access public */ function json_decode2($str, $always_true = TRUE) { $str = _reduce_string($str); switch (strtolower($str)) { case 'true': return true; case 'false': return false; case 'null': return null; default: if (is_numeric($str)) { // Lookie-loo, it's a number // This would work on its own, but I'm trying to be // good about returning integers where appropriate: // return (float)$str; // Return float or int, as appropriate return (double) $str == (int) $str ? (int) $str : (double) $str; } elseif (preg_match('/^("|\').+("|\')$/s', $str, $m) && $m[1] == $m[2]) { // STRINGS RETURNED IN UTF-8 FORMAT $delim = substr($str, 0, 1); $chrs = substr($str, 1, -1); $utf8 = ''; $strlen_chrs = strlen($chrs); for ($c = 0; $c < $strlen_chrs; ++$c) { $substr_chrs_c_2 = substr($chrs, $c, 2); $ord_chrs_c = ord($chrs[$c]); switch ($substr_chrs_c_2) { case '\\b': $utf8 .= chr(0x8); $c += 1; break; case '\\t': $utf8 .= chr(0x9); $c += 1; break; case '\\n': $utf8 .= chr(0xa); $c += 1; break; case '\\f': $utf8 .= chr(0xc); $c += 1; break; case '\\r': $utf8 .= chr(0xd); $c += 1; break; case '\\"': case '\\\'': case '\\\\': case '\\/': if ($delim == '"' && $substr_chrs_c_2 != '\\\'' || $delim == "'" && $substr_chrs_c_2 != '\\"') { $utf8 .= $chrs[++$c]; } break; default: if (preg_match('/\\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6))) { // single, escaped unicode character $utf16 = chr(hexdec(substr($chrs, $c + 2, 2))) . chr(hexdec(substr($chrs, $c + 4, 2))); $utf8 .= mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); $c += 5; } elseif ($ord_chrs_c >= 0x20 && $ord_chrs_c <= 0x7f) { $utf8 .= $chrs[$c]; } elseif (($ord_chrs_c & 0xe0) == 0xc0) { // characters U-00000080 - U-000007FF, mask 110XXXXX //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $utf8 .= substr($chrs, $c, 2); $c += 1; } elseif (($ord_chrs_c & 0xf0) == 0xe0) { // characters U-00000800 - U-0000FFFF, mask 1110XXXX // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $utf8 .= substr($chrs, $c, 3); $c += 2; } elseif (($ord_chrs_c & 0xf8) == 0xf0) { // characters U-00010000 - U-001FFFFF, mask 11110XXX // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $utf8 .= substr($chrs, $c, 4); $c += 3; } elseif (($ord_chrs_c & 0xfc) == 0xf8) { // characters U-00200000 - U-03FFFFFF, mask 111110XX // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $utf8 .= substr($chrs, $c, 5); $c += 4; } elseif (($ord_chrs_c & 0xfe) == 0xfc) { // characters U-04000000 - U-7FFFFFFF, mask 1111110X // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 $utf8 .= substr($chrs, $c, 6); $c += 5; } break; } } return $utf8; } elseif (preg_match('/^\\[.*\\]$/s', $str) || preg_match('/^\\{.*\\}$/s', $str)) { // array, or object notation if ($str[0] == '[') { $stk = array(JSON_IN_ARR); $arr = array(); } else { $stk = array(JSON_IN_OBJ); $obj = array(); } array_push($stk, array('what' => JSON_SLICE, 'where' => 0, 'delim' => false)); $chrs = substr($str, 1, -1); $chrs = _reduce_string($chrs); if ($chrs == '') { if (reset($stk) == JSON_IN_ARR) { return $arr; } else { return $obj; } } $strlen_chrs = strlen($chrs); for ($c = 0; $c <= $strlen_chrs; ++$c) { $top = end($stk); $substr_chrs_c_2 = substr($chrs, $c, 2); if ($c == $strlen_chrs || $chrs[$c] == ',' && $top['what'] == JSON_SLICE) { // found a comma that is not inside a string, array, etc., // OR we've reached the end of the character list $slice = substr($chrs, $top['where'], $c - $top['where']); array_push($stk, array('what' => JSON_SLICE, 'where' => $c + 1, 'delim' => false)); //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); if (reset($stk) == JSON_IN_ARR) { // we are in an array, so just push an element onto the stack array_push($arr, json_decode2($slice)); } elseif (reset($stk) == JSON_IN_OBJ) { // we are in an object, so figure // out the property name and set an // element in an associative array, // for now if (preg_match('/^\\s*(["\'].*[^\\\\]["\'])\\s*:\\s*(\\S.*),?$/Uis', $slice, $parts)) { // "name":value pair $key = json_decode2($parts[1]); $val = json_decode2($parts[2]); $obj[$key] = $val; } elseif (preg_match('/^\\s*(\\w+)\\s*:\\s*(\\S.*),?$/Uis', $slice, $parts)) { // name:value pair, where name is unquoted $key = $parts[1]; $val = json_decode2($parts[2]); $obj[$key] = $val; } } } elseif (($chrs[$c] == '"' || $chrs[$c] == "'") && in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) { // found a quote, and we are not inside a string array_push($stk, array('what' => JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c])); //print("Found start of string at {$c}\n"); } elseif ($chrs[$c] == $top['delim'] && $top['what'] == JSON_IN_STR && ($chrs[$c - 1] != "\\" || $chrs[$c - 1] == "\\" && $chrs[$c - 2] == "\\")) { // found a quote, we're in a string, and it's not escaped array_pop($stk); } elseif ($chrs[$c] == '[' && in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) { // found a left-bracket, and we are in an array, object, or slice array_push($stk, array('what' => JSON_IN_ARR, 'where' => $c, 'delim' => false)); } elseif ($chrs[$c] == ']' && $top['what'] == JSON_IN_ARR) { // found a right-bracket, and we're in an array array_pop($stk); } elseif ($chrs[$c] == '{' && in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) { // found a left-brace, and we are in an array, object, or slice array_push($stk, array('what' => JSON_IN_OBJ, 'where' => $c, 'delim' => false)); } elseif ($chrs[$c] == '}' && $top['what'] == JSON_IN_OBJ) { // found a right-brace, and we're in an object array_pop($stk); } elseif ($substr_chrs_c_2 == '/*' && in_array($top['what'], array(JSON_SLICE, JSON_IN_ARR, JSON_IN_OBJ))) { // found a comment start, and we are in an array, object, or slice array_push($stk, array('what' => JSON_IN_CMT, 'where' => $c, 'delim' => false)); $c++; } elseif ($substr_chrs_c_2 == '*/' && $top['what'] == JSON_IN_CMT) { // found a comment end, and we're in one now array_pop($stk); $c++; for ($i = $top['where']; $i <= $c; ++$i) { $chrs = substr_replace($chrs, ' ', $i, 1); } } } if (reset($stk) == JSON_IN_ARR) { return $arr; } elseif (reset($stk) == JSON_IN_OBJ) { return $obj; } } } }
/** * unserialize an array the JSON way * * @param string the serialized version * @return mixed the resulting array, or FALSE on error */ public static function json_decode($text) { global $context; // maybe we have a native extension --return an associative array // if(is_callable('json_decode')) // return json_decode($text, TRUE); // load the PHP library if (file_exists($context['path_to_root'] . 'included/json.php')) { include_once $context['path_to_root'] . 'included/json.php'; return json_decode2($text); } // tough luck return FALSE; }
/** * use oEmbed to embed some object * * @link http://oembed.com/ the specification of the oEmbed protocol * * The strategy to find oEmbed data is the following: * - list of well-known services (e.g., slideshare, ...) * - sense the target url in case an oEmbed endpoint is provided * - submit the url to http://www.noembed.com/ * * The array returned reflects the outcome of the oEmbed transaction. * If a network error takes place, the type is set to 'error'. * * @param string the address of the object to embed * @return array attributes returned by provider */ public static function oembed($url) { global $context; // we return an array of results $result = array(); // the list of known endpoints $endpoints = array(); // type: video $endpoints['https?://(www\\.)?youtube\\.com/watch\\?'] = 'http://www.youtube.com/oembed'; $endpoints['http://youtu\\.be/'] = 'http://www.youtube.com/oembed'; $endpoints['https?://(www\\.)?vimeo\\.com/'] = 'http://vimeo.com/api/oembed.json'; $endpoints['http://revision3\\.com/'] = 'http://revision3.com/api/oembed/'; $endpoints['http://(www\\.)?5min\\.com/video/'] = 'http://api.5min.com/oembed.json'; $endpoints['http://(www\\.)?dotsub\\.com/view/'] = 'http://dotsub.com/services/oembed'; $endpoints['https?://(www\\.)?hulu\\.com/watch/'] = 'http://www.hulu.com/api/oembed.json'; $endpoints['https?://(www\\.)?dailymotion\\.com/'] = 'http://www.dailymotion.com/services/oembed/'; $endpoints['http://[^\\.]+\\.blip\\.tv/'] = 'http://blip.tv/oembed/'; $endpoints['http://blip\\.tv/'] = 'http://blip.tv/oembed/'; $endpoints['https?://(www\\.)?viddler\\.com/'] = 'http://lab.viddler.com/services/oembed/'; // type: photo (or file) $endpoints['https?://(www\\.)?flickr\\.com/'] = 'http://www.flickr.com/services/oembed/'; $endpoints['http://flic\\.kr/'] = 'http://www.flickr.com/services/oembed/'; $endpoints['http://[^\\.]+\\.deviantart\\.com/'] = 'http://backend.deviantart.com/oembed'; $endpoints['http://yfrog\\.'] = 'http://www.yfrog.com/api/oembed'; $endpoints['https?://(www\\.)?smugmug\\.com/'] = 'http://api.smugmug.com/services/oembed/'; $endpoints['http://[^\\.]+\\.photobucket\\.com/'] = 'http://photobucket.com/oembed'; // type: rich $endpoints['https?://[^\\.]+\\.twitter\\.com/.+?/status(es)?/'] = 'https://api.twitter.com/1/statuses/oembed.json'; $endpoints['https?://twitter\\.com/.+?/status(es)?/'] = 'https://api.twitter.com/1/statuses/oembed.json'; $endpoints['http://official\\.fm/'] = 'http://official.fm/services/oembed.json'; $endpoints['http://soundcloud\\.com/'] = 'http://soundcloud.com/oembed'; $endpoints['http://rd\\.io/'] = 'http://www.rdio.com/api/oembed/'; $endpoints['https?://(www\\.)?slideshare\\.net/'] = 'http://www.slideshare.net/api/oembed/2'; $endpoints['https?://(www\\.)?scribd\\.com/'] = 'http://www.scribd.com/services/oembed'; // look at each providers $endpoint = null; foreach ($endpoints as $pattern => $api) { // stop when one provider has been found if (preg_match('/' . str_replace('/', '\\/', $pattern) . '/i', $url)) { $endpoint = $api; break; } } // finalize the query for the matching endpoint if ($endpoint) { // prepare the oEmbed request $parameters = array(); $parameters[] = 'url=' . urlencode($url); $parameters[] = 'maxwidth=500'; $parameters[] = 'format=json'; // encode provided data, if any $endpoint .= '?' . implode('&', $parameters); // else try to auto-detect an endpoint } elseif ($content = http::proceed_natively($url)) { // it is not necessary to look at page content $content = substr($content, 0, stripos($content, '</head>')); // if the endpoint signature is found if (stripos($content, 'application/json+oembed') !== FALSE) { // extract all links if (preg_match_all('/<link([^<>]+)>/i', $content, $links)) { // look at each of them sequentially foreach ($links[1] as $link) { // not the oEmbed endpoint! if (!stripos($link, 'application/json+oembed')) { continue; } // got it! if (preg_match('/href="([^"]+)"/i', $link, $matches)) { $endpoint = trim($matches[1]); break; } } } } } // if no provider has been found, submit this url to noembed.com if (!$endpoint) { // prepare the oEmbed request $parameters = array(); $parameters[] = 'url=' . urlencode($url); $parameters[] = 'maxwidth=500'; $parameters[] = 'format=json'; // encode provided data, if any $endpoint = 'http://noembed.com/embed?' . implode('&', $parameters); } // do the transaction if (!($response = http::proceed_natively($endpoint))) { $result['type'] = 'error'; return $result; // decode the received snippet } else { include_once $context['path_to_root'] . 'included/json.php'; if (!($data = json_decode2($response))) { $result['type'] = 'error'; return $result; } // return data to caller foreach ($data as $name => $value) { $result[$name] = $value; } // ensure that type is set if (!isset($result['type'])) { $result['type'] = 'error'; } } // job done return $result; }