util.php 11 KB

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