Utility.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-12-15)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Services;
  20. use Illuminate\Database\Capsule\Manager as DB;
  21. /**
  22. * Description of Utility
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. class Utility
  28. {
  29. public static function unitFormat(&$value, $inUnit, $outUnit)
  30. {
  31. if (!in_array($inUnit, ['bytes', 'mb', 'gb', 'tb'])) {
  32. $debug = debug_backtrace();
  33. throw new \Exception(sprintf("Unit value ('%s') is invalid. File: %s:%s", $inUnit, $debug[0]['file'], $debug[0]['line']));
  34. }
  35. if (!in_array($outUnit, ['mb', 'gb', 'bytes', 'tb'])) {
  36. $debug = debug_backtrace();
  37. throw new \Exception(sprintf("Unit value ('%s') is invalid. File: %s:%s", $outUnit, $debug[0]['file'], $debug[0]['line']));
  38. }
  39. if ($value == 0) {
  40. return;
  41. }
  42. if (empty($value) || !is_numeric($value)) {
  43. $debug = debug_backtrace();
  44. throw new \Exception(sprintf("Unit value ('%s') is invalid. File: %s:%s", $value, $debug[0]['file'], $debug[0]['line']));
  45. }
  46. if ($inUnit == 'mb' && $outUnit == 'gb' && $value < 1024) {
  47. $debug = debug_backtrace();
  48. throw new \Exception(sprintf("Unit value %sMB is smaller than 1 GB. File: %s:%s", $value, $debug[0]['file'], $debug[0]['line']));
  49. }
  50. if ($inUnit == $outUnit) {
  51. return;
  52. } else if ($inUnit == 'bytes' && $outUnit == 'mb') {
  53. $value /= pow(1024, 2);
  54. $value = round($value);
  55. } else if ($inUnit == 'bytes' && $outUnit == 'gb') {
  56. $value /= pow(1024, 3);
  57. $value = round($value);
  58. } else if ($inUnit == 'mb' && $outUnit == 'gb') {
  59. $value = ceil($value / 1024);
  60. } else if ($inUnit == 'gb' && $outUnit == 'mb') {
  61. $value *= 1024;
  62. } else if ($inUnit == 'gb' && $outUnit == 'bytes') {
  63. $value *= pow(1024, 3);
  64. } else if ($inUnit == 'mb' && $outUnit == 'bytes') {
  65. $value *= pow(1024, 2);
  66. }else if ($inUnit == 'tb' && $outUnit == 'gb') {
  67. $value *= 1024;
  68. }else if ($inUnit == 'tb' && $outUnit == 'bytes') {
  69. $value *= pow(1024, 4);
  70. }else if ($inUnit == 'gb' && $outUnit == 'tb') {
  71. $value = number_format($value / 1024, 3, '.', '');
  72. }else if ($inUnit == 'tb' && $outUnit == 'mb') {
  73. $value *= pow(1024, 2);
  74. }
  75. }
  76. static function timeStamp($strTime = 'now')
  77. {
  78. return date('Y-m-d H:i:s', strtotime($strTime));
  79. }
  80. static function obClean()
  81. {
  82. $outputBuffering = ob_get_contents();
  83. if ($outputBuffering !== false) {
  84. if (!empty($outputBuffering)) {
  85. ob_clean();
  86. } else {
  87. ob_start();
  88. }
  89. }
  90. http_response_code(200);
  91. }
  92. /**
  93. * FUNCTION MG_uptime
  94. * Calculate uptime
  95. * @param int $uptime
  96. * @return boolean
  97. */
  98. public static function uptime($uptime)
  99. {
  100. if (!$uptime) {
  101. return false;
  102. }
  103. $days = floor($uptime / 60 / 60 / 24);
  104. $hours = $uptime / 60 / 60 % 24;
  105. $mins = $uptime / 60 % 60;
  106. $secs = $uptime % 60;
  107. $hours = ($hours < 10) ? "0" . $hours : $hours;
  108. $mins = ($mins < 10) ? "0" . $mins : $mins;
  109. $secs = ($secs < 10) ? "0" . $secs : $secs;
  110. if ($days) {
  111. return "{$days} days $hours:$mins:$secs";
  112. } else {
  113. return "$hours:$mins:$secs";
  114. }
  115. }
  116. public static function generatePassword($length = 8, $chars = "")
  117. {
  118. if (!$chars) {
  119. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  120. }
  121. $count = mb_strlen($chars);
  122. for ($i = 0, $result = ''; $i < $length; $i++) {
  123. $index = rand(0, $count - 1);
  124. $result .= mb_substr($chars, $index, 1);
  125. }
  126. return $result;
  127. }
  128. public static function passwordHash($password){
  129. $salt = self::generatePassword();
  130. return crypt($password, "\$5\$$salt\$");
  131. }
  132. public static function isAddon($name)
  133. {
  134. if (DB::table('tbladdonmodules')->where("module", $name)->count()) {
  135. $file = ROOTDIR . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'addons' . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . $name . '.php';
  136. return file_exists($file);
  137. }
  138. return false;
  139. }
  140. static function isIpManagerProxmoxVPSIntegration()
  141. {
  142. if (!self::isAddon('ipmanager2')) {
  143. return false;
  144. }
  145. return DB::table('ip_manager_modules')->where("modulename", "ProxmoxVPSIntegration")->where("enabled", "1")->count();
  146. }
  147. static function isIpManagerProxmoxCloudIntegration()
  148. {
  149. if (!self::isAddon('ipmanager2')) {
  150. return false;
  151. }
  152. return DB::table('ip_manager_modules')->where("modulename", "ProxmoxCloudIntegration")->where("enabled", "1")->count();
  153. }
  154. static function replaceSpecialChars($string)
  155. {
  156. $replace = [
  157. '&lt;' => '', '&gt;' => '', '&#039;' => '', '&amp;' => '',
  158. '&quot;' => '', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'Ae',
  159. '&Auml;' => 'A', 'Å' => 'A', 'Ā' => 'A', 'Ą' => 'A', 'Ă' => 'A', 'Æ' => 'Ae',
  160. 'Ç' => 'C', 'Ć' => 'C', 'Č' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Ď' => 'D', 'Đ' => 'D',
  161. 'Ð' => 'D', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ē' => 'E',
  162. 'Ę' => 'E', 'Ě' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ĝ' => 'G', 'Ğ' => 'G',
  163. 'Ġ' => 'G', 'Ģ' => 'G', 'Ĥ' => 'H', 'Ħ' => 'H', 'Ì' => 'I', 'Í' => 'I',
  164. 'Î' => 'I', 'Ï' => 'I', 'Ī' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Į' => 'I',
  165. 'İ' => 'I', 'IJ' => 'IJ', 'Ĵ' => 'J', 'Ķ' => 'K', 'Ł' => 'K', 'Ľ' => 'K',
  166. 'Ĺ' => 'K', 'Ļ' => 'K', 'Ŀ' => 'K', 'Ñ' => 'N', 'Ń' => 'N', 'Ň' => 'N',
  167. 'Ņ' => 'N', 'Ŋ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O',
  168. 'Ö' => 'Oe', '&Ouml;' => 'Oe', 'Ø' => 'O', 'Ō' => 'O', 'Ő' => 'O', 'Ŏ' => 'O',
  169. 'Œ' => 'OE', 'Ŕ' => 'R', 'Ř' => 'R', 'Ŗ' => 'R', 'Ś' => 'S', 'Š' => 'S',
  170. 'Ş' => 'S', 'Ŝ' => 'S', 'Ș' => 'S', 'Ť' => 'T', 'Ţ' => 'T', 'Ŧ' => 'T',
  171. 'Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue', 'Ū' => 'U',
  172. '&Uuml;' => 'Ue', 'Ů' => 'U', 'Ű' => 'U', 'Ŭ' => 'U', 'Ũ' => 'U', 'Ų' => 'U',
  173. 'Ŵ' => 'W', 'Ý' => 'Y', 'Ŷ' => 'Y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'Ž' => 'Z',
  174. 'Ż' => 'Z', 'Þ' => 'T', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
  175. 'ä' => 'ae', '&auml;' => 'ae', 'å' => 'a', 'ā' => 'a', 'ą' => 'a', 'ă' => 'a',
  176. 'æ' => 'ae', 'ç' => 'c', 'ć' => 'c', 'č' => 'c', 'ĉ' => 'c', 'ċ' => 'c',
  177. 'ď' => 'd', 'đ' => 'd', 'ð' => 'd', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
  178. 'ë' => 'e', 'ē' => 'e', 'ę' => 'e', 'ě' => 'e', 'ĕ' => 'e', 'ė' => 'e',
  179. 'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'ĥ' => 'h',
  180. 'ħ' => 'h', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ī' => 'i',
  181. 'ĩ' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ı' => 'i', 'ij' => 'ij', 'ĵ' => 'j',
  182. 'ķ' => 'k', 'ĸ' => 'k', 'ł' => 'l', 'ľ' => 'l', 'ĺ' => 'l', 'ļ' => 'l',
  183. 'ŀ' => 'l', 'ñ' => 'n', 'ń' => 'n', 'ň' => 'n', 'ņ' => 'n', 'ʼn' => 'n',
  184. 'ŋ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe',
  185. '&ouml;' => 'oe', 'ø' => 'o', 'ō' => 'o', 'ő' => 'o', 'ŏ' => 'o', 'œ' => 'oe',
  186. 'ŕ' => 'r', 'ř' => 'r', 'ŗ' => 'r', 'š' => 's', 'ù' => 'u', 'ú' => 'u',
  187. 'û' => 'u', 'ü' => 'ue', 'ū' => 'u', '&uuml;' => 'ue', 'ů' => 'u', 'ű' => 'u',
  188. 'ŭ' => 'u', 'ũ' => 'u', 'ų' => 'u', 'ŵ' => 'w', 'ý' => 'y', 'ÿ' => 'y',
  189. 'ŷ' => 'y', 'ž' => 'z', 'ż' => 'z', 'ź' => 'z', 'þ' => 't', 'ß' => 'ss',
  190. 'ſ' => 'ss', 'ый' => 'iy', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G',
  191. 'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I',
  192. 'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
  193. 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F',
  194. 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '',
  195. 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'YU', 'Я' => 'YA', 'а' => 'a',
  196. 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo',
  197. 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
  198. 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
  199. 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
  200. 'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e',
  201. 'ю' => 'yu', 'я' => 'ya'
  202. ];
  203. return str_replace(array_keys($replace), $replace, $string);
  204. }
  205. public static function cpuUsage($usege)
  206. {
  207. $usege *= 100;
  208. return number_format($usege, 2, '.', '');
  209. }
  210. }