Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /* Настройки сайта
  5. -----------------------------------------------------------------------------*/
  6. define( 'TEST_MODE', true );
  7. define( 'SITE_ADDRESS', 'soc.tt' );
  8. define( 'SITE_NAME', 'Social network' );
  9. define( 'SITE_EMAIL', 'mail@soc.tt' );
  10. define( 'EXPIRE_COOKIE', 3600*24*32 );
  11.  
  12.  
  13. /* Установки
  14. -----------------------------------------------------------------------------*/
  15. session_start();
  16. ini_set( "display_errors", TEST_MODE );
  17. date_default_timezone_set( "Asia/Yekaterinburg" );
  18.  
  19.  
  20. /* Автозагрузка классов
  21. -----------------------------------------------------------------------------*/
  22. function __autoload($name) {
  23. include_once('classes/' . $name . '.php');
  24. }
  25.  
  26.  
  27. /* База Данных
  28. -----------------------------------------------------------------------------*/
  29. class DataBase {
  30. const host = 'localhost';
  31. const dbName = 'soc';
  32. const user = 'root';
  33. const password = '';
  34. public $db;
  35. private $attr = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
  36. private static $instance;
  37. private function __construct(){
  38. $this->db = new PDO('mysql:host='.self::host.';dbname='.self::dbName, self::user, self::password, $this->attr);
  39. }
  40. private function __clone(){}
  41. private function __wakeup(){}
  42. private function __destruct(){
  43. $this->db = null;
  44. }
  45. public static function getInstance() {
  46. if ( is_null(self::$instance) ) {
  47. self::$instance = new self();
  48. }
  49. return self::$instance;
  50. }
  51. }
  52.  
  53.  
  54. /* Сессия и куки
  55. -----------------------------------------------------------------------------*/
  56. class Session {
  57. public $data;
  58. public $auth = false;
  59. public function __construct(){
  60. if(!empty($_SESSION['key0']) || !empty($_SESSION['key1']) || !empty($_SESSION['save']))
  61. if(!empty($_SESSION['key0']) && !empty($_SESSION['key1']) && !empty($_SESSION['save'])){
  62. $this->data=DataBase::getInstance()->db->query('SELECT * FROM `users` WHERE `id`=\''.$_SESSION['key0'].'\' AND `key`=\''.$_SESSION['key1'].'\'');
  63. $this->data=$this->data->fetch();
  64. if(!empty($this->data[0])){
  65. $_SESSION['save']==1?$this->createCookie():$this->deleteCookie();
  66. $this->auth = true;
  67. }
  68. else {
  69. $this->deleteSession();
  70. $this->deleteCookie();
  71. }
  72. }
  73. else {
  74. $this->deleteSession();
  75. $this->deleteCookie();
  76. }
  77. else
  78. if(!empty($_COOKIE['key0']) || !empty($_COOKIE['key1']))
  79. if(!empty($_COOKIE['key0']) && !empty($_COOKIE['key1'])){
  80. $this->data=DataBase::getInstance()->db->query('SELECT * FROM `users` WHERE `id`=\''.$_COOKIE['key0'].'\' AND `key`=\''.$_COOKIE['key1'].'\'');
  81. $this->data=$this->data->fetch();
  82. if(!empty($this->data[0])){
  83. $this->createSession(1);
  84. $this->createCookie();
  85. $this->auth = true;
  86. }
  87. else
  88. $this->delete();
  89. }
  90. else
  91. $this->delete();
  92. else
  93. $this->delete();
  94. }
  95. public function login($login, $password, $save){
  96. $this->data=DataBase::getInstance()->db->query('SELECT * FROM users WHERE (nick=\''.$login.'\' OR email=\''.$login.'\') AND password=\''.$password.'\'');
  97. $this->data=$this->data->fetch();
  98. if(!empty($this->data[0])){
  99. if($save){
  100. $this->createCookie();
  101. $this->createSession(1);
  102. }
  103. else
  104. $this->delete();
  105. $this->auth = true;
  106. }
  107. else
  108. $this->delete();
  109. }
  110. private function createSession($save=0) {
  111. $_SESSION['key0'] = $this->data['id'];
  112. $_SESSION['key1'] = $this->data['key'];
  113. $_SESSION['save'] = $save;
  114. }
  115. private function deleteSession() {
  116. $_SESSION['key0'] = '';
  117. $_SESSION['key1'] = '';
  118. $_SESSION['save'] = '';
  119. }
  120. private function createCookie() {
  121. $key0=$this->data['id'];
  122. $key1=$this->data['key'];
  123. $_COOKIE['key0']=$key0;
  124. $_COOKIE['key1']=$key1;
  125. setcookie("key0",$key0,time()+EXPIRE_COOKIE,'/');
  126. setcookie("key1",$key1,time()+EXPIRE_COOKIE,'/');
  127. }
  128. private function deleteCookie() {
  129. $key0='';
  130. $key1='';
  131. $_COOKIE['key0']=$key0;
  132. $_COOKIE['key1']=$key1;
  133. setcookie("key0",$key0,time()+EXPIRE_COOKIE,'/');
  134. setcookie("key1",$key1,time()+EXPIRE_COOKIE,'/');
  135. }
  136. public function delete() {
  137. $this->deleteSession();
  138. $this->deleteCookie();
  139. }
  140. }
  141. $u = new Session();
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement