name; 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 .= '' . pp_html_escape(ucfirst($tag)) . ''; } return $output; } function tagsToHtmlUser(string | array | Collection $tags, $profile_username) : string { $output = ""; if (is_a($tags, Collection::class)) { $tags = $tags->toArray(); } if (is_array($tags)) { $tagsSplit = array_map(function($tag) { return $tag['name']; }, $tags); } else { $tagsSplit = explode(",", $tags); } if (count($tagsSplit) === 0) { return 'No 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 .= '' . pp_html_escape(ucfirst($tag)) . ''; } return $output; } 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('~(.*?|<.*?>)~i', function ($match) use (&$links) { return '<' . array_push($links, $match[1]) . '>'; }, $value); // Extract text links for each protocol foreach ((array) $protocols as $protocol) { $value = match ($protocol) { 'http', 'https' => preg_replace_callback('~(?:(https?)://([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?$protocol://$link") . '>'; }, $value), default => preg_replace_callback('~' . preg_quote($protocol, '~') . '://([^\s<]+?)(?$protocol://{$match[1]}") . '>'; }, $value), }; } // Insert all link return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) { return $links[$match[1] - 1]; }, $value); } function formatBytes($size, $precision = 2) { $base = log($size, 1024); $suffixes = ['B', 'KB', 'MB', 'GB', 'TB']; if ($size === 0) { return '0 B'; } return round(pow(1024, $base - floor($base)), $precision) . ' ' . $suffixes[floor($base)]; } 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'; } function truncate(string $input, int $maxWords, int $maxChars) : string { $words = preg_split('/\s+/', $input); $words = array_slice($words, 0, $maxWords); $words = array_reverse($words); $chars = 0; $truncated = array(); while (count($words) > 0) { $fragment = trim(array_pop($words)); $chars += strlen($fragment); if ($chars > $maxChars) break; $truncated[] = $fragment; } $result = implode(' ', $truncated); return $result . ($input == $result ? '' : '[...]'); } function embedView($paste_id, $p_title, $content, $title) { $stats = false; if ($content) { // Build the output $output = "
"; $output .= ""; $output .= $content; // Paste content $output .= ""; $output .= "
"; // Display embed conntent using json_encode since that escapes // characters well enough to satisfy javascript. http://stackoverflow.com/a/169035 header('Content-Type: text/javascript; charset=utf-8;'); echo 'document.write(' . json_encode($output) . ')'; $stats = true; } else { // 404 header('HTTP/1.1 404 Not Found'); } return $stats; } function addToSitemap(\PonePaste\Models\Paste $paste, $priority, $changefreq) { $c_date = date('Y-m-d'); $site_data = file_get_contents("sitemap.xml"); $site_data = str_replace("", "", $site_data); $c_sitemap = ' ' . urlForPaste($paste) . ' ' . $priority . ' ' . $changefreq . ' ' . $c_date . ' '; $full_map = $site_data . $c_sitemap; file_put_contents("sitemap.xml", $full_map); } function paste_protocol() : string { return !empty($_SERVER['HTTPS']) ? 'https://' : 'http://'; }