util.php 11 KB

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