PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/user.inc.php

https://bitbucket.org/mhell/mhmcr
PHP | 210 lines | 181 code | 28 blank | 1 comment | 30 complexity | aae1c00d5df8ce85f4d8dcad9a5d2fd6 MD5 | raw file
  1. <?php
  2. class User {
  3. var $id;
  4. var $name;
  5. var $lvl;
  6. var $tmp;
  7. var $ip;
  8. var $email;
  9. function User($input,$method) {
  10. global $db;
  11. $result = mysql_query("SELECT ".$db['users']['username'].",".$db['users']['id'].",".$db['users']['level'].",".$db['users']['temp'].",".$db['users']['ip'].",".$db['users']['email']." FROM ".$db['tables']['users']." WHERE $method='$input'");
  12. if (!$result)
  13. $this->id=false;
  14. else {
  15. $line = mysql_fetch_array($result, MYSQL_ASSOC);
  16. $this->id = $line[$db['users']['id']];
  17. $this->name = $line[$db['users']['username']];
  18. $this->lvl = $line[$db['users']['level']];
  19. $this->tmp = $line[$db['users']['temp']];
  20. $this->ip = $line[$db['users']['ip']];
  21. $this->email = $line[$db['users']['email']];
  22. }
  23. return true;
  24. }
  25. function gameLoginConfirm() {
  26. global $db;
  27. if (!$this->id)
  28. return false;
  29. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['lastlogin']."=NOW() WHERE ".$db['users']['id']."='".intval($this->id)."' LIMIT 1;");
  30. return true;
  31. }
  32. function gameLogoutConfirm() {
  33. global $db;
  34. if (!$this->id)
  35. return false;
  36. $result = mysql_query("SELECT ".$db['users']['id']." FROM ".$db['tables']['users']." WHERE ".$db['users']['server']." IS NOT NULL and ".$db['users']['id']."='".intval($this->id)."' LIMIT 1;");
  37. if ($result) //(mysql_num_rows($result) == 1)
  38. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['server']."=NULL WHERE ".$db['users']['id']."='".intval($this->id)."'");
  39. return true;
  40. }
  41. function gameLoginLast() {
  42. global $db;
  43. if (!$this->id) return false;
  44. $result = mysql_query("SELECT ".$db['users']['lastlogin']." FROM ".$db['tables']['users']." WHERE ".$db['users']['lastlogin']."<>'0000-00-00 00:00:00' and ".$db['users']['id']."='".intval($this->id)."' LIMIT 1;");
  45. if ($result) {
  46. return mysql_result($result,0);
  47. } else {
  48. return false;
  49. }
  50. }
  51. function login($tmp,$ip) {
  52. global $db;
  53. if (!$this->id) return false;
  54. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['temp']."='$tmp' WHERE ".$db['users']['id']."='".$this->id."'");
  55. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['ip']."='".GetRealIp()."' WHERE ".$db['users']['id']."='".$this->id."'");
  56. $this->tmp = $tmp;
  57. return true;
  58. }
  59. function logout() {
  60. global $db;
  61. if (!isset($_SESSION)) session_start();
  62. if (isset($_SESSION)) session_destroy();
  63. if (isset($_COOKIE['PRTCookie1']))
  64. {
  65. $cook=$_COOKIE['PRTCookie1'];
  66. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['temp']."='0' WHERE ".$db['users']['temp']."='$cook'");
  67. setcookie("PRTCookie1","",time()-3600);
  68. $this->tmp = 0;
  69. }
  70. }
  71. function name()
  72. {
  73. return $this->name;
  74. }
  75. function email()
  76. {
  77. return $this->email;
  78. }
  79. function getSkinFName() {
  80. global $config;
  81. return $config['mcsrv_dir'].$config['skins_dir'].$this->name.'.png';
  82. }
  83. function getCloakFName() {
  84. global $config;
  85. return $config['mcsrv_dir'].$config['cloaks_dir'].$this->name.'.png';
  86. }
  87. function deleteSkin() {
  88. if (file_exists($this->getSkinFName())) {
  89. unlink($this->getSkinFName());
  90. $this->deleteBuffer();
  91. }
  92. }
  93. function deleteCloak() {
  94. if (file_exists($this->getCloakFName())) {
  95. unlink($this->getCloakFName());
  96. $this->deleteBuffer();
  97. }
  98. }
  99. function deleteBuffer() {
  100. global $config;
  101. $mini = $config['mcsrv_dir'].'tmp/skin_buffer/'.$this->name.'_Mini.png';
  102. $skin = $config['mcsrv_dir'].'tmp/skin_buffer/'.$this->name.'.png';
  103. if (file_exists($mini))
  104. unlink($mini);
  105. if (file_exists($skin))
  106. unlink($skin);
  107. }
  108. function changeName($newname) {
  109. global $db,$config;
  110. if (!$this->id)
  111. return false;
  112. $newname = addslashes(trim($newname));
  113. if (!preg_match("/^[a-zA-Z0-9_-]+$/", $newname))
  114. return false;
  115. $result = mysql_query("SELECT ".$db['users']['username']." FROM ".$db['tables']['users']." WHERE ".$db['users']['username']."='$newname'");
  116. if (mysql_num_rows($result))
  117. return false;
  118. if ((strlen($newname) < 4) or (strlen($newname) > 8))
  119. return false;
  120. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['username']."='$newname' WHERE ".$db['users']['username']."='".$this->name."'");
  121. if (!empty($_SESSION['user_name']) and $_SESSION['user_name'] == $this->name)
  122. $_SESSION['user_name'] = $newname;
  123. /* Переименование файла скина и плаща */
  124. $way_tmp_old = $config['mcsrv_dir'].$config['skins_dir'].$this->name.'.png';
  125. $way_tmp_new = $config['mcsrv_dir'].$config['skins_dir'].$newname.'.png';
  126. if (file_exists($way_tmp_old) and !file_exists($way_tmp_new))
  127. rename($way_tmp_old, $way_tmp_new);
  128. $way_tmp_old = $config['mcsrv_dir'].$config['skins_dir'].$this->name.'.png';
  129. $way_tmp_new = $config['mcsrv_dir'].$config['skins_dir'].$newname.'.png';
  130. if (file_exists($way_tmp_old) and !file_exists($way_tmp_new))
  131. rename($way_tmp_old, $way_tmp_new);
  132. $this->name = $newname;
  133. return true;
  134. }
  135. function changePassword($newpass,$pass = '',$check = false) {
  136. global $db;
  137. include_once('inc/pass.inc.php');
  138. if (!$this->id)
  139. return 0;
  140. if ($check) {
  141. if (strlen($newpass)<6)
  142. return 13;
  143. $result = mysql_query("SELECT ".$db['users']['password']." FROM ".$db['tables']['users']." WHERE ".$db['users']['username']."='".$this->name."'");
  144. $line = mysql_fetch_array( $result );
  145. if (!$line or !checkPass($line[$db['users']['password']],$pass))
  146. return 12;
  147. }
  148. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['password']."='".createPass($newpass)."' WHERE ".$db['users']['username']."='".$this->name."'");
  149. return 15;
  150. }
  151. function changeLvl($newlvl) {
  152. global $db;
  153. $newlvl = (int) $newlvl;
  154. if ($newlvl < 0 or $newlvl > 15)
  155. return false;
  156. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['level']."='$newlvl' WHERE ".$db['users']['username']."='".$this->name."'");
  157. return true;
  158. }
  159. function changeEmail($newemail) {
  160. global $db;
  161. if (!filter_var($newemail,FILTER_VALIDATE_EMAIL))
  162. return false;
  163. mysql_query("UPDATE ".$db['tables']['users']." SET ".$db['users']['email']."='$newemail' WHERE ".$db['users']['username']."='".$this->name."'");
  164. return true;
  165. }
  166. function id()
  167. {
  168. return $this->id;
  169. }
  170. function lvl()
  171. {
  172. return $this->lvl;
  173. }
  174. function tmp()
  175. {
  176. return $this->tmp;
  177. }
  178. function ip()
  179. {
  180. return $this->ip;
  181. }
  182. }