Viewing File: /usr/local/cpanel/3rdparty/wpinstaller/src/WpDownloader/HttpApi.php
<?php
namespace WpDownloader;
use WpDownloader\Log;
abstract class HttpApi
{
public static $baseUrl;
protected static $logFile = 'api';
protected static $logRequests = false;
protected static function apiRequest($path, $params=null, $postParams=null)
{
$response = self::httpRequest(self::buildApiUrl($path, $params), $postParams);
return static::unserializeResponse($response);
}
protected static function unserializeResponse($responseText)
{
$toReturn = json_decode($responseText, 1);
if (json_last_error()) {
Log::throwException("Error decoding JSON: ".json_last_error_msg().'json was: '.substr($responseText, 0, 600));
}
return $toReturn;
}
protected static function httpRequest($url, $postParams=null)
{
self::log("Sending request to {$url}");
$ch = self::prepareCurl($url);
if ($postParams !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
}
// Execute
$result=curl_exec($ch);
self::log("Got response {$result}");
// Closing
curl_close($ch);
return $result;
}
protected static function resolveRedirect($url)
{
$ch = self::prepareCurl($url);
$result=curl_exec($ch);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
self::log("Got response:" . curl_getinfo($ch, CURLINFO_HTTP_CODE));
return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
private static function prepareCurl($url=null)
{
$ch = curl_init();
// cURL timeout settings
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
// Disable SSL verification
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Seems dangerous to ignore SSL warnings when unserializing php. Opens us up to MitM attacks. See warning @ http://php.net/manual/en/function.unserialize.php
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, self::getUserAgent());
if ($url) {
curl_setopt($ch, CURLOPT_URL, $url);
}
return $ch;
}
public static function getUserAgent()
{
return 'funklabs/wpinstaller/updater';
}
protected static function buildApiUrl($path, $params)
{
if (!empty($params)) {
$queryString = '?'.http_build_query($params);
} else {
$queryString = '';
}
return static::$baseUrl . "{$path}{$queryString}";
}
protected static function log($msg, $level=7)
{
Log::write($msg, $level, static::$logFile);
}
}
Back to Directory
File Manager