util.php 10 KB

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