sc.inc.old 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Copyright 2021, 2026 5 Mode
  4. *
  5. * This file is part of SqueePF.
  6. *
  7. * SqueePF is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SqueePF is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with SqueePF. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. * sc.inc
  21. *
  22. * Sanity check code.
  23. *
  24. * @author Daniele Bonini <my25mb@has.im>
  25. * @copyrights (c) 2016, 2024, 5 Mode
  26. */
  27. function SC_CHECK_ROUTE_STRU($route) {
  28. $ret = false;
  29. echo_ifdebug(true, "CHECKING_ROUTE_STRU: Start<br>");
  30. // Check for +DESC
  31. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/+DESC")) {
  32. return $ret;
  33. }
  34. echo_ifdebug(true, "CHECKING_ROUTE_STRU: +DESC: ok<br>");
  35. // Check for +CONTENT
  36. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT")) {
  37. return $ret;
  38. }
  39. echo_ifdebug(true, "CHECKING_ROUTE_STRU: +CONTENT: exists<br>");
  40. // Check file dependencies list
  41. // Private/routes/test/test.php
  42. // Private/functions/func.various.inc
  43. // Public/static/css/style.css
  44. // Public/static/res/logot.png
  45. $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT");
  46. foreach($af as &$line) {
  47. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/" . trim(basename($line)) . "+scr")) {
  48. return $ret;
  49. }
  50. echo_ifdebug(true, "CHECKING_ROUTE_STRU: " . trim(basename($line)) . "+scr: exists<br>");
  51. if (left($line,7)==="Private") {
  52. if (!is_readable(APP_PRIVATE_PATH. trim(substr($line, 7)))) {
  53. return $ret;
  54. }
  55. } else {
  56. if (!is_readable(APP_PUBLIC_PATH . trim(substr($line, 6)))) {
  57. return $ret;
  58. }
  59. }
  60. }
  61. //echo_ifdebug(true, "CHECKING_ROUTE_STRU: +CONTENT: ok<br>");
  62. // Check for +REQUIRING
  63. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+REQUIRING")) {
  64. return $ret;
  65. }
  66. // Check route dependencies list
  67. $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+REQUIRING");
  68. foreach($af as &$line) {
  69. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $line)) {
  70. return $ret;
  71. }
  72. }
  73. echo_ifdebug(true, "CHECKING_ROUTE_STRU: +REQUIRING: ok<br>");
  74. return true;
  75. }
  76. function SC_CHECK_ROUTE_DEP($route) {
  77. $ret = false;
  78. echo_ifdebug(true, "CHECKING_ROUTE_DEP: Start<br>");
  79. // Check for +CONTENT
  80. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT")) {
  81. return $ret;
  82. }
  83. // echo_ifdebug(true, "CHECKING_ROUTE_DEP: +CONTENT: exists<br>");
  84. //
  85. // Check file dependencies list
  86. //
  87. //
  88. // index-scrs/:
  89. // func.various.inc+scr:
  90. // partial path
  91. // size
  92. // sha
  93. // style.css+scr
  94. // partial path
  95. // size
  96. // sha
  97. //
  98. // Private/routes/test/test.php
  99. // Private/functions/func.various.inc
  100. // Public/static/css/style.css
  101. // Public/static/res/logot.png
  102. //
  103. $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT");
  104. foreach($af as &$line) {
  105. if (is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/" . trim(basename($line)) . "+scr")) {
  106. $asf = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/" . trim(basename($line)) . "+scr");
  107. $size = trim($asf[1]);
  108. $sha = trim($asf[2]);
  109. if (left($line,7)==="Private") {
  110. $filePath = APP_PRIVATE_PATH . trim(substr($line, 7));
  111. } else {
  112. $filePath = APP_PUBLIC_PATH . trim(substr($line, 6));
  113. }
  114. if (filesize($filePath) != $size) {
  115. return $ret;
  116. }
  117. if (hash("sha256", file_get_contents($filePath), false) !== $sha) {
  118. return $ret;
  119. }
  120. echo_ifdebug(true, "CHECKING_ROUTE_DEP: " . trim(basename($line)) . ": recognized<br>");
  121. } else {
  122. return $ret;
  123. }
  124. }
  125. return true;
  126. }
  127. function SC_CHECK_ROUTE_RES($route) {
  128. $ret = false;
  129. $res = [];
  130. echo_ifdebug(true, "CHECKING_ROUTE_RES: Start<br>");
  131. // Check for +CONTENT
  132. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT")) {
  133. return $ret;
  134. }
  135. //echo_ifdebug(true, "CHECKING_ROUTE_RES: +CONTENT: exists<br>");
  136. $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT");
  137. if (left($af[0],7)==="Private") {
  138. $routeFilePath = APP_PRIVATE_PATH . trim(substr($af[0], 7));
  139. } else {
  140. $routeFilePath = APP_PUBLIC_PATH . trim(substr($af[0], 6));
  141. }
  142. $acf = file_get_contents($routeFilePath);
  143. // ANALYZING HEADER..
  144. preg_match_all("/<head>.*<\/head>/s", $acf, $c, PREG_PATTERN_ORDER);
  145. //print_r($c[0][0]); // html header
  146. preg_match_all('/\/js\/.+\.js/s', $c[0][0], $res, PREG_PATTERN_ORDER);
  147. if (!empty($res[0])) {
  148. foreach($res[0] as $item) {
  149. if (!in_array("Public".$item.PHP_EOL,$af)) {
  150. return $ret;
  151. }
  152. echo_ifdebug(true, "CHECKING_ROUTE_RES: $item in " . basename($routeFilePath) . ": recognized<br>");
  153. }
  154. }
  155. preg_match_all('/\/css\/.+\.css/s', $c[0][0], $res, PREG_PATTERN_ORDER);
  156. //print_r($res);
  157. if (!empty($res)) {
  158. foreach($res[0] as $item) {
  159. //echo("Public".$item);
  160. if (!in_array("Public".$item.PHP_EOL,$af)) {
  161. return $ret;
  162. }
  163. echo_ifdebug(true, "CHECKING_ROUTE_RES: $item in " . basename($routeFilePath) . ": recognized<br>");
  164. }
  165. }
  166. //print_r($res[0]);
  167. // ANALYZING BODY..
  168. preg_match_all("/<body>.*<\/body>/s", $acf, $c, PREG_PATTERN_ORDER);
  169. //print_r($c[0][0]); // html body
  170. preg_match_all('/\/res\/.+\.png/s', $c[0][0], $res, PREG_PATTERN_ORDER);
  171. if (!empty($res[0])) {
  172. foreach($res[0] as $item) {
  173. //echo("Public".$item);
  174. if (!in_array("Public".$item.PHP_EOL,$af)) {
  175. return $ret;
  176. }
  177. echo_ifdebug(true, "CHECKING_ROUTE_RES: $item in " . basename($routeFilePath) . ": recognized<br>");
  178. }
  179. }
  180. return true;
  181. }
  182. function SC_CHECK_ROUTE_ALL($route) {
  183. $ret = 200;
  184. if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route)) {
  185. $ret = 404;
  186. return $ret;
  187. }
  188. if (!SC_CHECK_ROUTE_STRU($route) || !SC_CHECK_ROUTE_DEP($route) || !SC_CHECK_ROUTE_RES($route)) {
  189. $ret = 502;
  190. return $ret;
  191. }
  192. return $ret;
  193. }