__loader__.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * phpMeccano v0.2.0. Web-framework written with php programming language. Core module [__loader__.php].
  4. * Copyright (C) 2015-2019 Alexei Muzarov
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * e-mail: azexmail@gmail.com
  21. * e-mail: azexmail@mail.ru
  22. * https://bitbucket.org/azexmail/phpmeccano
  23. */
  24. namespace core;
  25. // a function to load PHP libraries of the core or any installed plugin
  26. function loadPHP($lib, $plugin = "core") {
  27. if (!is_string($lib) || preg_match('/.*\.\.\/*./', $lib) || !is_string($plugin) || preg_match('/.*\.\.\/*./', $plugin)) {
  28. return null;
  29. }
  30. if ($plugin == "core") {
  31. $fullPath = realpath(MECCANO_CORE_DIR."/$lib.php");
  32. if ($fullPath && is_file($fullPath) && is_readable($fullPath)) {
  33. require_once $fullPath;
  34. return true;
  35. }
  36. }
  37. else {
  38. $fullPath = realpath(MECCANO_PHP_DIR."/$plugin/$lib.php");
  39. if ($fullPath && is_file($fullPath) && is_readable($fullPath)) {
  40. require_once $fullPath;
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. // a function to load JavaScript libraries of the core or any installed plugin
  47. function loadJS($lib, $plugin = "core") {
  48. if (!is_string($lib) || preg_match('/.*\.\.\/*./', $lib) || !is_string($plugin) || preg_match('/.*\.\.\/*./', $plugin)) {
  49. return null;
  50. }
  51. $fullPath = realpath(MECCANO_JS_DIR."/$plugin/$lib.js");
  52. if ($fullPath && is_file($fullPath) && is_readable($fullPath)) {
  53. return file_get_contents($fullPath);
  54. }
  55. return false;
  56. }
  57. // a function to load CSS libraries of the core or any installed plugin
  58. function loadCSS($lib, $plugin = "core") {
  59. if (!is_string($lib) || preg_match('/.*\.\.\/*./', $lib) || !is_string($plugin) || preg_match('/.*\.\.\/*./', $plugin)) {
  60. return null;
  61. }
  62. $fullPath = realpath(MECCANO_CSS_DIR."/$plugin/$lib.css");
  63. if ($fullPath && is_file($fullPath) && is_readable($fullPath)) {
  64. return file_get_contents($fullPath);
  65. }
  66. return false;
  67. }
  68. // a function to load documents and files of the core or any installed plugin
  69. function loadDOC($doc, $plugin = "core", $disp = "inline", $nocache = false) {
  70. if (!isset($_SERVER['SERVER_SOFTWARE'])) {
  71. return false; // The function must be executed on a web server
  72. }
  73. if (!is_string($doc) || preg_match('/.*\.\.\/*./', $doc) || !is_string($plugin) || preg_match('/.*\.\.\/*./', $plugin) || !in_array($disp, ['inline', 'attachment'])) {
  74. include MECCANO_SERVICE_PAGES.'/400.php'; // Bad Request
  75. exit();
  76. }
  77. $fullPath = realpath(MECCANO_DOCUMENTS_DIR."/$plugin/$doc");
  78. if (!$fullPath || !is_file($fullPath)) {
  79. include MECCANO_SERVICE_PAGES.'/404.php'; // Not Found
  80. exit();
  81. }
  82. if (!is_readable($fullPath)) {
  83. include MECCANO_SERVICE_PAGES.'/403.php'; // Forbidden
  84. exit();
  85. }
  86. if (preg_match('/.*Apache.*/', $_SERVER['SERVER_SOFTWARE'])) {
  87. // https://tn123.org/mod_xsendfile/
  88. header("X-SendFile: $fullPath");
  89. }
  90. elseif (preg_match('/.*nginx.*/', $_SERVER['SERVER_SOFTWARE'])) {
  91. // https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/
  92. header("X-Accel-Redirect: /".basename(MECCANO_DOCUMENTS_DIR)."/$plugin/$doc");
  93. }
  94. elseif (preg_match('/.*lighttpd.*/', $_SERVER['SERVER_SOFTWARE'])) {
  95. // https://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file
  96. header("X-LIGHTTPD-send-file: $fullPath");
  97. }
  98. else {
  99. include MECCANO_SERVICE_PAGES.'/501.php'; // Not Implemented
  100. exit();
  101. }
  102. if ($nocache) { // if the file should't be cached
  103. header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  104. header("Pragma: no-cache");
  105. }
  106. $mimeType = mime_content_type($fullPath);
  107. $fileSize = filesize($fullPath);
  108. $fileName = basename($fullPath);
  109. header("Content-Type: $mimeType");
  110. header("Content-Length: $fileSize");
  111. header("Content-Disposition: $disp; filename=$fileName");
  112. exit();
  113. }
  114. function mntc() {
  115. $conf = json_decode(file_get_contents(MECCANO_SERVICE_PAGES.'/maintenance.json'));
  116. if (is_object($conf) && isset($conf->enabled) && isset($conf->startpoint) && $conf->enabled && $conf->startpoint<=time() && !in_array($_SERVER['REMOTE_ADDR'], preg_split('/\s*,\s*/', preg_replace('/\s\s+/', ' ', MECCANO_MNTC_IP)), true)) {
  117. include MECCANO_SERVICE_PAGES.'/maintenance.php'; // Maintenance mode is enabled
  118. exit();
  119. }
  120. return false; // Maintenance mode is disabled
  121. }
  122. function dbLink() {
  123. $db = new \mysqli(MECCANO_DBHOST, MECCANO_DBANAME, MECCANO_DBAPASS, MECCANO_DBNAME, MECCANO_DBPORT);
  124. $db->set_charset("utf8mb4");
  125. return $db;
  126. }