├── index.php ├── .gitignore ├── auth.php ├── api.php ├── server.class.php ├── auth_helper.php └── README.md /index.php: -------------------------------------------------------------------------------- 1 | SSO -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build* 2 | phpdoc.xml 3 | config.php -------------------------------------------------------------------------------- /auth.php: -------------------------------------------------------------------------------- 1 | getAccount($hash,$cookie); 9 | if ($account===false) { 10 | $server->createAccount($hash,$cookie); 11 | } else { 12 | $loggedin=$account['loggedin']; 13 | } 14 | } else { 15 | $cookie=md5(__FILE__.$hash.microtime()).date('YmdHis'); 16 | $server->createAccount($hash,$cookie); 17 | setcookie('cdsso',$cookie, time()+3600*2); 18 | } 19 | $message=json_encode(array('cookie'=>$cookie,'loggedin'=>$loggedin)); 20 | ?> 21 | 22 |
23 | 24 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /api.php: -------------------------------------------------------------------------------- 1 | updateAccount($hash,$cookie,1,$email); 16 | break; 17 | case 'logout': //a user logs out 18 | $email=isset($_REQUEST['email']) ? $_REQUEST['email'] : null; 19 | $account=$server->updateAccount($hash,$cookie,0,$email); 20 | break; 21 | case 'loggedin': //check if user is logged in 22 | $account=$server->getAccount($hash,$cookie); 23 | if ($account===false) $results['loggedin']=false; 24 | else { 25 | $results['loggedin']=$account['loggedin']; 26 | $results['email']=$account['email']; 27 | } 28 | break; 29 | } 30 | 31 | $results['status']='success'; 32 | $results['request']=$_REQUEST; 33 | echo json_encode($results); 34 | -------------------------------------------------------------------------------- /server.class.php: -------------------------------------------------------------------------------- 1 | pdo = new PDO('mysql:host='.$conf['db']['hostSpec'].';dbname='.$conf['db']['dbName'],$conf['db']['dbUser'],$conf['db']['dbPass']); 9 | $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 10 | } 11 | 12 | function getAccount($hash,$cookie) { 13 | $query='select * from connections where hash=? and cookie=?'; 14 | $sth=$this->pdo->prepare($query); 15 | $sth->execute(array($hash,$cookie)); 16 | if ($account=$sth->fetch()) { 17 | return $account; 18 | } else { 19 | return false; 20 | } 21 | } 22 | 23 | function createAccount($hash,$cookie) { 24 | $query='insert into connections (hash,cookie,loggedin) values (?,?,0)'; 25 | $sth=$this->pdo->prepare($query); 26 | $sth->execute(array($hash,$cookie)); 27 | } 28 | 29 | function updateAccount($hash,$cookie,$loggedin,$email) { 30 | $query='update connections set email=?, loggedin=? where hash=? and cookie=?'; 31 | $sth=$this->pdo->prepare($query); 32 | $sth->execute(array($email,$loggedin,$hash,$cookie)); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /auth_helper.php: -------------------------------------------------------------------------------- 1 | 2 |