PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/arlicle/modules/arlicle/libraries/SpaceInfo.php

http://arlicle.googlecode.com/
PHP | 144 lines | 129 code | 8 blank | 7 comment | 22 complexity | 29e415828b31bed48e4a5a20721083a7 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Get the space info.
  4. * show the info of diskspace you have used and left.
  5. * show the info of database you have used and left.
  6. */
  7. class SpaceInfo_Core {
  8. // get the disk space you have used
  9. public static function disk_space() {
  10. $used_space_size = SpaceInfo::holdersize(DOCROOT);
  11. $space_size = SpaceInfo::size_format(Kohana::config('arlicle.space_size'), 'B');
  12. $info->total = SpaceInfo::size_format($space_size);
  13. $info->percent = round(($used_space_size / $space_size), 4) * 100;
  14. $info->used = SpaceInfo::size_format($used_space_size);
  15. return $info;
  16. }
  17. public static function database() {
  18. $orginal_database_size = SpaceInfo::used_database();
  19. $database_size = SpaceInfo::size_format(Kohana::config('arlicle.database_size'), 'B');
  20. $info->total = SpaceInfo::size_format($database_size);
  21. $info->percent = round(($orginal_database_size / $database_size ), 4) * 100;
  22. $info->used = SpaceInfo::size_format($orginal_database_size);
  23. return $info;
  24. }
  25. public static function used_database() {
  26. $db = new Database();
  27. $tables = $db->query("SHOW TABLE STATUS");
  28. $datasize = 0;
  29. $indexsize = 0;
  30. foreach($tables as $table) {
  31. $datasize += $table->Data_length;
  32. $indexsize += $table->Index_length;
  33. }
  34. return $datasize + $indexsize;
  35. }
  36. // get the dir's size,
  37. public static function holdersize($dir , $holdersize = 0) {
  38. if(@$handle = opendir($dir)) {
  39. while(false !== ($file = readdir($handle))) {
  40. if ($file != "." && $file != "..") {
  41. if(is_dir( $dir . '/' . $file)) {
  42. $holdersize = SpaceInfo::holdersize($dir . '/' . $file, $holdersize);
  43. }else {
  44. $holdersize = $holdersize + filesize($dir . '/' . $file);
  45. }
  46. }
  47. }
  48. } else {
  49. $holdersize = 'Can\'t read folder ' . $dir;
  50. }
  51. return $holdersize;
  52. }
  53. public static function notice_color($number) {
  54. $color = '#0a0';
  55. if ($number < 50) {
  56. $color = '#0a0';
  57. } else if ($number >= 85) {
  58. $color = '#ff0000';
  59. } else {
  60. $color = '#ff9900';
  61. }
  62. return $color;
  63. }
  64. public static function size_format($size, $unit = null) {
  65. $kb = 1024; // Kilobyte
  66. $mb = 1024 * $kb; // Megabyte
  67. $gb = 1024 * $mb; // Gigabyte
  68. $tb = 1024 * $gb; // Terabyte
  69. if (!is_numeric($size)) {
  70. $in_size = intval($size);
  71. $u = strtoupper(str_replace($in_size, '', $size));
  72. switch ($u) {
  73. case 'KB':
  74. case 'K':
  75. $size = $in_size * $kb;
  76. break;
  77. case 'MB':
  78. case 'M':
  79. $size = $in_size * $mb;
  80. break;
  81. case 'GB':
  82. case 'G':
  83. $size = $in_size * $gb;
  84. break;
  85. case 'TB':
  86. case 'T':
  87. $size = $in_size * $tb;
  88. break;
  89. }
  90. }
  91. if (!empty($unit)) {
  92. $unit = strtoupper($unit);
  93. switch($unit) {
  94. case 'KB':
  95. case 'K':
  96. $result = round($size / $kb, 2) . ' KB';
  97. break;
  98. case 'MB':
  99. case 'M':
  100. $result = round($size / $mb, 2) . ' MB';
  101. break;
  102. case 'GB':
  103. case 'G':
  104. $result = round($size / $gb, 2) . ' GB';
  105. break;
  106. case 'TB':
  107. case 'T':
  108. $result = round($size / $tb, 2) . ' TB';
  109. break;
  110. case 'B':
  111. case 'BITE':
  112. default:
  113. $result = $size;
  114. }
  115. } else {
  116. if($size < $mb) {
  117. if($size < $kb) {
  118. $result = $size . ' B';
  119. } else {
  120. $result = round($size / $kb, 2) . ' KB';
  121. }
  122. } else {
  123. if($size < $gb) {
  124. $result = round($size / $mb, 2) . ' MB';
  125. } else {
  126. if($size < $tb) {
  127. $result = round($size / $gb, 2) . ' GB';
  128. }else {
  129. $result = round($size / $tb, 2) . ' TB';
  130. }
  131. }
  132. }
  133. }
  134. return $result;
  135. }
  136. }