Mercurial
comparison .cms/index.php @ 1:1d486627aa1e draft default tip
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Sat, 12 Oct 2024 02:51:39 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:78edf6b517a0 | 1:1d486627aa1e |
---|---|
1 <?php | |
2 | |
3 /* | |
4 $time_start = microtime( true ); | |
5 */ | |
6 | |
7 // for bad hosters | |
8 ini_set( "display_errors", "Off" ); | |
9 | |
10 $cms["kernel_version"] = "24.10"; | |
11 $cms["kernel_compat"] = "23.04"; | |
12 | |
13 // PHP_VERSION_ID available in PHP 5.2.7 and above | |
14 if ( ! defined( "PHP_VERSION_ID" ) ) { | |
15 $version = explode( ".", PHP_VERSION ); | |
16 define( "PHP_VERSION_ID", ( $version[0] * 10000 + $version[1] * 100 + $version[2] ) ); | |
17 } | |
18 | |
19 $cms["cms_dir"] = dirname( __FILE__ ); | |
20 | |
21 // for windows | |
22 if ( DIRECTORY_SEPARATOR === "\\" ) { | |
23 $cms["cms_dir"] = str_replace( "\\", "/", $cms["cms_dir"] ); | |
24 } | |
25 | |
26 $cms["site_dir"] = dirname( $cms["cms_dir"] ); | |
27 | |
28 if ( empty( $_SERVER["HTTPS"] ) ) { | |
29 $protocol = "http"; | |
30 } else { | |
31 $protocol = "https"; | |
32 } | |
33 | |
34 // if tail DocumentRoot containing / or \ | |
35 $_SERVER["DOCUMENT_ROOT"] = rtrim( $_SERVER["DOCUMENT_ROOT"], "/\\" ); | |
36 | |
37 // https://coffee-cms.com/base_path/ | |
38 // ^ ^ | |
39 $cms["base_path"] = str_replace( $_SERVER["DOCUMENT_ROOT"], "", $cms["site_dir"] ) . "/"; | |
40 $regex_base_path = str_replace( "/", "\\/", $cms["base_path"] ); // escape / for regexp | |
41 | |
42 //#todo? It seems there is no point in adding /u for the URL and checking preg_replace for null | |
43 $url = "{$protocol}://{$_SERVER['SERVER_NAME']}/" . preg_replace( "/^{$regex_base_path}/", "", $_SERVER['REQUEST_URI'] ); | |
44 $cms["url"] = parse_url( $url ); | |
45 $cms["url"]["path"] = urldecode( $cms["url"]["path"] ); | |
46 // file in .cms/ directory (for prevent sql query in pages module) | |
47 $cms["cms_file"] = $cms["cms_dir"] . $cms["url"]["path"]; | |
48 $cms["url"]["path"] = substr( $cms["url"]["path"], 1 ); // remove leading / | |
49 | |
50 $cms["status"] = "404"; | |
51 | |
52 | |
53 // Load config.php | |
54 $cms["config_file"] = "{$cms['cms_dir']}/config.php"; | |
55 if ( is_file( $cms["config_file"] ) && $handle = fopen( $cms["config_file"], "r" ) ) { | |
56 flock( $handle, LOCK_SH ); | |
57 include( $cms["config_file"] ); | |
58 flock( $handle, LOCK_UN ); | |
59 } | |
60 | |
61 | |
62 if ( ! empty( $cms["config"]["locale"] ) ) { | |
63 setlocale( LC_ALL, $cms["config"]["locale"] ); | |
64 } | |
65 | |
66 | |
67 if ( ! empty( $cms["config"]["timezone"] ) ) { | |
68 date_default_timezone_set ( $cms["config"]["timezone"] ); | |
69 } | |
70 | |
71 | |
72 // Init hooks | |
73 // Отключение производится добавлением disable => true рядом с next | |
74 $cms["hook"] = "query"; // Default hook | |
75 $cms["hooks"] = array( | |
76 "query" => array( "next" => "template" ), | |
77 "template" => array( "next" => "echo" ), | |
78 "echo" => array( "next" => "write" ), | |
79 "write" => array( "next" => "" ), | |
80 "admin" => array( "next" => "template" ), | |
81 "api" => array( "next" => "" ), | |
82 "cron" => array( "next" => "" ), | |
83 ); | |
84 | |
85 // route array | |
86 $cms["urls"] = array(); | |
87 | |
88 // Load functions files | |
89 foreach( glob( $cms["cms_dir"] . "/*.fn.php" ) as $file ) { | |
90 include_once( $file ); | |
91 } | |
92 | |
93 // Load modules | |
94 foreach( glob( "{$cms['cms_dir']}/mod/*.mod.php" ) as $module ) { | |
95 include_once( $module ); | |
96 } | |
97 | |
98 // Load current template modules | |
99 if ( ! empty( $cms["config"]["template.mod.php"]["template"] ) ) { | |
100 foreach( glob( "{$cms['cms_dir']}/{$cms['config']['template.mod.php']['template']}/*.mod.php" ) as $module ) { | |
101 include_once( $module ); | |
102 } | |
103 } | |
104 | |
105 // Отсортировать маршруты чтобы короткие совпадения не перекрывали более длинные | |
106 function urls_sort( $a, $b ) { | |
107 if ( strlen( $a ) >= strlen( $b ) ) | |
108 return -1; | |
109 else | |
110 return 1; | |
111 } | |
112 uksort( $cms["urls"], "urls_sort" ); | |
113 | |
114 // select route | |
115 foreach( $cms["urls"] as $url => $hook ) { | |
116 // Нет смысла в /u для URL | |
117 if ( $url = preg_replace( "/\//", "\\/", $url ) ) { // Escape / for regexp | |
118 if ( preg_match( "/{$url}/", $cms["base_path"] . $cms["url"]["path"], $cms["url"]["match"] ) ) { | |
119 $cms["hook"] = $hook; | |
120 break; | |
121 } | |
122 } | |
123 } | |
124 | |
125 // RUN | |
126 while ( ! empty( $cms["hook"] ) ) { | |
127 do_hook( $cms["hook"] ); | |
128 $cms["hook"] = $cms["hooks"][ $cms["hook"] ]["next"]; | |
129 } | |
130 | |
131 // Saving accumulated debugging information to a file | |
132 if ( ! empty( $cms["config"]["debug"] ) && ! empty( $cms["debug"] ) ) { | |
133 $debug_file = $cms["cms_dir"] . "/debug.log.php"; | |
134 $new_debug = $cms["debug"]; | |
135 if ( is_file( $debug_file ) && $handle = fopen( $debug_file, "r" ) ) { | |
136 flock( $handle, LOCK_SH ); | |
137 include( $debug_file ); | |
138 flock( $handle, LOCK_UN ); | |
139 $cms["debug"] = array_replace_recursive( $cms["debug"], $new_debug ); | |
140 } | |
141 file_put_contents( $debug_file, '<?php $cms["debug"] = ' . var_export( $cms["debug"], true ) . ";\n", LOCK_EX ); | |
142 } | |
143 | |
144 /* Measuring speed and memory consumption | |
145 if ( $cms["url"]["path"] == $cms["config"]["admin.mod.php"]["api_url"] ) { | |
146 $fn = $_POST["fn"] . " "; | |
147 } else { | |
148 $fn = ""; | |
149 } | |
150 file_put_contents( __DIR__ . "/perf.log", | |
151 $cms["base_path"] . $cms["url"]["path"] . " " . | |
152 $fn . | |
153 (int)( ( microtime( true ) - $time_start ) * 1000 ) . "ms " . | |
154 (int)( memory_get_peak_usage() / 1000 ) . " kB\n", | |
155 FILE_APPEND ); | |
156 */ | |
157 //file_put_contents( __DIR__ . "/cms.log.php", '<?php $cms = ' . var_export( $cms, true ) . ";\n" ); |