Code cleanup

This commit is contained in:
Floorb 2022-03-26 23:57:28 -04:00
parent 02715158d3
commit 82dd103144
13 changed files with 32 additions and 152 deletions

View file

@ -23,6 +23,6 @@ $results = Tag::select('name')
->fetchAll()
->toArray();
array_push($tags, ['name' => $tag_name]);
$tags[] = ['name' => $tag_name];
echo json_encode($tags);

View file

@ -15,5 +15,5 @@ updatePageViews();
// Theme
$page_template = 'archive';
$page_title = 'Pastes Archive';
array_push($script_bundles, 'archive');
$script_bundles[] = 'archive';
require_once('theme/' . $default_theme . '/common.php');

View file

@ -5,19 +5,6 @@ require_once('includes/functions.php');
use PonePaste\Models\Paste;
function transformPasteRow(Paste $row) : array {
return [
'id' => $row['id'],
'title' => $row['title'],
'member' => $row['member'],
'time' => $row['created_at'],
'time_update' => $row['updated_at'],
'friendly_update_time' => friendlyDateDifference(new DateTime($row['updated_at']), new DateTime()),
'friendly_time' => friendlyDateDifference(new DateTime($row['created_at']), new DateTime()),
'tags' => $row->tags
];
}
$popular_pastes = Paste::getMostViewed();//->map('transformPasteRow');
$monthly_popular_pastes = Paste::getMonthPopular();//->map('transformPasteRow');
$recent_pastes = Paste::getRecent();//->map('transformPasteRow');

View file

@ -1,38 +0,0 @@
<?php
class DatabaseHandle {
private PDO $conn;
public function __construct(string $conString, string $username, string $password) {
$this->conn = new PDO($conString, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]);
}
public function prepare(string $query) : PDOStatement {
return $this->conn->prepare($query);
}
public function query(string $query, array $params = null) : PDOStatement {
if (empty($params)) {
return $this->conn->query($query);
}
$stmt = $this->conn->prepare($query);
$stmt->execute($params);
return $stmt;
}
public function querySelectOne(string $query, array $params = null, int $fetchMode = PDO::FETCH_ASSOC) : array|null {
$stmt = $this->query($query, $params);
if ($row = $stmt->fetch($fetchMode)) {
return $row;
}
return null;
}
}

View file

@ -47,7 +47,7 @@ class Tag extends Model {
$cleanName = Tag::cleanTagName($tagName);
if (!empty($cleanName)) {
array_push($cleanTags, $cleanName);
$cleanTags[] = $cleanName;
}
}

View file

@ -1,37 +0,0 @@
<?php
use Illuminate\Database\Eloquent\Builder;
class NonRetardedSSP {
public static function run(array $request, Builder $builder) {
/* Some of these parameters might not be passed; zero is an OK default */
$draw = (int) @$request['draw'];
$start = (int) @$request['start'];
$length = (int) @$request['length'];
/* figure out total records */
$recordsTotal = $builder->count();
/* build query */
$params = [];
if ($length != 0) {
$builder = $builder->limit($length);
if ($start != 0) {
$builder = $builder->offset($start);
}
}
/* fire it off */
$data = $builder->get();
return [
'draw' => $draw,
'recordsTotal' => $recordsTotal,
'recordsFiltered' => ($recordsTotal - count($data)),
'data' => $data
];
}
}

View file

@ -1,34 +0,0 @@
<?php
class ViewBag {
private static array $global = [];
private static array $local = [];
public static function putGlobal(string $key, string $value) : void {
ViewBag::$global[$key] = $value;
}
public static function put(string $key, string $value) : void {
ViewBag::$local[$key] = $value;
}
public static function getGlobal(string $key, bool $escape = true) : string {
$value = ViewBag::$global[$key];
if ($escape) {
$value = pp_html_escape($value);
}
return $value;
}
public static function get(string $key, bool $escape = true) : string {
$value = ViewBag::$local[$key];
if ($escape) {
$value = pp_html_escape($value);
}
return $value;
}
}

View file

@ -147,8 +147,9 @@ if (isset($_GET['_CAPTCHA'])) {
$font = $captcha_config['fonts'][rand(0, count($captcha_config['fonts']) - 1)];
// Verify font file exists
if (!file_exists($font))
throw new Exception('Font file not found: ' . $font);
if (!file_exists($font)) {
die('Font file not found.');
}
// Set the font size
$font_size = rand($captcha_config['min_font_size'], $captcha_config['max_font_size']);

View file

@ -88,7 +88,7 @@ function flash(string $level, string $message) {
throw new Exception('Invalid flash level ' . $level);
}
array_push($_SESSION['flashes'][$level], $message);
$_SESSION['flashes'][$level][] = $message;
}

View file

@ -1,5 +1,6 @@
<?php
use Illuminate\Database\Eloquent\Collection;
use PonePaste\Models\Paste;
function tagsToHtml(array | Collection $tags) : string {
$output = "";
@ -215,7 +216,7 @@ function embedView($paste_id, $p_title, $content, $title) {
return $stats;
}
function addToSitemap(\PonePaste\Models\Paste $paste, $priority, $changefreq) {
function addToSitemap(Paste $paste, $priority, $changefreq) {
$c_date = date('Y-m-d');
$site_data = file_get_contents("sitemap.xml");
$site_data = str_replace("</urlset>", "", $site_data);

View file

@ -28,15 +28,15 @@ function replaceSelection(e, t) {
var n = e.selectionStart;
var r = e.selectionEnd;
e.value = e.value.substring(0, n) + t + e.value.substring(r);
if (n != r) {
if (n !== r) {
setSelectionRange(e, n, n + t.length);
} else {
setSelectionRange(e, n + t.length, n + t.length);
}
} else if (document.selection) {
var i = document.selection.createRange();
if (i.parentElement() == e) {
var s = i.text == "";
if (i.parentElement() === e) {
var s = i.text === "";
i.text = t;
if (!s) {
i.moveStart("character", -t.length);
@ -47,13 +47,14 @@ function replaceSelection(e, t) {
}
function catchTab(e, t) {
let c;
if (navigator.userAgent.match("Gecko")) {
c = t.which;
} else {
c = t.keyCode;
}
if (c == 9) {
var n = e.scrollTop;
if (c === 9) {
const n = e.scrollTop;
replaceSelection(e, String.fromCharCode(9));
stopEvent(t);
e.scrollTop = n;
@ -77,8 +78,7 @@ var js = {
return this.getLines(e).length;
},
getLines: function (e) {
var t = e.split("\n");
return t;
return e.split("\n");
},
},
textElement: {
@ -97,7 +97,7 @@ var js = {
}
t.start = i;
t.end = i + n.text.replace(/\r/g, "").length;
} else if (e.selectionStart || e.selectionStart == 0) {
} else if (e.selectionStart || e.selectionStart === 0) {
t.start = e.selectionStart;
t.end = e.selectionEnd;
}
@ -118,32 +118,32 @@ var js = {
};
function highlight(e) {
var t = js.textElement.caretPosition(e);
const t = js.textElement.caretPosition(e);
if (!t.start && !t.end) return;
var n = js.text.getLines(js.textElement.value(e));
var r = 0,
const n = js.text.getLines(js.textElement.value(e));
let r = 0,
i = 0;
var s = "";
var o = false;
var u = 0;
for (var a in n) {
let s = "";
let o = false;
let u = 0;
for (const a in n) {
i = r + n[a].length;
if (t.start >= r && t.start <= i) o = true;
if (o) {
var f = n[a].substr(0, 11) == "!highlight!";
const f = n[a].substr(0, 11) === "!highlight!";
if (!u) {
if (f) u = 1;
else u = 2;
}
if (u == 1 && f) n[a] = n[a].substr(11, n[a].length - 11);
else if (u == 2 && !f) s += "!highlight!";
if (u === 1 && f) n[a] = n[a].substr(11, n[a].length - 11);
else if (u === 2 && !f) s += "!highlight!";
}
s = s + n[a] + "\n";
if (t.end >= r && t.end <= i) o = false;
r = i + 1;
}
e.value = s.substring(0, s.length - 1);
var l = t.start + (u == 1 ? -11 : 11);
const l = t.start + (u === 1 ? -11 : 11);
js.textElement.setCaretPosition(e, {
start: l,
end: l,
@ -152,7 +152,7 @@ function highlight(e) {
function togglev() {
if (
document.getElementsByTagName("ol")[0].style.listStyle.substr(0, 4) ==
document.getElementsByTagName("ol")[0].style.listStyle.substr(0, 4) ===
"none"
) {
document.getElementsByTagName("ol")[0].style.listStyle = "decimal";

View file

@ -1,7 +1,7 @@
<link rel="stylesheet" href="theme/bulma/css/bulma-tagsinput.min.css"/>
<script>
function openreport() {
var x = document.getElementById("panel");
const x = document.getElementById("panel");
if (x.style.display === "none") {
x.style.display = "block";
} else {
@ -10,7 +10,7 @@
}
function closereport() {
var x = document.getElementById("panel");
const x = document.getElementById("panel");
if (x.style.display === "none") {
x.style.display = "block";
} else {

View file

@ -67,5 +67,5 @@ updatePageViews();
$csrf_token = setupCsrfToken();
$page_title = 'Profile of ' . $profile_username;
$page_template = 'user_profile';
array_push($script_bundles, 'user_profile');
$script_bundles[] = 'user_profile';
require_once('theme/' . $default_theme . '/common.php');