PageRenderTime 35ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/web/includes/CUserManager.php

https://gitlab.com/Rushaway/sourcebanspp
PHP | 332 lines | 187 code | 43 blank | 102 comment | 31 complexity | a2d54d23952b1696536e22c8b5ed898d MD5 | raw file
  1. <?php
  2. use Lcobucci\JWT\Token;
  3. /*************************************************************************
  4. This file is part of SourceBans++
  5. SourceBans++ (c) 2014-2019 by SourceBans++ Dev Team
  6. The SourceBans++ Web panel is licensed under a
  7. Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  8. You should have received a copy of the license along with this
  9. work. If not, see <http://creativecommons.org/licenses/by-nc-sa/3.0/>.
  10. This program is based off work covered by the following copyright(s):
  11. SourceBans 1.4.11
  12. Copyright © 2007-2014 SourceBans Team - Part of GameConnect
  13. Licensed under CC-BY-NC-SA 3.0
  14. Page: <http://www.sourcebans.net/> - <http://www.gameconnect.net/>
  15. *************************************************************************/
  16. class CUserManager
  17. {
  18. /**
  19. * @var int|mixed
  20. */
  21. private $aid = -1;
  22. /**
  23. * @var array
  24. */
  25. private $admins = array();
  26. /**
  27. * @var Database
  28. */
  29. private $dbh = null;
  30. /**
  31. * CUserManager constructor.
  32. * @param Token|false $token
  33. */
  34. public function __construct($token)
  35. {
  36. $this->dbh = new Database(DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS, DB_PREFIX, DB_CHARSET);
  37. $this->aid = ((bool)$token) ? $token->getClaim('aid') : -1;
  38. $this->GetUserArray($this->aid);
  39. }
  40. /**
  41. * Gets all user details from the database, saves them into
  42. * the admin array 'cache', and then returns the array
  43. *
  44. * @param int $aid the ID of admin to get info for.
  45. * @return array|false
  46. */
  47. public function GetUserArray($aid = null)
  48. {
  49. if (is_null($aid)) {
  50. $aid = $this->aid;
  51. }
  52. // Invalid aid
  53. if ($aid < 0 || empty($aid)) {
  54. return false;
  55. }
  56. // We already got the data from the DB, and its saved in the manager
  57. if (isset($this->admins[$aid]) && !empty($this->admins[$aid])) {
  58. return $this->admins[$aid];
  59. }
  60. // Not in the manager, so we need to get them from DB
  61. $this->dbh->query("SELECT adm.user user, adm.authid authid, adm.password password, adm.gid gid, adm.email email, adm.validate validate, adm.extraflags extraflags,
  62. adm.immunity admimmunity,sg.immunity sgimmunity, adm.srv_password srv_password, adm.srv_group srv_group, adm.srv_flags srv_flags,sg.flags sgflags,
  63. wg.flags wgflags, wg.name wgname, adm.lastvisit lastvisit
  64. FROM `:prefix_admins` AS adm
  65. LEFT JOIN `:prefix_groups` AS wg ON adm.gid = wg.gid
  66. LEFT JOIN `:prefix_srvgroups` AS sg ON adm.srv_group = sg.name
  67. WHERE adm.aid = :aid");
  68. $this->dbh->bind(':aid', $aid);
  69. $res = $this->dbh->single();
  70. if (!$res) {
  71. return false; // ohnoes some type of db error
  72. }
  73. $user = array();
  74. //$user['user'] = stripslashes($res[0]);
  75. $user['aid'] = $aid; //immediately obvious
  76. $user['user'] = $res['user'];
  77. $user['authid'] = $res['authid'];
  78. $user['password'] = $res['password'];
  79. $user['gid'] = $res['gid'];
  80. $user['email'] = $res['email'];
  81. $user['validate'] = $res['validate'];
  82. $user['extraflags'] = (intval($res['extraflags']) | intval($res['wgflags']));
  83. $user['srv_immunity'] = intval($res['sgimmunity']);
  84. if (intval($res['admimmunity']) > intval($res['sgimmunity'])) {
  85. $user['srv_immunity'] = intval($res['admimmunity']);
  86. }
  87. $user['srv_password'] = $res['srv_password'];
  88. $user['srv_groups'] = $res['srv_group'];
  89. $user['srv_flags'] = $res['srv_flags'] . $res['sgflags'];
  90. $user['group_name'] = $res['wgname'];
  91. $user['lastvisit'] = $res['lastvisit'];
  92. $this->admins[$aid] = $user;
  93. return $user;
  94. }
  95. /**
  96. * Will check to see if an admin has any of the flags given
  97. *
  98. * @param int $flags The flags to check for.
  99. * @param int $aid The user to check flags for.
  100. * @return bool
  101. */
  102. public function HasAccess($flags, $aid = null)
  103. {
  104. if (is_null($aid)) {
  105. $aid = $this->aid;
  106. }
  107. if (empty($flags) || $aid <= 0) {
  108. return false;
  109. }
  110. if (!isset($this->admins[$aid])) {
  111. $this->GetUserArray($aid);
  112. }
  113. if (is_numeric($flags)) {
  114. return ($this->admins[$aid]['extraflags'] & $flags) != 0 ? true : false;
  115. }
  116. for ($i=0; $i < strlen($this->admins[$aid]['srv_flags']); $i++) {
  117. for ($a=0; $a < strlen($flags); $a++) {
  118. if (strstr($this->admins[$aid]['srv_flags'][$i], $flags[$a])) {
  119. return true;
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * Gets a 'property' from the user array eg. 'authid'
  127. *
  128. * @param string $name
  129. * @param int $aid the ID of admin to get info for.
  130. * @return mixed.
  131. */
  132. public function GetProperty($name, $aid = null)
  133. {
  134. if (is_null($aid)) {
  135. $aid = $this->aid;
  136. }
  137. if (empty($name) || $aid < 0) {
  138. return false;
  139. }
  140. if (!isset($this->admins[$aid])) {
  141. $this->GetUserArray($aid);
  142. }
  143. return $this->admins[$aid][$name];
  144. }
  145. /**
  146. * @return bool
  147. */
  148. public function is_logged_in()
  149. {
  150. if ($this->aid != -1) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * @param null $aid
  157. * @return bool
  158. */
  159. public function is_admin($aid = null)
  160. {
  161. if (is_null($aid)) {
  162. $aid = $this->aid;
  163. }
  164. if ($this->HasAccess(ALL_WEB, $aid)) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * @return int|mixed
  171. */
  172. public function GetAid()
  173. {
  174. return $this->aid;
  175. }
  176. /**
  177. * @return array
  178. */
  179. public function GetAllAdmins()
  180. {
  181. $this->dbh->query('SELECT aid FROM `:prefix_admins`');
  182. $res = $this->dbh->resultset();
  183. foreach ($res as $admin) {
  184. $this->GetUserArray($admin['aid']);
  185. }
  186. return $this->admins;
  187. }
  188. /**
  189. * @param int|null $aid
  190. * @return bool|mixed
  191. */
  192. public function GetAdmin($aid = null)
  193. {
  194. if (is_null($aid)) {
  195. $aid = $this->aid;
  196. }
  197. if ($aid < 0 || !is_int($aid)) {
  198. return false;
  199. }
  200. if (!isset($this->admins[$aid])) {
  201. $this->GetUserArray($aid);
  202. }
  203. return $this->admins[$aid];
  204. }
  205. /**
  206. * @param string $name
  207. * @return bool
  208. */
  209. public function isNameTaken($name)
  210. {
  211. $this->dbh->query("SELECT 1 FROM `:prefix_admins` WHERE user = :user");
  212. $this->dbh->bind(':user', $name);
  213. $data = $this->dbh->single();
  214. return (bool)$data[1];
  215. }
  216. /**
  217. * @param string $steamid
  218. * @return bool
  219. */
  220. public function isSteamIDTaken($steamid)
  221. {
  222. $this->dbh->query("SELECT 1 FROM `:prefix_admins` WHERE authid = :steamid");
  223. $this->dbh->bind(':steamid', $steamid);
  224. $data = $this->dbh->single();
  225. return (bool)$data[1];
  226. }
  227. /**
  228. * @param string $email
  229. * @return bool
  230. */
  231. public function isEmailTaken($email)
  232. {
  233. $this->dbh->query("SELECT 1 FROM `:prefix_admins` WHERE email = :email");
  234. $this->dbh->bind(':email', $email);
  235. $data = $this->dbh->single();
  236. return (bool)$data[1];
  237. }
  238. /**
  239. * @param int $aid
  240. * @param string $pass
  241. * @return bool
  242. */
  243. public function isCurrentPasswordValid($aid, $pass)
  244. {
  245. $this->dbh->query("SELECT password FROM `:prefix_admins` WHERE aid = :aid");
  246. $this->dbh->bind(':aid', $aid);
  247. $hash = $this->dbh->single();
  248. return password_verify($pass, $hash['password']);
  249. }
  250. /**
  251. * @param string $name
  252. * @param string $steam
  253. * @param string $password
  254. * @param string $email
  255. * @param int $web_group
  256. * @param int $web_flags
  257. * @param string $srv_group
  258. * @param string $srv_flags
  259. * @param int $immunity
  260. * @param string $srv_password
  261. * @return int
  262. */
  263. public function AddAdmin($name, $steam, $password, $email, $web_group, $web_flags, $srv_group, $srv_flags, $immunity, $srv_password)
  264. {
  265. if (!empty($password) && strlen($password) < MIN_PASS_LENGTH) {
  266. throw new RuntimeException('Password must be at least ' . MIN_PASS_LENGTH . ' characters long.');
  267. }
  268. if (empty($password)) {
  269. throw new RuntimeException('Password cannot be empty.');
  270. }
  271. $this->dbh->query('INSERT INTO `:prefix_admins` (user, authid, password, gid, email, extraflags, immunity, srv_group, srv_flags, srv_password)
  272. VALUES (:user, :authid, :password, :gid, :email, :extraflags, :immunity, :srv_group, :srv_flags, :srv_password)');
  273. $this->dbh->bind(':user', $name);
  274. $this->dbh->bind(':authid', str_replace('STEAM_1', 'STEAM_0', $steam));
  275. $this->dbh->bind(':password', password_hash($password, PASSWORD_BCRYPT));
  276. $this->dbh->bind(':gid', $web_group);
  277. $this->dbh->bind(':email', $email);
  278. $this->dbh->bind(':extraflags', $web_flags);
  279. $this->dbh->bind(':immunity', $immunity);
  280. $this->dbh->bind(':srv_group', $srv_group);
  281. $this->dbh->bind(':srv_flags', $srv_flags);
  282. $this->dbh->bind(':srv_password', $srv_password);
  283. return ($this->dbh->execute()) ? (int)$this->dbh->lastInsertId() : -1;
  284. }
  285. }