Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. $GLOBALS ['config'] = array(
  5. 'mysql' => array(
  6. 'host' => '127.0.0.1',
  7. 'username' => 'root',
  8. 'password' => '',
  9. 'db' => 'rikars'
  10. ),
  11. 'remember' => array(
  12. 'cookie_name' => 'hash',
  13. 'cookie_expiry' => 604800
  14. ),
  15. 'session' => array(
  16. 'session_name' => 'user',
  17. 'token_name' => 'token'
  18. )
  19. );
  20.  
  21. spl_autoload_register(function($class) {
  22. require_once 'classes/' . $class . '.php';
  23. });
  24.  
  25. require_once 'functions/sanitize.php';
  26.  
  27.  
  28. if(Cookie::exists(Config::get('remember/cookie_name')) && Session::exists(Config::get('session/session_name'))) {
  29. $hash = Cookie::get(Config::get('remember/cookie_name'));
  30. $hashCheck = DB::getInstance()->get('users_sessions', array('hash', '=', $hash));
  31.  
  32. if($hashCheck->count()) {
  33. echo 'asass';
  34. }
  35. }
  36.  
  37. <?php
  38. class DB {
  39. private static $_instance = null;
  40. private $_pdo,
  41. $_query,
  42. $_error = false,
  43. $_result,
  44. $_count = 0;
  45.  
  46. private function __construct() {
  47. try {
  48. $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
  49. } catch(PDOException $e) {
  50. die($e->getMessage());
  51. }
  52. }
  53.  
  54. public static function getInstance() {
  55. if(!isset(self::$_instance)) {
  56. self::$_instance = new DB();
  57. }
  58.  
  59. return self::$_instance;
  60. }
  61.  
  62. public function query($sql, $params = array()) {
  63. $this->_error = false;
  64. if($this->_query = $this->_pdo->prepare($sql)) {
  65. $x = 1;
  66. if(count($params)){
  67. foreach($params as $param){
  68. $this->_query->bindValue($x, $param);
  69. $x++;
  70. }
  71. }
  72. if($this->_query->execute()){
  73. $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
  74. $this->_count = $this->_query->rowCount();
  75. } else {
  76. $this->_error = true;
  77. }
  78. }
  79.  
  80. return $this;
  81. }
  82.  
  83. public function action($action, $table, $where = array()) {
  84. if(count($where) === 3) {
  85. $operators = array('=', '>', '<', '>=', '<=');
  86.  
  87. $field = $where[0];
  88. $operator = $where[1];
  89. $value = $where[2];
  90.  
  91. if(in_array($operator, $operators)) {
  92. $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
  93.  
  94. if(!$this->query($sql, array($value))->error()) {
  95. return $this;
  96. }
  97. }
  98. }
  99.  
  100. return false;
  101. }
  102.  
  103. public function get($table, $where) {
  104. return $this->action('SELECT *', $table, $where);
  105. }
  106.  
  107. public function delete($table, $where) {
  108. return $this->action('DELETE *', $table, $where);
  109. }
  110.  
  111. public function insert($table, $fields = array()) {
  112. $keys = array_keys($fields);
  113. $values = null;
  114. $x = 1;
  115.  
  116. foreach($fields as $field) {
  117. $values .= '?';
  118. if($x < count($fields)) {
  119. $values .= ', ';
  120. }
  121. $x++;
  122. }
  123.  
  124. $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
  125.  
  126. if(!$this->query($sql, $fields)->error()) {
  127. return true;
  128. }
  129.  
  130. return false;
  131. }
  132.  
  133. public function update($table, $id, $fields) {
  134. $set = '';
  135. $x= 1;
  136.  
  137. foreach ($fields as $name => $value) {
  138. $set .= "{$name} = ?";
  139. if($x < count($fields)) {
  140. $set .= ', ';
  141. }
  142. $x++;
  143. }
  144.  
  145. $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";
  146.  
  147. if($this->query($sql, $fields)->error()) {
  148. return true;
  149. }
  150.  
  151. return false;
  152. }
  153.  
  154. public function results() {
  155. return $this->_results;
  156. }
  157.  
  158. public function first() {
  159. return $this->results()[0];
  160. }
  161.  
  162. public function error() {
  163. return $this->_error;
  164. }
  165.  
  166. public function count() {
  167. return $this->_count;
  168. }
  169. public function ins() {
  170. $instance = DB::getInstance();
  171. $instance->get('users',array('user_id','=','1'));
  172. if (!$instance->Count()) {
  173. echo 'No user';
  174. } else {
  175. echo 'User exists';
  176. }
  177. }
  178. }
  179. ?>
  180.  
  181. if(Cookie::exists(Config::get('remember/cookie_name')) && Session::exists(Config::get('session/session_name'))) {
  182. $hash = Cookie::get(Config::get('remember/cookie_name'));
  183. $hashCheck = DB::getInstance()->get('users_sessions', array('hash', '=', $hash));
  184.  
  185. if($hashCheck === false) {
  186. echo 'Action return false';
  187. } else {
  188. $count = $hashCheck->count();
  189. if($count) {
  190. echo 'Its object and count ='.$count;
  191. }
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement