Skip to content

Commit

Permalink
fix and dev
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1881 committed May 16, 2022
1 parent 03d7718 commit 2344047
Showing 1 changed file with 55 additions and 30 deletions.
85 changes: 55 additions & 30 deletions src/curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace C4N;

use ReflectionClass;
use stdClass;

class C4NException extends \Exception
{
Expand Down Expand Up @@ -751,21 +752,29 @@ public function getRespJson(bool $array = false, $flags = 0)
*/
public function find($search_datas, $source = null)
{
$json = new stdClass();
$json->result = false;
if (\is_null($source)) {
$source = $this->getResponse();
}

if (\is_array($search_datas)) {
foreach ($search_datas as $search_data) {
$search_data = \preg_quote($search_data, '/');
if (\preg_match('/' . $search_data . '/si', $source)) $result = json_encode(['result' => true, 'finded' => $search_data]);
if (\preg_match('/' . $search_data . '/si', $source)) {
$json->result = true;
$json->finded = $search_data;
}
}
} else {
$search_data = \preg_quote($search_datas, '/');
if (\preg_match('/' . $search_data . '/si', $source)) $result = json_encode(['result' => true, 'finded' => $search_datas]);
if (\preg_match('/' . $search_data . '/si', $source)) {
$json->result = true;
$json->finded = $search_datas;
}
}
$null = json_encode(['result' => false]);
return isset($result) ? json_decode($result) : json_decode($null);

return $json;
}

/**
Expand Down Expand Up @@ -813,7 +822,7 @@ public function getBetweens(string $start = '', string $end = '', bool $remove_l
*/
public function getEffective(): string
{
return $this->res->effective_url;
return strval($this->res->effective_url);
}

/**
Expand All @@ -834,13 +843,32 @@ public function getHttpCode(): int
* @return mixed
*/
public function getHeader(string $header, int $header_id = null)
{
$headers = $this->getHeaders($header_id);
if (\array_key_exists($header, $headers)) {
return $headers[$header];
} else {
$this->setError('Header not found', $header);
}
}

/**
* Getting headers from request response
*
* @param int|null $header_id
* @return mixed
*/
public function getHeaders(int $header_id = null)
{
if (\is_null($header_id)) {
$header = end($this->res->headers_array)[$header] ?? false;
$header = end($this->res->headers_array);
} else {
$header = $this->res->headers_array[$header_id][$header] ?? false;
if (!isset($this->res->headers_array[$header_id])) {
$this->setError('Header not found', "Header id: " . $header_id);
}
$header = $this->res->headers_array[$header_id];
}
return $header;
return $header ?? [];
}

/**
Expand All @@ -852,12 +880,12 @@ public function getHeader(string $header, int $header_id = null)
*/
public function getCookie(string $cookie, int $header_id = null)
{
if (\is_null($header_id)) {
$cookie = end($this->res->headers_array)['set_cookie'][$cookie] ?? false;
$cookies = $this->getCookiesArray($header_id);
if (\array_key_exists($cookie, $cookies)) {
return $cookies[$cookie];
} else {
$cookie = $this->res->headers_array[$header_id]['set_cookie'][$cookie] ?? false;
$this->setError('Cookie not found', $cookie);
}
return $cookie;
}

/**
Expand All @@ -868,21 +896,8 @@ public function getCookie(string $cookie, int $header_id = null)
*/
public function getCookiesRaw(int $header_id = null): string
{
if (\is_null($header_id)) {
$array = end($this->res->headers_array)['set_cookie'] ?? false;
$cookie = '';
foreach ($array as $key => $val) {
$cookie .= $key . '=' . $val . '; ';
}
} else {
$array = $this->res->headers_array[$header_id]['set_cookie'] ?? false;
$cookie = '';
foreach ($array as $key => $val) {
$cookie .= $key . '=' . $val . '; ';
}
}
$cookie = trim($cookie);
return $cookie;
$cookies = $this->getCookiesArray($header_id);
return http_build_query($cookies, '', '; ');
}

/**
Expand All @@ -894,11 +909,21 @@ public function getCookiesRaw(int $header_id = null): string
public function getCookiesArray(int $header_id = null): array
{
if (\is_null($header_id)) {
$cookie = end($this->res->headers_array)['set_cookie'] ?? false;
$last_array = end($this->res->headers_array);
$cookie_check = array_key_exists('set_cookie', $last_array);
if ($cookie_check) $cookies = $last_array['set_cookie'];
else $this->setError('Cookies not found', 'set_cookie not found in header id: ' . $header_id);
} else {
$cookie = $this->res->headers_array[$header_id]['set_cookie'] ?? false;
if (!isset($this->res->headers_array[$header_id])) {
$this->setError('Header not found', "Header id: " . $header_id);
}

$select_array = $this->res->headers_array[$header_id];
$cookie_check = array_key_exists('set_cookie', $select_array);
if ($cookie_check) $cookies = $select_array['set_cookie'];
else $this->setError('Cookies not found', 'set_cookie not found in header id: ' . $header_id);
}
return $cookie;
return $cookies ?? [];
}

/**
Expand Down

0 comments on commit 2344047

Please sign in to comment.