1
0

Core.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App;
  3. use Exception;
  4. use Gettext\Loader\MoLoader;
  5. use Gettext\Translator;
  6. use Gettext\TranslatorFunctions;
  7. use RuntimeException;
  8. class Core {
  9. public const SCRIPT_GLOBAL = 0;
  10. public const SCRIPT_PAGE = 1;
  11. private static $instance;
  12. /** @var User */
  13. protected $user;
  14. /** @var Breadcrumb[] */
  15. protected $breadcrumbs = [];
  16. /** @var string */
  17. protected $locale;
  18. /** @var Translator */
  19. protected $translator;
  20. /** @var string */
  21. protected $controllerName = "home";
  22. /** @var string[] Global scripts */
  23. protected $scripts = [];
  24. /** @var string[] Page scripts */
  25. protected $pageScripts = [];
  26. /** @var string[] CSS stylesheets */
  27. protected $stylesheets = [];
  28. // Static methods
  29. public static function getInstance(): Core {
  30. if(self::$instance === null) {
  31. self::$instance = new Core();
  32. }
  33. return self::$instance;
  34. }
  35. public static function hasReCaptcha(): bool {
  36. return !empty($_ENV["RECAPTCHA_SECRET_KEY"]);
  37. }
  38. public static function reCaptchaCheck(string $recaptcha_token): bool {
  39. if(!self::hasReCaptcha()) {
  40. return true;
  41. }
  42. $url = sprintf("https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s",
  43. $_ENV["RECAPTCHA_SECRET_KEY"], $recaptcha_token, $_SERVER["REMOTE_ADDR"]
  44. );
  45. $response = file_get_contents($url);
  46. $object = json_decode($response);
  47. if(!$object) {
  48. return false;
  49. }
  50. return $object->success && $object->score >= 0.6;
  51. }
  52. /**
  53. * Core constructor.
  54. * @throws Exception
  55. */
  56. public function __construct() {
  57. if(PHP_SAPI !== "cli") {
  58. $this->initSession();
  59. }
  60. $this->initBreadcrumbs();
  61. $this->initLocalisation();
  62. }
  63. // Object methods
  64. /**
  65. * @return void
  66. * @throws Exception
  67. */
  68. private function initSession(): void {
  69. session_name($_ENV["SESSION_NAME"]);
  70. session_start();
  71. $user_id = $_SESSION["user_id"] ?? null;
  72. if($user_id && $this->hasConnection()) {
  73. $this->user = User::get($user_id);
  74. }
  75. if($this->isLoggedIn()) {
  76. setcookie($_ENV["SESSION_NAME"], session_id(), time() + 28 * 86400, "/");
  77. $this->user->touch();
  78. }
  79. register_shutdown_function(static function() {
  80. if(empty($_SESSION) && session_status() === PHP_SESSION_ACTIVE) {
  81. session_destroy();
  82. }
  83. });
  84. }
  85. private function initBreadcrumbs(): void {
  86. $this->breadcrumbs[] = new Breadcrumb("/", $this->getSiteName());
  87. }
  88. private function initLocalisation(): void {
  89. $this->locale = $_ENV["LOCALE"];
  90. $this->translator = new Translator;
  91. TranslatorFunctions::register($this->translator);
  92. $mo_path = $_ENV["DIR_ROOT"] . "/i18n/$this->locale/LC_MESSAGES/$this->locale.mo";
  93. if(!file_exists($mo_path)) {
  94. return;
  95. }
  96. $loader = new MoLoader();
  97. $translations = $loader->loadFile($mo_path);
  98. $this->translator->addTranslations($translations->toArray());
  99. }
  100. /**
  101. * @param string $path
  102. * @param string $mode
  103. * @throws Exception
  104. */
  105. public function addStylesheet(string $path, string $mode = "append"): void {
  106. if(!file_exists($_ENV["DIR_ROOT"] . "/public/$path")) {
  107. throw new RuntimeException("'$path' does not exist.");
  108. }
  109. if($mode === "prepend") {
  110. array_unshift($this->stylesheets, $path);
  111. }
  112. else {
  113. $this->stylesheets[] = $path;
  114. }
  115. }
  116. /**
  117. * @param string $path File path relative to /public
  118. * @param int $type
  119. * @throws Exception
  120. */
  121. public function addScript(string $path, int $type = self::SCRIPT_GLOBAL): void {
  122. if(!file_exists($_ENV["DIR_ROOT"] . "/public/$path")) {
  123. throw new RuntimeException("'$path' does not exist.");
  124. }
  125. if($type === self::SCRIPT_PAGE) {
  126. $this->pageScripts[] = $path;
  127. }
  128. else {
  129. $this->scripts[] = $path;
  130. }
  131. }
  132. public function getStylesheets(): array {
  133. return $this->stylesheets;
  134. }
  135. public function getGlobalScripts(): array {
  136. return $this->scripts;
  137. }
  138. public function getPageScripts(): array {
  139. return $this->pageScripts;
  140. }
  141. /**
  142. * @return Breadcrumb[]
  143. */
  144. public function getBreadcrumbs(): array {
  145. return $this->breadcrumbs;
  146. }
  147. public function getLocale(): string {
  148. return $this->locale;
  149. }
  150. /**
  151. * @param string $name
  152. * @param array $params
  153. * @throws Exception
  154. */
  155. public function requireController(string $name, array $params = []): void {
  156. extract($params, EXTR_OVERWRITE);
  157. $path = $_ENV["DIR_ROOT"] . "/controllers/$name.php";
  158. if(!file_exists($path)) {
  159. throw new RuntimeException("Controller '$name' could not be found.");
  160. }
  161. $this->controllerName = $name;
  162. require $path;
  163. }
  164. public function getUser(): ?User {
  165. return $this->user;
  166. }
  167. /**
  168. * @return bool
  169. * @throws Exception
  170. */
  171. public function isLoggedIn(): bool {
  172. return ($this->user !== null);
  173. }
  174. /**
  175. * @return string
  176. */
  177. public function getControllerName(): string {
  178. return $this->controllerName;
  179. }
  180. public function addBreadcrumb(string $url, string $name): void {
  181. $this->breadcrumbs[] = new Breadcrumb($url, $name);
  182. }
  183. public function hasConnection(): bool {
  184. return Database::$connection !== null;
  185. }
  186. public function getSiteName(): string {
  187. return $_ENV["SITE_NAME"];
  188. }
  189. public function getTabTitle(): string {
  190. $breadcrumbs = array_reverse($this->getBreadcrumbs());
  191. $parts = [];
  192. foreach($breadcrumbs as $breadcrumb) {
  193. $parts[] = $breadcrumb->getName();
  194. }
  195. return implode(" | ", $parts);
  196. }
  197. public function getSettings(): object {
  198. $filename = "settings.json";
  199. if(file_exists($filename)) {
  200. $settings_json = file_get_contents($filename);
  201. $settings = json_decode($settings_json);
  202. }
  203. else {
  204. $settings = (object) [
  205. "defaultCompanyId" => null
  206. ];
  207. }
  208. return $settings;
  209. }
  210. public function saveSettings(object $settings): bool {
  211. return file_put_contents("settings.json", json_encode($settings));
  212. }
  213. }