Example #1
0
 /**
  * Downloads a report from a URL. If the filePath parameter is specified it
  * will be downloaded to the file at that path, otherwise it will be
  * downloaded to memory and be returned as a string.
  * @param string $downloadUrl the URL of the report download
  * @param string $filePath an optional path of a file to download the report
  *     to
  * @return mixed if $filePath isn't specified it will return the contents of
  *     the report, otherwise the size in bytes of the downloaded report
  */
 public static function DownloadReport($downloadUrl, $filePath = NULL)
 {
     /* 
      * This method should not be static and instantiation of this class should
      * be allowed so we can "inject" CurlUtils, but would break too many things
      * that rely on this method being static.
      */
     $curlUtils = new CurlUtils();
     $ch = $curlUtils->CreateSession($downloadUrl);
     if (isset($filePath)) {
         $file = fopen($filePath, 'w');
         $curlUtils->SetOpt($ch, CURLOPT_FILE, $file);
     } else {
         $curlUtils->SetOpt($ch, CURLOPT_RETURNTRANSFER, 1);
     }
     $result = $curlUtils->Exec($ch);
     $httpCode = $curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE);
     $error = $curlUtils->Error($ch);
     $downloadSize = $curlUtils->GetInfo($ch, CURLINFO_SIZE_DOWNLOAD);
     $curlUtils->Close($ch);
     if (isset($file)) {
         fclose($file);
     }
     if ($httpCode != 200) {
         $message = sprintf('Invalid report download URL: %s', $downloadUrl);
         throw new InvalidArgumentException($message, $httpCode);
     }
     if (isset($filePath)) {
         return $downloadSize;
     } else {
         return $result;
     }
 }
 /**
  * Downloads a report from a URL. If the filePath parameter is specified it
  * will be downloaded to the file at that path, otherwise it will be
  * downloaded to memory and be returned as a string.
  * @param string $downloadUrl the URL of the report download
  * @param string $filePath an optional path of a file to download the report
  *     to
  * @return mixed if $filePath isn't specified it will return the contents of
  *     the report, otherwise the size in bytes of the downloaded report
  */
 public static function DownloadReport($downloadUrl, $filePath = null)
 {
     DeprecationUtils::LogDeprecatedMethodUsage('ReportUtils::DownloadReport', 'Please use ReportDownloader.php instead.');
     // TODO(vtsao): This method should not be static and instantiation of this
     // class should be allowed so we can "inject" CurlUtils, but would break too
     // many things that rely on this method being static.
     $curlUtils = new CurlUtils();
     $ch = $curlUtils->CreateSession($downloadUrl);
     if (isset($filePath)) {
         $file = fopen($filePath, 'w');
         $curlUtils->SetOpt($ch, CURLOPT_FILE, $file);
     } else {
         $curlUtils->SetOpt($ch, CURLOPT_RETURNTRANSFER, 1);
     }
     $result = $curlUtils->Exec($ch);
     $httpCode = $curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE);
     $error = $curlUtils->Error($ch);
     $downloadSize = $curlUtils->GetInfo($ch, CURLINFO_SIZE_DOWNLOAD);
     $curlUtils->Close($ch);
     if (isset($file)) {
         fclose($file);
     }
     if ($httpCode != 200) {
         $message = sprintf('Invalid report download URL: %s', $downloadUrl);
         throw new InvalidArgumentException($message, $httpCode);
     }
     if (isset($filePath)) {
         return $downloadSize;
     } else {
         return $result;
     }
 }
 /**
  * Downloads a Gzip report from an URL. to file located at {@code fileName}.
  * If the {@code filePath} is specified the report will be downloaded to the
  * file at that path, otherwise it will be downloaded to memory and
  * returned as a string.
  *
  * @param string $exportFormat the export format of the report
  * @param string $filePath an optional file path to download the report to
  * @return mixed report contents as a string if {@code filePath} isn't
  *     specified, otherwise the size of the downloaded report in bytes
  * @throws InvalidArgumentException if the report download URL is invalid
  */
 public function downloadReport($exportFormat, $filePath = null)
 {
     $downloadUrl = $this->getDownloadUrl($exportFormat);
     $curlUtils = new CurlUtils();
     $ch = $curlUtils->CreateSession($downloadUrl);
     if (isset($filePath)) {
         $file = fopen($filePath, 'w');
         $curlUtils->SetOpt($ch, CURLOPT_FILE, $file);
     } else {
         $curlUtils->SetOpt($ch, CURLOPT_RETURNTRANSFER, 1);
     }
     $result = $curlUtils->Exec($ch);
     $httpCode = $curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE);
     $error = $curlUtils->Error($ch);
     $downloadSize = $curlUtils->GetInfo($ch, CURLINFO_SIZE_DOWNLOAD);
     $curlUtils->Close($ch);
     if (isset($file)) {
         fclose($file);
     }
     if ($httpCode != 200) {
         $message = sprintf('Invalid report download URL: %s', $downloadUrl);
         throw new InvalidArgumentException($message, $httpCode);
     }
     if (isset($filePath)) {
         return $downloadSize;
     } else {
         return $result;
     }
 }
Example #4
0
 /**
  * Downloads a report from a URL. If the filePath parameter is specified it
  * will be downloaded to the file at that path, otherwise it will be
  * downloaded to memory and be returned as a string.
  * @param float $downloadUrl the URL of the report download
  * @param string $filePath an optional path of a file to download the report
  *     to
  * @return mixed if $filePath isn't specified it will return the contents of
  *     the report, otherwise the size in bytes of the downloaded report
  */
 public static function DownloadReport($downloadUrl, $filePath = NULL)
 {
     $ch = CurlUtils::CreateSession($downloadUrl);
     if (isset($filePath)) {
         $file = fopen($filePath, 'w');
         curl_setopt($ch, CURLOPT_FILE, $file);
     } else {
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     }
     $result = curl_exec($ch);
     $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $error = curl_error($ch);
     $downloadSize = curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
     curl_close($ch);
     if (isset($file)) {
         fclose($file);
     }
     if ($httpCode != 200) {
         $message = sprintf('Invalid report download URL: %s', $downloadUrl);
         throw new InvalidArgumentException($message, $httpCode);
     }
     if (isset($filePath)) {
         return $downloadSize;
     } else {
         return $result;
     }
 }
Example #5
0
 /**
  * Downloads a report using the URL provided.
  * @param string $url the URL to make the request to
  * @param array $headers the headers to use in the request
  * @param array $params the parameters to pass in the request
  * @param string $path the optional path to download the report to
  * @return mixed if path isn't specified the contents of the report,
  *     otherwise the size in bytes of the downloaded report
  */
 private static function DownloadReportFromUrl($url, $headers, $params, $path = NULL)
 {
     /*
      * This method should not be static and instantiation of this class should
      * be allowed so we can "inject" CurlUtils, but would break too many things
      * that rely on this method being static.
      */
     $curlUtils = new CurlUtils();
     $ch = $curlUtils->CreateSession($url);
     $curlUtils->SetOpt($ch, CURLOPT_POST, TRUE);
     $curlUtils->SetOpt($ch, CURLINFO_HEADER_OUT, TRUE);
     $flatHeaders = array();
     foreach ($headers as $name => $value) {
         $flatHeaders[] = sprintf('%s: %s', $name, $value);
     }
     $curlUtils->SetOpt($ch, CURLOPT_HTTPHEADER, $flatHeaders);
     if (isset($params)) {
         $curlUtils->SetOpt($ch, CURLOPT_POSTFIELDS, $params);
     }
     if (isset($path)) {
         $file = fopen($path, 'w');
         $curlUtils->SetOpt($ch, CURLOPT_RETURNTRANSFER, FALSE);
         $curlUtils->SetOpt($ch, CURLOPT_FILE, $file);
     }
     $response = $curlUtils->Exec($ch);
     $error = $curlUtils->Error($ch);
     $code = $curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE);
     $downloadSize = $curlUtils->GetInfo($ch, CURLINFO_SIZE_DOWNLOAD);
     $request = $curlUtils->GetInfo($ch, CURLINFO_HEADER_OUT);
     $curlUtils->Close($ch);
     if (isset($file)) {
         fclose($file);
     }
     $exception = null;
     if ($code != 200) {
         // Get the beginning of the response.
         if (isset($path)) {
             $file = fopen($path, 'r');
             $snippet = fread($file, self::$SNIPPET_LENGTH);
             fclose($file);
         } else {
             $snippet = substr($response, 0, self::$SNIPPET_LENGTH);
         }
         // Create exception.
         $error = self::ParseApiErrorXml($snippet);
         if ($error) {
             $errorMessage = "Report download failed. Underlying errors are \n";
             foreach ($error->ApiError as $apiError) {
                 $errorMessage .= sprintf("Type = '%s', Trigger = '%s', FieldPath = " . "'%s'. ", $apiError->type, $apiError->trigger, $apiError->fieldPath);
             }
             $exception = new ReportDownloadException($errorMessage, $code);
         } else {
             if (preg_match(self::$ERROR_MESSAGE_REGEX, $snippet, $matches)) {
                 $exception = new ReportDownloadException($matches[2], $code);
             } else {
                 if (!empty($error)) {
                     $exception = new ReportDownloadException($error);
                 } else {
                     if (isset($code)) {
                         $exception = new ReportDownloadException('Report download failed.', $code);
                     }
                 }
             }
         }
     }
     self::LogRequest($request, $code, $params, $exception);
     if (isset($exception)) {
         throw $exception;
     } else {
         if (isset($path)) {
             return $downloadSize;
         } else {
             return $response;
         }
     }
 }
Example #6
0
 /**
  * Makes the client login request and stores the result.
  * @return string the response from the ClientLogin API
  * @throws AuthTokenException if an error occurs during authentication
  * @access private
  */
 private function Login()
 {
     $postUrl = $this->server . '/accounts/ClientLogin';
     $postVars = http_build_query(array('accountType' => $this->accountType, 'Email' => $this->email, 'Passwd' => $this->password, 'service' => $this->service, 'source' => $this->source, 'logintoken' => $this->captchaToken, 'logincaptcha' => $this->captchaResponse), NULL, '&');
     $ch = CurlUtils::CreateSession($postUrl);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $postVars);
     $response = curl_exec($ch);
     $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $error = curl_error($ch);
     curl_close($ch);
     if (!empty($error)) {
         throw new AuthTokenException($error);
     } else {
         if ($httpCode != 200 && $httpCode != 403) {
             throw new AuthTokenException($httpCode);
         }
     }
     return $response;
 }
 /**
  * Makes an HTTP request to the given URL and extracts the returned OAuth
  * token.
  * @param string $url the URL to make the request to
  * @return OAuthToken the returned token
  */
 private function GetTokenFromUrl($url)
 {
     $ch = CurlUtils::CreateSession($url);
     $response = curl_exec($ch);
     $error = curl_error($ch);
     $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     curl_close($ch);
     if (!empty($error)) {
         throw new OAuthException($error, $httpCode);
     }
     if ($httpCode != 200) {
         throw new Exception($response, $httpCode);
     }
     return self::GetTokenFromQueryString($response);
 }
 /**
  * Makes an HTTP request to the given URL and extracts the returned OAuth
  * token.
  * @param string $url the URL to make the request to
  * @return OAuthToken the returned token
  */
 private function GetTokenFromUrl($url)
 {
     $ch = CurlUtils::CreateSession($url);
     $response = curl_exec($ch);
     $headers = curl_getinfo($ch);
     curl_close($ch);
     if ($headers['http_code'] != 200) {
         throw new Exception($response);
     }
     return self::GetTokenFromQueryString($response);
 }
 /**
  * Downloads a report using the URL provided.
  * @param string $url the URL to make the request to
  * @param array $headers the headers to use in the request
  * @param array $params the parameters to pass in the request
  * @param string $path the optional path to download the report to
  * @return mixed if path isn't specified the contents of the report,
  *     otherwise the size in bytes of the downloaded report
  */
 private static function DownloadReportFromUrl($url, $headers, $params, $path = NULL)
 {
     $ch = CurlUtils::CreateSession($url);
     curl_setopt($ch, CURLOPT_POST, TRUE);
     curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
     $flatHeaders = array();
     foreach ($headers as $name => $value) {
         $flatHeaders[] = sprintf('%s: %s', $name, $value);
     }
     curl_setopt($ch, CURLOPT_HTTPHEADER, $flatHeaders);
     if (isset($params)) {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
     }
     if (isset($path)) {
         $file = fopen($path, 'w');
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
         curl_setopt($ch, CURLOPT_FILE, $file);
     }
     $response = curl_exec($ch);
     $error = curl_error($ch);
     $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $downloadSize = curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
     $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
     curl_close($ch);
     if (isset($file)) {
         fclose($file);
     }
     $exception = null;
     if ($code != 200) {
         // Get the beginning of the response.
         if (isset($path)) {
             $file = fopen($path, 'r');
             $snippet = fread($file, self::$SNIPPET_LENGTH);
             fclose($file);
         } else {
             $snippet = substr($response, 0, self::$SNIPPET_LENGTH);
         }
         // Create exception.
         if (preg_match(self::$ERROR_MESSAGE_REGEX, $snippet, $matches)) {
             $exception = new ReportDownloadException($matches[2], $code);
         } else {
             if (!empty($error)) {
                 $exception = new ReportDownloadException($error);
             } else {
                 if (isset($code)) {
                     $exception = new ReportDownloadException('Report download failed.', $code);
                 }
             }
         }
     }
     self::LogRequest($request, $code, $params, $exception);
     if (isset($exception)) {
         throw $exception;
     } else {
         if (isset($path)) {
             return $downloadSize;
         } else {
             return $response;
         }
     }
 }