Переглянути джерело

Settings, error return types

Nathaniel van Diepen 8 роки тому
батько
коміт
52b85f90f1

+ 0 - 4
config.php

@@ -4,8 +4,4 @@
 	define('DB_USER','bugs');
 	define('DB_PASS','bugs');
 	define('DB','bugs');
-	define('URL_BASE','/bugs/');
-	define('URL_HOST','localhost');
-	define('ADMIN_EMAIL','bugs@localhost');
-	define('DEFAULT_PRIORITY',4);
 ?>

+ 1 - 1
index.php

@@ -17,7 +17,7 @@
 		}
 	}
 	if(empty($_SERVER['REDIRECT_URL'])){
-		$_SERVER['REDIRECT_URL'] = 'http://'.URL_HOST.URL_BASE;
+		$_SERVER['REDIRECT_URL'] = 'http://'.Bugs::setting('url.host').Bugs::setting('url.base');
 	}
 	Router::handle($_SERVER['REDIRECT_URL'],null,function($res,$url){
 		trigger_error("Not implemented");

+ 35 - 0
install/db_install/00_tables/settings.sql

@@ -0,0 +1,35 @@
+-- --------------------------------------------------------
+
+--
+-- Table structure for table `settings`
+--
+-- Creation: Sep 02, 2015 at 11:35 AM
+--
+
+DROP TABLE IF EXISTS `settings`;
+CREATE TABLE IF NOT EXISTS `settings` (
+  `name` varchar(50) COLLATE utf8_bin NOT NULL,
+  `value` varchar(100) COLLATE utf8_bin NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
+
+--
+-- RELATIONS FOR TABLE `settings`:
+--
+
+--
+-- Dumping data for table `priorities`
+--
+
+INSERT INTO `settings` (`name`, `value`) VALUES
+('issue.default.priority', '4'),
+('issue.default.status', '1'),
+('project.default.status', '1'),
+('admin.email', 'bugs@localhost'),
+('url.host', 'localhost'),
+('url.base', '/bugs/');
+
+--
+-- Indexes for table `settings`
+--
+ALTER TABLE `settings`
+  ADD PRIMARY KEY (`name`);

+ 14 - 0
install/db_install/03_functions/getsetting.sql

@@ -0,0 +1,14 @@
+CREATE FUNCTION `getsetting`(
+	a_name VARCHAR(50)
+) RETURNS VARCHAR(100)
+DETERMINISTIC
+READS SQL DATA
+SQL SECURITY INVOKER
+BEGIN
+	DECLARE t_value VARCHAR(100);
+	SELECT IFNULL(value,'')
+	INTO t_value
+	FROM settings
+	WHERE name = a_name;
+	return t_value;
+END;

+ 13 - 0
install/db_install/03_functions/setsetting.sql

@@ -0,0 +1,13 @@
+CREATE FUNCTION `setsetting`(
+	a_name VARCHAR(50),
+	a_value VARCHAR(100)
+) RETURNS VARCHAR(100)
+DETERMINISTIC
+MODIFIES SQL DATA
+SQL SECURITY INVOKER
+BEGIN
+	UPDATE settings
+	SET value = a_value
+	WHERE name = a_name;
+	return getsetting(a_name);
+END;

+ 2 - 1
install/db_uninstall/01_data/tables.sql

@@ -14,4 +14,5 @@ CALL drop_table('users');
 CALL drop_table('statuses');
 CALL drop_table('priorities');
 CALL drop_table('permissions');
-CALL drop_table('sessions');
+CALL drop_table('sessions');
+CALL drop_table('settings');

+ 2 - 0
install/db_uninstall/99_cleanup/functions.sql

@@ -0,0 +1,2 @@
+DROP FUNCTION IF EXISTS `getsetting`;
+DROP FUNCTION IF EXISTS `setsetting`;

+ 0 - 0
install/db_uninstall/99_cleanup/drop_table.sql → install/db_uninstall/99_cleanup/procedures.sql


+ 0 - 1
install/index.php

@@ -38,7 +38,6 @@
 			if(empty($_POST['uninstall'])){
 				$res = run_scripts('db_install');
 				if(!empty($_POST['email'])){
-					define('URL_BASE','/bugs/');
 					require_once('../lib/bugs.class.php');
 					Bugs::connect($_POST['server'],$_POST['user'],$_POST['password'],$_POST['db']);
 					if(Bugs::$sql->query("SELECT COUNT(*) count FROM users")->assoc_result['count'] === 0){

+ 0 - 1
js/issue.js

@@ -14,7 +14,6 @@ ready(function(){
 				return res.json();
 			})
 			.then(function(data){
-				console.log(data);
 				if(data.error){
 					alert(data.error);
 				}else{

+ 0 - 1
js/project.js

@@ -14,7 +14,6 @@ ready(function(){
 				return res.json();
 			})
 			.then(function(data){
-				console.log(data);
 				if(data.error){
 					alert(data.error);
 				}else{

+ 8 - 1
js/sessions.js

@@ -6,7 +6,14 @@ ready(function(){
 				credentials: 'include'
 			})
 			.then(function(res){
-				location.reload();
+				return res.json();
+			})
+			.then(function(data){
+				if(data.error){
+					alert(data.error);
+				}else{
+					location.reload();
+				}
 			})
 			.catch(function(e){
 				alert(e);

+ 0 - 1
js/user.js

@@ -14,7 +14,6 @@ ready(function(){
 				return res.json();
 			})
 			.then(function(data){
-				console.log(data);
 				if(data.error){
 					alert(data.error);
 				}else{

+ 8 - 4
lib/bugs.class.php

@@ -4,9 +4,6 @@
 	require_once('user.class.php');
 	require_once('project.class.php');
 	require_once('router.class.php');
-	if(defined('URL_BASE')){
-		Router::base(URL_BASE);
-	}
 	class Bugs {
 		public static $sql;
 		public static $cache = array(
@@ -65,6 +62,7 @@
 					static::logout();
 				}
 			}
+			Router::base(static::setting('url.base'));
 		}
 		static function ip(){
 		    if(getenv('HTTP_CLIENT_IP')){
@@ -252,6 +250,12 @@
 				VALUES (?,?)
 			",'is',static::action($action),$description)->execute();
 		}
+		static function setting($name){
+			return static::$sql->query("
+				SELECT getsetting(?)
+				FROM DUAL;
+			",'s',$name)->num_result[0];
+		}
 	}
 	register_shutdown_function(function(){
 		$emails = Bugs::$sql->query("
@@ -266,7 +270,7 @@
 			ORDER by e.date_created ASC
 		")->assoc_results;
 		foreach($emails as $email){
-			$status = @mail($email['email'],$email['subject'],$email['body'],"From: ".ADMIN_EMAIL."\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n");
+			$status = @mail($email['email'],$email['subject'],$email['body'],"From: ".Bugs::setting('admin.email')."\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n");
 			if($status){
 				Bugs::$sql->query("
 					DELETE FROM emails

+ 42 - 21
lib/errorhandler.php

@@ -1,33 +1,54 @@
 <?php
 	error_reporting(E_ALL);
 	//ini_set('display_errors', 'Off');
-	set_error_handler(function($errno, $errstr, $errfile, $errline){
-		Router::write(
-			Bugs::template('error')
-				->run(new Arguments(array(
-					'error'=> array(
-						'type'=> $errno,
-						'message'=> $errstr,
-						'file'=> $errfile,
-						'line'=> $errline
-					),
-					'backtrace'=> debug_backtrace(),
-					'included'=> get_included_files()
-				)))
+	function error_handle_type($type){
+		static $etype;
+		if(!$etype){
+			$etype = 'html';
+		}
+		if(!empty($type)){
+			$etype = $type;
+		}
+		return $etype;
+	}
+	function handle_error($error,$backtrace,$included){
+		$context = array(
+			'error'=>$error,
+			'backtrace'=>$backtrace,
+			'included'=>$included
 		);
+		Router::clear();
+		switch(error_handle_type()){
+			case 'json':
+				Router::json($context);
+			break;
+			case 'html':default:
+				Router::write(
+					Bugs::template('error')
+						->run(new Arguments($context))
+				);
+		}
 		die();
+	}
+	set_error_handler(function($errno, $errstr, $errfile, $errline){
+		handle_error(
+			array(
+				'type'=> $errno,
+				'message'=> $errstr,
+				'file'=> $errfile,
+				'line'=> $errline
+			),
+			debug_backtrace(),
+			get_included_files()
+		);
 	},E_ALL);
 	register_shutdown_function(function(){
 		$error = error_get_last();
 		if(!is_null($error) && $error['type'] == 1){
-			Router::clear();
-			Router::write(
-				Bugs::template('error')
-					->run(new Arguments(array(
-						'error'=> $error,
-						'backtrace'=> debug_backtrace(),
-						'included'=> get_included_files()
-					)))
+			handle_error(
+				$error,
+				debug_backtrace(),
+				get_included_files()
 			);
 		} 
 	});

+ 2 - 5
lib/issue.class.php

@@ -1,7 +1,4 @@
 <?php
-	if(!defined('DEFAULT_PRIORITY')){
-		define('DEFAULT_PRIORITY',4);
-	}
 	class Issue implements JsonSerializable{
 		public $id;
 		public $cache = array(
@@ -22,11 +19,11 @@
 					if(
 						Bugs::$sql->query("
 							INSERT INTO issues (name,description,pr_id,u_id,p_id,s_id)
-							VALUES (?,?,?,?,?,1);
+							VALUES (?,?,?,?,?,getsetting('issue.default.status'));
 						",'ssiii',
 							$name,
 							func_get_arg(1),
-							func_num_args()>=3&&!empty(func_get_arg(2))?func_get_arg(2):DEFAULT_PRIORITY,
+							func_num_args()>=3&&!empty(func_get_arg(2))?func_get_arg(2):Bugs::setting('issue.default.priority'),
 							func_num_args()>=4&&!empty(func_get_arg(3))?func_get_arg(3)->id:Bugs::$user->id,
 							func_num_args()==5&&!empty(func_get_arg(4))?func_get_arg(4)->id:null
 						)->execute()

+ 1 - 1
lib/project.class.php

@@ -19,7 +19,7 @@
 					$name= func_get_arg(0);
 					Bugs::$sql->query("
 						INSERT INTO projects (name,description,u_id,s_id)
-						VALUES (?,?,?,1)
+						VALUES (?,?,?,getsetting('project.default.status'))
 					",'ssi',
 						func_get_arg(0),
 						func_get_arg(1),

+ 7 - 2
lib/router.class.php

@@ -86,10 +86,15 @@
 			}
 			static::$responses[0]->write($chunk);
 		}
+		public static function header($name,$value){
+			static::$responses[0]->header($name,$value);
+		}
+		public static function json($json){
+			static::$responses[0]->json($json);
+		}
 		public static function redirect($url){
 			static::clear();
-			static::$responses[0]->header('Location',$url);
-
+			static::header('Location',$url);
 		}
 	}
 	register_shutdown_function(function(){

+ 1 - 1
lib/sql.class.php

@@ -180,7 +180,7 @@
 				case 'num_result':case 'result_num':
 					if($this->query){
 						$r = $this->results;
-						return $r?$r->fetch_num():false;
+						return $r?$r->fetch_row():false;
 					}else{
 						return false;
 					}

+ 2 - 0
paths/issue.php

@@ -10,6 +10,7 @@
 			Router::redirect(Router::url(Router::$base.'/!'.$args->issue));
 		},
 		'/issue/{issue}/update'=>function($res,$args){
+			error_handle_type('json');
 			if(!empty($_POST['name'])&&!empty($_POST['description'])){
 				$issue = Bugs::issue($args->issue);
 				$issue->name = $_POST['name'];
@@ -27,6 +28,7 @@
 			$res->write(Bugs::template('issue'));
 		},
 		'/create/issue/complete'=>function($res,$args){
+			error_handle_type('json');
 			if(!empty($_POST['name'])&&!empty($_POST['description'])){
 				$issue = Bugs::issue($_POST['name'],$_POST['description']);
 				$res->json(array(

+ 3 - 0
paths/project.php

@@ -19,6 +19,7 @@
 			Router::redirect(Router::url(Router::$base."!{$args->issue}"));
 		},
 		'/project/{project}/update'=>function($res,$args){
+			error_handle_type('json');
 			if(!empty($_POST['name'])&&!empty($_POST['description'])){
 				$project = Bugs::project(is_numeric($args->project)?intval($args->project):$args->project);
 				$project->name = $_POST['name'];
@@ -36,6 +37,7 @@
 			$res->write(Bugs::template('issue'));
 		},
 		'/project/{project}/create/issue/complete'=>function($res,$args){
+			error_handle_type('json');
 			if(!empty($_POST['name'])&&!empty($_POST['description'])){
 				$issue = Bugs::project(is_numeric($args->project)?intval($args->project):$args->project)
 					->issue($_POST['name'],$_POST['description']);
@@ -52,6 +54,7 @@
 			$res->write(Bugs::template('project'));
 		},
 		'/create/project/complete'=>function($res,$args){
+			error_handle_type('json');
 			if(!empty($_POST['name'])){
 				if(!Bugs::project_id($_POST['name'])){
 					$project = Bugs::project($_POST['name'],$_POST['description']);

+ 1 - 1
paths/register.php

@@ -38,7 +38,7 @@
 							->run($user)
 					);
 					$user->email('Registered',"
-						<a href=\"http://".URL_HOST.URL_BASE."/register/activate/{$user->name}/{$user->activation_code}\">Activate Account</a>
+						<a href=\"http://".Bugs::setting('url.host').Bugs::setting('url.base')."/register/activate/{$user->name}/{$user->activation_code}\">Activate Account</a>
 					");
 					Bugs::activity('user_register',$_POST['name'].' has registered.');
 				}else{

+ 2 - 0
paths/sessions.php

@@ -11,6 +11,7 @@
 			}
 		},
 		'/sessions/remove/{id}'=>function($res,$args){
+			error_handle_type('json');
 			if(Bugs::$user){
 				Bugs::$sql->query("
 					DELETE FROM sessions
@@ -18,6 +19,7 @@
 					AND id = ?
 				",'is',Bugs::$user->id,$args->id)->execute();
 			}
+			$res->json(array());
 		}
 	));
 ?>

+ 1 - 0
paths/user.php

@@ -18,6 +18,7 @@
 			Router::redirect(Router::url(Router::$base.'/~'.$args->user));
 		},
 		'/user/{user}/update'=>function($res,$args){
+			error_handle_type('json');
 			if(Bugs::$user){
 					if(!empty($_POST['password'])&&!empty($_POST['id'])&&!empty($_POST['name'])&&!empty($_POST['email'])){
 					$user = Bugs::user(intval($args->user));