util.php 12 KB

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