1: <?php
2:
3: namespace Budabot\Core;
4:
5: 6: 7:
8: class Util {
9:
10:
11: public $chatBot;
12:
13:
14: public $logger;
15:
16: const DATETIME = "d-M-Y H:i T";
17:
18: public function bytesConvert($bytes) {
19: $ext = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
20: $unitCount = 0;
21: for ($max = count($ext) - 1; $bytes >= 1024 && $unitCount < $max; $unitCount++) {
22: $bytes /= 1024;
23: }
24: return round($bytes, 2) ." ". $ext[$unitCount];
25: }
26:
27: public function unixtimeToReadable($time, $showSeconds = true) {
28: if ($time == 0) {
29: return '0 secs';
30: }
31:
32: $units = [
33: "day" => 86400,
34: "hr" => 3600,
35: "min" => 60,
36: "sec" => 1
37: ];
38:
39: $timeshift = '';
40: forEach ($units as $unit => $seconds) {
41: if ($time > 0) {
42: $length = floor($time / $seconds);
43: } else {
44: $length = ceil($time / $seconds);
45: }
46: if ($unit != "sec" || $showSeconds || $timeshift == '') {
47: if ($length > 1) {
48: $timeshift .= $length . " " . $unit . "s ";
49: } else if ($length == 1) {
50: $timeshift .= $length . " " . $unit . " ";
51: }
52: }
53: $time = $time % $seconds;
54: }
55:
56: return trim($timeshift);
57: }
58:
59: public function parseTime($budatime) {
60: $unixtime = 0;
61:
62: $matches = array();
63: $pattern = '/([0-9]+)([a-z]+)/';
64: preg_match_all($pattern, $budatime, $matches, PREG_SET_ORDER);
65:
66: forEach ($matches as $match) {
67: switch ($match[2]) {
68: case 'y':
69: case 'yr':
70: case 'year':
71: case 'years':
72: $unixtime += $match[1] * 31536000;
73: break;
74: case 'mo':
75: case 'month':
76: case 'months':
77: $unixtime += $match[1] * 2592000;
78: break;
79: case 'weeks':
80: case 'week':
81: case 'w':
82: $unixtime += $match[1] * 604800;
83: break;
84: case 'days':
85: case 'day':
86: case 'd':
87: $unixtime += $match[1] * 86400;
88: break;
89: case 'hours':
90: case 'hour':
91: case 'hrs':
92: case 'hr':
93: case 'h':
94: $unixtime += $match[1] * 3600;
95: break;
96: case 'mins':
97: case 'min':
98: case 'm':
99: $unixtime += $match[1] * 60;
100: break;
101: case 'secs':
102: case 'sec':
103: case 's':
104: $unixtime += $match[1];
105: break;
106: default:
107: return 0;
108: }
109: }
110:
111: return $unixtime;
112: }
113:
114: 115: 116: 117:
118: public function compareVersionNumbers($ver1, $ver2) {
119: $ver1Array = explode('.', $ver1);
120: $ver2Array = explode('.', $ver2);
121:
122: for ($i = 0; $i < count($ver1Array) && $i < count($ver2Array); $i++) {
123: if ($ver1Array[$i] > $ver2Array[$i]) {
124: return 1;
125: } else if ($ver1Array[$i] < $ver2Array[$i]) {
126: return -1;
127: }
128: }
129:
130: if (count($ver1Array) > count($ver2Array)) {
131: return 1;
132: } else if (count($ver1Array) < count($ver2Array)) {
133: return -1;
134: } else {
135: return 0;
136: }
137: }
138:
139: 140: 141: 142:
143: public function getProfessionName($search) {
144: $search = strtolower($search);
145: switch ($search) {
146: case "adv":
147: case "advy":
148: case "adventurer":
149: $prof = "Adventurer";
150: break;
151: case "agent":
152: $prof = "Agent";
153: break;
154: case "crat":
155: case "bureaucrat":
156: $prof = "Bureaucrat";
157: break;
158: case "doc":
159: case "doctor":
160: $prof = "Doctor";
161: break;
162: case "enf":
163: case "enfo":
164: case "enforcer":
165: $prof = "Enforcer";
166: break;
167: case "eng":
168: case "engi":
169: case "engy":
170: case "engineer":
171: $prof = "Engineer";
172: break;
173: case "fix":
174: case "fixer":
175: $prof = "Fixer";
176: break;
177: case "keep":
178: case "keeper":
179: $prof = "Keeper";
180: break;
181: case "ma":
182: case "martial":
183: case "martialartist":
184: case "martial artist":
185: $prof = "Martial Artist";
186: break;
187: case "mp":
188: case "meta":
189: case "metaphysicist":
190: case "meta-physicist":
191: $prof = "Meta-Physicist";
192: break;
193: case "nt":
194: case "nano":
195: case "nanotechnician":
196: case "nano-technician":
197: $prof = "Nano-Technician";
198: break;
199: case "sol":
200: case "sold":
201: case "soldier":
202: $prof = "Soldier";
203: break;
204: case "tra":
205: case "trad":
206: case "trader":
207: $prof = "Trader";
208: break;
209: case "shade":
210: $prof = "Shade";
211: break;
212: default:
213: $prof = '';
214: }
215:
216: return $prof;
217: }
218:
219: public function getProfessionAbbreviation($profession) {
220: switch ($profession) {
221: case "Adventurer":
222: $prof = "Adv";
223: break;
224: case "Agent":
225: $prof = "Agent";
226: break;
227: case "Bureaucrat":
228: $prof = "Crat";
229: break;
230: case "Doctor":
231: $prof = "Doc";
232: break;
233: case "Enforcer":
234: $prof = "Enf";
235: break;
236: case "Engineer":
237: $prof = "Eng";
238: break;
239: case "Fixer":
240: $prof = "Fixer";
241: break;
242: case "Keeper":
243: $prof = "Keeper";
244: break;
245: case "Martial Artist":
246: $prof = "MA";
247: break;
248: case "Meta-Physicist":
249: $prof = "MP";
250: break;
251: case "Nano-Technician":
252: $prof = "NT";
253: break;
254: case "Soldier":
255: $prof = "Sol";
256: break;
257: case "Trader":
258: $prof = "Trader";
259: break;
260: case "Shade":
261: $prof = "Shade";
262: break;
263: default:
264: $prof = "Unknown";
265: break;
266: }
267:
268: return $prof;
269: }
270:
271: 272: 273: 274:
275: public function verifyFilename($filename) {
276:
277: $filename = str_replace("\\", "/", $filename);
278:
279:
280: forEach (array_reverse($this->chatBot->vars['module_load_paths']) as $modulePath) {
281: if (file_exists("$modulePath/$filename")) {
282: return "$modulePath/$filename";
283: }
284: }
285: if (file_exists("./core/$filename")) {
286: return "./core/$filename";
287: }
288: if (file_exists($filename)) {
289: return $filename;
290: }
291: return "";
292: }
293:
294: public function getAbility($ability, $getFullName = false) {
295: $abilities = array(
296: 'agi' => 'Agility',
297: 'int' => 'Intelligence',
298: 'psy' => 'Psychic',
299: 'sta' => 'Stamina',
300: 'str' => 'Strength',
301: 'sen' => 'Sense'
302: );
303:
304: $ability = strtolower(substr($ability, 0, 3));
305:
306: if (isset($abilities[$ability])) {
307: if ($getFullName) {
308: return $abilities[$ability];
309: } else {
310: return $ability;
311: }
312: } else {
313: return null;
314: }
315: }
316:
317: public function randomArrayValue($array) {
318: return $array[rand(0, count($array) - 1)];
319: }
320:
321:
322:
323:
324:
325:
326: public function isValidSender($sender) {
327: return (int)0xFFFFFFFF == $sender ? false : true;
328: }
329:
330:
331: public function genRandomString($length = 10, $characters = '0123456789abcdefghijklmnopqrstuvwxyz') {
332: $string = '';
333: for ($p = 0; $p < $length; $p++) {
334: $string .= $characters[mt_rand(0, strlen($characters))];
335: }
336: return $string;
337: }
338:
339: public function getStackTrace() {
340: $trace = debug_backtrace();
341: $arr1 = array();
342: $arr2 = array();
343: forEach ($trace as $obj) {
344: $file = str_replace(getcwd(), "", $obj['file']);
345: $arr1 []= "{$file}({$obj['line']})";
346: $arr2 []= "{$obj['function']}()";
347: }
348:
349: array_shift($arr2);
350:
351: $str = '';
352: for ($i = 0; $i < count($arr1); $i++) {
353: $str .= "$arr1[$i] : $arr2[$i]\n";
354: }
355: return $str;
356: }
357:
358: 359: 360: 361: 362: 363: 364:
365: public function strpos_r($haystack, $needles) {
366: $seeks = array();
367:
368: forEach ($needles as $needle) {
369: $search = $haystack;
370: while ($seek = strrpos($search, $needle)) {
371: $seeks[$seek] = $needle;
372: $search = substr($search, 0, $seek);
373: }
374: }
375: ksort($seeks);
376: return $seeks;
377: }
378:
379: public function date($unixtime) {
380: return date(self::DATETIME, $unixtime);
381: }
382:
383: public function endsWith($string, $test) {
384: $strlen = strlen($string);
385: $testlen = strlen($test);
386: if ($testlen > $strlen) {
387: return false;
388: }
389: return substr_compare($string, $test, -$testlen) === 0;
390: }
391:
392:
393: public function startsWith($haystack, $needle) {
394: return !strncmp($haystack, $needle, strlen($needle));
395: }
396:
397: public function stripColors($msg) {
398: $msg = preg_replace("~<font color=#.{6}>~", "", $msg);
399: $msg = preg_replace("~</font>~", "", $msg);
400: return $msg;
401: }
402:
403: public function generateQueryFromParams($params, $column) {
404: $queryParams = array();
405: $first = true;
406: forEach ($params as $key => $value) {
407: if ($value[0] == "-" && strlen($value) > 1) {
408: $value = substr($value, 1);
409: $op = "NOT LIKE";
410: } else {
411: $op = "LIKE";
412: }
413: if ($first) {
414: $query .= "$column $op ?";
415: $first = false;
416: } else {
417: $query .= " AND $column $op ?";
418: }
419: $queryParams []= '%' . $value . '%';
420: }
421: return array($query, $queryParams);
422: }
423:
424:
425: public function mergesort(&$array, $cmp_function) {
426:
427: if (count($array) < 2) {
428: return;
429: }
430:
431: $halfway = count($array) / 2;
432: $array1 = array_slice($array, 0, $halfway);
433: $array2 = array_slice($array, $halfway);
434:
435: $this->mergesort($array1, $cmp_function);
436: $this->mergesort($array2, $cmp_function);
437:
438: if ($cmp_function(end($array1), $array2[0]) < 1) {
439: $array = array_merge($array1, $array2);
440: return;
441: }
442:
443: $array = array();
444: $ptr1 = $ptr2 = 0;
445: while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
446: if ($cmp_function($array1[$ptr1], $array2[$ptr2]) < 1) {
447: $array[] = $array1[$ptr1++];
448: } else {
449: $array[] = $array2[$ptr2++];
450: }
451: }
452:
453: while ($ptr1 < count($array1)) {
454: $array[] = $array1[$ptr1++];
455: }
456: while ($ptr2 < count($array2)) {
457: $array[] = $array2[$ptr2++];
458: }
459: return;
460: }
461:
462: public function interpolate($x1, $x2, $y1, $y2, $x) {
463: if ($x1 == $x2) {
464: return $y2;
465: }
466: $result = ($y2 - $y1) / ($x2 - $x1) * ($x - $x1) + $y1;
467: $result = round($result, 0);
468: return $result;
469: }
470:
471: public function mapFilterCombine($arr, $glue, $func) {
472: $newArr = array();
473: forEach ($arr as $key => $value) {
474: $result = call_user_func($func, $key, $value);
475: if ($result !== null) {
476: $newArr []= $result;
477: }
478: }
479: return implode($glue, $newArr);
480: }
481:
482: public function getFilesInDirectory($path) {
483: $files = array();
484: if ($dir = dir($path)) {
485: while (false !== ($entry = $dir->read())) {
486: if (!is_dir($path . "/" . $entry)) {
487: $files []= $entry;
488: }
489: }
490: $dir->close();
491: }
492: return $files;
493: }
494:
495: public function getDirectoriesInDirectory($path) {
496: return array_filter(scandir($path), function ($f) use($path) {
497: return $f != '.' && $f != '..' && is_dir($path . DIRECTORY_SEPARATOR . $f);
498: });
499: }
500:
501: public function isInteger($input){
502: return(ctype_digit(strval($input)));
503: }
504: }
505: