Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
4.9KB

  1. <?php
  2. namespace Friendica\App;
  3. use Detection\MobileDetect;
  4. use Friendica\Core\Config\Cache\ConfigCache;
  5. use Friendica\Database\Database;
  6. use Friendica\Util\BasePath;
  7. /**
  8. * Mode of the current Friendica Node
  9. *
  10. * @package Friendica\App
  11. */
  12. class Mode
  13. {
  14. const LOCALCONFIGPRESENT = 1;
  15. const DBAVAILABLE = 2;
  16. const DBCONFIGAVAILABLE = 4;
  17. const MAINTENANCEDISABLED = 8;
  18. /***
  19. * @var int The mode of this Application
  20. *
  21. */
  22. private $mode;
  23. /**
  24. * @var bool True, if the call is a backend call
  25. */
  26. private $isBackend;
  27. /**
  28. * @var bool True, if the call is a ajax call
  29. */
  30. private $isAjax;
  31. /**
  32. * @var bool True, if the call is from a mobile device
  33. */
  34. private $isMobile;
  35. /**
  36. * @var bool True, if the call is from a tablet device
  37. */
  38. private $isTablet;
  39. public function __construct(int $mode = 0, bool $isBackend = false, bool $isAjax = false, bool $isMobile = false, bool $isTablet = false)
  40. {
  41. $this->mode = $mode;
  42. $this->isBackend = $isBackend;
  43. $this->isAjax = $isAjax;
  44. $this->isMobile = $isMobile;
  45. $this->isTablet = $isTablet;
  46. }
  47. /**
  48. * Sets the App mode
  49. *
  50. * - App::MODE_INSTALL : Either the database connection can't be established or the config table doesn't exist
  51. * - App::MODE_MAINTENANCE: The maintenance mode has been set
  52. * - App::MODE_NORMAL : Normal run with all features enabled
  53. *
  54. * @return Mode returns the determined mode
  55. *
  56. * @throws \Exception
  57. */
  58. public function determine(BasePath $basepath, Database $database, ConfigCache $configCache)
  59. {
  60. $mode = 0;
  61. $basepathName = $basepath->getPath();
  62. if (!file_exists($basepathName . '/config/local.config.php')
  63. && !file_exists($basepathName . '/config/local.ini.php')
  64. && !file_exists($basepathName . '/.htconfig.php')) {
  65. return new Mode($mode);
  66. }
  67. $mode |= Mode::LOCALCONFIGPRESENT;
  68. if (!$database->connected()) {
  69. return new Mode($mode);
  70. }
  71. $mode |= Mode::DBAVAILABLE;
  72. if ($database->fetchFirst("SHOW TABLES LIKE 'config'") === false) {
  73. return new Mode($mode);
  74. }
  75. $mode |= Mode::DBCONFIGAVAILABLE;
  76. if (!empty($configCache->get('system', 'maintenance')) ||
  77. // Don't use Config or Configuration here because we're possibly BEFORE initializing the Configuration,
  78. // so this could lead to a dependency circle
  79. !empty($database->selectFirst('config', ['v'], ['cat' => 'system', 'k' => 'maintenance'])['v'])) {
  80. return new Mode($mode);
  81. }
  82. $mode |= Mode::MAINTENANCEDISABLED;
  83. return new Mode($mode, $this->isBackend, $this->isAjax, $this->isMobile, $this->isTablet);
  84. }
  85. /**
  86. * Checks if the site is called via a backend process
  87. *
  88. * @param bool $isBackend True, if the call is from a backend script (daemon, worker, ...)
  89. * @param Module $module The pre-loaded module (just name, not class!)
  90. * @param array $server The $_SERVER variable
  91. * @param MobileDetect $mobileDetect The mobile detection library
  92. *
  93. * @return Mode returns the determined mode
  94. */
  95. public function determineRunMode(bool $isBackend, Module $module, array $server, MobileDetect $mobileDetect)
  96. {
  97. $isBackend = $isBackend ||
  98. $module->isBackend();
  99. $isMobile = $mobileDetect->isMobile();
  100. $isTablet = $mobileDetect->isTablet();
  101. $isAjax = strtolower($server['HTTP_X_REQUESTED_WITH'] ?? '') == 'xmlhttprequest';
  102. return new Mode($this->mode, $isBackend, $isAjax, $isMobile, $isTablet);
  103. }
  104. /**
  105. * Checks, if the Friendica Node has the given mode
  106. *
  107. * @param int $mode A mode to test
  108. *
  109. * @return bool returns true, if the mode is set
  110. */
  111. public function has($mode)
  112. {
  113. return ($this->mode & $mode) > 0;
  114. }
  115. /**
  116. * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
  117. *
  118. * @return bool
  119. */
  120. public function isInstall()
  121. {
  122. return !$this->has(Mode::LOCALCONFIGPRESENT) ||
  123. !$this->has(MODE::DBCONFIGAVAILABLE);
  124. }
  125. /**
  126. * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
  127. *
  128. * @return bool
  129. */
  130. public function isNormal()
  131. {
  132. return $this->has(Mode::LOCALCONFIGPRESENT) &&
  133. $this->has(Mode::DBAVAILABLE) &&
  134. $this->has(Mode::DBCONFIGAVAILABLE) &&
  135. $this->has(Mode::MAINTENANCEDISABLED);
  136. }
  137. /**
  138. * Returns true, if the call is from a backend node (f.e. from a worker)
  139. *
  140. * @return bool Is it a backend call
  141. */
  142. public function isBackend()
  143. {
  144. return $this->isBackend;
  145. }
  146. /**
  147. * Check if request was an AJAX (xmlhttprequest) request.
  148. *
  149. * @return bool true if it was an AJAX request
  150. */
  151. public function isAjax()
  152. {
  153. return $this->isAjax;
  154. }
  155. /**
  156. * Check if request was a mobile request.
  157. *
  158. * @return bool true if it was an mobile request
  159. */
  160. public function isMobile()
  161. {
  162. return $this->isMobile;
  163. }
  164. /**
  165. * Check if request was a tablet request.
  166. *
  167. * @return bool true if it was an tablet request
  168. */
  169. public function isTablet()
  170. {
  171. return $this->isTablet;
  172. }
  173. }