Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #673 from denisdulici/httpful
Browse files Browse the repository at this point in the history
New HTTP client
  • Loading branch information
denisdulici committed Mar 6, 2017
2 parents a059a26 + 3b2aafe commit 4d46f43
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 136 deletions.
6 changes: 5 additions & 1 deletion admin/controller/extension/installer.php
Expand Up @@ -716,7 +716,11 @@ public function install()
$this->request->post['store'] = 'extension';
}

$data = $this->utility->getRemoteData(html_entity_decode("http://arastta.io/" . rtrim($this->request->post['store'], 's') . "/1.0/download/" . $this->request->post['product_id'] . "/latest/" . VERSION . "/" . $this->config->get('api_key')), array('referrer' => true));
$url = html_entity_decode("http://arastta.io/" . rtrim($this->request->post['store'], 's') . "/1.0/download/" . $this->request->post['product_id'] . "/latest/" . VERSION . "/" . $this->config->get('api_key'));
$data = \Httpful\Request::get($url)
->addOnCurlOption('CURLOPT_REFERER', $this->url->getDomain())
->send()
->raw_body;

$response = json_decode($data, true);

Expand Down
18 changes: 12 additions & 6 deletions admin/model/common/update.php
Expand Up @@ -28,7 +28,9 @@ public function changelog()

$url = 'https://api.github.com/repos/arastta/arastta/releases';

$json = $this->utility->getRemoteData($url);
$json = \Httpful\Request::get($url)
->send()
->raw_body;

if (empty($json)) {
return $output;
Expand Down Expand Up @@ -235,7 +237,10 @@ public function getVersions($addons = array())

public function getRemoteVersion($url)
{
$remote_data = $this->utility->getRemoteData($url, array('referrer' => true));
$remote_data = \Httpful\Request::get($url)
->addOnCurlOption('CURLOPT_REFERER', $this->url->getDomain())
->send()
->raw_body;

if (is_string($remote_data)) {
$version = json_decode($remote_data);
Expand Down Expand Up @@ -270,10 +275,11 @@ public function downloadUpdate($product_id, $version)
$url = $base_url.'/'.$type.'/1.0/update/'.$product_id.'/'.$version.'/'.$info['arastta'].'/'.$info['api'];
}

$options['timeout'] = 30;
$options['referrer'] = true;

$file = $this->utility->getRemoteData($url, $options);
$file = \Httpful\Request::get($url)
->timeout(30)
->addOnCurlOption('CURLOPT_REFERER', $this->url->getDomain())
->send()
->raw_body;

return $file;
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -21,8 +21,9 @@
"joomla/cache": "1.*@dev",
"joomla/profiler": "1.*@dev",
"joomla/uri": "1.*@dev",
"psr/log": "1.0.0",
"kartik-v/bootstrap-fileinput": "4.3.*",
"nategood/httpful": "0.2.*",
"psr/log": "1.0.0",
"symfony/console": "2.8.*",
"symfony/debug": "2.8.*",
"symfony/dom-crawler": "2.8.*",
Expand Down
8 changes: 6 additions & 2 deletions install/model/language.php
Expand Up @@ -17,7 +17,9 @@ public function getLanguages()
if (empty($data)) {
$link = 'http://arastta.io/translation/1.0/installer/translated';

$json = $this->utility->getRemoteData($link);
$json = \Httpful\Request::get($link)
->send()
->raw_body;

if (!empty($json)) {
$data = json_decode($json, true);
Expand Down Expand Up @@ -48,7 +50,9 @@ public function downloadLanguage($data)

$link = 'https://crowdin.com/download/project/arastta/'.$code.'.zip';

$data = $this->utility->getRemoteData($link);
$data = \Httpful\Request::get($link)
->send()
->raw_body;

if (empty($data)) {
return false;
Expand Down
5 changes: 4 additions & 1 deletion install/model/setting.php
Expand Up @@ -92,7 +92,10 @@ public function createDatabaseTables($data)
$db->query("SET @@session.sql_mode = 'MYSQL40'");

// Check if admin uses Gravatar
$gravatar = $this->utility->getRemoteData('https://www.gravatar.com/avatar/' . md5(strtolower($data['admin_email'])).'?size=45&d=404', array('timeout' => 3));
$gravatar = \Httpful\Request::get('https://www.gravatar.com/avatar/' . md5(strtolower($data['admin_email'])).'?size=45&d=404')
->timeout(3)
->send()
->raw_body;

if ($gravatar) {
$user_image = '';
Expand Down
125 changes: 0 additions & 125 deletions system/library/utility.php
Expand Up @@ -154,131 +154,6 @@ public function getBrowserLangCode($system_langs)
return $lang;
}

public function getRemoteData($url, $options = array('timeout' => 10))
{
$user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
$data = false;

// cURL
if (extension_loaded('curl')) {
$process = @curl_init($url);

@curl_setopt($process, CURLOPT_HEADER, false);
@curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
@curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
@curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
@curl_setopt($process, CURLOPT_AUTOREFERER, true);
@curl_setopt($process, CURLOPT_FAILONERROR, true);
@curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
@curl_setopt($process, CURLOPT_TIMEOUT, $options['timeout']);
@curl_setopt($process, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
@curl_setopt($process, CURLOPT_MAXREDIRS, 20);

if (!empty($options['referrer'])) {
@curl_setopt($process, CURLOPT_REFERER, $this->url->getDomain());
}

if (!empty($options['post'])) {
@curl_setopt($process, CURLOPT_POST, 1);
@curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($options['post_fields']));
}

$data = @curl_exec($process);

@curl_close($process);

return $data;
}

// fsockopen
if (function_exists('fsockopen')) {
$errno = 0;
$errstr = '';

$url_info = parse_url($url);
if ($url_info['host'] == 'localhost') {
$url_info['host'] = '127.0.0.1';
}

// Open socket connection
if ($url_info['scheme'] == 'http') {
$fsock = @fsockopen($url_info['scheme'].'://'.$url_info['host'], 80, $errno, $errstr, 5);
} else {
$fsock = @fsockopen('ssl://'.$url_info['host'], 443, $errno, $errstr, 5);
}

if ($fsock) {
@fputs($fsock, 'GET '.$url_info['path'].(!empty($url_info['query']) ? '?'.$url_info['query'] : '').' HTTP/1.1'."\r\n");
@fputs($fsock, 'HOST: '.$url_info['host']."\r\n");
@fputs($fsock, "User-Agent: ".$user_agent."\n");
@fputs($fsock, 'Connection: close'."\r\n\r\n");

// Set timeout
@stream_set_blocking($fsock, 1);
@stream_set_timeout($fsock, 5);

$data = '';
$passed_header = false;
while (!@feof($fsock)) {
if ($passed_header) {
$data .= @fread($fsock, 1024);
} else {
if (@fgets($fsock, 1024) == "\r\n") {
$passed_header = true;
}
}
}

// Clean up
@fclose($fsock);

// Return data
return $data;
}
}

// fopen
if (function_exists('fopen') && ini_get('allow_url_fopen')) {
// Set timeout
if (ini_get('default_socket_timeout') < 5) {
ini_set('default_socket_timeout', 5);
}

@stream_set_blocking($handle, 1);
@stream_set_timeout($handle, 5);
@ini_set('user_agent', $user_agent);

$url = str_replace('://localhost', '://127.0.0.1', $url);

$handle = @fopen($url, 'r');

if ($handle) {
$data = '';
while (!feof($handle)) {
$data .= @fread($handle, 8192);
}

// Clean up
@fclose($handle);

// Return data
return $data;
}
}

// file_get_contents
if (function_exists('file_get_contents') && ini_get('allow_url_fopen')) {
$url = str_replace('://localhost', '://127.0.0.1', $url);
@ini_set('user_agent', $user_agent);
$data = @file_get_contents($url);

// Return data
return $data;
}

return $data;
}

public function getInfo()
{
$info = array();
Expand Down

0 comments on commit 4d46f43

Please sign in to comment.