Init.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Widget;
  3. use Typecho\Common;
  4. use Typecho\Cookie;
  5. use Typecho\Date;
  6. use Typecho\Db;
  7. use Typecho\I18n;
  8. use Typecho\Plugin;
  9. use Typecho\Response;
  10. use Typecho\Router;
  11. use Typecho\Widget;
  12. if (!defined('__TYPECHO_ROOT_DIR__')) {
  13. exit;
  14. }
  15. /**
  16. * 初始化模块
  17. *
  18. * @package Widget
  19. */
  20. class Init extends Widget
  21. {
  22. /**
  23. * 入口函数,初始化路由器
  24. *
  25. * @access public
  26. * @return void
  27. * @throws Db\Exception
  28. */
  29. public function execute()
  30. {
  31. /** 初始化exception */
  32. if (!defined('__TYPECHO_DEBUG__') || !__TYPECHO_DEBUG__) {
  33. set_exception_handler(function (\Throwable $exception) {
  34. Response::getInstance()->clean();
  35. ob_end_clean();
  36. ob_start(function ($content) {
  37. Response::getInstance()->sendHeaders();
  38. return $content;
  39. });
  40. if (404 == $exception->getCode()) {
  41. ExceptionHandle::alloc();
  42. } else {
  43. Common::error($exception);
  44. }
  45. exit;
  46. });
  47. }
  48. // init class
  49. define('__TYPECHO_CLASS_ALIASES__', [
  50. 'Typecho_Plugin_Interface' => '\Typecho\Plugin\PluginInterface',
  51. 'Typecho_Widget_Helper_Empty' => '\Typecho\Widget\Helper\EmptyClass',
  52. 'Typecho_Db_Adapter_Mysql' => '\Typecho\Db\Adapter\Mysqli',
  53. 'Widget_Abstract' => '\Widget\Base',
  54. 'Widget_Abstract_Contents' => '\Widget\Base\Contents',
  55. 'Widget_Abstract_Comments' => '\Widget\Base\Comments',
  56. 'Widget_Abstract_Metas' => '\Widget\Base\Metas',
  57. 'Widget_Abstract_Options' => '\Widget\Base\Options',
  58. 'Widget_Abstract_Users' => '\Widget\Base\Users',
  59. 'Widget_Metas_Category_List' => '\Widget\Metas\Category\Rows',
  60. 'Widget_Contents_Page_List' => '\Widget\Contents\Page\Rows',
  61. 'Widget_Plugins_List' => '\Widget\Plugins\Rows',
  62. 'Widget_Themes_List' => '\Widget\Themes\Rows',
  63. 'Widget_Interface_Do' => '\Widget\ActionInterface',
  64. 'Widget_Do' => '\Widget\Action',
  65. 'AutoP' => '\Utils\AutoP',
  66. 'PasswordHash' => '\Utils\PasswordHash',
  67. 'Markdown' => '\Utils\Markdown',
  68. 'HyperDown' => '\Utils\HyperDown',
  69. 'Helper' => '\Utils\Helper',
  70. 'Upgrade' => '\Utils\Upgrade'
  71. ]);
  72. /** 对变量赋值 */
  73. $options = Options::alloc();
  74. /** 语言包初始化 */
  75. if ($options->lang && $options->lang != 'zh_CN') {
  76. $dir = defined('__TYPECHO_LANG_DIR__') ? __TYPECHO_LANG_DIR__ : __TYPECHO_ROOT_DIR__ . '/usr/langs';
  77. I18n::setLang($dir . '/' . $options->lang . '.mo');
  78. }
  79. /** 备份文件目录初始化 */
  80. if (!defined('__TYPECHO_BACKUP_DIR__')) {
  81. define('__TYPECHO_BACKUP_DIR__', __TYPECHO_ROOT_DIR__ . '/usr/backups');
  82. }
  83. /** cookie初始化 */
  84. Cookie::setPrefix($options->rootUrl);
  85. if (defined('__TYPECHO_COOKIE_OPTIONS__')) {
  86. Cookie::setOptions(__TYPECHO_COOKIE_OPTIONS__);
  87. }
  88. /** 初始化路由器 */
  89. Router::setRoutes($options->routingTable);
  90. /** 初始化插件 */
  91. Plugin::init($options->plugins);
  92. /** 初始化回执 */
  93. $this->response->setCharset($options->charset);
  94. $this->response->setContentType($options->contentType);
  95. /** 初始化时区 */
  96. Date::setTimezoneOffset($options->timezone);
  97. /** 开始会话, 减小负载只针对后台打开session支持 */
  98. if ($options->installed && User::alloc()->hasLogin()) {
  99. @session_start();
  100. }
  101. }
  102. }