Statistics.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Pages\Admin;
  10. use ForkBB\Models\Page;
  11. use ForkBB\Models\Pages\Admin;
  12. use function \ForkBB\{__, num};
  13. class Statistics extends Admin
  14. {
  15. /**
  16. * phpinfo
  17. */
  18. public function info(): Page
  19. {
  20. $this->c->Lang->load('admin_index');
  21. // Is phpinfo() a disabled function?
  22. if (! \function_exists('\\phpinfo')) {
  23. return $this->c->Message->message('PHPinfo disabled message', true, 200);
  24. }
  25. \ob_start();
  26. \phpinfo();
  27. $page = \ob_get_clean();
  28. if (\preg_match('%<body[^>]*>(.*)</body[^>]*>%is', $page, $matches)) {
  29. $phpinfo = $matches[1];
  30. if (\preg_match('%<style[^>]*>(.*?)</style[^>]*>%is', $page, $matches)) {
  31. $style = \preg_replace_callback(
  32. '%(\S[^{]*)({[^}]+})%',
  33. function ($match) {
  34. $result = \array_map(
  35. function ($val) {
  36. $val = \str_replace('body', '#id-phpinfo-div', $val, $count);
  37. return $count ? $val : '#id-phpinfo-div ' . $val;
  38. },
  39. \explode(',', $match[1])
  40. );
  41. return \implode(', ', $result) . $match[2];
  42. },
  43. $matches[1]
  44. );
  45. $this->c->Cache->set('phpinfoCSS', $style);
  46. $this->pageHeader('phpinfoStyle', 'link', 0, [
  47. 'rel' => 'stylesheet',
  48. 'type' => 'text/css',
  49. 'href' => $this->c->Router->link('AdminInfoCSS', ['time' => \time()] ),
  50. ]);
  51. }
  52. } else {
  53. $phpinfo = '- - -';
  54. }
  55. $this->nameTpl = 'admin/phpinfo';
  56. $this->mainSuffix = '-one-column';
  57. $this->aCrumbs[] = [$this->c->Router->link('AdminInfo'), ['%s', 'phpinfo()']];
  58. $this->aCrumbs[] = [$this->c->Router->link('AdminStatistics'), 'Server statistics'];
  59. $this->phpinfo = $phpinfo;
  60. return $this;
  61. }
  62. /**
  63. * Возврат css из phpinfo() как файл
  64. */
  65. public function infoCSS(): Page
  66. {
  67. $this->c->DEBUG = 0;
  68. $this->nameTpl = 'layouts/plain';
  69. $this->plainText = $this->c->Cache->get('phpinfoCSS', '');
  70. $this->header('Content-type', 'text/css; charset=utf-8');
  71. return $this;
  72. }
  73. /**
  74. * Подготавливает данные для шаблона
  75. */
  76. public function statistics(): Page
  77. {
  78. $this->c->Lang->load('admin_index');
  79. $this->nameTpl = 'admin/statistics';
  80. $this->aCrumbs[] = [$this->c->Router->link('AdminStatistics'), 'Server statistics'];
  81. $this->linkInfo = $this->c->Router->link('AdminInfo');
  82. // Get the server load averages (if possible)
  83. $this->serverLoad = __('Not available');
  84. switch (\PHP_OS_FAMILY) {
  85. case 'Windows':
  86. if (\function_exists('\\exec')) {
  87. \exec('wmic cpu get loadpercentage /all', $output);
  88. if (
  89. \is_array($output)
  90. && \preg_match('%(?:^|==)(\d+)(?:$|==)%', \implode('==', $output), $load)
  91. ) {
  92. $this->serverLoad = $load[1] . ' %';
  93. }
  94. }
  95. break;
  96. default:
  97. if (
  98. \function_exists('\\sys_getloadavg')
  99. && \is_array($load = \sys_getloadavg())
  100. ) {
  101. $this->serverLoad = num($load[0], 2) . ' ' . num($load[1], 2) . ' ' . num($load[2], 2);
  102. } elseif (\function_exists('\\exec')) {
  103. \exec('uptime', $output);
  104. if (
  105. \is_array($output)
  106. && \preg_match(
  107. '%averages?: ([0-9\.]+),?\s+([0-9\.]+),?\s+([0-9\.]+)%i',
  108. \implode(' ', $output),
  109. $load
  110. )
  111. ) {
  112. $this->serverLoad = num($load[1], 2) . ' ' . num($load[2], 2) . ' ' . num($load[3], 2);
  113. }
  114. }
  115. break;
  116. }
  117. // Get number of current visitors
  118. $this->onlineDetail = false;
  119. $this->numOnline = $this->c->Online->calc($this)->all;
  120. $stat = $this->c->DB->statistics();
  121. $this->dbVersion = $stat['db'];
  122. $this->tSize = $stat['size'];
  123. $this->tRecords = $stat['records'];
  124. $this->tTables = $stat['tables'];
  125. unset($stat['db'], $stat['size'], $stat['records'], $stat['tables']);
  126. $this->tOther = $stat;
  127. // Check for the existence of various PHP opcode caches/optimizers
  128. if (
  129. \ini_get('opcache.enable')
  130. && \function_exists('\\opcache_invalidate')
  131. ) {
  132. $this->accelerator = 'Zend OPcache';
  133. $this->linkAcc = 'https://www.php.net/opcache/';
  134. } elseif (\ini_get('wincache.fcenabled')) {
  135. $this->accelerator = 'Windows Cache for PHP';
  136. $this->linkAcc = 'https://www.php.net/wincache/';
  137. } else {
  138. $this->accelerator = __('NA');
  139. $this->linkAcc = null;
  140. }
  141. return $this;
  142. }
  143. }