hosting_total_size_limit.php 3.1 KB

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