ponepaste/includes/functions.php

344 lines
12 KiB
PHP
Raw Normal View History

2021-07-10 19:18:17 +01:00
<?php
2021-08-05 08:18:32 -04:00
function getPasteTags(DatabaseHandle $conn, int $paste_id) : array {
return $conn->query(
'SELECT name, slug FROM tags
INNER JOIN paste_taggings ON paste_taggings.tag_id = tags.id
WHERE paste_taggings.paste_id = ?',
[$paste_id])->fetchAll();
}
function getUserFavs(DatabaseHandle $conn, int $user_id) : array {
2021-07-15 18:06:24 -04:00
$query = $conn->prepare(
2021-08-05 08:18:32 -04:00
"SELECT pins.f_time, pastes.id, pins.paste_id, pastes.title, pastes.created_at, pastes.updated_at
2021-07-16 09:53:34 -04:00
FROM pins
INNER JOIN pastes ON pastes.id = pins.paste_id
WHERE pins.user_id = ?");
$query->execute([$user_id]);
return $query->fetchAll();
2021-07-10 19:18:17 +01:00
}
2021-07-20 12:15:41 -04:00
function checkFavorite(DatabaseHandle $conn, int $paste_id, int $user_id) : string {
2021-07-16 09:53:34 -04:00
$query = $conn->prepare("SELECT 1 FROM pins WHERE user_id = ? AND paste_id = ?");
$query->execute([$user_id, $paste_id]);
2021-07-13 08:50:52 -04:00
if ($query->fetch()) {
return "<a href='#' id='favorite' class='icon tool-icon' data-fid='" . $paste_id . "'><i class='fas fa-star fa-lg has-text-grey' title='Favourite'></i></a>";
} else {
return "<a href='#' id='favorite' class='icon tool-icon' data-fid='" . $paste_id . "'><i class='far fa-star fa-lg has-text-grey' title='Favourite'></i></a>";
}
}
2021-07-10 19:18:17 +01:00
2021-07-13 00:22:46 +01:00
function getreports($conn, $count = 10) {
2021-07-10 16:21:01 -04:00
$query = $conn->prepare('SELECT * FROM user_reports LIMIT ?');
$query->execute([$count]);
return $query->fetchAll();
}
2021-08-22 22:05:26 -04:00
function tagsToHtml(string|array $tags) : string {
2021-08-17 13:13:04 -04:00
$output = "";
if (is_array($tags)) {
2021-08-22 22:05:26 -04:00
$tagsSplit = array_map(function ($tag) {
return $tag['name'];
}, $tags);
2021-08-17 13:13:04 -04:00
} else {
$tagsSplit = explode(",", $tags);
}
foreach ($tagsSplit as $tag) {
if (stripos($tag, 'nsfw') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-danger";
} elseif (stripos($tag, 'SAFE') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-success";
} elseif (str_contains($tag, '/')) {
$tagcolor = "tag is-primary";
} else {
$tagcolor = "tag is-info";
}
$output .= '<a href="/archive?q=' . urlencode($tag) . '"><span class="' . $tagcolor . '">' . pp_html_escape(ucfirst($tag)) . '</span></a>';
}
return $output;
}
function tagsToHtmlUser(string | array $tags, $profile_username) : string {
$output = "";
if (is_array($tags)) {
$tagsSplit = array_map(function($tag) { return $tag['name']; }, $tags);
} else {
$tagsSplit = explode(",", $tags);
}
foreach ($tagsSplit as $tag) {
if (stripos($tag, 'nsfw') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-danger";
} elseif (stripos($tag, 'SAFE') !== false) {
$tag = strtoupper($tag);
$tagcolor = "tag is-success";
} elseif (str_contains($tag, '/')) {
$tagcolor = "tag is-primary";
} else {
$tagcolor = "tag is-info";
}
$output .= '<a href="/user.php?user=' . $profile_username . '&q=' . urlencode($tag) . '"><span class="' . $tagcolor . '">' . pp_html_escape(ucfirst($tag)) . '</span></a>';
}
return $output;
}
2021-08-17 13:13:04 -04:00
function getevent($conn, $event_name, $count) {
2021-08-13 16:43:38 -04:00
$query = $conn->prepare("SELECT id, visible, title, date, now_time, views, member FROM pastes WHERE visible='1' AND tagsys LIKE '%?%'
ORDER BY RAND () LIMIT 0, ?");
$query->execute([$event_name, $count]);
return $query->fetchAll();
2021-07-10 19:18:17 +01:00
}
function linkify($value, $protocols = array('http', 'mail'), array $attributes = array()) {
// Link attributes
$attr = '';
foreach ($attributes as $key => $val) {
$attr .= ' ' . $key . '="' . htmlentities($val) . '"';
}
$links = array();
// Extract existing links and tags
$value = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) {
return '<' . array_push($links, $match[1]) . '>';
}, $value);
// Extract text links for each protocol
2021-08-22 22:05:26 -04:00
foreach ((array) $protocols as $protocol) {
2021-07-17 18:29:36 -04:00
$value = match ($protocol) {
'http', 'https' => preg_replace_callback('~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) {
if ($match[1]) $protocol = $match[1];
$link = $match[2] ?: $match[3];
return '<' . array_push($links, "<a $attr href=\"$protocol://$link\">$protocol://$link</a>") . '>';
}, $value),
default => preg_replace_callback('~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?<![\.,:])~i', function ($match) use ($protocol, &$links, $attr) {
return '<' . array_push($links, "<a $attr href=\"$protocol://{$match[1]}\">$protocol://{$match[1]}</a>") . '>';
}, $value),
};
2021-07-10 19:18:17 +01:00
}
// Insert all link
return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) {
return $links[$match[1] - 1];
}, $value);
}
2021-07-10 19:18:17 +01:00
2021-07-20 12:15:41 -04:00
function getUserRecom(DatabaseHandle $conn, int $user_id) : array {
$query = $conn->prepare(
"SELECT pastes.id AS id, users.username AS member, title, visible
FROM pastes
2021-07-16 09:53:34 -04:00
INNER JOIN users ON pastes.user_id = users.id
WHERE pastes.visible = '0' AND users.id = ?
ORDER BY id DESC
LIMIT 0, 5");
2021-07-16 09:53:34 -04:00
$query->execute([$user_id]);
2021-07-10 19:18:17 +01:00
return $query->fetchAll();
}
function formatBytes($size, $precision = 2) {
2021-07-10 19:18:17 +01:00
$base = log($size, 1024);
$suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
2021-07-10 19:18:17 +01:00
return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)];
2021-07-10 19:18:17 +01:00
}
function getRecentadmin($conn, $count = 5) {
2021-07-19 18:38:13 -04:00
$query = $conn->prepare(
'SELECT pastes.id AS id, pastes.ip AS ip, title, created_at, views, users.username AS member
FROM pastes
INNER JOIN users ON users.id = pastes.user_id
ORDER BY id DESC LIMIT 0, ?');
2021-07-10 16:21:01 -04:00
$query->execute([$count]);
return $query->fetchAll();
}
2021-07-20 12:15:41 -04:00
function getUserPastes(DatabaseHandle $conn, int $user_id) : array {
2021-08-05 08:18:32 -04:00
return $conn->query(
"SELECT id, title, visible, code, created_at, views FROM pastes
WHERE user_id = ?
ORDER by pastes.id DESC", [$user_id])->fetchAll();
2021-07-10 19:18:17 +01:00
}
function getTotalPastes(DatabaseHandle $conn, int $user_id) : int {
$query = $conn->prepare("SELECT COUNT(*) AS total_pastes
FROM pastes INNER JOIN users ON users.id = pastes.user_id
WHERE users.id = ?");
$query->execute([$user_id]);
return intval($query->fetch(PDO::FETCH_NUM)[0]);
2021-07-10 19:18:17 +01:00
}
function friendlyDateDifference(DateTime $lesser, DateTime $greater) : string {
$delta = $greater->diff($lesser, true);
$parts = [
'year' => $delta->y,
'month' => $delta->m,
'day' => $delta->d,
'hour' => $delta->h,
'min' => $delta->i,
'sec' => $delta->s
];
$friendly = '';
foreach ($parts as $part => $value) {
if ($value !== 0) {
$pluralizer = ($value === 1 ? '' : 's');
$friendly .= "${value} ${part}${pluralizer} ";
}
}
return trim($friendly) . ' ago';
}
2021-07-19 18:38:13 -04:00
function truncate(string $input, int $maxWords, int $maxChars) : string {
2021-07-10 19:18:17 +01:00
$words = preg_split('/\s+/', $input);
$words = array_slice($words, 0, $maxWords);
$words = array_reverse($words);
$chars = 0;
2021-07-10 19:18:17 +01:00
$truncated = array();
while (count($words) > 0) {
$fragment = trim(array_pop($words));
$chars += strlen($fragment);
if ($chars > $maxChars)
break;
$truncated[] = $fragment;
}
$result = implode(' ', $truncated);
2021-07-10 19:18:17 +01:00
return $result . ($input == $result ? '' : '[...]');
}
function doDownload($paste_id, $p_title, $p_member, $p_conntent, $p_code) {
2021-07-10 19:18:17 +01:00
$stats = false;
if ($p_code) {
// Figure out extensions.
2021-07-17 18:29:36 -04:00
$ext = match ($p_code) {
default => 'txt',
};
2021-07-10 19:18:17 +01:00
// Download
$p_title = stripslashes($p_title);
header('content-type: text/plain');
header('content-Disposition: attachment; filename="' . $paste_id . '_' . $p_title . '_' . $p_member . '.' . $ext . '"');
echo $p_conntent;
$stats = true;
} else {
// 404
header('HTTP/1.1 404 Not Found');
}
return $stats;
}
2021-08-22 21:45:26 -04:00
function embedView($paste_id, $p_title, $content, $p_code, $title, $baseurl, $lang) {
2021-07-10 19:18:17 +01:00
$stats = false;
2021-08-22 21:45:26 -04:00
if ($content) {
2021-07-10 19:18:17 +01:00
// Build the output
$output = "<div class='paste_embed_conntainer'>";
$output .= "<style>"; // Add our own styles
$output .= "
2021-07-10 19:18:17 +01:00
.paste_embed_conntainer {
font-size: 12px;
color: #333;
text-align: left;
margin-bottom: 1em;
border: 1px solid #ddd;
background-color: #f7f7f7;
border-radius: 3px;
}
.paste_embed_conntainer a {
font-weight: bold;
color: #666;
text-decoration: none;
border: 0;
}
.paste_embed_conntainer ol {
color: white;
background-color: #f7f7f7;
border-right: 1px solid #ccc;
margin: 0;
}
.paste_embed_footer {
font-size:14px;
padding: 10px;
overflow: hidden;
color: #767676;
background-color: #f7f7f7;
border-radius: 0 0 2px 2px;
border-top: 1px solid #ccc;
}
.de1, .de2 {
-moz-user-select: text;
-khtml-user-select: text;
-webkit-user-select: text;
-ms-user-select: text;
user-select: text;
padding: 0 8px;
color: #000;
border-left: 1px solid #ddd;
background: #ffffff;
line-height:20px;
}";
$output .= "</style>";
2021-08-22 21:45:26 -04:00
$output .= $content; // Paste content
$output .= "<div class='paste_embed_footer'>";
2021-08-26 05:35:21 -04:00
$output .= "<a href='https://ponepaste.org/$paste_id'>$p_title</a> Hosted by <a href='https://ponepaste.org'>$title</a> | <a href='https://ponepaste.org/raw/$paste_id'>view raw</a>";
$output .= "</div>";
$output .= "</div>";
2021-07-10 19:18:17 +01:00
// Display embed conntent using json_encode since that escapes
// characters well enough to satisfy javascript. http://stackoverflow.com/a/169035
2021-08-22 21:45:26 -04:00
header('Content-Type: text/javascript; charset=utf-8;');
echo 'document.write(' . json_encode($output) . ')';
2021-07-10 19:18:17 +01:00
$stats = true;
} else {
// 404
header('HTTP/1.1 404 Not Found');
2021-07-10 19:18:17 +01:00
}
return $stats;
}
function addToSitemap($paste_id, $priority, $changefreq, $mod_rewrite) {
$c_date = date('Y-m-d');
2021-07-10 17:54:43 -04:00
$site_data = file_get_contents("sitemap.xml");
2021-07-10 19:18:17 +01:00
$site_data = str_replace("</urlset>", "", $site_data);
// which protocol are we on
$protocol = paste_protocol();
2021-07-10 19:18:17 +01:00
if (PP_MOD_REWRITE) {
2021-07-10 19:18:17 +01:00
$server_name = $protocol . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/" . $paste_id;
} else {
$server_name = $protocol . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/paste.php?id=" . $paste_id;
}
$c_sitemap =
' <url>
2021-07-10 19:18:17 +01:00
<loc>' . $server_name . '</loc>
<priority>' . $priority . '</priority>
<changefreq>' . $changefreq . '</changefreq>
<lastmod>' . $c_date . '</lastmod>
</url>
</urlset>';
$full_map = $site_data . $c_sitemap;
2021-07-10 17:54:43 -04:00
file_put_contents("sitemap.xml", $full_map);
2021-07-10 19:18:17 +01:00
}
2021-07-12 10:44:39 -04:00
function paste_protocol() : string {
2021-08-22 21:45:26 -04:00
return !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
2021-07-10 19:18:17 +01:00
}