Skip to content

Commit

Permalink
fix: some refactoring/fixing of sub-optimal PHP code (merge pull requ…
Browse files Browse the repository at this point in the history
…est #123 from dehlirious/minor-changes)

Minor fixes
  • Loading branch information
Ahwxorg authored Feb 24, 2024
2 parents 770c49f + 054f6a1 commit 5acc51d
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 37 deletions.
10 changes: 5 additions & 5 deletions donate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</div>

<img
src="/static/images/btc.png"
src="static/images/btc.png"
height="160"
width="160"
alt="btc qr code"
Expand All @@ -35,7 +35,7 @@
</p>
</div>
<img
src="/static/images/xmr.png"
src="static/images/xmr.png"
height="160"
width="160"
alt="xmr qr code (hnhx)"
Expand All @@ -57,7 +57,7 @@
</p>
</div>
<img
src="/static/images/xmr-ahwx.png"
src="static/images/xmr-ahwx.png"
height="160"
width="160"
alt="xmr qr code (ahwx)"
Expand All @@ -67,15 +67,15 @@
<div class="flex-row">
<a href="https://ko-fi.com/Ahwxorg" target="_blank"
><img
src="/static/images/kofi.png"
src="static/images/kofi.png"
alt="kifi img"
height="50"
width="auto"
/></a>

<a href="https://www.buymeacoffee.com/ahwx" target="_blank">
<img
src="/static/images/buy-me-a-coffee.png"
src="static/images/buy-me-a-coffee.png"
height="50"
width="auto"
alt="buy-me-a-coffee img"
Expand Down
41 changes: 21 additions & 20 deletions engines/invidious/video.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function get_request_url() {

public function parse_results($response) {
$results = array();
$json_response = json_decode($response, true);
$json_response = json_decode($response, true) ?? [];

foreach ($json_response as $response) {
if ($response["type"] == "video") {
Expand Down Expand Up @@ -41,26 +41,27 @@ public function parse_results($response) {
public static function print_results($results, $opts) {
echo "<div class=\"text-result-container\">";

foreach($results as $result) {
$title = $result["title"];
$url = $result["url"];
$url = check_for_privacy_frontend($url, $opts);
$base_url = get_base_url($url);
$uploader = $result["uploader"];
$views = $result["views"];
$date = $result["date"];
$thumbnail = $result["thumbnail"];
foreach ($results as $result) {
$title = $result["title"] ?? '';
$url = $result["url"] ?? '';
$url = check_for_privacy_frontend($url, $opts);
$base_url = get_base_url($url);
$uploader = $result["uploader"] ?? '';
$views = $result["views"] ?? '';
$date = $result["date"] ?? '';
$thumbnail = $result["thumbnail"] ?? '';

echo "<div class=\"text-result-wrapper\">";
echo "<a href=\"$url\">";
echo "$base_url";
echo "<h2>$title</h2>";
echo "<img class=\"video-img\" src=\"image_proxy.php?url=$thumbnail\">";
echo "<br>";
echo "<span>$uploader - $date - $views views</span>";
echo "</a>";
echo "</div>";
}

echo "<div class=\"text-result-wrapper\">";
echo "<a href=\"$url\">";
echo "$base_url";
echo "<h2>$title</h2>";
echo "<img class=\"video-img\" src=\"image_proxy.php?url=$thumbnail\">";
echo "<br>";
echo "<span>$uploader - $date - $views views</span>";
echo "</a>";
echo "</div>";
}

echo "</div>";
}
Expand Down
3 changes: 1 addition & 2 deletions engines/librex/fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function get_librex_results($opts) {
if (!(filter_var($instance, FILTER_VALIDATE_URL)))
continue;

if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
if (parse_url($instance)["host"] == $_SERVER['HTTP_HOST'])
continue;

$librex_request = new LibreXFallback($instance, $opts, null);
Expand All @@ -62,7 +62,6 @@ function get_librex_results($opts) {

if (!empty($results)) {
$results["results_source"] = parse_url($instance)["host"];
error_log($results["results_source"]);
return $results;
}

Expand Down
2 changes: 1 addition & 1 deletion engines/text/brave.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function parse_results($response) {
continue;
$title = $title->textContent;

$description = $xpath->evaluate(".//div[contains(@class, 'snippet-content')]//div[contains(@class, 'snippet-description')]", $result)[0]->textContent;
$description = ($xpath->evaluate(".//div[contains(@class, 'snippet-content')]//div[contains(@class, 'snippet-description')]", $result)[0] ?? null) ?->textContent ?? '';

array_push($results,
array (
Expand Down
2 changes: 1 addition & 1 deletion engines/text/mojeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function parse_results($response) {

$title = $title->textContent;

$description = $xpath->evaluate(".//p[contains(@class, 's')]", $result)[0]->textContent;
$description = ($xpath->evaluate(".//p[contains(@class, 's')]", $result)[0] ?? null) ?->textContent ?? '';

array_push($results,
array (
Expand Down
2 changes: 1 addition & 1 deletion engines/text/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function parse_results($response) {
$results = $this->engine_request->get_results();

if (empty($results)) {
set_cooldown($this->engine, ($opts->request_cooldown ?? "1") * 60, $this->opts->cooldowns);
set_cooldown($this->engine, ($this->opts->request_cooldown ?? "1") * 60, $this->opts->cooldowns);
} else {
if ($this->special_request) {
$special_result = $this->special_request->get_results();
Expand Down
2 changes: 1 addition & 1 deletion locale/localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function printtext($key) {
function printftext() {
$argv = func_get_args();
$key = array_shift($argv);
vprintf(TEXTS[$key], $argv);
return vprintf(TEXTS[$key], $argv);
}

// default to language "en"
Expand Down
2 changes: 1 addition & 1 deletion misc/search_engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ abstract class EngineRequest {
protected $url, $query, $page, $opts, $mh, $ch;

protected $DO_CACHING = true;
function __construct($opts, $mh) {
public function __construct($opts, $mh) {
$this->query = $opts->query;
$this->page = $opts->page;
$this->mh = $mh;
Expand Down
39 changes: 38 additions & 1 deletion misc/tools.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php
function get_base_url($url) {
$parsed = parse_url($url);
return $parsed["scheme"] . "://" . $parsed["host"] . "/";

if (isset($parsed["scheme"]) && isset($parsed["host"]) && !empty($parsed["scheme"]) && !empty($parsed["host"])) {
return $parsed["scheme"] . "://" . $parsed["host"] . "/";
} else {
logStackTrace();
return "";
}
}


function get_root_domain($url) {
return parse_url($url, PHP_URL_HOST);
}
Expand Down Expand Up @@ -137,4 +144,34 @@ function get_country_emote($code) {
return join($emoji);
}

function logStackTrace() {
// Get the stack trace
$stackTrace = debug_backtrace();

// Format the stack trace for logging
$logMessage = "Stack Trace: ";
foreach ($stackTrace as $index => $trace) {
// Skip the first entry as it's the current function call
if ($index === 0) {
continue;
}

// Build the log message for each stack frame
$logMessage .= "#{$index} ";
if (isset($trace['file'])) {
$logMessage .= "File: {$trace['file']} ";
}
if (isset($trace['line'])) {
$logMessage .= "Line: {$trace['line']} ";
}
if (isset($trace['function'])) {
$logMessage .= "Function: {$trace['function']} ";
}
$logMessage .= "\n";
}

// Log the stack trace to the error log
error_log($logMessage);
}

?>
5 changes: 1 addition & 4 deletions static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ a:hover,
}

.sub-search-container input {
margin-bottom: 20px;
width: 580px;
position: relative;
left: 140px;
Expand Down Expand Up @@ -462,7 +461,6 @@ hr.small-line {
padding: 10px;
font-size: 28px;
display: block;
margin-top: 0px;
top: 0px;
left: 0px;
}
Expand Down Expand Up @@ -552,7 +550,6 @@ hr.small-line {
word-wrap: break-word;
align-items: center;
justify-content: space-between;
word-wrap: break-word;
height: auto;
}

Expand Down Expand Up @@ -587,4 +584,4 @@ hr.small-line {
/* donate css end */


}
}

0 comments on commit 5acc51d

Please sign in to comment.