]> jfr.im git - z_archive/KronOS.git/commitdiff
Added a simple login/sessions system.
authorJohn Runyon <redacted>
Tue, 9 Oct 2012 07:58:15 +0000 (10:58 +0300)
committerJohn Runyon <redacted>
Tue, 9 Oct 2012 07:58:15 +0000 (10:58 +0300)
.gitignore [new file with mode: 0644]
core/common.php [new file with mode: 0644]
core/login.php [new file with mode: 0644]
dump.sql [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..0487d7c
--- /dev/null
@@ -0,0 +1,4 @@
+*.pem
+*~
+*.bak
+config.php
diff --git a/core/common.php b/core/common.php
new file mode 100644 (file)
index 0000000..2ffbec1
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+include('config.php');
+
+function make_reply($data, $errcode=NULL) {
+       if ($errcode === NULL) {
+               $resp = array(
+                       'success' => TRUE,
+                       'time' => time(),
+                       'contents' => $data,
+               );
+               echo json_encode($resp);
+       } else {
+               $resp = array(
+                       'success' => FALSE,
+                       'time' => time(),
+                       'error' => array(
+                               'code' => $errcode,
+                               'reason' => $data,
+                       ),
+               );
+               echo json_encode($resp);
+       }
+       exit(0);
+}
diff --git a/core/login.php b/core/login.php
new file mode 100644 (file)
index 0000000..5520337
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+include('common.php');
+
+if (empty($_POST['user']) || empty($_POST['pass'])) {
+       make_reply('Username or password empty.', 1);
+}
+$sth = $db->prepare('SELECT uid FROM users WHERE username = ? AND password = ?');
+$sth->bind_param('ss', $_POST['user'], sha1(PWSALT.$_POST['pass']));
+$sth->execute();
+$sth->bind_result($uid);
+if (!$sth->fetch()) { // no row returned
+       make_reply('Username or password incorrect.', 2);
+}
+
+// row returned, user/pw good
+$sth->close();
+$sth = $db->prepare('INSERT INTO sessions(sid, uid, started, last, active) VALUES (NULL, ?, NOW(), NOW(), 1)');
+$sth->bind_param('i', $uid);
+$sth->execute();
+$sid = $sth->insert_id;
+
+make_reply(array('uid' => $uid, 'sid' => $sid));
diff --git a/dump.sql b/dump.sql
new file mode 100644 (file)
index 0000000..c161490
--- /dev/null
+++ b/dump.sql
@@ -0,0 +1,77 @@
+-- MySQL dump 10.13  Distrib 5.5.27, for debian-linux-gnu (x86_64)
+--
+-- Host: localhost    Database: dime_wos
+-- ------------------------------------------------------
+-- Server version      5.5.27-1~dotdeb.0
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Table structure for table `sessions`
+--
+
+DROP TABLE IF EXISTS `sessions`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `sessions` (
+  `sid` int(10) unsigned NOT NULL AUTO_INCREMENT,
+  `uid` int(10) unsigned NOT NULL,
+  `started` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+  `last` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
+  `active` tinyint(1) NOT NULL DEFAULT '1',
+  PRIMARY KEY (`sid`)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `sessions`
+--
+
+LOCK TABLES `sessions` WRITE;
+/*!40000 ALTER TABLE `sessions` DISABLE KEYS */;
+/*!40000 ALTER TABLE `sessions` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `users`
+--
+
+DROP TABLE IF EXISTS `users`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `users` (
+  `uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
+  `username` varchar(15) NOT NULL,
+  `password` char(40) NOT NULL,
+  PRIMARY KEY (`uid`)
+) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `users`
+--
+
+LOCK TABLES `users` WRITE;
+/*!40000 ALTER TABLE `users` DISABLE KEYS */;
+/*!40000 ALTER TABLE `users` ENABLE KEYS */;
+UNLOCK TABLES;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2012-10-09 10:55:41