private function doBatchRun($useHttps = true) { $batchQueue = $this->batchQueue[$useHttps ? 0 : 1]; if (empty($batchQueue)) { return; } $num = count($batchQueue); $params = array(); foreach ($batchQueue as $item) { $params[] = $item['i']; } $json = json_encode($params); $serialOnly = $this->batchMode === self::BATCH_MODE_SERIAL_ONLY; $params = array('method' => $json, 'serial_only' => $serialOnly); if ($useHttps) { $params['access_token'] = $this->getAccessToken(); $domain = self::$BD_OPENAPI_DEFAULT_DOMAINS['rest']; } else { $params['client_id'] = $this->getClientId(); $domain = self::$BD_OPENAPI_DEFAULT_DOMAINS['public']; } $result = BaiduUtils::request($domain . '/batch/run', $params, 'POST'); if ($result === false) { throw new BaiduException('failed to call batch/run api: ' . BaiduUtils::errmsg(), BaiduUtils::errno()); } $result = $this->converJson2Array($result); if (is_array($result) && isset($result['error_code'])) { throw new BaiduException('failed to call batch/run api: ' . $result['error_msg'], $result['error_code']); } for ($i = 0; $i < $num; $i++) { $item = $batchQueue[$i]; $itemResult = $result[$i]; if (is_array($itemResult) && isset($itemResult['error_code'])) { throw new BaiduException('failed to call ' . $item['i']['path'] . ' api: ' . $itemResult['error_msg'], $itemResult['error_code']); } $item['r'] = $itemResult; } }
public function getLoggedInUser($access_token) { $result = BaiduUtils::request(self::$BD_OAUTH2_ENDPOINTS['getLoginUser'], array('access_token' => $access_token, 'redirect_uri' => $this->redirectUri), 'GET'); if ($result) { $result = json_decode($result, true); if (isset($result['error_msg'])) { BaiduUtils::setError($result['error_code'], $result['error_msg']); return false; } return $result; } return false; }
/** * Get the authorization code from the query parameters, if it exists, * otherwise return false to signal no authorization code was discoverable. * * @return mixed Returns the authorization code, or false if the authorization * code could not be determined. */ protected function getCode() { if (isset($_GET['code'])) { if ($this->state && $this->state === $_GET['state']) { // CSRF state has done its job, so clear it $this->state = null; $this->store->remove('state'); return $_GET['code']; } else { BaiduUtils::errorLog('CSRF state token does not match one provided.'); return false; } } return false; }
/** * Make an oauth access token request * * The parameters: * - client_id: The client identifier, just use api key * - response_type: 'token' or 'code' * - redirect_uri: the url to go to after a successful login * - scope: The scope of the access request expressed as a list of space-delimited, case sensitive strings. * - state: An opaque value used by the client to maintain state between the request and callback. * - display: login page style, 'page', 'popup', 'touch' or 'mobile' * * @param array $params oauth request parameters * @return mixed returns access token info if success, or false if failed */ public function makeAccessTokenRequest($params) { $result = BaiduUtils::request(self::$BD_OAUTH2_ENDPOINTS['token'], $params, 'POST'); if ($result) { $result = json_decode($result, true); if (isset($result['error_description'])) { BaiduUtils::setError($result['error'], $result['error_description']); return false; } return $result; } return false; }
/** * Tries to detect MIME type of a file * * The method will try to use fileinfo extension if it is available, * deprecated mime_content_type() function in the other case. If neither * works, default 'application/octet-stream' MIME type is returned * * @param string filename * @return string file MIME type */ private static function detectMimeType($filename) { // finfo extension from PECL available if (function_exists('finfo_open')) { if (!isset(self::$fileinfoDb)) { self::$fileinfoDb = finfo_open(FILEINFO_MIME); } if (self::$fileinfoDb) { $info = finfo_file(self::$fileinfoDb, $filename); } } // (deprecated) mime_content_type function available if (empty($info) && function_exists('mime_content_type')) { $info = mime_content_type($filename); } return empty($info) ? 'application/octet-stream' : $info; }
/** * Get currently logged in user's uid. * * @return uint|false Return uid of the loggedin user, or return * false if user isn't loggedin. */ public function getLoggedInUser() { // Get user from cached data or from access token $user = $this->getUser(); p($user); die; // If there's bd_sig & bd_user parameter in query parameters, // it must be an inside web app(app on baidu) loading request, // then we must check whether the uid passed from baidu is the // same as we get from persistent data or from access token, // if it's not, we should clear all the persistent data and to // get an access token again. if (isset($_REQUEST['bd_sig']) && isset($_REQUEST['bd_user'])) { $params = array('bd_user' => $_REQUEST['bd_user']); $sig = BaiduUtils::generateSign($params, $this->clientSecret, 'bd_sig'); if ($sig != $_REQUEST['bd_sig'] || $user['uid'] != $_REQUEST['bd_user']) { $this->store->remove('session'); return false; } } return $user; }