123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /**
- * Copyright 2021, 2026 5 Mode
- *
- * This file is part of SqueePF.
- *
- * SqueePF is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * SqueePF is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with SqueePF. If not, see <https://www.gnu.org/licenses/>.
- *
- * sc.inc
- *
- * Sanity check code.
- *
- * @author Daniele Bonini <my25mb@has.im>
- * @copyrights (c) 2016, 2024, 5 Mode
- */
-
-
- function SC_CHECK_ROUTE_STRU($route) {
- $ret = false;
- echo_ifdebug(true, "CHECKING_ROUTE_STRU: Start<br>");
- // Check for +DESC
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/+DESC")) {
- return $ret;
- }
- echo_ifdebug(true, "CHECKING_ROUTE_STRU: +DESC: ok<br>");
- // Check for +CONTENT
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT")) {
- return $ret;
- }
- echo_ifdebug(true, "CHECKING_ROUTE_STRU: +CONTENT: exists<br>");
- // Check file dependencies list
- // Private/routes/test/test.php
- // Private/functions/func.various.inc
- // Public/static/css/style.css
- // Public/static/res/logot.png
- $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT");
- foreach($af as &$line) {
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/" . trim(basename($line)) . "+scr")) {
- return $ret;
- }
- echo_ifdebug(true, "CHECKING_ROUTE_STRU: " . trim(basename($line)) . "+scr: exists<br>");
- if (left($line,7)==="Private") {
- if (!is_readable(APP_PRIVATE_PATH. trim(substr($line, 7)))) {
- return $ret;
- }
- } else {
- if (!is_readable(APP_PUBLIC_PATH . trim(substr($line, 6)))) {
- return $ret;
- }
- }
- }
- //echo_ifdebug(true, "CHECKING_ROUTE_STRU: +CONTENT: ok<br>");
-
- // Check for +REQUIRING
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+REQUIRING")) {
- return $ret;
- }
- // Check route dependencies list
- $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+REQUIRING");
- foreach($af as &$line) {
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $line)) {
- return $ret;
- }
- }
- echo_ifdebug(true, "CHECKING_ROUTE_STRU: +REQUIRING: ok<br>");
- return true;
- }
- function SC_CHECK_ROUTE_DEP($route) {
- $ret = false;
- echo_ifdebug(true, "CHECKING_ROUTE_DEP: Start<br>");
- // Check for +CONTENT
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT")) {
- return $ret;
- }
- // echo_ifdebug(true, "CHECKING_ROUTE_DEP: +CONTENT: exists<br>");
- //
- // Check file dependencies list
- //
- //
- // index-scrs/:
- // func.various.inc+scr:
- // partial path
- // size
- // sha
- // style.css+scr
- // partial path
- // size
- // sha
- //
- // Private/routes/test/test.php
- // Private/functions/func.various.inc
- // Public/static/css/style.css
- // Public/static/res/logot.png
- //
-
- $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT");
- foreach($af as &$line) {
- if (is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/" . trim(basename($line)) . "+scr")) {
- $asf = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/" . trim(basename($line)) . "+scr");
- $size = trim($asf[1]);
- $sha = trim($asf[2]);
- if (left($line,7)==="Private") {
- $filePath = APP_PRIVATE_PATH . trim(substr($line, 7));
- } else {
- $filePath = APP_PUBLIC_PATH . trim(substr($line, 6));
- }
- if (filesize($filePath) != $size) {
- return $ret;
- }
-
- if (hash("sha256", file_get_contents($filePath), false) !== $sha) {
- return $ret;
- }
- echo_ifdebug(true, "CHECKING_ROUTE_DEP: " . trim(basename($line)) . ": recognized<br>");
- } else {
- return $ret;
- }
- }
- return true;
- }
- function SC_CHECK_ROUTE_RES($route) {
- $ret = false;
- $res = [];
-
- echo_ifdebug(true, "CHECKING_ROUTE_RES: Start<br>");
- // Check for +CONTENT
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT")) {
- return $ret;
- }
- //echo_ifdebug(true, "CHECKING_ROUTE_RES: +CONTENT: exists<br>");
- $af = file(APP_PRIVATE_PATH . "/routes/" . $route . "/" . APP_SECRETS_FOLDER . "/+CONTENT");
- if (left($af[0],7)==="Private") {
- $routeFilePath = APP_PRIVATE_PATH . trim(substr($af[0], 7));
- } else {
- $routeFilePath = APP_PUBLIC_PATH . trim(substr($af[0], 6));
- }
- $acf = file_get_contents($routeFilePath);
-
- // ANALYZING HEADER..
- preg_match_all("/<head>.*<\/head>/s", $acf, $c, PREG_PATTERN_ORDER);
-
- //print_r($c[0][0]); // html header
- preg_match_all('/\/js\/.+\.js/s', $c[0][0], $res, PREG_PATTERN_ORDER);
- if (!empty($res[0])) {
- foreach($res[0] as $item) {
- if (!in_array("Public".$item.PHP_EOL,$af)) {
- return $ret;
- }
- echo_ifdebug(true, "CHECKING_ROUTE_RES: $item in " . basename($routeFilePath) . ": recognized<br>");
- }
- }
- preg_match_all('/\/css\/.+\.css/s', $c[0][0], $res, PREG_PATTERN_ORDER);
- //print_r($res);
- if (!empty($res)) {
- foreach($res[0] as $item) {
- //echo("Public".$item);
- if (!in_array("Public".$item.PHP_EOL,$af)) {
- return $ret;
- }
- echo_ifdebug(true, "CHECKING_ROUTE_RES: $item in " . basename($routeFilePath) . ": recognized<br>");
- }
- }
- //print_r($res[0]);
- // ANALYZING BODY..
- preg_match_all("/<body>.*<\/body>/s", $acf, $c, PREG_PATTERN_ORDER);
-
- //print_r($c[0][0]); // html body
- preg_match_all('/\/res\/.+\.png/s', $c[0][0], $res, PREG_PATTERN_ORDER);
- if (!empty($res[0])) {
- foreach($res[0] as $item) {
- //echo("Public".$item);
- if (!in_array("Public".$item.PHP_EOL,$af)) {
- return $ret;
- }
- echo_ifdebug(true, "CHECKING_ROUTE_RES: $item in " . basename($routeFilePath) . ": recognized<br>");
- }
- }
- return true;
- }
- function SC_CHECK_ROUTE_ALL($route) {
- $ret = 200;
- if (!is_readable(APP_PRIVATE_PATH . "/routes/" . $route)) {
- $ret = 404;
- return $ret;
- }
- if (!SC_CHECK_ROUTE_STRU($route) || !SC_CHECK_ROUTE_DEP($route) || !SC_CHECK_ROUTE_RES($route)) {
- $ret = 502;
- return $ret;
- }
- return $ret;
- }
|