1
0

util.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 getUTCDateTime($time = false) {
  169. return Util::getDateTime($time);
  170. }
  171. public static function getDateTime($time = false) {
  172. if(!$time) $time = time();
  173. return gmdate("Y-m-d H:i:s", $time);
  174. }
  175. public static function getUnixTimeFromUTC($dts) {
  176. return strtotime($dts . " UTC");
  177. }
  178. public static function getDurationFromDTS($dts1, $dts2) {
  179. $tz = date_default_timezone_get();
  180. date_default_timezone_set("UTC");
  181. $t1 = strtotime($dts1);
  182. $t2 = strtotime($dts2);
  183. date_default_timezone_set($tz);
  184. return abs($t2 - $t1);
  185. }
  186. public static function getDisplayDateTime($time = false) {
  187. if($time === false) $time = time();
  188. $current_year = date("Y");
  189. $year = date("Y", $time);
  190. $format = ($year == $current_year) ? "M j, H:i" : "M j Y, H:i";
  191. return date($format, $time);
  192. }
  193. public static function getDisplayDate($time = false) {
  194. if($time === false) $time = time();
  195. $current_year = date("Y");
  196. $year = date("Y", $time);
  197. $format = ($year == $current_year) ? "M j" : "M j Y";
  198. return date($format, $time);
  199. }
  200. public static function getDisplayDateTimeFromUTC($dts) {
  201. $unix = Util::getUnixTimeFromUTC($dts);
  202. return Util::getDisplayDateTime($unix);
  203. }
  204. public static function sortObjectArray($array, $field, $compare_function = "") {
  205. usort($array, function($a, $b) use ($compare_function, $field) {
  206. switch($compare_function) {
  207. case "":
  208. if($a->$field == $b->$field) return 0;
  209. return ($a->$field < $b->$field) ? -1 : 1;
  210. case "strnatcasecmp":
  211. return strnatcasecmp($a->$field, $b->$field);
  212. }
  213. });
  214. return $array;
  215. }
  216. public static function arrayFlipByID($input = []) {
  217. $output = [];
  218. foreach($input as $object) {
  219. $output[$object->getID()] = $object;
  220. }
  221. return $output;
  222. }
  223. public static function formatText($string, $options = []) {
  224. $bb_code = $options["bb_code"] ?? true;
  225. $emoticon = $options["emoticon"] ?? true;
  226. $string = htmlspecialchars($string);
  227. $string = nl2br($string);
  228. /*
  229. if($emoticon && $emoticonresult) {
  230. db::data_seek($emoticonresult, 0);
  231. while($myrow = db::fetch_array($emoticonresult)) {
  232. $replacetext = $myrow["replacetext"];
  233. $url = $myrow["url"];
  234. $description = $myrow["description"];
  235. $string = str_replace("$replacetext", "<img src=\"$dirPrefix$url\" alt=\"$description\" />", $string);
  236. }
  237. }
  238. */
  239. if($emoticon) {
  240. $string = Emoticon::apply($string);
  241. }
  242. if($bb_code) {
  243. $string = Util::BBCode($string);
  244. }
  245. $pattern = "[0-9a-zA-Z\&\/\-\#\%\.\?\=\;\_\:\,\+\(\)\!]";
  246. $string = preg_replace("/^http(s?):\/\/($pattern*)/", "<a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a> ", $string); // Case for beginning
  247. $string = preg_replace("/\shttp(s?):\/\/($pattern*)/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for middle
  248. $string = preg_replace("/http(s?):\/\/($pattern*)$/", " <a href=\"http$1://$2\" target=\"_blank\">http$1://$2</a>", $string); // Case for end
  249. return $string;
  250. }
  251. public static function BBCode($string) {
  252. preg_match_all("/\[quote/i", $string, $matches);
  253. $quote_begin_count = count($matches["0"]);
  254. preg_match_all("/\[\/quote\]/i", $string, $matches);
  255. $quote_end_count = count($matches["0"]);
  256. $quotes = min($quote_begin_count, $quote_end_count);
  257. $valid_quotes = ($quote_begin_count == $quote_end_count);
  258. if(!$valid_quotes) {
  259. }
  260. $search = [
  261. "/\[b\](.*?)\[\/b\]/is",
  262. "/\[i\](.*?)\[\/i\]/is",
  263. "/\[u\](.*?)\[\/u\]/is",
  264. "/\[s\](.*?)\[\/s\]/is",
  265. "/\[spoiler\](.*?)\[\/spoiler\]/is",
  266. "/\[img\=(.*?)\]/is",
  267. "/\[img\](.*?)\[\/img\]/is",
  268. "/\[img width\=(.*?)\](.*?)\[\/img\]/is",
  269. "/\[url\](.*?)\[\/url\]/is",
  270. "/\[url\=(.*?)\](.*?)\[\/url\]/is",
  271. "/\[color\=(.*?)\](.*?)\[\/color\]/is",
  272. "/\[colour\=(.*?)\](.*?)\[\/colour\]/is",
  273. "/\[size\=(.*?)\](.*?)\[\/size\]/is",
  274. "/\[font\=(.*?)\](.*?)\[\/font\]/is",
  275. "/\[center\](.*?)\[\/center\]<br \/>/is",
  276. "/\[center\](.*?)\[\/center\]/is",
  277. "/\[code\](.*?)\[\/code\]/is",
  278. "/\[background\=(.*?)\](.*?)\[\/background\]/is",
  279. "/\[youtube\][^\s]+v\=([A-Za-z0-9\-_]+)[^\s]*\[\/youtube\]/is", // YouTube URL
  280. "/\[youtube\]([A-Za-z0-9\-_]+?)\[\/youtube\]/is", // YouTube ID
  281. "/\[twitch\]([A-Za-z0-9\-_]+?)\[\/twitch\]/is", // Twitch ID
  282. "/\[hrd?\]\<br \/\>/is",
  283. "/\[nico\](.*?)\[\/nico\]/is",
  284. "/\[leftfloat\](.*?)\[\/leftfloat\]/is",
  285. "/\[rightfloat\](.*?)\[\/rightfloat\]/is",
  286. "/\[leftfloat width\=(.*?)\](.*?)\[\/leftfloat\]/is",
  287. "/\[clear\]/is",
  288. ];
  289. $replace = [
  290. '<strong>$1</strong>',
  291. '<em>$1</em>',
  292. '<u>$1</u>',
  293. '<del>$1</del>',
  294. '<span class="spoiler">$1</span>',
  295. '<img class="img-fluid" src="$1" alt="$1" />',
  296. '<img class="img-fluid" src="$1" alt="$1" />',
  297. '<img class="img-fluid" width="$1" src="$2" alt="$2" />',
  298. '<a href="$1" target="_blank">$1</a>',
  299. '<a href="$1" target="_blank">$2</a>',
  300. '<span style="color: $1;">$2</span>',
  301. '<span style="color: $1;">$2</span>',
  302. '<span style="font-size: $1'.'px;">$2</span>',
  303. '<span style="font-family: $1;">$2</span>',
  304. '<div class="text-center">$1</div>',
  305. '<div class="text-center">$1</div>',
  306. '<div class="code">$1</div>',
  307. '<span style="background: $1;">$2</span>',
  308. '<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
  309. '<iframe width="448" height="280" src="https://www.youtube.com/embed/$1"></iframe>',
  310. '<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">
  311. <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>',
  312. '<hr />',
  313. '<script type="text/javascript" src="https://ext.nicovideo.jp/thumb_watch/$1"></script>',
  314. '<div class="float-left">$1</div>',
  315. '<div class="float-right">$1</div>',
  316. '<div class="float-left" style="width: $1%;">$2</div>',
  317. '<div class="float-none"></div>',
  318. ];
  319. $string = preg_replace($search, $replace, $string);
  320. if($valid_quotes) {
  321. $search = [
  322. "/\[quote\]/is",
  323. "/\[quote\=(.*?)\]([^\]])/is",
  324. "/\[quote\=(.*?)\]/is",
  325. "/\[\/quote\]<br \/>/is",
  326. "/\[\/quote\]/is",
  327. ];
  328. $replace = [
  329. '<div class="card quote mb-3"><div class="card-header">Quote</div><div class="card-body">',
  330. '<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">$2',
  331. '<div class="card quote mb-3"><div class="card-header">Quote: $1</div><div class="card-body">',
  332. '</div></div>',
  333. '</div></div>',
  334. ];
  335. $string = preg_replace($search, $replace, $string, $quotes);
  336. }
  337. return $string;
  338. }
  339. }