1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21:
22: if (!defined('DEBUG')) {
23: define('DEBUG', 0);
24: }
25:
26: if (DEBUG > 0) {
27:
28: ini_set('display_errors', 1);
29: ini_set('display_startup_errors', 1);
30: error_reporting(-1);
31: } else {
32:
33: error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
34: }
35: 36: 37: 38: 39:
40: define('VERSION', '4.0.0');
41: 42: 43: 44: 45: 46:
47: if (!defined('DB_LOG')) {
48: define('DB_LOG', false);
49: }
50: 51: 52:
53: $vendorDir = str_replace('load.php', '', __FILE__);
54: if (DIRECTORY_SEPARATOR != '/') {
55: $vendorDir = str_replace('\\', '/', $vendorDir);
56: }
57: define('VENDOR_DIR', $vendorDir);
58: 59: 60:
61: $docRoot = dirname($vendorDir);
62: if (!defined('ROOT_PATH')) {
63: define('ROOT_PATH', $docRoot.'/');
64: }
65: 66: 67:
68: $contextPrefix = '';
69: if (isset($_SERVER['APPL_PHYSICAL_PATH'])) {
70: $docRoot = rtrim(realpath($_SERVER['APPL_PHYSICAL_PATH']), DIRECTORY_SEPARATOR);
71: if (DIRECTORY_SEPARATOR != '/' && $docRoot != '') {
72: $docRoot = str_replace('\\', '/', $docRoot);
73: }
74: } elseif (strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) !== false) {
75: $docRoot = rtrim(realpath($_SERVER['DOCUMENT_ROOT']), DIRECTORY_SEPARATOR);
76: if (DIRECTORY_SEPARATOR != '/' && $docRoot != '') {
77: $docRoot = str_replace('\\', '/', $docRoot);
78: }
79: } else {
80: $dir = basename($docRoot);
81: $ds = explode($dir, dirname($_SERVER['SCRIPT_NAME']), 2);
82: if (count($ds) > 1) {
83: $contextPrefix = $ds[0].$dir;
84: $appPath = $ds[1];
85: if (DIRECTORY_SEPARATOR != '/') {
86: $contextPrefix = str_replace('\\', '/', $contextPrefix);
87: }
88: }
89: if (!defined('APP_PATH')) {
90: define('APP_PATH', $docRoot.$appPath.'/');
91: }
92: if (!defined('BASE_PATH')) {
93: define('BASE_PATH', $contextPrefix.$appPath.'/');
94: }
95: }
96:
97: 98: 99:
100: if (!defined('APP_PATH')) {
101: $appPath = dirname($_SERVER['SCRIPT_NAME']);
102: if (DIRECTORY_SEPARATOR != '/') {
103: $appPath = str_replace('\\', '/', $appPath);
104: }
105: define('APP_PATH', rtrim($docRoot.$appPath, '/').'/');
106: }
107: 108: 109:
110: if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
111: $scheme = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
112: } elseif ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) {
113: $scheme = 'https://';
114: } else {
115: $scheme = 'http://';
116: }
117: if (!defined('HTTPS')) {
118: define('HTTPS', $scheme == 'https://');
119: }
120: 121: 122:
123: if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
124: $host = trim(current(explode(',', $_SERVER['HTTP_X_FORWARDED_HOST'])));
125: } elseif (empty($_SERVER['HTTP_HOST'])) {
126: $host = $_SERVER['SERVER_NAME'];
127: } else {
128: $host = $_SERVER['HTTP_HOST'];
129: }
130: if (!defined('HOST')) {
131: define('HOST', str_replace('www.', '', $host));
132: }
133: 134: 135: 136:
137: if (!defined('BASE_PATH')) {
138: if (empty($_SERVER['CONTEXT_DOCUMENT_ROOT'])) {
139: define('BASE_PATH', str_replace($docRoot, '', APP_PATH));
140: } else {
141: $basePath = str_replace($_SERVER['CONTEXT_DOCUMENT_ROOT'], '', dirname($_SERVER['SCRIPT_NAME']));
142: if (DIRECTORY_SEPARATOR != '/') {
143: $basePath = str_replace('\\', '/', $basePath);
144: }
145: define('BASE_PATH', rtrim($basePath, '/').'/');
146: }
147: }
148: 149: 150:
151: if (!defined('WEB_URL')) {
152: define('WEB_URL', $scheme.$host.$contextPrefix.str_replace($docRoot, '', ROOT_PATH));
153: }
154: 155: 156: 157: 158: 159: 160:
161: if (!defined('TOKEN_LIMIT')) {
162: define('TOKEN_LIMIT', 10);
163: }
164: 165: 166: 167: 168: 169:
170: if (!defined('TOKEN_AGE')) {
171: define('TOKEN_AGE', 3600);
172: }
173:
174: 175: 176: 177: 178: 179: 180: 181:
182: function createClass($className, $param = null)
183: {
184: return new $className($param);
185: }
186:
187: 188: 189: 190: 191:
192: function debug($expression)
193: {
194: if (is_array($expression) || is_object($expression)) {
195: $content = json_encode((array) $expression);
196: } else {
197: $content = '"'.str_replace(array('/', '"'), array('\/', '\"'), $expression).'"';
198: }
199: echo '<script>console.log('.$content.')</script>';
200: }
201: 202: 203: 204:
205: if (DEBUG != 2) {
206: set_error_handler(function ($errno, $errstr, $errfile, $errline) {
207: switch ($errno) {
208: case E_WARNING:
209: $type = 'PHP warning';
210: break;
211: case E_NOTICE:
212: $type = 'PHP notice';
213: break;
214: case E_USER_ERROR:
215: $type = 'User error';
216: break;
217: case E_USER_WARNING:
218: $type = 'User warning';
219: break;
220: case E_USER_NOTICE:
221: $type = 'User notice';
222: break;
223: case E_RECOVERABLE_ERROR:
224: $type = 'Recoverable error';
225: break;
226: default:
227: $type = 'PHP Error';
228: }
229: \Kotchasan\Log\Logger::create()->error('<br>'.$type.' : <em>'.$errstr.'</em> in <b>'.$errfile.'</b> on line <b>'.$errline.'</b>');
230: });
231: set_exception_handler(function ($e) {
232: $tract = $e->getTrace();
233: if (empty($tract)) {
234: $tract = array(
235: 'file' => $e->getFile(),
236: 'line' => $e->getLine(),
237: );
238: } else {
239: $tract = next($tract);
240: }
241: \Kotchasan\Log\Logger::create()->error('<br>Exception : <em>'.$e->getMessage().'</em> in <b>'.$tract['file'].'</b> on line <b>'.$tract['line'].'</b>');
242: });
243: }
244:
245: 246: 247: 248: 249: 250: 251:
252: function getClassPath($className)
253: {
254: if (preg_match_all('/([\/\\])([a-zA-Z0-9]+)/', $className, $match)) {
255: $className = implode(DIRECTORY_SEPARATOR, $match[1]).'.php';
256: if (is_file(ROOT_PATH.$className)) {
257: return ROOT_PATH.$className;
258: } elseif (isset($match[1][2])) {
259: if (isset($match[1][3])) {
260: $className = strtolower('modules'.DIRECTORY_SEPARATOR.$match[1][0].DIRECTORY_SEPARATOR.$match[1][3].'s'.DIRECTORY_SEPARATOR.$match[1][1].DIRECTORY_SEPARATOR.$match[1][2].'.php');
261: } else {
262: $className = strtolower('modules'.DIRECTORY_SEPARATOR.$match[1][0].DIRECTORY_SEPARATOR.$match[1][2].'s'.DIRECTORY_SEPARATOR.$match[1][1].'.php');
263: }
264: if (is_file(APP_PATH.$className)) {
265: return APP_PATH.$className;
266: } elseif (is_file(ROOT_PATH.$className)) {
267: return ROOT_PATH.$className;
268: }
269: }
270: }
271:
272: return null;
273: }
274: 275: 276: 277: 278: 279:
280: spl_autoload_register(function ($className) {
281: $files = array(
282: 'Kotchasan\Cache\ApcCache' => 'Cache/ApcCache.php',
283: 'Kotchasan\Cache\Cache' => 'Cache/Cache.php',
284: 'Kotchasan\Cache\CacheItem' => 'Cache/CacheItem.php',
285: 'Kotchasan\Cache\Exception' => 'Cache/Exception.php',
286: 'Kotchasan\Cache\FileCache' => 'Cache/FileCache.php',
287: 'Kotchasan\Database\Db' => 'Database/Db.php',
288: 'Kotchasan\Database\DbCache' => 'Database/DbCache.php',
289: 'Kotchasan\Database\Driver' => 'Database/Driver.php',
290: 'Kotchasan\Database\Exception' => 'Database/Exception.php',
291: 'Kotchasan\Database\PdoMysqlDriver' => 'Database/PdoMysqlDriver.php',
292: 'Kotchasan\Database\Query' => 'Database/Query.php',
293: 'Kotchasan\Database\QueryBuilder' => 'Database/QueryBuilder.php',
294: 'Kotchasan\Database\Schema' => 'Database/Schema.php',
295: 'Kotchasan\Database\Sql' => 'Database/Sql.php',
296: 'Kotchasan\Http\Message' => 'Http/Message.php',
297: 'Kotchasan\Http\NotFound' => 'Http/NotFound.php',
298: 'Kotchasan\Http\Response' => 'Http/Response.php',
299: 'Kotchasan\Http\Stream' => 'Http/Stream.php',
300: 'Kotchasan\Http\UploadedFile' => 'Http/UploadedFile.php',
301: 'Kotchasan\Log\AbstractLogger' => 'Log/AbstractLogger.php',
302: 'Kotchasan\Log\Logger' => 'Log/Logger.php',
303: 'Kotchasan\Orm\Field' => 'Orm/Field.php',
304: 'Kotchasan\Orm\Recordset' => 'Orm/Recordset.php',
305: 'Kotchasan\PHPMailer\class' => 'PHPMailer/class.php',
306: 'Kotchasan\PHPMailer\class.smtp' => 'PHPMailer/class.smtp.php',
307: 'Psr\Cache\CacheItemInterface' => 'Psr/Cache/CacheItemInterface.php',
308: 'Psr\Cache\CacheItemPoolInterface' => 'Psr/Cache/CacheItemPoolInterface.php',
309: 'Psr\Http\Message\ResponseInterface' => 'Psr/Http/Message/ResponseInterface.php',
310: 'Psr\Http\Message\ServerRequestInterface' => 'Psr/Http/Message/ServerRequestInterface.php',
311: 'Psr\Http\Message\StreamInterface' => 'Psr/Http/Message/StreamInterface.php',
312: 'Psr\Http\Message\UploadedFileInterface' => 'Psr/Http/Message/UploadedFileInterface.php',
313: 'Psr\Log\AbstractLogger' => 'Psr/Log/AbstractLogger.php',
314: 'Psr\Log\LogLevel' => 'Psr/Log/LogLevel.php',
315: 'Psr\Log\LoggerAwareInterface' => 'Psr/Log/LoggerAwareInterface.php',
316: 'Psr\Log\LoggerInterface' => 'Psr/Log/LoggerInterface.php',
317: 'Psr\Log\LoggerTrait' => 'Psr/Log/LoggerTrait.php',
318: 'Psr\Log\NullLogger' => 'Psr/Log/NullLogger.php',
319: 'Kotchasan\Accordion' => 'Accordion.php',
320: 'Kotchasan\ArrayTool' => 'ArrayTool.php',
321: 'Kotchasan\CKEditor' => 'CKEditor.php',
322: 'Kotchasan\Collection' => 'Collection.php',
323: 'Kotchasan\Country' => 'Country.php',
324: 'Kotchasan\Csv' => 'Csv.php',
325: 'Kotchasan\Curl' => 'Curl.php',
326: 'Kotchasan\Currency' => 'Currency.php',
327: 'Kotchasan\DOMNode' => 'DOMNode.php',
328: 'Kotchasan\DOMParser' => 'DOMParser.php',
329: 'Kotchasan\DataTable' => 'DataTable.php',
330: 'Kotchasan\Database' => 'Database.php',
331: 'Kotchasan\Date' => 'Date.php',
332: 'Kotchasan\Email' => 'Email.php',
333: 'Kotchasan\File' => 'File.php',
334: 'Kotchasan\Files' => 'Files.php',
335: 'Kotchasan\Form' => 'Form.php',
336: 'Kotchasan\Grid' => 'Grid.php',
337: 'Kotchasan\Html' => 'Html.php',
338: 'Kotchasan\HtmlTable' => 'HtmlTable.php',
339: 'Kotchasan\Htmldoc' => 'Htmldoc.php',
340: 'Kotchasan\Image' => 'Image.php',
341: 'Kotchasan\InputItem' => 'InputItem.php',
342: 'Kotchasan\Inputs' => 'Inputs.php',
343: 'Kotchasan\Language' => 'Language.php',
344: 'Kotchasan\ListItem' => 'ListItem.php',
345: 'Kotchasan\Login' => 'Login.php',
346: 'Kotchasan\Menu' => 'Menu.php',
347: 'Kotchasan\Mime' => 'Mime.php',
348: 'Kotchasan\Model' => 'Model.php',
349: 'Kotchasan\Number' => 'Number.php',
350: 'Kotchasan\Password' => 'Password.php',
351: 'Kotchasan\Pdf' => 'Pdf.php',
352: 'Kotchasan\Province' => 'Province.php',
353: 'Kotchasan\Singleton' => 'Singleton.php',
354: 'Kotchasan\Tab' => 'Tab.php',
355: 'Kotchasan\Template' => 'Template.php',
356: 'Kotchasan\Text' => 'Text.php',
357: 'Kotchasan\Validator' => 'Validator.php',
358: 'Kotchasan\View' => 'View.php',
359: 'Kotchasan\load' => 'load.php',
360: 'Kotchasan\ObjectTool' => 'ObjectTool.php',
361: 'Kotchasan\InputItemException' => 'InputItemException.php',
362: );
363: if (isset($files[$className])) {
364: $file = VENDOR_DIR.$files[$className];
365: } else {
366: $file = getClassPath($className);
367: }
368: if ($file !== null) {
369: require $file;
370: }
371: });
372:
373: 374: 375:
376: require VENDOR_DIR.'KBase.php';
377: require VENDOR_DIR.'Config.php';
378: require VENDOR_DIR.'Psr/Http/Message/MessageInterface.php';
379: require VENDOR_DIR.'Psr/Http/Message/RequestInterface.php';
380: require VENDOR_DIR.'Psr/Http/Message/UriInterface.php';
381: require VENDOR_DIR.'Http/AbstractMessage.php';
382: require VENDOR_DIR.'Http/AbstractRequest.php';
383: require VENDOR_DIR.'Http/Request.php';
384: require VENDOR_DIR.'Http/Uri.php';
385: require VENDOR_DIR.'Router.php';
386: require VENDOR_DIR.'Kotchasan.php';
387: require VENDOR_DIR.'Controller.php';
388: