util.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. class Util {
  3. public static function getGenderText($gender) {
  4. switch($gender) {
  5. case "M": return MALE;
  6. case "F": return FEMALE;
  7. default: return "";
  8. }
  9. }
  10. public static function getBooleanText($boolean) {
  11. switch($boolean) {
  12. case "Y": return YES;
  13. case "N": return NO;
  14. default: return "";
  15. }
  16. }
  17. public static function convertUTF8($string, $target_encoding = "CP1252") {
  18. return mb_convert_encoding($string, $target_encoding, "UTF-8");
  19. }
  20. public static function getPaginationObject($array, $page_size = 25) {
  21. $item_count = count($array);
  22. $page = http::value("page");
  23. if($page < 1) $page = 1;
  24. $offset = ($page - 1) * $page_size;
  25. $page_count = ceil($item_count / $page_size);
  26. if($page_count < 1) $page_count = 1;
  27. $first_item = ($offset + 1);
  28. $last_item = $offset + $page_size;
  29. if($last_item > $item_count) {
  30. $last_item = $item_count;
  31. }
  32. if($page_count == 1) {
  33. $range_text = "Showing $item_count items";
  34. }
  35. else {
  36. $range_text = "Showing $first_item - $last_item of $item_count items";
  37. }
  38. return (object) [
  39. "page" => $page,
  40. "pageCount" => $page_count,
  41. "pageSize" => $page_size,
  42. "offset" => $offset,
  43. "firstItem" => $first_item,
  44. "lastItem" => $last_item,
  45. "rangeText" => $range_text
  46. ];
  47. }
  48. public static function getItemDirectory($folder, $id) {
  49. $shard = str_pad($id % 1000, 3, "0", STR_PAD_LEFT);
  50. return ROOT_DIR . "/files/" . $folder . "/" . $shard . "/" . $id;
  51. }
  52. public static function createItemDirectory($folder, $id) {
  53. $directory = Util::getItemDirectory($folder, $id);
  54. if(!file_exists($directory)) {
  55. mkdir($directory, 0777, true);
  56. }
  57. return $directory;
  58. }
  59. public static function slug($string) {
  60. $slug = strtolower($string);
  61. $slug = preg_replace("/[^0-9a-z]/i", "-", $slug); // Replace anything not 0-9 a-z with dash
  62. $slug = preg_replace("/\-+/", "-", $slug); // Replace consequential dashes with dash
  63. $slug = trim($slug, "-");
  64. return $slug;
  65. }
  66. public static function getDisplayFileSize($size) {
  67. if($size < pow(2, 10)) return number_format($size) . " bytes";
  68. if($size < pow(2, 20)) return number_format($size / pow(2, 10), 2) . " KB";
  69. if($size < pow(2, 30)) return number_format($size / pow(2, 20), 2) . " MB";
  70. if($size < pow(2, 40)) return number_format($size / pow(2, 30), 2) . " GB";
  71. return number_format($size / pow(2, 40), 2) . " TB";
  72. }
  73. public static function timeDifference($unix_time, $options = []) {
  74. $plain = $options["plain"] ?? false;
  75. $now = time();
  76. $difference = $now - $unix_time;
  77. $tense = ($difference >= 0) ? "ago" : "to go";
  78. $difference = abs($difference);
  79. if($difference < 60) {
  80. $value = $difference;
  81. $class = "text-danger";
  82. $unit = "second";
  83. }
  84. else if($difference < 3600) {
  85. $value = floor($difference / 60);
  86. $class = "text-danger";
  87. $unit = "minute";
  88. }
  89. else if($difference < 86400) {
  90. $value = floor($difference / 3600);
  91. $class = "text-warning";
  92. $unit = "hour";
  93. }
  94. else if($difference < 2592000) {
  95. $value = floor($difference / 86400);
  96. $class = "";
  97. $unit = "day";
  98. }
  99. else if($difference < 31536000) {
  100. $value = floor($difference / 2592000);
  101. $class = "text-muted";
  102. $unit = "month";
  103. }
  104. else {
  105. $value = floor($difference / 31536000);
  106. $class = "text-muted";
  107. $unit = "year";
  108. }
  109. $text = "$value $unit" . ($value != 1 ? "s" : "") . " $tense";
  110. if($plain) {
  111. return "$text";
  112. }
  113. else {
  114. return "<span class=\"$class\">$text</span>";
  115. }
  116. }
  117. public static function getTimeZones() {
  118. // Taken and modified from: https://stackoverflow.com/a/17355238
  119. static $regions = [
  120. DateTimeZone::AFRICA,
  121. DateTimeZone::AMERICA,
  122. DateTimeZone::ANTARCTICA,
  123. DateTimeZone::ASIA,
  124. DateTimeZone::ATLANTIC,
  125. DateTimeZone::AUSTRALIA,
  126. DateTimeZone::EUROPE,
  127. DateTimeZone::INDIAN,
  128. DateTimeZone::PACIFIC,
  129. ];
  130. $identifiers = [];
  131. foreach($regions as $region) {
  132. $identifiers = array_merge($identifiers, DateTimeZone::listIdentifiers($region));
  133. }
  134. $time_zone_offsets = [];
  135. foreach($identifiers as $identifier) {
  136. $tz = new DateTimeZone($identifier);
  137. $time_zone_offsets[] = (object) [
  138. "identifier" => $identifier,
  139. "offset" => $tz->getOffset(new DateTime),
  140. ];
  141. }
  142. uasort($time_zone_offsets, function($a, $b) {
  143. if($a->offset > $b->offset) return 1;
  144. else if($a->offset < $b->offset) return -1;
  145. else return strcasecmp($a->identifier, $b->identifier);
  146. });
  147. $sr = [
  148. "/" => ", ",
  149. "St" => "St.",
  150. "_" => " ",
  151. ];
  152. $time_zone_list = [];
  153. foreach($time_zone_offsets as $object) {
  154. $identifier = $object->identifier;
  155. $offset = $object->offset;
  156. $display_identifier = str_replace(array_keys($sr), array_values($sr), $identifier);
  157. $offset_prefix = $offset < 0 ? "-" : "+";
  158. $offset_formatted = gmdate("H:i", abs($offset));
  159. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  160. $time_zone_list[$identifier] = "${pretty_offset} - $display_identifier";
  161. }
  162. return $time_zone_list;
  163. }
  164. public static function getDate($time = false) {
  165. if(!$time) $time = time();
  166. return gmdate("Y-m-d", $time);
  167. }
  168. public static function getDateTime($time = false) {
  169. if(!$time) $time = time();
  170. return gmdate("Y-m-d H:i:s", $time);
  171. }
  172. public static function getUnixTimeFromUTC($dts) {
  173. return strtotime($dts . " UTC");
  174. }
  175. public static function getDurationFromDTS($dts1, $dts2) {
  176. $tz = date_default_timezone_get();
  177. date_default_timezone_set("UTC");
  178. $t1 = strtotime($dts1);
  179. $t2 = strtotime($dts2);
  180. date_default_timezone_set($tz);
  181. return abs($t2 - $t1);
  182. }
  183. public static function getDisplayDateTime($time = false) {
  184. if($time === false) $time = time();
  185. $current_year = date("Y");
  186. $year = date("Y", $time);
  187. $format = ($year == $current_year) ? "M j, H:i" : "M j Y, H:i";
  188. return date($format, $time);
  189. }
  190. public static function getDisplayDate($time = false) {
  191. if($time === false) $time = time();
  192. $current_year = date("Y");
  193. $year = date("Y", $time);
  194. $format = ($year == $current_year) ? "M j" : "M j Y";
  195. return date($format, $time);
  196. }
  197. public static function getDisplayDateTimeFromUTC($dts) {
  198. $unix = Util::getUnixTimeFromUTC($dts);
  199. return Util::getDisplayDateTime($unix);
  200. }
  201. public static function sortObjectArray($array, $field, $compare_function = "") {
  202. usort($array, function($a, $b) use ($compare_function, $field) {
  203. switch($compare_function) {
  204. case "":
  205. if($a->$field == $b->$field) return 0;
  206. return ($a->$field < $b->$field) ? -1 : 1;
  207. case "strnatcasecmp":
  208. return strnatcasecmp($a->$field, $b->$field);
  209. }
  210. });
  211. return $array;
  212. }
  213. public static function arrayFlipByID($input = []) {
  214. $output = [];
  215. foreach($input as $object) {
  216. $output[$object->getID()] = $object;
  217. }
  218. return $output;
  219. }
  220. public static function formatText($string, $options = []) {
  221. $bb_code = $options["bb_code"] ?? true;
  222. $emoticon = $options["emoticon"] ?? true;
  223. $string = htmlspecialchars($string);
  224. $string = nl2br($string);
  225. /*
  226. if($emoticon && $emoticonresult) {
  227. db::data_seek($emoticonresult, 0);
  228. while($myrow = db::fetch_array($emoticonresult)) {
  229. $replacetext = $myrow["replacetext"];
  230. $url = $myrow["url"];
  231. $description = $myrow["description"];
  232. $string = str_replace("$replacetext", "<img src=\"$dirPrefix$url\" alt=\"$description\" />", $string);
  233. }
  234. }
  235. */
  236. if($emoticon) {
  237. $string = Emoticon::apply($string);
  238. }
  239. if($bb_code) {
  240. $string = Util::BBCode($string);
  241. }
  242. $pattern = "[0-9a-zA-Z\&\/\-\#\%\.\?\=\;\_\:\,\+\(\)\!]";
  243. $string = preg_replace("/^http(s?):\/\/($pattern*)/", "<a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a> ", $string); // Case for beginning
  244. $string = preg_replace("/\shttp(s?):\/\/($pattern*)/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for middle
  245. $string = preg_replace("/http(s?):\/\/($pattern*)$/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for end
  246. return $string;
  247. }
  248. public static function BBCode($string) {
  249. preg_match_all("/\[quote/i", $string, $matches);
  250. $quote_begin_count = count($matches["0"]);
  251. preg_match_all("/\[\/quote\]/i", $string, $matches);
  252. $quote_end_count = count($matches["0"]);
  253. $quotes = min($quote_begin_count, $quote_end_count);
  254. $valid_quotes = ($quote_begin_count == $quote_end_count);
  255. if(!$valid_quotes) {
  256. }
  257. $search = [
  258. "/\[b\](.*?)\[\/b\]/is",
  259. "/\[i\](.*?)\[\/i\]/is",
  260. "/\[u\](.*?)\[\/u\]/is",
  261. "/\[s\](.*?)\[\/s\]/is",
  262. "/\[spoiler\](.*?)\[\/spoiler\]/is",
  263. "/\[img\=(.*?)\]/is",
  264. "/\[img\](.*?)\[\/img\]/is",
  265. "/\[img width\=(.*?)\](.*?)\[\/img\]/is",
  266. "/\[url\](.*?)\[\/url\]/is",
  267. "/\[url\=(.*?)\](.*?)\[\/url\]/is",
  268. "/\[color\=(.*?)\](.*?)\[\/color\]/is",
  269. "/\[colour\=(.*?)\](.*?)\[\/colour\]/is",
  270. "/\[size\=(.*?)\](.*?)\[\/size\]/is",
  271. "/\[font\=(.*?)\](.*?)\[\/font\]/is",
  272. "/\[center\](.*?)\[\/center\]<br \/>/is",
  273. "/\[center\](.*?)\[\/center\]/is",
  274. "/\[code\](.*?)\[\/code\]/is",
  275. "/\[background\=(.*?)\](.*?)\[\/background\]/is",
  276. "/\[youtube\][^\s]+v\=([A-Za-z0-9\-_]+)[^\s]*\[\/youtube\]/is", // YouTube URL
  277. "/\[youtube\]([A-Za-z0-9\-_]+?)\[\/youtube\]/is", // YouTube ID
  278. "/\[twitch\]([A-Za-z0-9\-_]+?)\[\/twitch\]/is", // Twitch ID
  279. "/\[hrd?\]\<br \/\>/is",
  280. "/\[nico\](.*?)\[\/nico\]/is",
  281. "/\[leftfloat\](.*?)\[\/leftfloat\]/is",
  282. "/\[rightfloat\](.*?)\[\/rightfloat\]/is",
  283. "/\[leftfloat width\=(.*?)\](.*?)\[\/leftfloat\]/is",
  284. "/\[clear\]/is",
  285. ];
  286. $replace = [
  287. '<strong>$1</strong>',
  288. '<em>$1</em>',
  289. '<u>$1</u>',
  290. '<del>$1</del>',
  291. '<span class="spoiler">$1</span>',
  292. '<img class="img-fluid" src="$1" alt="$1" />',
  293. '<img class="img-fluid" src="$1" alt="$1" />',
  294. '<img class="img-fluid" width="$1" src="$2" alt="$2" />',
  295. '<a href="$1" target="_blank">$1</a>',
  296. '<a href="$1" target="_blank">$2</a>',
  297. '<span style="color: $1;">$2</span>',
  298. '<span style="color: $1;">$2</span>',
  299. '<span style="font-size: $1'.'px;">$2</span>',
  300. '<span style="font-family: $1;">$2</span>',
  301. '<div class="text-center">$1</div>',
  302. '<div class="text-center">$1</div>',
  303. '<div class="code">$1</div>',
  304. '<span style="background: $1;">$2</span>',
  305. '<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
  306. '<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
  307. '<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">
  308. <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>',
  309. '<hr />',
  310. '<script type="text/javascript" src="https://ext.nicovideo.jp/thumb_watch/$1"></script>',
  311. '<div class="float-left">$1</div>',
  312. '<div class="float-right">$1</div>',
  313. '<div class="float-left" style="width: $1%;">$2</div>',
  314. '<div class="float-none"></div>',
  315. ];
  316. $string = preg_replace($search, $replace, $string);
  317. if($valid_quotes) {
  318. $search = [
  319. "/\[quote\]/is",
  320. "/\[quote\=(.*?)\]([^\]])/is",
  321. "/\[quote\=(.*?)\]/is",
  322. "/\[\/quote\]<br \/>/is",
  323. "/\[\/quote\]/is",
  324. ];
  325. $replace = [
  326. '<div class="card quote mb-3"><div class="card-header">Quote</div><div class="card-body">',
  327. '<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">$2',
  328. '<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">',
  329. '</div></div>',
  330. '</div></div>',
  331. ];
  332. $string = preg_replace($search, $replace, $string, $quotes);
  333. }
  334. return $string;
  335. }
  336. }