/src/frontend/inc/class_loggin.php

https://github.com/KaTaLyzer/KaTaLyzer · PHP · 211 lines · 149 code · 46 blank · 16 comment · 21 complexity · 9005b9807ae8f72f47f8fd83978eb606 MD5 · raw file

  1. <?php
  2. class database{
  3. private $link;
  4. private $servername;
  5. private $username;
  6. private $password;
  7. private $database;
  8. private $result;
  9. private $table;
  10. private $no_data;
  11. private $db_loaded;
  12. private $sd_table;
  13. private $table_suffix; // tablename suffix - generated automaticaly - convert() function
  14. function db(){
  15. $this->db_loaded = false;
  16. $this->no_data = false;
  17. $this->sd_table = false;
  18. // mysql connect
  19. // mysql_connect($host,$username,$password);
  20. // @mysql_select_db($database) or die( "Unable to select database");
  21. }
  22. // set server
  23. function set_server($servername){
  24. $this->servername = $servername;
  25. }
  26. // set username
  27. function set_username($username){
  28. $this->username = $username;
  29. }
  30. // set password
  31. function set_password($password){
  32. $this->password = $password;
  33. }
  34. // set database
  35. function set_database($database){
  36. $this->database = $database;
  37. }
  38. // set tablename
  39. function set_table($table,$table_suffix_last='',$table_relation_index_name='id'){
  40. $this->table = $table;
  41. $this->table_suffix_last = $table_suffix_last;
  42. $this->table_relation_index_name = $table_relation_index_name;
  43. }
  44. function load_db(){
  45. if(!$this->db_loaded)
  46. $this->db_loaded = $this->connect();
  47. }
  48. function set_sd_table($v){
  49. $this->sd_table = $v;
  50. }
  51. function connect() {
  52. //connect to database
  53. $this->link = @mysql_connect(
  54. $this->servername,
  55. $this->username,
  56. $this->password);
  57. // if connection was no successfull
  58. if(!$this->link){
  59. $this->error_message = mysql_error($this->link);
  60. echo "unable to connect to database".$this->error_message;
  61. return false;
  62. }
  63. //try to select database
  64. if(!@mysql_select_db($this->database, $this->link)){
  65. // if the select was unsuccessfull
  66. $this->error_message = mysql_error($this->link);
  67. echo "unable to select database:".$this->error_message;
  68. return false;
  69. }
  70. mysql_query("SET character set utf8");
  71. mysql_query("SET character_set_results=utf8");
  72. mysql_query("SET character_set_connection=utf8");
  73. return true;
  74. }
  75. function query($query = ""){
  76. $this->error_message = "";
  77. //posle spravu na server
  78. $result = mysql_query($query);
  79. // skontroluje ci sprava bola uspesna
  80. if(!$result){
  81. $this->error_message = mysql_query($this->link);
  82. echo "error in query: ".$this->error_message;
  83. }
  84. $this->result = $result;
  85. return $result;
  86. }
  87. function num_rows($result = null){
  88. if($result == null)
  89. return mysql_num_rows($this->result);
  90. return mysql_num_rows($result);
  91. }
  92. function fetch($result = null){
  93. if($result == null)
  94. return mysql_fetch_assoc($this->result);
  95. return mysql_fetch_assoc($result);
  96. }
  97. function get_records($query = "", $attributes = null){
  98. $records = array();
  99. // if query was unsuccessfull return empty array
  100. if(!$this->query($query, $attributes)){
  101. return $records;
  102. }
  103. while ($row = $this->fetch()) {
  104. $records[] = $row;
  105. }
  106. return $row;
  107. }
  108. function affected_rows($link = null){
  109. if($link == null)
  110. return mysql_affected_rows($this->link);
  111. return mysql_affected_rows($link);
  112. }
  113. function insert_id(){
  114. return mysql_insert_id();
  115. }
  116. function escape($text){
  117. return mysql_real_escape_string($text);
  118. }
  119. function close($link = null){
  120. if($link == null)
  121. return mysql_close($this->link);
  122. return mysql_close($link);
  123. }
  124. //funkcia sa pripoji k otvorenej databaze
  125. function set_db_link($db_link = NULL){
  126. if($db_link == NULL)
  127. echo "invalid database link";
  128. $this->link = $db_link;
  129. }
  130. }
  131. class loggin extends database {
  132. public function __construct($servername, $username, $password, $database){
  133. $this->set_server($servername);
  134. $this->set_username($username);
  135. $this->set_password($password);
  136. $this->set_database($database);
  137. $this->connect();
  138. }
  139. public static function login_password_encryption($password)
  140. {
  141. return sha1($password);
  142. }
  143. function loged($username, $password){
  144. if (empty($password)) {
  145. $passw = $password;
  146. }
  147. else {
  148. $passw = $this->login_password_encryption($password);
  149. }
  150. $sql = "SELECT * FROM USER WHERE name='".$username."' AND password='".$passw."'";
  151. $this->query($sql);
  152. if($this->num_rows() != 0){
  153. if (!isset($_SESSION)) {
  154. session_start();
  155. }
  156. $row = $this->fetch();
  157. $_SESSION['name'] = $row['name'];
  158. $_SESSION['group'] = $row['groups'];
  159. $_SESSION['logged'] = true;
  160. return true;
  161. }
  162. return false;
  163. }
  164. }
  165. ?>