From 8c8b61dd295c577ec33a2cdc4b305a619e26f855 Mon Sep 17 00:00:00 2001 From: Oreolek Date: Sat, 15 Oct 2011 12:57:18 +0700 Subject: [PATCH] Added error handling. Also in development mode now you can install the database - provided you configured Kohana. --- application/bootstrap.php | 17 ++++--- application/classes/controller/error.php | 33 ++++++++++++++ application/classes/controller/header.php | 2 +- application/classes/controller/install.php | 25 +++++++++++ application/classes/kohana/exception.php | 40 +++++++++++++++++ application/config/database.php | 52 +++++++++++----------- application/database.sql | 15 +++++++ application/views/install.php | 15 +++++++ index.php | 1 + system | 2 +- 10 files changed, 169 insertions(+), 33 deletions(-) create mode 100644 application/classes/controller/error.php create mode 100644 application/classes/controller/install.php create mode 100644 application/classes/kohana/exception.php create mode 100644 application/database.sql create mode 100644 application/views/install.php diff --git a/application/bootstrap.php b/application/bootstrap.php index 2c6404e..88bb1ba 100644 --- a/application/bootstrap.php +++ b/application/bootstrap.php @@ -82,7 +82,9 @@ if (isset($_SERVER['KOHANA_ENV'])) Kohana::init(array( 'base_url' => '/', 'index_file' => FALSE, - 'errors' => TRUE + 'errors' => TRUE, + 'profile' => (Kohana::$environment == Kohana::DEVELOPMENT), + 'caching' => (Kohana::$environment == Kohana::PRODUCTION) )); /** @@ -113,8 +115,13 @@ Kohana::modules(array( * Set the routes. Each route must have a minimum of a name, a URI and a set of * defaults for the URI. */ +Route::set('error', 'error/(/)', array('action' => '[0-9]++','message' => '.+')) + ->defaults(array( + 'controller' => 'error', +)); + Route::set('default', '((/(/)))') - ->defaults(array( - 'controller' => 'copyright', - 'action' => 'view', - )); + ->defaults(array( + 'controller' => 'copyright', + 'action' => 'view', + )); diff --git a/application/classes/controller/error.php b/application/classes/controller/error.php new file mode 100644 index 0000000..678fdbe --- /dev/null +++ b/application/classes/controller/error.php @@ -0,0 +1,33 @@ +request->is_initial()) $this->request->action(404); + $this->response->status((int) $this->request->action()); + } + + /** + * Serves HTTP 404 error page + */ +//адрес страницы не выводится + public function action_404() { + $this->template->title = 'Страница не найдена'; + $this->template->description = 'Запрошенная вами страница не найдена. Скорее всего, это была просто опечатка. Проверьте строку адреса.'; + } + + /** + * Serves HTTP 500 error page + */ + public function action_500() { + $this->template->description = 'Произошла внутренняя ошибка. Не волнуйтесь, её должны скоро исправить.'; + $this->template->title ='Внутренняя ошибка сервера'; + } +} \ No newline at end of file diff --git a/application/classes/controller/header.php b/application/classes/controller/header.php index cdc481f..126b36a 100644 --- a/application/classes/controller/header.php +++ b/application/classes/controller/header.php @@ -11,7 +11,7 @@ class Controller_Header extends Controller_Template { foreach($styles as $style=>$media): if ($media != 'screen' and $media != 'print'){ $style=$media; - $media="screen"; + $media='screen'; } $temp .= ''."\n"; endforeach; diff --git a/application/classes/controller/install.php b/application/classes/controller/install.php new file mode 100644 index 0000000..40d1b25 --- /dev/null +++ b/application/classes/controller/install.php @@ -0,0 +1,25 @@ +request->redirect(''); + if ($_POST){ + if ($this->request->post('create')){ + DB::query(NULL,'CREATE DATABASE '.$database.';')->execute(); + } + $queries = fopen(Kohana::find_file('', 'database', 'sql'), "r"); + while (!feof($queries)) { + $buffer = fgets($queries);//btw: by default reads 1kb of string! + DB::query(NULL,$buffer)->execute(); + } + fclose($queries); + $user = ORM::factory('user')->values(arr::extract($this->request->post(), array('username', 'password', 'email', 'password_confirm'))); + $user->create(); + $login_role = new Model_Role(array('name' =>'login')); + $admin_role = new Model_Role(array('name' =>'admin')); + $user->add('roles',$admin_role); + $user->add('roles',$login_role); + } + } +} diff --git a/application/classes/kohana/exception.php b/application/classes/kohana/exception.php new file mode 100644 index 0000000..2f73ad8 --- /dev/null +++ b/application/classes/kohana/exception.php @@ -0,0 +1,40 @@ +add(Log::ERROR, Kohana_Exception::text($e)); + + $attributes = array( + 'action' => 500, + 'message' => rawurlencode($e->getMessage()) + ); + + if ($e instanceof Http_Exception) { + $attributes['action'] = $e->getCode(); + } + + // Error sub request + echo Request::factory(Route::get('error')->uri($attributes)) + ->execute() + ->send_headers() + ->body(); + } + catch (Exception $e){ + // Clean the output buffer if one exists + ob_get_level() and ob_clean(); + + // Display the exception text + echo parent::text($e); + + // Exit with an error status + exit(1); + } + } + } +} \ No newline at end of file diff --git a/application/config/database.php b/application/config/database.php index 4882766..f1d852a 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -2,30 +2,30 @@ return array ( - 'default' => array - ( - 'type' => 'mysql', - 'connection' => array( - /** - * The following options are available for MySQL: - * - * string hostname server hostname, or socket - * string database database name - * string username database username - * string password database password - * boolean persistent use persistent connections? - * - * Ports and sockets may be appended to the hostname. - */ - 'hostname' => 'localhost', - 'database' => 'nemove', - 'username' => 'dandelion', - 'password' => '', - 'persistent' => FALSE, - ), - 'table_prefix' => '', - 'charset' => 'utf8', - 'caching' => FALSE, - 'profiling' => TRUE, - ), + 'default' => array + ( + 'type' => 'mysql', + 'connection' => array( + /** + * The following options are available for MySQL: + * + * string hostname server hostname, or socket + * string database database name + * string username database username + * string password database password + * boolean persistent use persistent connections? + * + * Ports and sockets may be appended to the hostname. + */ + 'hostname' => 'localhost', + 'database' => 'nemove', + 'username' => 'dandelion', + 'password' => '', + 'persistent' => FALSE, + ), + 'table_prefix' => '', + 'charset' => 'utf8', + 'caching' => TRUE, + 'profiling' => FALSE, + ), ); diff --git a/application/database.sql b/application/database.sql new file mode 100644 index 0000000..6f81fa5 --- /dev/null +++ b/application/database.sql @@ -0,0 +1,15 @@ +SET FOREIGN_KEY_CHECKS = 0; +DROP TABLE IF EXISTS `roles_users`; +DROP TABLE IF EXISTS `roles`; +DROP TABLE IF EXISTS `users`; +DROP TABLE IF EXISTS `user_tokens`; +SET FOREIGN_KEY_CHECKS = 1; +CREATE TABLE IF NOT EXISTS `roles` (`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,`name` varchar(32) NOT NULL,`description` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uniq_name` (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; +INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation'); +INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.'); +INSERT INTO `roles` (`id`, `name`, `description`) VALUES(3, 'author', 'Uploads games'); +CREATE TABLE IF NOT EXISTS `roles_users` (`user_id` int(10) UNSIGNED NOT NULL,`role_id` int(10) UNSIGNED NOT NULL, PRIMARY KEY (`user_id`,`role_id`), KEY `fk_role_id` (`role_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `users` (`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,`email` varchar(127) NOT NULL, `username` varchar(32) NOT NULL DEFAULT '', `password` char(50) NOT NULL, `logins` int(10) UNSIGNED NOT NULL DEFAULT '0', `last_login` int(10) UNSIGNED, PRIMARY KEY (`id`), UNIQUE KEY `uniq_username` (`username`), UNIQUE KEY `uniq_email` (`email`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `user_tokens` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` int(11) UNSIGNED NOT NULL, `user_agent` varchar(40) NOT NULL, `token` varchar(32) NOT NULL, `created` int(10) UNSIGNED NOT NULL, `expires` int(10) UNSIGNED NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uniq_token` (`token`), KEY `fk_user_id` (`user_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; +ALTER TABLE `roles_users` ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, ADD CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE; +ALTER TABLE `user_tokens` ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE; \ No newline at end of file diff --git a/application/views/install.php b/application/views/install.php new file mode 100644 index 0000000..5154605 --- /dev/null +++ b/application/views/install.php @@ -0,0 +1,15 @@ +post('title','Установка движка')->post('styles','main.css')->execute() ?> +

Установка движка

+

Внимание! При продолжении база данных будет переписана.

+

Предполагается, что база данных MySQL находится по адресу localhost и использует кодировку UTF-8. Вы можете поменять настройки в файле config/database.php.

+ +

+

+

+

+

+

+

+

+ + execute() ?> \ No newline at end of file diff --git a/index.php b/index.php index 71c3249..3fba4bf 100644 --- a/index.php +++ b/index.php @@ -45,6 +45,7 @@ define('EXT', '.php'); * deprecated notices. Disable with: E_ALL & ~E_DEPRECATED */ error_reporting(E_ALL | E_STRICT); +ini_set('display_errors', 'On'); /** * End of standard configuration! Changing any of the code below should only be diff --git a/system b/system index ed12c18..4419480 160000 --- a/system +++ b/system @@ -1 +1 @@ -Subproject commit ed12c18fdd2182e1cf0859860af80f388b23b1da +Subproject commit 4419480faec65835f4b989cf7a73d76b4dc60756