hosting_total_size_limit.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. /**
  5. * Checks total platform size.
  6. *
  7. * @param bool $debug
  8. *
  9. * @return bool
  10. */
  11. function isTotalPortalSizeBiggerThanLimit($debug = true)
  12. {
  13. $sizeLimit = api_get_configuration_value('hosting_total_size_limit');
  14. if (empty($sizeLimit)) {
  15. return true;
  16. }
  17. $updateFile = true;
  18. $file = api_get_path(SYS_COURSE_PATH).'hosting_total_size.php';
  19. // Default data
  20. $hostingData = [
  21. 'frequency' => 86400,
  22. ];
  23. $log = null;
  24. // Check if file exists and if it is updated
  25. if (file_exists($file)) {
  26. $hostingDataFromFile = require $file;
  27. // Check time() is UTC
  28. if (isset($hostingDataFromFile['updated_at']) &&
  29. isset($hostingDataFromFile['frequency']) &&
  30. isset($hostingDataFromFile['size'])
  31. ) {
  32. $hostingData = $hostingDataFromFile;
  33. $time = $hostingData['updated_at'] + $hostingData['frequency'];
  34. $diff = $time - time();
  35. if ($time > time()) {
  36. $log .= "You need to wait $diff seconds to update the file \n";
  37. $updateFile = false;
  38. }
  39. }
  40. }
  41. // Now get values for total portal size
  42. $log .= "Frequency loaded: ".$hostingData['frequency']."\n";
  43. if ($updateFile) {
  44. $log .= "Updating total size ... \n";
  45. $totalSize = calculateTotalPortalSize($debug);
  46. $log .= "Total size calculated: $totalSize \n";
  47. $hostingData['updated_at'] = time();
  48. $hostingData['size'] = $totalSize;
  49. $writer = new Zend\Config\Writer\PhpArray();
  50. $phpCode = $writer->toString($hostingData);
  51. file_put_contents($file, $phpCode);
  52. $log .= "File saved in $file \n";
  53. } else {
  54. $log .= "Total size not updated \n";
  55. $totalSize = $hostingData['size'];
  56. }
  57. $result = true;
  58. if ($totalSize > $sizeLimit) {
  59. $log .= "Current total size of $totalSize MB is bigger than limit: $sizeLimit MB \n";
  60. $result = false;
  61. }
  62. if ($debug) {
  63. echo $log;
  64. }
  65. return $result;
  66. }
  67. /**
  68. * @param bool $debug
  69. *
  70. * @return int total size in MB
  71. */
  72. function calculateTotalPortalSize($debug)
  73. {
  74. $table = Database::get_course_table(TABLE_DOCUMENT);
  75. // Documents
  76. $sql = "SELECT SUM(size) total FROM $table
  77. WHERE filetype = 'file' AND c_id <> ''";
  78. $result = Database::query($sql);
  79. $row = Database::fetch_array($result, 'ASSOC');
  80. $totalSize = $row['total'];
  81. if ($debug) {
  82. echo "Total size in table $table ".(round($totalSize / 1024))." MB \n";
  83. }
  84. $table = Database::get_course_table(TABLE_FORUM_ATTACHMENT);
  85. $sql = "SELECT SUM(size) total FROM $table WHERE c_id <> ''";
  86. $result = Database::query($sql);
  87. $row = Database::fetch_array($result, 'ASSOC');
  88. $subTotal = $row['total'];
  89. $totalSize += $subTotal;
  90. if ($debug) {
  91. echo "Total size in table $table ".(round($subTotal / 1024))." MB \n";
  92. }
  93. $totalSize = $totalSize / 1024;
  94. return $totalSize;
  95. }
  96. isTotalPortalSizeBiggerThanLimit(true);