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.

Mode.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Friendica\App;
  3. use Friendica\Core\Config;
  4. use Friendica\Database\DBA;
  5. /**
  6. * Mode of the current Friendica Node
  7. *
  8. * @package Friendica\App
  9. */
  10. class Mode
  11. {
  12. const LOCALCONFIGPRESENT = 1;
  13. const DBAVAILABLE = 2;
  14. const DBCONFIGAVAILABLE = 4;
  15. const MAINTENANCEDISABLED = 8;
  16. /***
  17. * @var int the mode of this Application
  18. *
  19. */
  20. private $mode;
  21. /**
  22. * @var string the basepath of the application
  23. */
  24. private $basepath;
  25. public function __construct($basepath = '')
  26. {
  27. $this->basepath = $basepath;
  28. $this->mode = 0;
  29. }
  30. /**
  31. * Sets the App mode
  32. *
  33. * - App::MODE_INSTALL : Either the database connection can't be established or the config table doesn't exist
  34. * - App::MODE_MAINTENANCE: The maintenance mode has been set
  35. * - App::MODE_NORMAL : Normal run with all features enabled
  36. *
  37. * @param string $basepath the Basepath of the Application
  38. *
  39. */
  40. public function determine($basepath = null)
  41. {
  42. if (!empty($basepath)) {
  43. $this->basepath = $basepath;
  44. }
  45. $this->mode = 0;
  46. if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
  47. && !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
  48. return;
  49. }
  50. $this->mode |= Mode::LOCALCONFIGPRESENT;
  51. if (!DBA::connected()) {
  52. return;
  53. }
  54. $this->mode |= Mode::DBAVAILABLE;
  55. if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
  56. return;
  57. }
  58. $this->mode |= Mode::DBCONFIGAVAILABLE;
  59. if (Config::get('system', 'maintenance')) {
  60. return;
  61. }
  62. $this->mode |= Mode::MAINTENANCEDISABLED;
  63. }
  64. /**
  65. * Checks, if the Friendica Node has the given mode
  66. *
  67. * @param int $mode A mode to test
  68. *
  69. * @return bool returns true, if the mode is set
  70. */
  71. public function has($mode)
  72. {
  73. echo "mode: " . $this->mode . " with " . $mode;
  74. echo "value: " . ($this->mode & $mode);
  75. return ($this->mode & $mode) > 0;
  76. }
  77. /**
  78. * Install mode is when the local config file is missing or the DB schema hasn't been installed yet.
  79. *
  80. * @return bool
  81. */
  82. public function isInstall()
  83. {
  84. return !$this->has(Mode::LOCALCONFIGPRESENT) ||
  85. !$this->has(MODE::DBCONFIGAVAILABLE);
  86. }
  87. /**
  88. * Normal mode is when the local config file is set, the DB schema is installed and the maintenance mode is off.
  89. *
  90. * @return bool
  91. */
  92. public function isNormal()
  93. {
  94. return $this->has(Mode::LOCALCONFIGPRESENT) &&
  95. $this->has(Mode::DBAVAILABLE) &&
  96. $this->has(Mode::DBCONFIGAVAILABLE) &&
  97. $this->has(Mode::MAINTENANCEDISABLED);
  98. }
  99. }