PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/_h5ai/private/php/core/class-setup.php

https://gitlab.com/billyprice1/h5ai
PHP | 185 lines | 151 code | 34 blank | 0 comment | 18 complexity | 3f0bfce23796c371d15f7b61157362de MD5 | raw file
  1. <?php
  2. class Setup {
  3. private $store;
  4. private $refresh;
  5. public function __construct($refresh = false) {
  6. $this->store = [];
  7. $this->refresh = $refresh;
  8. $this->add_globals_and_envs();
  9. $this->add_php_checks();
  10. $this->add_app_metadata();
  11. $this->add_server_metadata_and_check();
  12. $this->add_paths();
  13. $this->add_sys_cmd_checks();
  14. }
  15. private function set($key, $value) {
  16. if (array_key_exists($key, $this->store)) {
  17. Logger::log('setup key already taken', [
  18. 'key' => $key,
  19. 'value' => $value,
  20. 'found' => $this->store[$key]
  21. ]);
  22. exit;
  23. }
  24. if (!is_string($value) && !is_bool($value)) {
  25. Logger::log('setup value neither string nor boolean', [
  26. 'key' => $key,
  27. 'value' => $value
  28. ]);
  29. exit;
  30. }
  31. $this->store[$key] = $value;
  32. }
  33. public function get($key) {
  34. if (!array_key_exists($key, $this->store)) {
  35. Logger::log('setup key not found', ['key' => $key]);
  36. exit;
  37. }
  38. return $this->store[$key];
  39. }
  40. private function add_globals_and_envs() {
  41. $this->set('PHP_VERSION', PHP_VERSION);
  42. $this->set('MIN_PHP_VERSION', MIN_PHP_VERSION);
  43. $this->set('PHP_ARCH', (PHP_INT_SIZE * 8) . '-bit');
  44. $this->set('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
  45. $this->set('REQUEST_HREF', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
  46. $this->set('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
  47. $this->set('SERVER_SOFTWARE', $_SERVER['SERVER_SOFTWARE']);
  48. $this->set('HTTP_USER_AGENT', $_SERVER['HTTP_USER_AGENT']);
  49. }
  50. private function add_php_checks() {
  51. $this->set('HAS_PHP_EXIF', function_exists('exif_thumbnail'));
  52. $has_php_jpeg = false;
  53. if (function_exists('gd_info')) {
  54. $infos = gd_info();
  55. $has_php_jpeg = array_key_exists('JPEG Support', $infos) && $infos['JPEG Support'];
  56. }
  57. $this->set('HAS_PHP_JPEG', $has_php_jpeg);
  58. }
  59. private function add_app_metadata() {
  60. $this->set('NAME', 'h5ai');
  61. $this->set('VERSION', H5AI_VERSION);
  62. $this->set('FILE_PREFIX', '_h5ai');
  63. }
  64. private function add_server_metadata_and_check() {
  65. $server_software = $this->get('SERVER_SOFTWARE');
  66. $server_name = null;
  67. $server_version = null;
  68. if ($server_software && preg_match('#^(.*?)(?:/(.*?))?(?: |$)#', strtolower($server_software), $matches)) {
  69. $server_name = $matches[1];
  70. $server_version = count($matches) > 2 ? $matches[2] : '';
  71. }
  72. $this->set('SERVER_NAME', $server_name);
  73. $this->set('SERVER_VERSION', $server_version);
  74. $this->set('HAS_SERVER', in_array($server_name, ['apache', 'lighttpd', 'nginx', 'cherokee']));
  75. }
  76. private function add_paths() {
  77. $script_name = $this->get('SCRIPT_NAME');
  78. if ($this->get('SERVER_NAME') === 'lighttpd') {
  79. $script_name = preg_replace('#^.*?//#', '/', $script_name);
  80. }
  81. $this->set('H5AI_HREF', Util::normalize_path(dirname(dirname($script_name)), true));
  82. $this->set('H5AI_PATH', Util::normalize_path(dirname(dirname(dirname(dirname(__FILE__)))), false));
  83. $this->set('ROOT_HREF', Util::normalize_path(dirname($this->get('H5AI_HREF')), true));
  84. $this->set('ROOT_PATH', Util::normalize_path(dirname($this->get('H5AI_PATH')), false));
  85. $this->set('PUBLIC_HREF', Util::normalize_path($this->get('H5AI_HREF') . '/public/', true));
  86. $this->set('PUBLIC_PATH', Util::normalize_path($this->get('H5AI_PATH') . '/public/', false));
  87. $this->set('INDEX_HREF', Util::normalize_path($this->get('PUBLIC_HREF') . '/index.php', false));
  88. $this->set('CACHE_PUB_HREF', Util::normalize_path($this->get('PUBLIC_HREF') . '/cache', true));
  89. $this->set('CACHE_PUB_PATH', Util::normalize_path($this->get('PUBLIC_PATH') . '/cache', false));
  90. $this->set('HAS_WRITABLE_CACHE_PUB', @is_writable($this->get('CACHE_PUB_PATH')));
  91. $this->set('PRIVATE_PATH', Util::normalize_path($this->get('H5AI_PATH') . '/private', false));
  92. $this->set('CONF_PATH', Util::normalize_path($this->get('PRIVATE_PATH') . '/conf', false));
  93. $this->set('CACHE_PRV_PATH', Util::normalize_path($this->get('PRIVATE_PATH') . '/cache', false));
  94. $this->set('HAS_WRITABLE_CACHE_PRV', @is_writable($this->get('CACHE_PRV_PATH')));
  95. }
  96. private function add_sys_cmd_checks() {
  97. $cmds_cache_path = Util::normalize_path($this->get('CACHE_PRV_PATH') . '/cmds.json', false);
  98. $cmds = Json::load($cmds_cache_path);
  99. if (sizeof($cmds) === 0 || $this->refresh) {
  100. $cmds['command'] = Util::exec_0('command -v command');
  101. $cmds['which'] = Util::exec_0('which which') || Util::exec_0('which which.exe');
  102. $cmd = false;
  103. if ($cmds['command']) {
  104. $cmd = 'command -v';
  105. } else if ($cmds['which']) {
  106. $cmd = 'which';
  107. }
  108. foreach (['avconv', 'convert', 'du', 'ffmpeg', 'gm', 'tar', 'zip'] as $c) {
  109. $cmds[$c] = ($cmd !== false) && (Util::exec_0($cmd . ' ' . $c) || Util::exec_0($cmd . ' ' . $c . '.exe'));
  110. }
  111. Json::save($cmds_cache_path, $cmds);
  112. }
  113. foreach ($cmds as $c => $has) {
  114. $this->set('HAS_CMD_' . strtoupper($c), $has);
  115. }
  116. }
  117. public function to_jsono($as_admin = false) {
  118. $keys = [
  119. 'PUBLIC_HREF',
  120. 'ROOT_HREF'
  121. ];
  122. if ($as_admin) {
  123. $keys = array_merge($keys, [
  124. 'VERSION',
  125. 'PHP_VERSION',
  126. 'MIN_PHP_VERSION',
  127. 'PHP_ARCH',
  128. 'HAS_PHP_EXIF',
  129. 'HAS_PHP_JPEG',
  130. 'SERVER_NAME',
  131. 'SERVER_VERSION',
  132. 'HAS_SERVER',
  133. 'INDEX_HREF',
  134. 'HAS_WRITABLE_CACHE_PUB',
  135. 'HAS_WRITABLE_CACHE_PRV',
  136. 'HAS_CMD_AVCONV',
  137. 'HAS_CMD_CONVERT',
  138. 'HAS_CMD_DU',
  139. 'HAS_CMD_FFMPEG',
  140. 'HAS_CMD_GM',
  141. 'HAS_CMD_TAR',
  142. 'HAS_CMD_ZIP'
  143. ]);
  144. }
  145. $jsono = ['AS_ADMIN' => $as_admin];
  146. foreach ($keys as $key) {
  147. $jsono[$key] = $this->get($key);
  148. }
  149. return $jsono;
  150. }
  151. }