Răsfoiți Sursa

Restructure

Nathaniel van Diepen 11 ani în urmă
părinte
comite
b41b33a947

+ 3 - 3
api.php

@@ -23,9 +23,9 @@
 					// TODO - handle admin requests
 				break;
 				case 'template':
-					$ret['template'] = file_get_contents('data/'.$id.'.template.html');
-					if(file_exists(PATH_DATA.$id.'.context.json')){
-						$context = json_decode(file_get_contents(PATH_DATA.$id.'.context.json'));
+					$ret['template'] = file_get_contents(PATH_DATA.'pages/'.$id.'.template');
+					if(file_exists(PATH_DATA.'context/'.$id.'.json')){
+						$context = json_decode(file_get_contents(PATH_DATA.'context/'.$id.'.json'));
 					}else{
 						$context = Array();
 					}

+ 1 - 0
config.default.json

@@ -0,0 +1 @@
+{"host":"localhost","user":"bugs","password":"bugs","database":"bugs","timeout":216000}

+ 0 - 0
data/index.context.json → data/context/index.json


+ 0 - 0
data/login.context.json → data/context/login.json


+ 0 - 0
data/register.context.json → data/context/register.json


+ 0 - 0
data/index.template.html → data/pages/index.template


+ 0 - 0
data/login.template.html → data/pages/login.template


+ 0 - 0
data/logout.template.html → data/pages/logout.template


+ 0 - 0
data/register.template.html → data/pages/register.template


+ 31 - 21
index.php

@@ -1,5 +1,5 @@
 <?php
-	if(!file_exists('config.json')){
+	if(!file_exists('config.default.json')){
 		header('Location: install');
 		die();
 	}elseif(file_exists('install')){
@@ -20,26 +20,36 @@
 			$type = $_GET['type'];
 			$id = $_GET['id'];
 		}
-		if($get == 'state'){
-			$json = Array();
-			$json['state'] = Array();
-			$json['state']['data'] = $_GET;
-			if(isset($_GET['key'])){
-				$json['key'] = $_GET['key'];
-				$json['state']['data']['key'] = $_GET['key'];
-			}
-			switch($type){
-				case 'user':$url='~'.$id;break;
-				case 'group':$url='+'.$id;break;
-				case 'issue':$url='!'.$id;break;
-				case 'template':$url='page-'.$id;break;
-				default:$url=$type.'-'.$id;
-			}
-			$json['state']['url'] = $url;
-			die(json_encode($json));
-			
-		}elseif($get == 'api'){
-			require_once('api.php');
+		switch($get){
+			case 'state':
+				$json = Array();
+				$json['state'] = Array();
+				$json['state']['data'] = $_GET;
+				if(isset($_GET['key'])){
+					$json['key'] = $_GET['key'];
+					$json['state']['data']['key'] = $_GET['key'];
+				}
+				switch($type){
+					case 'user':$url='~'.$id;break;
+					case 'group':$url='+'.$id;break;
+					case 'issue':$url='!'.$id;break;
+					case 'template':$url='page-'.$id;break;
+					default:$url=$type.'-'.$id;
+				}
+				$json['state']['url'] = $url;
+				die(json_encode($json));
+			break;
+			case 'api':
+				require_once('api.php');
+			break;
+			case 'settings':
+				$settings = Array();
+				$keys = Array('timeout');
+				foreach($keys as $key){
+					$settings[$key] = get($key);
+				}
+				die(json_encode($settings));
+			break;
 		}
 	}
 ?>

+ 1 - 1
install/api.php

@@ -128,7 +128,7 @@
 							foreach($sql_query as $sql){
 								mysql_query($sql) or die('error in query');
 							}
-							file_put_contents('../config.json',"{\"host\":\"{$dbhost}\",\"user\":\"{$dbuser}\",\"password\":\"{$dbpass}\",\"database\":\"{$dbname}\"}");
+							file_put_contents('../config.default.json',"{\"host\":\"{$dbhost}\",\"user\":\"{$dbuser}\",\"password\":\"{$dbpass}\",\"database\":\"{$dbname}\",\"timeout\":216000}");
 							echo 'pass';
 						}else{
 							echo "Please don't leave any fields blank";

+ 28 - 10
js/index.js

@@ -3,15 +3,23 @@
 	var State = History.getState(),
 		Key = null,
 		flag = false,
+		settings = {},
 		exists = function(v){
 			return typeof v != 'undefined';
 		},
+		get = window.get = function(s){
+			return settings[s];
+		},
+		set = window.set = function(s,v){
+			settings[s] = v;
+			return v;
+		},
 		setKey = window.setKey = function(key){
 			if(key !== null){
 				console.log('Key change to '+key);
 				Key = key;
 				var d = new Date();
-				d.setTime(d.getTime()+3600);
+				d.setTime(d.getTime()+get('timeout'));
 				$.cookie('key',key,{
 					expires: d
 				});
@@ -134,15 +142,25 @@
 		}else{
 			flag = true;
 		}
-		apiState(location.href,function(){
-			if(flag){
-				State.data = {
-					type: '',
-					data: ''
-				};
-			}
-			$(window).trigger('statechange');
-		});
+		var data = {
+			get:'settings',
+			timestamp:+new Date
+		};
+		if(Key !== null){
+			data.key = Key;
+		}
+		$.get(location.href,data,function(d){
+			settings = d;
+			apiState(location.href,function(){
+				if(flag){
+					State.data = {
+						type: '',
+						data: ''
+					};
+				}
+				$(window).trigger('statechange');
+			});
+		},'json');
 	});
 	$.fn.serializeObject = function(){
 		var o = {},

+ 48 - 4
php/config.php

@@ -1,17 +1,61 @@
 <?php
 	define('PATH_ROOT',realpath(dirname(__FILE__)).'/../');
 	define('PATH_CONFIG',PATH_ROOT.'config.json');
+	define('PATH_DEFAULT_CONFIG',PATH_ROOT.'config.default.json');
 	define('PATH_PHP',PATH_ROOT.'php/');
 	define('PATH_JS',PATH_ROOT.'js/');
 	define('PATH_CSS',PATH_ROOT.'css/');
 	define('PATH_DATA',PATH_ROOT.'data/');
-	$GLOBALS['config'] = json_decode(file_get_contents(PATH_CONFIG),true);
+	global $config;
+	if(file_exists(PATH_CONFIG)){
+		$config = objectToArray(json_decode(file_get_contents(PATH_CONFIG),true));
+	}else{
+		$config = Array();
+	}
+	$config = array_merge($config,objectToArray(json_decode(file_get_contents(PATH_DEFAULT_CONFIG),true)));
 	function get($setting){
-		return $GLOBALS['config'][$setting];
+		global $config;
+		if(isset($config[$setting])){
+			return $config[$setting];
+		}else{
+			return false;
+		}
 	}
 	function set($setting,$value){
-		$GLOBALS['config'][$setting] = $value;
-		file_put_contents(PAT_CONFIG,json_encode($GLOBALS['config']));
+		global $config;
+		$config[$setting] = $value;
+		file_put_contents(PAT_CONFIG,json_encode($config));
 		return $value;
 	}
+	function objectToArray($d){
+		if(is_object($d)){
+			// Gets the properties of the given object
+			// with get_object_vars function
+			$d = get_object_vars($d);
+		}
+		if(is_array($d)){
+			/*
+			* Return array converted to object
+			* Using __FUNCTION__ (Magic constant)
+			* for recursive call
+			*/
+			return array_map(__FUNCTION__, $d);
+		}else{
+			// Return array
+			return $d;
+		}
+	}
+	function arrayToObject($d){
+		if(is_array($d)){
+			/*
+			* Return array converted to object
+			* Using __FUNCTION__ (Magic constant)
+			* for recursive call
+			*/
+			return (object)array_map(__FUNCTION__, $d);
+		}else{
+			// Return object
+			return $d;
+		}
+	}
 ?>

+ 1 - 1
php/security.php

@@ -34,7 +34,7 @@
 			unset($SESSION['username']);
 		}else{
 			$SESSION['key'] = $key;
-			setcookie('key',$key,time()+3600);
+			setcookie('key',$key,time()+get('timeout'));
 		}
 	}
 ?>