1
0

util.php 11 KB

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