|
|
@@ -1,7 +1,12 @@
|
|
|
<?php
|
|
|
+
|
|
|
class Util {
|
|
|
|
|
|
- public static function getGenderText($gender) {
|
|
|
+ /**
|
|
|
+ * @param string $gender
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getGenderText(string $gender): string {
|
|
|
switch($gender) {
|
|
|
case "M": return MALE;
|
|
|
case "F": return FEMALE;
|
|
|
@@ -9,7 +14,11 @@ class Util {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static function getBooleanText($boolean) {
|
|
|
+ /**
|
|
|
+ * @param string $boolean
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getBooleanText(string $boolean): string {
|
|
|
switch($boolean) {
|
|
|
case "Y": return YES;
|
|
|
case "N": return NO;
|
|
|
@@ -17,12 +26,23 @@ class Util {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static function convertUTF8($string, $target_encoding = "CP1252") {
|
|
|
+ /**
|
|
|
+ * @param string $string
|
|
|
+ * @param string $target_encoding
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function convertUTF8(string $string, string $target_encoding = "CP1252"): string {
|
|
|
return mb_convert_encoding($string, $target_encoding, "UTF-8");
|
|
|
}
|
|
|
|
|
|
- public static function getPaginationObject($array, $page_size = 25) {
|
|
|
- $item_count = count($array);
|
|
|
+ /**
|
|
|
+ * @param array $items
|
|
|
+ * @param int $page_size
|
|
|
+ * @return object
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static function getPaginationObject(array $items, int $page_size = 25) {
|
|
|
+ $item_count = count($items);
|
|
|
|
|
|
$page = http::value("page");
|
|
|
if($page < 1) $page = 1;
|
|
|
@@ -56,12 +76,22 @@ class Util {
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- public static function getItemDirectory($folder, $id) {
|
|
|
+ /**
|
|
|
+ * @param string $folder
|
|
|
+ * @param int $id
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getItemDirectory(string $folder, int $id): string {
|
|
|
$shard = str_pad($id % 1000, 3, "0", STR_PAD_LEFT);
|
|
|
return ROOT_DIR . "/files/" . $folder . "/" . $shard . "/" . $id;
|
|
|
}
|
|
|
|
|
|
- public static function createItemDirectory($folder, $id) {
|
|
|
+ /**
|
|
|
+ * @param string $folder
|
|
|
+ * @param int $id
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function createItemDirectory(string $folder, int $id): string {
|
|
|
|
|
|
$directory = Util::getItemDirectory($folder, $id);
|
|
|
|
|
|
@@ -72,19 +102,32 @@ class Util {
|
|
|
return $directory;
|
|
|
}
|
|
|
|
|
|
- public static function slug($string) {
|
|
|
+ /**
|
|
|
+ * @param string $string
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function slug(string $string): 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 = preg_replace("/-+/", "-", $slug); // Replace consequential dashes with dash
|
|
|
$slug = trim($slug, "-");
|
|
|
return $slug;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param string $string
|
|
|
+ * @param int $id
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
public static function slugWithId(string $string, int $id): string {
|
|
|
return Util::slug($string) . "-" . $id;
|
|
|
}
|
|
|
|
|
|
- public static function getDisplayFileSize($size) {
|
|
|
+ /**
|
|
|
+ * @param int $size
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getDisplayFileSize(int $size): string {
|
|
|
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";
|
|
|
@@ -92,7 +135,12 @@ class Util {
|
|
|
return number_format($size / pow(2, 40), 2) . " TB";
|
|
|
}
|
|
|
|
|
|
- public static function timeDifference($unix_time, $options = []) {
|
|
|
+ /**
|
|
|
+ * @param int $unix_time
|
|
|
+ * @param array $options
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function timeDifference(int $unix_time, $options = []): string {
|
|
|
|
|
|
$plain = $options["plain"] ?? false;
|
|
|
|
|
|
@@ -145,7 +193,11 @@ class Util {
|
|
|
|
|
|
}
|
|
|
|
|
|
- public static function getTimeZones() {
|
|
|
+ /**
|
|
|
+ * @return array
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static function getTimeZones(): array {
|
|
|
|
|
|
// Taken and modified from: https://stackoverflow.com/a/17355238
|
|
|
|
|
|
@@ -206,25 +258,46 @@ class Util {
|
|
|
return $time_zone_list;
|
|
|
}
|
|
|
|
|
|
- public static function getDate($time = false) {
|
|
|
+ /**
|
|
|
+ * @param bool $time
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getDate($time = false): string {
|
|
|
if(!$time) $time = time();
|
|
|
return gmdate("Y-m-d", $time);
|
|
|
}
|
|
|
|
|
|
- public static function getUTCDateTime($time = false) {
|
|
|
+ /**
|
|
|
+ * @param bool $time
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getUTCDateTime($time = false): string {
|
|
|
return Util::getDateTime($time);
|
|
|
}
|
|
|
|
|
|
- public static function getDateTime($time = false) {
|
|
|
+ /**
|
|
|
+ * @param bool $time
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getDateTime($time = false): string {
|
|
|
if(!$time) $time = time();
|
|
|
return gmdate("Y-m-d H:i:s", $time);
|
|
|
}
|
|
|
|
|
|
- public static function getUnixTimeFromUTC($dts) {
|
|
|
+ /**
|
|
|
+ * @param string $dts
|
|
|
+ * @return false|int
|
|
|
+ */
|
|
|
+ public static function getUnixTimeFromUTC(string $dts) {
|
|
|
return strtotime($dts . " UTC");
|
|
|
}
|
|
|
|
|
|
- public static function getDurationFromDTS($dts1, $dts2) {
|
|
|
+ /**
|
|
|
+ * @param string $dts1
|
|
|
+ * @param string $dts2
|
|
|
+ * @return float|int
|
|
|
+ */
|
|
|
+ public static function getDurationFromDTS(string $dts1, string $dts2) {
|
|
|
$tz = date_default_timezone_get();
|
|
|
date_default_timezone_set("UTC");
|
|
|
|
|
|
@@ -236,7 +309,11 @@ class Util {
|
|
|
return abs($t2 - $t1);
|
|
|
}
|
|
|
|
|
|
- public static function getDisplayDateTime($time = false) {
|
|
|
+ /**
|
|
|
+ * @param int|bool $time
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getDisplayDateTime($time = false): string {
|
|
|
if($time === false) $time = time();
|
|
|
|
|
|
$current_year = date("Y");
|
|
|
@@ -247,7 +324,11 @@ class Util {
|
|
|
return date($format, $time);
|
|
|
}
|
|
|
|
|
|
- public static function getDisplayDate($time = false) {
|
|
|
+ /**
|
|
|
+ * @param int|bool $time
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getDisplayDate($time = false): string {
|
|
|
if($time === false) $time = time();
|
|
|
|
|
|
$current_year = date("Y");
|
|
|
@@ -258,17 +339,27 @@ class Util {
|
|
|
return date($format, $time);
|
|
|
}
|
|
|
|
|
|
- public static function getDisplayDateTimeFromUTC($dts) {
|
|
|
+ /**
|
|
|
+ * @param string $dts
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function getDisplayDateTimeFromUTC(string $dts): string {
|
|
|
$unix = Util::getUnixTimeFromUTC($dts);
|
|
|
return Util::getDisplayDateTime($unix);
|
|
|
}
|
|
|
|
|
|
- public static function sortObjectArray($array, $field, $compare_function = "") {
|
|
|
+ /**
|
|
|
+ * @param array $array
|
|
|
+ * @param string $field
|
|
|
+ * @param string $compare_function
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function sortObjectArray(array $array, string $field, string $compare_function = ""): array {
|
|
|
|
|
|
usort($array, function($a, $b) use ($compare_function, $field) {
|
|
|
|
|
|
switch($compare_function) {
|
|
|
- case "":
|
|
|
+ default:
|
|
|
if($a->$field == $b->$field) return 0;
|
|
|
return ($a->$field < $b->$field) ? -1 : 1;
|
|
|
case "strnatcasecmp":
|
|
|
@@ -279,7 +370,11 @@ class Util {
|
|
|
return $array;
|
|
|
}
|
|
|
|
|
|
- public static function arrayFlipByID($input = []) {
|
|
|
+ /**
|
|
|
+ * @param array $input
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function arrayFlipByID(array $input = []): array {
|
|
|
$output = [];
|
|
|
|
|
|
foreach($input as $object) {
|
|
|
@@ -289,141 +384,4 @@ class Util {
|
|
|
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;
|
|
|
- }
|
|
|
}
|