| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- class Util {
- public static function getGenderText($gender) {
- switch($gender) {
- case "M": return MALE;
- case "F": return FEMALE;
- default: return "";
- }
- }
- public static function getBooleanText($boolean) {
- switch($boolean) {
- case "Y": return YES;
- case "N": return NO;
- default: return "";
- }
- }
- public static function convertUTF8($string, $target_encoding = "CP1252") {
- return mb_convert_encoding($string, $target_encoding, "UTF-8");
- }
- public static function getPaginationObject($array, $page_size = 25) {
- $item_count = count($array);
- $page = http::value("page");
- if($page < 1) $page = 1;
- $offset = ($page - 1) * $page_size;
- $page_count = ceil($item_count / $page_size);
- if($page_count < 1) $page_count = 1;
- $first_item = ($offset + 1);
- $last_item = $offset + $page_size;
- if($last_item > $item_count) {
- $last_item = $item_count;
- }
- if($page_count == 1) {
- $range_text = "Showing $item_count items";
- }
- else {
- $range_text = "Showing $first_item - $last_item of $item_count items";
- }
- return (object) [
- "page" => $page,
- "pageCount" => $page_count,
- "pageSize" => $page_size,
- "offset" => $offset,
- "firstItem" => $first_item,
- "lastItem" => $last_item,
- "rangeText" => $range_text
- ];
- }
- public static function getItemDirectory($folder, $id) {
- $shard = str_pad($id % 1000, 3, "0", STR_PAD_LEFT);
- return ROOT_DIR . "/files/" . $folder . "/" . $shard . "/" . $id;
- }
- public static function createItemDirectory($folder, $id) {
- $directory = Util::getItemDirectory($folder, $id);
- if(!file_exists($directory)) {
- mkdir($directory, 0777, true);
- }
- return $directory;
- }
- public static function slug($string) {
- $slug = strtolower($string);
- $slug = preg_replace("/[^0-9a-z]/i", "-", $slug); // Replace anything not 0-9 a-z with dash
- $slug = preg_replace("/\-+/", "-", $slug); // Replace consequential dashes with dash
- $slug = trim($slug, "-");
- return $slug;
- }
- public static function getDisplayFileSize($size) {
- if($size < pow(2, 10)) return number_format($size) . " bytes";
- if($size < pow(2, 20)) return number_format($size / pow(2, 10), 2) . " KB";
- if($size < pow(2, 30)) return number_format($size / pow(2, 20), 2) . " MB";
- if($size < pow(2, 40)) return number_format($size / pow(2, 30), 2) . " GB";
- return number_format($size / pow(2, 40), 2) . " TB";
- }
- public static function timeDifference($unix_time, $options = []) {
- $plain = $options["plain"] ?? false;
- $now = time();
- $difference = $now - $unix_time;
- $tense = ($difference >= 0) ? "ago" : "to go";
- $difference = abs($difference);
- if($difference < 60) {
- $value = $difference;
- $class = "text-danger";
- $unit = "second";
- }
- else if($difference < 3600) {
- $value = floor($difference / 60);
- $class = "text-danger";
- $unit = "minute";
- }
- else if($difference < 86400) {
- $value = floor($difference / 3600);
- $class = "text-warning";
- $unit = "hour";
- }
- else if($difference < 2592000) {
- $value = floor($difference / 86400);
- $class = "";
- $unit = "day";
- }
- else if($difference < 31536000) {
- $value = floor($difference / 2592000);
- $class = "text-muted";
- $unit = "month";
- }
- else {
- $value = floor($difference / 31536000);
- $class = "text-muted";
- $unit = "year";
- }
- $text = "$value $unit" . ($value != 1 ? "s" : "") . " $tense";
- if($plain) {
- return "$text";
- }
- else {
- return "<span class=\"$class\">$text</span>";
- }
- }
- public static function getTimeZones() {
- // Taken and modified from: https://stackoverflow.com/a/17355238
- static $regions = [
- DateTimeZone::AFRICA,
- DateTimeZone::AMERICA,
- DateTimeZone::ANTARCTICA,
- DateTimeZone::ASIA,
- DateTimeZone::ATLANTIC,
- DateTimeZone::AUSTRALIA,
- DateTimeZone::EUROPE,
- DateTimeZone::INDIAN,
- DateTimeZone::PACIFIC,
- ];
- $identifiers = [];
- foreach($regions as $region) {
- $identifiers = array_merge($identifiers, DateTimeZone::listIdentifiers($region));
- }
- $time_zone_offsets = [];
- foreach($identifiers as $identifier) {
- $tz = new DateTimeZone($identifier);
- $time_zone_offsets[] = (object) [
- "identifier" => $identifier,
- "offset" => $tz->getOffset(new DateTime),
- ];
- }
- uasort($time_zone_offsets, function($a, $b) {
- if($a->offset > $b->offset) return 1;
- else if($a->offset < $b->offset) return -1;
- else return strcasecmp($a->identifier, $b->identifier);
- });
- $sr = [
- "/" => ", ",
- "St" => "St.",
- "_" => " ",
- ];
- $time_zone_list = [];
- foreach($time_zone_offsets as $object) {
- $identifier = $object->identifier;
- $offset = $object->offset;
- $display_identifier = str_replace(array_keys($sr), array_values($sr), $identifier);
- $offset_prefix = $offset < 0 ? "-" : "+";
- $offset_formatted = gmdate("H:i", abs($offset));
- $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
- $time_zone_list[$identifier] = "${pretty_offset} - $display_identifier";
- }
- return $time_zone_list;
- }
- public static function getDate($time = false) {
- if(!$time) $time = time();
- return gmdate("Y-m-d", $time);
- }
- public static function getUTCDateTime($time = false) {
- return Util::getDateTime($time);
- }
- public static function getDateTime($time = false) {
- if(!$time) $time = time();
- return gmdate("Y-m-d H:i:s", $time);
- }
- public static function getUnixTimeFromUTC($dts) {
- return strtotime($dts . " UTC");
- }
- public static function getDurationFromDTS($dts1, $dts2) {
- $tz = date_default_timezone_get();
- date_default_timezone_set("UTC");
- $t1 = strtotime($dts1);
- $t2 = strtotime($dts2);
- date_default_timezone_set($tz);
- return abs($t2 - $t1);
- }
- public static function getDisplayDateTime($time = false) {
- if($time === false) $time = time();
- $current_year = date("Y");
- $year = date("Y", $time);
- $format = ($year == $current_year) ? "M j, H:i" : "M j Y, H:i";
- return date($format, $time);
- }
- public static function getDisplayDate($time = false) {
- if($time === false) $time = time();
- $current_year = date("Y");
- $year = date("Y", $time);
- $format = ($year == $current_year) ? "M j" : "M j Y";
- return date($format, $time);
- }
- public static function getDisplayDateTimeFromUTC($dts) {
- $unix = Util::getUnixTimeFromUTC($dts);
- return Util::getDisplayDateTime($unix);
- }
- public static function sortObjectArray($array, $field, $compare_function = "") {
- usort($array, function($a, $b) use ($compare_function, $field) {
- switch($compare_function) {
- case "":
- if($a->$field == $b->$field) return 0;
- return ($a->$field < $b->$field) ? -1 : 1;
- case "strnatcasecmp":
- return strnatcasecmp($a->$field, $b->$field);
- }
- });
- return $array;
- }
- public static function arrayFlipByID($input = []) {
- $output = [];
- foreach($input as $object) {
- $output[$object->getID()] = $object;
- }
- return $output;
- }
- public static function formatText($string, $options = []) {
- $bb_code = $options["bb_code"] ?? true;
- $emoticon = $options["emoticon"] ?? true;
- $string = htmlspecialchars($string);
- $string = nl2br($string);
- /*
- if($emoticon && $emoticonresult) {
- db::data_seek($emoticonresult, 0);
- while($myrow = db::fetch_array($emoticonresult)) {
- $replacetext = $myrow["replacetext"];
- $url = $myrow["url"];
- $description = $myrow["description"];
- $string = str_replace("$replacetext", "<img src=\"$dirPrefix$url\" alt=\"$description\" />", $string);
- }
- }
- */
- if($emoticon) {
- $string = Emoticon::apply($string);
- }
- if($bb_code) {
- $string = Util::BBCode($string);
- }
- $pattern = "[0-9a-zA-Z\&\/\-\#\%\.\?\=\;\_\:\,\+\(\)\!]";
- $string = preg_replace("/^http(s?):\/\/($pattern*)/", "<a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a> ", $string); // Case for beginning
- $string = preg_replace("/\shttp(s?):\/\/($pattern*)/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for middle
- $string = preg_replace("/http(s?):\/\/($pattern*)$/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for end
- return $string;
- }
- public static function BBCode($string) {
- preg_match_all("/\[quote/i", $string, $matches);
- $quote_begin_count = count($matches["0"]);
- preg_match_all("/\[\/quote\]/i", $string, $matches);
- $quote_end_count = count($matches["0"]);
- $quotes = min($quote_begin_count, $quote_end_count);
- $valid_quotes = ($quote_begin_count == $quote_end_count);
- if(!$valid_quotes) {
- }
- $search = [
- "/\[b\](.*?)\[\/b\]/is",
- "/\[i\](.*?)\[\/i\]/is",
- "/\[u\](.*?)\[\/u\]/is",
- "/\[s\](.*?)\[\/s\]/is",
- "/\[spoiler\](.*?)\[\/spoiler\]/is",
- "/\[img\=(.*?)\]/is",
- "/\[img\](.*?)\[\/img\]/is",
- "/\[img width\=(.*?)\](.*?)\[\/img\]/is",
- "/\[url\](.*?)\[\/url\]/is",
- "/\[url\=(.*?)\](.*?)\[\/url\]/is",
- "/\[color\=(.*?)\](.*?)\[\/color\]/is",
- "/\[colour\=(.*?)\](.*?)\[\/colour\]/is",
- "/\[size\=(.*?)\](.*?)\[\/size\]/is",
- "/\[font\=(.*?)\](.*?)\[\/font\]/is",
- "/\[center\](.*?)\[\/center\]<br \/>/is",
- "/\[center\](.*?)\[\/center\]/is",
- "/\[code\](.*?)\[\/code\]/is",
- "/\[background\=(.*?)\](.*?)\[\/background\]/is",
- "/\[youtube\][^\s]+v\=([A-Za-z0-9\-_]+)[^\s]*\[\/youtube\]/is", // YouTube URL
- "/\[youtube\]([A-Za-z0-9\-_]+?)\[\/youtube\]/is", // YouTube ID
- "/\[twitch\]([A-Za-z0-9\-_]+?)\[\/twitch\]/is", // Twitch ID
- "/\[hrd?\]\<br \/\>/is",
- "/\[nico\](.*?)\[\/nico\]/is",
- "/\[leftfloat\](.*?)\[\/leftfloat\]/is",
- "/\[rightfloat\](.*?)\[\/rightfloat\]/is",
- "/\[leftfloat width\=(.*?)\](.*?)\[\/leftfloat\]/is",
- "/\[clear\]/is",
- ];
- $replace = [
- '<strong>$1</strong>',
- '<em>$1</em>',
- '<u>$1</u>',
- '<del>$1</del>',
- '<span class="spoiler">$1</span>',
- '<img class="img-fluid" src="$1" alt="$1" />',
- '<img class="img-fluid" src="$1" alt="$1" />',
- '<img class="img-fluid" width="$1" src="$2" alt="$2" />',
- '<a href="$1" target="_blank">$1</a>',
- '<a href="$1" target="_blank">$2</a>',
- '<span style="color: $1;">$2</span>',
- '<span style="color: $1;">$2</span>',
- '<span style="font-size: $1'.'px;">$2</span>',
- '<span style="font-family: $1;">$2</span>',
- '<div class="text-center">$1</div>',
- '<div class="text-center">$1</div>',
- '<div class="code">$1</div>',
- '<span style="background: $1;">$2</span>',
- '<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
- '<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
- '<object type="application/x-shockwave-flash" height="378" width="620" id="live_embed_player_flash" data="https://www.twitch.tv/widgets/live_embed_player.swf?channel=$1" bgcolor="#000000">
- <param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://www.twitch.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="hostname=www.twitch.tv&channel=$1&auto_play=false&start_volume=25" /></object>',
- '<hr />',
- '<script type="text/javascript" src="https://ext.nicovideo.jp/thumb_watch/$1"></script>',
- '<div class="float-left">$1</div>',
- '<div class="float-right">$1</div>',
- '<div class="float-left" style="width: $1%;">$2</div>',
- '<div class="float-none"></div>',
- ];
- $string = preg_replace($search, $replace, $string);
- if($valid_quotes) {
- $search = [
- "/\[quote\]/is",
- "/\[quote\=(.*?)\]([^\]])/is",
- "/\[quote\=(.*?)\]/is",
- "/\[\/quote\]<br \/>/is",
- "/\[\/quote\]/is",
- ];
- $replace = [
- '<div class="card quote mb-3"><div class="card-header">Quote</div><div class="card-body">',
- '<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">$2',
- '<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">',
- '</div></div>',
- '</div></div>',
- ];
- $string = preg_replace($search, $replace, $string, $quotes);
- }
- return $string;
- }
- }
|