Browse Source

php api updates

Nathaniel van Diepen 10 years ago
parent
commit
90a5c1030b
11 changed files with 96 additions and 4 deletions
  1. 1 1
      .gitignore
  2. 5 0
      api.php
  3. 2 0
      data/index.template.html
  4. 3 0
      data/register.context.json
  5. 25 0
      data/register.template.html
  6. 2 0
      index.php
  7. 3 3
      install/api.php
  8. 17 0
      php/config.php
  9. 7 0
      php/database.php
  10. 26 0
      php/functions.php
  11. 5 0
      php/include.php

+ 1 - 1
.gitignore

@@ -1,4 +1,4 @@
-config.php
+config.json
 
 #################
 ## Eclipse

+ 5 - 0
api.php

@@ -1,4 +1,6 @@
 <?php
+	@session_start();
+	require_once('php/include.php');
 	// MYSQL default bugs:bugs
 	function retj($json,$title){
 		$type=$_GET['type'];
@@ -43,6 +45,9 @@
 					$ret['context'] = json_decode(file_get_contents('data/'.$id.'.context.json'));
 					retj($ret,$id);
 				break;
+				case 'login':
+						// TODO - handle logins
+				break;
 				default:
 					die("invalid type");
 			}

+ 2 - 0
data/index.template.html

@@ -6,4 +6,6 @@
 </p>
 <p>
 	<a href="#page-login">Login</a>
+	-
+	<a href="#page-register">Register</a>
 </p>

+ 3 - 0
data/register.context.json

@@ -0,0 +1,3 @@
+{
+	"title": "Register"
+}

+ 25 - 0
data/register.template.html

@@ -0,0 +1,25 @@
+<h1>
+	{{title}}
+</h1>
+<form id="login">
+	<div>
+		Email: <input name="email" type="text"/>
+	</div>
+	<div>
+		Username: <input name="username" type="text"/>
+	</div>
+	<div>
+		Password: <input name="password" type="password"/>
+	</div>
+	<input type="submit" value="register"/>
+	<input type="button" value="cancel" class="cancel"/>
+</form>
+<script>
+	$('#login').submit(function(){
+		// TODO - Handle register
+		return false;
+	}).children('.cancel').click(function(){
+		loadState('page-index');
+		return false;
+	});
+</script>

+ 2 - 0
index.php

@@ -1,4 +1,6 @@
 <?php
+	session_start();
+	require_once('php/include.php');
 	if(isset($_GET['get'])){
 		$get = $_GET['get'];
 		unset($_GET['get']);

+ 3 - 3
install/api.php

@@ -1,7 +1,7 @@
 <?php
 	// MYSQL default bugs:bugs
-	ini_set('memory_limit', '5120M');
-	set_time_limit ( 0 );
+	ini_set('memory_limit','5120M');
+	set_time_limit(0);
 	function remove_comments(&$output){
 		$lines = explode("\n",$output);
 		$output = "";
@@ -128,7 +128,7 @@
 							foreach($sql_query as $sql){
 								mysql_query($sql) or die('error in query');
 							}
-							file_put_contents('../config.php',"<?php\n\t\$host='{$dbhost}';\n\t\$user = '{$dbuser}';\n\t\$pass = '{$dbpass}';\n\t\$name = '{$dbname}';\n?>");
+							file_put_contents('../config.json',"{\"host\":\"{$dbhost}\",\"user\":\"{$dbuser}\",\"password\":\"{$dbpass}\",\"database\":\"{$dbname}\"}");
 							echo 'pass';
 						}else{
 							echo "Please don't leave any fields blank";

+ 17 - 0
php/config.php

@@ -0,0 +1,17 @@
+<?php
+	define('PATH_ROOT',realpath(dirname(__FILE__)).'/../');
+	define('PATH_CONFIG',PATH_ROOT.'config.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);
+	function get($setting){
+		return $GLOBALS['config'][$setting];
+	}
+	function set($setting,$value){
+		$GLOBALS['config'][$setting] = $value;
+		file_put_contents(PAT_CONFIG,json_encode($GLOBALS['config']));
+		return $value;
+	}
+?>

+ 7 - 0
php/database.php

@@ -0,0 +1,7 @@
+<?php
+	require_once(PATH_PHP.'config.php');
+	$mysqli = new mysqli(get('host'),get('user'),get('password'),get('database'));
+	if($mysqli->connect_errno){
+		echo "Failed to connect to MySQL: ".$mysqli->connect_error;
+	}
+?>

+ 26 - 0
php/functions.php

@@ -0,0 +1,26 @@
+<?php
+	@session_start();
+	require_once(PATH_PHP.'database.php');
+	// TODO - create php functions for the api
+	function addUser($username,$password,$email){
+		$salt = $mysqli->escape_string(salt());
+		$email = $mysqli->escape_string($email);
+		$username = $mysqli->escape_string($username);
+		$hash = $mysqli->escape_string(saltedHash($password,$salt));
+		return $mysqli->query("INSERT INTO `bugs`.`users` (email,name,pass,salt) VALUES '{$email}','{$username}','{$password}','{$salt}'");
+	}
+	function salt(){
+		return uniqid(mt_rand(0,61), true);
+	}
+	function saltedHash($pass,$salt){
+		$hash = $pass.$salt;
+		for($i = 0;$i<50;$i++){
+			$hash = hash('sha512',$pass.$hash.$salt);
+		}
+		return $hash;
+	}
+	function compareSaltedHash($pass,$salt,$hash){
+		return $hash == saltedHash($pass,$salt);
+	}
+	
+?>

+ 5 - 0
php/include.php

@@ -0,0 +1,5 @@
+<?php
+	require_once(realpath(dirname(__FILE__)).'/'.'config.php');
+	require_once(PATH_PHP.'functions.php');
+	require_once(PATH_PHP.'database.php');
+?>